mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-06 21:04:18 +01:00
One set of semantic colour tokens (src/ui/theme.py) drives the uConsole and, generated into tokens.css, the Remote Crew browser. Night (default), day (light, paper LOFARgram waterfall) and high contrast; a Dark/Light pill in the top bar and the Options row switch it, red light forces night. The theme is a preference only, saves stay v50. Panels are rounded with an accent title mark, tabs are pills and key hints chips. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AYDCcBWFmT4ipVCfJeKESR
114 lines
5.7 KiB
JavaScript
114 lines
5.7 KiB
JavaScript
import { S } from "../state/store.js";
|
|
import { $, sideStations, stationKey, stationNames } from "../core/base.js";
|
|
import { t } from "../core/format.js";
|
|
import { setRedLightTheme } from "../core/theme.js";
|
|
import { node } from "./dom.js";
|
|
import { activateTab, chooseStation } from "./lobby.js";
|
|
import { toggleWeatherStation } from "./weather.js";
|
|
|
|
// ---- Station tabs -----------------------------------------------------
|
|
// One tab per station this client holds, in fixed station order. The digit on
|
|
// a tab is its station number (1-9), also usable as a hotkey.
|
|
const stationTabText = {bridge: "tab_bridge", sonar: "tab_sonar", weapons: "tab_weapons", damage: "tab_damage",
|
|
opz: "tab_opz", radio: "tab_radio", engine: "tab_engine", helicopter: "tab_helicopter", eloka: "tab_eloka",
|
|
uboot: "tab_uboot", uboot_sonar: "tab_uboot_sonar", uboot_weapons: "tab_uboot_weapons",
|
|
uboot_engine: "tab_uboot_engine", uboot_esm: "tab_uboot_esm", uboot_nav: "tab_uboot_nav",
|
|
uboot_radio: "tab_uboot_radio"};
|
|
export function renderStationTabs(leased, shown) {
|
|
const bar = $("station-tabs");
|
|
const signature = leased.map((station) => `${station}:${t(stationTabText[station])}`).join("|");
|
|
if (bar.dataset.signature !== signature) {
|
|
bar.replaceChildren(...leased.map((station) => {
|
|
const tab = node("button", undefined, "station-tab");
|
|
tab.type = "button";
|
|
tab.id = `station-tab-${station}`;
|
|
tab.setAttribute("role", "tab");
|
|
tab.setAttribute("aria-controls", "panel-operations");
|
|
tab.dataset.station = station;
|
|
tab.setAttribute("aria-label", t(`station_${station}`));
|
|
tab.append(node("span", String(stationKey(station)), "station-key"),
|
|
node("span", t(stationTabText[station]), "station-name"));
|
|
tab.addEventListener("click", () => chooseStation(station));
|
|
tab.addEventListener("keydown", (event) => {
|
|
const tabs = [...bar.children];
|
|
let index = tabs.indexOf(tab);
|
|
if (event.key === "ArrowRight") index = (index + 1) % tabs.length;
|
|
else if (event.key === "ArrowLeft") index = (index - 1 + tabs.length) % tabs.length;
|
|
else if (event.key === "Home") index = 0;
|
|
else if (event.key === "End") index = tabs.length - 1;
|
|
else return;
|
|
event.preventDefault();
|
|
tabs[index].focus();
|
|
chooseStation(tabs[index].dataset.station);
|
|
});
|
|
return tab;
|
|
}));
|
|
bar.dataset.signature = signature;
|
|
}
|
|
for (const tab of bar.children) {
|
|
const on = tab.dataset.station === shown;
|
|
tab.setAttribute("aria-selected", String(on));
|
|
tab.tabIndex = on ? 0 : -1;
|
|
tab.title = `${t(`station_${tab.dataset.station}`)} \u00b7 ${t("station_tab_hint", {key: stationKey(tab.dataset.station)})}`;
|
|
}
|
|
renderStationAlarms();
|
|
}
|
|
// Alarm lamps on the tabs (the projection's ``alarms``: this side's stations
|
|
// with a torpedo, missile, fire or leak alarm) and the red light, which comes
|
|
// on by itself at night and on an alarm unless switched off in the settings.
|
|
export function renderStationAlarms() {
|
|
const alarms = new Map((S.v2State?.alarms ?? []).map((row) => [row.station, row.level]));
|
|
for (const tab of $("station-tabs").children) {
|
|
const level = alarms.get(tab.dataset.station);
|
|
if (level) {
|
|
tab.dataset.alarm = level;
|
|
tab.title = `${t(`station_${tab.dataset.station}`)} \u00b7 ${t(`station_alarm_${level}`)}`;
|
|
} else delete tab.dataset.alarm;
|
|
}
|
|
const danger = [...alarms.values()].includes("danger");
|
|
const red = S.redLightAuto !== false && Boolean(S.v2State?.role) &&
|
|
(danger || S.v2State?.environment?.is_night === true);
|
|
document.body.classList.toggle("red-light", red);
|
|
// The red light (and the boat's red silent-running light, set by the
|
|
// station view just before) forces the night theme while it is lit, as on
|
|
// the uConsole: a red filter over a light page would glare.
|
|
setRedLightTheme(red || document.body.dataset.boatLight === "red");
|
|
// Sferics: a thunderstorm crackles on the ESM and HF/DF receivers.
|
|
const storm = Number(S.v2State?.environment?.storm) || 0;
|
|
$("sferics").hidden = !(storm > 0 && SFERICS_STATIONS.has(S.v2State?.role));
|
|
}
|
|
const SFERICS_STATIONS = new Set(["eloka", "radio", "uboot_esm", "uboot_radio"]);
|
|
function stepStation(delta) {
|
|
const held = stationNames.filter((station) => S.session?.stations[station].status === "mine");
|
|
if (held.length < 2) return;
|
|
const index = held.indexOf(S.activatingStation ?? S.session.station);
|
|
chooseStation(held[(index + delta + held.length) % held.length]);
|
|
}
|
|
|
|
export function init() {
|
|
document.addEventListener("keydown", (event) => {
|
|
if (!S.session?.station || event.ctrlKey || event.altKey || event.metaKey || event.isComposing ||
|
|
event.repeat || $("operations").hidden) return;
|
|
const target = event.target;
|
|
if (event.key === "0" && !(target instanceof Element && (target.closest("input, select, textarea") || target.isContentEditable)) &&
|
|
!document.querySelector("dialog[open]:not(#weather-dialog)")) {
|
|
event.preventDefault();
|
|
toggleWeatherStation();
|
|
return;
|
|
}
|
|
if (target instanceof Element && (target.closest("input, select, textarea, dialog[open]") ||
|
|
target.isContentEditable)) return;
|
|
if (/^[1-9]$/.test(event.key)) {
|
|
const held = stationNames.find((name) => S.session.stations[name].status === "mine");
|
|
const station = sideStations(held ?? "bridge")[Number(event.key) - 1];
|
|
if (station && S.session.stations[station].status === "mine") { event.preventDefault(); chooseStation(station); }
|
|
} else if (event.key === "[" || event.key === "]") {
|
|
event.preventDefault();
|
|
stepStation(event.key === "]" ? 1 : -1);
|
|
} else if (event.key === "?") {
|
|
event.preventDefault();
|
|
activateTab("guide");
|
|
}
|
|
});
|
|
}
|