raygui
raygui is an immediate-mode GUI toolkit
bundled with raylib. raylib-rs 6.0 exposes all 57 RAYGUIAPI functions at
parity with raygui 6.0. Controls are grouped into sub-traits
(RaylibGuiControls, RaylibGuiContainers, RaylibGuiState,
RaylibGuiAdvanced, RaylibGuiIcons) and blanket-implemented for every draw
handle — just use raylib::prelude::* and the methods are available inside
a begin_drawing frame.
String parameters accept impl AsRef<str> (pass &str, String, or any
type that derefs to a string slice) — conversion goes through a thread-local
scratch buffer so there are no per-frame heap allocations.
Feature gate. raygui is behind the
rayguiCargo feature. Addfeatures = ["raygui"](or"full") to yourCargo.tomldependency.
API surface
RaylibGuiControls::gui_button— button control; returnstruewhen clicked.RaylibGuiControls::gui_label— label control; displays text.RaylibGuiControls::gui_slider— horizontal slider; takes&mut f32for the current value.RaylibGuiControls::gui_combo_box— combo box; semicolon-separated option string,&mut i32active index.RaylibGuiContainers::gui_window_box— window box container; returnstruewhen the close button is clicked.RaylibGuiState::gui_set_state— set global GUI state (Normal,Focused,Pressed,Disabled).RaylibGuiState::gui_load_style— load a.rgsstyle file to retheme all controls.RaylibDrawGui— umbrella supertrait re-exported for source compatibility.
Example
raygui controls require an active drawing frame and a GPU context.
The example below is marked ignore because the software_renderer feature
is mutually exclusive with the default OpenGL build used by the book’s CI.
The WS9 showcase will provide a
runnable raygui demo once the web gallery is deployed.
extern crate raylib;
use raylib::prelude::*;
fn main() {
let (mut rl, thread) = raylib::init()
.size(400, 300)
.title("raygui demo")
.build();
let mut slider_val: f32 = 0.5;
while !rl.window_should_close() {
let mut d = rl.begin_drawing(&thread);
d.clear_background(Color::RAYWHITE);
// Label
d.gui_label(
Rectangle::new(20.0, 20.0, 200.0, 30.0),
"Hello, raygui!",
);
// Button — returns true on click
if d.gui_button(Rectangle::new(20.0, 60.0, 120.0, 30.0), "Click me") {
println!("button clicked");
}
// Slider — mutates slider_val in place
d.gui_slider(
Rectangle::new(20.0, 110.0, 200.0, 20.0),
"Min",
"Max",
&mut slider_val,
0.0,
1.0,
);
}
}
Gotchas
- String parameters are
impl AsRef<str>. Pass&str,String, or any type implementingAsRef<str>. Under the hood, each call writes the string to a thread-localCStringscratch buffer — safe and zero-alloc per frame. - GUI state is global. raygui is immediate-mode; all style and state
(
gui_set_state,gui_load_style) affects every control drawn this frame. Call ordering matters. gui_load_style_from_memoryis absent from the vendored raygui header (backlog #296 — deferred until the vendored raygui advances).rayguifeature required. Withoutfeatures = ["raygui"]the trait methods are not compiled in.
See also
- Window and drawing — the frame loop that hosts GUI calls.
- Strings and allocations — why raygui uses
impl AsRef<str>. RaylibGuiControlsdocs.rs
Showcase examples
Showcase examples that exercise this module: