123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- "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;
|