IMGUI
IMGUI
IMGUI
void init()
{
ws = new WidgetSet();
ws->add(create_button(ID_BUTTON, "Click me", ...));
ws->add(create_slider_float(ID_SLIDER, "Slide me", 0.f, 1.f, ...));
ws->set_float_value(ID_SLIDER, my_float);
ws->set_callback(&callback_ws);
}
void callback_ws(int widget_id)
{
switch(widget_id)
{
case ID_BUTTON: do_action(); break;
case ID_SLIDER: my_float = ws->get_float_value(ID_SLIDER); break;
}
}
void main()
{
init();
while(running)
ws->draw();
}
Immediate Mode GUI
• Casey Muratori stumbled upon IMGUI around
2002 while working at RAD Game Tools.
void main()
{
GUIState_t* state;
float my_float = 0.5f;
while(running)
{
update(state, ...);
draw_gui(state, &my_float);
}
}
IMGUI traits
• No data synchronization needed, all your data
is stored in the application.
• Stores very little state in the library
• Uses procedural function-calls as ”widgets”.
• Code-driven
• Centralized flow control
• Easy to dive in to, easy to extend.
• Must be redrawn every frame
Simple IMGUI implementation
• Basic concepts
– A widget is concidered ”hot” if there is a
possibility that it will be interacted with.
– An ”active” widget is currently engaged by the
user.
– Widget ID’s are used for the library to keep track
of hot and active widgets.
Simple IMGUI implementation contd.
struct GUIState_t bool do_button(GUIState_t* state, void* id, Rect_t* rect, const char* text, ...)
{ {
void* hot_item; bool result = false;
void* active_item; bool inside = mouse_inside_rect(state, rect);
int mouse_x, mouse_y; if (inside)
unsigned mouse_buttons; state->hot_item = id;
};
if(state->active_item == id && !is_button_down(1)
struct Rect_t {
{ if (inside)
float x, y, w, h; {
}; result = true;
bool is_button_down(GUIState_t* state, unsigned button) state->hot_item = id;
{ }
return (state->mouse_buttons >> button-1) & 1; else
} state->hot_item = 0;
state->active_item = 0;
bool mouse_inside_rect(GUIState_t* state, Rect_t* rect) }
{ /* point-in-rect-test */ } else if (state->hot_item == id)
{
void update(GUIState_t* state, unsigned mb, if (is_button_down(1))
int mx, int my) {
{ state->focused_item = id;
state->mouse_x = mx; state->active_item = id;
state->mouse_y = my; }
state->mouse_buttons = mb; }
} draw_button(rect, text, …);
return result;
}
Demo-time
IMGUI in Teeworlds
Unity
What’s next?
• Partitioned IMGUI (”PIMGUI”)
Sources & more info
• ”Immediate Mode GUIs” written by Sean
Barret for GDM Sept. 2005.
• Introduction video by Casey Muratori
• The Molly Rocket forums
• Book in progress by Johannes Norneby
• Tutorial by Jari Komppa
• IMGUI implementation by nVidia
• Zero Memory Widgets
Questions?