line-segment-drawer.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(LineSegmentDrawer.FIRST_POINT_STATE);
  79. }
  80. }
  81. drawByPoints(points, aggregators) {
  82. console.info("points", points);
  83. if (points == undefined || points.length < 1) return;
  84. this.setPointA(points[0]);
  85. this.setPointB(points[1]);
  86. if (aggregators == undefined)
  87. aggregators = Selector.getSelectedPointsAggregators();
  88. else {
  89. aggregators = [
  90. Objects.getByGenericObject(this.pointA)[0],
  91. Objects.getByGenericObject(this.pointB)[0]
  92. ];
  93. }
  94. this.label = Label.draw();
  95. this.lineSegment = new LineSegmentModel(
  96. this.pointA,
  97. this.pointB,
  98. this.label
  99. );
  100. const konvaObject = LineSegmentDrawer.getKonvaLine(
  101. this.pointA,
  102. this.pointB
  103. );
  104. super.setKonvaObject(konvaObject);
  105. const aggregator = new DrawerAggregator(
  106. this,
  107. this.lineSegment,
  108. this.konvaObject,
  109. ELEMENTS_CLASS.LINE_SEGMENT
  110. );
  111. super.addAggregator(aggregator);
  112. aggregators[1].addAggregator(aggregator);
  113. aggregators[0].addAggregator(aggregator);
  114. Drawer.drawObject(this.konvaObject);
  115. this.konvaObject.zIndex(1);
  116. super.batchDraw();
  117. this.clear();
  118. }
  119. update(aggregator, e) {
  120. const pointA = aggregator.genericObject.pointA;
  121. const pointB = aggregator.genericObject.pointB;
  122. aggregator.konvaObject.points([
  123. pointA.posX,
  124. pointA.posY,
  125. pointB.posX,
  126. pointB.posY
  127. ]);
  128. super.batchDraw();
  129. }
  130. insertPoint(aggregator) {
  131. const pointA = aggregator.genericObject.pointA;
  132. const pointB = aggregator.genericObject.pointB;
  133. const pointCAggregator = this.pointDrawer.drawPoint();
  134. const pointC = pointCAggregator.genericObject;
  135. aggregator.konvaObject.points([
  136. pointA.posX,
  137. pointA.posY,
  138. pointB.posX,
  139. pointB.posY,
  140. pointC.posX,
  141. pointC.posY
  142. ]);
  143. super.batchDraw();
  144. }
  145. static getKonvaLine(pointA, pointB, useLabel) {
  146. const points = [pointA.posX, pointA.posY, pointB.posX, pointB.posY];
  147. const line = new Konva.Line({
  148. points: points,
  149. stroke: "grey",
  150. strokeWidth: 2,
  151. lineJoin: "round",
  152. draggable: false,
  153. strokeScaleEnabled: false,
  154. class: ELEMENTS_CLASS.LINE_SEGMENT,
  155. connections: [],
  156. index: 1,
  157. selectable: false,
  158. draggable: false
  159. });
  160. LineSegmentDrawer.configureLineEvents(line);
  161. return line;
  162. }
  163. static configureLineEvents(line) {
  164. line.on("mouseover", function () {
  165. const selectedTool = App.getSelectedTool();
  166. if (
  167. selectedTool != undefined &&
  168. selectedTool.drawer != undefined &&
  169. selectedTool.drawer.elementClass == ELEMENTS_CLASS.INTERSECTION_POINT
  170. ) {
  171. this.strokeWidth(HOVER_STYLE.strokeWidth);
  172. this.stroke(HOVER_STYLE.stroke);
  173. StageManager.getCurrentKonvaStage().draw();
  174. }
  175. });
  176. line.on("mouseout", function () {
  177. this.strokeWidth(STYLE.strokeWidth);
  178. this.stroke(STYLE.stroke);
  179. StageManager.getCurrentKonvaStage().draw();
  180. });
  181. return line;
  182. }
  183. static drawKonvaLine(pointA, pointB) {
  184. const line = LineSegmentDrawer.getKonvaLine(pointA, pointB);
  185. return line;
  186. }
  187. }