Files
horizon-airdef/crates/horizon-mods/tests/ui_guest/src/lib.rs
T
socksprox af7a685b5c Mod API: capability ui — mod-owned screens and settings pages
New WIT interface `ui` (text, item and list widgets; open-screen,
close-screen, set-settings) plus a `ui-events` export (select, closed,
settings-opened), in a second world `horizon-ui-mod` so mods built before
it keep loading. The game lays out and draws the widgets with its own
menus' code: a screen the mod owns (Esc/Done to leave) and a Settings
button on the mod's Options > Mods row that opens its page.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-25 17:09:25 +01:00

67 lines
1.9 KiB
Rust

//! A mod for the runtime's `ui` tests: `/ui_screen` opens a screen with one
//! of each widget, `/ui_settings` sets a settings page, `/ui_close` closes
//! the screen; the `ui-events` calls answer in chat so the host sees them.
use horizon_mod_sdk::{
Mod, Registration, chat,
ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget},
};
struct Guest;
fn widgets() -> Vec<Widget> {
vec![
Widget::Text(TextWidget {
id: 1,
text: "Pick a row".into(),
color: 0,
}),
Widget::Item(ItemWidget {
id: 2,
item: "minecraft:diamond".into(),
name: String::new(),
}),
Widget::List(ListWidget {
id: 7,
rows: vec!["first".into(), "second".into(), "third".into()],
selected: Some(1),
}),
]
}
impl Mod for Guest {
fn init() -> Result<(Self, Registration), String> {
let hooks = ["ui_screen", "ui_settings", "ui_close"]
.into_iter()
.fold(Registration::new(), Registration::command);
Ok((Guest, hooks))
}
fn command(&mut self, name: &str, _args: &str) -> Result<Option<String>, String> {
match name {
"ui_screen" => ui::open_screen(&Screen {
title: "Guest's screen".into(),
widgets: widgets(),
}),
"ui_settings" => ui::set_settings(&widgets()),
"ui_close" => ui::close_screen(),
_ => {}
}
Ok(None)
}
fn ui_select(&mut self, id: u32, row: u32) {
chat::show(&format!("selected {id}:{row}"));
}
fn ui_closed(&mut self) {
chat::show("closed");
}
fn ui_settings_opened(&mut self) {
ui::set_settings(&[Widget::Text(TextWidget {
id: 1,
text: "fresh".into(),
color: 0x33CC33,
})]);
chat::show("settings opened");
}
}
horizon_mod_sdk::export_mod!(Guest);