SceneMarkItem.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * ********************************************************************************************************************
  3. *
  4. * :*$@@%$*: ;: ;; ;;
  5. * :@@%! :!@@%: %! ;%%@@%$ =@@@@@@@%; @%@@@%%%%@@@@@
  6. * :@%; :$= %%$$$%$$ ;$$ ;$@= !@$
  7. * =@! %! @ $=;% !@@@%: !$$$$$$$$$$$$$$=
  8. * =@* %! @ $= % %@= =%@! %=
  9. * *$%%! @@= ;=$%%%$*: %! @ $= % =%%%%%%@$ *%: =%
  10. * %@@!: !@@@%=$@@@@%! :*@@$: %! @ $= % $* ;@ @* :%*
  11. * ;@@! ;!!!;: ;@%: =======@%========* @ $$ % $%*****$@ :@$=*********=@$
  12. * $@* ;@@@%=!: *@*
  13. * =@$ ;;;!=%@@@@=! =@!
  14. * %@$: =@%: :*@@@* %@= Copyright (c) 2016-2019. 北京上格云技术有限公司
  15. * ;%@@$=$@@%* *@@@$=%@@%;
  16. * ::;:: ::;:: All rights reserved.
  17. *
  18. * ********************************************************************************************************************
  19. */
  20. import { Opt } from "../types/Opt";
  21. import { SGraphyItem } from "@saga-web/graphy/lib";
  22. import {
  23. SColor,
  24. SLineCapStyle,
  25. SPainter,
  26. SPath2D,
  27. SPoint,
  28. SPolygonUtil
  29. } from "@saga-web/draw/lib";
  30. import { SMouseEvent } from "@saga-web/base/lib";
  31. import { ItemOrder } from "../types/ItemOrder";
  32. import { SMathUtil } from "../utils/SMathUtil";
  33. /**
  34. * 蒙版item
  35. *
  36. * @author 郝建龙
  37. */
  38. export class SceneMarkItem extends SGraphyItem {
  39. /** 轮廓线坐标 */
  40. outLine: SPoint[] = [];
  41. /** 是否闭合 */
  42. closeFlag = false;
  43. /** 鼠标移动点 */
  44. private lastPoint = new SPoint();
  45. /** 蒙版 */
  46. private mask = new SPath2D();
  47. /**
  48. * 构造函数
  49. *
  50. * @param parent 指向父对象
  51. * @param data 蒙版起点数据
  52. */
  53. constructor(parent: SGraphyItem | null, data: SPoint); // Constructor
  54. /**
  55. * 构造函数
  56. *
  57. * @param parent 指向父对象
  58. * @param data 蒙版轮廓线数据
  59. */
  60. constructor(parent: SGraphyItem | null, data: SPoint[]); // Constructor
  61. /**
  62. * 构造函数
  63. *
  64. * @param parent 指向父对象
  65. * @param data 蒙版起点数据 | 蒙版轮廓线数据
  66. */
  67. constructor(parent: SGraphyItem | null, data: SPoint[] | SPoint) {
  68. super(parent);
  69. if (data instanceof Array) {
  70. this.outLine = data;
  71. this.createMask();
  72. } else {
  73. this.outLine.push(data);
  74. this.lastPoint = data;
  75. }
  76. this.zOrder = ItemOrder.sceneMarkOrder;
  77. } // Constructor
  78. /**
  79. * 鼠标按下事件
  80. *
  81. * @param event 事件参数
  82. * @return boolean
  83. */
  84. onMouseDown(event: SMouseEvent): boolean {
  85. if (!this.closeFlag && event.buttons == 1) {
  86. if (
  87. this.lastPoint.x == this.outLine[0].x &&
  88. this.lastPoint.y == this.outLine[0].y &&
  89. this.outLine.length >= 3
  90. ) {
  91. this.createMask();
  92. return true;
  93. }
  94. let p = new SPoint(event.x, event.y);
  95. this.lastPoint.x = p.x;
  96. this.lastPoint.y = p.y;
  97. this.outLine.push(p);
  98. }
  99. this.update();
  100. return true;
  101. } // Function onMouseDown()
  102. /**
  103. * 鼠标移动事件
  104. *
  105. * @param event 事件参数
  106. * @return boolean
  107. */
  108. onMouseMove(event: SMouseEvent): boolean {
  109. if (!this.closeFlag) {
  110. this.lastPoint = new SPoint(event.x, event.y);
  111. if (this.outLine.length >= 3) {
  112. let le = SMathUtil.pointDistance(
  113. this.lastPoint.x,
  114. this.lastPoint.y,
  115. this.outLine[0].x,
  116. this.outLine[0].y
  117. );
  118. // @ts-ignore
  119. let scale = this.parent.scene.view.scale;
  120. if (le * scale < 30) {
  121. this.lastPoint.x = this.outLine[0].x;
  122. this.lastPoint.y = this.outLine[0].y;
  123. }
  124. }
  125. }
  126. this.update();
  127. return true;
  128. } // Function onMouseMove()
  129. /***
  130. * 键盘按键弹起事件
  131. *
  132. * @param event 事件参数
  133. */
  134. onKeyUp(event: KeyboardEvent): void {
  135. if (event.keyCode == 13 && this.outLine.length >= 3) {
  136. this.createMask();
  137. }
  138. } // Function onKeyUp()
  139. /**
  140. * 创建蒙版
  141. *
  142. */
  143. createMask(): void {
  144. this.closeFlag = true;
  145. if (this.scene) {
  146. this.scene.grabItem = null;
  147. }
  148. this.mask = new SPath2D();
  149. this.mask.moveTo(Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  150. this.mask.lineTo(Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  151. this.mask.lineTo(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
  152. this.mask.lineTo(Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
  153. this.mask.lineTo(Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
  154. let antiArr = this.outLine.concat([]);
  155. // (计算基于数学坐标系统,在绘图坐标系由于y轴方向相反结果也要取反)
  156. if (SPolygonUtil.clockDir(antiArr) > 0) {
  157. antiArr = antiArr.reverse();
  158. }
  159. this.mask.polygon(antiArr);
  160. this.update();
  161. } // Function createMask()
  162. /**
  163. * 判断点是否在区域内
  164. *
  165. * @param x
  166. * @param y
  167. */
  168. contains(x: number, y: number): boolean {
  169. let arr = this.outLine;
  170. return !SPolygonUtil.pointIn(x, y, arr);
  171. } // Function contains()
  172. /**
  173. * Item绘制操作
  174. *
  175. * @param painter painter对象
  176. */
  177. onDraw(painter: SPainter): void {
  178. painter.pen.lineCapStyle = SLineCapStyle.Square;
  179. if (this.closeFlag) {
  180. painter.pen.color = SColor.Transparent;
  181. painter.brush.color = Opt.sceneMarkColor;
  182. painter.drawPath(this.mask);
  183. } else {
  184. painter.pen.color = new SColor("#ff0000");
  185. painter.pen.lineWidth = painter.toPx(3);
  186. painter.drawPolyline(this.outLine);
  187. painter.drawLine(
  188. this.outLine[this.outLine.length - 1],
  189. this.lastPoint
  190. );
  191. }
  192. } // Function onDraw()
  193. } // Class SceneMarkItem