Module pix_engine::gui

source ·
Expand description

Graphical User Interface methods.

Uses immediate mode. See the gui example in the examples/ folder for a full demo.

Note

Many widgets rely on unique labels or IDs that are consistent across frames for internal state management. This ID is generally built using the hash of the label combined with any parents they may be under, for example items rendered under a tab are combined with the tabs label hash.

e.g.

if s.button("Click")? {     // Label = "Click", ID = hash of "Click"
    // Handle click action
}
s.advanced_tooltip(
    "Advanced Tooltip",
    rect![s.mouse_pos(), 300, 100],
    |s: &mut PixState| {
        // Label = "Click", ID = hash of "Click" + "Advanced Tooltip"
        if s.button("Click")? {
            // Handle click action
        }
        Ok(())
    },
)?;

There is an ID stack system in place in addition to a ## string pattern that can be used to uniquely identify elements that may require an empty label or conflict with another element.

If you find that your widget is not properly interacting with user events or maintaining state correctly, try one of the following methods to ensure the label is unique.

You can append a unique identifier after your label with ##. Anything after this pattern won’t be visible to the user:

if s.button("Click##action1")? {     // Label = "Click", ID = hash of "Click##action1"
    // Handle action 1
}
if s.button("Click##action2")? {     // Label = "Click", ID = hash of "Click##action2"
    // Handle action 2
}

You can use PixState::push_id and PixState::pop_id either by itself, or as part of a loop:

for i in 0..5 {
  s.push_id(i);             // Push i to the ID stack
  if s.button("Click")? {   // Label = "Click",  ID = hash of "Click" + i
    // Handle click action
  }
  s.pop_id();
}

Example

fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
    s.text("Some text")?;
    s.separator()?; // Adds a horizontal line separator
    s.spacing(); // Adds a line of spacing

    if s.button("Button")? {
        // Button was clicked!
    }

    s.checkbox("Checkbox", &mut self.checkbox)?;

    s.next_width(200);
    s.text_field("Text Field", &mut self.text_field)?;
    Ok(())
}

Modules

  • UI spacing & layout rendering methods.
  • Operating System related methods.
  • UI theme methods.
  • UI widget rendering methods.

Enums

Constants

  • Platform-specific control modifier key. Command on macOS.