| 1 | import { |
| 2 | DEFAULT_HAIR_HUE, |
| 3 | DEFAULT_HAIR_SATURATION, |
| 4 | DEFAULT_HAIR_DARKNESS, |
| 5 | normalizeHairSliders, |
| 6 | hairColorFromSliders |
| 7 | } from "/shared/appearance/hair.js"; |
| 8 | import { |
| 9 | CreationLegsKind, |
| 10 | CREATION_LEGS_KINDS, |
| 11 | defaultCreateClothing, |
| 12 | normalizeCreateClothing, |
| 13 | creationClothingPreviewBag |
| 14 | } from "/shared/items/createClothing.js"; |
| 15 | |
| 16 | const LEGS_LABELS = Object.freeze({ |
| 17 | [CreationLegsKind.shorts]: "Shorts", |
| 18 | [CreationLegsKind.skirt]: "Skirt", |
| 19 | [CreationLegsKind.trousers]: "Trousers" |
| 20 | }); |
| 21 | |
| 22 | /** |
| 23 | * Character-creation clothing tab: cotton shirt + legs choice, HSL dyes per piece. |
| 24 | */ |
| 25 | export function bindCreateClothingControls ({ |
| 26 | root, |
| 27 | onChange = null |
| 28 | }) |
| 29 | { |
| 30 | const legsSelect = root.querySelector("[data-create-legs]"); |
| 31 | const shirtHue = root.querySelector("[data-shirt-hue]"); |
| 32 | const shirtSat = root.querySelector("[data-shirt-saturation]"); |
| 33 | const shirtDark = root.querySelector("[data-shirt-darkness]"); |
| 34 | const shirtSwatch = root.querySelector("[data-shirt-swatch]"); |
| 35 | const legsHue = root.querySelector("[data-legs-hue]"); |
| 36 | const legsSat = root.querySelector("[data-legs-saturation]"); |
| 37 | const legsDark = root.querySelector("[data-legs-darkness]"); |
| 38 | const legsSwatch = root.querySelector("[data-legs-swatch]"); |
| 39 | |
| 40 | function fillLegs () |
| 41 | { |
| 42 | if (!legsSelect) |
| 43 | { return; } |
| 44 | const selected = legsSelect.value || CreationLegsKind.trousers; |
| 45 | legsSelect.replaceChildren(); |
| 46 | for (const kind of CREATION_LEGS_KINDS) |
| 47 | { |
| 48 | const option = document.createElement("option"); |
| 49 | option.value = kind; |
| 50 | option.textContent = LEGS_LABELS[kind] ?? kind; |
| 51 | legsSelect.append(option); |
| 52 | } |
| 53 | legsSelect.value = CREATION_LEGS_KINDS.includes(selected) |
| 54 | ? selected |
| 55 | : CreationLegsKind.trousers; |
| 56 | } |
| 57 | |
| 58 | function readSliders (hueEl, satEl, darkEl, fallback) |
| 59 | { |
| 60 | return normalizeHairSliders({ |
| 61 | hue: hueEl?.value ?? fallback.hue, |
| 62 | saturation: Number(satEl?.value ?? fallback.saturation * 100) / 100, |
| 63 | darkness: Number(darkEl?.value ?? fallback.darkness * 100) / 100 |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | function currentClothing () |
| 68 | { |
| 69 | const defaults = defaultCreateClothing(); |
| 70 | return normalizeCreateClothing({ |
| 71 | legsKind: legsSelect?.value ?? defaults.legsKind, |
| 72 | shirt: readSliders(shirtHue, shirtSat, shirtDark, defaults.shirt), |
| 73 | legs: readSliders(legsHue, legsSat, legsDark, defaults.legs) |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | function syncSwatch (el, sliders) |
| 78 | { |
| 79 | if (!el) |
| 80 | { return; } |
| 81 | const color = hairColorFromSliders(sliders); |
| 82 | el.style.background = `rgb(${Math.round(color.r * 255)}, ${Math.round(color.g * 255)}, ${Math.round(color.b * 255)})`; |
| 83 | } |
| 84 | |
| 85 | function applyDefaults () |
| 86 | { |
| 87 | const defaults = defaultCreateClothing(); |
| 88 | fillLegs(); |
| 89 | if (legsSelect) |
| 90 | { legsSelect.value = defaults.legsKind; } |
| 91 | if (shirtHue) |
| 92 | { shirtHue.value = String(Math.round(defaults.shirt.hue)); } |
| 93 | if (shirtSat) |
| 94 | { shirtSat.value = String(Math.round(defaults.shirt.saturation * 100)); } |
| 95 | if (shirtDark) |
| 96 | { shirtDark.value = String(Math.round(defaults.shirt.darkness * 100)); } |
| 97 | if (legsHue) |
| 98 | { legsHue.value = String(Math.round(defaults.legs.hue)); } |
| 99 | if (legsSat) |
| 100 | { legsSat.value = String(Math.round(defaults.legs.saturation * 100)); } |
| 101 | if (legsDark) |
| 102 | { legsDark.value = String(Math.round(defaults.legs.darkness * 100)); } |
| 103 | } |
| 104 | |
| 105 | function refresh () |
| 106 | { |
| 107 | const clothing = currentClothing(); |
| 108 | syncSwatch(shirtSwatch, clothing.shirt); |
| 109 | syncSwatch(legsSwatch, clothing.legs); |
| 110 | onChange?.(clothing); |
| 111 | } |
| 112 | |
| 113 | function previewBag () |
| 114 | { |
| 115 | return creationClothingPreviewBag(currentClothing()); |
| 116 | } |
| 117 | |
| 118 | applyDefaults(); |
| 119 | for (const el of [legsSelect, shirtHue, shirtSat, shirtDark, legsHue, legsSat, legsDark]) |
| 120 | { el?.addEventListener("input", () => refresh()); } |
| 121 | refresh(); |
| 122 | |
| 123 | return { |
| 124 | currentClothing, |
| 125 | previewBag, |
| 126 | refresh, |
| 127 | applyDefaults |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | export { CreationLegsKind, DEFAULT_HAIR_HUE, DEFAULT_HAIR_SATURATION, DEFAULT_HAIR_DARKNESS }; |