MikaFil commited on
Commit
d32f85e
·
verified ·
1 Parent(s): 8b545a5

Update tooltips.js

Browse files
Files changed (1) hide show
  1. tooltips.js +27 -31
tooltips.js CHANGED
@@ -20,17 +20,24 @@ export async function initializeTooltips(options) {
20
  moveDuration = 0.6
21
  } = options;
22
 
23
- if (!app || !cameraEntity || !tooltipsUrl) return;
 
 
 
24
 
25
- // Load tooltips
26
  let tooltipsData;
27
  try {
28
  const resp = await fetch(tooltipsUrl);
29
  tooltipsData = await resp.json();
30
  } catch (e) {
 
 
 
 
 
31
  return;
32
  }
33
- if (!Array.isArray(tooltipsData)) return;
34
 
35
  const tooltipEntities = [];
36
 
@@ -49,6 +56,8 @@ export async function initializeTooltips(options) {
49
  const sphere = new pc.Entity("tooltip-" + i);
50
  sphere.addComponent("model", { type: "sphere" });
51
  sphere.model.material = mat;
 
 
52
  sphere.setLocalScale(0.05, 0.05, 0.05);
53
  sphere.setLocalPosition(x, y, z);
54
  sphere.tooltipData = { title, description, imgUrl };
@@ -70,11 +79,19 @@ export async function initializeTooltips(options) {
70
  setTooltipsVisibility(!!visible);
71
  });
72
 
73
- // Cancel in-flight camera tween
74
  let currentTween = null;
75
 
76
- // Tooltip pick logic (for both mouse/tap)
77
- function pickTooltip(x, y) {
 
 
 
 
 
 
 
 
78
  const from = new pc.Vec3();
79
  const to = new pc.Vec3();
80
  const camera = cameraEntity.camera;
@@ -83,6 +100,7 @@ export async function initializeTooltips(options) {
83
  camera.screenToWorld(x, y, camera.farClip, to);
84
 
85
  const dir = new pc.Vec3().sub2(to, from).normalize();
 
86
  let closestT = Infinity;
87
  let pickedEntity = null;
88
 
@@ -114,40 +132,18 @@ export async function initializeTooltips(options) {
114
  }));
115
  tweenCameraToTooltip(pickedEntity, moveDuration);
116
  }
117
- }
118
 
119
- // Mouse picking
120
  const canvasId = app.graphicsDevice.canvas.id;
121
  const htmlCanvas = document.getElementById(canvasId);
122
- if (htmlCanvas) {
123
- htmlCanvas.addEventListener("mousedown", (event) => {
124
- if (currentTween) {
125
- app.off("update", currentTween);
126
- currentTween = null;
127
- }
128
- pickTooltip(event.offsetX * window.devicePixelRatio, event.offsetY * window.devicePixelRatio);
129
- }, { passive: false });
130
-
131
- // Touch picking (for iOS/Android)
132
- htmlCanvas.addEventListener("touchstart", (event) => {
133
- if (event.touches.length === 1) {
134
- const rect = htmlCanvas.getBoundingClientRect();
135
- const x = (event.touches[0].clientX - rect.left) * window.devicePixelRatio;
136
- const y = (event.touches[0].clientY - rect.top) * window.devicePixelRatio;
137
- pickTooltip(x, y);
138
- event.preventDefault();
139
- }
140
- }, { passive: false });
141
- }
142
-
143
- // Also close tooltip panel if user interacts (mouse or touch) on the canvas
144
  if (htmlCanvas) {
145
  htmlCanvas.addEventListener("mousedown", () => {
146
  document.dispatchEvent(new CustomEvent("hide-tooltip"));
147
  });
148
  htmlCanvas.addEventListener("touchstart", () => {
149
  document.dispatchEvent(new CustomEvent("hide-tooltip"));
150
- }, { passive: false });
151
  }
152
 
153
  // Helper to normalize angle difference into [-180, +180]
 
20
  moveDuration = 0.6
21
  } = options;
22
 
23
+ if (!app || !cameraEntity || !tooltipsUrl) {
24
+ console.error("tooltips.js → missing required initialization options");
25
+ return;
26
+ }
27
 
28
+ // Load JSON of tooltips:
29
  let tooltipsData;
30
  try {
31
  const resp = await fetch(tooltipsUrl);
32
  tooltipsData = await resp.json();
33
  } catch (e) {
34
+ console.error("tooltips.js → failed fetching tooltips.json:", e);
35
+ return;
36
+ }
37
+ if (!Array.isArray(tooltipsData)) {
38
+ console.error("tooltips.js → tooltips.json must be an array");
39
  return;
40
  }
 
41
 
42
  const tooltipEntities = [];
43
 
 
56
  const sphere = new pc.Entity("tooltip-" + i);
57
  sphere.addComponent("model", { type: "sphere" });
58
  sphere.model.material = mat;
59
+
60
+ // Scale small (primitive sphere radius = 0.5)
61
  sphere.setLocalScale(0.05, 0.05, 0.05);
62
  sphere.setLocalPosition(x, y, z);
63
  sphere.tooltipData = { title, description, imgUrl };
 
79
  setTooltipsVisibility(!!visible);
80
  });
81
 
82
+ // Keep track of any in-flight camera tween so we can cancel it
83
  let currentTween = null;
84
 
85
+ // On mouse down (or touch equivalent), perform manual ray‐sphere intersection
86
+ app.mouse.on(pc.EVENT_MOUSEDOWN, (event) => {
87
+ // If a tween is running, cancel it immediately
88
+ if (currentTween) {
89
+ app.off("update", currentTween);
90
+ currentTween = null;
91
+ }
92
+
93
+ const x = event.x;
94
+ const y = event.y;
95
  const from = new pc.Vec3();
96
  const to = new pc.Vec3();
97
  const camera = cameraEntity.camera;
 
100
  camera.screenToWorld(x, y, camera.farClip, to);
101
 
102
  const dir = new pc.Vec3().sub2(to, from).normalize();
103
+
104
  let closestT = Infinity;
105
  let pickedEntity = null;
106
 
 
132
  }));
133
  tweenCameraToTooltip(pickedEntity, moveDuration);
134
  }
135
+ });
136
 
137
+ // Also close tooltip panel if user interacts (mouse or touch) on the canvas
138
  const canvasId = app.graphicsDevice.canvas.id;
139
  const htmlCanvas = document.getElementById(canvasId);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  if (htmlCanvas) {
141
  htmlCanvas.addEventListener("mousedown", () => {
142
  document.dispatchEvent(new CustomEvent("hide-tooltip"));
143
  });
144
  htmlCanvas.addEventListener("touchstart", () => {
145
  document.dispatchEvent(new CustomEvent("hide-tooltip"));
146
+ });
147
  }
148
 
149
  // Helper to normalize angle difference into [-180, +180]