"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SLineItem = void 0; const lib_1 = require("@persagy-web/draw/lib"); const base_1 = require("@persagy-web/base"); const SMathUtil_1 = require("../utils/SMathUtil"); const __1 = require(".."); const lib_2 = require("@persagy-web/graph/lib"); class SLineItem extends lib_2.SGraphItem { constructor(parent, l) { super(parent); this.minX = Number.MAX_SAFE_INTEGER; this.maxX = Number.MIN_SAFE_INTEGER; this.minY = Number.MAX_SAFE_INTEGER; this.maxY = Number.MIN_SAFE_INTEGER; this._line = []; this._status = __1.SItemStatus.Normal; this._strokeColor = lib_1.SColor.Black; this._lineStyle = lib_2.SLineStyle.Solid; this._fillColor = lib_1.SColor.White; this._activeFillColor = new lib_1.SColor("#2196f3"); this._lineWidth = 1; this.dis = 5; this.sceneDis = 5; this.curIndex = -1; this.curPoint = null; this.undoStack = new base_1.SUndoStack(); if (l) { if (l instanceof lib_1.SPoint) { this.line.push(l); } else { this.line = l; } } else { this.line = []; } } get line() { return this._line; } set line(arr) { this._line = arr; this.update(); } get status() { return this._status; } set status(v) { this._status = v; this.undoStack.clear(); this.update(); } get strokeColor() { return this._strokeColor; } set strokeColor(v) { this._strokeColor = v; this.update(); } get lineStyle() { return this._lineStyle; } set lineStyle(v) { this._lineStyle = v; this.update(); } get fillColor() { return this._fillColor; } set fillColor(v) { this._fillColor = v; this.update(); } get activeFillColor() { return this._activeFillColor; } set activeFillColor(v) { this._activeFillColor = v; this.update(); } get lineWidth() { return this._lineWidth; } set lineWidth(v) { this._lineWidth = v; this.update(); } addPoint(p) { if (this.line.length < 2) { this.line.push(p); this.recordAction(lib_2.SGraphPointListInsert, [this.line, p]); } else { this.line[1] = p; this.recordAction(lib_2.SGraphPointListInsert, [this.line, p, 1]); this.status = __1.SItemStatus.Normal; this.releaseItem(); this.$emit("finishCreated"); } this.update(); } onDoubleClick(event) { if (this.status == __1.SItemStatus.Normal) { this.status = __1.SItemStatus.Edit; this.grabItem(this); } else if (this.status == __1.SItemStatus.Edit) { this.status = __1.SItemStatus.Normal; this.releaseItem(); } this.update(); return true; } onMouseDown(event) { this.curIndex = -1; this.curPoint = null; if (event.shiftKey) { event = this.compare(event); } if (event.buttons == base_1.SMouseButton.LeftButton) { if (this.status == __1.SItemStatus.Normal) { return super.onMouseDown(event); } else if (this.status == __1.SItemStatus.Edit) { this.findNearestPoint(new lib_1.SPoint(event.x, event.y)); } else if (this.status == __1.SItemStatus.Create) { this.addPoint(new lib_1.SPoint(event.x, event.y)); return true; } } return true; } onMouseUp(event) { if (this.status == __1.SItemStatus.Edit) { if (this.curIndex > -1) { const p = new lib_1.SPoint(this.line[this.curIndex].x, this.line[this.curIndex].y); this.recordAction(lib_2.SGraphPointListUpdate, [ this.line, this.curPoint, p, this.curIndex ]); } } else if (this.status == __1.SItemStatus.Normal) { this.moveToOrigin(this.x, this.y); return super.onMouseUp(event); } this.curIndex = -1; this.curPoint = null; return true; } onMouseMove(event) { if (event.shiftKey) { event = this.compare(event); } if (this.status == __1.SItemStatus.Create) { if (this.line[0] instanceof lib_1.SPoint) { this.line[1] = new lib_1.SPoint(event.x, event.y); } } else if (this.status == __1.SItemStatus.Edit) { if (-1 != this.curIndex) { this.line[this.curIndex].x = event.x; this.line[this.curIndex].y = event.y; } } else { return super.onMouseMove(event); } this.update(); return true; } findNearestPoint(p) { let len = this.sceneDis; for (let i = 0; i < this.line.length; i++) { let dis = SMathUtil_1.SMathUtil.pointDistance(p.x, p.y, this.line[i].x, this.line[i].y); if (dis < len) { len = dis; this.curIndex = i; this.curPoint = new lib_1.SPoint(this.line[this.curIndex]); } } } recordAction(SGraphCommand, any) { const command = new SGraphCommand(this.scene, this, ...any); this.undoStack.push(command); } moveToOrigin(x, y) { super.moveToOrigin(x, y); this.line = this.line.map(t => { t.x = t.x + x; t.y = t.y + y; return t; }); this.x = 0; this.y = 0; } compare(event) { if (this.line.length) { let last = new lib_1.SPoint(event.x, event.y); if (this.status == __1.SItemStatus.Create) { last = this.line[0]; } else if (this.status == __1.SItemStatus.Edit) { if (this.curIndex == 1) { last = this.line[0]; } else if (this.curIndex == 0) { last = this.line[1]; } } const dx = Math.abs(event.x - last.x); const dy = Math.abs(event.y - last.y); if (dy > dx) { event.x = last.x; } else { event.y = last.y; } } return event; } contains(x, y) { if (this.line.length == 2) { let p = new lib_1.SPoint(x, y); if (SMathUtil_1.SMathUtil.pointToLine(p, new lib_1.SLine(this.line[0], this.line[1])) .MinDis < this.dis) { return true; } } return false; } undo() { if (this.status != __1.SItemStatus.Normal) { this.undoStack.undo(); } } redo() { if (this.status != __1.SItemStatus.Normal) { this.undoStack.redo(); } } cancelOperate() { if (this.status == __1.SItemStatus.Create) { this.parent = null; this.releaseItem(); } else if (this.status == __1.SItemStatus.Edit) { this.status = __1.SItemStatus.Normal; this.releaseItem(); } } boundingRect() { if (this.line.length) { this.minX = this.line[0].x; this.maxX = this.line[0].x; this.minY = this.line[0].y; this.maxY = this.line[0].y; this.line.forEach(it => { let x = it.x, y = it.y; if (x < this.minX) { this.minX = x; } if (y < this.minY) { this.minY = y; } if (x > this.maxX) { this.maxX = x; } if (y > this.maxY) { this.maxY = y; } }); } return new lib_1.SRect(this.minX, this.minY, this.maxX - this.minX, this.maxY - this.minY); } onDraw(painter) { this.sceneDis = painter.toPx(this.dis); painter.pen.lineWidth = painter.toPx(this.lineWidth); painter.pen.color = this.strokeColor; if (this.line.length == 2) { painter.pen.color = new lib_1.SColor(this.strokeColor); if (this.lineStyle == lib_2.SLineStyle.Dashed) { painter.pen.lineDash = [ painter.toPx(this.lineWidth * 3), painter.toPx(this.lineWidth * 7) ]; } else if (this.lineStyle == lib_2.SLineStyle.Dotted) { painter.pen.lineDash = [ painter.toPx(this.lineWidth), painter.toPx(this.lineWidth) ]; } if (this.selected && this.status == __1.SItemStatus.Normal) { painter.pen.lineWidth = painter.toPx(this.lineWidth * 2); painter.shadow.shadowBlur = 10; painter.shadow.shadowColor = new lib_1.SColor(`#00000033`); painter.shadow.shadowOffsetX = 5; painter.shadow.shadowOffsetY = 5; } painter.drawLine(this.line[0], this.line[1]); if (this.status == __1.SItemStatus.Edit || this.status == __1.SItemStatus.Create) { this.line.forEach((p, i) => { painter.brush.color = this.fillColor; if (i == this.curIndex) { painter.brush.color = this.activeFillColor; } painter.drawCircle(p.x, p.y, painter.toPx(5)); }); } } else if (this.line.length == 1) { if (this.status == __1.SItemStatus.Edit || this.status == __1.SItemStatus.Create) { painter.brush.color = this.fillColor; painter.drawCircle(this.line[0].x, this.line[0].y, painter.toPx(5)); } } } } exports.SLineItem = SLineItem;