mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Every layer and tool bound its own ScreenSpaceEventHandler and picked the same click again: with the usual layers on, one click ran about 26 scene.pick and 2 drillPick passes (120-215 ms in NYC with 18 layers). Pointer input now comes in through src/input/dispatcher.js, one handler per scene. A click is picked lazily and at most once per kind of query, and every subscriber reads the same ClickEvent (pick, hit, id, ownerId, gesture, drill, ground). The same click now costs 3 picks and 2 drills, 17-70 ms. - Ambient onClick subscribers run only while no tool holds the pointer, checked once per event; tools subscribe with their lease. Moves and releases always reach ambient subscribers, so hover and drags end cleanly. - Camera input and the viewer's stock select/track clicks are ref-counted holds (suspendCamera, suppressStockClicks). The dispatcher owns the controller's enableInputs, so a Cesium flight that ends no longer turns input back on under a drag or the cockpit. - The draw tool, imagery box, CCTV gizmo, cockpit, directions and every selecting layer use it; bindTrackingClickGesture is gone (the gesture is on the event). A source scan forbids new handlers, enableInputs writes and stock-click removal outside the dispatcher. - The sensor-style pick correction transit had now applies to every layer. - Radio's 8-pick near-miss ring is one 17x17 windowed drill. - A missed pick passed to pickContact is no longer picked again, and a touch tap's contact search shares the click's drill. - A gizmo drag whose release Cesium never reported ends on the next press instead of leaving the camera held. The dispatcher is exported as ./input with the pointer-lease functions, and documented in docs/APPLICATION.md.
174 lines
6.0 KiB
JavaScript
174 lines
6.0 KiB
JavaScript
import * as Cesium from 'cesium';
|
|
import { GBFS_CITY_REGISTRY } from './registry.js';
|
|
import { BIKESHARE_SELECTED_OVERLAY_SOURCE_ID } from './policy.js';
|
|
import {
|
|
claimCameraSensitivity,
|
|
releaseCameraSensitivity,
|
|
} from '../../data/cameraSensitivity.js';
|
|
|
|
export function createLifecycle({
|
|
state: layerState,
|
|
services,
|
|
parts,
|
|
source,
|
|
}) {
|
|
const { registerSpriteCollection, restoreSpriteOrder } = services.sprites;
|
|
const { registerPickOwner, unregisterPickOwner } = services.picking;
|
|
|
|
const methods = {
|
|
/**
|
|
* Initialize the bikeshare layer. Creates the point primitive collection,
|
|
* resets all internal state, and installs the click handler.
|
|
* Called once during app bootstrap.
|
|
* @param {Cesium.Viewer} viewer - Cesium viewer instance.
|
|
*/
|
|
init(viewer) {
|
|
layerState._viewer = viewer;
|
|
layerState._pointCollection = new Cesium.PointPrimitiveCollection({
|
|
blendOption: Cesium.BlendOption.TRANSLUCENT,
|
|
});
|
|
viewer.scene.primitives.add(layerState._pointCollection);
|
|
registerSpriteCollection('bikeshare', layerState._pointCollection);
|
|
layerState._pointCollection.show = false;
|
|
|
|
layerState._enabled = false;
|
|
layerState._cameraDebounceTimer = null;
|
|
layerState._cameraChangedAttached = false;
|
|
layerState._altitudeGateEnabled = false;
|
|
layerState._proximityGeneration = 0;
|
|
|
|
layerState._activeCityIds = new Set();
|
|
layerState._cityRuntime = new Map();
|
|
layerState._stationInfoCache = new Map();
|
|
layerState._statusCache = new Map();
|
|
layerState._inFlightInfo = new Map();
|
|
layerState._inFlightStatus = new Map();
|
|
layerState._stationRenderMap = new Map();
|
|
layerState._clickSubscription = null;
|
|
layerState._selectedKey = null;
|
|
layerState._selectedEntity = null;
|
|
layerState._count = 0;
|
|
layerState._lastUpdate = null;
|
|
layerState._loading = false;
|
|
layerState._loadingOps = 0;
|
|
layerState._failedCityIds.clear();
|
|
layerState._limitWarned = false;
|
|
|
|
layerState._overlayHost.setVisible(
|
|
BIKESHARE_SELECTED_OVERLAY_SOURCE_ID,
|
|
false,
|
|
);
|
|
|
|
parts.selection._installClickHandler(viewer);
|
|
|
|
restoreSpriteOrder(viewer);
|
|
|
|
console.log(
|
|
`[Data:Bikeshare] Initialized with ${GBFS_CITY_REGISTRY.length} cities`,
|
|
);
|
|
},
|
|
|
|
/**
|
|
* Enable the bikeshare layer. Shows points, attaches the camera listener,
|
|
* and triggers an initial proximity check.
|
|
* @param {Cesium.Viewer} viewer - Cesium viewer instance.
|
|
*/
|
|
enable(viewer) {
|
|
layerState._enabled = true;
|
|
layerState._failedCityIds.clear();
|
|
layerState._pointCollection.show = true;
|
|
layerState._overlayHost.setVisible(
|
|
BIKESHARE_SELECTED_OVERLAY_SOURCE_ID,
|
|
true,
|
|
);
|
|
parts.selection._installClickHandler(viewer);
|
|
// Pick-ownership (H2): station point ids are string render-map keys.
|
|
registerPickOwner('bikeshare', (pickedId) =>
|
|
layerState._stationRenderMap.has(pickedId),
|
|
);
|
|
|
|
if (!layerState._cameraChangedAttached) {
|
|
viewer.camera.changed.addEventListener(parts.viewport.onCameraChanged);
|
|
// Through the shared ledger: this is a single number every layer's
|
|
// camera.changed listener shares, and a layer leaving must not hand
|
|
// back a coarse default while this one is still driving off it.
|
|
claimCameraSensitivity(viewer.camera, 'bikeshare', 0.05);
|
|
layerState._cameraChangedAttached = true;
|
|
}
|
|
|
|
void parts.viewport.runProximityCheck();
|
|
restoreSpriteOrder(viewer);
|
|
},
|
|
|
|
/**
|
|
* Disable the bikeshare layer. Hides points, removes event listeners,
|
|
* aborts all pending fetches, and tears down all city data.
|
|
* @param {Cesium.Viewer} viewer - Cesium viewer instance.
|
|
*/
|
|
disable(viewer) {
|
|
layerState._enabled = false;
|
|
layerState._proximityGeneration++;
|
|
layerState._altitudeGateEnabled = false;
|
|
clearTimeout(layerState._cameraDebounceTimer);
|
|
layerState._cameraDebounceTimer = null;
|
|
parts.selection._clearSelection();
|
|
layerState._overlayHost.setVisible(
|
|
BIKESHARE_SELECTED_OVERLAY_SOURCE_ID,
|
|
false,
|
|
);
|
|
|
|
layerState._clickSubscription?.();
|
|
layerState._clickSubscription = null;
|
|
document.removeEventListener('keydown', parts.selection._onKeyDown);
|
|
unregisterPickOwner('bikeshare');
|
|
|
|
if (layerState._cameraChangedAttached) {
|
|
viewer.camera.changed.removeEventListener(
|
|
parts.viewport.onCameraChanged,
|
|
);
|
|
releaseCameraSensitivity(viewer.camera, 'bikeshare');
|
|
layerState._cameraChangedAttached = false;
|
|
}
|
|
|
|
parts.ingestion.abortAllInFlight();
|
|
parts.viewport.deactivateAllCities();
|
|
layerState._cityRuntime.clear();
|
|
layerState._pointCollection.show = false;
|
|
layerState._count = 0;
|
|
layerState._loading = false;
|
|
layerState._loadingOps = 0;
|
|
},
|
|
|
|
/** Permanently release primitives, handlers, and the selected host source. */
|
|
destroy(viewer) {
|
|
if (layerState._enabled) this.disable(viewer);
|
|
else {
|
|
parts.selection._clearSelection();
|
|
layerState._overlayHost.setVisible(
|
|
BIKESHARE_SELECTED_OVERLAY_SOURCE_ID,
|
|
false,
|
|
);
|
|
layerState._clickSubscription?.();
|
|
layerState._clickSubscription = null;
|
|
document.removeEventListener('keydown', parts.selection._onKeyDown);
|
|
unregisterPickOwner('bikeshare');
|
|
}
|
|
if (layerState._cameraChangedAttached) {
|
|
viewer.camera.changed.removeEventListener(
|
|
parts.viewport.onCameraChanged,
|
|
);
|
|
releaseCameraSensitivity(viewer.camera, 'bikeshare');
|
|
layerState._cameraChangedAttached = false;
|
|
}
|
|
parts.ingestion.abortAllInFlight();
|
|
if (layerState._pointCollection) {
|
|
viewer.scene.primitives.remove(layerState._pointCollection);
|
|
layerState._pointCollection = null;
|
|
}
|
|
layerState._viewer = null;
|
|
},
|
|
};
|
|
|
|
return { methods };
|
|
}
|