SGraphItem.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SGraphItem = void 0;
  4. const lib_1 = require("@persagy-web/base/lib");
  5. const lib_2 = require("@persagy-web/draw/lib");
  6. class SGraphItem extends lib_1.SObject {
  7. constructor(parent = null) {
  8. super();
  9. this._scene = null;
  10. this._parent = null;
  11. this.children = [];
  12. this._zOrder = 0;
  13. this.pos = new lib_2.SPoint(0, 0);
  14. this.scale = 1;
  15. this._inverseScale = 1;
  16. this._rotate = 0;
  17. this._visible = true;
  18. this.moveable = false;
  19. this._isMoving = false;
  20. this.enabled = true;
  21. this.selectable = false;
  22. this._selected = false;
  23. this.isTransform = true;
  24. this._mouseDownPos = new lib_2.SPoint();
  25. this.cursor = "default";
  26. this._lastGrab = null;
  27. if (parent) {
  28. this.parent = parent;
  29. }
  30. }
  31. get scene() {
  32. if (null != this._parent) {
  33. return this._parent.scene;
  34. }
  35. else {
  36. return this._scene;
  37. }
  38. }
  39. set scene(v) {
  40. this._scene = v;
  41. this.update();
  42. }
  43. get parent() {
  44. return this._parent;
  45. }
  46. set parent(v) {
  47. if (this.parent == v) {
  48. return;
  49. }
  50. if (this._parent != null) {
  51. let i = this._parent.children.indexOf(this);
  52. this._parent.children.splice(i, 1);
  53. }
  54. this._parent = v;
  55. this.update();
  56. if (this._parent != null) {
  57. this._parent.children.push(this);
  58. this._parent.children.sort(SGraphItem.sortItemZOrder);
  59. }
  60. }
  61. get zOrder() {
  62. return this._zOrder;
  63. }
  64. set zOrder(v) {
  65. this._zOrder = v;
  66. if (this._parent != null) {
  67. this._parent.children.sort(SGraphItem.sortItemZOrder);
  68. }
  69. this.update();
  70. }
  71. get x() {
  72. return this.pos.x;
  73. }
  74. set x(v) {
  75. if (this.pos.x == v) {
  76. return;
  77. }
  78. let old = this.pos.x;
  79. this.pos.x = v;
  80. this.update();
  81. }
  82. get y() {
  83. return this.pos.y;
  84. }
  85. set y(v) {
  86. if (this.pos.y == v) {
  87. return;
  88. }
  89. let old = this.pos.y;
  90. this.pos.y = v;
  91. this.update();
  92. }
  93. get inverseScale() {
  94. return this._inverseScale;
  95. }
  96. get rotate() {
  97. return this._rotate;
  98. }
  99. set rotate(v) {
  100. this._rotate = v;
  101. this.update();
  102. }
  103. get visible() {
  104. return this._visible;
  105. }
  106. set visible(v) {
  107. this._visible = v;
  108. this.update();
  109. }
  110. get selected() {
  111. return this._selected && this.selectable && this.enabled;
  112. }
  113. set selected(value) {
  114. if (this.selected == value) {
  115. return;
  116. }
  117. this._selected = value;
  118. this.update();
  119. }
  120. onPaint(painter, rect) {
  121. this.onDraw(painter);
  122. for (let item of this.children) {
  123. if (!item.visible) {
  124. continue;
  125. }
  126. painter.save();
  127. try {
  128. painter.translate(item.x, item.y);
  129. painter.scale(item.scale, item.scale);
  130. painter.rotate(item.rotate);
  131. if (!item.isTransform) {
  132. let matrix = painter.worldTransform;
  133. item._inverseScale = 1.0 / matrix.a;
  134. painter.scale(item._inverseScale, item._inverseScale);
  135. }
  136. item.onPaint(painter, rect);
  137. }
  138. catch (e) {
  139. console.log(e);
  140. }
  141. painter.restore();
  142. }
  143. }
  144. onDraw(painter) { }
  145. hide() {
  146. this.visible = false;
  147. }
  148. show() {
  149. this.visible = true;
  150. }
  151. update() {
  152. if (null != this.scene) {
  153. const view = this.scene.view;
  154. if (null != view) {
  155. view.update();
  156. }
  157. }
  158. }
  159. boundingRect() {
  160. return new lib_2.SRect(0, 0, 10, 10);
  161. }
  162. moveTo(x, y) {
  163. this.x = x;
  164. this.y = y;
  165. }
  166. contains(x, y) {
  167. return this.boundingRect().contains(x, y);
  168. }
  169. itemPath() {
  170. if (this.parent != null) {
  171. let list = this.parent.itemPath();
  172. list.push(this);
  173. return list;
  174. }
  175. return [this];
  176. }
  177. mapFromScene(x, y) {
  178. const m = this.scene2itemMattrix();
  179. return new lib_2.SPoint(x, y).matrixTransform(m.inversed());
  180. }
  181. mapToScene(x, y) {
  182. if (this.parent == null) {
  183. return new lib_2.SPoint(x, y);
  184. }
  185. const m = this.scene2itemMattrix();
  186. return new lib_2.SPoint(x, y).matrixTransform(m);
  187. }
  188. scene2itemMattrix() {
  189. let m = new lib_1.SMatrix();
  190. let list = this.itemPath();
  191. for (let item of list) {
  192. m.translate(item.x, item.y);
  193. m.scale(item.scale, item.scale);
  194. m.rotate(item.rotate);
  195. if (!item.isTransform) {
  196. m.scale(item._inverseScale, item._inverseScale);
  197. }
  198. }
  199. return m;
  200. }
  201. onClick(event) {
  202. for (let i = this.children.length - 1; i >= 0; i--) {
  203. let item = this.children[i];
  204. if (!this.acceptEvent() || !item.visible) {
  205. continue;
  206. }
  207. let ce = SGraphItem.toChildMouseEvent(item, event);
  208. if (item.contains(ce.x, ce.y) && item.onClick(ce)) {
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. onDoubleClick(event) {
  215. for (let i = this.children.length - 1; i >= 0; i--) {
  216. let item = this.children[i];
  217. if (!this.acceptEvent() || !item.visible) {
  218. continue;
  219. }
  220. let ce = SGraphItem.toChildMouseEvent(item, event);
  221. if (item.contains(ce.x, ce.y) && item.onDoubleClick(ce)) {
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. onMouseDown(event) {
  228. for (let i = this.children.length - 1; i >= 0; i--) {
  229. try {
  230. let item = this.children[i];
  231. if (!this.acceptEvent() || !item.visible) {
  232. continue;
  233. }
  234. let ce = SGraphItem.toChildMouseEvent(item, event);
  235. if (item.contains(ce.x, ce.y) && item.onMouseDown(ce)) {
  236. return true;
  237. }
  238. }
  239. catch (e) {
  240. console.log(e);
  241. }
  242. }
  243. let select = false;
  244. if (this.selectable) {
  245. select = this.clickSelect(event);
  246. }
  247. if (this.moveable) {
  248. this._mouseDownPos = new lib_2.SPoint(event.x, event.y);
  249. this._isMoving = true;
  250. if (this.scene) {
  251. this._lastGrab = this.scene.grabItem;
  252. }
  253. this.grabItem(this);
  254. return true;
  255. }
  256. return select;
  257. }
  258. onMouseMove(event) {
  259. for (let i = this.children.length - 1; i >= 0; i--) {
  260. let item = this.children[i];
  261. if (!this.acceptEvent() || !item.visible) {
  262. continue;
  263. }
  264. let ce = SGraphItem.toChildMouseEvent(item, event);
  265. if (item.contains(ce.x, ce.y) && item.onMouseMove(ce)) {
  266. return true;
  267. }
  268. }
  269. if (this.scene && this.scene.view) {
  270. this.scene.view.cursor = this.cursor;
  271. }
  272. if (event.buttons & lib_1.SMouseButton.LeftButton &&
  273. this.moveable &&
  274. this._isMoving) {
  275. let old = new lib_2.SPoint(this.pos.x, this.pos.y);
  276. const mp = this.toParentChange(event.x - this._mouseDownPos.x, event.y - this._mouseDownPos.y);
  277. this.moveTo(this.pos.x + mp.x, this.pos.y + mp.y);
  278. this.$emit("onMove", old, this.pos);
  279. }
  280. const scene = this.scene;
  281. if (null != scene) {
  282. if (scene.hoverItem == null || scene.hoverItem !== this) {
  283. if (scene.hoverItem != null) {
  284. scene.hoverItem.onMouseLeave(event);
  285. }
  286. this.onMouseEnter(event);
  287. scene.hoverItem = this;
  288. }
  289. }
  290. return true;
  291. }
  292. onMouseUp(event) {
  293. for (let i = this.children.length - 1; i >= 0; i--) {
  294. let item = this.children[i];
  295. if (!this.acceptEvent() || !item.visible) {
  296. continue;
  297. }
  298. let ce = SGraphItem.toChildMouseEvent(item, event);
  299. if (item.contains(ce.x, ce.y) && item.onMouseUp(ce)) {
  300. return true;
  301. }
  302. }
  303. this._isMoving = false;
  304. this.releaseItem();
  305. if (this.scene) {
  306. this.scene.grabItem = this._lastGrab;
  307. }
  308. return false;
  309. }
  310. onMouseEnter(event) {
  311. return false;
  312. }
  313. onMouseLeave(event) {
  314. return false;
  315. }
  316. onContextMenu(event) {
  317. for (let i = this.children.length - 1; i >= 0; i--) {
  318. let item = this.children[i];
  319. if (!this.acceptEvent() || !item.visible) {
  320. continue;
  321. }
  322. let ce = SGraphItem.toChildMouseEvent(item, event);
  323. if (item.contains(ce.x, ce.y) && item.onContextMenu(ce)) {
  324. return true;
  325. }
  326. }
  327. return false;
  328. }
  329. onKeyDown(event) { }
  330. onKeyPress(event) { }
  331. onKeyUp(event) { }
  332. cancelOperate() { }
  333. toData() {
  334. return null;
  335. }
  336. moveToOrigin(x, y) {
  337. if (this.children && this.children.length) {
  338. this.children.forEach(it => {
  339. it.moveToOrigin(x, y);
  340. });
  341. }
  342. }
  343. static sortItemZOrder(a, b) {
  344. return a.zOrder - b.zOrder;
  345. }
  346. static toChildMouseEvent(child, event) {
  347. let ce = new lib_1.SMouseEvent(event);
  348. ce.matrix.translate(child.x, child.y);
  349. ce.matrix.scale(child.scale, child.scale);
  350. ce.matrix.rotate(0, 0, child.rotate);
  351. if (!child.isTransform) {
  352. ce.matrix.scale(child._inverseScale, child._inverseScale);
  353. }
  354. const mp = new lib_2.SPoint(event.offsetX, event.offsetY).matrixTransform(ce.matrix.inversed());
  355. ce.x = mp.x;
  356. ce.y = mp.y;
  357. return ce;
  358. }
  359. grabItem(item) {
  360. if (this.scene != null) {
  361. this.scene.grabItem = item;
  362. }
  363. }
  364. releaseItem() {
  365. if (this.scene != null) {
  366. this.scene.grabItem = null;
  367. }
  368. }
  369. toParentChange(x, y) {
  370. const m = new lib_1.SMatrix();
  371. m.scale(this.scale, this.scale);
  372. if (!this.isTransform) {
  373. m.scale(this._inverseScale, this._inverseScale);
  374. }
  375. m.rotate(this.rotate);
  376. const mp = new lib_2.SPoint(x, y).matrixTransform(m);
  377. return new lib_2.SPoint(mp.x, mp.y);
  378. }
  379. acceptEvent() {
  380. return this.visible && this.enabled;
  381. }
  382. clickSelect(event) {
  383. if (!this.selectable || event.buttons != lib_1.SMouseButton.LeftButton) {
  384. return false;
  385. }
  386. const scene = this.scene;
  387. if (scene == null) {
  388. return false;
  389. }
  390. if (event.ctrlKey) {
  391. scene.selectContainer.toggleItem(this);
  392. }
  393. else {
  394. scene.selectContainer.setItem(this);
  395. }
  396. return true;
  397. }
  398. }
  399. exports.SGraphItem = SGraphItem;