SGraphItem.ts 20 KB

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