SGraphyItem.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. import { SMouseButton, SMouseEvent, SObject } from "@saga-web/base/lib";
  2. import { SPainter, SPoint, SRect } from "@saga-web/draw/lib";
  3. import { SGraphyScene } from "./SGraphyScene";
  4. /**
  5. * Graphy图形引擎Item类
  6. *
  7. * @author 庞利祥(sybotan@126.com)
  8. */
  9. export class SGraphyItem extends SObject {
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. // 属性
  12. /** 场景对象 */
  13. private _scene: SGraphyScene | null = null;
  14. get scene(): SGraphyScene | 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: SGraphyScene | null) {
  22. this._scene = v;
  23. this.update();
  24. } // Set scene
  25. /** parent属性存值函数 */
  26. private _parent: SGraphyItem | null = null;
  27. get parent(): SGraphyItem | null {
  28. return this._parent;
  29. } // Get parent
  30. set parent(v: SGraphyItem | 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(SGraphyItem.sortItemZOrder);
  48. }
  49. } // Set parent()
  50. /** 子节点 */
  51. children: SGraphyItem[] = [];
  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(SGraphyItem.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: SGraphyItem | 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. // 保存画布状态
  153. painter.save();
  154. // item位移到指定位置绘制
  155. painter.translate(item.x, item.y);
  156. // 如果不进行变形处理,则取消painter的变型操作
  157. if (!item.isTransform) {
  158. let matrix = painter.worldTransform;
  159. let x0 = matrix.e;
  160. let y0 = matrix.f;
  161. painter.resetTransform();
  162. painter.translate(x0, y0);
  163. }
  164. // 设置绘制区域
  165. // canvas.clip(item.boundingRect())
  166. // 绘制item
  167. item.onPaint(painter, rect);
  168. // 恢复画布状态
  169. painter.restore();
  170. }
  171. } // Function onPaint()
  172. /**
  173. * Item绘制操作
  174. *
  175. * @param painter painter对象
  176. */
  177. onDraw(painter: SPainter): void {} // Function onDraw()
  178. /**
  179. * 隐藏对象
  180. */
  181. hide(): void {
  182. this.visible = false;
  183. } // Function hide()
  184. /**
  185. * 显示对象
  186. */
  187. show(): void {
  188. this.visible = true;
  189. } // Function show()
  190. /**
  191. * 更新Item
  192. */
  193. update(): void {
  194. if (null != this.scene) {
  195. const view = this.scene.view;
  196. if (null != view) {
  197. view.update();
  198. }
  199. }
  200. } // Function update()
  201. /**
  202. * Item对象边界区域
  203. *
  204. * @return 对象边界区域
  205. */
  206. boundingRect(): SRect {
  207. return new SRect(0, 0, 10, 10);
  208. } // Function boundingRect()
  209. /**
  210. * 移动item到指定位置
  211. *
  212. * @param x 新位置的x坐标
  213. * @param y 新位置的y坐标
  214. */
  215. moveTo(x: number, y: number): void {
  216. this.x = x;
  217. this.y = y;
  218. } // moveTo()
  219. /**
  220. * 判断item是否包含点x,y
  221. *
  222. * @param x 横坐标(当前item)
  223. * @param y 纵坐标(当前item)
  224. *
  225. * @return boolean
  226. */
  227. contains(x: number, y: number): boolean {
  228. return this.boundingRect().contains(x - this.x, y - this.y);
  229. } // Function contains()
  230. /**
  231. * 获得item的路径节点列表。(该节点被加载到场景中,如果未被加载到场景中,计算会出错)
  232. *
  233. * @return *[]
  234. */
  235. itemPath(): SGraphyItem[] {
  236. if (this.parent != null) {
  237. let list = this.parent.itemPath();
  238. list.push(this);
  239. return list;
  240. }
  241. return [this];
  242. } // Function itemPath()
  243. /**
  244. * 将场景中的xy坐标转换成item坐标。(该节点被加载到场景中,如果未被加载到场景中,计算会出错)
  245. *
  246. * @param x 场景中的横坐标
  247. * @param y 场景中的纵坐标
  248. *
  249. * @return 在item中的坐标
  250. */
  251. mapFromScene(x: number, y: number): SPoint {
  252. let list = this.itemPath();
  253. let x0 = x;
  254. let y0 = y;
  255. for (let item of list) {
  256. x0 = (x0 - item.x) / item.scale;
  257. y0 = (y0 - item.y) / item.scale;
  258. }
  259. return new SPoint(x0, y0);
  260. } // Function mapFromScene()
  261. /**
  262. * 将item中的xy坐标转换成场景坐标。(该节点被加载到场景中,如果未被加载到场景中,计算会出错)
  263. *
  264. * @param x item中的横坐标
  265. * @param y item中的纵坐标
  266. *
  267. * @return 在场景中的坐标
  268. */
  269. mapToScene(x: number, y: number): SPoint {
  270. if (this.parent == null) {
  271. return new SPoint(x, y);
  272. }
  273. return this.parent.mapToScene(
  274. x * this.scale + this.x,
  275. y * this.scale + this.y
  276. );
  277. } // Function mapToScene()
  278. // =================================================================================================================
  279. // 事件
  280. /**
  281. * 鼠标单击事件
  282. *
  283. * @param event 保存事件参数
  284. * @return boolean
  285. */
  286. onClick(event: SMouseEvent): boolean {
  287. for (let i = this.children.length - 1; i >= 0; i--) {
  288. let item = this.children[i];
  289. if (!this.acceptEvent() || !item.visible) {
  290. continue;
  291. }
  292. let ce = SGraphyItem.toChildMouseEvent(item, event);
  293. if (item.contains(event.x, event.y) && item.onClick(ce)) {
  294. // 如果点在子项目上且子项目处理了事件
  295. return true;
  296. }
  297. }
  298. return false;
  299. } // Function onClick()
  300. /**
  301. * 鼠标双击事件
  302. *
  303. * @param event 保存事件参数
  304. * @return boolean
  305. */
  306. onDoubleClick(event: SMouseEvent): boolean {
  307. for (let i = this.children.length - 1; i >= 0; i--) {
  308. let item = this.children[i];
  309. if (!this.acceptEvent() || !item.visible) {
  310. continue;
  311. }
  312. let ce = SGraphyItem.toChildMouseEvent(item, event);
  313. if (item.contains(event.x, event.y) && item.onDoubleClick(ce)) {
  314. // 如果点在子项目上且子项目处理了事件
  315. return true;
  316. }
  317. }
  318. return false;
  319. } // Function onDoubleClick()
  320. /**
  321. * 鼠标按下事件
  322. *
  323. * @param event 保存事件参数
  324. * @return boolean
  325. */
  326. onMouseDown(event: SMouseEvent): boolean {
  327. for (let i = this.children.length - 1; i >= 0; i--) {
  328. let item = this.children[i];
  329. if (!this.acceptEvent() || !item.visible) {
  330. continue;
  331. }
  332. let ce = SGraphyItem.toChildMouseEvent(item, event);
  333. if (item.contains(event.x, event.y) && item.onMouseDown(ce)) {
  334. // 如果点在子项目上且子项目处理了事件
  335. return true;
  336. }
  337. }
  338. // if (this.selectable) {
  339. // this.clickSelect(event);
  340. // }
  341. if (this.moveable) {
  342. this._mouseDownPos = new SPoint(event.x, event.y);
  343. this._isMoving = true;
  344. this.grabItem(this);
  345. return true;
  346. }
  347. return false;
  348. } // Function onMouseDown()
  349. /**
  350. * 鼠标移动事件
  351. *
  352. * @param event 保存事件参数
  353. * @return boolean
  354. */
  355. onMouseMove(event: SMouseEvent): boolean {
  356. for (let i = this.children.length - 1; i >= 0; i--) {
  357. let item = this.children[i];
  358. if (!this.acceptEvent() || !item.visible) {
  359. continue;
  360. }
  361. let ce = SGraphyItem.toChildMouseEvent(item, event);
  362. if (item.contains(event.x, event.y) && item.onMouseMove(ce)) {
  363. // 如果点在子项目上且子项目处理了事件
  364. return true;
  365. }
  366. }
  367. if (
  368. event.buttons & SMouseButton.LeftButton &&
  369. this.moveable &&
  370. this._isMoving
  371. ) {
  372. let old = new SPoint(this.pos.x, this.pos.y);
  373. this.moveTo(
  374. this.pos.x + event.x - this._mouseDownPos.x,
  375. this.pos.y + event.y - this._mouseDownPos.y
  376. );
  377. this.$emit("onMove", old, this.pos);
  378. }
  379. // 处理hover
  380. const scene = this.scene;
  381. if (null != scene) {
  382. if (scene.hoverItem == null || scene.hoverItem !== this) {
  383. if (scene.hoverItem != null) {
  384. scene.hoverItem.onMouseLeave(event);
  385. }
  386. this.onMouseEnter(event);
  387. scene.hoverItem = this;
  388. }
  389. }
  390. return true;
  391. } // Function onMouseMove()
  392. /**
  393. * 释放鼠标事件
  394. *
  395. * @param event 保存事件参数
  396. * @return boolean
  397. */
  398. onMouseUp(event: SMouseEvent): boolean {
  399. for (let i = this.children.length - 1; i >= 0; i--) {
  400. let item = this.children[i];
  401. if (!this.acceptEvent() || !item.visible) {
  402. continue;
  403. }
  404. let ce = SGraphyItem.toChildMouseEvent(item, event);
  405. if (item.contains(event.x, event.y) && item.onMouseUp(ce)) {
  406. // 如果点在子项目上且子项目处理了事件
  407. return true;
  408. }
  409. }
  410. this._isMoving = false;
  411. this.releaseItem();
  412. return false;
  413. } // Function onMouseUp()
  414. /**
  415. * 鼠标进入事件
  416. *
  417. * @param event 保存事件参数
  418. * @return boolean
  419. */
  420. onMouseEnter(event: SMouseEvent): boolean {
  421. return false;
  422. } // Function onMouseEnter()
  423. /**
  424. * 鼠标离开事件
  425. *
  426. * @param event 保存事件参数
  427. * @return boolean
  428. */
  429. onMouseLeave(event: SMouseEvent): boolean {
  430. return false;
  431. } // Function onMouseLeave()
  432. /**
  433. * 上下文菜单事件
  434. *
  435. * @param event 事件参数
  436. */
  437. onContextMenu(event: SMouseEvent): boolean {
  438. for (let i = this.children.length - 1; i >= 0; i--) {
  439. let item = this.children[i];
  440. if (!this.acceptEvent() || !item.visible) {
  441. continue;
  442. }
  443. let ce = SGraphyItem.toChildMouseEvent(item, event);
  444. if (item.contains(event.x, event.y) && item.onContextMenu(ce)) {
  445. // 如果点在子项目上且子项目处理了事件
  446. return true;
  447. }
  448. }
  449. return false;
  450. } // Function onContextMenu()
  451. /**
  452. * 按键按下事件
  453. *
  454. * @param event 事件参数
  455. */
  456. onKeyDown(event: KeyboardEvent): void {} // Function onKeyDown()
  457. /**
  458. * 按键press事件
  459. *
  460. * @param event 事件参数
  461. */
  462. onKeyPress(event: KeyboardEvent): void {} // Function onKeyPress()
  463. /**
  464. * 按键松开事件
  465. *
  466. * @param event 事件参数
  467. */
  468. onKeyUp(event: KeyboardEvent): void {} // Function onKeyUp()
  469. // =================================================================================================================
  470. // 私有方法
  471. /**
  472. * 按ZOrder排序
  473. *
  474. * @param a 比较元素1
  475. * @param b 比较元素2
  476. * @return {number}
  477. */
  478. private static sortItemZOrder(a: SGraphyItem, b: SGraphyItem): number {
  479. return a.zOrder - b.zOrder;
  480. } // Function sortItemZOrder()
  481. /**
  482. * 鼠标事件转子对象鼠标事件
  483. *
  484. * @param child 子对象
  485. * @param event 事件参数
  486. * @return 子对象鼠标事件
  487. */
  488. private static toChildMouseEvent(
  489. child: SGraphyItem,
  490. event: SMouseEvent
  491. ): SMouseEvent {
  492. let ce = new SMouseEvent(event);
  493. ce.x = (event.x - child.x) / child.scale;
  494. ce.y = (event.y - child.y) / child.scale;
  495. return ce;
  496. } // Function toChildMouseEvent()
  497. /**
  498. * 锁定item
  499. *
  500. * @param item 被锁定的item
  501. */
  502. private grabItem(item: SGraphyItem): void {
  503. if (this.scene != null) {
  504. this.scene.grabItem = item;
  505. }
  506. } // Function grabItem
  507. /**
  508. * 释放被锁定的item
  509. */
  510. private releaseItem(): void {
  511. if (this.scene != null) {
  512. this.scene.grabItem = null;
  513. }
  514. } // Function grabItem
  515. /**
  516. * 判断是否处理事件
  517. *
  518. * @return true: 处理事件,否则不处理
  519. */
  520. private acceptEvent(): boolean {
  521. return this.visible && this.enabled;
  522. } // Function acceptEvent()
  523. /**
  524. * 点选item对象
  525. *
  526. * @param event 事件参数
  527. */
  528. private clickSelect(event: SMouseEvent): void {
  529. // 如果Item不可被选中,或没有按下鼠标左键,则直接返回
  530. if (!this.selectable) {
  531. return;
  532. }
  533. // 如果按下Ctrl键,只改变当前item的选择标志
  534. if (event.ctrlKey) {
  535. this.selected = !this.selected;
  536. return;
  537. }
  538. this.selected = true;
  539. } // Function clickSelect()
  540. } // Class SGraphyItem