line-segment-drawer.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { Drawer } from "../../../core/drawers/drawer";
  2. import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
  3. import { label as Label } from "../../../component-registry/label";
  4. import { DrawerAggregator } from "../../../core/drawers/drawer-aggregator";
  5. import { selector as Selector } from "../../../core/application/selector";
  6. import { LineSegmentModel } from "../models/line-segment-model";
  7. import { PointDrawer } from "../../point-component/drawers/point-drawer";
  8. import { objects as Objects } from "../../../core/application/objects";
  9. import { app as App } from "../../../app";
  10. import { stageManager as StageManager } from "../../../core/application/stage-manager";
  11. const HOVER_STYLE = {
  12. fill: "#33BCFF",
  13. strokeWidth: 4,
  14. stroke: "#33BCFF"
  15. };
  16. const STYLE = {
  17. fill: "grey",
  18. strokeWidth: 2,
  19. stroke: "grey"
  20. };
  21. export class LineSegmentDrawer extends Drawer {
  22. static FIRST_POINT_STATE() {
  23. return "FIRST_POINT";
  24. }
  25. static SECOND_POINT_STATE() {
  26. return "SECOND_POINT";
  27. }
  28. constructor() {
  29. super();
  30. this.pointA;
  31. this.pointB;
  32. this.aggregatorA;
  33. this.aggregatorB;
  34. this.label;
  35. this.states = [
  36. LineSegmentDrawer.FIRST_POINT_STATE,
  37. LineSegmentDrawer.SECOND_POINT_STATE
  38. ];
  39. this.lineSegment;
  40. this.pointDrawer = new PointDrawer();
  41. super.setElementClass(ELEMENTS_CLASS.LINE_SEGMENT);
  42. }
  43. setPointA(point) {
  44. this.pointA = point;
  45. }
  46. setPointB(point) {
  47. this.pointB = point;
  48. }
  49. setAggregatorA(aggregator) {
  50. this.aggregatorA = aggregator;
  51. this.setPointA(aggregator.genericObject);
  52. }
  53. setAggregatorB(aggregator) {
  54. this.aggregatorB = aggregator;
  55. this.setPointB(aggregator.genericObject);
  56. }
  57. draw() {
  58. const points = Selector.getSelectedPoints();
  59. if (points == undefined || points.length == 0) {
  60. this.drawByState();
  61. }
  62. this.drawByPoints(points);
  63. }
  64. drawByState() {
  65. if (this.state == undefined) {
  66. super.setState(LineSegmentDrawer.FIRST_POINT_STATE);
  67. } else if (this.state == LineSegmentDrawer.FIRST_POINT_STATE) {
  68. const aggregator = this.pointDrawer.drawPoint();
  69. this.setAggregatorA(aggregator);
  70. super.setState(LineSegmentDrawer.SECOND_POINT_STATE);
  71. } else if (this.state == LineSegmentDrawer.SECOND_POINT_STATE) {
  72. const aggregator = this.pointDrawer.drawPoint();
  73. this.setAggregatorB(aggregator);
  74. this.drawByPoints(
  75. [this.pointA, this.pointB],
  76. [this.aggregatorA, this.aggregatorB]
  77. );
  78. super.setState(undefined);
  79. }
  80. }
  81. drawByPoints(points, aggregators) {
  82. if (points == undefined || points.length < 1) return;
  83. this.setPointA(points[0]);
  84. this.setPointB(points[1]);
  85. if (aggregators == undefined)
  86. aggregators = Selector.getSelectedPointsAggregators();
  87. else {
  88. aggregators = [
  89. Objects.getByGenericObject(this.pointA)[0],
  90. Objects.getByGenericObject(this.pointB)[0]
  91. ];
  92. }
  93. this.label = Label.draw();
  94. this.lineSegment = new LineSegmentModel(
  95. this.pointA,
  96. this.pointB,
  97. this.label
  98. );
  99. const konvaObject = LineSegmentDrawer.getKonvaLine(
  100. this.pointA,
  101. this.pointB
  102. );
  103. super.setKonvaObject(konvaObject);
  104. const aggregator = new DrawerAggregator(
  105. this,
  106. this.lineSegment,
  107. this.konvaObject,
  108. ELEMENTS_CLASS.LINE_SEGMENT
  109. );
  110. super.addAggregator(aggregator);
  111. aggregators[1].addAggregator(aggregator);
  112. aggregators[0].addAggregator(aggregator);
  113. Drawer.drawObject(this.konvaObject);
  114. this.konvaObject.zIndex(0);
  115. super.batchDraw();
  116. }
  117. update(aggregator) {
  118. const pointA = aggregator.genericObject.pointA;
  119. const pointB = aggregator.genericObject.pointB;
  120. aggregator.konvaObject.points([
  121. pointA.posX,
  122. pointA.posY,
  123. pointB.posX,
  124. pointB.posY
  125. ]);
  126. super.batchDraw();
  127. }
  128. insertPoint(aggregator) {
  129. const pointA = aggregator.genericObject.pointA;
  130. const pointB = aggregator.genericObject.pointB;
  131. const pointCAggregator = this.pointDrawer.drawPoint();
  132. const pointC = pointCAggregator.genericObject;
  133. aggregator.konvaObject.points([
  134. pointA.posX,
  135. pointA.posY,
  136. pointB.posX,
  137. pointB.posY,
  138. pointC.posX,
  139. pointC.posY
  140. ]);
  141. super.batchDraw();
  142. }
  143. static getKonvaLine(pointA, pointB, useLabel) {
  144. const points = [pointA.posX, pointA.posY, pointB.posX, pointB.posY];
  145. const line = new Konva.Line({
  146. points: points,
  147. stroke: "grey",
  148. strokeWidth: 2,
  149. lineJoin: "round",
  150. draggable: false,
  151. strokeScaleEnabled: false,
  152. class: ELEMENTS_CLASS.LINE_SEGMENT,
  153. connections: [],
  154. index: 0,
  155. selectable: false,
  156. draggable: false
  157. });
  158. LineSegmentDrawer.configureLineEvents(line);
  159. return line;
  160. }
  161. static configureLineEvents(line) {
  162. line.on("mouseover", function() {
  163. const selectedTool = App.getSelectedTool();
  164. if (
  165. selectedTool != undefined &&
  166. selectedTool.drawer != undefined &&
  167. selectedTool.drawer.elementClass == ELEMENTS_CLASS.INTERSECTION_POINT
  168. ) {
  169. this.strokeWidth(HOVER_STYLE.strokeWidth);
  170. this.stroke(HOVER_STYLE.stroke);
  171. StageManager.getCurrentKonvaStage().draw();
  172. }
  173. });
  174. line.on("mouseout", function() {
  175. this.strokeWidth(STYLE.strokeWidth);
  176. this.stroke(STYLE.stroke);
  177. StageManager.getCurrentKonvaStage().draw();
  178. });
  179. return line;
  180. }
  181. static drawKonvaLine(pointA, pointB) {
  182. const line = LineSegmentDrawer.getKonvaLine(pointA, pointB);
  183. return line;
  184. }
  185. }