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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<ui::Scroll>,
|
||||
/// 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<String>,
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -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<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.
|
||||
/// 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<held-item>;
|
||||
|
||||
@@ -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<UiWidget>) -> Vec<UiWidget> {
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user