1 |
|
2 |
|
3 |
|
4 | import * as go from '../release/go-module.js';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export class SpotRotatingTool extends go.RotatingTool {
|
14 | constructor() {
|
15 | super();
|
16 | this._originalRotationSpot = go.Spot.Default;
|
17 | const $ = go.GraphObject.make;
|
18 | this._spotAdornmentTemplate =
|
19 | $(go.Adornment, "Spot", { locationSpot: go.Spot.Center, cursor: "move" }, $(go.Shape, "Circle", { fill: "lightblue", stroke: "dodgerblue", width: 10, height: 10 }), $(go.Shape, "Circle", { fill: "dodgerblue", strokeWidth: 0, width: 4, height: 4 }));
|
20 | }
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 | updateAdornments(part) {
|
27 | super.updateAdornments(part);
|
28 | if (part === null)
|
29 | return;
|
30 | if (part.isSelected && !this.diagram.isReadOnly) {
|
31 | const rotateObj = part.rotateObject;
|
32 | if (rotateObj !== null && part.canRotate() && part.actualBounds.isReal() &&
|
33 | part.isVisible() && rotateObj.actualBounds.isReal() && rotateObj.isVisibleObject()) {
|
34 | let ad = part.findAdornment("RotateSpot");
|
35 | if (ad === null || ad.adornedObject !== rotateObj) {
|
36 | ad = this._spotAdornmentTemplate.copy();
|
37 | ad.adornedObject = part.rotateObject;
|
38 | }
|
39 | if (ad !== null) {
|
40 | ad.location = this.computeRotationPoint(ad.adornedObject);
|
41 | part.addAdornment("RotateSpot", ad);
|
42 | return;
|
43 | }
|
44 | }
|
45 | }
|
46 | part.removeAdornment("RotateSpot");
|
47 | }
|
48 | ;
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | computeAdornmentLocation(obj) {
|
58 | let p = this.rotationPoint;
|
59 | if (!p.isReal())
|
60 | p = this.computeRotationPoint(obj);
|
61 | const q = obj.getLocalPoint(p);
|
62 |
|
63 | q.x = Math.max(obj.naturalBounds.right, q.x) + this.handleDistance;
|
64 | return obj.getDocumentPoint(q);
|
65 | }
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 | canStart() {
|
72 | if (!this.isEnabled)
|
73 | return false;
|
74 | const diagram = this.diagram;
|
75 | if (diagram.isReadOnly)
|
76 | return false;
|
77 | if (!diagram.allowRotate)
|
78 | return false;
|
79 | if (!diagram.lastInput.left)
|
80 | return false;
|
81 | let h = this.findToolHandleAt(diagram.firstInput.documentPoint, this.name);
|
82 | if (h !== null)
|
83 | return true;
|
84 | h = this.findToolHandleAt(diagram.firstInput.documentPoint, "RotateSpot");
|
85 | return (h !== null);
|
86 | }
|
87 | |
88 |
|
89 |
|
90 | doActivate() {
|
91 |
|
92 | this.handle = this.findToolHandleAt(this.diagram.firstInput.documentPoint, "RotateSpot");
|
93 | if (this.handle !== null) {
|
94 | const ad = this.handle.part;
|
95 | if (ad.adornedObject !== null) {
|
96 | const part = ad.adornedPart;
|
97 | if (part !== null)
|
98 | this._originalRotationSpot = part.rotationSpot;
|
99 | }
|
100 | }
|
101 |
|
102 | super.doActivate();
|
103 | }
|
104 | |
105 |
|
106 |
|
107 | doCancel() {
|
108 | if (this.adornedObject !== null) {
|
109 | const part = this.adornedObject.part;
|
110 | if (part !== null) {
|
111 | part.rotationSpot = this._originalRotationSpot;
|
112 | this.rotationPoint.set(this.computeRotationPoint(this.adornedObject));
|
113 | this.updateAdornments(part);
|
114 | }
|
115 | }
|
116 | super.doCancel();
|
117 | }
|
118 | |
119 |
|
120 |
|
121 | doMouseMove() {
|
122 | if (this.isActive) {
|
123 | if (this.handle !== null && this.handle.part && this.handle.part.category === "RotateSpot") {
|
124 |
|
125 | this.shiftRotationPoint();
|
126 | }
|
127 | else {
|
128 | super.doMouseMove();
|
129 | }
|
130 | }
|
131 | }
|
132 | |
133 |
|
134 |
|
135 | doMouseUp() {
|
136 | if (this.isActive) {
|
137 | if (this.handle !== null && this.handle.part && this.handle.part.category === "RotateSpot") {
|
138 |
|
139 | this.shiftRotationPoint();
|
140 | this.transactionResult = "Shifted rotationSpot";
|
141 | this.stopTool();
|
142 | }
|
143 | else {
|
144 | super.doMouseUp();
|
145 | }
|
146 | }
|
147 | }
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | shiftRotationPoint() {
|
159 | const dp = this.diagram.lastInput.documentPoint;
|
160 | const obj = this.adornedObject;
|
161 | if (obj === null)
|
162 | return;
|
163 | const w = obj.naturalBounds.width || 1;
|
164 | const h = obj.naturalBounds.height || 1;
|
165 | const part = obj.part;
|
166 | if (part === null)
|
167 | return;
|
168 | const op = obj.getLocalPoint(dp);
|
169 | const fx = (op.x < 0) ? 0 : (op.x > w ? 1 : op.x / w);
|
170 | const fy = (op.y < 0) ? 0 : (op.y > h ? 1 : op.y / h);
|
171 | const ox = (op.x < 0) ? op.x : (op.x > w ? op.x - w : 0);
|
172 | const oy = (op.y < 0) ? op.y : (op.y > h ? op.y - h : 0);
|
173 | part.rotationSpot = new go.Spot(fx, fy, ox, oy);
|
174 | this.rotationPoint.set(this.computeRotationPoint(obj));
|
175 | this.updateAdornments(part);
|
176 | }
|
177 | }
|