BlueGit

timfa questward

Files

questward / client / src / render / potionIcon.js

potionIcon.js

potionIcon.js 4.8 KiB Download Raw
1import { ItemId, itemDefinition, itemAssetPathForStack } from "/shared/definitions/items.js";
2import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
3import {
4 applyMagentaTintToImageData,
5 applyDualChannelTintToImageData,
6 normalizeRgb,
7 resolveItemTint,
8 resolveItemDualTint
9} from "/shared/render/tint.js";
10
11const bottleCache = new Map();
12const fillCache = new Map();
13const urlCache = new Map();
14const imageCache = new Map();
15
16function loadImage (src)
17{
18 if (imageCache.has(src))
19 { return imageCache.get(src); }
20 const promise = new Promise((resolve, reject) =>
21 {
22 const img = new Image();
23 img.onload = () => resolve(img);
24 img.onerror = () => reject(new Error(`Failed to load ${src}`));
25 img.src = src;
26 });
27 imageCache.set(src, promise);
28 return promise;
29}
30
31async function getBottle ()
32{
33 if (!bottleCache.has("bottle"))
34 { bottleCache.set("bottle", loadImage("/assets/items/glass-bottle/glass-bottle.png")); }
35 return bottleCache.get("bottle");
36}
37
38async function getFill (level)
39{
40 const key = Math.max(1, Math.min(5, level | 0 || 4));
41 if (!fillCache.has(key))
42 { fillCache.set(key, loadImage(`/assets/items/potion/fill-${key}.png`)); }
43 return fillCache.get(key);
44}
45
46function colorKey (color)
47{
48 const c = normalizeRgb(color);
49 return `${Math.round(c.r * 255)},${Math.round(c.g * 255)},${Math.round(c.b * 255)}`;
50}
51
52/**
53 * Bottle + magenta fill mask → tinted potion icon (UI).
54 * Fill sprites use magenta lightness; bottle stays non-magenta.
55 */
56export async function potionIconDataUrl (instance)
57{
58 const fillLevel = Math.max(1, Math.min(5, instance?.fillLevel | 0 || 4));
59 const tint = normalizeRgb(instance?.color);
60 const key = `potion|${fillLevel}|${colorKey(tint)}`;
61 if (urlCache.has(key))
62 { return urlCache.get(key); }
63
64 const [bottle, fill] = await Promise.all([getBottle(), getFill(fillLevel)]);
65 const canvas = document.createElement("canvas");
66 canvas.width = SPRITE_SIZE_PX;
67 canvas.height = SPRITE_SIZE_PX;
68 const ctx = canvas.getContext("2d");
69 ctx.clearRect(0, 0, canvas.width, canvas.height);
70 ctx.drawImage(fill, 0, 0);
71 const image = ctx.getImageData(0, 0, canvas.width, canvas.height);
72 applyMagentaTintToImageData(image, tint);
73 ctx.putImageData(image, 0, 0);
74 ctx.drawImage(bottle, 0, 0);
75
76 const url = canvas.toDataURL("image/png");
77 urlCache.set(key, url);
78 return url;
79}
80
81/** Tint magenta pixels in an arbitrary item PNG for UI icons. */
82export async function tintedItemIconDataUrl (assetPath, tint)
83{
84 const key = `item|${assetPath}|${colorKey(tint)}`;
85 if (urlCache.has(key))
86 { return urlCache.get(key); }
87 const img = await loadImage(assetPath);
88 const canvas = document.createElement("canvas");
89 canvas.width = img.width || SPRITE_SIZE_PX;
90 canvas.height = img.height || SPRITE_SIZE_PX;
91 const ctx = canvas.getContext("2d");
92 ctx.clearRect(0, 0, canvas.width, canvas.height);
93 ctx.drawImage(img, 0, 0);
94 const image = ctx.getImageData(0, 0, canvas.width, canvas.height);
95 applyMagentaTintToImageData(image, tint);
96 ctx.putImageData(image, 0, 0);
97 const url = canvas.toDataURL("image/png");
98 urlCache.set(key, url);
99 return url;
100}
101
102/** Dual-channel arrow icon (red shaft + blue tip masks). */
103export async function dualTintedItemIconDataUrl (assetPath, dualTint)
104{
105 const key = `dual|${assetPath}|${colorKey(dualTint.shaft)}|${colorKey(dualTint.tip)}`;
106 if (urlCache.has(key))
107 { return urlCache.get(key); }
108 const img = await loadImage(assetPath);
109 const canvas = document.createElement("canvas");
110 canvas.width = img.width || SPRITE_SIZE_PX;
111 canvas.height = img.height || SPRITE_SIZE_PX;
112 const ctx = canvas.getContext("2d");
113 ctx.clearRect(0, 0, canvas.width, canvas.height);
114 ctx.drawImage(img, 0, 0);
115 const image = ctx.getImageData(0, 0, canvas.width, canvas.height);
116 applyDualChannelTintToImageData(image, dualTint);
117 ctx.putImageData(image, 0, 0);
118 const url = canvas.toDataURL("image/png");
119 urlCache.set(key, url);
120 return url;
121}
122
123export function isPotionStack (stack)
124{
125 return (stack?.itemId === ItemId.potion && stack.instance?.kind === "potion")
126 || (stack?.itemId === ItemId.dye && stack.instance?.kind === "dye");
127}
128
129/** Set img.src to tinted icon when the stack/def has a tint; else fallback path. */
130export function applyItemIcon (img, stack, fallbackPath)
131{
132 if (!img)
133 { return; }
134 img.src = fallbackPath;
135 const def = itemDefinition(stack?.itemId);
136 const dualTint = resolveItemDualTint(stack, def);
137 if (dualTint)
138 {
139 dualTintedItemIconDataUrl(fallbackPath || itemAssetPathForStack(stack), dualTint).then((url) =>
140 {
141 img.src = url;
142 }).catch(() =>
143 {
144 img.src = fallbackPath;
145 });
146 return;
147 }
148 const tint = resolveItemTint(stack, def);
149 if (!tint)
150 { return; }
151 const work = isPotionStack(stack)
152 ? potionIconDataUrl(stack.instance)
153 : tintedItemIconDataUrl(fallbackPath || itemAssetPathForStack(stack), tint);
154 work.then((url) =>
155 {
156 img.src = url;
157 }).catch(() =>
158 {
159 img.src = fallbackPath;
160 });
161}