Immediate mode¶
This page matches the current implementation in the FlashBang repo: global ImmediateContext, set_immediate_context per frame, and widget procs in package widgets exposed as flashbang.*.
Per-frame sequence¶
- Clear or rebuild your
DrawListfor the frame (seedraw_list_clear/ init patterns in your app). - Feed input into the context after
set_immediate_contextresets per-frame buffers: - Pointer: you pass
mouse_x,mouse_y,mouse_downintoset_immediate_context. - Text:
flashbang.push_text_inputwith UTF-8 bytes for the frame. - Keys:
flashbang.push_key_eventfor each key up/down you care about. - Scroll:
flashbang.push_scroll_event(widgets readscroll_delta_*from context). - Optional:
set_shift_held,set_middle_mouse,set_click_countwhen your platform provides them. - Call
flashbang.set_immediate_context(ctx_draw_list, text_backend, mouse_x, mouse_y, mouse_down)once per frame before drawing widgets. This resets text/key scroll accumulators, widget dimension map, layout rect cache for the frame, theme stack depth, etc., and ensures a default theme exists on first use. - Optional startup-only (not each frame unless backends change):
set_platform_backend,set_layout_backend,set_image_backend,set_theme. - Call
flashbang.get_imm_ctx()to obtain^ImmediateContextand pass it to widgets:flashbang.Button(ctx, config),flashbang.Label(ctx, config), … - Submit your
DrawListthrough your render backend.
What ImmediateContext carries¶
Defined in src/widgets/button.odin (same compilation unit as early immediate-mode widgets):
draw_list,text_backend— required for almost all widgets.platform_backend,layout_backend,image_backend— optional; set viaset_*helpers.- Pointer state:
mouse_x,mouse_y,mouse_down, scroll deltas,mouse_click_count, etc. - Coordinate origin:
origin_x,origin_y— container widgets (e.g. window) adjust these so children use local coordinates. - Last sizes:
last_widget_w,last_widget_h, and for containerslast_content_w,last_content_h— used byGetWidth/GetHeight/GetContentWidth/GetContentHeighthelpers. - Input buffers: raw text bytes and key events for the frame (consumed by text widgets).
- Focus:
focused_id— hashed widget id; useset_focus/ widget-specific helpers as documented per widget. - Theme:
global_themeplus a small per-frame stack (push_theme/pop_themereset each frame afterset_immediate_context).
Click detection (example: button)¶
ButtonConfig includes was_active: bool. You must store ButtonState.active from the previous frame and pass it back as was_active next frame. clicked is true when the pointer was down on the widget last frame, released this frame, while still hovering—see check_click in src/widgets/button.odin.
Widget IDs and layout overrides¶
If id in a config is non-empty, the widget stores dimensions for that id (hashed) so flashbang.GetWidth("id") / GetHeight("id") work. The same hash keys the per-frame layout rect cache populated by an external layout backend, so apply_layout_override can replace x,y,w,h before drawing.
Z order¶
Use push_z_layer / pop_z_layer (re-exported on flashbang) to bracket draw sections so later ops sort above earlier ones when your backend respects z_index.