ZoneItem.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 { SGraphyItem } from "@saga-web/graphy/lib";
  21. import {
  22. SColor,
  23. SPainter,
  24. SPath2D,
  25. SPoint,
  26. SPolygonUtil,
  27. SRect
  28. } from "@saga-web/draw/lib";
  29. import { Zone } from "../types/Zone";
  30. import { SMouseEvent } from "@saga-web/base/lib";
  31. import { ItemOrder } from "../types/ItemOrder";
  32. import { Opt } from "../types/Opt";
  33. import { Transparency } from "../types/Transparency";
  34. /**
  35. * 业务空间item
  36. *
  37. * @author 郝建龙
  38. */
  39. export class ZoneItem extends SGraphyItem {
  40. /** 空间所有数据 */
  41. data: Zone;
  42. /** 空间轮廓线坐标list */
  43. readonly pointArr: SPoint[][][] = [];
  44. /** X坐标最小值 */
  45. private minX = Number.MAX_SAFE_INTEGER;
  46. /** X坐标最大值 */
  47. private maxX = Number.MIN_SAFE_INTEGER;
  48. /** Y坐标最小值 */
  49. private minY = Number.MAX_SAFE_INTEGER;
  50. /** Y坐标最大值 */
  51. private maxY = Number.MIN_SAFE_INTEGER;
  52. /** path */
  53. private pathList: SPath2D[] = [];
  54. /** 点击位置坐标 */
  55. private clickPoint: SPoint | undefined;
  56. /** 选中时的颜色 */
  57. private selectColor = Opt.selectColor;
  58. /** 不可选时的颜色 */
  59. private unselectColor = Opt.zoneUnselectColor;
  60. /** 高亮状态 */
  61. private _highLightFlag: boolean = false;
  62. get highLightFlag(): boolean {
  63. return this._highLightFlag;
  64. } // Get highLightFlag
  65. set highLightFlag(value: boolean) {
  66. this._highLightFlag = value;
  67. this.update();
  68. } // Set highLightFlag
  69. /** 透明度 */
  70. _transparency: number = 20;
  71. get transparency(): number {
  72. return this._transparency;
  73. } // Get transparency
  74. set transparency(value: number) {
  75. this._transparency = value;
  76. this.update();
  77. } // Set transparency
  78. /** 受影响状态 */
  79. _isInfected: boolean = false;
  80. get isInfected(): boolean {
  81. return this._isInfected;
  82. } // Get isInfected
  83. set isInfected(value: boolean) {
  84. this._isInfected = value;
  85. this.update();
  86. } // Set isInfected
  87. private infectedColor = Opt.zoneInfectedColor;
  88. /**
  89. * 构造函数
  90. *
  91. * @param parent 指向父对象
  92. * @param data 空间数据
  93. */
  94. constructor(parent: SGraphyItem | null, data: Zone) {
  95. super(parent);
  96. this.data = data;
  97. this.zOrder = ItemOrder.zoneOrder;
  98. this.highLightFlag = data.HighLightFlag || false;
  99. this.transparency = data.Transparency || 20;
  100. this.isInfected = data.Infected || false;
  101. if (
  102. this.data.OutLine.length &&
  103. this.data.OutLine[0] &&
  104. this.data.OutLine[0].length
  105. ) {
  106. let tempArr = this.data.OutLine;
  107. this.minX = tempArr[0][0][0].X;
  108. this.maxX = this.minX;
  109. this.minY = -tempArr[0][0][0].Y;
  110. this.maxY = this.minY;
  111. // 处理轮廓点数组,同时计算最大最小值
  112. this.pointArr = tempArr.map((t): SPoint[][] => {
  113. let sPath = new SPath2D();
  114. let tempArr = t.map((it): SPoint[] => {
  115. let array = it.map(
  116. (item): SPoint => {
  117. let x = item.X,
  118. y = -item.Y;
  119. if (x < this.minX) {
  120. this.minX = x;
  121. }
  122. if (y < this.minY) {
  123. this.minY = y;
  124. }
  125. if (x > this.maxX) {
  126. this.maxX = x;
  127. }
  128. if (y > this.maxY) {
  129. this.maxY = y;
  130. }
  131. return new SPoint(x, y);
  132. }
  133. );
  134. sPath.polygon(array);
  135. return array;
  136. });
  137. this.pathList.push(sPath);
  138. return tempArr;
  139. });
  140. }
  141. } // Constructor
  142. /**
  143. * Item对象边界区域
  144. *
  145. * @return SRect
  146. */
  147. boundingRect(): SRect {
  148. return new SRect(
  149. this.minX,
  150. this.minY,
  151. this.maxX - this.minX,
  152. this.maxY - this.minY
  153. );
  154. } // Function boundingRect()
  155. /**
  156. * 鼠标单击事件
  157. *
  158. * @param event 事件参数
  159. * @return boolean
  160. */
  161. onClick(event: SMouseEvent): boolean {
  162. if (this.selectable) {
  163. this.selected = !this.selected;
  164. this.clickPoint = new SPoint(event.x, event.y);
  165. }
  166. this.$emit("click", event);
  167. return true;
  168. } // Function onClick()
  169. /**
  170. * 判断点是否在区域内
  171. *
  172. * @param x
  173. * @param y
  174. */
  175. contains(x: number, y: number): boolean {
  176. // return true;
  177. for (let j = 0; j < this.pointArr.length; j++) {
  178. let arr = this.pointArr[j];
  179. if (arr.length < 1 || !SPolygonUtil.pointIn(x, y, arr[0])) {
  180. continue;
  181. }
  182. if (arr.length == 1) {
  183. return true;
  184. }
  185. let flag = false;
  186. for (let i = 1; i < arr.length; i++) {
  187. if (SPolygonUtil.pointIn(x, y, arr[i])) {
  188. flag = true;
  189. break;
  190. }
  191. }
  192. if (flag) {
  193. continue;
  194. }
  195. return true;
  196. }
  197. return false;
  198. } // Function contains()
  199. /**
  200. * Item绘制操作
  201. *
  202. * @param painter painter对象
  203. * @param rect 绘制区域
  204. */
  205. onDraw(painter: SPainter, rect?: SRect): void {
  206. painter.pen.color = SColor.Transparent;
  207. if (!this.selectable) {
  208. painter.brush.color = this.unselectColor;
  209. } else {
  210. if (this.selected) {
  211. painter.brush.color = this.selectColor;
  212. } else if (this.highLightFlag) {
  213. painter.brush.color = new SColor(this.data.Color);
  214. } else if (this.isInfected) {
  215. painter.brush.color = this.infectedColor;
  216. } else {
  217. painter.brush.color = new SColor(
  218. `${this.data.Color}${Transparency[this.transparency]}`
  219. );
  220. }
  221. }
  222. painter.pen.lineWidth = 200;
  223. this.pathList.forEach((t): void => {
  224. painter.drawPath(t);
  225. });
  226. painter.brush.color = SColor.Black;
  227. painter.font.size = painter.toPx(10);
  228. if (this.clickPoint) {
  229. painter.drawText(
  230. this.data.RoomLocalName,
  231. this.clickPoint.x,
  232. this.clickPoint.y
  233. );
  234. }
  235. } // Function onDraw()
  236. } // Class ZoneItem