Skip to main content

Module widgets

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’s widget/button sprite.
TextEdit
The text of a field and where its cursor and selection are, in characters (EditBox.value, cursorPos, highlightPos, maxLength).
TextField
A single-line text field (EditBox).
TextFieldLayout
What DrawContext::widget_text_field drew: 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.
TextFieldView
A text field as DrawContext::widget_text_field draws it: the game’s EditBox look, filled in by Widgets.
WidgetId
A widget of a Widgets group, 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.keyPressed handles it. ctrl is the platform’s word modifier and shift extends the selection.
Outcome
What a widget made of an input.
Widget
A widget of a group.

Functions§

byte_index
The byte index of character chars in s (its length past the end).
char_len
The length of s in 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’s beginIndex (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 index dir words away from from. Forward, past the rest of the word and its spaces; backward, over any spaces to the start of the word.