SGraphScene.ts 7.7 KB

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