selector.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { APP_STATE } from "../enums/app-state-enum";
  2. import { stages as Stages } from "../components/stages";
  3. import { state as State } from "../components/state";
  4. import { objects as Objects } from "../components/objects";
  5. class Selector {
  6. constructor() {
  7. this._objects = [];
  8. this._selectorPosStart;
  9. this._selectorPosNow;
  10. this._mode = "";
  11. this._stage = Stages.getCurrentStage();
  12. this._layer = Stages.getCurrentLayer();
  13. this._selectorRect = new Konva.Rect({
  14. x: 0,
  15. y: 0,
  16. width: 0,
  17. height: 0,
  18. stroke: "#33BCFF",
  19. dash: [2, 2]
  20. });
  21. }
  22. bootstrap() {
  23. this.configureSelectorEvents();
  24. this.addToLayer();
  25. }
  26. addToLayer() {
  27. this._selectorRect.listening(false);
  28. this._layer.add(this._selectorRect);
  29. }
  30. getSelectedObjects() {
  31. return this._objects;
  32. }
  33. startDragSelector(posIn) {
  34. this._selectorPosStart = { x: posIn.x, y: posIn.y };
  35. this._selectorPosNow = { x: posIn.x, y: posIn.y };
  36. }
  37. configureSelectorEvents() {
  38. var _this = this;
  39. this._stage.on("mousedown", function(e) {
  40. _this._mode = "drawing";
  41. _this.startDragSelector({ x: e.evt.layerX, y: e.evt.layerY });
  42. });
  43. this._stage.on("mousemove", function(e) {
  44. if (_this._mode === "drawing") {
  45. _this.updateDragSelector({ x: e.evt.layerX, y: e.evt.layerY });
  46. }
  47. });
  48. this._stage.on("mouseup", function(e) {
  49. _this._mode = "";
  50. _this._selectorRect.visible(false);
  51. _this._stage.draw();
  52. });
  53. }
  54. updateDragSelector(posIn) {
  55. if (State.getCurrentState() != APP_STATE.NONE) return;
  56. let currentObjects = Objects.get();
  57. let posRect = this.reverse(this._selectorPosStart, this._selectorPosNow);
  58. this._selectorPosNow = { x: posIn.x, y: posIn.y };
  59. this._selectorRect.x(posRect.x1);
  60. this._selectorRect.y(posRect.y1);
  61. this._selectorRect.width(posRect.x2 - posRect.x1);
  62. this._selectorRect.height(posRect.y2 - posRect.y1);
  63. this._selectorRect.visible(true);
  64. for (let i = 0; i < currentObjects.length; i = i + 1) {
  65. let object = currentObjects[i];
  66. if (object.children != undefined && object.children.length > 0) {
  67. for (let j = 0; j < object.children.length; j++) {
  68. this.style(object.children[j], this._selectorRect);
  69. }
  70. } else {
  71. this.style(object, this._selectorRect);
  72. }
  73. }
  74. this._stage.draw();
  75. }
  76. style(object, selectorRect) {
  77. if (object.attrs.selectable != undefined) {
  78. if (object.attrs.selectable == false) {
  79. return;
  80. }
  81. }
  82. if (this.hitCheck(object, selectorRect)) {
  83. object.stroke("#33BCFF");
  84. object.fill("#33BCFF");
  85. this.setObject(object);
  86. } else {
  87. if (object.attrs.style != undefined) {
  88. this.removeObject(object);
  89. object.stroke(object.attrs.style.stroke);
  90. object.fill(object.attrs.style.fill);
  91. } else {
  92. this.removeObject(object);
  93. object.stroke("black");
  94. }
  95. }
  96. }
  97. hitCheck(shape1, shape2) {
  98. let s1 = shape1.getClientRect();
  99. let s2 = shape2.getClientRect();
  100. let X = s1.x;
  101. let Y = s1.y;
  102. let A = s1.x + s1.width;
  103. let B = s1.y + s1.height;
  104. let X1 = s2.x;
  105. let A1 = s2.x + s2.width;
  106. let Y1 = s2.y;
  107. let B1 = s2.y + s2.height;
  108. if (A < X1 || A1 < X || B < Y1 || B1 < Y) {
  109. return false;
  110. } else {
  111. return true;
  112. }
  113. }
  114. setObject(object) {
  115. if (this._objects.includes(object)) return;
  116. this._objects.push(object);
  117. }
  118. removeObject(object) {
  119. if (this._objects.length == 0) return;
  120. this._objects.splice(this._objects.indexOf(object), 1);
  121. }
  122. reverse(r1, r2) {
  123. let r1x = r1.x,
  124. r1y = r1.y,
  125. r2x = r2.x,
  126. r2y = r2.y,
  127. d;
  128. if (r1x > r2x) {
  129. d = Math.abs(r1x - r2x);
  130. r1x = r2x;
  131. r2x = r1x + d;
  132. }
  133. if (r1y > r2y) {
  134. d = Math.abs(r1y - r2y);
  135. r1y = r2y;
  136. r2y = r1y + d;
  137. }
  138. return { x1: r1x, y1: r1y, x2: r2x, y2: r2y };
  139. }
  140. }
  141. export const selector = new Selector();