|
@@ -0,0 +1,375 @@
|
|
|
|
+import { SMouseEvent, SUndoStack } from "@persagy-web/base/lib";
|
|
|
|
+import { SItemStatus } from "@persagy-web/big/lib";
|
|
|
|
+import { SMathUtil } from "@persagy-web/big/lib/utils/SMathUtil"
|
|
|
|
+import { SColor, SLine, SPainter, SPoint, SRect } from "@persagy-web/draw/lib";
|
|
|
|
+import { SGraphItem, SGraphPointListInsert, SGraphStyleItem } from "@persagy-web/graph/lib";
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 用户自定义墙
|
|
|
|
+*/
|
|
|
|
+export class CustomWall extends SGraphStyleItem {
|
|
|
|
+ /** X 坐标最小值 */
|
|
|
|
+ private minX = Number.MAX_SAFE_INTEGER;
|
|
|
|
+ /** X 坐标最大值 */
|
|
|
|
+ private maxX = Number.MIN_SAFE_INTEGER;
|
|
|
|
+ /** Y 坐标最小值 */
|
|
|
|
+ private minY = Number.MAX_SAFE_INTEGER;
|
|
|
|
+ /** Y 坐标最大值 */
|
|
|
|
+ private maxY = Number.MIN_SAFE_INTEGER;
|
|
|
|
+ /** 折点信息 */
|
|
|
|
+ pointList: SPoint[] = [];
|
|
|
|
+ /** 是否绘制完成 */
|
|
|
|
+ _status: SItemStatus = SItemStatus.Normal;
|
|
|
|
+ get status(): SItemStatus {
|
|
|
|
+ return this._status;
|
|
|
|
+ }
|
|
|
|
+ set status(v: SItemStatus) {
|
|
|
|
+ this._status = v;
|
|
|
|
+ this.undoStack.clear();
|
|
|
|
+ this.update();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /** 鼠标移动时的点 */
|
|
|
|
+ protected lastPoint: SPoint | null = null;
|
|
|
|
+
|
|
|
|
+ /** 全局灵敏度 */
|
|
|
|
+ dis: number = 10;
|
|
|
|
+ /** 灵敏度转换为场景长度 */
|
|
|
|
+ protected sceneDis: number = 10;
|
|
|
|
+ /** 当前点索引 */
|
|
|
|
+ protected curIndex: number = -1;
|
|
|
|
+ /** 当前点 */
|
|
|
|
+ private curPoint: SPoint | null = null;
|
|
|
|
+ /** undo/redo 堆栈 */
|
|
|
|
+ private undoStack: SUndoStack = new SUndoStack();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 构造函数
|
|
|
|
+ *
|
|
|
|
+ * @param parent 父级
|
|
|
|
+ * @param list 坐标集合
|
|
|
|
+ */
|
|
|
|
+ constructor(parent: null | SGraphItem, list: SPoint[]);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 构造函数
|
|
|
|
+ *
|
|
|
|
+ * @param parent 父级
|
|
|
|
+ * @param list 第一个坐标
|
|
|
|
+ */
|
|
|
|
+ constructor(parent: null | SGraphItem, list: SPoint);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 构造函数
|
|
|
|
+ *
|
|
|
|
+ * @param parent 父级
|
|
|
|
+ * @param list 第一个坐标|坐标集合
|
|
|
|
+ */
|
|
|
|
+ constructor(parent: null | SGraphItem, list: SPoint | SPoint[]) {
|
|
|
|
+ super(parent);
|
|
|
|
+ // 数据类型为 SPoint
|
|
|
|
+ if (list instanceof SPoint) {
|
|
|
|
+ this.pointList.push(list);
|
|
|
|
+ } else {
|
|
|
|
+ this.pointList = list;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 添加点至数组中
|
|
|
|
+ *
|
|
|
|
+ * @param p 添加的点
|
|
|
|
+ * @param index 添加到的索引
|
|
|
|
+ */
|
|
|
|
+ private addPoint(p: SPoint, index?: number): void {
|
|
|
|
+ // 添加的索引存在且索引值有效
|
|
|
|
+ if (index && this.canHandle(index)) {
|
|
|
|
+ this.pointList.splice(index, 0, p);
|
|
|
|
+ this.recordAction(SGraphPointListInsert, [
|
|
|
|
+ this.pointList,
|
|
|
|
+ p,
|
|
|
|
+ index
|
|
|
|
+ ]);
|
|
|
|
+ } else {
|
|
|
|
+ // 否则
|
|
|
|
+ this.pointList.push(p);
|
|
|
|
+ this.recordAction(SGraphPointListInsert, [this.pointList, p]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.update();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 是否可以添加点到数组中
|
|
|
|
+ *
|
|
|
|
+ * @param index 要添加到的索引
|
|
|
|
+ * @return 是否可添加
|
|
|
|
+ */
|
|
|
|
+ private canHandle(index: number): boolean {
|
|
|
|
+ return index >= 0 && index <= this.pointList.length;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据索引删除点
|
|
|
|
+ *
|
|
|
|
+ * @param index 删除点
|
|
|
|
+ */
|
|
|
|
+ deletePoint(index: number): void {
|
|
|
|
+ // 索引值有效且折线列表有2个以上点
|
|
|
|
+ if (this.canHandle(index) && this.pointList.length > 2) {
|
|
|
|
+ const p = new SPoint(
|
|
|
|
+ this.pointList[this.curIndex].x,
|
|
|
|
+ this.pointList[this.curIndex].y
|
|
|
|
+ );
|
|
|
|
+ this.pointList.splice(index, 1);
|
|
|
|
+ this.recordAction(SGraphPointListInsert, [
|
|
|
|
+ this.pointList,
|
|
|
|
+ p,
|
|
|
|
+ index
|
|
|
|
+ ]);
|
|
|
|
+ this.curIndex = -1;
|
|
|
|
+ this.curPoint = null;
|
|
|
|
+ this.update();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 移动后处理所有坐标,并肩原点置为场景原点
|
|
|
|
+ *
|
|
|
|
+ * @param x x 坐标
|
|
|
|
+ * @param y y 坐标
|
|
|
|
+ */
|
|
|
|
+ moveToOrigin(x: number, y: number): void {
|
|
|
|
+ super.moveToOrigin(x, y);
|
|
|
|
+ // 遍历点集列表
|
|
|
|
+ this.pointList = this.pointList.map(
|
|
|
|
+ (t): SPoint => {
|
|
|
|
+ t.x = t.x + x;
|
|
|
|
+ t.y = t.y + y;
|
|
|
|
+ return t;
|
|
|
|
+ }
|
|
|
|
+ );
|
|
|
|
+ this.x = 0;
|
|
|
|
+ this.y = 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取点击点与点集中距离最近点
|
|
|
|
+ *
|
|
|
|
+ * @param p 鼠标点击点
|
|
|
|
+ */
|
|
|
|
+ findNearestPoint(p: SPoint): void {
|
|
|
|
+ let len = this.sceneDis;
|
|
|
|
+ // 遍历点集列表
|
|
|
|
+ for (let i = 0; i < this.pointList.length; i++) {
|
|
|
|
+ let dis = 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 SPoint(
|
|
|
|
+ this.pointList[this.curIndex].x,
|
|
|
|
+ this.pointList[this.curIndex].y
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 计算增加点的位置
|
|
|
|
+ *
|
|
|
|
+ * @param p 鼠标点击点
|
|
|
|
+ */
|
|
|
|
+ findAddPos(p: SPoint): void {
|
|
|
|
+ let len = SMathUtil.pointToLine(
|
|
|
|
+ p,
|
|
|
|
+ new SLine(this.pointList[0], this.pointList[1])
|
|
|
|
+ ),
|
|
|
|
+ index = 0;
|
|
|
|
+ // 折线点集多余2
|
|
|
|
+ if (this.pointList.length > 2) {
|
|
|
|
+ for (let i = 1; i < this.pointList.length - 1; i++) {
|
|
|
|
+ let dis = SMathUtil.pointToLine(
|
|
|
|
+ p,
|
|
|
|
+ new SLine(this.pointList[i], this.pointList[i + 1])
|
|
|
|
+ );
|
|
|
|
+ if (dis.MinDis < len.MinDis) {
|
|
|
|
+ len = dis;
|
|
|
|
+ index = i;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 最短距离在可吸附范围内且吸附点存在
|
|
|
|
+ if (len.MinDis < this.sceneDis && len.Point) {
|
|
|
|
+ this.addPoint(len.Point, index + 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * shift 垂直水平创建或编辑
|
|
|
|
+ *
|
|
|
|
+ * @param event 事件
|
|
|
|
+ * @return 事件对象
|
|
|
|
+ */
|
|
|
|
+ compare(event: SMouseEvent): SMouseEvent {
|
|
|
|
+ // 点集列表存在
|
|
|
|
+ if (this.pointList.length) {
|
|
|
|
+ let last = new SPoint(event.x, event.y);
|
|
|
|
+ if (this.status == SItemStatus.Create) {
|
|
|
|
+ last = this.pointList[this.pointList.length - 1];
|
|
|
|
+ } else if (this.status == 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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 记录相关动作并推入栈中
|
|
|
|
+ *
|
|
|
|
+ * @param SGraphCommand 相关命令类
|
|
|
|
+ * @param any 对应传入参数
|
|
|
|
+ */
|
|
|
|
+ protected recordAction(SGraphCommand: any, any: any[]): void {
|
|
|
|
+ // 记录相关命令并推入堆栈中
|
|
|
|
+ const command = new SGraphCommand(this.scene, this, ...any);
|
|
|
|
+ this.undoStack.push(command);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Item 对象边界区域
|
|
|
|
+ *
|
|
|
|
+ * @return 外接矩阵
|
|
|
|
+ */
|
|
|
|
+ boundingRect(): SRect {
|
|
|
|
+ // 点集列表有值
|
|
|
|
+ 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): void => {
|
|
|
|
+ let x = it.x,
|
|
|
|
+ y = it.y;
|
|
|
|
+
|
|
|
|
+ // 如果数据 x 坐标小于 x 坐标最小值
|
|
|
|
+ if (x < this.minX) {
|
|
|
|
+ this.minX = x;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 如果数据 y 坐标小于 y 坐标最小值
|
|
|
|
+ if (y < this.minY) {
|
|
|
|
+ this.minY = y;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 如果数据 x 坐标大于 x 坐标最小值
|
|
|
|
+ if (x > this.maxX) {
|
|
|
|
+ this.maxX = x;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 如果数据 y 坐标大于 y 坐标最小值
|
|
|
|
+ if (y > this.maxY) {
|
|
|
|
+ this.maxY = y;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new SRect(
|
|
|
|
+ this.minX,
|
|
|
|
+ this.minY,
|
|
|
|
+ this.maxX - this.minX,
|
|
|
|
+ this.maxY - this.minY
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断点是否在区域内
|
|
|
|
+ *
|
|
|
|
+ * @param x x 坐标
|
|
|
|
+ * @param y y 坐标
|
|
|
|
+ * @return 是否在区域内
|
|
|
|
+ */
|
|
|
|
+ contains(x: number, y: number): boolean {
|
|
|
|
+ let p = new SPoint(x, y);
|
|
|
|
+ // 遍历点集列表
|
|
|
|
+ for (let i = 1; i < this.pointList.length; i++) {
|
|
|
|
+ let PTL = SMathUtil.pointToLine(
|
|
|
|
+ p,
|
|
|
|
+ new 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(): void {
|
|
|
|
+ // 非正常态
|
|
|
|
+ if (this._status != SItemStatus.Normal) {
|
|
|
|
+ this.undoStack.undo();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 重做操作
|
|
|
|
+ */
|
|
|
|
+ redo(): void {
|
|
|
|
+ // 非正常态
|
|
|
|
+ if (this._status != SItemStatus.Normal) {
|
|
|
|
+ this.undoStack.redo();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 取消操作执行
|
|
|
|
+ */
|
|
|
|
+ cancelOperate(): void {
|
|
|
|
+ // 创建态
|
|
|
|
+ if (this.status == SItemStatus.Create) {
|
|
|
|
+ this.parent = null;
|
|
|
|
+ this.releaseItem();
|
|
|
|
+ } else if (this.status == SItemStatus.Edit) {
|
|
|
|
+ // 编辑态
|
|
|
|
+ this.status = SItemStatus.Normal;
|
|
|
|
+ this.releaseItem();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Item 绘制操作
|
|
|
|
+ *
|
|
|
|
+ * @param painter 绘制对象
|
|
|
|
+ */
|
|
|
|
+ onDraw(painter: SPainter): void {
|
|
|
|
+ // 缓存场景长度
|
|
|
|
+ this.sceneDis = painter.toPx(this.dis);
|
|
|
|
+ }
|
|
|
|
+}
|