SSceneMaskItem.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. // 数据是数组类型
  87. if (data instanceof Array) {
  88. this.outline = data;
  89. this.createMask();
  90. this.outline.forEach(it => {
  91. let x = it.x,
  92. y = it.y;
  93. if (x < this.minX) {
  94. this.minX = x;
  95. }
  96. if (y < this.minY) {
  97. this.minY = y;
  98. }
  99. if (x > this.maxX) {
  100. this.maxX = x;
  101. }
  102. if (y > this.maxY) {
  103. this.maxY = y;
  104. }
  105. });
  106. } else {
  107. this.outline.push(data);
  108. this.lastPoint = data;
  109. this.minY = data.y;
  110. this.maxY = data.y;
  111. this.minX = data.x;
  112. this.maxX = data.x;
  113. }
  114. this.zOrder = ItemOrder.sceneMarkOrder;
  115. }
  116. /**
  117. * 创建蒙版
  118. */
  119. createMask(): void {
  120. this.status = SItemStatus.Normal;
  121. this.releaseItem();
  122. this.$emit("finishCreated");
  123. this.mask = new SPath();
  124. this.mask.moveTo(Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  125. this.mask.lineTo(Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  126. this.mask.lineTo(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
  127. this.mask.lineTo(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
  128. this.mask.lineTo(Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  129. let antiArr = this.outline.concat([]);
  130. // (计算基于数学坐标系统,在绘图坐标系由于y轴方向相反结果也要取反)
  131. if (SPolygonUtil.clockDir(antiArr) > 0) {
  132. antiArr = antiArr.reverse();
  133. }
  134. this.mask.polygon(antiArr);
  135. this.update();
  136. }
  137. /**
  138. * 外接矩阵
  139. */
  140. boundingRect(): SRect {
  141. return new SRect(
  142. this.minX,
  143. this.minY,
  144. this.maxX - this.minX,
  145. this.maxY - this.minY
  146. );
  147. }
  148. /**
  149. * 判断点是否在区域内
  150. *
  151. * @param x
  152. * @param y
  153. */
  154. contains(x: number, y: number): boolean {
  155. let arr = this.outline;
  156. return !SPolygonUtil.pointIn(x, y, arr);
  157. }
  158. /**
  159. * 鼠标按下事件
  160. *
  161. * @param event 事件参数
  162. * @return boolean
  163. */
  164. onMouseDown(event: SMouseEvent): boolean {
  165. // 创建态且为鼠标左键
  166. if (this.status == SItemStatus.Create && event.buttons == 1) {
  167. if (
  168. this.lastPoint.x == this.outline[0].x &&
  169. this.lastPoint.y == this.outline[0].y &&
  170. this.outline.length >= 3
  171. ) {
  172. this.createMask();
  173. return true;
  174. }
  175. let p = new SPoint(event.x, event.y);
  176. let x = p.x,
  177. y = p.y;
  178. this.lastPoint.x = x;
  179. this.lastPoint.y = y;
  180. this.outline.push(p);
  181. if (x < this.minX) {
  182. this.minX = x;
  183. }
  184. if (y < this.minY) {
  185. this.minY = y;
  186. }
  187. if (x > this.maxX) {
  188. this.maxX = x;
  189. }
  190. if (y > this.maxY) {
  191. this.maxY = y;
  192. }
  193. }
  194. this.update();
  195. return true;
  196. } // Function onMouseDown()
  197. /**
  198. * 鼠标移动事件
  199. *
  200. * @param event 事件参数
  201. * @return boolean
  202. */
  203. onMouseMove(event: SMouseEvent): boolean {
  204. // 创建态
  205. if (this.status == SItemStatus.Create) {
  206. this.lastPoint = new SPoint(event.x, event.y);
  207. // 点集包含3个以上数量
  208. if (this.outline.length >= 3) {
  209. let le = SMathUtil.pointDistance(
  210. this.lastPoint.x,
  211. this.lastPoint.y,
  212. this.outline[0].x,
  213. this.outline[0].y
  214. );
  215. // @ts-ignore
  216. let scale = this.parent.scene.view.scale;
  217. if (le * scale < 30) {
  218. this.lastPoint.x = this.outline[0].x;
  219. this.lastPoint.y = this.outline[0].y;
  220. }
  221. }
  222. }
  223. this.update();
  224. return true;
  225. } // Function onMouseMove()
  226. /***
  227. * 键盘按键弹起事件
  228. *
  229. * @param event 事件参数
  230. */
  231. onKeyUp(event: KeyboardEvent): void {
  232. // 回车键被按下且轮廓线有3个以上点
  233. if (event.keyCode == 13 && this.outline.length >= 3) {
  234. this.createMask();
  235. }
  236. } // Function onKeyUp()
  237. /**
  238. * Item绘制操作
  239. *
  240. * @param painter painter对象
  241. */
  242. onDraw(painter: SPainter): void {
  243. painter.pen.lineCapStyle = SLineCapStyle.Square;
  244. painter.pen.color = this.strokeColor;
  245. painter.brush.color = this.fillColor;
  246. // 线宽是否需要转像素
  247. if (this.widthIsPx) {
  248. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  249. } else {
  250. painter.pen.lineWidth = this.lineWidth;
  251. }
  252. //正常态
  253. if (this.status == SItemStatus.Normal) {
  254. painter.drawPath(this.mask);
  255. } else if (this.status == SItemStatus.Create) {
  256. // 创建态
  257. painter.drawPolyline(this.outline);
  258. painter.drawLine(
  259. this.outline[this.outline.length - 1],
  260. this.lastPoint
  261. );
  262. }
  263. }
  264. }