Spaces:
Running
Running
Update tooltips.js
Browse files- tooltips.js +3 -34
tooltips.js
CHANGED
|
@@ -21,21 +21,17 @@ export async function initializeTooltips(options) {
|
|
| 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 |
|
|
@@ -47,8 +43,8 @@ export async function initializeTooltips(options) {
|
|
| 47 |
mat.specular = new pc.Color(1, 1, 1);
|
| 48 |
mat.shininess = 20;
|
| 49 |
mat.emissive = new pc.Color(0.85, 0.85, 0.85); // Strong orange emissive
|
| 50 |
-
mat.emissiveIntensity = 1;
|
| 51 |
-
mat.useLighting = false;
|
| 52 |
mat.update();
|
| 53 |
|
| 54 |
// Build each tooltip sphere + attach custom data
|
|
@@ -60,7 +56,6 @@ export async function initializeTooltips(options) {
|
|
| 60 |
sphere.addComponent("model", { type: "sphere" });
|
| 61 |
sphere.model.material = mat;
|
| 62 |
|
| 63 |
-
// Scale small (primitive sphere radius = 0.5)
|
| 64 |
sphere.setLocalScale(0.05, 0.05, 0.05);
|
| 65 |
sphere.setLocalPosition(x, y, z);
|
| 66 |
sphere.tooltipData = { title, description, imgUrl };
|
|
@@ -68,7 +63,6 @@ export async function initializeTooltips(options) {
|
|
| 68 |
tooltipEntities.push(sphere);
|
| 69 |
}
|
| 70 |
|
| 71 |
-
// Show/hide all tooltip spheres
|
| 72 |
function setTooltipsVisibility(visible) {
|
| 73 |
tooltipEntities.forEach(ent => {
|
| 74 |
ent.enabled = visible;
|
|
@@ -76,18 +70,14 @@ export async function initializeTooltips(options) {
|
|
| 76 |
}
|
| 77 |
setTooltipsVisibility(!!defaultVisible);
|
| 78 |
|
| 79 |
-
// Respond to toggle-tooltips event from interface.js
|
| 80 |
document.addEventListener("toggle-tooltips", (evt) => {
|
| 81 |
const { visible } = evt.detail;
|
| 82 |
setTooltipsVisibility(!!visible);
|
| 83 |
});
|
| 84 |
|
| 85 |
-
// Keep track of any in-flight camera tween so we can cancel it
|
| 86 |
let currentTween = null;
|
| 87 |
|
| 88 |
-
// On mouse down (or touch equivalent), perform manual ray‐sphere intersection
|
| 89 |
app.mouse.on(pc.EVENT_MOUSEDOWN, (event) => {
|
| 90 |
-
// If a tween is running, cancel it immediately
|
| 91 |
if (currentTween) {
|
| 92 |
app.off("update", currentTween);
|
| 93 |
currentTween = null;
|
|
@@ -137,17 +127,7 @@ export async function initializeTooltips(options) {
|
|
| 137 |
}
|
| 138 |
});
|
| 139 |
|
| 140 |
-
//
|
| 141 |
-
const canvasId = app.graphicsDevice.canvas.id;
|
| 142 |
-
const htmlCanvas = document.getElementById(canvasId);
|
| 143 |
-
if (htmlCanvas) {
|
| 144 |
-
htmlCanvas.addEventListener("mousedown", () => {
|
| 145 |
-
document.dispatchEvent(new CustomEvent("hide-tooltip"));
|
| 146 |
-
});
|
| 147 |
-
htmlCanvas.addEventListener("touchstart", () => {
|
| 148 |
-
document.dispatchEvent(new CustomEvent("hide-tooltip"));
|
| 149 |
-
});
|
| 150 |
-
}
|
| 151 |
|
| 152 |
// Helper to normalize angle difference into [-180, +180]
|
| 153 |
function shortestAngleDiff(target, current) {
|
|
@@ -156,25 +136,20 @@ export async function initializeTooltips(options) {
|
|
| 156 |
return delta;
|
| 157 |
}
|
| 158 |
|
| 159 |
-
// Tween helper: smoothly move and reorient camera to focus the chosen tooltip sphere
|
| 160 |
function tweenCameraToTooltip(tooltipEnt, duration) {
|
| 161 |
const orbitCam = cameraEntity.script.orbitCamera;
|
| 162 |
if (!orbitCam) return;
|
| 163 |
|
| 164 |
-
// Compute target pivot exactly at the sphere center
|
| 165 |
const targetPos = tooltipEnt.getPosition().clone();
|
| 166 |
-
// Compute current state
|
| 167 |
const startPivot = orbitCam.pivotPoint.clone();
|
| 168 |
const startYaw = orbitCam._yaw;
|
| 169 |
const startPitch = orbitCam._pitch;
|
| 170 |
const startDist = orbitCam._distance;
|
| 171 |
|
| 172 |
-
// Compute direction & candidate distance:
|
| 173 |
const worldRadius = 0.5 * tooltipEnt.getLocalScale().x;
|
| 174 |
const minZoom = orbitCam.distanceMin;
|
| 175 |
const desiredDistance = Math.max(minZoom * 1.2, worldRadius * 4);
|
| 176 |
|
| 177 |
-
// Compute raw target yaw/pitch from camera pointing at targetPos
|
| 178 |
const camWorldPos = cameraEntity.getPosition().clone();
|
| 179 |
const tempEnt = new pc.Entity();
|
| 180 |
tempEnt.setPosition(camWorldPos);
|
|
@@ -186,7 +161,6 @@ export async function initializeTooltips(options) {
|
|
| 186 |
const yawDelta = shortestAngleDiff(rawTgtYaw, startYaw);
|
| 187 |
const endYaw = startYaw + yawDelta;
|
| 188 |
|
| 189 |
-
// Pitch calculation and normalization
|
| 190 |
const yawQuat = new pc.Quat().setFromEulerAngles(0, -rawTgtYaw, 0);
|
| 191 |
const rotNoYaw = new pc.Quat().mul2(yawQuat, rotation);
|
| 192 |
const fNoYaw = new pc.Vec3();
|
|
@@ -197,7 +171,6 @@ export async function initializeTooltips(options) {
|
|
| 197 |
|
| 198 |
tempEnt.destroy();
|
| 199 |
|
| 200 |
-
// Target state:
|
| 201 |
const endPivot = targetPos.clone();
|
| 202 |
const endDist = desiredDistance;
|
| 203 |
|
|
@@ -207,22 +180,18 @@ export async function initializeTooltips(options) {
|
|
| 207 |
const orgPitch = startPitch;
|
| 208 |
const orgDist = startDist;
|
| 209 |
|
| 210 |
-
// If another tween is running, cancel it
|
| 211 |
if (currentTween) {
|
| 212 |
app.off("update", currentTween);
|
| 213 |
currentTween = null;
|
| 214 |
}
|
| 215 |
|
| 216 |
-
// Per-frame update
|
| 217 |
function lerpUpdate(dt) {
|
| 218 |
elapsed += dt;
|
| 219 |
const t = Math.min(elapsed / duration, 1);
|
| 220 |
|
| 221 |
-
// Interpolate pivot (vector lerp)
|
| 222 |
const newPivot = new pc.Vec3().lerp(orgPivot, endPivot, t);
|
| 223 |
orbitCam.pivotPoint.copy(newPivot);
|
| 224 |
|
| 225 |
-
// Interpolate yaw/pitch/distance (simple lerp)
|
| 226 |
const newYaw = pc.math.lerp(orgYaw, endYaw, t);
|
| 227 |
const newPitch = pc.math.lerp(orgPitch, endPitch, t);
|
| 228 |
const newDist = pc.math.lerp(orgDist, endDist, t);
|
|
|
|
| 21 |
} = options;
|
| 22 |
|
| 23 |
if (!app || !cameraEntity || !tooltipsUrl) {
|
|
|
|
| 24 |
return;
|
| 25 |
}
|
| 26 |
|
|
|
|
| 27 |
let tooltipsData;
|
| 28 |
try {
|
| 29 |
const resp = await fetch(tooltipsUrl);
|
| 30 |
tooltipsData = await resp.json();
|
| 31 |
} catch (e) {
|
|
|
|
| 32 |
return;
|
| 33 |
}
|
| 34 |
if (!Array.isArray(tooltipsData)) {
|
|
|
|
| 35 |
return;
|
| 36 |
}
|
| 37 |
|
|
|
|
| 43 |
mat.specular = new pc.Color(1, 1, 1);
|
| 44 |
mat.shininess = 20;
|
| 45 |
mat.emissive = new pc.Color(0.85, 0.85, 0.85); // Strong orange emissive
|
| 46 |
+
mat.emissiveIntensity = 1;
|
| 47 |
+
mat.useLighting = false;
|
| 48 |
mat.update();
|
| 49 |
|
| 50 |
// Build each tooltip sphere + attach custom data
|
|
|
|
| 56 |
sphere.addComponent("model", { type: "sphere" });
|
| 57 |
sphere.model.material = mat;
|
| 58 |
|
|
|
|
| 59 |
sphere.setLocalScale(0.05, 0.05, 0.05);
|
| 60 |
sphere.setLocalPosition(x, y, z);
|
| 61 |
sphere.tooltipData = { title, description, imgUrl };
|
|
|
|
| 63 |
tooltipEntities.push(sphere);
|
| 64 |
}
|
| 65 |
|
|
|
|
| 66 |
function setTooltipsVisibility(visible) {
|
| 67 |
tooltipEntities.forEach(ent => {
|
| 68 |
ent.enabled = visible;
|
|
|
|
| 70 |
}
|
| 71 |
setTooltipsVisibility(!!defaultVisible);
|
| 72 |
|
|
|
|
| 73 |
document.addEventListener("toggle-tooltips", (evt) => {
|
| 74 |
const { visible } = evt.detail;
|
| 75 |
setTooltipsVisibility(!!visible);
|
| 76 |
});
|
| 77 |
|
|
|
|
| 78 |
let currentTween = null;
|
| 79 |
|
|
|
|
| 80 |
app.mouse.on(pc.EVENT_MOUSEDOWN, (event) => {
|
|
|
|
| 81 |
if (currentTween) {
|
| 82 |
app.off("update", currentTween);
|
| 83 |
currentTween = null;
|
|
|
|
| 127 |
}
|
| 128 |
});
|
| 129 |
|
| 130 |
+
// Do NOT close the tooltip panel on canvas mousedown/touchstart!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
// Helper to normalize angle difference into [-180, +180]
|
| 133 |
function shortestAngleDiff(target, current) {
|
|
|
|
| 136 |
return delta;
|
| 137 |
}
|
| 138 |
|
|
|
|
| 139 |
function tweenCameraToTooltip(tooltipEnt, duration) {
|
| 140 |
const orbitCam = cameraEntity.script.orbitCamera;
|
| 141 |
if (!orbitCam) return;
|
| 142 |
|
|
|
|
| 143 |
const targetPos = tooltipEnt.getPosition().clone();
|
|
|
|
| 144 |
const startPivot = orbitCam.pivotPoint.clone();
|
| 145 |
const startYaw = orbitCam._yaw;
|
| 146 |
const startPitch = orbitCam._pitch;
|
| 147 |
const startDist = orbitCam._distance;
|
| 148 |
|
|
|
|
| 149 |
const worldRadius = 0.5 * tooltipEnt.getLocalScale().x;
|
| 150 |
const minZoom = orbitCam.distanceMin;
|
| 151 |
const desiredDistance = Math.max(minZoom * 1.2, worldRadius * 4);
|
| 152 |
|
|
|
|
| 153 |
const camWorldPos = cameraEntity.getPosition().clone();
|
| 154 |
const tempEnt = new pc.Entity();
|
| 155 |
tempEnt.setPosition(camWorldPos);
|
|
|
|
| 161 |
const yawDelta = shortestAngleDiff(rawTgtYaw, startYaw);
|
| 162 |
const endYaw = startYaw + yawDelta;
|
| 163 |
|
|
|
|
| 164 |
const yawQuat = new pc.Quat().setFromEulerAngles(0, -rawTgtYaw, 0);
|
| 165 |
const rotNoYaw = new pc.Quat().mul2(yawQuat, rotation);
|
| 166 |
const fNoYaw = new pc.Vec3();
|
|
|
|
| 171 |
|
| 172 |
tempEnt.destroy();
|
| 173 |
|
|
|
|
| 174 |
const endPivot = targetPos.clone();
|
| 175 |
const endDist = desiredDistance;
|
| 176 |
|
|
|
|
| 180 |
const orgPitch = startPitch;
|
| 181 |
const orgDist = startDist;
|
| 182 |
|
|
|
|
| 183 |
if (currentTween) {
|
| 184 |
app.off("update", currentTween);
|
| 185 |
currentTween = null;
|
| 186 |
}
|
| 187 |
|
|
|
|
| 188 |
function lerpUpdate(dt) {
|
| 189 |
elapsed += dt;
|
| 190 |
const t = Math.min(elapsed / duration, 1);
|
| 191 |
|
|
|
|
| 192 |
const newPivot = new pc.Vec3().lerp(orgPivot, endPivot, t);
|
| 193 |
orbitCam.pivotPoint.copy(newPivot);
|
| 194 |
|
|
|
|
| 195 |
const newYaw = pc.math.lerp(orgYaw, endYaw, t);
|
| 196 |
const newPitch = pc.math.lerp(orgPitch, endPitch, t);
|
| 197 |
const newDist = pc.math.lerp(orgDist, endDist, t);
|