Repository navigation
Expand file tree
/
Copy pathEditorControls.js
More file actions
378 lines (339 loc) · 13.8 KB
/
Copy pathEditorControls.js
File metadata and controls
378 lines (339 loc) · 13.8 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import * as THREE from 'three';
// ============================================================================
// Editor camera controls, mouse only:
// - left-drag : pan across the board (clamped to board bounds)
// - right-drag : orbit around the focus point
// - wheel : zoom (clamped min/max distance)
// Smooth-damped spherical camera around a target point on the board plane.
// Modes: 'orbit' (free angle) and 'topdown' (locked overhead).
// ============================================================================
const DEG = Math.PI / 180;
export class EditorControls {
constructor(camera, domElement) {
this.camera = camera;
this.dom = domElement;
this.target = new THREE.Vector3(0, 0, 0);
this.goalTarget = new THREE.Vector3(0, 0, 0);
this.panCenter = new THREE.Vector3(0, 0, 0); // assembly center for pan clamp
// spherical state: radius, phi (from +Y), theta (azimuth)
this.radius = 2850; this.goalRadius = 2850;
this.phi = 55 * DEG; this.goalPhi = 55 * DEG;
this.theta = 45 * DEG; this.goalTheta = 45 * DEG;
this.mode = 'orbit';
this.minRadius = 180;
this.maxRadius = 7000;
this.minPhi = 8 * DEG;
this.maxPhi = 80 * DEG;
this.panLimit = 1024; // set from board size
// Optional presentation framing (Mini Terrain): home pose, pivot height
// and limits derived from the footprint. null = classic board behaviour.
this.home = null;
this.elevationCenter = 0;
this.elevationSpan = 0;
this.onFirstInteract = null;
this.enabled = true; // false while the studio player walks
this.autoOrbit = false; // slow showcase spin (landing page)
this.autoOrbitSpeed = 0.045; // radians per second
this.inputMode = 'all'; // 'all' or 'orbitOnly' while paint mode owns left drag
this.primaryPointerFilter = null; // optional editor-mode gate for left mouse / primary touch
this.wheelFilter = null; // optional editor-mode gate for wheel gestures
this._interacted = false;
this._drag = null; // { button, x, y }
this._touches = new Map(); // active touch pointers for pan / pinch zoom
this._pinch = null; // { x, y, dist, radius }
// bound handlers kept so dispose() can detach them (the canvas DOM node
// outlives the engine across HMR / remounts — inline arrows would leak)
this._onPointerDown = (e) => this._onDown(e);
this._onPointerMove = (e) => this._onMove(e);
this._onPointerUp = (e) => this._onUp(e);
this._onContextMenu = (e) => e.preventDefault();
this._onWheelBound = (e) => this._onWheel(e);
domElement.addEventListener('pointerdown', this._onPointerDown);
domElement.addEventListener('pointermove', this._onPointerMove);
domElement.addEventListener('pointerup', this._onPointerUp);
domElement.addEventListener('pointercancel', this._onPointerUp);
domElement.addEventListener('wheel', this._onWheelBound, { passive: false });
domElement.addEventListener('contextmenu', this._onContextMenu);
}
dispose() {
const d = this.dom;
d.removeEventListener('pointerdown', this._onPointerDown);
d.removeEventListener('pointermove', this._onPointerMove);
d.removeEventListener('pointerup', this._onPointerUp);
d.removeEventListener('pointercancel', this._onPointerUp);
d.removeEventListener('wheel', this._onWheelBound);
d.removeEventListener('contextmenu', this._onContextMenu);
this._drag = null;
this._touches.clear();
this._pinch = null;
this.onFirstInteract = null;
this.primaryPointerFilter = null;
this.wheelFilter = null;
}
setBoardSize(boardSize, center = null) {
if (center) this.panCenter.set(center.x, 0, center.z);
if (this.home) {
this._applyHomeLimits();
return;
}
this.panLimit = boardSize * 0.55;
this.minRadius = Math.max(120, boardSize * 0.06);
this.maxRadius = Math.max(boardSize, this.elevationSpan) * 3.2;
// pan clamp is centred on the assembly (origin for a single tile)
if (center) this.panCenter.set(center.x, 0, center.z);
this.goalRadius = Math.min(Math.max(this.goalRadius, this.minRadius), this.maxRadius);
}
setMode(mode) {
this.mode = mode;
if (mode === 'topdown') {
this.goalPhi = 0.5 * DEG;
} else if (this.goalPhi < this.minPhi) {
this.goalPhi = 55 * DEG;
}
}
setElevationRange(range) {
this.elevationCenter = range ? (range.min + range.max) * 0.5 : 0;
this.elevationSpan = range ? range.max - range.min : 0;
if (this.home) { this._applyHomeLimits(); return; }
this.maxRadius = Math.max(this.panLimit / 0.55, this.elevationSpan) * 3.2;
}
setView(view) {
if (view === 'top') {
this.setMode('topdown');
} else if (view === 'angled' && this.home) {
this.goToView(this.home, { blend: true });
} else if (view === 'angled') {
this.mode = 'orbit';
this.goalPhi = 55 * DEG;
this.goalTheta = 45 * DEG;
}
}
/**
* Install (or clear with null) a home framing:
* { theta, phi, radius, target:[x,y,z], minRadius, maxRadius, minPhi, maxPhi, panLimit }
*/
setHome(home) {
this.home = home ? { ...home, target: [...(home.target || [0, 0, 0])] } : null;
if (this.home) this._applyHomeLimits();
else {
this.minPhi = 8 * DEG;
this.maxPhi = 80 * DEG;
}
}
_applyHomeLimits() {
const h = this.home;
if (Number.isFinite(h.minRadius)) this.minRadius = h.minRadius;
if (Number.isFinite(h.maxRadius)) this.maxRadius = h.maxRadius;
if (Number.isFinite(h.minPhi)) this.minPhi = h.minPhi;
if (Number.isFinite(h.maxPhi)) this.maxPhi = h.maxPhi;
if (Number.isFinite(h.panLimit)) this.panLimit = h.panLimit;
this.goalRadius = Math.min(Math.max(this.goalRadius, this.minRadius), this.maxRadius);
this.goalPhi = Math.min(Math.max(this.goalPhi, this.minPhi), this.maxPhi);
}
/** Current pose, for saved views. */
getView() {
return {
theta: this.goalTheta,
phi: this.goalPhi,
radius: this.goalRadius,
target: [this.goalTarget.x, this.goalTarget.y, this.goalTarget.z],
};
}
/** Move to a pose; `blend` eases there instead of cutting. */
goToView(view, { blend = true } = {}) {
if (!view) return;
this.mode = 'orbit';
const t = view.target || [0, 0, 0];
this.goalTarget.set(t[0], t[1], t[2]);
this.goalRadius = Math.min(Math.max(view.radius, this.minRadius), this.maxRadius);
this.goalPhi = Math.min(Math.max(view.phi, this.minPhi), this.maxPhi);
let dTheta = view.theta - this.theta;
while (dTheta > Math.PI) dTheta -= Math.PI * 2;
while (dTheta < -Math.PI) dTheta += Math.PI * 2;
this.goalTheta = this.theta + dTheta;
if (blend) {
this._smoothRate = 3.2;
return;
}
this.target.copy(this.goalTarget);
this.radius = this.goalRadius;
this.theta = this.goalTheta;
this.phi = this.goalPhi;
this._smoothRate = null;
}
reset(boardSize) {
if (this.home && this.mode !== 'topdown') {
this.goToView(this.home, { blend: false });
return;
}
this.goalTarget.set(0, this.elevationCenter, 0);
// The landing showcase (autoOrbit) frames tighter and lower than the
// editor default; previews reset the camera repeatedly while it's active.
const showcase = this.autoOrbit;
this.goalRadius = Math.max(boardSize, this.elevationSpan) * (showcase ? 0.82 : 1.4);
this.goalTheta = 45 * DEG;
this.goalPhi = this.mode === 'topdown' ? 0.5 * DEG : (showcase ? 66 : 55) * DEG;
this.target.copy(this.goalTarget);
this.radius = this.goalRadius;
this.theta = this.goalTheta;
this.phi = this.goalPhi;
this._smoothRate = null;
}
blendToDefault(boardSize) {
if (this.home && this.mode !== 'topdown') {
this.goToView(this.home, { blend: true });
return;
}
this.goalTarget.set(0, this.elevationCenter, 0);
this.goalRadius = Math.max(boardSize, this.elevationSpan) * 1.4;
const goalTheta = 45 * DEG;
this.goalPhi = this.mode === 'topdown' ? 0.5 * DEG : 55 * DEG;
let dTheta = goalTheta - this.theta;
while (dTheta > Math.PI) dTheta -= Math.PI * 2;
while (dTheta < -Math.PI) dTheta += Math.PI * 2;
this.goalTheta = this.theta + dTheta;
this._smoothRate = 3.2;
}
get isSettling() {
if (this._smoothRate == null) return false;
return Math.abs(this.goalRadius - this.radius) > 2
|| this.target.distanceToSquared(this.goalTarget) > 4
|| Math.abs(this.goalPhi - this.phi) > 0.002
|| Math.abs(this.goalTheta - this.theta) > 0.002;
}
_nearGoals() {
return Math.abs(this.goalRadius - this.radius) <= 2
&& this.target.distanceToSquared(this.goalTarget) <= 4
&& Math.abs(this.goalPhi - this.phi) <= 0.002
&& Math.abs(this.goalTheta - this.theta) <= 0.002;
}
focusCenter() { this.goalTarget.set(0, this.elevationCenter, 0); }
_markInteract() {
if (!this._interacted) {
this._interacted = true;
if (this.onFirstInteract) this.onFirstInteract();
}
}
_onDown(e) {
if (!this.enabled) return;
if ((e.button === 0 || e.pointerType === 'touch') && this.primaryPointerFilter && !this.primaryPointerFilter(e)) return;
if (e.pointerType === 'touch') {
if (this.inputMode === 'orbitOnly') return;
this._markInteract();
this._touches.set(e.pointerId, { x: e.clientX, y: e.clientY });
this._pinch = this._getPinchState();
this.dom.setPointerCapture(e.pointerId);
return;
}
if (e.button !== 0 && e.button !== 2) return;
if (this.inputMode === 'orbitOnly' && e.button !== 2) return;
this._markInteract();
this._drag = { button: e.button, x: e.clientX, y: e.clientY };
this.dom.setPointerCapture(e.pointerId);
}
_panByPixels(dx, dy) {
// pan in the ground plane, screen-relative, scaled by zoom + FOV
const h = this.dom.clientHeight || 1;
const worldPerPx = (2 * this.radius * Math.tan(this.camera.fov * DEG / 2)) / h;
const sin = Math.sin(this.theta), cos = Math.cos(this.theta);
// screen right in world XZ
const rx = cos, rz = -sin;
// screen up projected onto ground (away from camera)
const fx = -sin, fz = -cos;
this.goalTarget.x += (-dx * rx + dy * fx) * worldPerPx;
this.goalTarget.z += (-dx * rz + dy * fz) * worldPerPx;
this._clampTarget();
}
_orbitByPixels(dx, dy) {
this.goalTheta -= dx * 0.005;
if (this.mode !== 'topdown') {
this.goalPhi = Math.min(Math.max(this.goalPhi - dy * 0.004, this.minPhi), this.maxPhi);
}
}
_getPinchState() {
const pts = Array.from(this._touches.values());
if (pts.length < 2) return null;
const [a, b] = pts;
const dx = b.x - a.x, dy = b.y - a.y;
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, dist: Math.hypot(dx, dy) || 1, radius: this.goalRadius };
}
_onMove(e) {
if (e.pointerType === 'touch' && this._touches.has(e.pointerId)) {
const prevTouch = this._touches.get(e.pointerId);
this._touches.set(e.pointerId, { x: e.clientX, y: e.clientY });
const pts = Array.from(this._touches.values());
if (pts.length >= 2) {
const next = this._getPinchState();
if (this._pinch && next) {
this._panByPixels(next.x - this._pinch.x, next.y - this._pinch.y);
this.goalRadius = Math.min(Math.max(this._pinch.radius * (this._pinch.dist / next.dist), this.minRadius), this.maxRadius);
this._pinch.x = next.x;
this._pinch.y = next.y;
}
return;
}
if (pts.length === 1 && this._pinch === null && prevTouch) {
this._orbitByPixels(e.clientX - prevTouch.x, e.clientY - prevTouch.y);
}
return;
}
if (!this._drag) return;
const dx = e.clientX - this._drag.x;
const dy = e.clientY - this._drag.y;
this._drag.x = e.clientX;
this._drag.y = e.clientY;
if (this._drag.button === 0) this._panByPixels(dx, dy);
else this._orbitByPixels(dx, dy);
}
_onUp(e) {
if (e.pointerType === 'touch') {
this._touches.delete(e.pointerId);
this._pinch = this._getPinchState();
try { this.dom.releasePointerCapture(e.pointerId); } catch { /* already released */ }
return;
}
if (this._drag) {
this._drag = null;
try { this.dom.releasePointerCapture(e.pointerId); } catch { /* already released */ }
}
}
_onWheel(e) {
if (!this.enabled) return;
if (this.wheelFilter && !this.wheelFilter(e)) return;
e.preventDefault();
this._markInteract();
this.goalRadius = Math.min(
Math.max(this.goalRadius * Math.exp(e.deltaY * 0.0011), this.minRadius),
this.maxRadius
);
}
_clampTarget() {
const lim = this.panLimit;
this.goalTarget.x = Math.min(Math.max(this.goalTarget.x, this.panCenter.x - lim), this.panCenter.x + lim);
this.goalTarget.z = Math.min(Math.max(this.goalTarget.z, this.panCenter.z - lim), this.panCenter.z + lim);
this.goalTarget.y = this.home ? (this.home.target?.[1] ?? 0) : this.elevationCenter;
}
update(dt) {
if (this.autoOrbit) this.goalTheta += dt * this.autoOrbitSpeed;
const rate = this._smoothRate ?? 9;
const k = 1 - Math.exp(-dt * rate);
this.target.lerp(this.goalTarget, k);
this.radius += (this.goalRadius - this.radius) * k;
this.phi += (this.goalPhi - this.phi) * k;
const dTheta = this.goalTheta - this.theta;
this.theta += dTheta * k;
if (this._smoothRate != null && this._nearGoals()) this._smoothRate = null;
const sinPhi = Math.sin(this.phi);
this.camera.position.set(
this.target.x + this.radius * sinPhi * Math.sin(this.theta),
this.target.y + this.radius * Math.cos(this.phi),
this.target.z + this.radius * sinPhi * Math.cos(this.theta)
);
this.camera.up.set(0, 1, 0);
this.camera.lookAt(this.target);
}
// For the camera info panel
get azimuthDeg() { return ((this.theta / DEG) % 360 + 360) % 360; }
get elevationDeg() { return -(90 - this.phi / DEG); }
get distance() { return this.radius; }
}