ShadeItem.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { SMouseEvent } from "@persagy-web/base/lib";
  2. import { ItemOrder } from "@persagy-web/big/lib";
  3. import { SMathUtil } from "@persagy-web/big/lib/utils/SMathUtil";
  4. import { SColor, SLineCapStyle, SPainter, SPoint, SPolygonUtil, SRect } from "@persagy-web/draw/lib";
  5. import { SGraphItem, SGraphStyleItem } from "@persagy-web/graph/lib";
  6. /**
  7. * 遮罩item
  8. *
  9. * @author 郝建龙
  10. */
  11. export class ShadeItem extends SGraphStyleItem {
  12. /** X 坐标最小值 */
  13. private minX = Number.MAX_SAFE_INTEGER;
  14. /** X 坐标最大值 */
  15. private maxX = Number.MIN_SAFE_INTEGER;
  16. /** Y 坐标最小值 */
  17. private minY = Number.MAX_SAFE_INTEGER;
  18. /** Y 坐标最大值 */
  19. private maxY = Number.MIN_SAFE_INTEGER;
  20. /** 轮廓线坐标 */
  21. pointList: SPoint[] = [];
  22. /** 是否闭合 */
  23. closeFlag = false;
  24. /** 鼠标移动点 */
  25. private lastPoint = new SPoint();
  26. /**
  27. * 构造函数
  28. *
  29. * @param parent 指向父对象
  30. * @param data 遮罩起点数据
  31. */
  32. constructor(parent: SGraphItem | null, data: SPoint); // Constructor
  33. /**
  34. * 构造函数
  35. *
  36. * @param parent 指向父对象
  37. * @param data 遮罩轮廓线数据
  38. */
  39. constructor(parent: SGraphItem | null, data: SPoint[]); // Constructor
  40. /**
  41. * 构造函数
  42. *
  43. * @param parent 指向父对象
  44. * @param data 遮罩起点数据 | 遮罩轮廓线数据
  45. */
  46. constructor(parent: SGraphItem | null, data: SPoint[] | SPoint) {
  47. super(parent);
  48. if (data instanceof Array) {
  49. this.pointList = data;
  50. this.createMask();
  51. } else {
  52. this.pointList.push(data);
  53. this.lastPoint = data;
  54. }
  55. this.zOrder = ItemOrder.shadeOrder;
  56. this.fillColor = new SColor('#00000080')
  57. } // Constructor
  58. /**
  59. * 鼠标按下事件
  60. *
  61. * @param event 事件参数
  62. * @return boolean
  63. */
  64. onMouseDown(event: SMouseEvent): boolean {
  65. if (!this.closeFlag && event.buttons == 1) {
  66. if (
  67. this.lastPoint.x == this.pointList[0].x &&
  68. this.lastPoint.y == this.pointList[0].y &&
  69. this.pointList.length >= 3
  70. ) {
  71. this.createMask();
  72. return true;
  73. }
  74. let p = new SPoint(event.x, event.y);
  75. this.lastPoint.x = p.x;
  76. this.lastPoint.y = p.y;
  77. this.pointList.push(p);
  78. }
  79. this.update();
  80. return true;
  81. } // Function onMouseDown()
  82. /**
  83. * 鼠标移动事件
  84. *
  85. * @param event 事件参数
  86. * @return boolean
  87. */
  88. onMouseMove(event: SMouseEvent): boolean {
  89. if (!this.closeFlag) {
  90. this.lastPoint = new SPoint(event.x, event.y);
  91. if (this.pointList.length >= 3) {
  92. let le = SMathUtil.pointDistance(
  93. this.lastPoint.x,
  94. this.lastPoint.y,
  95. this.pointList[0].x,
  96. this.pointList[0].y
  97. );
  98. // @ts-ignore
  99. let scale = this.parent.scene.view.scale;
  100. if (le * scale < 30) {
  101. this.lastPoint.x = this.pointList[0].x;
  102. this.lastPoint.y = this.pointList[0].y;
  103. }
  104. }
  105. }
  106. this.update();
  107. return true;
  108. } // Function onMouseMove()
  109. /**
  110. * 鼠标抬起事件
  111. *
  112. * @param event 事件参数
  113. * @return boolean
  114. */
  115. onMouseUp(event: SMouseEvent): boolean {
  116. return false;
  117. } // Function onMouseUp()
  118. /***
  119. * 键盘按键弹起事件
  120. *
  121. * @param event 事件参数
  122. */
  123. onKeyUp(event: KeyboardEvent): void {
  124. if (event.keyCode == 13 && this.pointList.length >= 3) {
  125. this.createMask();
  126. }
  127. } // Function onKeyUp()
  128. /**
  129. * 创建蒙版
  130. *
  131. */
  132. createMask(): void {
  133. this.closeFlag = true;
  134. this.releaseItem();
  135. this.calRect();
  136. this.$emit('createSuc')
  137. this.update();
  138. } // Function createMask()
  139. /**
  140. * 计算最大最小值
  141. */
  142. calRect() {
  143. // 点集存在
  144. if (this.pointList.length) {
  145. this.minX = this.pointList[0].x;
  146. this.maxX = this.pointList[0].x;
  147. this.minY = this.pointList[0].y;
  148. this.maxY = this.pointList[0].y;
  149. // 遍历点集并计算最大最小值
  150. this.pointList.forEach((it): void => {
  151. let x = it.x,
  152. y = it.y;
  153. if (x < this.minX) {
  154. this.minX = x;
  155. }
  156. if (y < this.minY) {
  157. this.minY = y;
  158. }
  159. if (x > this.maxX) {
  160. this.maxX = x;
  161. }
  162. if (y > this.maxY) {
  163. this.maxY = y;
  164. }
  165. });
  166. }
  167. }
  168. /**
  169. * Item 对象边界区域
  170. *
  171. * @return 边界区域
  172. */
  173. boundingRect(): SRect {
  174. return new SRect(
  175. this.minX,
  176. this.minY,
  177. this.maxX - this.minX,
  178. this.maxY - this.minY
  179. );
  180. }
  181. /**
  182. * 判断点是否在区域内
  183. *
  184. * @param x
  185. * @param y
  186. */
  187. contains(x: number, y: number): boolean {
  188. let arr = this.pointList;
  189. return SPolygonUtil.pointIn(x, y, arr);
  190. } // Function contains()
  191. /**
  192. * Item绘制操作
  193. *
  194. * @param painter painter对象
  195. */
  196. onDraw(painter: SPainter): void {
  197. painter.pen.lineCapStyle = SLineCapStyle.Square;
  198. if (this.closeFlag) {
  199. painter.pen.color = SColor.Transparent;
  200. painter.brush.color = this.fillColor;
  201. painter.drawPolygon(this.pointList);
  202. } else {
  203. painter.pen.color = new SColor("#ff0000");
  204. painter.pen.lineWidth = painter.toPx(3);
  205. painter.drawPolyline(this.pointList);
  206. painter.drawLine(
  207. this.pointList[this.pointList.length - 1],
  208. this.lastPoint
  209. );
  210. }
  211. } // Function onDraw()
  212. } // Class SceneMarkItem