Share a scene's camera-input holds beyond the dispatcher

The take-over of the camera controller's enableInputs moves, unchanged,
to src/render/cesium/cameraInput.js. acquireCameraInput(scene) gives every
user one set of counted holds per scene, so the renderer's input and
camera ports can hold camera input alongside the dispatcher; the
controller gets its own property back when the last user releases. The
dispatcher acquires its share once its handler is bound. The input guard
names the one module allowed to write the flag, and the new render files
join the format scope.
This commit is contained in:
Sameh Khamis committed 2026-11-05 18:29:38 -08:00
1 parent 35a73b6fda
commit ad98c80118
7 files changed
+237 -77

No files matched your search

+119
View File
@@ -0,0 +1,119 @@
/**
* @module render/cesium/cameraInput
* @description User camera input on a Cesium scene, suspended by counted
* holds. Every suspender gets its own release, and input comes back when the
* last one releases.
*
* The holds own the controller's `enableInputs`: what others write to it
* (Cesium's own camera flights turn it off and back on) is kept, and it reads
* true only while that is true and no hold is out, so a flight that ends can
* no longer re-enable input under a drag or the cockpit.
*
* One set of holds per scene, shared by everyone who acquires it (the input
* port, the camera port, the application's dispatcher); the controller gets
* its own property back when the last of them releases.
*/
/** @type {WeakMap<object, {holds: Set<object>, users: number, restore: () => void}>} */
const shared = new WeakMap();
/**
* Take over the controller's `enableInputs`: what others write to it is kept
* (through an accessor already there, if any), and it reads true only while
* that is true and no hold is out.
* @param {object|undefined} controller
* @param {Set<object>} holds
* @returns {() => void} Gives the controller its own property back.
*/
function ownCameraInput(controller, holds) {
if (!controller) return () => {};
const own = Object.getOwnPropertyDescriptor(controller, 'enableInputs');
if (own && !own.configurable) {
console.warn('Camera input holds are unavailable: enableInputs is fixed');
return () => {};
}
// A class may define it on its prototype; the hold shadows that for a while.
let original = own;
for (let proto = Object.getPrototypeOf(controller); !original && proto;) {
original = Object.getOwnPropertyDescriptor(proto, 'enableInputs');
proto = Object.getPrototypeOf(proto);
}
original ??= { value: true, writable: true, enumerable: true };
let written = Boolean(original.get ? true : original.value);
const read = () =>
original.get ? Boolean(original.get.call(controller)) : written;
Object.defineProperty(controller, 'enableInputs', {
configurable: true,
enumerable: original.enumerable ?? true,
get: () => read() && holds.size === 0,
set: (value) => {
written = Boolean(value);
original.set?.call(controller, value);
},
});
return () => {
delete controller.enableInputs;
if (own?.get || own?.set)
Object.defineProperty(controller, 'enableInputs', own);
else if (own || !(original.get || original.set))
controller.enableInputs = written;
};
}
/**
* A user of the scene's camera-input holds.
* @typedef {object} CameraInput
* @property {(reason: string) => () => void} suspend Hold camera input off;
* returns this hold's release (a second call does nothing).
* @property {boolean} enabled False while any hold, anyone's, is out.
* @property {() => string[]} reasons The reasons of every hold out.
* @property {() => void} release Give back this user's holds, and the
* controller's property once no user is left.
*/
/**
* @param {{screenSpaceCameraController?: object}} scene
* @returns {CameraInput}
*/
export function acquireCameraInput(scene) {
let state = shared.get(scene);
if (!state) {
const holds = new Set();
state = {
holds,
users: 0,
restore: ownCameraInput(scene.screenSpaceCameraController, holds),
};
shared.set(scene, state);
}
state.users += 1;
const s = state;
const mine = new Set();
let released = false;
return {
suspend(reason) {
if (released) return () => {};
const hold = { reason: String(reason) };
s.holds.add(hold);
mine.add(hold);
return () => {
if (!mine.delete(hold)) return;
s.holds.delete(hold);
};
},
get enabled() {
return s.holds.size === 0;
},
reasons: () => [...s.holds].map((hold) => hold.reason),
release() {
if (released) return;
released = true;
for (const hold of mine) s.holds.delete(hold);
mine.clear();
s.users -= 1;
if (s.users > 0) return;
s.restore();
if (shared.get(scene) === s) shared.delete(scene);
},
};
}