SSceneMaskItem.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SGraphStyleItem } from "@persagy-web/graph";
  27. import {
  28. SLineCapStyle,
  29. SPainter,
  30. SPath,
  31. SPoint,
  32. SPolygonUtil,
  33. SRect
  34. } from "@persagy-web/draw";
  35. import { SGraphItem } from "@persagy-web/graph/lib";
  36. import { ItemOrder } from "../config/ItemOrder";
  37. import { SItemStatus } from "..";
  38. import { SMouseEvent } from "@persagy-web/base";
  39. import { SMathUtil } from "@persagy-web/graph/lib/utils/SMathUtil";
  40. export class SSceneMaskItem extends SGraphStyleItem {
  41. /** 轮廓线坐标 */
  42. outline: SPoint[] = [];
  43. /** X坐标最小值 */
  44. private minX = Number.MAX_SAFE_INTEGER;
  45. /** X坐标最大值 */
  46. private maxX = Number.MIN_SAFE_INTEGER;
  47. /** Y坐标最小值 */
  48. private minY = Number.MAX_SAFE_INTEGER;
  49. /** Y坐标最大值 */
  50. private maxY = Number.MIN_SAFE_INTEGER;
  51. /** 鼠标移动点 */
  52. private lastPoint = new SPoint();
  53. /** 蒙版 */
  54. private mask = new SPath();
  55. /** 是否完成绘制 */
  56. protected _status: SItemStatus = SItemStatus.Normal;
  57. get status(): SItemStatus {
  58. return this._status;
  59. }
  60. set status(v: SItemStatus) {
  61. this._status = v;
  62. this.update();
  63. }
  64. /**
  65. * 构造函数
  66. *
  67. * @param parent 指向父对象
  68. * @param data 蒙版起点数据
  69. */
  70. constructor(parent: SGraphItem | null, data: SPoint);
  71. /**
  72. * 构造函数
  73. *
  74. * @param parent 指向父对象
  75. * @param data 蒙版轮廓线数据
  76. */
  77. constructor(parent: SGraphItem | null, data: SPoint[]);
  78. /**
  79. * 构造函数
  80. *
  81. * @param parent 指向父对象
  82. * @param data 蒙版起点数据 | 蒙版轮廓线数据
  83. */
  84. constructor(parent: SGraphItem | null, data: SPoint[] | SPoint) {
  85. super(parent);
  86. if (data instanceof Array) {
  87. this.outline = data;
  88. this.createMask();
  89. this.outline.forEach(it => {
  90. let x = it.x,
  91. y = it.y;
  92. if (x < this.minX) {
  93. this.minX = x;
  94. }
  95. if (y < this.minY) {
  96. this.minY = y;
  97. }
  98. if (x > this.maxX) {
  99. this.maxX = x;
  100. }
  101. if (y > this.maxY) {
  102. this.maxY = y;
  103. }
  104. });
  105. } else {
  106. this.outline.push(data);
  107. this.lastPoint = data;
  108. this.minY = data.y;
  109. this.maxY = data.y;
  110. this.minX = data.x;
  111. this.maxX = data.x;
  112. }
  113. this.zOrder = ItemOrder.sceneMarkOrder;
  114. }
  115. /**
  116. * 创建蒙版
  117. */
  118. createMask(): void {
  119. this.status = SItemStatus.Normal;
  120. this.releaseItem();
  121. this.$emit("finishCreated");
  122. this.mask = new SPath();
  123. this.mask.moveTo(Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  124. this.mask.lineTo(Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  125. this.mask.lineTo(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
  126. this.mask.lineTo(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
  127. this.mask.lineTo(Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  128. let antiArr = this.outline.concat([]);
  129. // (计算基于数学坐标系统,在绘图坐标系由于y轴方向相反结果也要取反)
  130. if (SPolygonUtil.clockDir(antiArr) > 0) {
  131. antiArr = antiArr.reverse();
  132. }
  133. this.mask.polygon(antiArr);
  134. this.update();
  135. }
  136. /**
  137. * 外接矩阵
  138. */
  139. boundingRect(): SRect {
  140. return new SRect(
  141. this.minX,
  142. this.minY,
  143. this.maxX - this.minX,
  144. this.maxY - this.minY
  145. );
  146. }
  147. /**
  148. * 判断点是否在区域内
  149. *
  150. * @param x
  151. * @param y
  152. */
  153. contains(x: number, y: number): boolean {
  154. let arr = this.outline;
  155. return !SPolygonUtil.pointIn(x, y, arr);
  156. }
  157. /**
  158. * 鼠标按下事件
  159. *
  160. * @param event 事件参数
  161. * @return boolean
  162. */
  163. onMouseDown(event: SMouseEvent): boolean {
  164. if (this.status == SItemStatus.Create && event.buttons == 1) {
  165. if (
  166. this.lastPoint.x == this.outline[0].x &&
  167. this.lastPoint.y == this.outline[0].y &&
  168. this.outline.length >= 3
  169. ) {
  170. this.createMask();
  171. return true;
  172. }
  173. let p = new SPoint(event.x, event.y);
  174. let x = p.x,
  175. y = p.y;
  176. this.lastPoint.x = x;
  177. this.lastPoint.y = y;
  178. this.outline.push(p);
  179. if (x < this.minX) {
  180. this.minX = x;
  181. }
  182. if (y < this.minY) {
  183. this.minY = y;
  184. }
  185. if (x > this.maxX) {
  186. this.maxX = x;
  187. }
  188. if (y > this.maxY) {
  189. this.maxY = y;
  190. }
  191. }
  192. this.update();
  193. return true;
  194. } // Function onMouseDown()
  195. /**
  196. * 鼠标移动事件
  197. *
  198. * @param event 事件参数
  199. * @return boolean
  200. */
  201. onMouseMove(event: SMouseEvent): boolean {
  202. if (this.status == SItemStatus.Create) {
  203. this.lastPoint = new SPoint(event.x, event.y);
  204. if (this.outline.length >= 3) {
  205. let le = SMathUtil.pointDistance(
  206. this.lastPoint.x,
  207. this.lastPoint.y,
  208. this.outline[0].x,
  209. this.outline[0].y
  210. );
  211. // @ts-ignore
  212. let scale = this.parent.scene.view.scale;
  213. if (le * scale < 30) {
  214. this.lastPoint.x = this.outline[0].x;
  215. this.lastPoint.y = this.outline[0].y;
  216. }
  217. }
  218. }
  219. this.update();
  220. return true;
  221. } // Function onMouseMove()
  222. /***
  223. * 键盘按键弹起事件
  224. *
  225. * @param event 事件参数
  226. */
  227. onKeyUp(event: KeyboardEvent): void {
  228. if (event.keyCode == 13 && this.outline.length >= 3) {
  229. this.createMask();
  230. }
  231. } // Function onKeyUp()
  232. /**
  233. * Item绘制操作
  234. *
  235. * @param painter painter对象
  236. */
  237. onDraw(painter: SPainter): void {
  238. painter.pen.lineCapStyle = SLineCapStyle.Square;
  239. painter.pen.color = this.strokeColor;
  240. painter.brush.color = this.fillColor;
  241. if (this.widthIsPx) {
  242. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  243. } else {
  244. painter.pen.lineWidth = this.lineWidth;
  245. }
  246. if (this.status == SItemStatus.Normal) {
  247. painter.drawPath(this.mask);
  248. } else if (this.status == SItemStatus.Create) {
  249. painter.drawPolyline(this.outline);
  250. painter.drawLine(
  251. this.outline[this.outline.length - 1],
  252. this.lastPoint
  253. );
  254. }
  255. }
  256. }