"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SGraphItem = void 0; const lib_1 = require("@persagy-web/base/lib"); const lib_2 = require("@persagy-web/draw/lib"); class SGraphItem extends lib_1.SObject { constructor(parent = null) { super(); this._scene = null; this._parent = null; this.children = []; this._zOrder = 0; this.pos = new lib_2.SPoint(0, 0); this.scale = 1; this._inverseScale = 1; this._rotate = 0; this._visible = true; this.moveable = false; this._isMoving = false; this.enabled = true; this.selectable = false; this._selected = false; this.isTransform = true; this._mouseDownPos = new lib_2.SPoint(); this.cursor = "default"; this._lastGrab = null; if (parent) { this.parent = parent; } } get scene() { if (null != this._parent) { return this._parent.scene; } else { return this._scene; } } set scene(v) { this._scene = v; this.update(); } get parent() { return this._parent; } set parent(v) { if (this.parent == v) { return; } if (this._parent != null) { let i = this._parent.children.indexOf(this); this._parent.children.splice(i, 1); } this._parent = v; this.update(); if (this._parent != null) { this._parent.children.push(this); this._parent.children.sort(SGraphItem.sortItemZOrder); } } get zOrder() { return this._zOrder; } set zOrder(v) { this._zOrder = v; if (this._parent != null) { this._parent.children.sort(SGraphItem.sortItemZOrder); } this.update(); } get x() { return this.pos.x; } set x(v) { if (this.pos.x == v) { return; } let old = this.pos.x; this.pos.x = v; this.update(); } get y() { return this.pos.y; } set y(v) { if (this.pos.y == v) { return; } let old = this.pos.y; this.pos.y = v; this.update(); } get inverseScale() { return this._inverseScale; } get rotate() { return this._rotate; } set rotate(v) { this._rotate = v; this.update(); } get visible() { return this._visible; } set visible(v) { this._visible = v; this.update(); } get selected() { return this._selected && this.selectable && this.enabled; } set selected(value) { if (this.selected == value) { return; } this._selected = value; this.update(); } onPaint(painter, rect) { this.onDraw(painter); for (let item of this.children) { if (!item.visible) { continue; } painter.save(); try { painter.translate(item.x, item.y); painter.scale(item.scale, item.scale); painter.rotate(item.rotate); if (!item.isTransform) { let matrix = painter.worldTransform; item._inverseScale = 1.0 / matrix.a; painter.scale(item._inverseScale, item._inverseScale); } item.onPaint(painter, rect); } catch (e) { console.log(e); } painter.restore(); } } onDraw(painter) { } hide() { this.visible = false; } show() { this.visible = true; } update() { if (null != this.scene) { const view = this.scene.view; if (null != view) { view.update(); } } } boundingRect() { return new lib_2.SRect(0, 0, 10, 10); } moveTo(x, y) { this.x = x; this.y = y; } contains(x, y) { return this.boundingRect().contains(x, y); } itemPath() { if (this.parent != null) { let list = this.parent.itemPath(); list.push(this); return list; } return [this]; } mapFromScene(x, y) { const m = this.scene2itemMattrix(); return new lib_2.SPoint(x, y).matrixTransform(m.inversed()); } mapToScene(x, y) { if (this.parent == null) { return new lib_2.SPoint(x, y); } const m = this.scene2itemMattrix(); return new lib_2.SPoint(x, y).matrixTransform(m); } scene2itemMattrix() { let m = new lib_1.SMatrix(); let list = this.itemPath(); for (let item of list) { m.translate(item.x, item.y); m.scale(item.scale, item.scale); m.rotate(item.rotate); if (!item.isTransform) { m.scale(item._inverseScale, item._inverseScale); } } return m; } onClick(event) { for (let i = this.children.length - 1; i >= 0; i--) { let item = this.children[i]; if (!this.acceptEvent() || !item.visible) { continue; } let ce = SGraphItem.toChildMouseEvent(item, event); if (item.contains(ce.x, ce.y) && item.onClick(ce)) { return true; } } return false; } onDoubleClick(event) { for (let i = this.children.length - 1; i >= 0; i--) { let item = this.children[i]; if (!this.acceptEvent() || !item.visible) { continue; } let ce = SGraphItem.toChildMouseEvent(item, event); if (item.contains(ce.x, ce.y) && item.onDoubleClick(ce)) { return true; } } return false; } onMouseDown(event) { for (let i = this.children.length - 1; i >= 0; i--) { try { let item = this.children[i]; if (!this.acceptEvent() || !item.visible) { continue; } let ce = SGraphItem.toChildMouseEvent(item, event); if (item.contains(ce.x, ce.y) && item.onMouseDown(ce)) { return true; } } catch (e) { console.log(e); } } let select = false; if (this.selectable) { select = this.clickSelect(event); } if (this.moveable) { this._mouseDownPos = new lib_2.SPoint(event.x, event.y); this._isMoving = true; if (this.scene) { this._lastGrab = this.scene.grabItem; } this.grabItem(this); return true; } return select; } onMouseMove(event) { for (let i = this.children.length - 1; i >= 0; i--) { let item = this.children[i]; if (!this.acceptEvent() || !item.visible) { continue; } let ce = SGraphItem.toChildMouseEvent(item, event); if (item.contains(ce.x, ce.y) && item.onMouseMove(ce)) { return true; } } if (this.scene && this.scene.view) { this.scene.view.cursor = this.cursor; } if (event.buttons & lib_1.SMouseButton.LeftButton && this.moveable && this._isMoving) { let old = new lib_2.SPoint(this.pos.x, this.pos.y); const mp = this.toParentChange(event.x - this._mouseDownPos.x, event.y - this._mouseDownPos.y); this.moveTo(this.pos.x + mp.x, this.pos.y + mp.y); this.$emit("onMove", old, this.pos); } const scene = this.scene; if (null != scene) { if (scene.hoverItem == null || scene.hoverItem !== this) { if (scene.hoverItem != null) { scene.hoverItem.onMouseLeave(event); } this.onMouseEnter(event); scene.hoverItem = this; } } return true; } onMouseUp(event) { for (let i = this.children.length - 1; i >= 0; i--) { let item = this.children[i]; if (!this.acceptEvent() || !item.visible) { continue; } let ce = SGraphItem.toChildMouseEvent(item, event); if (item.contains(ce.x, ce.y) && item.onMouseUp(ce)) { return true; } } this._isMoving = false; this.releaseItem(); if (this.scene) { this.scene.grabItem = this._lastGrab; } return false; } onMouseEnter(event) { return false; } onMouseLeave(event) { return false; } onContextMenu(event) { for (let i = this.children.length - 1; i >= 0; i--) { let item = this.children[i]; if (!this.acceptEvent() || !item.visible) { continue; } let ce = SGraphItem.toChildMouseEvent(item, event); if (item.contains(ce.x, ce.y) && item.onContextMenu(ce)) { return true; } } return false; } onKeyDown(event) { } onKeyPress(event) { } onKeyUp(event) { } cancelOperate() { } toData() { return null; } moveToOrigin(x, y) { if (this.children && this.children.length) { this.children.forEach(it => { it.moveToOrigin(x, y); }); } } static sortItemZOrder(a, b) { return a.zOrder - b.zOrder; } static toChildMouseEvent(child, event) { let ce = new lib_1.SMouseEvent(event); ce.matrix.translate(child.x, child.y); ce.matrix.scale(child.scale, child.scale); ce.matrix.rotate(0, 0, child.rotate); if (!child.isTransform) { ce.matrix.scale(child._inverseScale, child._inverseScale); } const mp = new lib_2.SPoint(event.offsetX, event.offsetY).matrixTransform(ce.matrix.inversed()); ce.x = mp.x; ce.y = mp.y; return ce; } grabItem(item) { if (this.scene != null) { this.scene.grabItem = item; } } releaseItem() { if (this.scene != null) { this.scene.grabItem = null; } } toParentChange(x, y) { const m = new lib_1.SMatrix(); m.scale(this.scale, this.scale); if (!this.isTransform) { m.scale(this._inverseScale, this._inverseScale); } m.rotate(this.rotate); const mp = new lib_2.SPoint(x, y).matrixTransform(m); return new lib_2.SPoint(mp.x, mp.y); } acceptEvent() { return this.visible && this.enabled; } clickSelect(event) { if (!this.selectable || event.buttons != lib_1.SMouseButton.LeftButton) { return false; } const scene = this.scene; if (scene == null) { return false; } if (event.ctrlKey) { scene.selectContainer.toggleItem(this); } else { scene.selectContainer.setItem(this); } return true; } } exports.SGraphItem = SGraphItem;