-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathCoordinatesTranslator.ts
More file actions
79 lines (66 loc) · 2.87 KB
/
CoordinatesTranslator.ts
File metadata and controls
79 lines (66 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Copyright 2020 Bonitasoft S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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.
*/
import type { BpmnGraph } from '../BpmnGraph';
import type { mxCell, mxPoint as mxPointType } from 'mxgraph';
import { mxPoint } from '../initializer';
/**
* @internal
*/
export default class CoordinatesTranslator {
constructor(readonly graph: BpmnGraph) {}
/**
* Compute an absolute coordinate in relative coordinates in the parent cell referential.
* @param parent the cell to use for the new coordinate referential
* @param absoluteCoordinate
*/
computeRelativeCoordinates(parent: mxCell, absoluteCoordinate: mxPointType): mxPointType {
const translateForRoot = this.getTranslateForRoot(parent);
const relativeX = absoluteCoordinate.x + translateForRoot.x;
const relativeY = absoluteCoordinate.y + translateForRoot.y;
return new mxPoint(relativeX, relativeY);
}
// Returns the translation to be applied to a cell whose Geometry x and y values are expressed with absolute coordinates
// (i.e. related to the graph default parent) you want to assign as parent to the cell passed as argument of this function.
// That way, you will be able to express the cell coordinates as relative to its parent cell.
//
// This implementation is taken from the example described in the documentation of mxgraph#getTranslateForRoot (4.1.1)
// The translation is generally negative
private getTranslateForRoot(cell: mxCell): mxPointType {
const model = this.graph.getModel();
const offset = new mxPoint(0, 0);
while (cell != null) {
const geo = model.getGeometry(cell);
if (geo != null) {
offset.x -= geo.x;
offset.y -= geo.y;
}
cell = model.getParent(cell);
}
return offset;
}
/**
* Compute the center of the provided `Cell` for absolute geometry: this is the center point of a segment whose edges
* are the terminal points of the Cell geometry points. Returns `undefined` if the 2 terminal points are not available.
*
* The center coordinates are given in the same referential as the `Cell`, so relative to its parent.
*/
computeEdgeCenter(edge: mxCell): mxPointType {
const points: mxPointType[] = edge.geometry.points;
const p0 = points[0];
const pe = points.at(-1);
// p0 and pe are always set (all tests passed so far)
const dx = pe.x - p0.x;
const dy = pe.y - p0.y;
return new mxPoint(p0.x + dx / 2, p0.y + dy / 2);
}
}