Files
gods-eye-view/src/ui/cockpitRollingValue.js
T
Sameh Khamis a2bca0fb22 feat(ui): render the Cockpit instruments, route and vision selector from a store
The Cockpit controller no longer writes the HUD's instruments, the flight
plan card, the visor's destination arrow or the vision selector. It keeps
them as plain state, starting from cockpitInstrumentState.js, and
publishes copies to the gev-cockpit store: the instruments on every HUD
update, the route only when it changed, the vision selector when the mode
changes. CockpitView renders the callsign, aircraft meta and emergency
mark, speed and altitude rims and ticks, compass tape, clock, position,
HUD layer, destination arrow and vision selector; a new CockpitRoute
component renders the flight plan card inside the briefing card.

The odometer readouts become CockpitRollingValue components. Their
throttle and digit roll move unchanged from setCockpitRollingValue into
cockpitRollingValue.js: every accepted reading restamps the throttle, a
change inside it is dropped, and each accepted change draws fresh glyphs
so the roll animation restarts. A rim tick with no reading still hides
where it stood, and a hidden arrow keeps its last angle and label.

In a dev build the HUD's Vue patch costs about 0.5 ms per update (1.4 ms
at 4x CPU throttle), against 0.4 ms (2.1 ms) for the DOM writes it
replaces, at about 10 updates a second.
2026-10-31 14:36:32 -07:00

117 lines
3.6 KiB
JavaScript

/**
* The Cockpit odometer readouts (ground speed, altitude, heading): which
* glyphs a reading shows and which of them roll. It has no imports, so the
* view that renders the glyphs can read it.
*/
/** The shortest gap between two rolls, in ms. */
const ROLL_THROTTLE_MS = 220;
// Digit grouping and decimal marks (`26,175`, `26 175`, `26.175`) take a
// narrow cell; a full digit cell would open a gap in the middle of a number.
const ROLL_SEPARATOR = /^[,.\u00a0\u202f]$/;
function rollTokenClass(character) {
return ROLL_SEPARATOR.test(character)
? 'cockpit-roll-token is-separator'
: 'cockpit-roll-token';
}
/**
* A readout before its first reading: no glyphs, so the view shows its
* placeholder.
* @returns {object}
*/
export function createRollingState() {
return {
text: undefined,
value: NaN,
at: NaN,
tokens: null,
generation: 0,
};
}
/**
* Take a reading. A changed reading inside the throttle is dropped; any
* other reading restamps the readout. A changed reading rolls each digit
* that differs, unless it is the first or `immediate`, which draw it flat.
*
* @param {object} state From createRollingState or an earlier call.
* @param {string} text The reading as shown.
* @param {?number} numericValue Its value, for the roll direction.
* @param {{circularRange?: ?number, immediate?: boolean, nowMs?: number}} [options]
* @returns {object} `state` itself when its glyphs stay, else a new state
* with a new `generation`, so every glyph is drawn afresh and its roll
* animation restarts.
*/
export function advanceRollingValue(
state,
text,
numericValue,
{ circularRange = null, immediate = false, nowMs = performance.now() } = {},
) {
const nextText = String(text);
const previousText = state.text;
const previousValue = state.value;
if (
!immediate &&
previousText !== undefined &&
previousText !== nextText &&
Number.isFinite(state.at) &&
nowMs - state.at < ROLL_THROTTLE_MS
) {
return state;
}
state.at = nowMs;
state.value = Number.isFinite(numericValue) ? numericValue : NaN;
if (previousText === nextText && state.tokens) return state;
const next = { ...state, text: nextText, generation: state.generation + 1 };
if (immediate || previousText === undefined || previousText === nextText) {
next.tokens = Array.from(nextText, (character) => ({
className: rollTokenClass(character),
text: character,
track: null,
}));
return next;
}
let delta =
Number.isFinite(numericValue) && Number.isFinite(previousValue)
? numericValue - previousValue
: 0;
if (Number.isFinite(circularRange) && circularRange > 0) {
const halfRange = circularRange / 2;
if (delta > halfRange) delta -= circularRange;
else if (delta < -halfRange) delta += circularRange;
}
const direction = delta < 0 ? 'down' : 'up';
const width = Math.max(previousText.length, nextText.length);
const from = previousText.padStart(width, ' ');
const to = nextText.padStart(width, ' ');
next.tokens = Array.from(to, (nextCharacter, index) => {
const previousCharacter = from[index];
if (
previousCharacter === nextCharacter ||
!/\d/.test(previousCharacter) ||
!/\d/.test(nextCharacter)
) {
return {
className: rollTokenClass(nextCharacter),
text: nextCharacter === ' ' ? '\u00a0' : nextCharacter,
track: null,
};
}
return {
className: `${rollTokenClass(nextCharacter)} is-rolling roll-${direction}`,
text: null,
track:
direction === 'up'
? [previousCharacter, nextCharacter]
: [nextCharacter, previousCharacter],
};
});
return next;
}