BlueGit

timfa questward

Files

questward / client / src / render / potionIcon.js

potionIcon.js

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