import { SMouseEvent } from "@persagy-web/base/lib"; import { ItemOrder } from "@persagy-web/big/lib"; import { SMathUtil } from "@persagy-web/big/lib/utils/SMathUtil"; import { SColor, SLineCapStyle, SPainter, SPoint, SPolygonUtil, SRect } from "@persagy-web/draw/lib"; import { SGraphItem, SGraphStyleItem } from "@persagy-web/graph/lib"; /** * 遮罩item * * @author 郝建龙 */ export class ShadeItem 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[] = []; /** 是否闭合 */ closeFlag = false; /** 鼠标移动点 */ private lastPoint = new SPoint(); /** * 构造函数 * * @param parent 指向父对象 * @param data 遮罩起点数据 */ constructor(parent: SGraphItem | null, data: SPoint); // Constructor /** * 构造函数 * * @param parent 指向父对象 * @param data 遮罩轮廓线数据 */ constructor(parent: SGraphItem | null, data: SPoint[]); // Constructor /** * 构造函数 * * @param parent 指向父对象 * @param data 遮罩起点数据 | 遮罩轮廓线数据 */ constructor(parent: SGraphItem | null, data: SPoint[] | SPoint) { super(parent); if (data instanceof Array) { this.pointList = data; this.createMask(); } else { this.pointList.push(data); this.lastPoint = data; } this.zOrder = ItemOrder.shadeOrder; this.fillColor = new SColor('#00000080') } // Constructor /** * 鼠标按下事件 * * @param event 事件参数 * @return boolean */ onMouseDown(event: SMouseEvent): boolean { if (!this.closeFlag && event.buttons == 1) { if ( this.lastPoint.x == this.pointList[0].x && this.lastPoint.y == this.pointList[0].y && this.pointList.length >= 3 ) { this.createMask(); return true; } let p = new SPoint(event.x, event.y); this.lastPoint.x = p.x; this.lastPoint.y = p.y; this.pointList.push(p); } this.update(); return true; } // Function onMouseDown() /** * 鼠标移动事件 * * @param event 事件参数 * @return boolean */ onMouseMove(event: SMouseEvent): boolean { if (!this.closeFlag) { this.lastPoint = new SPoint(event.x, event.y); if (this.pointList.length >= 3) { let le = SMathUtil.pointDistance( this.lastPoint.x, this.lastPoint.y, this.pointList[0].x, this.pointList[0].y ); // @ts-ignore let scale = this.parent.scene.view.scale; if (le * scale < 30) { this.lastPoint.x = this.pointList[0].x; this.lastPoint.y = this.pointList[0].y; } } } this.update(); return true; } // Function onMouseMove() /** * 鼠标抬起事件 * * @param event 事件参数 * @return boolean */ onMouseUp(event: SMouseEvent): boolean { return false; } // Function onMouseUp() /*** * 键盘按键弹起事件 * * @param event 事件参数 */ onKeyUp(event: KeyboardEvent): void { if (event.keyCode == 13 && this.pointList.length >= 3) { this.createMask(); } } // Function onKeyUp() /** * 创建蒙版 * */ createMask(): void { this.closeFlag = true; this.releaseItem(); this.calRect(); this.$emit('createSuc') this.update(); } // Function createMask() /** * 计算最大最小值 */ calRect() { // 点集存在 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; 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; } }); } } /** * Item 对象边界区域 * * @return 边界区域 */ boundingRect(): SRect { return new SRect( this.minX, this.minY, this.maxX - this.minX, this.maxY - this.minY ); } /** * 判断点是否在区域内 * * @param x * @param y */ contains(x: number, y: number): boolean { let arr = this.pointList; return SPolygonUtil.pointIn(x, y, arr); } // Function contains() /** * Item绘制操作 * * @param painter painter对象 */ onDraw(painter: SPainter): void { painter.pen.lineCapStyle = SLineCapStyle.Square; if (this.closeFlag) { painter.pen.color = SColor.Transparent; painter.brush.color = this.fillColor; painter.drawPolygon(this.pointList); } else { painter.pen.color = new SColor("#ff0000"); painter.pen.lineWidth = painter.toPx(3); painter.drawPolyline(this.pointList); painter.drawLine( this.pointList[this.pointList.length - 1], this.lastPoint ); } } // Function onDraw() } // Class SceneMarkItem