Contents

HUD text and parts

The mod developer docs are only available in Korean for now.

Drawing text, boxes and images on top of the game. You do it in the hud method of the type you give to veil_mod! as main:.

Basics: text and boxes

Subscribe to sub::HUD and Mod::hud(partial, gw, gh) is called every frame. gw and gh are the screen size after GUI scaling, and every coordinate uses those units.

  • hud::text(x, y, argb, shadow, "text") draws text and returns its width. Colours are 0xAARRGGBB.
  • hud::text_width("text") measures without drawing. Use it to right-align or centre.
  • hud::rect(x0, y0, x1, y1, argb) fills a rectangle. Note the arguments are two corners, not x, y, width, height.
  • Item icons: hud::item(x, y, &item). PNG images: upload once with hud::image_load, then draw with hud::image.

Text is 8 units tall. Centring a label in a cell, from the keystrokes example:

fn cell(x: f32, y: f32, w: f32, h: f32, on: bool, label: &str, off: u32) {
    hud::rect(x, y, x + w, y + h, if on { ON } else { off });
    let tw = hud::text_width(label);
    hud::text(x + (w - tw) / 2.0, y + (h - 8.0) / 2.0, if on { TXT_ON } else { TXT_OFF }, false, label);
}

Rounded boxes, shadows and blur: ui2

For anything beyond the vanilla look, use ui2: rounded corners, outlines, circles, gradients, drop shadows, background blur. Two conditions:

  • groups must include "ui" (and "base"), otherwise the mod will not link.
  • It only draws inside the HUD and mod-screen draw callbacks. Calls made anywhere else draw nothing and return 0.
#![no_std]
use mc189_mod_sdk::prelude::*;
use mc189_mod_sdk::ui2::Radii;

struct Card;

impl Mod for Card {
    fn init() -> Self {
        subscribe(sub::HUD);
        Card
    }

    fn hud(&mut self, _partial: f32, _gw: u32, _gh: u32) {
        // a rounded, translucent card with text on it
        ui2::rect(2.0, 2.0, 70.0, 14.0, Radii::all(4.0), 0x9010_1018);
        hud::text(6.0, 5.0, 0xFFFF_FFFF, true, &format!("FPS {}", client().fps));
    }
}

veil_mod! {
    id: "hud_card",
    name: "HUD card",
    version: "1.0.0",
    description: "FPS on a rounded card",
    authors: ["me"],
    main: Card,
    groups: ["base", "ui"],
}

Unlike hud::rect, ui2::rect(x, y, w, h, radii, argb) does take a width and height. The other shapes (stroke, circle, line, shadow, blur, nine) and the transform stack (save, translate, clip) are listed under ui2 in ../../reference.md. The ui_showcase example uses custom fonts (ui2::Font) and the animation helpers (Anim, ease).

hud::text (the vanilla font) is the default for text. ui2::Font needs a TTF or OTF file and does no GPOS kerning or shaping.

HUD parts: letting players move things

The game's HUD layout editor lets players drag widgets around. Split what your mod draws into parts and each part can be moved separately.

  1. Name the parts in veil_mod!: hud_parts: ["fps" = "FPS", "cps" = "CPS"]. The first value is the id ([a-z0-9_]{1,32}), the second is the label shown in the editor. Their order gives the part numbers.
  2. Call hud::part(0) before drawing the first part, hud::part(1) before the next, and so on. Everything drawn between two calls belongs to that part.
        hud::part(0);
        if on("fps", true) {
            hud::text(2.0, 2.0, col, sh, &format!("FPS: {}", c.fps));
        }
        hud::part(1);
        if on("cps", true) {
            hud::text(2.0, 12.0, col, sh, &format!("CPS: {} | {}", c.cps_left, c.cps_right));
        }
// …
mc189_hud_parts!("fps" = "FPS", "cps" = "CPS");

(fps_cps is written with the old macros. mc189_hud_parts!(…) is the old spelling of the hud_parts: field.)

The parts list lives in its own wasm section (mc189-hud), so older games that do not know about it still run the mod.

Reading state: client() and its flags

client() returns a ClientV1 with fps, cps_left, cps_right, ping_ms, gui_scale, server_addr_len and a flags bit set:

BitConstantMethodMeaning
1ClientV1::FLAG_IN_WORLDin_world()In a world
2FLAG_GUI_OPENgui_open()A GUI screen is open
4FLAG_MULTIPLAYERmultiplayer()Playing multiplayer
8FLAG_HUD_EDIThud_editing()The HUD layout editor is open
16FLAG_THIRD_PERSONthird_person()Third-person camera

If your HUD is normally hidden (outside a world, say), draw it anyway while hud_editing() is true. Otherwise it never shows up in the layout editor and players cannot place it.

Saving the on/off state

Values you want to keep between sessions go through config. The typed helpers parse for you:

let on = config::get_bool("on", true);     // "1"/"true"/"on" → true, missing → default
config::set_bool("on", !on);
let tint = config::get_color("tint", 0xFFFFFF);  // "#rrggbb" or "0xrrggbb"

The easy_hud example uses exactly this to remember whether its card is shown. For settings players change in the hub, use a settings table as in the quickstart template.

Common pitfalls

  • hud runs (you can see your log) but nothing shows. Is the F3 debug screen open? Every HUD layout element, mod HUD included, has "hide while F3 is open" switched on by default. Close F3, or turn that option off for the element in the hub.
  • Reading position, facing and the clock. player().yaw is vanilla yaw in degrees: 0 = south (+Z), 90 = west, 180 = north, 270 = east (same as the first number in F3's Facing); wrap it to 0..360 yourself (%, there is no rem_euclid for f32 in no_std). now_ms() is Unix epoch milliseconds in UTC with no time zone, so add the offset yourself. There is no client-side world time; use srvdata::world_time() on the server.
  • hud is never called. Did init call subscribe(sub::HUD)? tick and event likewise only arrive if you subscribe.
  • Things end up in the wrong place. Anchor to the right or bottom edge using gw and gh. Hard-coded coordinates break as soon as the GUI scale changes.
  • Allocating every frame. hud runs every frame under a time limit of about 5 to 6 ms; go over it repeatedly and the mod is switched off. Build strings and lists in tick (20 times a second) and only draw in hud.

Examples: easy_hud, fps_cps, keystrokes, armor_hud, potion_hud, minimap, ui_showcase.