@pretty-chitty/core 2.3.5 → 2.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/1ea77c9e193d437b9bfa.woff +0 -0
- package/dist/2282b9ff81c2cd7cf5b4.mp3 +0 -0
- package/dist/22a445dd65cce94defdd.woff2 +0 -0
- package/dist/2b94f846c05846d73536.mp3 +0 -0
- package/dist/32d6296b80e676d66bda.woff +0 -0
- package/dist/36e7e033bfef7b32bf3c.mp3 +0 -0
- package/dist/37f3b2ddcbe588b2aaa5.jpg +0 -0
- package/dist/3f0fb9058a03e14ad2fa.jpg +0 -0
- package/dist/412050b824afb24fef88.mp3 +0 -0
- package/dist/468cf705a21091e5f755.mp3 +0 -0
- package/dist/47a823ae095f5f4cfd82.woff +0 -0
- package/dist/5062c95fb3c68af2ee2e.woff2 +0 -0
- package/dist/5aecd40f51b84ffb6efa.woff2 +0 -0
- package/dist/5d184fc5808150d51726.mp3 +0 -0
- package/dist/60b937eec087d648c831.woff2 +0 -0
- package/dist/63da6d06ce0ff84b8b2c.mp3 +0 -0
- package/dist/63f3d4f28784375801ac.woff +0 -0
- package/dist/669804475e1d5e52a4f5.mp3 +0 -0
- package/dist/6a6d042ba2a9290aba9c.woff2 +0 -0
- package/dist/72e60d1c7f7c603f31d6.woff +0 -0
- package/dist/788c3dfe27f9734edff5.mp3 +0 -0
- package/dist/7e339dfbea59fe54f2e3.png +0 -0
- package/dist/7ecd698217d211cad841.mp3 +0 -0
- package/dist/8c8f3dcefaec6a40d787.mp3 +0 -0
- package/dist/8d29c92c7f9ca79d8a5c.mp3 +0 -0
- package/dist/8ea5f16c95238d2ff902.mp3 +0 -0
- package/dist/97e19a7d82ab67080019.mp3 +0 -0
- package/dist/98373f88bbc3b8237b09.mp3 +0 -0
- package/dist/98c71bee993adc81d5be.mp3 +0 -0
- package/dist/a09db3cc936e581f320a.mp3 +0 -0
- package/dist/a51c64560480e6fdae0d.mp3 +0 -0
- package/dist/ad2392bc6cdf9bb1b5fd.mp3 +0 -0
- package/dist/b3708bfbeff0d340bdef.mp3 +0 -0
- package/dist/b81893c113e684d9eec4.woff +0 -0
- package/dist/bc6bc3d7802469910f83.mp3 +0 -0
- package/dist/bde37bf538e175149c3b.mp3 +0 -0
- package/dist/d6b5a560b17d31cd3c7c.mp3 +0 -0
- package/dist/dd3231a4b15c4f8f2b94.mp3 +0 -0
- package/dist/f63358bf5b9e4f8d7f79.mp3 +0 -0
- package/dist/fc02be176de567378fa4.woff2 +0 -0
- package/dist/fe1535c25dc5d879c085.mp3 +0 -0
- package/dist/index.html +12 -0
- package/dist/main.034dfcc6b83fdd218994.hot-update.js +265 -0
- package/dist/main.034dfcc6b83fdd218994.hot-update.js.map +1 -0
- package/dist/main.034dfcc6b83fdd218994.hot-update.json +1 -0
- package/dist/main.0c0a98b3b3e1e004beb2.hot-update.js +265 -0
- package/dist/main.0c0a98b3b3e1e004beb2.hot-update.js.map +1 -0
- package/dist/main.0c0a98b3b3e1e004beb2.hot-update.json +1 -0
- package/dist/main.13daf8cac0f0a3231f6a.hot-update.js +190 -0
- package/dist/main.13daf8cac0f0a3231f6a.hot-update.js.map +1 -0
- package/dist/main.13daf8cac0f0a3231f6a.hot-update.json +1 -0
- package/dist/main.14e055541f09be93a122.hot-update.js +321 -0
- package/dist/main.14e055541f09be93a122.hot-update.js.map +1 -0
- package/dist/main.14e055541f09be93a122.hot-update.json +1 -0
- package/dist/main.26499c88c63113e7a3f8.hot-update.js +190 -0
- package/dist/main.26499c88c63113e7a3f8.hot-update.js.map +1 -0
- package/dist/main.26499c88c63113e7a3f8.hot-update.json +1 -0
- package/dist/main.2c3a3f1928ea25c4b79a.hot-update.js +11 -0
- package/dist/main.2c3a3f1928ea25c4b79a.hot-update.js.map +1 -0
- package/dist/main.2c3a3f1928ea25c4b79a.hot-update.json +1 -0
- package/dist/main.324b63996b3b36daf5af.hot-update.js +603 -0
- package/dist/main.324b63996b3b36daf5af.hot-update.js.map +1 -0
- package/dist/main.324b63996b3b36daf5af.hot-update.json +1 -0
- package/dist/main.4b0f7799388b5de9908d.hot-update.js +265 -0
- package/dist/main.4b0f7799388b5de9908d.hot-update.js.map +1 -0
- package/dist/main.4b0f7799388b5de9908d.hot-update.json +1 -0
- package/dist/main.6424291443afea7eb7ac.hot-update.js +187 -0
- package/dist/main.6424291443afea7eb7ac.hot-update.js.map +1 -0
- package/dist/main.6424291443afea7eb7ac.hot-update.json +1 -0
- package/dist/main.69127e32a0c76b421f15.hot-update.js +392 -0
- package/dist/main.69127e32a0c76b421f15.hot-update.js.map +1 -0
- package/dist/main.69127e32a0c76b421f15.hot-update.json +1 -0
- package/dist/main.755d91e1a6c7e6bd6702.hot-update.js +265 -0
- package/dist/main.755d91e1a6c7e6bd6702.hot-update.js.map +1 -0
- package/dist/main.755d91e1a6c7e6bd6702.hot-update.json +1 -0
- package/dist/main.75a666c9d30baa58c5ec.hot-update.js +265 -0
- package/dist/main.75a666c9d30baa58c5ec.hot-update.js.map +1 -0
- package/dist/main.75a666c9d30baa58c5ec.hot-update.json +1 -0
- package/dist/main.828f4c348446c6c3640d.hot-update.js +11 -0
- package/dist/main.828f4c348446c6c3640d.hot-update.js.map +1 -0
- package/dist/main.828f4c348446c6c3640d.hot-update.json +1 -0
- package/dist/main.8459249b2915a8823bd3.hot-update.js +321 -0
- package/dist/main.8459249b2915a8823bd3.hot-update.js.map +1 -0
- package/dist/main.8459249b2915a8823bd3.hot-update.json +1 -0
- package/dist/main.87f757c2602cac7001cb.hot-update.js +382 -0
- package/dist/main.87f757c2602cac7001cb.hot-update.js.map +1 -0
- package/dist/main.87f757c2602cac7001cb.hot-update.json +1 -0
- package/dist/main.8aa248086ab27072c348.hot-update.js +265 -0
- package/dist/main.8aa248086ab27072c348.hot-update.js.map +1 -0
- package/dist/main.8aa248086ab27072c348.hot-update.json +1 -0
- package/dist/main.8ce461bf769f3943b7ed.hot-update.js +265 -0
- package/dist/main.8ce461bf769f3943b7ed.hot-update.js.map +1 -0
- package/dist/main.8ce461bf769f3943b7ed.hot-update.json +1 -0
- package/dist/main.9fdbde5beb7c70940250.hot-update.js +392 -0
- package/dist/main.9fdbde5beb7c70940250.hot-update.js.map +1 -0
- package/dist/main.9fdbde5beb7c70940250.hot-update.json +1 -0
- package/dist/main.af59faaae348c919e033.hot-update.js +11 -0
- package/dist/main.af59faaae348c919e033.hot-update.js.map +1 -0
- package/dist/main.af59faaae348c919e033.hot-update.json +1 -0
- package/dist/main.b11900976593b0e609ee.hot-update.js +187 -0
- package/dist/main.b11900976593b0e609ee.hot-update.js.map +1 -0
- package/dist/main.b11900976593b0e609ee.hot-update.json +1 -0
- package/dist/main.b57518d376449c846971.hot-update.js +265 -0
- package/dist/main.b57518d376449c846971.hot-update.js.map +1 -0
- package/dist/main.b57518d376449c846971.hot-update.json +1 -0
- package/dist/main.b5fa8b91f66c4a67740b.hot-update.js +265 -0
- package/dist/main.b5fa8b91f66c4a67740b.hot-update.js.map +1 -0
- package/dist/main.b5fa8b91f66c4a67740b.hot-update.json +1 -0
- package/dist/main.bdb9e6ab1d13cf4250e8.hot-update.js +265 -0
- package/dist/main.bdb9e6ab1d13cf4250e8.hot-update.js.map +1 -0
- package/dist/main.bdb9e6ab1d13cf4250e8.hot-update.json +1 -0
- package/dist/main.c9b051fce9f983306d51.hot-update.js +265 -0
- package/dist/main.c9b051fce9f983306d51.hot-update.js.map +1 -0
- package/dist/main.c9b051fce9f983306d51.hot-update.json +1 -0
- package/dist/main.d03d8e0bfcbd86932c58.hot-update.js +638 -0
- package/dist/main.d03d8e0bfcbd86932c58.hot-update.js.map +1 -0
- package/dist/main.d03d8e0bfcbd86932c58.hot-update.json +1 -0
- package/dist/main.d3b74a9a35325f35373d.hot-update.js +265 -0
- package/dist/main.d3b74a9a35325f35373d.hot-update.js.map +1 -0
- package/dist/main.d3b74a9a35325f35373d.hot-update.json +1 -0
- package/dist/main.d61b231278a9c69df2f8.hot-update.js +265 -0
- package/dist/main.d61b231278a9c69df2f8.hot-update.js.map +1 -0
- package/dist/main.d61b231278a9c69df2f8.hot-update.json +1 -0
- package/dist/pretty-chitty.js +243813 -0
- package/dist/pretty-chitty.js.map +1 -0
- package/package.json +1 -1
- package/src/library/components/ActionLogDisplay.tsx +1 -1
- package/src/library/components/ActionLogSidebar.tsx +1 -1
- package/src/library/components/BottomBar.tsx +2 -0
- package/src/library/components/BottomBarButton.tsx +3 -3
- package/src/library/components/MatchViewer.tsx +23 -9
- package/src/library/components/Panel/MultiPanel.tsx +7 -6
- package/src/library/components/Panel/PanelContents.tsx +4 -2
- package/src/library/components/Panel/SinglePanel.tsx +7 -6
- package/src/library/components/SettingsDisplay.tsx +152 -90
- package/src/library/components/TopBar.tsx +2 -0
- package/src/library/components/TopBarPlayers.tsx +4 -2
- package/src/library/game/Chit.ts +3 -2
- package/src/library/game/DisplaySettings.test.ts +80 -0
- package/src/library/game/DisplaySettings.ts +153 -0
- package/src/library/game/GameButton.ts +2 -0
- package/src/library/game/GameTheme.ts +10 -0
- package/src/library/game/Pick.ts +3 -2
- package/src/library/game/PlayerPromptStatusChit.ts +2 -0
- package/src/library/game/SparkChit.ts +2 -0
- package/src/library/game/typeName.test.ts +51 -0
- package/src/library/game/typeName.ts +40 -0
- package/src/library/hooks/useDisplaySettings.tsx +58 -0
- package/src/library/index.ts +1 -0
- package/src/library/utilities/CanvasStack/CanvasStack.ts +40 -3
- package/src/library/hooks/usePanelScale.tsx +0 -23
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { GameTheme } from "./GameTheme";
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_DISPLAY_SETTINGS,
|
|
5
|
+
allAtStep,
|
|
6
|
+
animationSpeedStep,
|
|
7
|
+
loadDisplaySettings,
|
|
8
|
+
overallStep,
|
|
9
|
+
scaledTheme,
|
|
10
|
+
sectionMultiplier,
|
|
11
|
+
} from "./DisplaySettings";
|
|
12
|
+
|
|
13
|
+
function storage(values: Record<string, string>) {
|
|
14
|
+
return { getItem: (key: string) => values[key] ?? null };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("DisplaySettings", () => {
|
|
18
|
+
it("leaves the theme alone at normal size", () => {
|
|
19
|
+
const theme = new GameTheme();
|
|
20
|
+
for (const section of ["panelPadding", "topBar", "actionLog", "bottomBar"] as const) {
|
|
21
|
+
expect(scaledTheme(theme, section, DEFAULT_DISPLAY_SETTINGS)).toBe(theme);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("scales only the section's own sizes, from the game's theme", () => {
|
|
26
|
+
const theme = GameTheme.withDefaults("#224466", "#ff8800");
|
|
27
|
+
theme.topBarHeight = 50;
|
|
28
|
+
theme.fontFamily = "Serif";
|
|
29
|
+
const bigger = allAtStep(4);
|
|
30
|
+
|
|
31
|
+
const top = scaledTheme(theme, "topBar", bigger);
|
|
32
|
+
expect(top).toBeInstanceOf(GameTheme);
|
|
33
|
+
expect(top.topBarHeight).toBeCloseTo(70);
|
|
34
|
+
expect(top.bottomBarHeight).toBe(theme.bottomBarHeight);
|
|
35
|
+
expect(top.barColor).toBe(theme.barColor);
|
|
36
|
+
expect(top.fontFamily).toBe("Serif");
|
|
37
|
+
expect(theme.topBarHeight).toBe(50);
|
|
38
|
+
|
|
39
|
+
const bottom = scaledTheme(theme, "bottomBar", allAtStep(0));
|
|
40
|
+
expect(bottom.bottomBarHeight).toBeLessThan(theme.bottomBarHeight);
|
|
41
|
+
expect(bottom.bottomBarIconSize).toBeLessThan(theme.bottomBarIconSize);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("scales the panel gap without touching spacing used inside panels", () => {
|
|
45
|
+
const theme = new GameTheme();
|
|
46
|
+
const tight = scaledTheme(theme, "panelPadding", allAtStep(0));
|
|
47
|
+
expect(tight.getPanelSpacing()).toBeLessThan(theme.spacing);
|
|
48
|
+
expect(tight.spacing).toBe(theme.spacing);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("fits fewer panels as the layout gets bigger", () => {
|
|
52
|
+
expect(sectionMultiplier("panelLayout", allAtStep(0))).toBeGreaterThan(1);
|
|
53
|
+
expect(sectionMultiplier("panelLayout", allAtStep(4))).toBeLessThan(1);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("knows when every section is at the same step", () => {
|
|
57
|
+
expect(overallStep(allAtStep(3))).toBe(3);
|
|
58
|
+
expect(overallStep({ ...allAtStep(3), topBar: 1 })).toBeUndefined();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("loads saved settings, clamping bad values", () => {
|
|
62
|
+
const loaded = loadDisplaySettings(storage({ displaySettings: JSON.stringify({ topBar: 9, actionLog: 1 }) }));
|
|
63
|
+
expect(loaded).toEqual({ ...DEFAULT_DISPLAY_SETTINGS, topBar: 4, actionLog: 1 });
|
|
64
|
+
expect(loadDisplaySettings(storage({ displaySettings: "not json" }))).toEqual(DEFAULT_DISPLAY_SETTINGS);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("carries over the old free-form panel scale", () => {
|
|
68
|
+
expect(loadDisplaySettings(storage({ panelScale: "1" })).panelLayout).toBe(2);
|
|
69
|
+
expect(loadDisplaySettings(storage({ panelScale: "3" })).panelLayout).toBe(0);
|
|
70
|
+
expect(loadDisplaySettings(storage({ panelScale: "0.5" })).panelLayout).toBe(4);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("maps animation duration multipliers to the nearest speed", () => {
|
|
74
|
+
expect(animationSpeedStep(1)).toBe(1);
|
|
75
|
+
expect(animationSpeedStep(1 / 8)).toBe(4);
|
|
76
|
+
expect(animationSpeedStep(1 / 4)).toBe(3);
|
|
77
|
+
expect(animationSpeedStep(1 / 2)).toBe(2);
|
|
78
|
+
expect(animationSpeedStep(4)).toBe(0);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { GameTheme } from "./GameTheme";
|
|
2
|
+
|
|
3
|
+
/** The parts of the match screen a player can size on their own */
|
|
4
|
+
export type DisplaySection = "panelLayout" | "panelPadding" | "topBar" | "actionLog" | "bottomBar";
|
|
5
|
+
|
|
6
|
+
/** A step per section: 0 (smallest) to 4 (biggest), 2 being the game's own theme */
|
|
7
|
+
export type DisplaySettings = Record<DisplaySection, number>;
|
|
8
|
+
|
|
9
|
+
export const DISPLAY_STEP_COUNT = 5;
|
|
10
|
+
export const NORMAL_STEP = 2;
|
|
11
|
+
export const DISPLAY_STEP_LABELS = ["Smallest", "Smaller", "Normal", "Bigger", "Biggest"];
|
|
12
|
+
|
|
13
|
+
export const DISPLAY_SECTIONS: { key: DisplaySection; label: string; smallerHint: string; biggerHint: string }[] = [
|
|
14
|
+
{ key: "panelLayout", label: "Panel Layout", smallerHint: "More panels", biggerHint: "Bigger panels" },
|
|
15
|
+
{ key: "panelPadding", label: "Panel Padding", smallerHint: "Tighter", biggerHint: "Roomier" },
|
|
16
|
+
{ key: "topBar", label: "Top Bar", smallerHint: "Smaller", biggerHint: "Bigger" },
|
|
17
|
+
{ key: "actionLog", label: "Action Log", smallerHint: "Smaller", biggerHint: "Bigger" },
|
|
18
|
+
{ key: "bottomBar", label: "Bottom Bar", smallerHint: "Smaller", biggerHint: "Bigger" },
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What each step multiplies the theme's sizes by. Panel layout is the odd one out: it is the scale
|
|
23
|
+
* the layout is worked out at, where a higher scale fits more (smaller) panels on screen, so it
|
|
24
|
+
* runs downwards as the panels get bigger.
|
|
25
|
+
*/
|
|
26
|
+
const MULTIPLIERS: Record<DisplaySection, number[]> = {
|
|
27
|
+
panelLayout: [1.8, 1.35, 1, 0.8, 0.65],
|
|
28
|
+
panelPadding: [0.25, 0.5, 1, 1.75, 2.5],
|
|
29
|
+
topBar: [0.75, 0.875, 1, 1.2, 1.4],
|
|
30
|
+
actionLog: [0.8, 0.9, 1, 1.2, 1.4],
|
|
31
|
+
bottomBar: [0.7, 0.85, 1, 1.15, 1.3],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Animation playback speeds, slowest to fastest: one step below normal (the default) and three
|
|
36
|
+
* doublings above it, up to 8x.
|
|
37
|
+
*/
|
|
38
|
+
export const ANIMATION_SPEEDS = [0.5, 1, 2, 4, 8];
|
|
39
|
+
|
|
40
|
+
export const DEFAULT_DISPLAY_SETTINGS: DisplaySettings = {
|
|
41
|
+
panelLayout: NORMAL_STEP,
|
|
42
|
+
panelPadding: NORMAL_STEP,
|
|
43
|
+
topBar: NORMAL_STEP,
|
|
44
|
+
actionLog: NORMAL_STEP,
|
|
45
|
+
bottomBar: NORMAL_STEP,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const STORAGE_KEY = "displaySettings";
|
|
49
|
+
/** The free-form panel scale that came before these settings */
|
|
50
|
+
const LEGACY_PANEL_SCALE_KEY = "panelScale";
|
|
51
|
+
|
|
52
|
+
function clampStep(step: unknown): number {
|
|
53
|
+
const n = typeof step === "number" && Number.isFinite(step) ? Math.round(step) : NORMAL_STEP;
|
|
54
|
+
return Math.max(0, Math.min(DISPLAY_STEP_COUNT - 1, n));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The index of the value in `values` nearest to `value`, measured as a ratio (they are all scales) */
|
|
58
|
+
function nearestIndex(values: number[], value: number): number {
|
|
59
|
+
let best = 0;
|
|
60
|
+
for (let i = 1; i < values.length; i++) {
|
|
61
|
+
if (Math.abs(Math.log(values[i] / value)) < Math.abs(Math.log(values[best] / value))) {
|
|
62
|
+
best = i;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return best;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function sectionMultiplier(section: DisplaySection, settings: DisplaySettings): number {
|
|
69
|
+
return MULTIPLIERS[section][clampStep(settings[section])];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The animation speed step nearest to a duration multiplier (1 / speed), as `ClientTimeState` keeps it */
|
|
73
|
+
export function animationSpeedStep(durationMultiplier: number): number {
|
|
74
|
+
return nearestIndex(ANIMATION_SPEEDS, 1 / (durationMultiplier || 1));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The step every section shares, or undefined when they have been set individually */
|
|
78
|
+
export function overallStep(settings: DisplaySettings): number | undefined {
|
|
79
|
+
const steps = DISPLAY_SECTIONS.map((s) => settings[s.key]);
|
|
80
|
+
return steps.every((s) => s === steps[0]) ? steps[0] : undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function allAtStep(step: number): DisplaySettings {
|
|
84
|
+
const result = { ...DEFAULT_DISPLAY_SETTINGS };
|
|
85
|
+
for (const { key } of DISPLAY_SECTIONS) {
|
|
86
|
+
result[key] = clampStep(step);
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function loadDisplaySettings(storage: Pick<Storage, "getItem"> | undefined): DisplaySettings {
|
|
92
|
+
const result = { ...DEFAULT_DISPLAY_SETTINGS };
|
|
93
|
+
try {
|
|
94
|
+
const raw = storage?.getItem(STORAGE_KEY);
|
|
95
|
+
if (raw) {
|
|
96
|
+
const parsed = JSON.parse(raw);
|
|
97
|
+
for (const { key } of DISPLAY_SECTIONS) {
|
|
98
|
+
result[key] = clampStep(parsed?.[key]);
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
const legacyScale = parseFloat(storage?.getItem(LEGACY_PANEL_SCALE_KEY) ?? "");
|
|
103
|
+
if (legacyScale > 0) {
|
|
104
|
+
result.panelLayout = nearestIndex(MULTIPLIERS.panelLayout, legacyScale);
|
|
105
|
+
}
|
|
106
|
+
} catch {
|
|
107
|
+
// unreadable storage: everything stays at the game's own sizes
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function saveDisplaySettings(storage: Pick<Storage, "setItem"> | undefined, settings: DisplaySettings) {
|
|
113
|
+
try {
|
|
114
|
+
storage?.setItem(STORAGE_KEY, JSON.stringify(settings));
|
|
115
|
+
} catch {
|
|
116
|
+
// storage unavailable: the setting lasts until the page closes
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A copy of `theme` with the sizes that belong to `section` multiplied by the player's setting for
|
|
122
|
+
* it. Returns `theme` itself when the section is at its normal size.
|
|
123
|
+
*/
|
|
124
|
+
export function scaledTheme(theme: GameTheme, section: DisplaySection, settings: DisplaySettings): GameTheme {
|
|
125
|
+
const m = sectionMultiplier(section, settings);
|
|
126
|
+
if (m === 1 || section === "panelLayout") {
|
|
127
|
+
return theme;
|
|
128
|
+
}
|
|
129
|
+
// Copy the fields onto the same prototype, so getters (fontFamily) and methods still work and
|
|
130
|
+
// no setter runs
|
|
131
|
+
const result: GameTheme = Object.assign(Object.create(Object.getPrototypeOf(theme)), theme);
|
|
132
|
+
switch (section) {
|
|
133
|
+
case "panelPadding":
|
|
134
|
+
result.panelSpacing = theme.getPanelSpacing() * m;
|
|
135
|
+
break;
|
|
136
|
+
case "topBar":
|
|
137
|
+
result.topBarHeight = theme.topBarHeight * m;
|
|
138
|
+
result.fontScalar = theme.fontScalar * m;
|
|
139
|
+
result.spacing = theme.spacing * m;
|
|
140
|
+
break;
|
|
141
|
+
case "actionLog":
|
|
142
|
+
result.fontScalar = theme.fontScalar * m;
|
|
143
|
+
result.actionLogSidebarWidth = theme.actionLogSidebarWidth * m;
|
|
144
|
+
break;
|
|
145
|
+
case "bottomBar":
|
|
146
|
+
result.bottomBarHeight = theme.bottomBarHeight * m;
|
|
147
|
+
result.bottomBarFontScalar = theme.bottomBarFontScalar * m;
|
|
148
|
+
result.bottomBarIconSize = theme.bottomBarIconSize * m;
|
|
149
|
+
result.spacing = theme.spacing * m;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
@@ -7,6 +7,7 @@ import { GalleryItemSource } from "../components/GalleryViewer";
|
|
|
7
7
|
import { GalleryItemChitChildrenSource } from "./GalleryItemChitChildrenSource";
|
|
8
8
|
import { GalleryItemRawSource } from "./GalleryItemRawSource";
|
|
9
9
|
import { chitsToGalleryItems } from "../utilities/GalleryItemConversion";
|
|
10
|
+
import { setTypeName } from "./typeName";
|
|
10
11
|
|
|
11
12
|
export type ButtonCallback = () => void | Promise<void>;
|
|
12
13
|
|
|
@@ -140,3 +141,4 @@ export class Confirm extends GameButton {
|
|
|
140
141
|
/** @internal */
|
|
141
142
|
canAutoResolve = false;
|
|
142
143
|
}
|
|
144
|
+
setTypeName(Confirm, "Confirm");
|
|
@@ -32,7 +32,11 @@ export class GameTheme {
|
|
|
32
32
|
public backgroundColor = "#0f0119";
|
|
33
33
|
|
|
34
34
|
public spacing = 8;
|
|
35
|
+
/** The gap around each panel; defaults to `spacing` */
|
|
36
|
+
public panelSpacing?: number;
|
|
35
37
|
public bottomBarHeight = 80;
|
|
38
|
+
/** The size of the icons on the bottom bar's buttons */
|
|
39
|
+
public bottomBarIconSize = 30;
|
|
36
40
|
public topBarHeight = 40;
|
|
37
41
|
public barColor = "#1c092c";
|
|
38
42
|
public barGradientPercent = 0.05;
|
|
@@ -100,6 +104,8 @@ export class GameTheme {
|
|
|
100
104
|
public actionLogTextColor = "#ffffff";
|
|
101
105
|
public actionBarWidth = 600;
|
|
102
106
|
public actionBarLinesToShow = 2;
|
|
107
|
+
/** The width of the action log shown beside the game on large screens */
|
|
108
|
+
public actionLogSidebarWidth = 350;
|
|
103
109
|
public inlineGalleryBackgroundColor = "rgba(255,255,255,0.1)";
|
|
104
110
|
public inlineGalleryButtonBackgroundColor = "#000000";
|
|
105
111
|
public inlineGalleryButtonForegroundColor = "rgba(255,255,255,0.5)";
|
|
@@ -142,6 +148,10 @@ export class GameTheme {
|
|
|
142
148
|
return result;
|
|
143
149
|
}
|
|
144
150
|
|
|
151
|
+
getPanelSpacing(): number {
|
|
152
|
+
return this.panelSpacing ?? this.spacing;
|
|
153
|
+
}
|
|
154
|
+
|
|
145
155
|
layoutSize(width: number): "large" | "medium" | "mobile" {
|
|
146
156
|
return width >= this.actionBarWidth * 2 ? "large" : width >= this.actionBarWidth * 1.3 ? "medium" : "mobile";
|
|
147
157
|
}
|
package/src/library/game/Pick.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { GalleryItemChitChildrenSource } from "./GalleryItemChitChildrenSource";
|
|
|
3
3
|
import { IButtonLibrary } from "./Game";
|
|
4
4
|
import { Confirm, GameButton, ToggleGalleryButton } from "./GameButton";
|
|
5
5
|
import { OrderedOutlet } from "./OrderedOutlet";
|
|
6
|
+
import { typeNameOfInstance } from "./typeName";
|
|
6
7
|
import { PickPrompt } from "./Prompt";
|
|
7
8
|
import { MismatchError, Turn } from "./Turn";
|
|
8
9
|
|
|
@@ -187,7 +188,7 @@ export class ChitPick<T extends Chit> extends Pick {
|
|
|
187
188
|
|
|
188
189
|
if (this.button) {
|
|
189
190
|
result.b = this.button.serialize();
|
|
190
|
-
result.b.__buttonType =
|
|
191
|
+
result.b.__buttonType = typeNameOfInstance(this.button);
|
|
191
192
|
}
|
|
192
193
|
|
|
193
194
|
return result;
|
|
@@ -436,7 +437,7 @@ export class ButtonPick extends Pick {
|
|
|
436
437
|
}
|
|
437
438
|
|
|
438
439
|
const details = this.button.serialize();
|
|
439
|
-
details.__buttonType =
|
|
440
|
+
details.__buttonType = typeNameOfInstance(this.button);
|
|
440
441
|
details.__f = this.focusChits.map((chit) => chit.id);
|
|
441
442
|
return details;
|
|
442
443
|
}
|
|
@@ -3,6 +3,7 @@ import { EventChannel } from "../utilities/EventChannel";
|
|
|
3
3
|
import { Chit } from "./Chit";
|
|
4
4
|
import { Prompt } from "./Prompt";
|
|
5
5
|
import StaticChitTypeRegistry from "./StaticChitTypeRegistry";
|
|
6
|
+
import { setTypeName } from "./typeName";
|
|
6
7
|
|
|
7
8
|
export class PlayerPromptStatusChit extends Chit {
|
|
8
9
|
/** Plain text of the prompt the player is currently being asked to answer (`:token:` markup resolved). @internal */
|
|
@@ -21,4 +22,5 @@ export class PlayerPromptStatusChit extends Chit {
|
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
24
|
|
|
25
|
+
setTypeName(PlayerPromptStatusChit, "PlayerPromptStatusChit");
|
|
24
26
|
StaticChitTypeRegistry["PlayerPromptStatusChit"] = PlayerPromptStatusChit;
|
|
@@ -4,6 +4,7 @@ import { Vector2 } from "three";
|
|
|
4
4
|
import { NonEditable } from "../utilities/Annotations";
|
|
5
5
|
import { PlayerChit } from "./PlayerChit";
|
|
6
6
|
import StaticChitTypeRegistry from "./StaticChitTypeRegistry";
|
|
7
|
+
import { setTypeName } from "./typeName";
|
|
7
8
|
import { IUpdatingCanvas } from "../utilities/IUpdatingCanvas";
|
|
8
9
|
|
|
9
10
|
export class SparkChit extends Chit {
|
|
@@ -82,6 +83,7 @@ export class SparkChit extends Chit {
|
|
|
82
83
|
}
|
|
83
84
|
}
|
|
84
85
|
|
|
86
|
+
setTypeName(SparkChit, "SparkChit");
|
|
85
87
|
StaticChitTypeRegistry["SparkChit"] = SparkChit;
|
|
86
88
|
|
|
87
89
|
export abstract class BagSparkChit<T extends Chit> extends SparkChit {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { Chit } from "./Chit";
|
|
4
|
+
import { Game } from "./Game";
|
|
5
|
+
import { Confirm } from "./GameButton";
|
|
6
|
+
import { SparkChit } from "./SparkChit";
|
|
7
|
+
import { setTypeName, typeNameOf, typeNameOfInstance } from "./typeName";
|
|
8
|
+
|
|
9
|
+
/** Simulate a bundler renaming a class (`Card` → `Card2`). */
|
|
10
|
+
function renamed<T extends abstract new () => unknown>(ctor: T, name: string): T {
|
|
11
|
+
Object.defineProperty(ctor, "name", { value: name, configurable: true });
|
|
12
|
+
return ctor;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe("typeName", () => {
|
|
16
|
+
test("falls back to constructor.name", () => {
|
|
17
|
+
class Plain extends Chit {}
|
|
18
|
+
expect(typeNameOf(Plain)).toBe("Plain");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("pinned name wins over a renamed constructor", () => {
|
|
22
|
+
const Card = renamed(class Card extends Chit {}, "Card2");
|
|
23
|
+
setTypeName(Card, "Card");
|
|
24
|
+
expect(typeNameOf(Card)).toBe("Card");
|
|
25
|
+
expect(typeNameOfInstance(new Card())).toBe("Card");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("subclasses do not inherit a parent's pinned name", () => {
|
|
29
|
+
class Card extends Chit {}
|
|
30
|
+
setTypeName(Card, "Card");
|
|
31
|
+
class SpecialCard extends Card {}
|
|
32
|
+
expect(typeNameOf(SpecialCard)).toBe("SpecialCard");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("engine built-ins are pinned", () => {
|
|
36
|
+
expect(typeNameOf(SparkChit)).toBe("SparkChit");
|
|
37
|
+
expect(typeNameOf(Confirm)).toBe("Confirm");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("serialize/deflate round-trips through a renamed class", () => {
|
|
41
|
+
const Card = renamed(class Card extends Chit {}, "Card2");
|
|
42
|
+
setTypeName(Card, "Card");
|
|
43
|
+
const game = { chitLibrary: { Card } } as unknown as Game<any, any>;
|
|
44
|
+
|
|
45
|
+
const card = new Card();
|
|
46
|
+
card.id = "c1";
|
|
47
|
+
const serialized = card.serialize();
|
|
48
|
+
expect(JSON.parse(serialized).__chitType).toBe("Card");
|
|
49
|
+
expect(Chit.deflate(serialized, game)).toBeInstanceOf(Card);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Static property holding a class's serialized type name (`__chitType` / `__buttonType`).
|
|
3
|
+
*
|
|
4
|
+
* Bundlers may rename classes (minification, or `Card` → `Card2` to avoid collisions), so
|
|
5
|
+
* `constructor.name` is not stable across builds. Setting this on a class pins the name that
|
|
6
|
+
* gets written to saved state, and it must match that class's key in `chitLibrary` / `buttonLibrary`.
|
|
7
|
+
*/
|
|
8
|
+
export const TYPE_NAME_KEY = "__typeName";
|
|
9
|
+
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
type AnyClass = abstract new (...args: any[]) => unknown;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Pin the serialized type name for a chit or button class.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* setTypeName(Card, "Card");
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function setTypeName(ctor: AnyClass, name: string) {
|
|
21
|
+
Object.defineProperty(ctor, TYPE_NAME_KEY, { value: name, writable: true, configurable: true, enumerable: false });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The serialized type name for a class: its own pinned `__typeName`, else `constructor.name`.
|
|
26
|
+
*
|
|
27
|
+
* Only an *own* property counts. Statics are inherited, so a subclass that wasn't stamped
|
|
28
|
+
* must not report its parent's name.
|
|
29
|
+
*/
|
|
30
|
+
export function typeNameOf(ctor: AnyClass): string {
|
|
31
|
+
if (Object.prototype.hasOwnProperty.call(ctor, TYPE_NAME_KEY)) {
|
|
32
|
+
return (ctor as any)[TYPE_NAME_KEY];
|
|
33
|
+
}
|
|
34
|
+
return ctor.name;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** `typeNameOf` for an instance. */
|
|
38
|
+
export function typeNameOfInstance(instance: object): string {
|
|
39
|
+
return typeNameOf(Object.getPrototypeOf(instance).constructor);
|
|
40
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import React, { useContext, createContext, ReactNode, useState, useMemo, useCallback } from "react";
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_DISPLAY_SETTINGS,
|
|
4
|
+
DisplaySection,
|
|
5
|
+
DisplaySettings,
|
|
6
|
+
loadDisplaySettings,
|
|
7
|
+
saveDisplaySettings,
|
|
8
|
+
scaledTheme,
|
|
9
|
+
sectionMultiplier,
|
|
10
|
+
} from "../game/DisplaySettings";
|
|
11
|
+
import { GameThemeProvider, useGameTheme } from "./useGameTheme";
|
|
12
|
+
|
|
13
|
+
function browserStorage(): Storage | undefined {
|
|
14
|
+
try {
|
|
15
|
+
return typeof localStorage === "undefined" ? undefined : localStorage;
|
|
16
|
+
} catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const DisplaySettingsContext = createContext<{
|
|
22
|
+
settings: DisplaySettings;
|
|
23
|
+
setSettings: (settings: DisplaySettings) => void;
|
|
24
|
+
}>({
|
|
25
|
+
settings: DEFAULT_DISPLAY_SETTINGS,
|
|
26
|
+
setSettings: () => {},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export function useDisplaySettings(): [DisplaySettings, (settings: DisplaySettings) => void] {
|
|
30
|
+
const { settings, setSettings } = useContext(DisplaySettingsContext);
|
|
31
|
+
return [settings, setSettings];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The scale the panel layout is worked out at: above 1 fits more panels on screen, below 1 fewer */
|
|
35
|
+
export function usePanelScale(): number {
|
|
36
|
+
return sectionMultiplier("panelLayout", useContext(DisplaySettingsContext).settings);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function DisplaySettingsProvider({ children }: { children: ReactNode }) {
|
|
40
|
+
// Not per game: a player's screen and eyesight are the same whatever they play
|
|
41
|
+
const [settings, setSettingsState] = useState<DisplaySettings>(() => loadDisplaySettings(browserStorage()));
|
|
42
|
+
const setSettings = useCallback((next: DisplaySettings) => {
|
|
43
|
+
saveDisplaySettings(browserStorage(), next);
|
|
44
|
+
setSettingsState(next);
|
|
45
|
+
}, []);
|
|
46
|
+
const value = useMemo(() => ({ settings, setSettings }), [settings, setSettings]);
|
|
47
|
+
return <DisplaySettingsContext.Provider value={value}>{children}</DisplaySettingsContext.Provider>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Gives everything inside the game theme sized by the player's setting for `section` */
|
|
51
|
+
export function DisplaySectionTheme({ section, children }: { section: DisplaySection; children: ReactNode }) {
|
|
52
|
+
const theme = useGameTheme();
|
|
53
|
+
const { settings } = useContext(DisplaySettingsContext);
|
|
54
|
+
const sectionStep = settings[section];
|
|
55
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
56
|
+
const scaled = useMemo(() => scaledTheme(theme, section, settings), [theme, section, sectionStep]);
|
|
57
|
+
return <GameThemeProvider theme={scaled}>{children}</GameThemeProvider>;
|
|
58
|
+
}
|
package/src/library/index.ts
CHANGED
|
@@ -6,6 +6,7 @@ export type { GameMetaData } from "./game/GameMetaData";
|
|
|
6
6
|
// NOTE: must be before Turn! or circular dependency!
|
|
7
7
|
/** @group Chits */
|
|
8
8
|
export { Chit } from "./game/Chit";
|
|
9
|
+
export { setTypeName, typeNameOf } from "./game/typeName";
|
|
9
10
|
|
|
10
11
|
/** @group Core Game Elements */
|
|
11
12
|
export { Turn } from "./game/Turn";
|
|
@@ -159,22 +159,59 @@ export class CanvasStack implements IUpdatingCanvas {
|
|
|
159
159
|
private static _pendingFontRedraws = new Set<CanvasStack>();
|
|
160
160
|
private static _fontRedrawScheduled = false;
|
|
161
161
|
|
|
162
|
+
// Records the font as it was *set*, not as ctx.font reads back: WebKit's getter drops the weight ("700 27px X"
|
|
163
|
+
// reads "27px X"), so tracking the read-back only ever checked the regular face. Once that had loaded, bold text
|
|
164
|
+
// drawn in the fallback while its own face was still downloading looked loaded and never redrew (the ghosted
|
|
165
|
+
// titles and re-wrapped bodies on iPhone). save/restore carry the set value the way the context carries its font.
|
|
162
166
|
private _trackFonts(ctx: CanvasRenderingContext2D) {
|
|
167
|
+
const fontProp = Object.getOwnPropertyDescriptor(CanvasRenderingContext2D.prototype, "font");
|
|
168
|
+
let current = ctx.font;
|
|
169
|
+
if (fontProp?.get && fontProp.set) {
|
|
170
|
+
const { get, set } = fontProp;
|
|
171
|
+
const saved: string[] = [];
|
|
172
|
+
Object.defineProperty(ctx, "font", {
|
|
173
|
+
configurable: true,
|
|
174
|
+
get: () => get.call(ctx),
|
|
175
|
+
set: (value: string) => {
|
|
176
|
+
set.call(ctx, value);
|
|
177
|
+
current = value;
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
const save = ctx.save;
|
|
181
|
+
const restore = ctx.restore;
|
|
182
|
+
ctx.save = () => {
|
|
183
|
+
saved.push(current);
|
|
184
|
+
save.call(ctx);
|
|
185
|
+
};
|
|
186
|
+
ctx.restore = () => {
|
|
187
|
+
if (saved.length > 0) current = saved.pop()!;
|
|
188
|
+
restore.call(ctx);
|
|
189
|
+
};
|
|
190
|
+
}
|
|
163
191
|
for (const name of ["fillText", "strokeText", "measureText"] as const) {
|
|
164
192
|
const original = ctx[name] as (...args: unknown[]) => unknown;
|
|
165
193
|
(ctx as any)[name] = (...args: unknown[]) => {
|
|
166
|
-
this._fontsUsed.add(
|
|
194
|
+
this._fontsUsed.add(current);
|
|
167
195
|
return original.apply(ctx, args);
|
|
168
196
|
};
|
|
169
197
|
}
|
|
170
198
|
}
|
|
171
199
|
|
|
200
|
+
/** check() throws on a string that isn't a valid font (the context ignored that assignment); treat it as loaded. */
|
|
201
|
+
private static fontLoaded(font: string): boolean {
|
|
202
|
+
try {
|
|
203
|
+
return document.fonts.check(font);
|
|
204
|
+
} catch {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
172
209
|
private _noteUnloadedFonts() {
|
|
173
210
|
this._unloadedFonts.clear();
|
|
174
211
|
const fonts = typeof document !== "undefined" ? document.fonts : undefined;
|
|
175
212
|
if (!fonts) return;
|
|
176
213
|
for (const font of this._fontsUsed) {
|
|
177
|
-
if (
|
|
214
|
+
if (CanvasStack.fontLoaded(font)) continue;
|
|
178
215
|
this._unloadedFonts.add(font);
|
|
179
216
|
CanvasStack.whenFontLoads(font);
|
|
180
217
|
}
|
|
@@ -182,7 +219,7 @@ export class CanvasStack implements IUpdatingCanvas {
|
|
|
182
219
|
|
|
183
220
|
/** A font this stack drew with before it loaded has loaded since: its pixels are stale. */
|
|
184
221
|
private _fontsArrived(): boolean {
|
|
185
|
-
for (const font of this._unloadedFonts) if (
|
|
222
|
+
for (const font of this._unloadedFonts) if (CanvasStack.fontLoaded(font)) return true;
|
|
186
223
|
return false;
|
|
187
224
|
}
|
|
188
225
|
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import React, { useContext, createContext, ReactNode, useState } from "react";
|
|
2
|
-
|
|
3
|
-
const PanelScaleContext = createContext<{ scale: number; setScale: (n: number) => void }>({
|
|
4
|
-
scale: 1,
|
|
5
|
-
setScale: () => {},
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
export function usePanelScale(): number {
|
|
9
|
-
return useContext(PanelScaleContext).scale;
|
|
10
|
-
}
|
|
11
|
-
export function usePanelSetScale(): (n: number) => void {
|
|
12
|
-
return useContext(PanelScaleContext).setScale;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function PanelScaleProvider({ children }: { children: ReactNode }) {
|
|
16
|
-
const initialScale = parseFloat(localStorage["panelScale"] ?? "1") || 1; // should this be game specific?
|
|
17
|
-
const [scale, setScaleState] = useState<number>(initialScale);
|
|
18
|
-
const setScale = (n: number) => {
|
|
19
|
-
localStorage["panelScale"] = n;
|
|
20
|
-
setScaleState(n);
|
|
21
|
-
};
|
|
22
|
-
return <PanelScaleContext.Provider value={{ scale, setScale }}>{children}</PanelScaleContext.Provider>;
|
|
23
|
-
}
|