SGraphItem.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. import { SMouseButton, SMouseEvent, SObject } from "@saga-web/base/lib";
  2. import { SPainter, SPoint, SRect } from "@saga-web/draw/lib";
  3. import { SGraphScene } from "./SGraphScene";
  4. /**
  5. * Graph图形引擎Item类
  6. *
  7. * @author 庞利祥(sybotan@126.com)
  8. */
  9. export class SGraphItem extends SObject {
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. // 属性
  12. /** 场景对象 */
  13. private _scene: SGraphScene | null = null;
  14. get scene(): SGraphScene | null {
  15. if (null != this._parent) {
  16. return this._parent.scene;
  17. } else {
  18. return this._scene;
  19. }
  20. } // Get scene
  21. set scene(v: SGraphScene | null) {
  22. this._scene = v;
  23. this.update();
  24. } // Set scene
  25. /** parent属性存值函数 */
  26. private _parent: SGraphItem | null = null;
  27. get parent(): SGraphItem | null {
  28. return this._parent;
  29. } // Get parent
  30. set parent(v: SGraphItem | null) {
  31. // 如果parent未变更
  32. if (this.parent == v) {
  33. return;
  34. }
  35. // 如果原parent不为空
  36. if (this._parent != null) {
  37. // 将节点从原parent节点中摘除
  38. let i = this._parent.children.indexOf(this);
  39. this._parent.children.splice(i, 1);
  40. }
  41. this._parent = v;
  42. this.update();
  43. // 如果新parent不为空
  44. if (this._parent != null) {
  45. // 将节点加入到新parent节点中
  46. this._parent.children.push(this);
  47. this._parent.children.sort(SGraphItem.sortItemZOrder);
  48. }
  49. } // Set parent()
  50. /** 子节点 */
  51. children: SGraphItem[] = [];
  52. /** Z轴顺序 */
  53. private _zOrder: number = 0;
  54. get zOrder(): number {
  55. return this._zOrder;
  56. } // Get zOrder
  57. set zOrder(v: number) {
  58. this._zOrder = v;
  59. if (this._parent != null) {
  60. this._parent.children.sort(SGraphItem.sortItemZOrder);
  61. }
  62. this.update();
  63. } // Set zOrder
  64. /** 位置 */
  65. pos: SPoint = new SPoint(0, 0);
  66. /** X轴坐标 */
  67. get x(): number {
  68. return this.pos.x;
  69. } // Get x
  70. set x(v: number) {
  71. if (this.pos.x == v) {
  72. return;
  73. }
  74. let old = this.pos.x;
  75. this.pos.x = v;
  76. this.update();
  77. } // Set x
  78. /** Y轴坐标 */
  79. get y(): number {
  80. return this.pos.y;
  81. } // Get y
  82. set y(v: number) {
  83. if (this.pos.y == v) {
  84. return;
  85. }
  86. let old = this.pos.y;
  87. this.pos.y = v;
  88. this.update();
  89. } // Set y
  90. /** 缩放比例 */
  91. scale: number = 1;
  92. /** 是否可见 */
  93. _visible: boolean = true;
  94. get visible(): boolean {
  95. return this._visible;
  96. } // Get visible
  97. set visible(v: boolean) {
  98. this._visible = v;
  99. this.update();
  100. } // Set visible
  101. /** 是否可以移动 */
  102. moveable: boolean = false;
  103. /** 是否正在移动 */
  104. private _isMoving = false;
  105. /** 是否可用 */
  106. enabled: boolean = true;
  107. /** 是否可被选中 */
  108. selectable = false;
  109. /** 是否被选中 */
  110. private _selected = false;
  111. get selected(): boolean {
  112. return this._selected && this.selectable && this.enabled;
  113. } // Get selected
  114. set selected(value: boolean) {
  115. // 如果选择状态未变更
  116. if (this.selected == value) {
  117. return;
  118. }
  119. this._selected = value;
  120. this.update();
  121. } // Set selected
  122. /** 是否进行变形 */
  123. isTransform = true;
  124. /** 鼠标按下时位置 */
  125. private _mouseDownPos = new SPoint();
  126. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  127. // 函数
  128. /**
  129. * 构造函数
  130. *
  131. * @param parent 指向父对象
  132. */
  133. constructor(parent: SGraphItem | null = null) {
  134. super();
  135. if (parent) {
  136. this.parent = parent;
  137. }
  138. } // Function constructor()
  139. /**
  140. * Item绘制框架
  141. *
  142. * @param painter painter对象
  143. * @param rect 绘制区域
  144. */
  145. onPaint(painter: SPainter, rect: SRect): void {
  146. this.onDraw(painter);
  147. for (let item of this.children) {
  148. // 如果item不可见
  149. if (!item.visible) {
  150. continue;
  151. }
  152. // item所占矩阵与要刷新的矩阵相交则刷新,否则不刷
  153. // if (item.boundingRect()--rect){
  154. //
  155. // }
  156. // 保存画布状态
  157. painter.save();
  158. // item位移到指定位置绘制
  159. painter.translate(item.x, item.y);
  160. // 如果不进行变形处理,则取消painter的变型操作
  161. if (!item.isTransform) {
  162. let matrix = painter.worldTransform;
  163. let x0 = matrix.e;
  164. let y0 = matrix.f;
  165. painter.resetTransform();
  166. painter.translate(x0, y0);
  167. }
  168. // 设置绘制区域
  169. // canvas.clip(item.boundingRect())
  170. // rect 调整 宽度高度取最小值 根据pos位移
  171. // 绘制item
  172. item.onPaint(painter, rect);
  173. // 恢复画布状态
  174. painter.restore();
  175. }
  176. } // Function onPaint()
  177. /**
  178. * Item绘制操作
  179. *
  180. * @param painter painter对象
  181. */
  182. onDraw(painter: SPainter): void {} // Function onDraw()
  183. /**
  184. * 隐藏对象
  185. */
  186. hide(): void {
  187. this.visible = false;
  188. } // Function hide()
  189. /**
  190. * 显示对象
  191. */
  192. show(): void {
  193. this.visible = true;
  194. } // Function show()
  195. /**
  196. * 更新Item
  197. */
  198. update(): void {
  199. if (null != this.scene) {
  200. const view = this.scene.view;
  201. if (null != view) {
  202. view.update();
  203. }
  204. }
  205. } // Function update()
  206. /**
  207. * Item对象边界区域
  208. *
  209. * @return 对象边界区域
  210. */
  211. boundingRect(): SRect {
  212. return new SRect(0, 0, 10, 10);
  213. } // Function boundingRect()
  214. /**
  215. * 移动item到指定位置
  216. *
  217. * @param x 新位置的x坐标
  218. * @param y 新位置的y坐标
  219. */
  220. moveTo(x: number, y: number): void {
  221. this.x = x;
  222. this.y = y;
  223. } // moveTo()
  224. /**
  225. * 判断item是否包含点x,y
  226. *
  227. * @param x 横坐标(当前item)
  228. * @param y 纵坐标(当前item)
  229. *
  230. * @return boolean
  231. */
  232. contains(x: number, y: number): boolean {
  233. return this.boundingRect().contains(x - this.x, y - this.y);
  234. } // Function contains()
  235. /**
  236. * 获得item的路径节点列表。(该节点被加载到场景中,如果未被加载到场景中,计算会出错)
  237. *
  238. * @return *[]
  239. */
  240. itemPath(): SGraphItem[] {
  241. if (this.parent != null) {
  242. let list = this.parent.itemPath();
  243. list.push(this);
  244. return list;
  245. }
  246. return [this];
  247. } // Function itemPath()
  248. /**
  249. * 将场景中的xy坐标转换成item坐标。(该节点被加载到场景中,如果未被加载到场景中,计算会出错)
  250. *
  251. * @param x 场景中的横坐标
  252. * @param y 场景中的纵坐标
  253. *
  254. * @return 在item中的坐标
  255. */
  256. mapFromScene(x: number, y: number): SPoint {
  257. let list = this.itemPath();
  258. let x0 = x;
  259. let y0 = y;
  260. for (let item of list) {
  261. x0 = (x0 - item.x) / item.scale;
  262. y0 = (y0 - item.y) / item.scale;
  263. }
  264. return new SPoint(x0, y0);
  265. } // Function mapFromScene()
  266. /**
  267. * 将item中的xy坐标转换成场景坐标。(该节点被加载到场景中,如果未被加载到场景中,计算会出错)
  268. *
  269. * @param x item中的横坐标
  270. * @param y item中的纵坐标
  271. *
  272. * @return 在场景中的坐标
  273. */
  274. mapToScene(x: number, y: number): SPoint {
  275. if (this.parent == null) {
  276. return new SPoint(x, y);
  277. }
  278. return this.parent.mapToScene(
  279. x * this.scale + this.x,
  280. y * this.scale + this.y
  281. );
  282. } // Function mapToScene()
  283. // =================================================================================================================
  284. // 事件
  285. /**
  286. * 鼠标单击事件
  287. *
  288. * @param event 保存事件参数
  289. * @return boolean
  290. */
  291. onClick(event: SMouseEvent): boolean {
  292. for (let i = this.children.length - 1; i >= 0; i--) {
  293. let item = this.children[i];
  294. if (!this.acceptEvent() || !item.visible) {
  295. continue;
  296. }
  297. let ce = SGraphItem.toChildMouseEvent(item, event);
  298. if (item.contains(event.x, event.y) && item.onClick(ce)) {
  299. // 如果点在子项目上且子项目处理了事件
  300. return true;
  301. }
  302. }
  303. return false;
  304. } // Function onClick()
  305. /**
  306. * 鼠标双击事件
  307. *
  308. * @param event 保存事件参数
  309. * @return boolean
  310. */
  311. onDoubleClick(event: SMouseEvent): boolean {
  312. for (let i = this.children.length - 1; i >= 0; i--) {
  313. let item = this.children[i];
  314. if (!this.acceptEvent() || !item.visible) {
  315. continue;
  316. }
  317. let ce = SGraphItem.toChildMouseEvent(item, event);
  318. if (item.contains(event.x, event.y) && item.onDoubleClick(ce)) {
  319. // 如果点在子项目上且子项目处理了事件
  320. return true;
  321. }
  322. }
  323. return false;
  324. } // Function onDoubleClick()
  325. /**
  326. * 鼠标按下事件
  327. *
  328. * @param event 保存事件参数
  329. * @return boolean
  330. */
  331. onMouseDown(event: SMouseEvent): boolean {
  332. for (let i = this.children.length - 1; i >= 0; i--) {
  333. let item = this.children[i];
  334. if (!this.acceptEvent() || !item.visible) {
  335. continue;
  336. }
  337. let ce = SGraphItem.toChildMouseEvent(item, event);
  338. if (item.contains(event.x, event.y) && item.onMouseDown(ce)) {
  339. // 如果点在子项目上且子项目处理了事件
  340. return true;
  341. }
  342. }
  343. if (this.selectable) {
  344. this.clickSelect(event);
  345. }
  346. if (this.moveable) {
  347. this._mouseDownPos = new SPoint(event.x, event.y);
  348. this._isMoving = true;
  349. this.grabItem(this);
  350. return true;
  351. }
  352. return false;
  353. } // Function onMouseDown()
  354. /**
  355. * 鼠标移动事件
  356. *
  357. * @param event 保存事件参数
  358. * @return boolean
  359. */
  360. onMouseMove(event: SMouseEvent): boolean {
  361. for (let i = this.children.length - 1; i >= 0; i--) {
  362. let item = this.children[i];
  363. if (!this.acceptEvent() || !item.visible) {
  364. continue;
  365. }
  366. let ce = SGraphItem.toChildMouseEvent(item, event);
  367. if (item.contains(event.x, event.y) && item.onMouseMove(ce)) {
  368. // 如果点在子项目上且子项目处理了事件
  369. return true;
  370. }
  371. }
  372. if (
  373. event.buttons & SMouseButton.LeftButton &&
  374. this.moveable &&
  375. this._isMoving
  376. ) {
  377. let old = new SPoint(this.pos.x, this.pos.y);
  378. this.moveTo(
  379. this.pos.x + event.x - this._mouseDownPos.x,
  380. this.pos.y + event.y - this._mouseDownPos.y
  381. );
  382. this.$emit("onMove", old, this.pos);
  383. }
  384. // 处理hover
  385. const scene = this.scene;
  386. if (null != scene) {
  387. if (scene.hoverItem == null || scene.hoverItem !== this) {
  388. if (scene.hoverItem != null) {
  389. scene.hoverItem.onMouseLeave(event);
  390. }
  391. this.onMouseEnter(event);
  392. scene.hoverItem = this;
  393. }
  394. }
  395. return true;
  396. } // Function onMouseMove()
  397. /**
  398. * 释放鼠标事件
  399. *
  400. * @param event 保存事件参数
  401. * @return boolean
  402. */
  403. onMouseUp(event: SMouseEvent): boolean {
  404. for (let i = this.children.length - 1; i >= 0; i--) {
  405. let item = this.children[i];
  406. if (!this.acceptEvent() || !item.visible) {
  407. continue;
  408. }
  409. let ce = SGraphItem.toChildMouseEvent(item, event);
  410. if (item.contains(event.x, event.y) && item.onMouseUp(ce)) {
  411. // 如果点在子项目上且子项目处理了事件
  412. return true;
  413. }
  414. }
  415. this._isMoving = false;
  416. this.releaseItem();
  417. return false;
  418. } // Function onMouseUp()
  419. /**
  420. * 鼠标进入事件
  421. *
  422. * @param event 保存事件参数
  423. * @return boolean
  424. */
  425. onMouseEnter(event: SMouseEvent): boolean {
  426. return false;
  427. } // Function onMouseEnter()
  428. /**
  429. * 鼠标离开事件
  430. *
  431. * @param event 保存事件参数
  432. * @return boolean
  433. */
  434. onMouseLeave(event: SMouseEvent): boolean {
  435. return false;
  436. } // Function onMouseLeave()
  437. /**
  438. * 上下文菜单事件
  439. *
  440. * @param event 事件参数
  441. */
  442. onContextMenu(event: SMouseEvent): boolean {
  443. for (let i = this.children.length - 1; i >= 0; i--) {
  444. let item = this.children[i];
  445. if (!this.acceptEvent() || !item.visible) {
  446. continue;
  447. }
  448. let ce = SGraphItem.toChildMouseEvent(item, event);
  449. if (item.contains(event.x, event.y) && item.onContextMenu(ce)) {
  450. // 如果点在子项目上且子项目处理了事件
  451. return true;
  452. }
  453. }
  454. return false;
  455. } // Function onContextMenu()
  456. /**
  457. * 按键按下事件
  458. *
  459. * @param event 事件参数
  460. */
  461. onKeyDown(event: KeyboardEvent): void {} // Function onKeyDown()
  462. /**
  463. * 按键press事件
  464. *
  465. * @param event 事件参数
  466. */
  467. onKeyPress(event: KeyboardEvent): void {} // Function onKeyPress()
  468. /**
  469. * 按键松开事件
  470. *
  471. * @param event 事件参数
  472. */
  473. onKeyUp(event: KeyboardEvent): void {} // Function onKeyUp()
  474. /**
  475. * 取消操作item事件
  476. *
  477. * */
  478. cancelOperate(): void {} // Function cancelOperate()
  479. // =================================================================================================================
  480. // 私有方法
  481. /**
  482. * 按ZOrder排序
  483. *
  484. * @param a 比较元素1
  485. * @param b 比较元素2
  486. * @return {number}
  487. */
  488. private static sortItemZOrder(a: SGraphItem, b: SGraphItem): number {
  489. return a.zOrder - b.zOrder;
  490. } // Function sortItemZOrder()
  491. /**
  492. * 鼠标事件转子对象鼠标事件
  493. *
  494. * @param child 子对象
  495. * @param event 事件参数
  496. * @return 子对象鼠标事件
  497. */
  498. private static toChildMouseEvent(
  499. child: SGraphItem,
  500. event: SMouseEvent
  501. ): SMouseEvent {
  502. let ce = new SMouseEvent(event);
  503. ce.x = (event.x - child.x) / child.scale;
  504. ce.y = (event.y - child.y) / child.scale;
  505. return ce;
  506. } // Function toChildMouseEvent()
  507. /**
  508. * 锁定item
  509. *
  510. * @param item 被锁定的item
  511. */
  512. private grabItem(item: SGraphItem): void {
  513. if (this.scene != null) {
  514. this.scene.grabItem = item;
  515. }
  516. } // Function grabItem
  517. /**
  518. * 释放被锁定的item
  519. */
  520. private releaseItem(): void {
  521. if (this.scene != null) {
  522. this.scene.grabItem = null;
  523. }
  524. } // Function grabItem
  525. /**
  526. * 判断是否处理事件
  527. *
  528. * @return true: 处理事件,否则不处理
  529. */
  530. private acceptEvent(): boolean {
  531. return this.visible && this.enabled;
  532. } // Function acceptEvent()
  533. /**
  534. * 点选item对象
  535. *
  536. * @param event 事件参数
  537. */
  538. private clickSelect(event: SMouseEvent): void {
  539. // 如果Item不可被选中,或没有按下鼠标左键,则直接返回
  540. if (!this.selectable || event.buttons != SMouseButton.LeftButton) {
  541. return;
  542. }
  543. const scene = this.scene;
  544. if (scene == null) {
  545. return;
  546. }
  547. // 如果按下Ctrl键,只改变当前item的选择标志
  548. if (event.ctrlKey) {
  549. scene.selectContainer.toggleItem(this);
  550. } else {
  551. scene.selectContainer.setItem(this);
  552. }
  553. } // Function clickSelect()
  554. } // Class SGraphItem