123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.SPolygonItem = void 0;
- const lib_1 = require("@persagy-web/graph/lib");
- const base_1 = require("@persagy-web/base/");
- const draw_1 = require("@persagy-web/draw");
- const __1 = require("..");
- const SMathUtil_1 = require("../utils/SMathUtil");
- class SPolygonItem extends lib_1.SGraphItem {
- constructor(parent) {
- 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._strokeColor = new draw_1.SColor("#0091FF");
- this._fillColor = new draw_1.SColor("#1EE887");
- this._lineStyle = lib_1.SLineStyle.Solid;
- this._lineWidth = 4;
- this.closeFlag = false;
- this.lastPoint = null;
- this.curIndex = -1;
- this.curPoint = null;
- this.len = 10;
- this.scenceLen = 15;
- this.isAlt = false;
- this.undoStack = new base_1.SUndoStack();
- }
- get getPointList() {
- return this.pointList;
- }
- set setPointList(arr) {
- this.pointList = arr;
- this.update();
- }
- get status() {
- return this._status;
- }
- set status(value) {
- this._status = value;
- 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 lineStyle() {
- return this._lineStyle;
- }
- set lineStyle(v) {
- this._lineStyle = v;
- this.update();
- }
- get lineWidth() {
- return this._lineWidth;
- }
- set lineWidth(v) {
- this._lineWidth = v;
- this.update();
- }
- insertPoint(x, y, i = null) {
- const point = new draw_1.SPoint(x, y);
- if (i == null) {
- this.pointList.push(point);
- }
- else {
- this.pointList.splice(i, 0, point);
- }
- this.update();
- return point;
- }
- deletePoint(i = null) {
- let point = null;
- if (i != null) {
- if (i >= this.pointList.length || i < 0) {
- point = null;
- }
- else {
- point = new draw_1.SPoint(this.pointList[i].x, this.pointList[i].y);
- this.pointList.splice(i, 1);
- }
- }
- else {
- if (this.pointList.length) {
- point = this.pointList[this.pointList.length - 1];
- this.pointList.pop();
- }
- else {
- point = null;
- }
- }
- this.curIndex = -1;
- this.curPoint = null;
- this.update();
- return point;
- }
- movePoint(x, y, i) {
- let point = null;
- if (i >= this.pointList.length || i < 0) {
- return null;
- }
- if (this.pointList.length) {
- this.pointList[i].x = x;
- this.pointList[i].y = y;
- }
- point = this.pointList[i];
- return point;
- }
- PrintPointList() {
- return this.pointList;
- }
- drawShowPolygon(painter, pointList) {
- painter.save();
- painter.pen.lineCapStyle = draw_1.SLineCapStyle.Square;
- painter.pen.color = this.strokeColor;
- painter.brush.color = this.fillColor;
- painter.pen.lineWidth = painter.toPx(this.lineWidth);
- if (this.lineStyle == lib_1.SLineStyle.Dashed) {
- painter.pen.lineDash = [
- painter.toPx(this.lineWidth * 3),
- painter.toPx(this.lineWidth * 7)
- ];
- }
- else if (this.lineStyle == lib_1.SLineStyle.Dotted) {
- painter.pen.lineDash = [
- painter.toPx(this.lineWidth),
- painter.toPx(this.lineWidth)
- ];
- }
- if (this.selected) {
- painter.shadow.shadowBlur = 10;
- painter.shadow.shadowColor = new draw_1.SColor(`#00000033`);
- painter.shadow.shadowOffsetX = 5;
- painter.shadow.shadowOffsetY = 5;
- }
- else {
- painter.shadow.shadowColor = draw_1.SColor.Transparent;
- }
- painter.drawPolygon([...pointList]);
- painter.restore();
- }
- drawCreatePolygon(painter, pointList) {
- painter.pen.lineCapStyle = draw_1.SLineCapStyle.Square;
- painter.pen.color = this.strokeColor;
- painter.pen.lineWidth = painter.toPx(this.lineWidth);
- if (this.lastPoint && pointList.length) {
- painter.drawLine(pointList[pointList.length - 1].x, pointList[pointList.length - 1].y, this.lastPoint.x, this.lastPoint.y);
- }
- painter.drawPolyline(pointList);
- painter.pen.color = draw_1.SColor.Transparent;
- painter.brush.color = new draw_1.SColor(this.fillColor.value);
- painter.pen.lineWidth = painter.toPx(this.lineWidth);
- if (this.lastPoint) {
- painter.drawPolygon([...pointList, this.lastPoint]);
- painter.pen.color = draw_1.SColor.Black;
- painter.brush.color = draw_1.SColor.White;
- pointList.forEach(item => {
- painter.drawCircle(item.x, item.y, painter.toPx(this.len / 2));
- });
- if (this.pointList.length) {
- if (SMathUtil_1.SMathUtil.pointDistance(this.lastPoint.x, this.lastPoint.y, this.pointList[0].x, this.pointList[0].y) < this.scenceLen) {
- painter.pen.color = draw_1.SColor.Black;
- painter.brush.color = draw_1.SColor.Red;
- painter.drawCircle(this.pointList[0].x, this.pointList[0].y, painter.toPx(this.len / 2));
- }
- }
- }
- else {
- painter.drawPolygon(pointList);
- }
- }
- drawEditPolygon(painter, pointList) {
- painter.pen.lineCapStyle = draw_1.SLineCapStyle.Square;
- painter.pen.color = this.strokeColor;
- painter.pen.lineWidth = painter.toPx(this.lineWidth);
- painter.brush.color = new draw_1.SColor(this.fillColor.value);
- painter.drawPolygon([...pointList]);
- painter.pen.color = draw_1.SColor.Black;
- painter.brush.color = draw_1.SColor.White;
- pointList.forEach((item, index) => {
- painter.brush.color = draw_1.SColor.White;
- if (index == this.curIndex) {
- painter.brush.color = new draw_1.SColor("#2196f3");
- }
- painter.drawCircle(item.x, item.y, painter.toPx(this.len / 2));
- });
- }
- editPolygonPoint(event) {
- if (this.isAlt) {
- let lenIndex = -1;
- let curenLen = this.scenceLen;
- this.pointList.forEach((item, index) => {
- let dis = SMathUtil_1.SMathUtil.pointDistance(event.x, event.y, item.x, item.y);
- if (dis < curenLen) {
- curenLen = dis;
- lenIndex = index;
- }
- });
- if (lenIndex != -1) {
- if (this.pointList.length <= 3) {
- return;
- }
- const delePoint = new draw_1.SPoint(this.pointList[lenIndex].x, this.pointList[lenIndex].y);
- this.deletePoint(lenIndex);
- this.recordAction(lib_1.SGraphPointListDelete, [
- this.pointList,
- delePoint,
- lenIndex
- ]);
- }
- }
- else {
- this.curIndex = -1;
- this.curPoint = null;
- let lenIndex = -1;
- let curenLen = this.scenceLen;
- this.pointList.forEach((item, index) => {
- let dis = SMathUtil_1.SMathUtil.pointDistance(event.x, event.y, item.x, item.y);
- if (dis < curenLen) {
- curenLen = dis;
- lenIndex = index;
- }
- });
- this.curIndex = lenIndex;
- if (-1 == lenIndex) {
- let len = SMathUtil_1.SMathUtil.pointToLine(new draw_1.SPoint(event.x, event.y), 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; i++) {
- let dis = SMathUtil_1.SMathUtil.pointToLine(new draw_1.SPoint(event.x, event.y), new draw_1.SLine(this.pointList[i], this.pointList[i + 1]));
- if (i + 1 == this.pointList.length) {
- dis = SMathUtil_1.SMathUtil.pointToLine(new draw_1.SPoint(event.x, event.y), new draw_1.SLine(this.pointList[i], this.pointList[0]));
- }
- if (dis.MinDis < len.MinDis) {
- len = dis;
- index = i;
- }
- }
- }
- if (len.Point) {
- if (len.MinDis <= this.scenceLen) {
- this.pointList.splice(index + 1, 0, len.Point);
- this.recordAction(lib_1.SGraphPointListInsert, [
- this.pointList,
- len.Point,
- index + 1
- ]);
- }
- else {
- super.onMouseDown(event);
- }
- }
- }
- else {
- this.curPoint = new draw_1.SPoint(this.pointList[this.curIndex].x, this.pointList[this.curIndex].y);
- }
- this.update();
- }
- }
- recordAction(SGraphCommand, any) {
- const sgraphcommand = new SGraphCommand(this.scene, this, ...any);
- this.undoStack.push(sgraphcommand);
- }
- undo() {
- if (this.status == __1.SItemStatus.Normal) {
- return;
- }
- this.undoStack.undo();
- }
- redo() {
- if (this.status == __1.SItemStatus.Normal) {
- return;
- }
- this.undoStack.redo();
- }
- onDoubleClick(event) {
- if (__1.SItemStatus.Normal == this.status) {
- this.status = __1.SItemStatus.Edit;
- this.grabItem(this);
- }
- else if (__1.SItemStatus.Edit == this.status) {
- this.status = __1.SItemStatus.Normal;
- this.releaseItem();
- }
- this.update();
- return true;
- }
- onKeyDown(event) {
- if (this.status == __1.SItemStatus.Normal) {
- return false;
- }
- else if (this.status == __1.SItemStatus.Create) {
- if (event.code == "Enter") {
- if (this.pointList.length > 2) {
- this.status = __1.SItemStatus.Normal;
- this.$emit("finishCreated");
- this.releaseItem();
- }
- }
- }
- else if (this.status == __1.SItemStatus.Edit) {
- if (event.key == "Alt") {
- this.isAlt = true;
- }
- }
- this.update();
- return true;
- }
- onKeyUp(event) {
- if (this.status == __1.SItemStatus.Edit) {
- if (event.key == "Alt") {
- this.isAlt = false;
- }
- else if (event.keyCode == base_1.SKeyCode.Delete) {
- if (this.pointList.length > 3) {
- this.deletePoint(this.curIndex);
- }
- }
- }
- this.update();
- }
- onMouseDown(event) {
- if (event.shiftKey) {
- event = this.compare(event);
- }
- if (this.status == __1.SItemStatus.Create) {
- let len = -1;
- if (this.pointList.length) {
- len = SMathUtil_1.SMathUtil.pointDistance(event.x, event.y, this.pointList[0].x, this.pointList[0].y);
- }
- if (this.pointList.length > 2 && len > 0 && len < this.scenceLen) {
- this.status = __1.SItemStatus.Normal;
- this.$emit("finishCreated");
- this.releaseItem();
- }
- else {
- this.insertPoint(event.x, event.y);
- let pos = new draw_1.SPoint(event.x, event.y);
- this.recordAction(lib_1.SGraphPointListInsert, [this.pointList, pos]);
- }
- }
- else if (this.status == __1.SItemStatus.Edit) {
- this.editPolygonPoint(event);
- }
- else {
- return super.onMouseDown(event);
- }
- return true;
- }
- onMouseEnter(event) {
- return true;
- }
- onMouseLeave(event) {
- return true;
- }
- onMouseMove(event) {
- if (event.shiftKey) {
- event = this.compare(event);
- }
- if (this.status == __1.SItemStatus.Create) {
- this.lastPoint = new draw_1.SPoint();
- this.lastPoint.x = event.x;
- this.lastPoint.y = event.y;
- this.update();
- }
- else if (this.status == __1.SItemStatus.Edit) {
- if (event.buttons == 1) {
- if (-1 != this.curIndex) {
- this.pointList[this.curIndex].x = event.x;
- this.pointList[this.curIndex].y = event.y;
- }
- }
- this.update();
- }
- else {
- return super.onMouseMove(event);
- }
- return true;
- }
- 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;
- }
- onMouseUp(event) {
- if (this.status == __1.SItemStatus.Edit) {
- if (-1 != this.curIndex) {
- const pos = new draw_1.SPoint(this.pointList[this.curIndex].x, this.pointList[this.curIndex].y);
- this.recordAction(lib_1.SGraphPointListUpdate, [
- this.pointList,
- this.curPoint,
- pos,
- this.curIndex
- ]);
- }
- }
- else if (this.status == __1.SItemStatus.Normal) {
- this.moveToOrigin(this.x, this.y);
- return super.onMouseUp(event);
- }
- return true;
- }
- 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;
- }
- onResize(event) {
- return true;
- }
- cancelOperate() {
- if (this.status == __1.SItemStatus.Create) {
- this.parent = null;
- }
- else if (this.status == __1.SItemStatus.Edit) {
- this.status = __1.SItemStatus.Normal;
- }
- this.update();
- }
- 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 arr = this.pointList;
- if (arr.length < 3 || !draw_1.SPolygonUtil.pointIn(x, y, arr)) {
- return false;
- }
- return true;
- }
- onDraw(painter) {
- this.scenceLen = painter.toPx(this.len);
- if (this.status == __1.SItemStatus.Normal) {
- this.drawShowPolygon(painter, this.pointList);
- }
- else if (this.status == __1.SItemStatus.Create) {
- this.drawCreatePolygon(painter, this.pointList);
- }
- else {
- this.drawEditPolygon(painter, this.pointList);
- }
- }
- }
- exports.SPolygonItem = SPolygonItem;
|