From 3e3c0cebb51fa5deb0022ff1c0ca8fa4ba9fc42d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 05:34:49 +0100 Subject: [PATCH] Mod keys: rows the game adds to the settings page, not a WIT widget A new case in ui's widget variant stops every ui mod built before it from loading: the runtime links a component only when each imported function's types match exactly ("expected variant of 5 cases, found 4"), so the published FPS Counter and Keystrokes would break. Instead, the game lists a mod's keys first on its page in Options > Mods, as Key Binds rows, and a mod with keys gets a page even without ui. No WIT type changes. docs/api/stability.md said a new case the mod sends isn't breaking; it is, and now says so. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/mods.rs | 113 ++++++++++-------- crates/horizon-mod-sdk/wit/horizon.wit | 17 +-- crates/horizon-mods/src/runtime.rs | 17 ++- crates/horizon-mods/tests/sandbox.rs | 8 +- crates/horizon-mods/tests/ui_guest/src/lib.rs | 11 +- docs/api/mods-reference.md | 12 +- docs/api/stability.md | 9 +- docs/mods.md | 4 +- 8 files changed, 86 insertions(+), 105 deletions(-) diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 66d00b233..e55a87d46 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -27,7 +27,7 @@ use { text::{self, Text}, ui, }, - horizon_mods::{Clock, Mods, Output, Player, Snapshot, Status, UiScreen, UiWidget}, + horizon_mods::{Clock, ModKey, Mods, Output, Player, Snapshot, Status, UiScreen, UiWidget}, std::time::Instant, winit::{event::MouseButton, keyboard::KeyCode}, }; @@ -102,7 +102,7 @@ struct ModScreen { under: Screen, /// One scroll per `list` widget, in widget order. scrolls: Vec, - /// A key widget waiting for the key to bind: the mod key's name. + /// A key row waiting for the key to bind: the mod key's name. binding: Option, } impl ModsState { @@ -575,7 +575,7 @@ impl Game<'_> { fn mod_ui_view<'a>(mods: &'a Mods, ms: &ModScreen) -> Option<(&'a str, &'a [UiWidget])> { if ms.settings { let widgets = mods.settings_widgets(ms.owner); - if widgets.is_empty() { + if widgets.is_empty() && mods.keys_of(ms.owner).is_empty() { return None; } let name = mods @@ -589,24 +589,25 @@ impl Game<'_> { Some((s.title.as_str(), s.widgets.as_slice())) } } - /// A key widget's label as Key Binds shows one, "Zoom: Z", marked + /// The keys a settings page lists first: the mod's own (`input`). + fn mod_page_keys<'a>(mods: &'a Mods, ms: &ModScreen) -> &'a [ModKey] { + if ms.settings { + mods.keys_of(ms.owner) + } else { + &[] + } + } + /// A key row's label as Key Binds shows one, "Zoom: Z", marked /// "> Zoom: Z <" in yellow while it waits for the new key. - fn mod_key_label(mods: &Mods, owner: usize, name: &str, waiting: bool) -> Text { - let (label, key) = mods - .keys_of(owner) - .iter() - .find(|k| k.name == name) - .map_or((name, "key.keyboard.unknown"), |k| { - (k.label.as_str(), k.key.as_str()) - }); - let value = format!("{label}: {}", keybinds::display_name(key).plain()); + fn mod_key_label(k: &ModKey, waiting: bool) -> Text { + let value = format!("{}: {}", k.label, keybinds::display_name(&k.key).plain()); if waiting { Text::literal(format!("> {value} <")).with_color(text::YELLOW) } else { Text::literal(value) } } - /// The player bound the key widget that waited to `key`: the mod's key + /// The player bound the key row that waited to `key`: the mod's key /// follows it now and at the next start (`horizon-mods.txt`). fn bind_mod_key(&mut self, key: String) { let Some(ms) = self.mods.screen.as_mut() else { @@ -637,7 +638,7 @@ impl Game<'_> { .insert(name, key); self.settings.save(&Self::options_path()); } - /// A key while a mod screen is up: true when a key widget waited for + /// A key while a mod screen is up: true when a key row waited for /// it (Key Binds: Escape binds none). pub(super) fn mod_screen_key(&mut self, key: KeyCode) -> bool { if self @@ -657,13 +658,13 @@ impl Game<'_> { } /// The layout the mod screen's widgets get at `h`: (widget, top, /// height), the column being `COLUMN` wide down the middle. - fn mod_ui_layout(h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> { + fn mod_ui_layout(h: f32, top: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> { let mut out = Vec::with_capacity(widgets.len()); - let mut y = MOD_UI_TOP; + let mut y = top; for w in widgets { let wh = match w { UiWidget::Text(_) => 12.0, - UiWidget::Item(_) | UiWidget::Button(_) | UiWidget::Key(_) => 20.0, + UiWidget::Item(_) | UiWidget::Button(_) => 20.0, UiWidget::List(l) => (l.rows.len().min(6) as f32 * 12.0) + 8.0, }; // Nothing may cover Done; a crowded mod loses what won't fit. @@ -683,7 +684,8 @@ impl Game<'_> { } if settings { self.mods.mods.ui_settings_opened(owner); - if self.mods.mods.settings_widgets(owner).is_empty() { + let keys = self.mods.mods.keys_of(owner); + if self.mods.mods.settings_widgets(owner).is_empty() && keys.is_empty() { self.show_mod_output(); return; } @@ -755,7 +757,27 @@ impl Game<'_> { ); let cx = (batch.width / 2.0).floor(); let x = cx - MOD_UI_COLUMN / 2.0; - let layout = Self::mod_ui_layout(batch.height, widgets); + // A settings page starts with the mod's keys, as Key Binds rows. + let keys = Self::mod_page_keys(&self.mods.mods, ms); + for (i, k) in keys.iter().enumerate() { + let wy = MOD_UI_TOP + i as f32 * MOD_UI_KEY_ROW; + let hover = ui::hovered(self.cursor, x, wy, MOD_UI_COLUMN, 20.0); + let waiting = ms.binding.as_deref() == Some(k.name.as_str()); + let label = Self::mod_key_label(k, waiting); + ui::button( + batch, + atlas, + x, + wy, + MOD_UI_COLUMN, + 20.0, + &label, + true, + hover, + ); + } + let top = MOD_UI_TOP + keys.len() as f32 * MOD_UI_KEY_ROW; + let layout = Self::mod_ui_layout(batch.height, top, widgets); let mut list_i = 0usize; for (w, &(wy, wh)) in widgets.iter().zip(&layout) { match w { @@ -774,22 +796,6 @@ impl Game<'_> { hover, ); } - UiWidget::Key(k) => { - let hover = ui::hovered(self.cursor, x, wy, MOD_UI_COLUMN, 20.0); - let waiting = ms.binding.as_deref() == Some(k.key.as_str()); - let label = Self::mod_key_label(&self.mods.mods, ms.owner, &k.key, waiting); - ui::button( - batch, - atlas, - x, - wy, - MOD_UI_COLUMN, - 20.0, - &label, - true, - hover, - ); - } UiWidget::Text(t) => { let color = if t.color == 0 { u32::MAX @@ -920,7 +926,22 @@ impl Game<'_> { }; let owner = ms.owner; let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0; - let layout = Self::mod_ui_layout(h, widgets); + // A key row waits for the key to bind. + let keys = Self::mod_page_keys(&self.mods.mods, ms); + let picked = (0..keys.len()).find(|&i| { + let wy = MOD_UI_TOP + i as f32 * MOD_UI_KEY_ROW; + ui::hovered(cursor, x, wy, MOD_UI_COLUMN, 20.0) + }); + if let Some(i) = picked { + let name = keys[i].name.clone(); + self.audio.play_button_click(); + if let Some(ms) = self.mods.screen.as_mut() { + ms.binding = Some(name); + } + return; + } + let top = MOD_UI_TOP + keys.len() as f32 * MOD_UI_KEY_ROW; + let layout = Self::mod_ui_layout(h, top, widgets); let mut list_i = 0usize; // The `ui_select` call needs `&mut`: find the pick first. let mut hit = None; @@ -935,17 +956,6 @@ impl Game<'_> { self.show_mod_output(); return; } - // A key widget waits for the key to bind. - if let UiWidget::Key(k) = w - && ui::hovered(cursor, x, wy, MOD_UI_COLUMN, 20.0) - { - self.audio.play_button_click(); - let name = k.key.clone(); - if let Some(ms) = self.mods.screen.as_mut() { - ms.binding = Some(name); - } - return; - } let UiWidget::List(l) = w else { continue }; let scroll = ms.scrolls.get(list_i).copied().unwrap_or_default(); list_i += 1; @@ -975,7 +985,9 @@ impl Game<'_> { return; }; let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0; - let layout = Self::mod_ui_layout(h, widgets); + let top = + MOD_UI_TOP + Self::mod_page_keys(&self.mods.mods, ms).len() as f32 * MOD_UI_KEY_ROW; + let layout = Self::mod_ui_layout(h, top, widgets); let mut list_i = 0usize; for (w, &(wy, wh)) in widgets.iter().zip(&layout) { let UiWidget::List(l) = w else { continue }; @@ -995,3 +1007,6 @@ impl Game<'_> { const MOD_UI_TOP: f32 = 40.0; #[cfg(feature = "mods")] const MOD_UI_COLUMN: f32 = 200.0; +/// A key row's height with its gap, as the widgets below it space out. +#[cfg(feature = "mods")] +const MOD_UI_KEY_ROW: f32 = 26.0; diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index f9c147cfa..9088c0c39 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -137,16 +137,6 @@ interface ui { /// Up to 256 characters. label: string, } - /// One of the mod's keys (`input.register-key`) as Controls shows a - /// binding, "Zoom: Z". A click waits for the next key or mouse button, - /// which becomes the key (Esc: none). The game keeps the player's - /// choice across starts; the mod's `key-down` and `key-up` follow it. - record key-widget { - /// The mod's own name for the widget. - id: u32, - /// The name the mod registered the key under. - key: string, - } variant widget { /// Text the player reads. text(text-widget), @@ -156,8 +146,6 @@ interface ui { item(item-widget), /// Rows the player picks from. %list(list-widget), - /// A key the player binds. - key(key-widget), } /// A screen the mod owns: a title over widgets stacked down the middle, /// the Done button the game adds, and Esc to leave it. @@ -232,8 +220,9 @@ interface input { claim-items: func(ids: list); /// A key of the mod's: `name` comes back in `key-down` and `key-up`, /// `label` says what it does, `key` is the key as options.txt writes - /// it (`key.keyboard.r`), the default: a key the player chose for it - /// (`ui`'s key widget) wins. At most 16; an unknown key is an error. + /// it (`key.keyboard.r`), the default: the player can bind another + /// one on the mod's page in Options > Mods, and that one wins. At most + /// 16; an unknown key is an error. register-key: func(name: string, label: string, key: string) -> result<_, string>; /// The main hand's stack, as of this tick; none when it is empty. held: func() -> option; diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index f596db81c..0011fa99d 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -37,8 +37,7 @@ use bindings::{ /// The widget model a `ui` screen or settings page shows (WIT `interface /// ui`), for the game to draw with its menus' own widgets. pub use ui_bindings::horizon::mods::ui::{ - ButtonWidget, ItemWidget, KeyWidget, ListWidget, Screen as UiScreen, TextWidget, - Widget as UiWidget, + ButtonWidget, ItemWidget, ListWidget, Screen as UiScreen, TextWidget, Widget as UiWidget, }; use ui_bindings::{UiHost, horizon::mods::ui}; /// Capability `render`'s imports (docs/mods.md): the game draws the shapes. @@ -153,11 +152,13 @@ pub struct Entry { } impl Entry { /// The running mod has a settings page (capability `ui` set widgets - /// with `ui.set-settings`): its row in Options > Mods gains a button. + /// with `ui.set-settings`, or `input` registered keys the player may + /// bind there): its row in Options > Mods gains a button. pub fn has_ui_settings(&self) -> bool { - self.run - .as_ref() - .is_some_and(|r| !r.store.data().ui.settings.is_empty()) + self.run.as_ref().is_some_and(|r| { + let d = r.store.data(); + !d.ui.settings.is_empty() || !d.input.keys.is_empty() + }) } } @@ -856,10 +857,6 @@ fn clip_widgets(widgets: Vec) -> Vec { l.selected = l.selected.filter(|&s| (s as usize) < l.rows.len()); UiWidget::List(l) } - UiWidget::Key(mut k) => { - k.key = clip(k.key); - UiWidget::Key(k) - } }) .collect() } diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index 793d03db6..3b33ab4b2 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -449,7 +449,7 @@ fn ui_screens_settings_and_events() { assert_eq!(mods.settings_widgets(0).len(), 3); assert!(mods.entries()[0].has_ui_settings()); mods.ui_settings_opened(0); - assert_eq!(mods.settings_widgets(0).len(), 3); + assert_eq!(mods.settings_widgets(0).len(), 2); // `/ui_close` closes the mod's own screen. assert!(mods.command("ui_close")); assert!(mods.drain().contains(&Output::CloseScreen { owner: 0 })); @@ -566,10 +566,8 @@ fn the_players_key_choice_wins() { }; let mut mods = load("horizon_mods_test_ui_guest.hmod"); assert!(mods.command("input_claim")); - assert!(matches!( - &mods.settings_widgets(0)[0], - horizon_mods::UiWidget::Key(k) if k.id == 9 && k.key == "wheel" - )); + // Its keys alone give it a page in Options > Mods, where the game lists them. + assert!(mods.settings_widgets(0).is_empty() && mods.entries()[0].has_ui_settings()); // Rebinding while held lets the old key go. mods.key("key.keyboard.r", true); assert!(mods.rebind(0, "wheel", "key.mouse.4")); diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index f2765e8da..9bede11f9 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -9,7 +9,7 @@ use horizon_mod_sdk::{ Mod, Registration, chat, input::{self, Event}, render::{self, FillShape, ItemShape, Point, QuadShape, Rect, Shape, SpriteShape, TextShape}, - ui::{self, ItemWidget, KeyWidget, ListWidget, Screen, TextWidget, Widget}, + ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget}, }; /// One of each shape, the text too long by 44 characters. @@ -162,10 +162,6 @@ impl Mod for Guest { // Not a key: refused. let bad = input::register_key("bad", "Bad", "R").unwrap_err(); chat::show(&format!("claimed; {bad}")); - ui::set_settings(&[Widget::Key(KeyWidget { - id: 9, - key: "wheel".into(), - })]); } "input_poll" => { for event in input::poll() { @@ -229,11 +225,6 @@ impl Mod for Guest { id: 3, label: "Corner: Top Right".into(), }), - // `/input_claim`'s key; drawn as its name until registered. - Widget::Key(KeyWidget { - id: 9, - key: "wheel".into(), - }), ]); chat::show("settings opened"); } diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index f525ace89..a51fb904b 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -197,15 +197,6 @@ A thing on a mod's screen or settings page. A button as the options pages have t | `id` | `u32` | The mod's own name for the widget. | | `label` | `string` | Up to 256 characters. | -### record `key-widget` - -One of the mod's keys (`input.register-key`) as Controls shows a binding, "Zoom: Z". A click waits for the next key or mouse button, which becomes the key (Esc: none). The game keeps the player's choice across starts; the mod's `key-down` and `key-up` follow it. - -| Field | Type | | -| --- | --- | --- | -| `id` | `u32` | The mod's own name for the widget. | -| `key` | `string` | The name the mod registered the key under. | - ### variant `widget` | Case | Payload | | @@ -214,7 +205,6 @@ One of the mod's keys (`input.register-key`) as Controls shows a binding, "Zoom: | `button` | `button-widget` | A button. | | `item` | `item-widget` | An item's icon and name. | | `list` | `list-widget` | Rows the player picks from. | -| `key` | `key-widget` | A key the player binds. | ### record `screen` @@ -288,7 +278,7 @@ A mapping as the player has bound it. | Function | Signature | What it does | | --- | --- | --- | | `claim-items` | `func(ids: list)` | The items whose use the mod handles instead of the game, by id (at most 64). Replaces the previous list. | -| `register-key` | `func(name: string, label: string, key: string) -> result<_, string>` | A key of the mod's: `name` comes back in `key-down` and `key-up`, `label` says what it does, `key` is the key as options.txt writes it (`key.keyboard.r`), the default: a key the player chose for it (`ui`'s key widget) wins. At most 16; an unknown key is an error. | +| `register-key` | `func(name: string, label: string, key: string) -> result<_, string>` | A key of the mod's: `name` comes back in `key-down` and `key-up`, `label` says what it does, `key` is the key as options.txt writes it (`key.keyboard.r`), the default: the player can bind another one on the mod's page in Options > Mods, and that one wins. At most 16; an unknown key is an error. | | `held` | `func() -> option` | The main hand's stack, as of this tick; none when it is empty. | | `poll` | `func() -> list` | The events since the last call, oldest first. | | `claim-wheel` | `func(on: bool)` | Takes the mouse wheel while playing (true) or gives it back: while taken, its steps are the mod's `wheel` events and the hotbar doesn't move. The game gives it back when the mod stops. | diff --git a/docs/api/stability.md b/docs/api/stability.md index 132c971f9..d5bd0e545 100644 --- a/docs/api/stability.md +++ b/docs/api/stability.md @@ -26,12 +26,13 @@ promise, how something is deprecated and removed, and the tests that keep old mo ## What counts as a breaking change Breaking: removing or renaming anything; changing a type, a default, a unit or a meaning; making something optional -required; refusing input that was accepted; a new case in a WIT `enum` or `variant` the game sends to mods (an older -component cannot decode it); a JSON field that changes type. +required; refusing input that was accepted; any change to a WIT type a function uses, a new `enum` or `variant` case +included, in either direction (the runtime links a component only when each imported function's types match exactly: +a mod built before the change no longer loads); a JSON field that changes type. Not breaking: a new function, interface, capability, command, endpoint, tool or `server.toml` key; a new optional -argument or JSON field (readers must ignore fields they don't know); a new WIT case the mod *sends* (the game decodes -every case it ever served); faster, or fixing behaviour to what the reference already says. +argument or JSON field (readers must ignore fields they don't know); a new WIT function or type (mods built earlier +don't import it); faster, or fixing behaviour to what the reference already says. ## Stability levels diff --git a/docs/mods.md b/docs/mods.md index 108e854be..a8d8da851 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -87,9 +87,9 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | `hud` | `hud` | `set-panel(corner, lines)`, `clear()`: up to 16 lines of 256 characters | | `storage` | `storage` | `get(key)`, `set(key, value)`, `delete(key)`: a per-mod folder of up to 64 files, 4 MiB total, at most 1 MiB written per call | | `command` | `command` | `run(line)`: a vanilla command run as the player, with the player's permissions, after the hook returns (8 per call); mods' own commands are not run | -| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, button, item, list and key widgets the game lays out and draws with its own menus' code. A key widget binds one of the mod's keys (`input.register-key`) as Key Binds does: click, then press the key or mouse button (Esc: none); the game keeps the choice in `horizon-mods.txt` (`key..:key.keyboard.z`) and the mod's `key-down` follows it. **Experimental:** the manifest needs `experimental = ["ui"]` | +| `ui` | `ui` | `open-screen`, `close-screen`, `set-settings`: a screen the mod owns and a page in Options > Mods, of text, button, item and list widgets the game lays out and draws with its own menus' code. **Experimental:** the manifest needs `experimental = ["ui"]` | | `render` | `render` | `set-hud(shapes)`: up to 1024 fills, four-cornered shapes, GUI sprites, items and text, kept on the HUD (over the hotbar, under chat and screens, hidden with F1) until replaced; `set-corner(corner, w, h, shapes)`: a box the game places in a corner, stacked with its overlays and other panels, as `hud`'s panels are; `text-width(text)`; `frame-stats()`: the game's FPS, min and 1% low; `gui-size()`; `shake(strength, seconds)`: the view turns, the player doesn't, scaled by Distortion Effects; `set-zoom(zoom)`: the field of view divided by up to 10, eased or at once, with slower turning, while playing and not through a spyglass (the strongest of several mods' zooms counts). The game's own HUD renderer draws them: a mod never supplies shader code. **Experimental:** `experimental = ["render"]` | -| `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs; a screen opening lets held keys up), mouse buttons as `key.mouse.4`; `claim-wheel(on)`: the wheel's steps become `wheel` events and the hotbar doesn't move (the one input a mod takes from the game; a mod's keys never do); `held()`; `mapping(name)`: a movement or click mapping's key label and whether it is held; `clicks-per-second()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | +| `input` | `input` | `claim-items(ids)`: the use button on those items is the mod's (`use-start`, `use-stop` with the ticks held; the game doesn't place, eat or swing); `register-key(name, label, key)`: up to 16 keys (`key.keyboard.r`), heard while playing as `key-down` / `key-up` (the game's own mapping on that key still runs; a screen opening lets held keys up), mouse buttons as `key.mouse.4`. The player rebinds them on the mod's page in Options > Mods, where the game lists them first as Key Binds rows (click, then press a key or mouse button; Esc binds none), even for a mod without `ui`; the choice is kept in `horizon-mods.txt` (`key..:key.keyboard.z`) and wins over `register-key`'s default; `claim-wheel(on)`: the wheel's steps become `wheel` events and the hotbar doesn't move (the one input a mod takes from the game; a mod's keys never do); `held()`; `mapping(name)`: a movement or click mapping's key label and whether it is held; `clicks-per-second()`; `poll()` returns the events, at most 256 kept. **Experimental:** `experimental = ["input"]` | | Hook (export) | When | |---|---|