2021-07-16 16:41:58 +02:00
|
|
|
/*
|
2021-12-25 23:39:34 +01:00
|
|
|
* Copyright [2021] [wisemapping]
|
2021-07-16 16:41:58 +02:00
|
|
|
*
|
|
|
|
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
|
|
|
|
* It is basically the Apache License, Version 2.0 (the "License") plus the
|
|
|
|
* "powered by wisemapping" text requirement on every single page;
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the license at
|
|
|
|
*
|
|
|
|
* http://www.wisemapping.org/license
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2021-12-19 17:31:29 +01:00
|
|
|
import { Elipse, Line, Point } from '@wisemapping/web2d';
|
2021-12-04 01:43:50 +01:00
|
|
|
import { $defined } from '@wisemapping/core-js';
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-02 01:41:56 +01:00
|
|
|
import Shape from './util/Shape';
|
|
|
|
import ActionDispatcher from './ActionDispatcher';
|
2022-02-10 05:02:57 +01:00
|
|
|
import Workspace from './Workspace';
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2021-12-05 00:39:20 +01:00
|
|
|
class ControlPoint {
|
2022-02-10 05:02:57 +01:00
|
|
|
private control1: Elipse;
|
|
|
|
|
|
|
|
private control2: Elipse;
|
|
|
|
|
|
|
|
private _controlPointsController: Elipse[];
|
|
|
|
|
|
|
|
private _controlLines: Line[];
|
|
|
|
|
|
|
|
private _isBinded: boolean;
|
|
|
|
|
|
|
|
_line: Line;
|
|
|
|
|
|
|
|
private _workspace: Workspace;
|
|
|
|
|
|
|
|
private _endPoint: any[];
|
|
|
|
|
|
|
|
private _orignalCtrlPoint: any;
|
|
|
|
|
|
|
|
private _controls: any;
|
|
|
|
|
|
|
|
private _mouseMoveFunction: (e: Event) => void;
|
|
|
|
|
|
|
|
private _mouseUpFunction: (e: Event) => void;
|
|
|
|
|
2021-12-05 00:39:20 +01:00
|
|
|
constructor() {
|
2022-01-26 20:25:11 +01:00
|
|
|
this.control1 = new Elipse({
|
2021-10-05 02:05:34 +02:00
|
|
|
width: 6,
|
|
|
|
height: 6,
|
|
|
|
stroke: '1 solid #6589de',
|
|
|
|
fillColor: 'gray',
|
|
|
|
visibility: false,
|
|
|
|
});
|
2022-01-26 20:25:11 +01:00
|
|
|
this.control1.setCursor('pointer');
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-01-26 20:25:11 +01:00
|
|
|
this.control2 = new Elipse({
|
2021-10-05 02:05:34 +02:00
|
|
|
width: 6,
|
|
|
|
height: 6,
|
|
|
|
stroke: '1 solid #6589de',
|
|
|
|
fillColor: 'gray',
|
|
|
|
visibility: false,
|
|
|
|
});
|
2022-01-26 20:25:11 +01:00
|
|
|
this.control2.setCursor('pointer');
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-01-26 20:25:11 +01:00
|
|
|
this._controlPointsController = [this.control1, this.control2];
|
2021-12-02 01:41:56 +01:00
|
|
|
this._controlLines = [
|
2021-12-19 17:31:29 +01:00
|
|
|
new Line({ strokeColor: '#6589de', strokeWidth: 1, opacity: 0.3 }),
|
|
|
|
new Line({ strokeColor: '#6589de', strokeWidth: 1, opacity: 0.3 }),
|
2021-12-02 01:41:56 +01:00
|
|
|
];
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
this._isBinded = false;
|
|
|
|
const me = this;
|
|
|
|
this._controlPointsController[0].addEvent('mousedown', (event) => {
|
2021-12-02 01:41:56 +01:00
|
|
|
me._mouseDown(event, ControlPoint.FROM, me);
|
2021-10-05 02:05:34 +02:00
|
|
|
});
|
|
|
|
this._controlPointsController[0].addEvent('click', (event) => {
|
2021-12-02 01:41:56 +01:00
|
|
|
me._mouseClick(event);
|
2021-10-05 02:05:34 +02:00
|
|
|
});
|
|
|
|
this._controlPointsController[0].addEvent('dblclick', (event) => {
|
2021-12-02 01:41:56 +01:00
|
|
|
me._mouseClick(event);
|
2021-10-05 02:05:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this._controlPointsController[1].addEvent('mousedown', (event) => {
|
2021-12-02 01:41:56 +01:00
|
|
|
me._mouseDown(event, ControlPoint.TO, me);
|
2021-10-05 02:05:34 +02:00
|
|
|
});
|
|
|
|
this._controlPointsController[1].addEvent('click', (event) => {
|
2021-12-02 01:41:56 +01:00
|
|
|
me._mouseClick(event);
|
2021-10-05 02:05:34 +02:00
|
|
|
});
|
|
|
|
this._controlPointsController[1].addEvent('dblclick', (event) => {
|
2021-12-02 01:41:56 +01:00
|
|
|
me._mouseClick(event);
|
2021-10-05 02:05:34 +02:00
|
|
|
});
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
setLine(line: Line) {
|
2021-10-05 02:05:34 +02:00
|
|
|
if ($defined(this._line)) {
|
|
|
|
this._removeLine();
|
|
|
|
}
|
|
|
|
this._line = line;
|
|
|
|
this._createControlPoint();
|
|
|
|
this._endPoint = [];
|
|
|
|
this._orignalCtrlPoint = [];
|
2021-12-14 18:06:09 +01:00
|
|
|
this._orignalCtrlPoint[0] = { ...this._controls[0] };
|
|
|
|
this._orignalCtrlPoint[1] = { ...this._controls[1] };
|
|
|
|
this._endPoint[0] = { ...this._line.getLine().getFrom() };
|
|
|
|
this._endPoint[1] = { ...this._line.getLine().getTo() };
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-01-26 20:25:11 +01:00
|
|
|
setControlPointTestId(ctrlPoint1, ctrlPoint2) {
|
|
|
|
this.control1.setTestId(ctrlPoint1);
|
|
|
|
this.control2.setTestId(ctrlPoint2);
|
|
|
|
}
|
|
|
|
|
2021-10-05 02:05:34 +02:00
|
|
|
redraw() {
|
|
|
|
if ($defined(this._line)) this._createControlPoint();
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
private _createControlPoint() {
|
2021-10-05 02:05:34 +02:00
|
|
|
this._controls = this._line.getLine().getControlPoints();
|
|
|
|
let pos = this._line.getLine().getFrom();
|
2021-12-02 01:41:56 +01:00
|
|
|
this._controlPointsController[0].setPosition(
|
|
|
|
this._controls[ControlPoint.FROM].x + pos.x,
|
|
|
|
this._controls[ControlPoint.FROM].y + pos.y - 3,
|
|
|
|
);
|
2021-10-05 02:05:34 +02:00
|
|
|
this._controlLines[0].setFrom(pos.x, pos.y);
|
2021-12-02 01:41:56 +01:00
|
|
|
this._controlLines[0].setTo(
|
|
|
|
this._controls[ControlPoint.FROM].x + pos.x + 3,
|
|
|
|
this._controls[ControlPoint.FROM].y + pos.y,
|
|
|
|
);
|
2021-10-05 02:05:34 +02:00
|
|
|
pos = this._line.getLine().getTo();
|
|
|
|
this._controlLines[1].setFrom(pos.x, pos.y);
|
2021-12-02 01:41:56 +01:00
|
|
|
this._controlLines[1].setTo(
|
|
|
|
this._controls[ControlPoint.TO].x + pos.x + 3,
|
|
|
|
this._controls[ControlPoint.TO].y + pos.y,
|
|
|
|
);
|
|
|
|
this._controlPointsController[1].setPosition(
|
|
|
|
this._controls[ControlPoint.TO].x + pos.x,
|
|
|
|
this._controls[ControlPoint.TO].y + pos.y - 3,
|
|
|
|
);
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
private _removeLine() {
|
2022-01-24 20:24:16 +01:00
|
|
|
// Overwrite default behaviour ...
|
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
private _mouseDown(event: Event, point, me) {
|
2021-10-05 02:05:34 +02:00
|
|
|
if (!this._isBinded) {
|
|
|
|
this._isBinded = true;
|
2021-12-19 18:07:01 +01:00
|
|
|
this._mouseMoveFunction = (e) => {
|
|
|
|
me._mouseMoveEvent(e, point, me);
|
2021-10-05 02:05:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this._workspace.getScreenManager().addEvent('mousemove', this._mouseMoveFunction);
|
2022-02-10 05:02:57 +01:00
|
|
|
this._mouseUpFunction = (e: Event) => {
|
2021-12-19 18:07:01 +01:00
|
|
|
me._mouseUp(e, point, me);
|
2021-10-05 02:05:34 +02:00
|
|
|
};
|
|
|
|
this._workspace.getScreenManager().addEvent('mouseup', this._mouseUpFunction);
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return false;
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
private _mouseMoveEvent(event: MouseEvent, point: Point) {
|
2021-10-05 02:05:34 +02:00
|
|
|
const screen = this._workspace.getScreenManager();
|
|
|
|
const pos = screen.getWorkspaceMousePosition(event);
|
|
|
|
|
|
|
|
let cords;
|
2021-12-20 21:54:31 +01:00
|
|
|
if (point === 0) {
|
2021-10-05 02:05:34 +02:00
|
|
|
cords = Shape.calculateRelationShipPointCoordinates(this._line.getSourceTopic(), pos);
|
|
|
|
this._line.setFrom(cords.x, cords.y);
|
2021-12-19 17:31:29 +01:00
|
|
|
this._line.setSrcControlPoint(new Point(pos.x - cords.x, pos.y - cords.y));
|
2021-10-05 02:05:34 +02:00
|
|
|
} else {
|
|
|
|
cords = Shape.calculateRelationShipPointCoordinates(this._line.getTargetTopic(), pos);
|
|
|
|
this._line.setTo(cords.x, cords.y);
|
2021-12-19 17:31:29 +01:00
|
|
|
this._line.setDestControlPoint(new Point(pos.x - cords.x, pos.y - cords.y));
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 01:41:56 +01:00
|
|
|
this._controls[point].x = pos.x - cords.x;
|
|
|
|
this._controls[point].y = pos.y - cords.y;
|
2021-10-05 02:05:34 +02:00
|
|
|
this._controlPointsController[point].setPosition(pos.x - 5, pos.y - 3);
|
|
|
|
this._controlLines[point].setFrom(cords.x, cords.y);
|
|
|
|
this._controlLines[point].setTo(pos.x - 2, pos.y);
|
|
|
|
this._line.getLine().updateLine(point);
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
private _mouseUp(event, point) {
|
2021-10-05 02:05:34 +02:00
|
|
|
this._workspace.getScreenManager().removeEvent('mousemove', this._mouseMoveFunction);
|
|
|
|
this._workspace.getScreenManager().removeEvent('mouseup', this._mouseUpFunction);
|
|
|
|
|
|
|
|
const actionDispatcher = ActionDispatcher.getInstance();
|
|
|
|
actionDispatcher.moveControlPoint(this, point);
|
|
|
|
this._isBinded = false;
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
|
|
|
_mouseClick(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return false;
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
setVisibility(visible: boolean) {
|
2021-10-05 02:05:34 +02:00
|
|
|
if (visible) {
|
|
|
|
this._controlLines[0].moveToFront();
|
|
|
|
this._controlLines[1].moveToFront();
|
|
|
|
this._controlPointsController[0].moveToFront();
|
|
|
|
this._controlPointsController[1].moveToFront();
|
2021-07-16 16:41:58 +02:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
this._controlPointsController[0].setVisibility(visible);
|
|
|
|
this._controlPointsController[1].setVisibility(visible);
|
|
|
|
this._controlLines[0].setVisibility(visible);
|
|
|
|
this._controlLines[1].setVisibility(visible);
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
addToWorkspace(workspace: Workspace): void {
|
2021-10-05 02:05:34 +02:00
|
|
|
this._workspace = workspace;
|
|
|
|
workspace.append(this._controlPointsController[0]);
|
|
|
|
workspace.append(this._controlPointsController[1]);
|
|
|
|
workspace.append(this._controlLines[0]);
|
|
|
|
workspace.append(this._controlLines[1]);
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
removeFromWorkspace(workspace: Workspace) {
|
2021-10-05 02:05:34 +02:00
|
|
|
this._workspace = null;
|
|
|
|
workspace.removeChild(this._controlPointsController[0]);
|
|
|
|
workspace.removeChild(this._controlPointsController[1]);
|
|
|
|
workspace.removeChild(this._controlLines[0]);
|
|
|
|
workspace.removeChild(this._controlLines[1]);
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
getControlPoint(index: number): ControlPoint {
|
2021-10-05 02:05:34 +02:00
|
|
|
return this._controls[index];
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
getOriginalEndPoint(index: number) {
|
2021-10-05 02:05:34 +02:00
|
|
|
return this._endPoint[index];
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
getOriginalCtrlPoint(index: number): ControlPoint {
|
2021-10-05 02:05:34 +02:00
|
|
|
return this._orignalCtrlPoint[index];
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2022-02-10 05:02:57 +01:00
|
|
|
static FROM = 0;
|
|
|
|
|
|
|
|
static TO = 1;
|
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
|
|
|
export default ControlPoint;
|