SGraphItem.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. if (this.moveable) {
  387. this._mouseDownPos = new SPoint(event.x, event.y);
  388. this._isMoving = true;
  389. if (this.scene) {
  390. this._lastGrab = this.scene.grabItem;
  391. }
  392. this.grabItem(this);
  393. return true;
  394. }
  395. return false;
  396. } // Function onMouseDown()
  397. /**
  398. * 鼠标移动事件
  399. *
  400. * @param event 保存事件参数
  401. * @return boolean
  402. */
  403. onMouseMove(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(ce.x, ce.y) && item.onMouseMove(ce)) {
  411. // 如果点在子项目上且子项目处理了事件
  412. return true;
  413. }
  414. }
  415. if (this.scene && this.scene.view) {
  416. this.scene.view.cursor = this.cursor;
  417. }
  418. if (
  419. event.buttons & SMouseButton.LeftButton &&
  420. this.moveable &&
  421. this._isMoving
  422. ) {
  423. let old = new SPoint(this.pos.x, this.pos.y);
  424. const mp = this.toParentChange(
  425. event.x - this._mouseDownPos.x,
  426. event.y - this._mouseDownPos.y
  427. );
  428. this.moveTo(this.pos.x + mp.x, this.pos.y + mp.y);
  429. if (this.scene && this.selected) {
  430. // this.scene.selectContainer.itemList.indexOf(this)
  431. this.scene.selectContainer.itemList.forEach(t => {
  432. if (t != this) {
  433. t.moveTo(t.pos.x + mp.x, t.pos.y + mp.y);
  434. }
  435. });
  436. }
  437. this.$emit("onMove", old, this.pos);
  438. }
  439. // 处理hover
  440. const scene = this.scene;
  441. if (null != scene) {
  442. if (scene.hoverItem == null || scene.hoverItem !== this) {
  443. if (scene.hoverItem != null) {
  444. scene.hoverItem.onMouseLeave(event);
  445. }
  446. this.onMouseEnter(event);
  447. scene.hoverItem = this;
  448. }
  449. }
  450. return true;
  451. } // Function onMouseMove()
  452. /**
  453. * 释放鼠标事件
  454. *
  455. * @param event 保存事件参数
  456. * @return boolean
  457. */
  458. onMouseUp(event: SMouseEvent): boolean {
  459. for (let i = this.children.length - 1; i >= 0; i--) {
  460. let item = this.children[i];
  461. if (!this.acceptEvent() || !item.visible) {
  462. continue;
  463. }
  464. let ce = SGraphItem.toChildMouseEvent(item, event);
  465. if (item.contains(ce.x, ce.y) && item.onMouseUp(ce)) {
  466. // 如果点在子项目上且子项目处理了事件
  467. return true;
  468. }
  469. }
  470. if (this.scene && this.selected) {
  471. // this.scene.selectContainer.itemList.indexOf(this)
  472. this.scene.selectContainer.itemList.forEach(t => {
  473. if (t != this) {
  474. t.moveToOrigin(t.x, t.y);
  475. }
  476. });
  477. }
  478. // 选择
  479. let select = false;
  480. if (this.selectable) {
  481. select = this.clickSelect(event);
  482. }
  483. this._isMoving = false;
  484. this.releaseItem();
  485. if (this.scene) {
  486. this.scene.grabItem = this._lastGrab;
  487. }
  488. return select;
  489. } // Function onMouseUp()
  490. /**
  491. * 鼠标进入事件
  492. *
  493. * @param event 保存事件参数
  494. * @return boolean
  495. */
  496. onMouseEnter(event: SMouseEvent): boolean {
  497. return false;
  498. } // Function onMouseEnter()
  499. /**
  500. * 鼠标离开事件
  501. *
  502. * @param event 保存事件参数
  503. * @return boolean
  504. */
  505. onMouseLeave(event: SMouseEvent): boolean {
  506. return false;
  507. } // Function onMouseLeave()
  508. /**
  509. * 上下文菜单事件
  510. *
  511. * @param event 事件参数
  512. */
  513. onContextMenu(event: SMouseEvent): boolean {
  514. for (let i = this.children.length - 1; i >= 0; i--) {
  515. let item = this.children[i];
  516. if (!this.acceptEvent() || !item.visible) {
  517. continue;
  518. }
  519. let ce = SGraphItem.toChildMouseEvent(item, event);
  520. if (item.contains(ce.x, ce.y) && item.onContextMenu(ce)) {
  521. // 如果点在子项目上且子项目处理了事件
  522. return true;
  523. }
  524. }
  525. return false;
  526. } // Function onContextMenu()
  527. /**
  528. * 按键按下事件
  529. *
  530. * @param event 事件参数
  531. */
  532. onKeyDown(event: KeyboardEvent): void {} // Function onKeyDown()
  533. /**
  534. * 按键press事件
  535. *
  536. * @param event 事件参数
  537. */
  538. onKeyPress(event: KeyboardEvent): void {} // Function onKeyPress()
  539. /**
  540. * 按键松开事件
  541. *
  542. * @param event 事件参数
  543. */
  544. onKeyUp(event: KeyboardEvent): void {} // Function onKeyUp()
  545. /**
  546. * 取消操作item事件
  547. *
  548. * */
  549. cancelOperate(): void {} // Function cancelOperate()
  550. /**
  551. * 返回item对应的数据对象
  552. *
  553. * */
  554. toData(): any | null {
  555. return null;
  556. } // Function toData()
  557. /**
  558. * 移动item后的处理,计算其他信息,将原点置为场景原点
  559. *
  560. * @param x x坐标
  561. * @param y y坐标
  562. * */
  563. moveToOrigin(x: number, y: number): void {
  564. if (this.children && this.children.length) {
  565. this.children.forEach(it => {
  566. it.moveToOrigin(x, y);
  567. });
  568. }
  569. } // Function moveToOrigin()
  570. // =================================================================================================================
  571. // 私有方法
  572. /**
  573. * 按ZOrder排序
  574. *
  575. * @param a 比较元素1
  576. * @param b 比较元素2
  577. * @return {number}
  578. */
  579. private static sortItemZOrder(a: SGraphItem, b: SGraphItem): number {
  580. return a.zOrder - b.zOrder;
  581. } // Function sortItemZOrder()
  582. /**
  583. * 鼠标事件转子对象鼠标事件
  584. *
  585. * @param child 子对象
  586. * @param event 事件参数
  587. * @return 子对象鼠标事件
  588. */
  589. private static toChildMouseEvent(
  590. child: SGraphItem,
  591. event: SMouseEvent
  592. ): SMouseEvent {
  593. let ce = new SMouseEvent(event);
  594. ce.matrix.translate(child.x, child.y);
  595. ce.matrix.scale(child.scale, child.scale);
  596. ce.matrix.rotate(0, 0, child.rotate);
  597. if (!child.isTransform) {
  598. ce.matrix.scale(child._inverseScale, child._inverseScale);
  599. }
  600. const mp = new SPoint(event.offsetX, event.offsetY).matrixTransform(
  601. ce.matrix.inversed()
  602. );
  603. ce.x = mp.x;
  604. ce.y = mp.y;
  605. return ce;
  606. } // Function toChildMouseEvent()
  607. /**
  608. * 锁定item
  609. *
  610. * @param item 被锁定的item
  611. */
  612. protected grabItem(item: SGraphItem): void {
  613. if (this.scene != null) {
  614. this.scene.grabItem = item;
  615. }
  616. } // Function grabItem
  617. /**
  618. * 释放被锁定的item
  619. */
  620. protected releaseItem(): void {
  621. if (this.scene != null) {
  622. this.scene.grabItem = null;
  623. }
  624. } // Function grabItem
  625. /**
  626. * 计算点在父节点的位置
  627. *
  628. * @param x X轴
  629. * @param y Y轴
  630. * @return 在父节点的位置
  631. */
  632. private toParentChange(x: number, y: number): SPoint {
  633. const m = new SMatrix();
  634. m.scale(this.scale, this.scale);
  635. if (!this.isTransform) {
  636. m.scale(this._inverseScale, this._inverseScale);
  637. }
  638. m.rotate(this.rotate);
  639. const mp = new SPoint(x, y).matrixTransform(m);
  640. return new SPoint(mp.x, mp.y);
  641. }
  642. /**
  643. * 判断是否处理事件
  644. *
  645. * @return true: 处理事件,否则不处理
  646. */
  647. private acceptEvent(): boolean {
  648. return this.visible && this.enabled;
  649. } // Function acceptEvent()
  650. /**
  651. * 点选item对象
  652. *
  653. * @param event 事件参数
  654. */
  655. private clickSelect(event: SMouseEvent): boolean {
  656. // 如果Item不可被选中,或没有按下鼠标左键,则直接返回
  657. if (!this.selectable) {
  658. // || event.buttons != SMouseButton.LeftButton
  659. return false;
  660. }
  661. const scene = this.scene;
  662. if (scene == null) {
  663. return false;
  664. }
  665. // 如果按下Ctrl键,只改变当前item的选择标志
  666. if (event.ctrlKey) {
  667. scene.selectContainer.toggleItem(this);
  668. } else {
  669. scene.selectContainer.setItem(this);
  670. }
  671. return true;
  672. } // Function clickSelect()
  673. } // Class SGraphItem