![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/html/libs/absol-form/js/ciruiteditor/diagrameditor/controller/ |
Upload File : |
import GCore from "../../../core/GCore"; import Color from "absol/src/Color/Color"; import {randomUniqueIdent} from "../../../core/utils"; import Vec2 from "absol/src/Math/Vec2"; function NewLineController(editor) { this.editor = editor; for (var i in this) { if (i.startsWith('ev_')) { this[i] = this[i].bind(this); } } this.isMouseListenning = false; this.u = null; this.v = null; this.lineElt = null; this.mousePos = null; this._prevClick = null; } NewLineController.prototype.addNodeEventControl = function (nodeElt) { nodeElt.on('presspin', this.ev_pressPin.bind(this, nodeElt)); }; NewLineController.prototype.ev_pressPin = function (nodeElt, event) { this.mousePos = new Vec2(event.clientX, event.clientY); var cClick = nodeElt.nodeId + '.' + event.mode + '.' + event.pinName; if (this._prevClick === cClick) { this.cancel(); return; } this._prevClick = cClick; if (event.mode === 'out') { this.u = { nodeElt: nodeElt, pinName: event.pinName } } else { this.v = { nodeElt: nodeElt, pinName: event.pinName } } var id; if (!this.lineElt) { id = randomUniqueIdent(); this.lineElt = GCore._({ tag: 'g', class: ['as-cde-line-group', 'as-new'], attr: { 'data-line-id': id }, style: { '--line-color': new Color([Math.random(), Math.random(), Math.random(), 1]).toString() }, child: [ { tag: 'path', class: 'as-cde-hit-line', }, { tag: 'path', class: 'as-cde-line' } ], props: { lineId: id } }); this.editor.$lineLayer.addChild(this.lineElt); } if (!!this.u !== !!this.v && !this.isMouseListenning) { document.body.addEventListener('mousemove', this.ev_mouseMove); document.body.addEventListener('wheel', this.ev_mouseMove); this.isMouseListenning = true; } this._updateLinePath(); if (this.u && this.v) { this.flushToEditor(); } }; NewLineController.prototype.ev_mouseMove = function (event) { this.mousePos = new Vec2(event.clientX, event.clientY); this._updateLinePath(); }; NewLineController.prototype._updateLinePath = function () { var viewportBound = this.editor.$lineLayer.getBoundingClientRect(); var pInViewport = this.mousePos.sub(new Vec2(viewportBound.left, viewportBound.top)); var p = pInViewport.sub(this.editor.$lineLayer.box.origin); var startPos; var endPos; if (this.u) { startPos = this.u.nodeElt.position.add(this.u.nodeElt.pinHolders.out[this.u.pinName].pos); } if (this.v) { endPos = this.v.nodeElt.position.add(this.v.nodeElt.pinHolders.in[this.v.pinName].pos); } startPos = startPos || p; endPos = endPos || p; var dx = 50; if (startPos.x > endPos.x) dx += Math.min(200, startPos.x - endPos.x) / 2; var path = ['M', startPos.x, startPos.y, 'C', startPos.x + dx, startPos.y, ',', endPos.x - dx, endPos.y, ',', endPos.x, endPos.y].join(' '); this.lineElt.lastChild.attr('d', path); this.lineElt.firstChild.attr('d', path); }; NewLineController.prototype.flushToEditor = function () { var u = this.u.nodeElt.name; var v = this.v.nodeElt.name; var uPin = this.u.pinName; var vPin = this.v.pinName; var lineId = this.lineElt.lineId; var line = { u: u, v: v, uPin: uPin, vPin: vPin, id: lineId }; if (!this.isDuplicatedLine(line)) { this.lineElt.uId = this.u.nodeElt.nodeId; this.lineElt.vId = this.v.nodeElt.nodeId; this.lineElt.uPin = uPin; this.lineElt.vPin = vPin; this.lineElt.removeClass('as-new'); this.editor.lines.push(line); this.editor.$lines[lineId] = this.lineElt; this.u.nodeElt.lines[lineId] = line; this.v.nodeElt.lines[lineId] = line; this.editor.lineSelectController.addLineControlEvent(this.lineElt); } else { this.lineElt.remove(); } if (this.isMouseListenning) { document.body.removeEventListener('mousemove', this.ev_mouseMove); document.body.removeEventListener('wheel', this.ev_mouseMove); this.isMouseListenning = false; } this.u = null; this.v = null; this.lineElt = null; this._prevClick = null; }; NewLineController.prototype.isDuplicatedLine = function (line) { var nodes = this.editor.$nodes; return this.editor.lines.some(function (other) { var uName = other.u; if (nodes[uName]) { uName = nodes[uName].name; } var vName = other.v; if (nodes[vName]) { vName = nodes[vName].name; } return uName === line.u && vName === line.v && other.uPin === line.uPin && other.vPin === line.vPin; }.bind(this)); }; NewLineController.prototype.cancel = function () { if (this.isMouseListenning) { document.body.removeEventListener('mousemove', this.ev_mouseMove); document.body.removeEventListener('wheel', this.ev_mouseMove); this.isMouseListenning = false; } this.u = null; this.v = null; this._prevClick = null; if (this.lineElt) { this.lineElt.remove(); this.lineElt = null; } }; export default NewLineController;