BlueGit

timfa questward

Files

questward / client / src / play / hairControls.js

hairControls.js

hairControls.js 3.5 KiB Download Raw
1import {
2 DEFAULT_HAIR_STYLE,
3 DEFAULT_HAIR_HUE,
4 DEFAULT_HAIR_SATURATION,
5 DEFAULT_HAIR_DARKNESS,
6 listHairStyles,
7 normalizeHairSliders,
8 hairColorFromSliders
9} from "/shared/appearance/hair.js";
10import { paintCharacterPreview } from "./characterPreview.js";
11
12/**
13 * Shared hair style + HSL controls with live character preview.
14 */
15export function bindHairControls ({
16 root,
17 resolveSpriteUrl,
18 getSex,
19 getBag = () => null,
20 hideHeadgear = true,
21 onChange = null
22})
23{
24 const preview = root.querySelector("[data-hair-preview]");
25 const styleSelect = root.querySelector("[data-hair-style]");
26 const hue = root.querySelector("[data-hair-hue]");
27 const saturation = root.querySelector("[data-hair-saturation]");
28 const darkness = root.querySelector("[data-hair-darkness]");
29 const swatch = root.querySelector("[data-hair-swatch]");
30 let paintToken = 0;
31
32 function currentHair ()
33 {
34 const style = styleSelect?.value === "none" ? null : (styleSelect?.value || DEFAULT_HAIR_STYLE);
35 const sliders = normalizeHairSliders({
36 hue: hue?.value,
37 saturation: Number(saturation?.value) / 100,
38 darkness: Number(darkness?.value) / 100
39 });
40 return {
41 style,
42 ...sliders,
43 color: hairColorFromSliders(sliders)
44 };
45 }
46
47 function fillStyles ()
48 {
49 if (!styleSelect)
50 { return; }
51 const sex = getSex?.() === "female" ? "female" : "male";
52 const selected = styleSelect.value || DEFAULT_HAIR_STYLE;
53 styleSelect.replaceChildren();
54 const none = document.createElement("option");
55 none.value = "none";
56 none.textContent = "Bald";
57 styleSelect.append(none);
58 for (const style of listHairStyles(sex))
59 {
60 const option = document.createElement("option");
61 option.value = style;
62 option.textContent = style.replace("hair-", "Style ");
63 styleSelect.append(option);
64 }
65 if ([...styleSelect.options].some((option) => option.value === selected))
66 { styleSelect.value = selected; }
67 else
68 { styleSelect.value = DEFAULT_HAIR_STYLE; }
69 }
70
71 function syncSwatch ()
72 {
73 if (!swatch)
74 { return; }
75 const color = hairColorFromSliders(currentHair());
76 swatch.style.background = `rgb(${Math.round(color.r * 255)}, ${Math.round(color.g * 255)}, ${Math.round(color.b * 255)})`;
77 }
78
79 async function refreshPreview ()
80 {
81 const token = ++paintToken;
82 syncSwatch();
83 onChange?.(currentHair());
84 if (!preview)
85 { return; }
86 try
87 {
88 await paintCharacterPreview(preview, {
89 sex: getSex?.() === "female" ? "female" : "male",
90 bag: getBag?.() ?? null,
91 hair: currentHair(),
92 hideHeadgear,
93 resolveSpriteUrl,
94 scale: 4
95 });
96 }
97 catch (error)
98 {
99 if (token === paintToken)
100 { console.warn(error); }
101 }
102 }
103
104 function setHair (hair)
105 {
106 fillStyles();
107 if (styleSelect)
108 { styleSelect.value = hair?.style ?? DEFAULT_HAIR_STYLE; }
109 const sliders = normalizeHairSliders(hair ?? {});
110 if (hue)
111 { hue.value = String(Math.round(sliders.hue)); }
112 if (saturation)
113 { saturation.value = String(Math.round(sliders.saturation * 100)); }
114 if (darkness)
115 { darkness.value = String(Math.round(sliders.darkness * 100)); }
116 void refreshPreview();
117 }
118
119 fillStyles();
120 if (hue && !hue.value)
121 { hue.value = String(DEFAULT_HAIR_HUE); }
122 if (saturation && !saturation.value)
123 { saturation.value = String(Math.round(DEFAULT_HAIR_SATURATION * 100)); }
124 if (darkness && !darkness.value)
125 { darkness.value = String(Math.round(DEFAULT_HAIR_DARKNESS * 100)); }
126
127 for (const el of [styleSelect, hue, saturation, darkness])
128 { el?.addEventListener("input", () => void refreshPreview()); }
129
130 return {
131 currentHair,
132 setHair,
133 fillStyles,
134 refreshPreview
135 };
136}