line-segment.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { app as App } from "../app";
  2. import { menu as Menu } from "../core/components/menu";
  3. import { point as Point } from "./point";
  4. import { selector as Selector } from "../core/components/selector";
  5. import { ELEMENTS_CLASS } from "../core/enums/elements-class-enum";
  6. export class LineSegment {
  7. static get STATE_PRIMEIRO_PONTO () {
  8. return "primeiro_ponto";
  9. }
  10. static get STATE_SEGUNDO_PONTO () {
  11. return "segundo_ponto";
  12. }
  13. constructor() {
  14. this._state = undefined;
  15. this._points = new Set();
  16. this._tool = {
  17. id: "line-segment",
  18. title: "Reta",
  19. icon: "line-segment",
  20. click: this.click,
  21. draw: this.draw,
  22. points: this.points,
  23. object: this
  24. };
  25. this.bootstrap();
  26. }
  27. draw (event) {
  28. console.log("chamou o método draw()");
  29. console.log("evento: ", event);
  30. let _this = this;
  31. if(event != null) {
  32. _this = event;
  33. }
  34. if(_this._state == null) {
  35. _this._state = LineSegment.STATE_PRIMEIRO_PONTO;
  36. _this._points.clear();
  37. App.setStatus("Selecione o primeiro ponto no canvas");
  38. } else if (_this._state === LineSegment.STATE_PRIMEIRO_PONTO) {
  39. const pos = App.pos();
  40. _this._points.add({x: pos.x, y: pos.y});
  41. _this._state = LineSegment.STATE_SEGUNDO_PONTO;
  42. } else if (_this._state === LineSegment.STATE_SEGUNDO_PONTO) {
  43. const pos = App.pos();
  44. _this._points.add({x: pos.x, y: pos.y});
  45. _this.drawPoint(_this._points);
  46. _this._points.clear();
  47. _this._state = LineSegment.STATE_PRIMEIRO_PONTO;
  48. }
  49. // let _this = e;
  50. // if (e == undefined) {
  51. // _this = this;
  52. // }
  53. // if (_this._state == undefined) {
  54. // _this._state = _this._states[0];
  55. // App.setStatus("Selecione o primeiro ponto no canvas");
  56. // } else if (_this._state == _this._states[0]) {
  57. // let pos = App.pos();
  58. // _this._points[0] = pos.x;
  59. // _this._points[1] = pos.y;
  60. // _this._state = _this._states[1];
  61. // App.setStatus("Selecione o segundo ponto no canvas");
  62. // } else if (_this._state == _this._states[1]) {
  63. // let pos = App.pos();
  64. // _this._points[2] = pos.x;
  65. // _this._points[3] = pos.y;
  66. // let p = _this._points.slice();
  67. // _this.drawPoint(p);
  68. // }
  69. }
  70. click(e) {
  71. console.log("chamou o método click()");
  72. let _this = e;
  73. if (e == undefined) {
  74. _this = this;
  75. }
  76. if (_this._state == LineSegment.STATE_PRIMEIRO_PONTO ||
  77. _this._state == LineSegment.STATE_SEGUNDO_PONTO) {
  78. App.clearSelectedTool();
  79. _this.clearState();
  80. return;
  81. }
  82. const objects = Selector.getPoints();
  83. if (
  84. _this._state == undefined &&
  85. objects != undefined &&
  86. objects.length > 0
  87. ) {
  88. const layer = App.currentLayer();
  89. const point_groups = new Set(objects.map(object => object.parent));
  90. const points_xy = objects.reduce((prev, next) => prev.concat([next.x(), next.y()]), []);
  91. let ln = _this.createLine(points_xy);
  92. _this.attachPoints(ln, point_groups);
  93. layer.add(ln);
  94. App.stage.draw();
  95. App.clearSelectedTool();
  96. _this.clearState();
  97. return;
  98. }
  99. App.setSelectedTool(_this._tool);
  100. _this._state = LineSegment.STATE_PRIMEIRO_PONTO;
  101. App.setStatus("Selecione o primeiro ponto no canvas");
  102. }
  103. clearState() {
  104. this._state = undefined;
  105. this._points.clear();
  106. }
  107. drawPoint (p) {
  108. const points = Array.from(p).map(xy => {
  109. return Point.drawPoint(xy.x, xy.y, true);
  110. });
  111. const points_xy = points.map( group => this.getPoint(group))
  112. .reduce((prev, next) => prev.concat([next.x(), next.y()]), []);
  113. let ln = this.createLine(points_xy);
  114. let layer = App.currentLayer();
  115. this.attachPoints(ln, points);
  116. layer.add(ln);
  117. App.stage.draw();
  118. //this.clearState();
  119. //App.clearSelectedTool();
  120. //App.setStatus("");
  121. return ln;
  122. }
  123. createLine (points) {
  124. return new Konva.Line({
  125. points: points,
  126. stroke: "grey",
  127. strokeWidth: 2,
  128. lineJoin: "round",
  129. draggable: false,
  130. strokeScaleEnabled: false,
  131. class: ELEMENTS_CLASS.LINE_SEGMENT,
  132. connections: []
  133. });
  134. }
  135. getPoint (group) {
  136. const point_collection = group.find(node => {
  137. return node.getClassName() === 'Circle';
  138. });
  139. return point_collection.toArray()[0];
  140. }
  141. attachPoints (line, points) {
  142. line._parents = []
  143. points.forEach( (p, idx)=> {
  144. line._parents.push(p);
  145. const point = this.getPoint(p);
  146. point._name = idx;
  147. if(!p._parents) {
  148. p._children = [];
  149. }
  150. p._children.push(line);
  151. p.on('dragmove', () => {
  152. App.clearSelectedTool();
  153. this.clearState();
  154. const current_points = line.points();
  155. const new_point = this.getPoint(p);
  156. const new_x = new_point.x() + p.x();
  157. const new_y = new_point.y() + p.y();
  158. if(new_point._name == 0) {
  159. // pos 0 ,1
  160. current_points[0] = new_x;
  161. current_points[1] = new_y;
  162. } else {
  163. current_points[2] = new_x;
  164. current_points[3] = new_y;
  165. }
  166. line.points(current_points);
  167. //App.stage.draw();
  168. });
  169. })
  170. }
  171. bootstrap() {
  172. Menu.add(this._tool);
  173. }
  174. }
  175. export const line_segment = new LineSegment();