SGraphScene.ts 7.5 KB

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