Fluid slots and kegs !
This commit is contained in:
@@ -31,6 +31,8 @@ export const GUI_ATLAS = {
|
||||
air_empty: { x: 100, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
|
||||
air_full: { x: 110, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
|
||||
air_half: { x: 120, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
|
||||
// the slot with modern industrialization's fluid slot droplet, drawn over it by hand
|
||||
fluid_slot: { x: 160, y: 0, width: 18, height: 18, border: [0, 0, 0, 0] },
|
||||
} as const;
|
||||
|
||||
export type GuiSprite = keyof typeof GUI_ATLAS;
|
||||
|
||||
+40
-10
@@ -11,11 +11,11 @@ import { ClientInventories } from "../inventory.ts";
|
||||
import { canvas, draw_rect, draw_texture_region, Texture } from "../renderer/mod.ts";
|
||||
import { AssetManager } from "../assets.ts";
|
||||
import { get_sprite_region } from "../sprites.ts";
|
||||
import { draw_item } from "../rendering/render_utils.ts";
|
||||
import { draw_tooltip, item_tooltip } from "./tooltip.ts";
|
||||
import { draw_fluid, draw_item } from "../rendering/render_utils.ts";
|
||||
import { draw_tooltip, item_tooltip, tank_tooltip } from "./tooltip.ts";
|
||||
import { ModGraphics } from "./mod_graphics.ts";
|
||||
import { inventory_overlays } from "../mods.ts";
|
||||
import type { ClientMenu, ClientSlot } from "../menu.ts";
|
||||
import type { ClientFluidSlot, ClientMenu, ClientSlot } from "../menu.ts";
|
||||
|
||||
// how far past the slot's edge its highlight goes
|
||||
const HIGHLIGHT_OVERHANG = 3 * GUI_SCALE;
|
||||
@@ -42,8 +42,8 @@ export abstract class GuiScreen {
|
||||
}
|
||||
}
|
||||
|
||||
// a menu's screen, like minecraft's AbstractContainerScreen: in the middle of the window, it draws the menu's slots,
|
||||
// the item on the cursor and tooltips, and sends clicks. the server owns what's in the slots: clicks are guessed
|
||||
// a menu's screen, like minecraft's AbstractContainerScreen: in the middle of the window, it draws the menu's slots and
|
||||
// fluid slots, the item on the cursor and tooltips, and sends clicks. the server owns what's in the slots: clicks are guessed
|
||||
// locally so they feel instant, then whatever the server syncs back wins. subclasses draw the rest
|
||||
export abstract class GuiMenuScreen extends GuiScreen {
|
||||
inventories: ClientInventories;
|
||||
@@ -54,6 +54,7 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
||||
height: number;
|
||||
|
||||
hovering: ClientSlot | undefined;
|
||||
hovering_fluid: ClientFluidSlot | undefined;
|
||||
// what the screen and mods draw with, in gui pixels
|
||||
graphics = new ModGraphics();
|
||||
|
||||
@@ -109,6 +110,7 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
||||
if (this.inventory_overlays) this.#render_overlays();
|
||||
if (g.mouse_blocked()) {
|
||||
this.hovering = undefined;
|
||||
this.hovering_fluid = undefined;
|
||||
} else {
|
||||
this.handle_interaction();
|
||||
}
|
||||
@@ -122,15 +124,17 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
||||
if (item) {
|
||||
draw_tooltip(item_tooltip(item), mouse.x, mouse.y);
|
||||
}
|
||||
} else if (this.hovering_fluid) {
|
||||
draw_tooltip(tank_tooltip(this.hovering_fluid.tank, this.hovering_fluid.output === true), mouse.x, mouse.y);
|
||||
}
|
||||
g.end();
|
||||
}
|
||||
|
||||
#slot_x(slot: ClientSlot) {
|
||||
#slot_x(slot: { x: number }) {
|
||||
return this.x + slot.x * GUI_SCALE;
|
||||
}
|
||||
|
||||
#slot_y(slot: ClientSlot) {
|
||||
#slot_y(slot: { y: number }) {
|
||||
return this.y + slot.y * GUI_SCALE;
|
||||
}
|
||||
|
||||
@@ -138,15 +142,24 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
||||
for (const slot of this.menu.slots) {
|
||||
draw_gui_sprite("slot", this.#slot_x(slot), this.#slot_y(slot));
|
||||
}
|
||||
// like modern industrialization's, an empty one shows a droplet
|
||||
for (const slot of this.menu.fluid_slots) {
|
||||
draw_gui_sprite(slot.tank.fluid ? "slot" : "fluid_slot", this.#slot_x(slot), this.#slot_y(slot));
|
||||
}
|
||||
|
||||
// like minecraft's, the highlight is a bit bigger than the slot, part of it under the item and part over
|
||||
const highlight = (sprite: "slot_highlight_back" | "slot_highlight_front") => {
|
||||
if (this.hovering) {
|
||||
const x = this.#slot_x(this.hovering) - HIGHLIGHT_OVERHANG;
|
||||
draw_gui_sprite(sprite, x, this.#slot_y(this.hovering) - HIGHLIGHT_OVERHANG);
|
||||
const hovered = this.hovering ?? this.hovering_fluid;
|
||||
if (hovered) {
|
||||
const x = this.#slot_x(hovered) - HIGHLIGHT_OVERHANG;
|
||||
draw_gui_sprite(sprite, x, this.#slot_y(hovered) - HIGHLIGHT_OVERHANG);
|
||||
}
|
||||
};
|
||||
highlight("slot_highlight_back");
|
||||
// the whole slot in its fluid while there's any, whatever the amount, like modern industrialization's
|
||||
for (const slot of this.menu.fluid_slots) {
|
||||
if (slot.tank.fluid) draw_fluid(slot.tank.fluid, this.#slot_x(slot), this.#slot_y(slot));
|
||||
}
|
||||
for (const slot of this.menu.slots) {
|
||||
const item = slot.container.get_item(slot.index);
|
||||
if (item) {
|
||||
@@ -218,8 +231,25 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
||||
|
||||
handle_interaction() {
|
||||
this.hovering = undefined;
|
||||
this.hovering_fluid = undefined;
|
||||
const mouse = InputManager.get_mouse_position();
|
||||
|
||||
for (const [index, slot] of this.menu.fluid_slots.entries()) {
|
||||
const [x, y] = [this.#slot_x(slot), this.#slot_y(slot)];
|
||||
if (!point_inside_rec(mouse.x, mouse.y, x, y, SLOT_SIZE, SLOT_SIZE)) {
|
||||
continue;
|
||||
}
|
||||
this.hovering_fluid = slot;
|
||||
for (const button of [0, 2]) {
|
||||
if (InputManager.is_mouse_pressed(button)) {
|
||||
InputManager.consume_mouse(button);
|
||||
// what the cursor's container turns into depends on the tank, the server says
|
||||
this.send({ type: "click_fluid", menu: this.menu.id, slot: index });
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (const [index, slot] of this.menu.slots.entries()) {
|
||||
const [x, y] = [this.#slot_x(slot), this.#slot_y(slot)];
|
||||
if (!point_inside_rec(mouse.x, mouse.y, x, y, SLOT_SIZE, SLOT_SIZE)) {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { display_name } from "$/common/utils.ts";
|
||||
import { fluid_rules } from "$/client/fluid_rules.ts";
|
||||
import { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "$/client/renderer/mod.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import type { MenuTank } from "$/common/protocol.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";
|
||||
@@ -62,6 +63,19 @@ export function item_tooltip(item: ItemStack): TooltipLine[] {
|
||||
return lines;
|
||||
}
|
||||
|
||||
// a menu's fluid slot, like modern industrialization's: the fluid, how much of the tank it fills, and what clicking it
|
||||
// with a fluid container does
|
||||
export function tank_tooltip(tank: MenuTank, output: boolean): TooltipLine[] {
|
||||
return [
|
||||
tank.fluid ? { text: fluid_rules.name(tank.fluid), color: NAME_COLOR } : { text: "Empty", color: LORE_COLOR },
|
||||
{ text: `${tank.amount} / ${tank.capacity} mB`, color: FLUID_COLOR },
|
||||
{
|
||||
text: output ? "Fluid Output, Click to Extract" : "Fluid Input, Click to Insert or Extract",
|
||||
color: LORE_COLOR,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user