Module widgets
Expand description
Buttons and text fields for mod screens.
Buttons and text fields for a mod’s own screens (ClientHost::mod_screen_open), kept by the
mod and driven by the screen hooks it already listens to: draw them in
screen_render_post, and hand them the presses, releases, drags, keys and typed characters of
screen_mouse_button_pressed_pre, screen_mouse_button_released_pre,
screen_mouse_dragged_pre, screen_key_pressed_pre and screen_char_typed_pre. Each input
call answers an Outcome: set the hook’s canceled when it
Outcome::handled the input.
They behave as vanilla’s Button and EditBox do: the game draws them with its own sprites,
label and caret (DrawContext::widget_button, DrawContext::widget_text_field), so they get
its narration and controller support, and a text field edits with the same core as the
game’s own (TextEdit). Everything else is plain Rust in the mod,
the same in a native and a wasm build.
use mod_api::client::widgets::{Button, Outcome, TextField, Widgets};
use mod_api::Text;
let mut widgets = Widgets::new();
let name = widgets.add_text_field(TextField::new([20.0, 20.0, 150.0, 20.0], 32));
let done = widgets.add_button(Button::new([20.0, 50.0, 150.0, 20.0], Text::literal("Done")));
widgets.text_field_mut(name).unwrap().set_value("Steve");
assert_eq!(widgets.text_field(name).unwrap().value(), "Steve");
assert!(widgets.button(done).unwrap().active);Structs§
- Button
- A push button (
Button): a label on the game’swidget/buttonsprite. - Text
Edit - The text of a field and where its cursor and selection are, in characters
(
EditBox.value,cursorPos,highlightPos,maxLength). - Text
Field - A single-line text field (
EditBox). - Text
Field Layout - What
DrawContext::widget_text_fielddrew: the first character shown (after scrolling to the cursor) and the GUI x of each shown character boundary, from the start of the shown text to its end, so a press maps to the character it is on. - Text
Field View - A text field as
DrawContext::widget_text_fielddraws it: the game’sEditBoxlook, filled in byWidgets. - Widget
Id - A widget of a
Widgetsgroup, as it answered when added. - Widgets
- The widgets of one screen in focus order, and which one has the keyboard focus.
Enums§
- EditKey
- An editing key, as
EditBox.keyPressedhandles it.ctrlis the platform’s word modifier andshiftextends the selection. - Outcome
- What a widget made of an input.
- Widget
- A widget of a group.
Functions§
- byte_
index - The byte index of character
charsins(its length past the end). - char_
len - The length of
sin characters. - filter_
text StringUtil.filterText: the allowed characters, and line breaks where the field takes several lines.- is_
allowed_ chat_ character StringUtil.isAllowedChatCharacter.- java_
whitespace Character.isWhitespace: Unicode spaces and line breaks but not the no-break spaces, plus the ASCII separators U+001C to U+001F.- multiline_
word_ position MultilineTextField.getPreviousWord/getNextWord’sbeginIndex(a book page): any whitespace, line breaks included, separates words, and the search starts from the cursor clamped to the last character, as vanilla’s does.- word_
position EditBox.getWordPosition(dir, from): the character indexdirwords away fromfrom. Forward, past the rest of the word and its spaces; backward, over any spaces to the start of the word.