mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
fix(trails): a double-click zooms to a trail line again
A trail was polyline entities, which the default renderer's double-click zoomed to. As draw-port lines they took no part in it. Every trail on a viewer shares one double-click handler, let go with the last trail.
This commit is contained in:
1 parent
322e4a91d3
commit
a9de7b3510
2 files changed
+120
-10
No files matched your search
+65
-10
@@ -27,8 +27,9 @@
|
|||||||
* changing settle into static geometry.
|
* changing settle into static geometry.
|
||||||
*/
|
*/
|
||||||
import { cssColor } from '../render/color.js';
|
import { cssColor } from '../render/color.js';
|
||||||
import { findDraw } from '../render/rendererOf.js';
|
import { findDraw, findInput } from '../render/rendererOf.js';
|
||||||
import { registerPickOwner } from './pickRegistry.js';
|
import { registerPickOwner } from './pickRegistry.js';
|
||||||
|
import { drawnItemTarget, followOnDoubleClick } from './stockFollow.js';
|
||||||
|
|
||||||
// Trail lines are pickable (a click on one must not fall through to the
|
// Trail lines are pickable (a click on one must not fall through to the
|
||||||
// ground under it). A trail hugs its aircraft, so an unclaimed pick would
|
// ground under it). A trail hugs its aircraft, so an unclaimed pick would
|
||||||
@@ -43,6 +44,48 @@ registerPickOwner('trails', (pickedId) =>
|
|||||||
/** @type {number} Uniquifier for trail set and line ids. */
|
/** @type {number} Uniquifier for trail set and line ids. */
|
||||||
let _trailSeq = 0;
|
let _trailSeq = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Per viewer, its trails' drawn lines by set id and the one double-click
|
||||||
|
* handler they share: a double-click on a trail line zooms to it, as the
|
||||||
|
* default renderer zoomed to the polyline entity a trail was drawn as.
|
||||||
|
* @type {WeakMap<object, {lines: Map<string, (id: string) => object|null>,
|
||||||
|
* follow: object|null}>}
|
||||||
|
*/
|
||||||
|
const doubleClicks = new WeakMap();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Take part in `viewer`'s trail double-click with the set `setId`, whose
|
||||||
|
* line by id `lineFor` gives. Returns how to leave it.
|
||||||
|
*/
|
||||||
|
function joinDoubleClick(viewer, setId, lineFor) {
|
||||||
|
let shared = doubleClicks.get(viewer);
|
||||||
|
if (!shared) {
|
||||||
|
const lines = new Map();
|
||||||
|
// A scene that takes no input has no double-click.
|
||||||
|
const input = findInput(viewer);
|
||||||
|
shared = {
|
||||||
|
lines,
|
||||||
|
follow: input
|
||||||
|
? followOnDoubleClick({
|
||||||
|
viewer,
|
||||||
|
inputFor: () => input,
|
||||||
|
name: 'trails',
|
||||||
|
enabled: () => true,
|
||||||
|
targetFor: (hit) =>
|
||||||
|
drawnItemTarget(hit.set, lines.get(hit.set)?.(hit.id)),
|
||||||
|
})
|
||||||
|
: null,
|
||||||
|
};
|
||||||
|
doubleClicks.set(viewer, shared);
|
||||||
|
}
|
||||||
|
shared.lines.set(setId, lineFor);
|
||||||
|
return () => {
|
||||||
|
if (!shared.lines.delete(setId) || shared.lines.size) return;
|
||||||
|
shared.follow?.destroy();
|
||||||
|
if (doubleClicks.get(viewer) === shared) doubleClicks.delete(viewer);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/** @constant {number} Alpha where the trail passes the depth test. */
|
/** @constant {number} Alpha where the trail passes the depth test. */
|
||||||
const TRAIL_ALPHA = 0.85;
|
const TRAIL_ALPHA = 0.85;
|
||||||
/** @constant {number} Alpha where the trail is behind/below scene geometry —
|
/** @constant {number} Alpha where the trail is behind/below scene geometry —
|
||||||
@@ -133,11 +176,21 @@ export function createTrail(viewer, { color, width = 2.5 }) {
|
|||||||
* the position objects it was drawn from, which never change. */
|
* the position objects it was drawn from, which never change. */
|
||||||
let chunks = [];
|
let chunks = [];
|
||||||
let chunkSeq = 0;
|
let chunkSeq = 0;
|
||||||
|
/** Leaves the viewer's trail double-click, once the set is drawn. */
|
||||||
|
let leaveDoubleClick = null;
|
||||||
|
|
||||||
|
/** The line drawn under `id`, for a double-click to zoom to. */
|
||||||
|
function lineFor(id) {
|
||||||
|
const shown = drawn.get(id)?.item;
|
||||||
|
if (shown) return shown.show === false ? null : shown;
|
||||||
|
return chunks.find((chunk) => chunk.id === id)?.item ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
function ensureSet() {
|
function ensureSet() {
|
||||||
if (set || destroyed || !viewer || viewer.isDestroyed?.()) return set;
|
if (set || destroyed || !viewer || viewer.isDestroyed?.()) return set;
|
||||||
set =
|
set =
|
||||||
findDraw(viewer)?.lines({ id: `trail:${seq}`, show: visible }) ?? null;
|
findDraw(viewer)?.lines({ id: `trail:${seq}`, show: visible }) ?? null;
|
||||||
|
if (set) leaveDoubleClick = joinDoubleClick(viewer, set.id, lineFor);
|
||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,16 +276,16 @@ export function createTrail(viewer, { color, width = 2.5 }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const positions = seg.slice(i, end + 1);
|
const positions = seg.slice(i, end + 1);
|
||||||
const chunk = { id: `${bodyId}:${++chunkSeq}`, positions };
|
const id = `${bodyId}:${++chunkSeq}`;
|
||||||
next.push(chunk);
|
const item = lineItem(
|
||||||
added.push(
|
id,
|
||||||
lineItem(
|
positions.map(plain),
|
||||||
chunk.id,
|
TRAIL_ALPHA,
|
||||||
positions.map(plain),
|
TRAIL_OCCLUDED_ALPHA,
|
||||||
TRAIL_ALPHA,
|
|
||||||
TRAIL_OCCLUDED_ALPHA,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
const chunk = { id, positions, item };
|
||||||
|
next.push(chunk);
|
||||||
|
added.push(item);
|
||||||
i = end;
|
i = end;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -318,6 +371,8 @@ export function createTrail(viewer, { color, width = 2.5 }) {
|
|||||||
/** Remove the trail permanently (layer disable/teardown). */
|
/** Remove the trail permanently (layer disable/teardown). */
|
||||||
destroy() {
|
destroy() {
|
||||||
destroyed = true;
|
destroyed = true;
|
||||||
|
leaveDoubleClick?.();
|
||||||
|
leaveDoubleClick = null;
|
||||||
try {
|
try {
|
||||||
set?.dispose();
|
set?.dispose();
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import assert from 'node:assert/strict';
|
|||||||
import { bindFakeDraw } from '../testSupport/fakeDrawPort.mjs';
|
import { bindFakeDraw } from '../testSupport/fakeDrawPort.mjs';
|
||||||
import { CHUNK_POINTS, createTrail } from './trailRenderer.js';
|
import { CHUNK_POINTS, createTrail } from './trailRenderer.js';
|
||||||
import { findPickOwner } from './pickRegistry.js';
|
import { findPickOwner } from './pickRegistry.js';
|
||||||
|
import { bindFakeGroundRenderer } from '../testSupport/fakeGroundRenderer.mjs';
|
||||||
|
import { fromDegrees } from '../geo/ellipsoid.js';
|
||||||
|
|
||||||
function viewerWithDraw(t) {
|
function viewerWithDraw(t) {
|
||||||
const viewer = { isDestroyed: () => false };
|
const viewer = { isDestroyed: () => false };
|
||||||
@@ -229,3 +231,56 @@ test('clearing a segmented trail lets every line go', (t) => {
|
|||||||
trail.setSegments([points(4)]);
|
trail.setSegments([points(4)]);
|
||||||
assert.equal(historyLines(set).length, 1, 'drawn again after a clear');
|
assert.equal(historyLines(set).length, 1, 'drawn again after a clear');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('a double-click on a trail line zooms to that line, as to the polyline entity it was', (t) => {
|
||||||
|
const viewer = { isDestroyed: () => false };
|
||||||
|
const fake = bindFakeGroundRenderer(viewer);
|
||||||
|
t.after(fake.unbind);
|
||||||
|
let dblclick = null;
|
||||||
|
const stock = { dblclickFollow: true };
|
||||||
|
fake.renderer.input = {
|
||||||
|
onPointer(type, handler) {
|
||||||
|
assert.equal(type, 'dblclick');
|
||||||
|
dblclick = handler;
|
||||||
|
return () => {
|
||||||
|
dblclick = null;
|
||||||
|
};
|
||||||
|
},
|
||||||
|
stockEnabled: (name) => stock[name] === true,
|
||||||
|
};
|
||||||
|
const trail = createTrail(viewer, { color: '#00d4ff' });
|
||||||
|
const a = fromDegrees(0, 0, 1000);
|
||||||
|
const b = fromDegrees(0.1, 0, 1000);
|
||||||
|
trail.setPositions([a, b]);
|
||||||
|
const set = fake.liveSets().find((s) => s.spec.id.startsWith('trail:'));
|
||||||
|
const [line] = set.items.values();
|
||||||
|
fake.setPick(() => ({ set: set.spec.id, id: line.id }));
|
||||||
|
const at = { position: { x: 1, y: 2 } };
|
||||||
|
|
||||||
|
dblclick(at);
|
||||||
|
assert.equal(fake.flights.length, 1, 'zoomed at once');
|
||||||
|
const { sphere, options } = fake.flights[0];
|
||||||
|
assert.equal(options.durationS, 0);
|
||||||
|
const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, z: (a.z + b.z) / 2 };
|
||||||
|
assert.ok(
|
||||||
|
Math.hypot(
|
||||||
|
sphere.center.x - mid.x,
|
||||||
|
sphere.center.y - mid.y,
|
||||||
|
sphere.center.z - mid.z,
|
||||||
|
) < 1,
|
||||||
|
);
|
||||||
|
assert.equal(fake.follows.length, 0, 'nothing followed');
|
||||||
|
|
||||||
|
// Held off (a tool holds the stock follow), a hidden trail line, or
|
||||||
|
// another layer's hit: nothing.
|
||||||
|
stock.dblclickFollow = false;
|
||||||
|
dblclick(at);
|
||||||
|
stock.dblclickFollow = true;
|
||||||
|
fake.setPick(() => ({ set: 'other', id: line.id }));
|
||||||
|
dblclick(at);
|
||||||
|
assert.equal(fake.flights.length, 1);
|
||||||
|
|
||||||
|
// The last trail gone, the viewer's double-click is let go.
|
||||||
|
trail.destroy();
|
||||||
|
assert.equal(dblclick, null);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user