123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- import { stageManager } from "../../../core/application/stage-manager";
- import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
- import { label as Label } from "../../../component-registry/label";
- import { LineModel } from "../models/line-model";
- import { objects as Objects } from "../../../core/application/objects";
- import { SelectableDrawer } from "../../../core/drawers/selectable-drawer";
- import { LineSegmentDrawer } from "../../line-segment-component/drawers/line-segment-drawer";
- import { DrawerAggregator } from "../../../core/drawers/drawer-aggregator";
- import { LineSegmentModel } from "../../line-segment-component/models/line-segment-model";
- export class LineDrawer extends LineSegmentDrawer {
- constructor () {
- super();
- this.setElementClass(ELEMENTS_CLASS.LINE);
- }
- drawByStates (konvaObject) {
- let aggregator = undefined;
- if (konvaObject != undefined) {
- aggregator = Objects.getByKonvaObject(konvaObject)[0];
- }
- if (this.state == undefined) {
- super.setState(LineDrawer.FIRST_POINT_STATE);
- } else if (this.state == LineDrawer.FIRST_POINT_STATE) {
- aggregator = aggregator != undefined ? aggregator : this.pointDrawer.drawPoint();
- this.setAggregatorA(aggregator);
- super.setState(LineDrawer.SECOND_POINT_STATE);
- } else if (this.state == LineDrawer.SECOND_POINT_STATE) {
- aggregator = aggregator != undefined ? aggregator : this.pointDrawer.drawPoint();
- this.setAggregatorB(aggregator);
- this.drawByPoints([this.pointA, this.pointB], [this.aggregatorA, this.aggregatorB]);
- super.setState(LineDrawer.FIRST_POINT_STATE);
- }
- }
- drawByLineSegment (lineSegment) {
- this.lineSegment = lineSegment;
- const group = SelectableDrawer.getKonvaGroup(false);
- const text = LineDrawer.getKonvaText(lineSegment, lineSegment.label);
- group.add(text);
- const konvaObject = LineDrawer.getKonvaLine(lineSegment.pointA, lineSegment.pointB);
- group.add(konvaObject);
- super.setKonvaObject(group);
- const aggregator = new DrawerAggregator(this, this.lineSegment, group, ELEMENTS_CLASS.LINE);
- super.addAggregator(aggregator);
- const aggregators = this.resolveAggregators([this.lineSegment.pointA, this.lineSegment.pointB], undefined);
- aggregators[1].addAggregator(aggregator);
- aggregators[0].addAggregator(aggregator);
-
-
-
- SelectableDrawer.drawObject(this.konvaObject);
- this.konvaObject.zIndex(1);
-
- this.update(aggregator,null);
- SelectableDrawer.setMaxIndex(aggregators[0].konvaObject);
- SelectableDrawer.setMaxIndex(aggregators[1].konvaObject);
- }
-
- drawByPoints (points, aggregators) {
- if (points == undefined || points.length < 1) return;
- this.setPointA(points[0]);
- this.setPointB(points[1]);
- aggregators = this.resolveAggregators(points, aggregators, true);
- this.label = Label.draw(true);
- this.lineSegment = new LineModel(this.pointA, this.pointB, this.label);
-
-
-
- this.drawByLineSegment(this.lineSegment);
- this.reset();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- update (aggregator, e) {
- if (!aggregator.visible) return;
- const pointA = aggregator.genericObject.pointA;
- const pointB = aggregator.genericObject.pointB;
- const Ax = pointA.posX, Ay = pointA.posY, Bx = pointB.posX, By = pointB.posY;
- const dx = Bx-Ax, dy = By-Ay;
- const size = stageManager.getSize();
- const W = size.w;
- const H = size.h;
- var Ix, Iy, Ex, Ey;
- var Lg, Rg, Ug, Dg;
-
- if (dx === 0) {
-
- Ug = -Ay / dy;
- Dg = (H-Ay) / dy;
-
- const ming = Math.min(Ug, Dg);
- const maxg = Math.max(Ug, Dg);
-
- Ix = Ax; Lg = 0;
- Iy = Ay + ming * dy;
- Ex = Ax; Rg = 0;
- Ey = Ay + maxg * dy;
- }
- else {
-
- Lg = -Ax / dx;
- Rg = (W-Ax) / dx;
- Ug = -Ay / dy;
- Dg = (H-Ay) / dy;
-
- var ming, maxg;
- if (dx>0) {
- if (Lg<Dg) ming = Dg;
- else ming = Lg;
- if (Rg>Ug) maxg = Ug;
- else maxg = Rg;
- }
- else {
- if (Lg>Ug) ming = Ug;
- else ming = Lg;
- if (Rg<Dg) maxg = Dg;
- else maxg = Rg;
- }
-
- Ix = Ax + ming * dx;
- Iy = Ay + ming * dy;
- Ex = Ax + maxg * dx;
- Ey = Ay + maxg * dy;
- }
-
- const points = [Ix, Iy, Ex, Ey];
-
-
- const pos = aggregator.genericObject.getMiddlePoint();
- aggregator.konvaObject.children[0].x(pos.posX);
- aggregator.konvaObject.children[0].y(pos.posY - 20);
- aggregator.konvaObject.children[1].points(points);
- super.batchDraw();
- }
- static getKonvaLine (pointA, pointB, useLabel) {
- const xA = pointA.posX, xB = pointB.posX,
- yA = pointA.posY, yB = pointB.posY;
- const slope = (yB - yA) / (xB - xA);
- const linearCoefficient = (yA * xB - yB * xA) / (xB - xA);
- const y = linearCoefficient;
- const x = -linearCoefficient / slope;
- const points = [x, 0, 0, y];
- const line = new Konva.Line({
- points: points,
- stroke: "grey",
- strokeWidth: 2,
- lineJoin: "round",
- draggable: false,
- strokeScaleEnabled: false,
- class: ELEMENTS_CLASS.LINE,
- connections: [],
- index: 1,
- selectable: false,
- draggable: false,
- style: { stroke: "grey", fill: "grey" }
- });
- SelectableDrawer.setSelectableIfIntersectionChanged(line);
- return line;
- }
- static drawKonvaLine (pointA, pointB) {
- const line = LineDrawer.getKonvaLine(pointA, pointB);
- return line;
- }
- }
|