Sell value on the tooltip !
This commit is contained in:
+34
-4
@@ -1,8 +1,10 @@
|
||||
// the box next to the mouse saying what an item is, like minecraft's: its name, then its lore
|
||||
// the box next to the mouse saying what an item is, like minecraft's: its name, what it's worth, then its lore
|
||||
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { display_name } from "$/common/utils.ts";
|
||||
import { canvas, draw_text, measure_text } from "$/client/renderer/mod.ts";
|
||||
import { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "$/client/renderer/mod.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
|
||||
import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts";
|
||||
|
||||
@@ -11,15 +13,22 @@ type Color = [number, number, number, number];
|
||||
const NAME_COLOR: Color = [1, 1, 1, 1];
|
||||
const LORE_COLOR: Color = [0.66, 0.66, 0.66, 1];
|
||||
const FOOD_COLOR: Color = [0.85, 0.6, 0.35, 1];
|
||||
const VALUE_COLOR: Color = [0.95, 0.8, 0.3, 1];
|
||||
// from the edge of the tooltip sprite to the text
|
||||
const PADDING = 4 * GUI_SCALE;
|
||||
const LINE_GAP = 4;
|
||||
// between a line's icon and its text
|
||||
const ICON_GAP = 4;
|
||||
// drawn before what an item sells for
|
||||
const VALUE_ICON = "bworld:copper_coin";
|
||||
// from the mouse, so the cursor doesn't cover it
|
||||
const OFFSET = 16;
|
||||
|
||||
export interface TooltipLine {
|
||||
text: string;
|
||||
color: Color;
|
||||
// a texture drawn before the text, a square as tall as the line
|
||||
icon?: string;
|
||||
}
|
||||
|
||||
// its name, the item's lore from its json, then what the server's scripts said about this stack
|
||||
@@ -29,6 +38,9 @@ export function item_tooltip(item: ItemStack): TooltipLine[] {
|
||||
if (info?.food) {
|
||||
lines.push({ text: `Restores ${info.food.nutrition} hunger`, color: FOOD_COLOR });
|
||||
}
|
||||
if (info?.base_sell_value !== undefined) {
|
||||
lines.push({ icon: VALUE_ICON, text: `${info.base_sell_value}`, color: VALUE_COLOR });
|
||||
}
|
||||
const static_lore = info?.lore;
|
||||
for (const lore of [static_lore, item.lore]) {
|
||||
for (const text of lore?.split("\n") ?? []) {
|
||||
@@ -41,7 +53,9 @@ export function item_tooltip(item: ItemStack): TooltipLine[] {
|
||||
// below and right of the mouse, moved back inside the window when it would go past its edge
|
||||
export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: number) {
|
||||
const line_height = TEXT_HEIGHT * TEXT_SCALE;
|
||||
const width = Math.max(...lines.map((line) => measure_text(line.text, TEXT_SCALE))) + PADDING * 2;
|
||||
const icon_width = (line: TooltipLine) => line.icon ? line_height + ICON_GAP : 0;
|
||||
const width = Math.max(...lines.map((line) => icon_width(line) + measure_text(line.text, TEXT_SCALE))) +
|
||||
PADDING * 2;
|
||||
const height = lines.length * line_height + (lines.length - 1) * LINE_GAP + PADDING * 2;
|
||||
|
||||
let x = mouse_x + OFFSET;
|
||||
@@ -52,7 +66,23 @@ export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: num
|
||||
y = Math.max(0, y);
|
||||
|
||||
draw_gui_sprite("tooltip", x, y, width, height);
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
lines.forEach((line, i) => {
|
||||
draw_text(line.text, x + PADDING, y + PADDING + i * (line_height + LINE_GAP), TEXT_SCALE, line.color);
|
||||
const line_y = y + PADDING + i * (line_height + LINE_GAP);
|
||||
if (line.icon) {
|
||||
const { x: sx, y: sy, width: sw, height: sh } = get_sprite_region(line.icon);
|
||||
draw_texture_region(
|
||||
atlas,
|
||||
sx,
|
||||
sy,
|
||||
sw,
|
||||
sh,
|
||||
x + PADDING - line_height / 2,
|
||||
line_y - line_height / 2,
|
||||
line_height * 2,
|
||||
line_height * 2,
|
||||
);
|
||||
}
|
||||
draw_text(line.text, x + PADDING + icon_width(line), line_y, TEXT_SCALE, line.color);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user