mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Settle leases, styles, credits and controls in the renderer contract
- claimPointer takes a grab filter, asked on each down: a lease takes
only the presses it grabs, and wheel, pinch and every other press stay
with the camera and everyone else. Each event of a press goes where its
down went, per button, so a claim never takes a press under way.
- The app owns the styles: effects.defineStyle passes a GLSL (and optionally
WGSL) full-screen pass, with the shader interface written out.
- Credits are plain data ({ text, href, logoUrl, inline }) that the app
renders; CreateOptions.creditContainer goes.
- controls.coast { drag, zoom } in seconds replaces the inertia trio; the
stub's defaults are the default renderer's, converted.
- The frame order is the default renderer's own: a move made by follow or
in preUpdate reaches changed on the next frame.
This commit is contained in:
1 parent
b7c26351f7
commit
a7aeeb558a
12 files changed
+591
-34
No files matched your search
@@ -111,6 +111,7 @@ export function createStubCamera({
|
||||
let moving = false;
|
||||
let lastMovedAt = 0;
|
||||
|
||||
let coast = Object.freeze({ drag: 0.4, zoom: 0.2 });
|
||||
const controls = {
|
||||
get enabled() {
|
||||
return holds.size === 0;
|
||||
@@ -125,9 +126,18 @@ export function createStubCamera({
|
||||
enableZoom: true,
|
||||
minDistance: 1,
|
||||
maxDistance: Infinity,
|
||||
inertiaSpin: 0.9,
|
||||
inertiaTranslate: 0.9,
|
||||
inertiaZoom: 0.8,
|
||||
// The default renderer's inertia as decay times: its speed falls as
|
||||
// exp(-25 (1 - c) t), so c = 0.9 (spin, which drags and tilts in 3D)
|
||||
// is 0.4 s and c = 0.8 (zoom) 0.2 s. Back: c = coast > 0 ? 1 - 1 / (25 coast) : 1.
|
||||
get coast() {
|
||||
return coast;
|
||||
},
|
||||
set coast(value) {
|
||||
for (const key of ['drag', 'zoom'])
|
||||
if (!(Number.isFinite(value?.[key]) && value[key] >= 0))
|
||||
throw new TypeError(`coast.${key} must be seconds, 0 or more`);
|
||||
coast = Object.freeze({ drag: value.drag, zoom: value.zoom });
|
||||
},
|
||||
collision: 'none',
|
||||
zoomFactor: 5,
|
||||
pitchRange: [-Math.PI / 2, 0],
|
||||
|
||||
+136
-6
@@ -733,6 +733,16 @@ export function createStubRenderer(options = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
/** Each lease's `grab`, or null when it takes every press. */
|
||||
const leaseGrab = new WeakMap();
|
||||
/**
|
||||
* The presses under way, by button, from their `down`: `owner` is the
|
||||
* lease each went to, or null when it went to everyone else. A press's
|
||||
* click and double-click go where its down went (an ended owner's to no
|
||||
* one).
|
||||
*/
|
||||
const presses = new Map();
|
||||
|
||||
const leaseOf = (entry) =>
|
||||
typeof entry.lease === 'function' ? entry.lease() : entry.lease;
|
||||
|
||||
@@ -746,9 +756,10 @@ export function createStubRenderer(options = {}) {
|
||||
if (i >= 0) pointerListeners.splice(i, 1);
|
||||
};
|
||||
},
|
||||
claimPointer(owner) {
|
||||
claimPointer(owner, opts = {}) {
|
||||
if (currentLease) currentLease.release();
|
||||
let released = false;
|
||||
const grab = typeof opts.grab === 'function' ? opts.grab : null;
|
||||
const lease = {
|
||||
owner: String(owner),
|
||||
get current() {
|
||||
@@ -761,6 +772,7 @@ export function createStubRenderer(options = {}) {
|
||||
},
|
||||
};
|
||||
currentLease = lease;
|
||||
leaseGrab.set(lease, grab);
|
||||
return lease;
|
||||
},
|
||||
isPointerFree: () => currentLease === null,
|
||||
@@ -774,8 +786,91 @@ export function createStubRenderer(options = {}) {
|
||||
ctrlWheelZoom: false,
|
||||
};
|
||||
|
||||
/** Who a new press belongs to, decided from its `down`. */
|
||||
function pressFor(down) {
|
||||
const lease = currentLease;
|
||||
if (!lease) return { owner: null, clicked: false };
|
||||
const grab = leaseGrab.get(lease);
|
||||
let grabbed = !grab;
|
||||
if (grab)
|
||||
try {
|
||||
grabbed = grab(down) === true;
|
||||
} catch (error) {
|
||||
report(error);
|
||||
}
|
||||
return { owner: grabbed ? lease : null, clicked: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* Where an event goes: to one lease's listeners (`lease`), to everyone
|
||||
* without a lease (`ambient`), or both (`move` and `up`).
|
||||
*/
|
||||
function recipients(type, ev) {
|
||||
const button = ev.button;
|
||||
// A click comes straight after its press's up, and a double-click
|
||||
// straight after that click, by the same button at the same spot;
|
||||
// anything else after the up ends the press.
|
||||
for (const [b, p] of presses) {
|
||||
if (!p.up) continue;
|
||||
const continues =
|
||||
b === button &&
|
||||
((type === 'click' &&
|
||||
!p.clicked &&
|
||||
Math.hypot(p.at.x - ev.position.x, p.at.y - ev.position.y) <= 4) ||
|
||||
(type === 'dblclick' &&
|
||||
p.clicked &&
|
||||
Math.hypot(p.at.x - ev.position.x, p.at.y - ev.position.y) <= 4));
|
||||
if (!continues) presses.delete(b);
|
||||
}
|
||||
if (type === 'up' && presses.has(button)) {
|
||||
const p = presses.get(button);
|
||||
p.up = true;
|
||||
p.at = ev.position;
|
||||
}
|
||||
if (type === 'move' || type === 'up')
|
||||
return { lease: currentLease, ambient: true };
|
||||
if (type === 'wheel' || type === 'pinch') {
|
||||
const exclusive = currentLease && !leaseGrab.get(currentLease);
|
||||
return exclusive
|
||||
? { lease: currentLease, ambient: false }
|
||||
: { lease: null, ambient: true };
|
||||
}
|
||||
// `grab` sees a click with no press of its own as the down it stands
|
||||
// for: no pick, no gesture.
|
||||
const asDown = () => ({
|
||||
...pressFor({
|
||||
type: 'down',
|
||||
position: ev.position,
|
||||
button: ev.button,
|
||||
modifiers: ev.modifiers,
|
||||
timestamp: ev.timestamp,
|
||||
pointerType: ev.pointerType,
|
||||
}),
|
||||
up: true,
|
||||
});
|
||||
let current;
|
||||
if (type === 'down') {
|
||||
current = pressFor(ev);
|
||||
presses.set(button, current);
|
||||
} else {
|
||||
// Only a press the loop above kept, at the right step, continues;
|
||||
// anything else is a press of its own and leaves a held one alone.
|
||||
const held = presses.get(button);
|
||||
const continues =
|
||||
held?.up && (type === 'click' ? !held.clicked : held.clicked);
|
||||
current = continues ? held : asDown();
|
||||
if (type === 'click' && (continues || !held)) {
|
||||
current.clicked = true;
|
||||
presses.set(button, current);
|
||||
} else if (continues) presses.delete(button);
|
||||
}
|
||||
current.at = ev.position;
|
||||
return current.owner
|
||||
? { lease: current.owner, ambient: false }
|
||||
: { lease: null, ambient: true };
|
||||
}
|
||||
|
||||
function dispatchPointer(type, x, y, extra = {}) {
|
||||
const free = currentLease === null;
|
||||
let picked;
|
||||
const ev = {
|
||||
type,
|
||||
@@ -801,13 +896,13 @@ export function createStubRenderer(options = {}) {
|
||||
opts.limit === undefined ? hits().slice() : hits().slice(0, opts.limit);
|
||||
ev.ground = () => rayEllipsoid(pickRay(x, y));
|
||||
}
|
||||
const ambientAlways = type === 'move' || type === 'up';
|
||||
const to = recipients(type, ev);
|
||||
for (const entry of pointerListeners.slice()) {
|
||||
if (!entry.live || entry.type !== type) continue;
|
||||
if (entry.lease) {
|
||||
const lease = leaseOf(entry);
|
||||
if (!lease?.current) continue;
|
||||
} else if (!free && !ambientAlways) continue;
|
||||
if (!to.lease || lease !== to.lease || !lease.current) continue;
|
||||
} else if (!to.ambient) continue;
|
||||
try {
|
||||
entry.cb(ev);
|
||||
} catch (error) {
|
||||
@@ -982,6 +1077,8 @@ export function createStubRenderer(options = {}) {
|
||||
modulation: null,
|
||||
};
|
||||
const effectListeners = [];
|
||||
/** Styles the app defined, by id. */
|
||||
const defined = new Map();
|
||||
const STYLES = Object.freeze([
|
||||
{
|
||||
id: 'retro',
|
||||
@@ -997,7 +1094,40 @@ export function createStubRenderer(options = {}) {
|
||||
}
|
||||
const effects = {
|
||||
styles: () =>
|
||||
capabilities.has('styles') ? STYLES.map((s) => ({ ...s })) : [],
|
||||
capabilities.has('styles')
|
||||
? [
|
||||
...STYLES.filter((s) => !defined.has(s.id)),
|
||||
...defined.values(),
|
||||
].map(({ id, params, animated }) => ({
|
||||
id,
|
||||
params: params.map((p) => ({ ...p })),
|
||||
animated,
|
||||
}))
|
||||
: [],
|
||||
defineStyle(definition) {
|
||||
// The stub runs GLSL only, as the default renderer does.
|
||||
if (
|
||||
!capabilities.has('styles') ||
|
||||
typeof definition?.shader?.glsl !== 'string'
|
||||
)
|
||||
return null;
|
||||
const entry = {
|
||||
id: String(definition.id),
|
||||
params: (definition.params ?? []).map((p) => ({ ...p })),
|
||||
animated: Boolean(definition.animated),
|
||||
};
|
||||
defined.set(entry.id, entry);
|
||||
effectsChanged();
|
||||
let removed = false;
|
||||
return () => {
|
||||
if (removed) return;
|
||||
removed = true;
|
||||
if (defined.get(entry.id) !== entry) return;
|
||||
defined.delete(entry.id);
|
||||
if (effectState.style === entry.id) effectState.style = null;
|
||||
effectsChanged();
|
||||
};
|
||||
},
|
||||
setStyle(id, params) {
|
||||
effectState.style = id;
|
||||
if (params) effectState.params[id] = { ...params };
|
||||
|
||||
Reference in new issue
Block a user