Crates.io | raylib_interactive |
lib.rs | raylib_interactive |
version | 0.1.6 |
created_at | 2024-12-23 19:42:32.797872+00 |
updated_at | 2025-07-22 17:25:50.319138+00 |
description | An interactive library for Raylib |
homepage | https://github.com/OrtheSnowJames/rayinteract |
repository | |
max_upload_size | |
id | 1493358 |
size | 180,941 |
This is a library that supports rust and adds many interactive things to your current raylib project, such as:
Raylib Interactive is a library built on raylib that adds many components to raylib, allowing you to make better graphical interfaces. The latest version:
This project is hosted on github at the GitHub homepage.
The library now has a Style
struct that provides theming across all components:
pub struct Style {
// Background colors
pub background_color: Color,
pub hover_color: Color,
pub pressed_color: Color,
pub active_color: Color,
pub disabled_color: Color,
// Border colors
pub border_color: Color,
pub border_color_hover: Color,
pub border_color_pressed: Color,
pub border_color_active: Color,
// Text colors
pub text_color: Color,
pub text_color_hover: Color,
pub text_color_pressed: Color,
pub text_color_disabled: Color,
// Special colors for specific components
pub check_color: Color, // For checkboxes
pub placeholder_color: Color, // For text fields
// Typography
pub font_size: i32,
// Layout
pub padding: f32,
pub corner_radius: f32,
pub border_thickness: f32,
}
Style::default()
- Default gray themeStyle::modern_blue()
- Modern blue accent themeStyle::dark_theme()
- Dark theme with blue accentsStyle::minimal()
- Clean minimal themeStyle::new()
- Create custom styles with builder patternpub struct Button {
pub bounds: Rectangle, // Position and size
pub label: String, // Button text
pub style: Style, // Visual styling
pub is_hovered: bool, // Mouse hover state
pub is_pressed: bool, // Mouse press state
pub animation_progress: f32, // Animation state (0.0-1.0)
pub enabled: bool, // Whether button is interactive
}
Methods:
new(x, y, width, height, label) -> Self
// Constructorwith_style(style) -> Self
// Apply styleset_colors(background, hover, pressed, border, text)
// Quick color setupupdate(rl)
// Handle input and animationsdraw(d)
// Render the buttonis_clicked(rl) -> bool
// Check if clicked this framepub struct Checkbox {
pub bounds: Rectangle, // Position and size
pub is_checked: bool, // Checked state
pub style: Style, // Visual styling
pub label: String, // Label text
pub is_hovered: bool, // Mouse hover state
pub animation_progress: f32, // Check animation (0.0-1.0)
pub is_clicked: bool, // Click state
}
Methods:
new(x, y, size, label) -> Self
// Constructorwith_style(style) -> Self
// Apply styleset_colors(background, check, border, hover, label)
// Quick color setupupdate(rl)
// Handle input and animationsdraw(d)
// Render the checkboxtoggle()
// Toggle checked statepub struct TextField {
pub bounds: Rectangle, // Position and size
pub text: String, // Current text content
pub placeholder: String, // Placeholder text
pub max_length: usize, // Maximum characters
pub style: Style, // Visual styling
pub is_active: bool, // Has focus
pub cursor_position: usize, // Text cursor position
pub cursor_blink_timer: f32, // Cursor blink animation
pub backspace_hold_timer: f32, // Backspace repeat timing
pub arrow_hold_timer: f32, // Arrow key repeat timing
pub scroll_offset: usize, // Horizontal scroll offset
pub is_scrolling: bool, // Scroll bar drag state
pub allowed_pattern: Option<Regex>, // Regex for allowed characters
pub character_callback: Option<Box<dyn Fn(char) -> char>>, // Character transform callback
}
Methods:
new(x, y, width, height, max_length) -> Self
// Constructorwith_style(style) -> Self
// Apply styleset_colors(background, border, text)
// Quick color setupupdate(rl)
// Handle input and focusdraw(d)
// Render the text fieldclear()
// Clear all textactivate()
// Give focusdeactivate()
// Remove focushandle_input(rl)
// Process keyboard inputonly_allow(regex: Regex) -> Self
// Only allow characters matching regexchange_character(callback: Fn(char) -> char) -> Self
// Transform each character as typedSpecial:
.only_allow()
to restrict allowed characters (e.g., no spaces)..change_character()
to mask or transform input (e.g., password fields).Password Field Example:
use regex::Regex;
let mut password_field = TextField::new(0.0, 0.0, 200.0, 30.0, 20)
.only_allow(Regex::new("^[^ ]$").unwrap()) // Disallow spaces
.change_character(|_| '*'); // Mask all input as '*'
pub struct Dropdown {
pub bounds: Rectangle, // Position and size
pub items: Vec<String>, // Available options
pub selected_index: Option<usize>, // Currently selected item
pub is_open: bool, // Dropdown expanded state
pub style: Style, // Visual styling
pub hover_index: Option<usize>, // Hovered item index
pub max_visible_items: usize, // Max items shown when open
pub scroll_offset: usize, // Scroll position for long lists
}
Methods:
new(x, y, width, height, items) -> Self
// Constructorwith_style(style) -> Self
// Apply stylewith_deselect_option(label) -> Self
// Add a deselect/clear option as the first itemset_colors(background, border, text, hover)
// Quick color setupupdate(rl)
// Handle input and selectiondraw(d)
// Render the dropdownget_selected_item() -> Option<&String>
// Get selected textadd_item(item)
// Add new optionremove_item(index)
// Remove optionclear_items()
// Remove all optionsclear_selection()
// Clear selectedopen()
// Expand dropdownclose()
// Collapse dropdowntoggle()
// Toggle open/closedSpecial:
Esc
while open will clear the selection.The library provides convenient style presets for common use cases:
presets::button_default()
- Default button stylepresets::button_primary()
- Primary action button (blue)presets::button_secondary()
- Secondary action button (gray)presets::button_success()
- Success action button (green)presets::button_danger()
- Danger action button (red)presets::textfield_default()
- Default text field stylepresets::checkbox_default()
- Default checkbox stylepresets::dropdown_default()
- Default dropdown styleThe Style
struct provides builder methods for easy customization:
with_background_colors(background, hover, pressed)
// Set background colorswith_border_colors(border, hover, pressed)
// Set border colorswith_text_colors(text, hover, pressed)
// Set text colorswith_typography(font_size)
// Set font sizewith_layout(padding, corner_radius, border_thickness)
// Set layout propertiesYou can update and draw multiple UI elements at once using the provided macros:
raylib_interactive::update_all!(
virtual_mouse, // The mouse position in virtual (UI) coordinates, useful for drawing to off-screen render targets
&mut rl, // The Raylib handle
button,
checkbox,
text_field,
dropdown,
custom_button,
);
raylib_interactive::draw_all!(
&mut d,
button,
checkbox,
text_field,
dropdown,
custom_button,
);
If you are rendering to a virtual resolution (e.g., with a render texture for scaling/aspect ratio):
let mouse = rl.get_mouse_position();
let virtual_mouse = Vector2 {
x: ((mouse.x - offset_x as f32) / scale).clamp(0.0, VIRTUAL_WIDTH as f32),
y: ((mouse.y - offset_y as f32) / scale).clamp(0.0, VIRTUAL_HEIGHT as f32),
};
raylib_interactive::update_all!(
virtual_mouse,
&mut rl,
button,
checkbox,
// ...
);
.with_deselect_option("None")
, the first item is a clear/deselect option. The selected index and get_selected_item()
are offset accordingly, so you always get the correct item or None
if deselected.use raylib::prelude::*;
use raylib_interactive::{Button, TextField, Checkbox, Dropdown, Style, presets};
fn main() {
let (mut rl, thread) = raylib::init()
.size(800, 600)
.title("Raylib Interactive Demo")
.build();
// Create components with different styles
let mut button = Button::new(350.0, 200.0, 100.0, 40.0, "Click Me!")
.with_style(presets::button_primary());
let mut checkbox = Checkbox::new(350.0, 300.0, 20.0, "Enable Feature")
.with_style(presets::checkbox_default());
let mut text_field = TextField::new(300.0, 400.0, 200.0, 30.0, 50)
.with_style(presets::textfield_default());
text_field.placeholder = "Enter text...".to_string();
let mut dropdown = Dropdown::new(300.0, 450.0, 200.0, 30.0,
vec!["Option 1".to_string(), "Option 2".to_string(), "Option 3".to_string()])
.with_deselect_option("None")
.with_style(presets::dropdown_default());
// Create custom style
let custom_style = Style::new(
Color::new(255, 200, 100, 255), // Orange background
Color::new(255, 220, 120, 255), // Light orange hover
Color::new(200, 150, 50, 255), // Dark orange pressed
Color::new(150, 100, 0, 255), // Dark orange border
Color::new(50, 25, 0, 255), // Dark brown text
).with_typography(18)
.with_layout(10.0, 8.0, 3.0);
let mut custom_button = Button::new(350.0, 500.0, 100.0, 40.0, "Custom")
.with_style(custom_style);
while !rl.window_should_close() {
// Get mouse position for UI interaction
let mouse = rl.get_mouse_position();
// Update all components
raylib_interactive::update_all!(
mouse,
&mut rl,
button,
checkbox,
text_field,
dropdown,
custom_button,
);
// Handle interactions
if button.is_clicked(&rl) {
println!("Button clicked!");
}
if checkbox.is_checked {
println!("Checkbox checked!");
}
if let Some(selected) = dropdown.get_selected_item() {
println!("Selected: {}", selected);
} else {
println!("Dropdown selection cleared");
}
// Direct field access examples
button.label = "Updated!".to_string();
text_field.text = "Hello World".to_string();
checkbox.is_checked = true;
dropdown.is_open = true;
let mut d = rl.begin_drawing(&thread);
d.clear_background(Color::WHITE);
// Draw all components
raylib_interactive::draw_all!(
&mut d,
button,
checkbox,
text_field,
dropdown,
custom_button,
);
}
}