mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
fix(cctv): a double-click zooms to a coverage line and follows the monitor plane again
The coverage rays and cap were polyline entities, which the default renderer's double-click zoomed to, and the monitor plane a plane entity with a position, which it followed until the plane was removed. As draw-port items they took no part in it. The camera icons were billboards before, so they still take none.
This commit is contained in:
1 parent
0dc3a1fb66
commit
898909c245
3 files changed
+122
-2
No files matched your search
@@ -17,6 +17,8 @@
|
||||
* Meshes and quads are capabilities: on a renderer without them there are
|
||||
* no viewshed volumes or monitor plane, and the lines still draw.
|
||||
*/
|
||||
import { positionToEcef } from '../../geo/ellipsoid.js';
|
||||
import { drawnItemTarget } from '../../data/stockFollow.js';
|
||||
import { drawOf } from '../../render/rendererOf.js';
|
||||
|
||||
/** Layer instances, for set ids unique to each. */
|
||||
@@ -91,6 +93,9 @@ export function createDrawSets({ state: layerState }) {
|
||||
plane: draw.quads?.({ id: `cctv:${n}:plane` }) ?? null,
|
||||
// What each line item was last written as, by id.
|
||||
lineKeys: new Map(),
|
||||
// Each line and plane item as last written, by id.
|
||||
lineItems: new Map(),
|
||||
planeItems: new Map(),
|
||||
};
|
||||
return current;
|
||||
}
|
||||
@@ -102,6 +107,7 @@ export function createDrawSets({ state: layerState }) {
|
||||
/** Let the sets go (the layer leaves its viewer, or moves to another). */
|
||||
function dispose() {
|
||||
if (!current) return;
|
||||
layerState._stockFollow?.stop();
|
||||
for (const set of [current.coverage, current.viewshed, current.plane])
|
||||
try {
|
||||
set?.dispose();
|
||||
@@ -139,6 +145,7 @@ export function createDrawSets({ state: layerState }) {
|
||||
const key = lineKey(item);
|
||||
if (s.lineKeys.get(item.id) === key) continue;
|
||||
s.lineKeys.set(item.id, key);
|
||||
s.lineItems.set(item.id, item);
|
||||
changed.push(item);
|
||||
}
|
||||
if (changed.length) s.coverage.upsert(changed);
|
||||
@@ -150,17 +157,62 @@ export function createDrawSets({ state: layerState }) {
|
||||
if (!s) return;
|
||||
s.coverage.clear();
|
||||
s.lineKeys.clear();
|
||||
s.lineItems.clear();
|
||||
}
|
||||
|
||||
/** Write the plane handle as it is now. */
|
||||
function writePlane(plane) {
|
||||
if (!plane?.id) return;
|
||||
sets()?.plane?.upsert([quadItemOf(plane)]);
|
||||
const s = sets();
|
||||
if (!s?.plane) return;
|
||||
const item = quadItemOf(plane);
|
||||
s.planeItems.set(item.id, item);
|
||||
s.plane.upsert([item]);
|
||||
}
|
||||
|
||||
/** Take a plane out. */
|
||||
function removePlane(plane) {
|
||||
if (plane?.id) live()?.plane?.remove([plane.id]);
|
||||
const s = live();
|
||||
if (!plane?.id || !s?.plane) return;
|
||||
s.planeItems.delete(plane.id);
|
||||
s.plane.remove([plane.id]);
|
||||
// Removing the plane entity ended the default renderer's follow of it.
|
||||
layerState._stockFollow?.retain(
|
||||
(key) => key !== `${s.plane.id}:${plane.id}`,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* What a double-click on a hit follows or zooms to, as the default
|
||||
* renderer did for the entities these were: a coverage line (a polyline
|
||||
* entity) is zoomed to and the monitor plane (a plane entity with a
|
||||
* position) followed, framed to fit it. Null for anything else.
|
||||
* @param {{set: string, id: string}} hit
|
||||
*/
|
||||
function followTargetFor(hit) {
|
||||
const s = live();
|
||||
if (!s) return null;
|
||||
if (hit.set === s.coverage.id) {
|
||||
const item = s.lineItems.get(hit.id);
|
||||
return item && item.show !== false
|
||||
? drawnItemTarget(hit.set, item)
|
||||
: null;
|
||||
}
|
||||
if (!s.plane || hit.set !== s.plane.id) return null;
|
||||
const item = s.planeItems.get(hit.id);
|
||||
if (!item?.center) return null;
|
||||
const planes = s.planeItems;
|
||||
const at = positionToEcef(item.center);
|
||||
const [width, height] = item.size ?? [0, 0];
|
||||
return {
|
||||
key: `${hit.set}:${hit.id}`,
|
||||
// Where the plane is drawn now, as the entity's position was read.
|
||||
position: () => {
|
||||
const now = planes.get(hit.id)?.center;
|
||||
return now ? positionToEcef(now) : null;
|
||||
},
|
||||
sphere: { center: at, radius: Math.hypot(width, height) / 2 || 0 },
|
||||
};
|
||||
}
|
||||
|
||||
/** Write a viewshed volume. */
|
||||
@@ -190,6 +242,7 @@ export function createDrawSets({ state: layerState }) {
|
||||
removePlane,
|
||||
writeVolume,
|
||||
removeVolume,
|
||||
followTargetFor,
|
||||
volumeWritesForTest,
|
||||
};
|
||||
}
|
||||
@@ -133,3 +133,57 @@ test('a renderer without meshes or quads still draws the lines', () => {
|
||||
unbind();
|
||||
}
|
||||
});
|
||||
|
||||
test('a double-click zooms to a coverage line and follows the plane, as their entities were', () => {
|
||||
const viewer = {};
|
||||
const { unbind } = bindFakeDraw(viewer);
|
||||
const retained = [];
|
||||
const state = {
|
||||
_viewer: viewer,
|
||||
_stockFollow: { retain: (drawn) => retained.push(drawn), stop() {} },
|
||||
};
|
||||
const drawSets = createDrawSets({ state });
|
||||
try {
|
||||
const a = line('cctv-cam-ray-tl');
|
||||
drawSets.writeLines([a]);
|
||||
const plane = {
|
||||
id: 'cctv-cam-plane',
|
||||
cameraId: 'cam',
|
||||
center: { x: 10, y: 0, z: 0 },
|
||||
orientation: { x: 0, y: 0, z: 0, w: 1 },
|
||||
size: [6, 8],
|
||||
show: true,
|
||||
};
|
||||
drawSets.writePlane(plane);
|
||||
const [coverage, planes] = drawSets.pickableSetIds();
|
||||
|
||||
const zoom = drawSets.followTargetFor({ set: coverage, id: a.id });
|
||||
assert.deepEqual(zoom.zoomTo.center, { x: 2.5, y: 3.5, z: 4.5 });
|
||||
|
||||
const follow = drawSets.followTargetFor({ set: planes, id: plane.id });
|
||||
assert.equal(follow.key, `${planes}:${plane.id}`);
|
||||
assert.deepEqual(follow.position(), { x: 10, y: 0, z: 0 });
|
||||
assert.equal(follow.sphere.radius, 5, 'framed to fit the plane');
|
||||
plane.center = { x: 20, y: 0, z: 0 };
|
||||
drawSets.writePlane(plane);
|
||||
assert.deepEqual(follow.position(), { x: 20, y: 0, z: 0 }, 'as drawn now');
|
||||
|
||||
assert.equal(drawSets.followTargetFor({ set: 'other', id: a.id }), null);
|
||||
a.show = false;
|
||||
drawSets.writeLines([a]);
|
||||
assert.equal(
|
||||
drawSets.followTargetFor({ set: coverage, id: a.id }),
|
||||
null,
|
||||
'a hidden line takes no part',
|
||||
);
|
||||
|
||||
// Taking the plane out ends a follow of it, and only of it.
|
||||
drawSets.removePlane(plane);
|
||||
assert.equal(retained.length, 1);
|
||||
assert.equal(retained[0](follow.key), false);
|
||||
assert.equal(retained[0]('other:key'), true);
|
||||
} finally {
|
||||
drawSets.dispose();
|
||||
unbind();
|
||||
}
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import { viewshedColors, cameraHue } from '../../data/cctvViewshed.js';
|
||||
import { CCTV_OVERLAY_SOURCE_ID } from '../../data/cctvCards.js';
|
||||
import { GIZMO_ID_PREFIX } from '../../data/cctvGizmo.js';
|
||||
import { inputFor } from '../../input/dispatcher.js';
|
||||
import { followOnDoubleClick } from '../../data/stockFollow.js';
|
||||
import {
|
||||
GROUND_PRIOR_INIT_WAIT_MS,
|
||||
CALIBRATION_RANGE_FLOOR_M,
|
||||
@@ -438,9 +439,21 @@ export function createLifecycle({
|
||||
const unsubscribeHover = input.onPointer('move', (movement) =>
|
||||
parts.hover.handleHoverMove(movement?.endPosition),
|
||||
);
|
||||
// A double-click zooms to a coverage line and follows the monitor
|
||||
// plane, as the default renderer did for their entities (the camera
|
||||
// icons were billboards, which it never followed).
|
||||
layerState._stockFollow = followOnDoubleClick({
|
||||
viewer: layerState._viewer,
|
||||
inputFor: () => input,
|
||||
name: 'cctv',
|
||||
enabled: () => layerState._enabled,
|
||||
targetFor: (hit) => parts.drawSets.followTargetFor(hit),
|
||||
});
|
||||
layerState._clickSubscription = () => {
|
||||
unsubscribeClick();
|
||||
unsubscribeHover();
|
||||
layerState._stockFollow?.destroy();
|
||||
layerState._stockFollow = null;
|
||||
};
|
||||
|
||||
await parts.health.syncHealthState(true);
|
||||
|
||||
Reference in new issue
Block a user