Carry Sonar with the Minimal HUD layout in links and scene shots

A link or scene shot made in Minimal with Sonar on played back with Sonar
off, and turned off the viewer's own. Tactical with Sonar keeps the `cyber`
layout code, which older readers understand; Minimal with Sonar now carries
an explicit mark beside its layout: flag bit 5 (`hudSonar`, default off) of
a compact link, `hs=1` in the long form, and `hud.sonar: true` in a scene
shot. A restore turns Sonar on when the state names Cyber or carries the
mark, so a state made before it restores exactly as it did.

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju authored and Sameh Khamis committed 2026-11-18 19:45:47 -08:00
1 parent c69a421678
commit 40e2998466
14 files changed
+266 -28

No files matched your search

+96 -12
View File
@@ -28,6 +28,13 @@ import { setCyberSonarControls } from './cyberSonarControls.js';
import { createPinia } from 'pinia';
import { useDisplayStore } from './stores/display.js';
import { VisualSettings } from './visualSettings.js';
import { normalizeShot } from '../scenes/project.js';
import {
SCENE_DOCUMENT_VERSION,
validateSceneDocument,
} from '../director/document.js';
import { decodeShareView, encodeShareView } from '../share/viewCodec.js';
import { toLegacyShareState } from '../share/legacyShareState.js';
import { STYLES } from './visualPresets.js';
const read = (relative) =>
@@ -140,9 +147,10 @@ test('a share link, scene or saved view naming Cyber opens Tactical with Sonar o
assert.equal(isCyberSonarEnabled(root), true);
});
test('links and scene shots record Tactical with Sonar as the Cyber code, and only that', (t) => {
const { owner, root } = styleOwner(t, 'tactical');
Object.assign(owner.services, {
/** A styleOwner that can also read its share state and scene shot. */
function sharingOwner(t, initialVariant) {
const fixture = styleOwner(t, initialVariant);
Object.assign(fixture.owner.services, {
getDetectionTuning: () => ({ allocationStrategy: 'ELASTIC' }),
getDetectionMode: () => 'OFF',
isScopeMaskEnabled: () => true,
@@ -150,18 +158,94 @@ test('links and scene shots record Tactical with Sonar as the Cyber code, and on
getScopeMaskOpacity: () => 1,
getScopeTerminusOverride: () => null,
});
owner.readCelestialRing = () => null;
const shared = () => owner._readShareState().options.hudVariant;
assert.equal(shared(), 'tactical');
fixture.owner.readCelestialRing = () => null;
return fixture;
}
test('links and scene shots record Tactical with Sonar as the Cyber code, and only that', (t) => {
const { owner, root } = sharingOwner(t, 'tactical');
const shared = () => {
const { hudVariant, hudSonar } = owner._readShareState().options;
return [hudVariant, hudSonar];
};
const shot = () => owner.getVisualState().hud;
assert.deepEqual(shared(), ['tactical', false]);
assert.deepEqual(shot(), { visible: true, variant: 'tactical' });
owner._setCyberSonarEnabled(true);
assert.equal(shared(), 'cyber');
// Minimal has no code that carries Sonar; it stays local.
// Tactical with Sonar is the Cyber code alone, as older readers expect.
assert.deepEqual(shared(), ['cyber', false]);
assert.deepEqual(shot(), { visible: true, variant: 'cyber' });
// Minimal has no such code, so Sonar is carried beside it.
owner._setHudVariant('minimal');
assert.equal(shared(), 'minimal');
owner._setHudVariant('tactical');
assert.equal(shared(), 'cyber');
assert.deepEqual(shared(), ['minimal', true]);
assert.deepEqual(shot(), { visible: true, variant: 'minimal', sonar: true });
owner._setCyberSonarEnabled(false);
assert.equal(shared(), 'tactical');
assert.deepEqual(shared(), ['minimal', false]);
assert.deepEqual(shot(), { visible: true, variant: 'minimal' });
owner._setHudVariant('tactical');
assert.deepEqual(shared(), ['tactical', false]);
assert.equal(isCyberSonarEnabled(root), false);
});
test('Minimal with Sonar round-trips through a compact link and a scene shot', async (t) => {
const { owner, hud, root } = sharingOwner(t, 'minimal');
owner._setCyberSonarEnabled(true);
const options = owner._readShareState().options;
const shot = normalizeShot(
JSON.parse(JSON.stringify({ visual: owner.getVisualState() })),
);
validateSceneDocument({
version: SCENE_DOCUMENT_VERSION,
scenes: [{ id: 'scene', title: 'Scene', shots: [shot] }],
});
const code = encodeShareView({
hudVariant: options.hudVariant,
hudSonar: options.hudSonar,
});
const link = decodeShareView(code, { nowMs: Date.now() }).state;
// The viewer is elsewhere: Tactical, Sonar off.
owner._setHudVariant('tactical');
owner._setCyberSonarEnabled(false);
// The HUD fields of the parsed link (the fixture drives no detection).
const { hudVariant, hudSonar } = toLegacyShareState(link);
assert.deepEqual([hudVariant, hudSonar], ['minimal', true]);
await owner.restoreShareState({ hudVariant, hudSonar });
assert.equal(hud.getVariant(), 'minimal');
assert.equal(isCyberSonarEnabled(root), true);
owner._setHudVariant('tactical');
owner._setCyberSonarEnabled(false);
// The saved shot's HUD, as the editor normalized and validated it.
assert.deepEqual(shot.visual.hud, {
visible: true,
variant: 'minimal',
sonar: true,
});
await owner.applyVisualState({ hud: shot.visual.hud });
assert.equal(hud.getVariant(), 'minimal');
assert.equal(isCyberSonarEnabled(root), true);
});
test('a link or scene shot that does not mention Sonar restores as before', async (t) => {
const { owner, hud, root } = sharingOwner(t, 'minimal');
// Made before Sonar was carried: Minimal turns the viewer's Sonar off...
setCyberSonarEnabled(true, root);
await owner.restoreShareState({ hudVariant: 'minimal' });
assert.equal(isCyberSonarEnabled(root), false);
setCyberSonarEnabled(true, root);
await owner.applyVisualState({ hud: { variant: 'minimal' } });
assert.equal(isCyberSonarEnabled(root), false);
// ...and Cyber still opens Tactical with Sonar on.
await owner.restoreShareState({ hudVariant: 'cyber', hudSonar: false });
assert.equal(hud.getVariant(), 'tactical');
assert.equal(isCyberSonarEnabled(root), true);
owner._setCyberSonarEnabled(false);
await owner.applyVisualState({ hud: { variant: 'cyber' } });
assert.equal(isCyberSonarEnabled(root), true);
// Sonar beside a layout counts only in a restore, not a layout choice.
owner._setCyberSonarEnabled(false);
owner._setHudVariant('minimal', { sonar: true });
assert.equal(isCyberSonarEnabled(root), false);
});
+35 -9
View File
@@ -73,6 +73,7 @@ export class VisualSettings {
bloomVersion,
sharpenIntensity,
hudVariant,
hudSonar,
hudVisible,
detectionMode,
detectionDensity,
@@ -122,7 +123,8 @@ export class VisualSettings {
this._setSharpenIntensity(sharpenIntensity, { syncShare: false });
if (typeof bloom === 'boolean') this._setBloomEnabled(bloom);
if (typeof sharpen === 'boolean') this._setSharpenEnabled(sharpen);
if (hudVariant) this._setHudVariant(hudVariant, { restore: true });
if (hudVariant)
this._setHudVariant(hudVariant, { restore: true, sonar: hudSonar });
if (typeof hudVisible === 'boolean') {
this.hud.setMode(hudVisible ? 'on' : 'off');
this._updateHudButtonState();
@@ -719,15 +721,21 @@ export class VisualSettings {
/**
* @param {string} variantName Layout, or the removed `cyber` layout.
* @param {{restore?: boolean}} [options] A share link, scene or saved view
* is authoritative for Sonar too: it is on only when the state names
* Cyber. Other callers leave Sonar as the viewer set it.
* @param {{restore?: boolean, sonar?: boolean}} [options] A share link,
* scene or saved view is authoritative for Sonar too: it is on only when
* the state names Cyber or says `sonar: true`, so a state that does not
* mention Sonar turns it off unless it names Cyber. Other callers leave
* Sonar as the viewer set it.
*/
_setHudVariant(variantName, { restore = false } = {}) {
_setHudVariant(
variantName,
{ restore = false, sonar: sonarOn = false } = {},
) {
if (!variantName) return;
// Cyber was removed; its sonar became a visual style. A state that names
// Cyber opens in Tactical with Sonar on.
const sonar = isLegacySonarHudLayout(variantName);
const sonar =
isLegacySonarHudLayout(variantName) || (restore && sonarOn === true);
if (sonar || (restore && isCyberSonarEnabled()))
this._setCyberSonarEnabled(sonar);
this.hud.setVariant(variantName);
@@ -761,8 +769,8 @@ export class VisualSettings {
/**
* The HUD layout a share link or scene shot records. Tactical with Sonar on
* is written as the retired Cyber code, which restores exactly that; Minimal
* has no such code, so its Sonar stays local.
* is written as the retired Cyber code, which restores exactly that and
* which older readers understand too.
*/
_sharedHudVariant() {
const variant = this.hud.getVariant();
@@ -771,6 +779,17 @@ export class VisualSettings {
: variant;
}
/**
* Whether a share link or scene shot records Sonar on beside its layout:
* only when the layout code does not already say so (Minimal with Sonar),
* so a state with Tactical and Sonar stays the Cyber code alone.
*/
_sharedHudSonar() {
return (
isCyberSonarEnabled() && !isLegacySonarHudLayout(this._sharedHudVariant())
);
}
_setCyberSonarSetting(name, value) {
const settings = readCyberSonarSettings();
if (!Object.hasOwn(settings, name)) return settings;
@@ -919,6 +938,7 @@ export class VisualSettings {
bloomVersion: BLOOM_SCALE_VERSION,
sharpenIntensity: this._sharpenPct,
hudVariant: this._sharedHudVariant(),
hudSonar: this._sharedHudSonar(),
hudVisible: this.hud.visible,
detectionMode: detection.mode,
detectionDensity: detection.densityPct,
@@ -1237,6 +1257,9 @@ export class VisualSettings {
hud: {
visible: this.hud.visible,
variant: this._sharedHudVariant(),
// Written only when on beside a layout that cannot say it, so a shot
// without Sonar or with Tactical and Sonar reads in older builds.
...(this._sharedHudSonar() ? { sonar: true } : {}),
},
detection: {
mode: getDetectionMode(),
@@ -1284,7 +1307,10 @@ export class VisualSettings {
const hudState = state.hud || {};
if (hudState.variant) {
this._setHudVariant(hudState.variant, { restore: true });
this._setHudVariant(hudState.variant, {
restore: true,
sonar: hudState.sonar,
});
}
if (typeof hudState.visible === 'boolean') {
this.hud.setMode(hudState.visible ? 'on' : 'off');