Files
gods-eye-view/src/scenes/interactions.js
T
Sameh Khamis bb3bf9858b fix(click): the card or label on top takes the click in every layer
Review fixes for #1781:

- One rule for cards: the world-overlay host hands its hit test to the
  pick registry, and every click handler reads the topmost interactive
  card or label once per click (`clickCard`, `isUnderOtherCard` in
  src/data/pickRegistry.js), so all handlers agree in any listener order.
  A layer whose own card or label is on top acts on it whatever is drawn
  under it; a layer under another layer's card neither acts nor treats
  the click as empty space (a tracked aircraft stays tracked, a selection
  stays selected); Context names nothing under a card. A selected news
  card over a vessel no longer opens the link and selects the vessel.
- With one rule, the per-owner arbitration options are no longer read
  and are removed: `passive`, `expanded` and `declinesClick` pick owners,
  `rememberClickPickActions`, `isActedOnByOtherLayer`, card backgrounds
  and `cardKeepsClick`. The dispatcher is unchanged from main.
- A drag that ends on a quake disc, label or card does nothing; opening
  the USGS page or clearing the selection needs a clean click.
- A click on the map under another layer's card opens no Street Level
  ground image.
- A CCTV card that is going away is still not the scene under it.
- Docs and changelog describe the new rule.
2026-11-18 18:23:41 -08:00

156 lines
4.9 KiB
JavaScript

import { nextTick } from 'vue';
import { isPointerFree } from '../data/inputOwnership.js';
import { inputFor } from '../input/dispatcher.js';
import { isUnderOtherCard } from '../data/pickRegistry.js';
import { createInteractionSession } from '../director/interactions/session.js';
import { t } from '../i18n/index.js';
import { useSceneInteractionsStore } from '../ui/stores/sceneInteractions.js';
import { applicationPinia } from '../ui/vueRuntime.js';
/**
* Own the active shot's pick handler, accessible actions and plain-text
* feedback. The Scene actions panel is presented to the scene interactions
* store, which its view renders inside the viewer's container.
*/
// Keys each activation's panel, so a new one never reuses an old one's DOM.
let activations = 0;
export function createSceneInteractions(
viewer,
{
execute,
available = () => true,
store = useSceneInteractionsStore(applicationPinia()),
},
) {
let panel = null,
unsubscribeClick,
items = [],
targets = new Map();
// Only the owner whose panel the store shows may change or remove it.
const shown = () => panel && store.panel?.owner === panel.owner;
const update = (patch) => {
if (!shown()) return;
panel = { ...panel, ...patch };
store.publish(panel);
};
const session = createInteractionSession({
execute: (item, signal) => {
if (!isPointerFree() || !available()) return false;
if (item.action.type === 'card') {
update({
card: { text: item.action.text, url: item.action.url || null },
});
return true;
}
update({ card: null });
return execute(item.action, signal);
},
changed(state) {
update({
busy: state.busy,
selected: state.selected,
outlined: state.selected ? [state.selected] : [],
});
},
});
function clear() {
session.clear();
unsubscribeClick?.();
unsubscribeClick = null;
if (shown()) {
store.connect(null);
store.publish(null);
}
panel = null;
items = [];
targets.clear();
}
async function dispatch(id) {
const current = panel;
const ok = await session.dispatch(id);
if (current && panel?.owner === current.owner)
update({
status: ok
? t('Action complete')
: t('Action unavailable or cancelled'),
});
return ok;
}
return {
clear,
destroy: clear,
getState: session.getState,
activate(shot, registered) {
clear();
items = shot.interactions || [];
if (!items.length) return;
targets = registered;
if (
items.some(
(item) =>
!targets.has(
JSON.stringify([item.target.packId, item.target.featureId]),
),
)
) {
items = [];
targets.clear();
throw new Error('Interaction feature is missing from the loaded pack');
}
// Each activation owns its panel; a click on an older one is ignored.
const owner = {};
panel = {
id: ++activations,
owner,
container: viewer.container,
items: items.map(({ id, label }) => ({ id, label })),
status: t('Select a feature or use Tab and Enter to choose an action.'),
busy: false,
selected: null,
outlined: [],
card: null,
};
store.publish(panel);
store.connect({
dispatch(id, from) {
if (from === owner && panel?.owner === owner) void dispatch(id);
},
});
session.activate(items);
unsubscribeClick = inputFor(viewer).onClick((event) => {
if (panel?.owner !== owner || !shown() || !available()) return;
// Another layer's card or label painted on top takes the click: the item
// under it does not act, and the click is not empty space either.
if (isUnderOtherCard(event)) return;
// A pack feature is its draw set's item (see dataPacks/presentation.js).
const hit = event.drawHit;
if (!hit) return;
const matches = items.filter((item) => {
const target = targets.get(
JSON.stringify([item.target.packId, item.target.featureId]),
);
return target?.set === hit.set && target.id === hit.id;
});
if (!matches.length) return;
update({
outlined: matches.map((item) => item.id),
status: t('Selected feature: {feature}. Choose an action.', {
feature: matches[0].target.featureId,
}),
});
// Focus the first match once the panel shows the selection.
void nextTick(() => {
if (panel?.owner !== owner || !shown()) return;
const id = globalThis.CSS?.escape?.(matches[0].id) ?? matches[0].id;
viewer.container
?.querySelector?.(
`[data-director-interactions] [data-director-action="${id}"]`,
)
?.focus({ preventScroll: true });
});
});
},
};
}