RelationItem.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * ********************************************************************************************************************
  3. *
  4. * :*$@@%$*: ;: ;; ;;
  5. * :@@%! :!@@%: %! ;%%@@%$ =@@@@@@@%; @%@@@%%%%@@@@@
  6. * :@%; :$= %%$$$%$$ ;$$ ;$@= !@$
  7. * =@! %! @ $=;% !@@@%: !$$$$$$$$$$$$$$=
  8. * =@* %! @ $= % %@= =%@! %=
  9. * *$%%! @@= ;=$%%%$*: %! @ $= % =%%%%%%@$ *%: =%
  10. * %@@!: !@@@%=$@@@@%! :*@@$: %! @ $= % $* ;@ @* :%*
  11. * ;@@! ;!!!;: ;@%: =======@%========* @ $$ % $%*****$@ :@$=*********=@$
  12. * $@* ;@@@%=!: *@*
  13. * =@$ ;;;!=%@@@@=! =@!
  14. * %@$: =@%: :*@@@* %@= Copyright (c) 2016-2020. 北京上格云技术有限公司
  15. * ;%@@$=$@@%* *@@@$=%@@%;
  16. * ::;:: ::;:: All rights reserved.
  17. *
  18. * ********************************************************************************************************************
  19. */
  20. import { SGraphItem } from "@saga-web/graph/lib";
  21. import { SColor, SLine, SPainter, SPoint, SRect } from "@saga-web/draw/lib";
  22. import { Relation } from "../types/Relation";
  23. import { SMouseEvent } from "@saga-web/base/lib";
  24. import { BaseRelationItem } from "./BaseRelationItem";
  25. import { AnchorItem } from "./AnchorItem";
  26. import { ItemOrder } from "../types/ItemOrder";
  27. import { SMathUtil } from "../util/SMathUtil";
  28. /**
  29. * 关系item-折线画法-横平竖直
  30. *
  31. * @author 郝建龙
  32. */
  33. export class RelationItem extends BaseRelationItem {
  34. /** 关系数据 */
  35. data: Relation | null = null;
  36. /** 折点信息 */
  37. pointList: SPoint[] = [];
  38. /** 尾端折点3 */
  39. lastPointList: SPoint[] = [];
  40. /** 是否结束 */
  41. closeFlag = false;
  42. //
  43. Anchor1: AnchorItem | null = null;
  44. //
  45. Anchor2: AnchorItem | null = null;
  46. //
  47. private _dashOffset: number = 20;
  48. get dashOffset(): number {
  49. return this._dashOffset;
  50. }
  51. set dashOffset(value: number) {
  52. this._dashOffset = value;
  53. this.update();
  54. }
  55. // 定时器
  56. timer: number | null = null;
  57. /**
  58. * 构造函数
  59. *
  60. * @param parent 指向父对象
  61. * @param data Relation数据
  62. */
  63. constructor(parent: SGraphItem | null, data: Relation) {
  64. super(parent);
  65. this.data = data;
  66. this.Anchor1 = data.Anchor1 || null;
  67. this.Anchor2 = data.Anchor2 || null;
  68. this.zOrder = ItemOrder.RelateOrder;
  69. if (data.PointList) {
  70. this.pointList = data.PointList.map(it => {
  71. let x = it.X,
  72. y = it.Y;
  73. if (x < this.minX) {
  74. this.minX = x;
  75. }
  76. if (y < this.minY) {
  77. this.minY = y;
  78. }
  79. if (x > this.maxX) {
  80. this.maxX = x;
  81. }
  82. if (y > this.maxY) {
  83. this.maxY = y;
  84. }
  85. return new SPoint(x, y);
  86. });
  87. } else {
  88. if (this.Anchor1) {
  89. this.pointList.push(
  90. this.Anchor1.mapToScene(this.Anchor1.X, this.Anchor1.Y)
  91. );
  92. this.lastPointList = [
  93. this.Anchor1.mapToScene(this.Anchor1.X, this.Anchor1.Y)
  94. ];
  95. }
  96. }
  97. } // Constructor
  98. /**
  99. * 鼠标按下事件
  100. *
  101. * @param event 事件参数
  102. * @return boolean
  103. */
  104. onMouseDown(event: SMouseEvent): boolean {
  105. if (!this.closeFlag && event.buttons == 1) {
  106. if (this.lastPointList.length > 1) {
  107. for (let i = 1; i < this.lastPointList.length; i++) {
  108. this.pointList.push(this.lastPointList[i]);
  109. }
  110. }
  111. }
  112. if (this.Anchor2) {
  113. this.closeFlag = true;
  114. }
  115. this.update();
  116. return true;
  117. } // Function onMouseDown()
  118. /**
  119. * 鼠标移动事件
  120. *
  121. * @param event 事件参数
  122. * @return boolean
  123. */
  124. onMouseMove(event: SMouseEvent): boolean {
  125. if (this.pointList.length) {
  126. this.lastPointList = [
  127. this.pointList[this.pointList.length - 1],
  128. this.getPoint(
  129. this.pointList[this.pointList.length - 1],
  130. new SPoint(event.x, event.y)
  131. ),
  132. new SPoint(event.x, event.y)
  133. ];
  134. }
  135. this.update();
  136. return true;
  137. } // Function onMouseMove()
  138. /***
  139. * 键盘按键弹起事件
  140. *
  141. * @param event 事件参数
  142. */
  143. onKeyUp(event: KeyboardEvent): void {
  144. if (event.keyCode == 13) {
  145. this.closeFlag = true;
  146. }
  147. } // Function onKeyUp()
  148. /**
  149. * 鼠标右键事件
  150. *
  151. * @param event 事件参数
  152. * @return boolean
  153. */
  154. onContextMenu(event: SMouseEvent): boolean {
  155. this.$emit("ContextMenu", event);
  156. return true;
  157. } // Function onContextMenu()
  158. /**
  159. * 点击点是否在item范围内
  160. *
  161. */
  162. contains(x: number, y: number): boolean {
  163. if (this.closeFlag) {
  164. let p = new SPoint(x, y),
  165. l = 10;
  166. // todo差缩放
  167. for (let i = 1; i < this.pointList.length; i++) {
  168. let PTL = SMathUtil.pointToLine(
  169. p,
  170. new SLine(
  171. this.pointList[i - 1].x,
  172. this.pointList[i - 1].y,
  173. this.pointList[i].x,
  174. this.pointList[i].y
  175. )
  176. );
  177. if (PTL.MinDis < l) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. return false;
  184. }
  185. /**
  186. * 根据点计算出折线点
  187. *
  188. */
  189. getPoint(p1: SPoint, p2: SPoint): SPoint {
  190. if (Math.abs(p1.x - p2.x) >= Math.abs(p1.y - p2.y)) {
  191. return new SPoint(p2.x, p1.y);
  192. } else {
  193. return new SPoint(p1.x, p2.y);
  194. }
  195. }
  196. /***/
  197. change(): void {
  198. if (this.pointList.length) {
  199. if (this.Anchor1) {
  200. // 判断删除equip后,不移动
  201. if (this.Anchor1.parent && this.Anchor1.parent.parent) {
  202. this.pointList[0] = this.Anchor1.mapToScene(
  203. this.Anchor1.X,
  204. this.Anchor1.Y
  205. );
  206. if (this.pointList[1] && this.pointList[2]) {
  207. this.pointList[1] = this.getPoint(
  208. this.pointList[0],
  209. this.pointList[2]
  210. );
  211. }
  212. }
  213. }
  214. if (this.Anchor2) {
  215. // 删除equip后
  216. if (this.Anchor2.parent && this.Anchor2.parent.parent) {
  217. this.pointList[
  218. this.pointList.length - 1
  219. ] = this.Anchor2.mapToScene(this.Anchor2.X, this.Anchor2.Y);
  220. if (
  221. this.pointList[this.pointList.length - 1] &&
  222. this.pointList[this.pointList.length - 3]
  223. ) {
  224. this.pointList[
  225. this.pointList.length - 2
  226. ] = this.getPoint(
  227. this.pointList[this.pointList.length - 1],
  228. this.pointList[this.pointList.length - 3]
  229. );
  230. }
  231. }
  232. }
  233. }
  234. this.update();
  235. } // Function change()
  236. /**
  237. * Item绘制操作
  238. *
  239. * @param painter painter对象
  240. */
  241. onDraw(painter: SPainter): void {
  242. painter.pen.lineWidth = 3;
  243. painter.pen.color = new SColor("#409EFF");
  244. painter.brush.color = new SColor("#409EFF");
  245. painter.drawPolyline(this.pointList);
  246. painter.pen.lineWidth = 1;
  247. painter.pen.lineDash = [10, 10];
  248. painter.pen.dashOffset = this.dashOffset;
  249. painter.pen.color = SColor.White;
  250. painter.drawPolyline(this.pointList);
  251. if (this.name) {
  252. painter.brush.color = SColor.Black;
  253. painter.font.size = 12;
  254. painter.drawText(
  255. this.name,
  256. this.pointList[0].x + 20,
  257. this.pointList[0].y
  258. );
  259. }
  260. this.dashOffset -= 2;
  261. if (!this.closeFlag) {
  262. painter.pen.color = new SColor("#409EFF");
  263. painter.drawPolyline(this.lastPointList);
  264. }
  265. } // Function onDraw()
  266. } // Class RelationItem