import { DEFAULT_HAIR_STYLE, DEFAULT_HAIR_HUE, DEFAULT_HAIR_SATURATION, DEFAULT_HAIR_DARKNESS, listHairStyles, normalizeHairSliders, hairColorFromSliders } from "/shared/appearance/hair.js"; import { paintCharacterPreview } from "./characterPreview.js"; /** * Shared hair style + HSL controls with live character preview. */ export function bindHairControls ({ root, resolveSpriteUrl, getSex, getBag = () => null, hideHeadgear = true, onChange = null }) { const preview = root.querySelector("[data-hair-preview]"); const styleSelect = root.querySelector("[data-hair-style]"); const hue = root.querySelector("[data-hair-hue]"); const saturation = root.querySelector("[data-hair-saturation]"); const darkness = root.querySelector("[data-hair-darkness]"); const swatch = root.querySelector("[data-hair-swatch]"); let paintToken = 0; function currentHair () { const style = styleSelect?.value === "none" ? null : (styleSelect?.value || DEFAULT_HAIR_STYLE); const sliders = normalizeHairSliders({ hue: hue?.value, saturation: Number(saturation?.value) / 100, darkness: Number(darkness?.value) / 100 }); return { style, ...sliders, color: hairColorFromSliders(sliders) }; } function fillStyles () { if (!styleSelect) { return; } const sex = getSex?.() === "female" ? "female" : "male"; const selected = styleSelect.value || DEFAULT_HAIR_STYLE; styleSelect.replaceChildren(); const none = document.createElement("option"); none.value = "none"; none.textContent = "Bald"; styleSelect.append(none); for (const style of listHairStyles(sex)) { const option = document.createElement("option"); option.value = style; option.textContent = style.replace("hair-", "Style "); styleSelect.append(option); } if ([...styleSelect.options].some((option) => option.value === selected)) { styleSelect.value = selected; } else { styleSelect.value = DEFAULT_HAIR_STYLE; } } function syncSwatch () { if (!swatch) { return; } const color = hairColorFromSliders(currentHair()); swatch.style.background = `rgb(${Math.round(color.r * 255)}, ${Math.round(color.g * 255)}, ${Math.round(color.b * 255)})`; } async function refreshPreview () { const token = ++paintToken; syncSwatch(); onChange?.(currentHair()); if (!preview) { return; } try { await paintCharacterPreview(preview, { sex: getSex?.() === "female" ? "female" : "male", bag: getBag?.() ?? null, hair: currentHair(), hideHeadgear, resolveSpriteUrl, scale: 4 }); } catch (error) { if (token === paintToken) { console.warn(error); } } } function setHair (hair) { fillStyles(); if (styleSelect) { styleSelect.value = hair?.style ?? DEFAULT_HAIR_STYLE; } const sliders = normalizeHairSliders(hair ?? {}); if (hue) { hue.value = String(Math.round(sliders.hue)); } if (saturation) { saturation.value = String(Math.round(sliders.saturation * 100)); } if (darkness) { darkness.value = String(Math.round(sliders.darkness * 100)); } void refreshPreview(); } fillStyles(); if (hue && !hue.value) { hue.value = String(DEFAULT_HAIR_HUE); } if (saturation && !saturation.value) { saturation.value = String(Math.round(DEFAULT_HAIR_SATURATION * 100)); } if (darkness && !darkness.value) { darkness.value = String(Math.round(DEFAULT_HAIR_DARKNESS * 100)); } for (const el of [styleSelect, hue, saturation, darkness]) { el?.addEventListener("input", () => void refreshPreview()); } return { currentHair, setHair, fillStyles, refreshPreview }; }