SGraphScene.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 { SMouseEvent, SMatrix } from "@persagy-web/base";
  27. import { SPainter, SPoint, SRect } from "@persagy-web/draw";
  28. import { SGraphItem } from "./SGraphItem";
  29. import { SGraphView } from "./SGraphView";
  30. import { SGraphSelectContainer } from "./SGraphSelectContainer";
  31. /**
  32. * Graph 图形引擎场景类
  33. *
  34. * @author 庞利祥 <sybotan@126.com>
  35. */
  36. export class SGraphScene {
  37. /** 展示场景的视图 */
  38. view: SGraphView | null = null;
  39. /** 根节点 */
  40. protected root: SGraphItem = new SGraphItem();
  41. /** 当前捕获 Item */
  42. grabItem: SGraphItem | null = null;
  43. /** 鼠标所在 Item */
  44. hoverItem: SGraphItem | null = null;
  45. /** 选择器 */
  46. selectContainer: SGraphSelectContainer = new SGraphSelectContainer();
  47. /**
  48. * 构造函数
  49. */
  50. constructor() {
  51. this.root.scene = this;
  52. this.addItem(this.selectContainer);
  53. } // Constructor
  54. /**
  55. * 添加 item 对象到场景。
  56. *
  57. * @param item 添加的对象
  58. */
  59. addItem(item: SGraphItem): void {
  60. item.parent = this.root;
  61. } // Function addItem()
  62. /**
  63. * 从场景中移除 Item。
  64. *
  65. * @param item 被移除的对象
  66. */
  67. removeItem(item: SGraphItem): void {
  68. item.parent = null;
  69. } // Function removeItem()
  70. /**
  71. * 绘制场景
  72. *
  73. * @param painter 绘制对象
  74. * @param rect 更新绘制区域
  75. */
  76. drawScene(painter: SPainter, rect: SRect): void {
  77. this.root.onPaint(painter, rect);
  78. } // Function drawScene()
  79. /**
  80. * 绘制背景
  81. *
  82. * @param painter 绘制对象
  83. * @param rect 更新绘制区域
  84. */
  85. drawBackground(painter: SPainter, rect: SRect) {
  86. // DO NOTHING
  87. } // Function drawBackground()
  88. /**
  89. * 绘制前景
  90. *
  91. * @param painter 绘制对象
  92. * @param rect 更新绘制区域
  93. */
  94. drawForeground(painter: SPainter, rect: SRect) {
  95. // DO NOTHING
  96. } // Function drawForeground()
  97. /**
  98. * 所有 item 占用的矩形区域
  99. *
  100. * @param flag 是否只对可见 item 处理
  101. * @return 所有 item 占用的矩形区域
  102. */
  103. allItemRect(flag: boolean = true): SRect | null {
  104. let rect: SRect | null = null;
  105. // 依次取 item 列中的所有 item。将所有 item 的边界做并焦处理。
  106. for (let item of this.root.children) {
  107. if ((flag && item.visible) || !flag) {
  108. if (rect == null) {
  109. rect = item
  110. .boundingRect()
  111. .translated(item.pos.x, item.pos.y);
  112. } else {
  113. rect.union(
  114. item.boundingRect().translated(item.pos.x, item.pos.y)
  115. );
  116. }
  117. }
  118. }
  119. return rect;
  120. } // Function allItemRect()
  121. /**
  122. * 被选中 item 占用的矩形区域
  123. *
  124. * @return 被选中 item 占用的矩形区域
  125. */
  126. selectedItemRect(): SRect | null {
  127. let rect: SRect | null = null;
  128. // 依次取 item 列中的所有 item。将所有 item 的边界做并焦处理。
  129. for (let item of this.root.children) {
  130. // 如果 item 未被选中,则去选择下一个 item
  131. if (!item.selected) {
  132. continue;
  133. }
  134. if (rect == null) {
  135. rect = item.boundingRect().translated(item.pos.x, item.pos.y);
  136. } else {
  137. rect.union(
  138. item.boundingRect().translated(item.pos.x, item.pos.y)
  139. );
  140. }
  141. }
  142. return rect;
  143. } // Function selectedItemRect()
  144. /**
  145. * 获得选中的对象列表
  146. *
  147. * @return 选中对象列表
  148. */
  149. selectedItems(): SGraphItem[] {
  150. let itemList = Array<SGraphItem>();
  151. for (let item of this.root.children) {
  152. // 如果 item 未被选中,则去选择下一个 item
  153. if (item.selected) {
  154. itemList.push(item);
  155. }
  156. }
  157. return itemList;
  158. } // Function selectedItems()
  159. // =================================================================================================================
  160. // 事件
  161. /**
  162. * 鼠标双击事件
  163. *
  164. * @param event 保存事件参数
  165. * @return 是否处理事件
  166. */
  167. onDoubleClick(event: SMouseEvent): boolean {
  168. if (this.grabItem != null) {
  169. return this.grabItem.onDoubleClick(
  170. this.toGrabItemMotionEvent(this.grabItem, event)
  171. );
  172. }
  173. return this.root.onDoubleClick(event);
  174. } // Function onDoubleClick()
  175. /**
  176. * 鼠标按下事件
  177. *
  178. * @param event 保存事件参数
  179. * @return 是否处理事件
  180. */
  181. onMouseDown(event: SMouseEvent): boolean {
  182. if (this.grabItem != null) {
  183. return this.grabItem.onMouseDown(
  184. this.toGrabItemMotionEvent(this.grabItem, event)
  185. );
  186. }
  187. const flag = this.root.onMouseDown(event);
  188. if (!flag) {
  189. this.selectContainer.clear();
  190. }
  191. return flag;
  192. } // Function onMouseDown()
  193. /**
  194. * 鼠标移动事件
  195. *
  196. * @param event 保存事件参数
  197. * @return 是否处理事件
  198. */
  199. onMouseMove(event: SMouseEvent): boolean {
  200. if (this.grabItem != null) {
  201. return this.grabItem.onMouseMove(
  202. this.toGrabItemMotionEvent(this.grabItem, event)
  203. );
  204. }
  205. return this.root.onMouseMove(event);
  206. } // Function onMouseMove()
  207. /**
  208. * 释放鼠标事件
  209. *
  210. * @param event 保存事件参数
  211. * @return 是否处理事件
  212. */
  213. onMouseUp(event: SMouseEvent): boolean {
  214. if (this.grabItem != null) {
  215. return this.grabItem.onMouseUp(
  216. this.toGrabItemMotionEvent(this.grabItem, event)
  217. );
  218. }
  219. return this.root.onMouseUp(event);
  220. } // Function onMouseUp()
  221. /**
  222. * 上下文菜单事件
  223. *
  224. * @param event 事件参数
  225. * @return 是否处理事件
  226. */
  227. onContextMenu(event: SMouseEvent): boolean {
  228. if (this.grabItem != null) {
  229. return this.grabItem.onContextMenu(
  230. this.toGrabItemMotionEvent(this.grabItem, event)
  231. );
  232. }
  233. return this.root.onContextMenu(event);
  234. } // Function onContextMenu()
  235. /**
  236. * 按键按下事件
  237. *
  238. * @param event 事件参数
  239. */
  240. onKeyDown(event: KeyboardEvent): void {
  241. if (this.grabItem != null) {
  242. return this.grabItem.onKeyDown(event);
  243. }
  244. return this.root.onKeyDown(event);
  245. } // Function onKeyDown()
  246. /**
  247. * 按键松开事件
  248. *
  249. * @param event 事件参数
  250. */
  251. onKeyUp(event: KeyboardEvent): void {
  252. if (this.grabItem != null) {
  253. return this.grabItem.onKeyUp(event);
  254. }
  255. return this.root.onKeyUp(event);
  256. } // Function onKeyUp()
  257. /**
  258. * 转换场景事件坐标到指定 Item 坐标事件
  259. *
  260. * @param item 指定的 item 对象
  261. * @param event 场景事件
  262. * @return 转换场景事件坐标到指定 Item 坐标事件
  263. */
  264. private toGrabItemMotionEvent(
  265. item: SGraphItem,
  266. event: SMouseEvent
  267. ): SMouseEvent {
  268. let se = { ...event };
  269. se.matrix = new SMatrix();
  270. if (this.view) {
  271. se.matrix.translate(this.view.origin.x, this.view.origin.y);
  272. se.matrix.scale(this.view.scale, this.view.scale);
  273. se.matrix.rotate(this.view.rotate);
  274. }
  275. se.matrix.multiply(item.scene2itemMattrix());
  276. let p = new SPoint(event.offsetX, event.offsetY).matrixTransform(
  277. se.matrix.inversed()
  278. );
  279. se.x = p.x;
  280. se.y = p.y;
  281. return se;
  282. } // Function toGrabItemMotionEvent()
  283. } // Class SGraphScene