|
@@ -1,77 +1,125 @@
|
|
|
import { app as App } from "../app";
|
|
|
import { menu as Menu } from "../core/components/menu";
|
|
|
-export const line = (function() {
|
|
|
- let _tool = {};
|
|
|
- let _states = ["primeiro_ponto", "segundo_ponto"];
|
|
|
- let _state = undefined;
|
|
|
- let _points = [0, 0, 0, 0];
|
|
|
+import { selector as Selector } from "../core/components/selector";
|
|
|
+import { ELEMENTS_CLASS } from "../core/enums/elements-class-enum";
|
|
|
+export class Line {
|
|
|
+ constructor() {
|
|
|
+ this._states = ["primeiro_ponto", "segundo_ponto"];
|
|
|
+ this._state = undefined;
|
|
|
+ this._points = [0, 0, 0, 0];
|
|
|
+ this._tool = {
|
|
|
+ id: "line",
|
|
|
+ title: "Reta",
|
|
|
+ icon: "line",
|
|
|
+ click: this.click,
|
|
|
+ draw: this.draw,
|
|
|
+ points: this.points,
|
|
|
+ object: this
|
|
|
+ };
|
|
|
+ this.bootstrap();
|
|
|
+ }
|
|
|
|
|
|
- function _draw() {
|
|
|
- if (_state == undefined) {
|
|
|
- _state = _states[0];
|
|
|
+ draw(e) {
|
|
|
+ let _this = e;
|
|
|
+ if (e == undefined) {
|
|
|
+ _this = this;
|
|
|
+ }
|
|
|
+ if (_this._state == undefined) {
|
|
|
+ _this._state = _this._states[0];
|
|
|
App.setStatus("Selecione o primeiro ponto no canvas");
|
|
|
- } else if (_state == _states[0]) {
|
|
|
+ } else if (_this._state == _this._states[0]) {
|
|
|
let pos = App.pos();
|
|
|
- _points[0] = pos.x;
|
|
|
- _points[1] = pos.y;
|
|
|
- _state = _states[1];
|
|
|
+ _this._points[0] = pos.x;
|
|
|
+ _this._points[1] = pos.y;
|
|
|
+ _this._state = _this._states[1];
|
|
|
App.setStatus("Selecione o segundo ponto no canvas");
|
|
|
- } else if (_state == _states[1]) {
|
|
|
+ } else if (_this._state == _this._states[1]) {
|
|
|
let pos = App.pos();
|
|
|
- _points[2] = pos.x;
|
|
|
- _points[3] = pos.y;
|
|
|
- let p = _points.slice();
|
|
|
- let ln = new Konva.Line({
|
|
|
- points: p,
|
|
|
- stroke: "grey",
|
|
|
- strokeWidth: 2,
|
|
|
- lineJoin: "round",
|
|
|
- draggable: true,
|
|
|
- strokeScaleEnabled: false
|
|
|
- });
|
|
|
-
|
|
|
- let layer = App.currentLayer();
|
|
|
- layer.add(ln);
|
|
|
- App.stage.draw();
|
|
|
-
|
|
|
- _clearState();
|
|
|
- App.clearSelectedTool();
|
|
|
- App.setStatus("");
|
|
|
+ _this._points[2] = pos.x;
|
|
|
+ _this._points[3] = pos.y;
|
|
|
+ let p = _this._points.slice();
|
|
|
+ _this.drawPoint(p);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- function _bootstrap() {
|
|
|
- Menu.add(_tool);
|
|
|
- }
|
|
|
+ click(e) {
|
|
|
+ let _this = e;
|
|
|
+ if (e == undefined) {
|
|
|
+ _this = this;
|
|
|
+ }
|
|
|
+ if (_this._state == _this._states[0] || _this._state == _this._states[1]) {
|
|
|
+ App.clearSelectedTool();
|
|
|
+ _this.clearState();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ let objects = Selector.getPoints();
|
|
|
+ if (
|
|
|
+ _this._state == undefined &&
|
|
|
+ objects != undefined &&
|
|
|
+ objects.length > 0
|
|
|
+ ) {
|
|
|
+ let layer = App.currentLayer();
|
|
|
+ let p = [];
|
|
|
+ let ln = _this.createLine(p);
|
|
|
+ objects.forEach(object => {
|
|
|
+ object.attrs.connections.push(ln);
|
|
|
+ p.push(object.x());
|
|
|
+ p.push(object.y());
|
|
|
+ object.parent.on("dragmove", () => {
|
|
|
+ let p = [];
|
|
|
+ let objects = Selector.getPoints();
|
|
|
+ objects.forEach(object => {
|
|
|
+ p.push(object.x());
|
|
|
+ p.push(object.y());
|
|
|
+ });
|
|
|
+ object.attrs.connections[0].points(p.slice());
|
|
|
+ layer.batchDraw();
|
|
|
+ });
|
|
|
+ ln.points(p.slice());
|
|
|
+ layer.batchDraw();
|
|
|
+ });
|
|
|
|
|
|
- function _click() {
|
|
|
- if (_state == _states[0] || _state == _states[1]) {
|
|
|
+ _this.drawPoint(p);
|
|
|
App.clearSelectedTool();
|
|
|
- _clearState();
|
|
|
+ _this.clearState();
|
|
|
return;
|
|
|
}
|
|
|
- App.setSelectedTool(_tool);
|
|
|
- _state = _states[0];
|
|
|
+ App.setSelectedTool(_this._tool);
|
|
|
+ _this._state = _this._states[0];
|
|
|
App.setStatus("Selecione o primeiro ponto no canvas");
|
|
|
}
|
|
|
|
|
|
- function _clearState() {
|
|
|
- _state = undefined;
|
|
|
+ clearState() {
|
|
|
+ this._state = undefined;
|
|
|
}
|
|
|
|
|
|
- _tool = {
|
|
|
- id: "line",
|
|
|
- title: "Reta",
|
|
|
- icon: "line",
|
|
|
- click: _click,
|
|
|
- draw: _draw,
|
|
|
- points: _points
|
|
|
- };
|
|
|
+ drawPoint(p) {
|
|
|
+ let ln = this.createLine(p);
|
|
|
|
|
|
- _bootstrap();
|
|
|
+ let layer = App.currentLayer();
|
|
|
+ layer.add(ln);
|
|
|
+ App.stage.draw();
|
|
|
|
|
|
- return {
|
|
|
- draw: _draw,
|
|
|
- click: _click
|
|
|
- };
|
|
|
-})();
|
|
|
+ this.clearState();
|
|
|
+ App.clearSelectedTool();
|
|
|
+ App.setStatus("");
|
|
|
+ }
|
|
|
+
|
|
|
+ createLine(p) {
|
|
|
+ return new Konva.Line({
|
|
|
+ points: p,
|
|
|
+ stroke: "grey",
|
|
|
+ strokeWidth: 2,
|
|
|
+ lineJoin: "round",
|
|
|
+ draggable: true,
|
|
|
+ strokeScaleEnabled: false,
|
|
|
+ class: ELEMENTS_CLASS.LINE,
|
|
|
+ connections: []
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ bootstrap() {
|
|
|
+ Menu.add(this._tool);
|
|
|
+ }
|
|
|
+}
|
|
|
+export const line = new Line();
|