SGraphScene.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import { SMouseEvent, SMatrix } from "@saga-web/base/lib";
  2. import {SPainter, SPoint, 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. this.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. this.toGrabItemMotionEvent(this.grabItem, event)
  164. );
  165. }
  166. const flag = this.root.onMouseDown(event);
  167. if (!flag && !event.ctrlKey) {
  168. this.selectContainer.clear();
  169. }
  170. return flag;
  171. } // Function onMouseDown()
  172. /**
  173. * 鼠标移动事件
  174. *
  175. * @param event 保存事件参数
  176. * @return boolean
  177. */
  178. onMouseMove(event: SMouseEvent): boolean {
  179. if (this.grabItem != null) {
  180. return this.grabItem.onMouseMove(
  181. this.toGrabItemMotionEvent(this.grabItem, event)
  182. );
  183. }
  184. return this.root.onMouseMove(event);
  185. } // Function onMouseMove()
  186. /**
  187. * 释放鼠标事件
  188. *
  189. * @param event 保存事件参数
  190. * @return boolean
  191. */
  192. onMouseUp(event: SMouseEvent): boolean {
  193. if (this.grabItem != null) {
  194. return this.grabItem.onMouseUp(
  195. this.toGrabItemMotionEvent(this.grabItem, event)
  196. );
  197. }
  198. return this.root.onMouseUp(event);
  199. } // Function onMouseUp()
  200. /**
  201. * 上下文菜单事件
  202. *
  203. * @param event 事件参数
  204. */
  205. onContextMenu(event: SMouseEvent): boolean {
  206. if (this.grabItem != null) {
  207. return this.grabItem.onContextMenu(
  208. this.toGrabItemMotionEvent(this.grabItem, event)
  209. );
  210. }
  211. return this.root.onContextMenu(event);
  212. } // Function onContextMenu()
  213. /**
  214. * 按键按下事件
  215. *
  216. * @param event 事件参数
  217. */
  218. onKeyDown(event: KeyboardEvent): void {
  219. if (this.grabItem != null) {
  220. return this.grabItem.onKeyDown(event);
  221. }
  222. return this.root.onKeyDown(event);
  223. } // Function onKeyDown()
  224. // /**
  225. // * 按键press事件
  226. // *
  227. // * @param event 事件参数
  228. // */
  229. // onKeyPress(event: KeyboardEvent): void {
  230. // if (this.grabItem != null) {
  231. // this.grabItem.onKeyPress(event);
  232. // }
  233. // } // Function onKeyPress()
  234. /**
  235. * 按键松开事件
  236. *
  237. * @param event 事件参数
  238. */
  239. onKeyUp(event: KeyboardEvent): void {
  240. if (this.grabItem != null) {
  241. return this.grabItem.onKeyUp(event);
  242. }
  243. return this.root.onKeyUp(event);
  244. } // Function onKeyUp()
  245. /**
  246. * 转换场景事件坐标到指定Item坐标事件
  247. *
  248. * @param item 指定的item对象
  249. * @param event 场景事件
  250. * @return {}
  251. */
  252. private toGrabItemMotionEvent(
  253. item: SGraphItem,
  254. event: SMouseEvent
  255. ): SMouseEvent {
  256. let se = { ...event };
  257. se.matrix = new SMatrix();
  258. if (this.view) {
  259. se.matrix.translate(this.view.origin.x, this.view.origin.y);
  260. se.matrix.scale(this.view.scale, this.view.scale);
  261. se.matrix.rotate(this.view.rotate);
  262. }
  263. se.matrix.multiply(item.scene2itemMattrix());
  264. let p = new SPoint(event.offsetX, event.offsetY).matrixTransform(
  265. se.matrix.inversed()
  266. );
  267. se.x = p.x;
  268. se.y = p.y;
  269. return se;
  270. } // Function toGrabItemMotionEvent()
  271. } // Class SGraphScene