"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SPolylineItem = void 0; const draw_1 = require("@persagy-web/draw"); const base_1 = require("@persagy-web/base"); const __1 = require(".."); const SMathUtil_1 = require("../utils/SMathUtil"); const lib_1 = require("@persagy-web/graph/lib"); class SPolylineItem extends lib_1.SGraphItem { constructor(parent, list) { 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.pointList = []; this._status = __1.SItemStatus.Normal; this.lastPoint = null; this._strokeColor = draw_1.SColor.Black; this._fillColor = new draw_1.SColor("#2196f3"); this._lineWidth = 1; this.dis = 10; this.sceneDis = 10; this.curIndex = -1; this.curPoint = null; this.undoStack = new base_1.SUndoStack(); if (list instanceof draw_1.SPoint) { this.pointList.push(list); } else { this.pointList = list; } } 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 fillColor() { return this._fillColor; } set fillColor(v) { this._fillColor = v; this.update(); } get lineWidth() { return this._lineWidth; } set lineWidth(v) { this._lineWidth = v; this.update(); } addPoint(p, index) { if (index && this.canHandle(index)) { this.pointList.splice(index, 0, p); this.recordAction(lib_1.SGraphPointListInsert, [ this.pointList, p, index ]); } else { this.pointList.push(p); this.recordAction(lib_1.SGraphPointListInsert, [this.pointList, p]); } this.update(); } canHandle(index) { return index >= 0 && index <= this.pointList.length; } deletePoint(index) { if (this.canHandle(index) && this.pointList.length > 2) { const p = new draw_1.SPoint(this.pointList[this.curIndex].x, this.pointList[this.curIndex].y); this.pointList.splice(index, 1); this.recordAction(lib_1.SGraphPointListDelete, [ this.pointList, p, index ]); this.curIndex = -1; this.curPoint = null; this.update(); } } onMouseDown(event) { this.curIndex = -1; this.curPoint = null; if (event.shiftKey) { event = this.compare(event); } if (event.buttons == 1) { if (this.status == __1.SItemStatus.Create) { this.addPoint(new draw_1.SPoint(event.x, event.y)); return true; } else if (this.status == __1.SItemStatus.Edit) { this.findNearestPoint(new draw_1.SPoint(event.x, event.y)); if (this.curIndex < 0) { this.findAddPos(new draw_1.SPoint(event.x, event.y)); } if (event.altKey && this.canHandle(this.curIndex)) { this.deletePoint(this.curIndex); } this.update(); return true; } else { return super.onMouseDown(event); } } return super.onMouseDown(event); } onMouseMove(event) { if (event.shiftKey) { event = this.compare(event); } if (this.status == __1.SItemStatus.Create) { if (this.lastPoint) { this.lastPoint.x = event.x; this.lastPoint.y = event.y; } else { this.lastPoint = new draw_1.SPoint(event.x, event.y); } this.update(); return true; } else if (this.status == __1.SItemStatus.Edit) { if (event.buttons == 1) { if (this.canHandle(this.curIndex)) { this.pointList[this.curIndex].x = event.x; this.pointList[this.curIndex].y = event.y; } } this.update(); return true; } else { return super.onMouseMove(event); } } onMouseUp(event) { if (this.status == __1.SItemStatus.Edit) { if (this.curIndex > -1) { const p = new draw_1.SPoint(this.pointList[this.curIndex].x, this.pointList[this.curIndex].y); this.recordAction(lib_1.SGraphPointListUpdate, [ this.pointList, this.curPoint, p, this.curIndex ]); } } else if (this.status == __1.SItemStatus.Normal) { this.moveToOrigin(this.x, this.y); return super.onMouseUp(event); } return true; } 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(); } else if (this.status == __1.SItemStatus.Create) { if (this.pointList.length > 1) { this.status = __1.SItemStatus.Normal; this.releaseItem(); this.$emit("finishCreated"); } } this.$emit("onDoubleClick", event); return true; } onKeyUp(event) { if (event.keyCode == base_1.SKeyCode.Enter) { if (this.pointList.length > 1) { if (this.status == __1.SItemStatus.Create) { this.$emit("finishCreated"); } this.status = __1.SItemStatus.Normal; this.releaseItem(); } } if (event.keyCode == base_1.SKeyCode.Delete && this.status == __1.SItemStatus.Edit) { this.deletePoint(this.curIndex); } } moveToOrigin(x, y) { super.moveToOrigin(x, y); this.pointList = this.pointList.map(t => { t.x = t.x + x; t.y = t.y + y; return t; }); this.x = 0; this.y = 0; } findNearestPoint(p) { let len = this.sceneDis; for (let i = 0; i < this.pointList.length; i++) { let dis = SMathUtil_1.SMathUtil.pointDistance(p.x, p.y, this.pointList[i].x, this.pointList[i].y); if (dis < len) { len = dis; this.curIndex = i; this.curPoint = new draw_1.SPoint(this.pointList[this.curIndex].x, this.pointList[this.curIndex].y); } } } findAddPos(p) { let len = SMathUtil_1.SMathUtil.pointToLine(p, new draw_1.SLine(this.pointList[0], this.pointList[1])), index = 0; if (this.pointList.length > 2) { for (let i = 1; i < this.pointList.length - 1; i++) { let dis = SMathUtil_1.SMathUtil.pointToLine(p, new draw_1.SLine(this.pointList[i], this.pointList[i + 1])); if (dis.MinDis < len.MinDis) { len = dis; index = i; } } } if (len.MinDis < this.sceneDis) { if (len.Point) { this.addPoint(len.Point, index + 1); } } } compare(event) { if (this.pointList.length) { let last = new draw_1.SPoint(event.x, event.y); if (this.status == __1.SItemStatus.Create) { last = this.pointList[this.pointList.length - 1]; } else if (this.status == __1.SItemStatus.Edit) { if (this.curIndex > 1) { last = this.pointList[this.curIndex - 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; } recordAction(SGraphCommand, any) { const command = new SGraphCommand(this.scene, this, ...any); this.undoStack.push(command); } boundingRect() { if (this.pointList.length) { this.minX = this.pointList[0].x; this.maxX = this.pointList[0].x; this.minY = this.pointList[0].y; this.maxY = this.pointList[0].y; this.pointList.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 draw_1.SRect(this.minX, this.minY, this.maxX - this.minX, this.maxY - this.minY); } contains(x, y) { let p = new draw_1.SPoint(x, y); for (let i = 1; i < this.pointList.length; i++) { let PTL = SMathUtil_1.SMathUtil.pointToLine(p, new draw_1.SLine(this.pointList[i - 1].x, this.pointList[i - 1].y, this.pointList[i].x, this.pointList[i].y)); if (PTL.MinDis < this.sceneDis) { 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(); } } drawBaseLine(painter) { painter.pen.color = this.strokeColor; painter.drawPolyline(this.pointList); } onDraw(painter) { this.sceneDis = painter.toPx(this.dis); painter.pen.lineWidth = painter.toPx(this.lineWidth); if (this.status == __1.SItemStatus.Create && this.lastPoint) { this.drawBaseLine(painter); painter.drawLine(this.pointList[this.pointList.length - 1], this.lastPoint); this.pointList.forEach((t, i) => { painter.brush.color = draw_1.SColor.White; if (i == this.curIndex) { painter.brush.color = this.fillColor; } painter.drawCircle(t.x, t.y, painter.toPx(5)); }); } else if (this.status == __1.SItemStatus.Edit) { this.drawBaseLine(painter); this.pointList.forEach((t, i) => { painter.brush.color = draw_1.SColor.White; if (i == this.curIndex) { painter.brush.color = this.fillColor; } painter.drawCircle(t.x, t.y, painter.toPx(5)); }); } else { if (this.selected) { painter.pen.lineWidth = painter.toPx(this.lineWidth * 2); painter.shadow.shadowBlur = 10; painter.shadow.shadowColor = new draw_1.SColor(`#00000033`); painter.shadow.shadowOffsetX = 5; painter.shadow.shadowOffsetY = 5; } this.drawBaseLine(painter); } } } exports.SPolylineItem = SPolylineItem;