@inweb/viewer-visualize 26.10.2 → 26.10.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,6 +23,7 @@
23
23
  import { Viewer } from "../Viewer";
24
24
  import { Point2d, Vector3 } from "./Common/Geometry";
25
25
  import { OdBaseDragger } from "./Common/OdBaseDragger";
26
+ import { OdJoyStickDragger } from "./OdJoyStickDragger";
26
27
 
27
28
  const FocalLengthConst = 42.0;
28
29
 
@@ -31,8 +32,7 @@ const calcFocalLength = (lensLength: number, fieldWidth: number, fieldHeight: nu
31
32
  };
32
33
 
33
34
  export class OdaWalkDragger extends OdBaseDragger {
34
- protected lastCoord: Point2d;
35
- protected speed: number;
35
+ protected baseSpeed: number;
36
36
  protected delta: number;
37
37
  protected keyPressMap: Set<string>;
38
38
  protected oldWCSEnableValue: boolean;
@@ -42,28 +42,38 @@ export class OdaWalkDragger extends OdBaseDragger {
42
42
  protected viewer: any;
43
43
  protected multiplier: number;
44
44
  protected lastFrameTS: number;
45
+ protected lastFrameJoyStickTS: number;
45
46
  protected animationId: any;
46
47
  protected deltaAngle: number;
47
48
  protected enableZoomWheelPreviousValue: boolean;
48
49
  protected dragPosition: Point2d;
50
+ protected joyStickOverlayElement: HTMLDivElement;
51
+ protected joyStickDragger: OdJoyStickDragger;
52
+ protected isJoyStickMoving: boolean;
53
+ protected lastJoyStickCoord: Point2d;
49
54
 
50
55
  constructor(subject: Viewer) {
51
56
  super(subject);
52
57
  this.viewer = undefined;
53
58
 
54
59
  this.multiplier = 5;
55
- this.speed = 1;
60
+ this.baseSpeed = 1;
56
61
 
57
62
  this.keyPressMap = new Set();
58
63
  this.keydown = this.keydown.bind(this);
59
64
  this.keyup = this.keyup.bind(this);
60
65
 
61
66
  this.lastFrameTS = 0;
67
+ this.lastFrameJoyStickTS = 0;
62
68
  this.animationId = undefined;
63
69
  this.processMovement = this.processMovement.bind(this);
70
+ this.processJoyStickMovement = this.processJoyStickMovement.bind(this);
64
71
 
65
72
  this.deltaAngle = Math.PI / 3600;
66
73
  this.autoSelect = true;
74
+
75
+ this.isJoyStickMoving = false;
76
+ this.addJoyStickDragger();
67
77
  }
68
78
 
69
79
  override initialize() {
@@ -78,7 +88,7 @@ export class OdaWalkDragger extends OdBaseDragger {
78
88
 
79
89
  const view = this.viewer.activeView;
80
90
  const maxDimension = this.getMaxDimension(view);
81
- this.speed = maxDimension / 30000;
91
+ this.baseSpeed = maxDimension / 30000;
82
92
 
83
93
  this.subject.emitEvent({ type: "walkstart" });
84
94
 
@@ -137,9 +147,12 @@ export class OdaWalkDragger extends OdBaseDragger {
137
147
  // CLOUD-5359 Demo Viewer crashes after the Walk Mode
138
148
  this.subject.update(true);
139
149
  this.subject.options.enableZoomWheel = this.enableZoomWheelPreviousValue;
150
+
151
+ this.joyStickOverlayElement.remove();
152
+ this.joyStickDragger.cleanup();
140
153
  }
141
154
 
142
- keydown(ev) {
155
+ keydown(ev: KeyboardEvent) {
143
156
  switch (ev.code) {
144
157
  case "NumpadSubtract":
145
158
  case "Minus":
@@ -167,7 +180,7 @@ export class OdaWalkDragger extends OdBaseDragger {
167
180
  }
168
181
  }
169
182
 
170
- keyup(ev) {
183
+ keyup(ev: KeyboardEvent) {
171
184
  this.keyPressMap.delete(ev.code);
172
185
  if (this.keyPressMap.size < 1 && this.animationId) {
173
186
  window.cancelAnimationFrame(this.animationId);
@@ -176,38 +189,39 @@ export class OdaWalkDragger extends OdBaseDragger {
176
189
  }
177
190
  }
178
191
 
179
- processMovement(timestamp) {
192
+ processMovement(timestamp: number) {
180
193
  this.animationId = requestAnimationFrame(this.processMovement);
181
194
 
182
195
  if (this.lastFrameTS !== 0) {
183
196
  const deltaTS = timestamp - this.lastFrameTS;
184
- const currentDelta = this.multiplier * deltaTS * this.speed;
185
-
186
- for (const keyCode of this.keyPressMap) {
187
- switch (keyCode) {
188
- case "KeyW":
189
- this.moveForward(currentDelta);
190
- break;
191
- case "KeyS":
192
- this.moveBackward(currentDelta);
193
- break;
194
- case "KeyA":
195
- this.cameraWalker.moveLeft(currentDelta);
196
- break;
197
- case "KeyD":
198
- this.cameraWalker.moveRight(currentDelta);
199
- break;
200
- case "KeyQ":
201
- this.cameraWalker.moveUp(currentDelta);
202
- break;
203
- case "KeyE":
204
- this.cameraWalker.moveDown(currentDelta);
205
- break;
206
- }
197
+ if (deltaTS > 0) {
198
+ const currentDelta = this.multiplier * deltaTS * this.baseSpeed;
199
+
200
+ Array.from(this.keyPressMap).forEach((keyCode) => {
201
+ switch (keyCode) {
202
+ case "KeyW":
203
+ this.moveForward(currentDelta);
204
+ break;
205
+ case "KeyS":
206
+ this.moveBackward(currentDelta);
207
+ break;
208
+ case "KeyA":
209
+ this.cameraWalker.moveLeft(currentDelta);
210
+ break;
211
+ case "KeyD":
212
+ this.cameraWalker.moveRight(currentDelta);
213
+ break;
214
+ case "KeyQ":
215
+ this.cameraWalker.moveUp(currentDelta);
216
+ break;
217
+ case "KeyE":
218
+ this.cameraWalker.moveDown(currentDelta);
219
+ break;
220
+ }
221
+ });
222
+
223
+ this.proceedChangeCamera();
207
224
  }
208
-
209
- this.subject.update();
210
- this.subject.emitEvent({ type: "changecamera" });
211
225
  }
212
226
 
213
227
  this.lastFrameTS = timestamp;
@@ -231,7 +245,7 @@ export class OdaWalkDragger extends OdBaseDragger {
231
245
  }
232
246
  }
233
247
 
234
- moveForward(currentDelta) {
248
+ moveForward(currentDelta: number) {
235
249
  const { Vector3d } = this.m_module;
236
250
 
237
251
  const camera = this.cameraWalker.camera().openObjectAsCamera();
@@ -252,11 +266,11 @@ export class OdaWalkDragger extends OdBaseDragger {
252
266
  camera.setupCamera(newPos.toArray(), newTarget.toArray(), up.toArray());
253
267
  }
254
268
 
255
- moveBackward(currentDelta) {
269
+ moveBackward(currentDelta: number) {
256
270
  this.moveForward(-currentDelta);
257
271
  }
258
272
 
259
- turnLeft(angle) {
273
+ turnLeft(angle: number) {
260
274
  //TODO: migrate to VisualizeJS
261
275
  const pCamera = this.cameraWalker.camera().openObjectAsCamera();
262
276
  const dir = this.toVector(pCamera.direction());
@@ -273,7 +287,7 @@ export class OdaWalkDragger extends OdBaseDragger {
273
287
  pCamera.delete();
274
288
  }
275
289
 
276
- setupCamera(view) {
290
+ setupCamera(view: any) {
277
291
  const pCamera = this.cameraId.openObjectAsCamera();
278
292
  const target = view.viewTarget;
279
293
 
@@ -308,10 +322,73 @@ export class OdaWalkDragger extends OdBaseDragger {
308
322
  pCamera.delete();
309
323
  }
310
324
 
311
- getMaxDimension(view) {
325
+ getMaxDimension(view: any) {
312
326
  const [xmax, ymax, zmax] = view.sceneExtents.max();
313
327
  const [xmin, ymin, zmin] = view.sceneExtents.min();
314
328
  const volume = [xmax - xmin, ymax - ymin, zmax - zmin];
315
329
  return Math.max(...volume);
316
330
  }
331
+
332
+ private addJoyStickDragger() {
333
+ this.joyStickOverlayElement = document.createElement("div");
334
+ this.joyStickOverlayElement.id = "joyStickDiv";
335
+ this.joyStickOverlayElement.style.background = "rgba(0,0,0,0)";
336
+ this.joyStickOverlayElement.style.position = "fixed";
337
+ this.joyStickOverlayElement.style.zIndex = "0";
338
+ document.body.appendChild(this.joyStickOverlayElement);
339
+
340
+ this.joyStickDragger = new OdJoyStickDragger(
341
+ this,
342
+ this.joyStickOverlayElement,
343
+ (stickData) => {
344
+ if (Math.sqrt(stickData.x * stickData.x + stickData.y * stickData.y) > 20) {
345
+ this.lastJoyStickCoord = { x: stickData.x, y: stickData.y };
346
+ if (!this.animationId && !this.isJoyStickMoving) {
347
+ this.isJoyStickMoving = true;
348
+ this.processJoyStickMovement(0);
349
+ }
350
+ } else {
351
+ this.isJoyStickMoving = false;
352
+ window.cancelAnimationFrame(this.animationId);
353
+ this.animationId = undefined;
354
+ this.lastFrameJoyStickTS = 0;
355
+ }
356
+ },
357
+ this.m_module.canvas
358
+ );
359
+ }
360
+
361
+ processJoyStickMovement(timestamp: number) {
362
+ if (!this.isJoyStickMoving) return;
363
+ this.animationId = requestAnimationFrame(this.processJoyStickMovement);
364
+
365
+ if (this.lastFrameJoyStickTS !== 0) {
366
+ const deltaTS = timestamp - this.lastFrameJoyStickTS;
367
+ if (deltaTS > 0) {
368
+ const maxJoystickDistance = 100;
369
+ const forward = this.lastJoyStickCoord.y / maxJoystickDistance;
370
+ const right = this.lastJoyStickCoord.x / maxJoystickDistance;
371
+
372
+ const currentDelta = this.multiplier * deltaTS * this.baseSpeed;
373
+ this.moveTotal(currentDelta, forward, right);
374
+ }
375
+ }
376
+
377
+ this.lastFrameJoyStickTS = timestamp;
378
+ }
379
+
380
+ moveTotal(currentDelta: number, forward: number, right: number): void {
381
+ if (forward !== 0) {
382
+ this.moveForward(currentDelta * forward);
383
+ }
384
+ if (right !== 0) {
385
+ this.cameraWalker.moveRight(currentDelta * right);
386
+ }
387
+ this.proceedChangeCamera();
388
+ }
389
+
390
+ proceedChangeCamera() {
391
+ this.subject.update();
392
+ this.subject.emitEvent({ type: "changecamera" });
393
+ }
317
394
  }