Skip to content

UI

Retained-mode UI widget system with theming, focus navigation, and input handling.

Header: #include <asw/modules/ui/ui.h>Namespace: asw::ui

Widget

Base class for all UI widgets. Provides identity, visibility, focus, parent/child tree management, and virtual methods for layout, event handling, and drawing.

cpp
class Widget;

Fields

FieldTypeDefaultDescription
visiblebooltrueWhether the widget is visible
enabledbooltrueWhether the widget is enabled
focusableboolfalseWhether the widget can receive focus
parentWidget*nullptrPointer to the parent widget
childrenstd::vector<std::unique_ptr<Widget>>Child widgets
transformasw::Quad<float>Position and size

Methods

id

cpp
WidgetId id() const;

Get the unique identifier for this widget (WidgetId is uint32_t).

layout

cpp
virtual void layout(Context& ctx);

Lay out this widget and its children.

on_event

cpp
virtual bool on_event(Context& ctx, const UIEvent& e);

Handle a UI event. Returns true if the event was consumed.

on_focus_changed

cpp
virtual void on_focus_changed(Context& ctx, bool focused);

Called when focus state changes.

draw

cpp
virtual void draw(Context& ctx);

Draw this widget and its children.

add_child

cpp
template <class T, class... Args>
T& add_child(Args&&... args);

Add a child widget of type T (must derive from Widget). Returns a reference to the new child.

UIEvent

Event structure for UI interactions.

cpp
struct UIEvent;

Event Types

cpp
enum class Type {
  KeyDown, KeyUp, TextInput,
  PointerDown, PointerUp, PointerMove,
  PointerEnter, PointerLeave,
  Activate, Back
};

Fields

FieldTypeDescription
typeTypeThe event type
keyasw::input::KeyKey associated with keyboard events
shiftboolWhether shift is held
pointer_idintPointer identifier
pointer_posasw::Vec2<float>Pointer position
mouse_buttonasw::input::MouseButtonMouse button for pointer events
textstd::stringText for TextInput events

Theme

Visual style configuration for all UI elements.

cpp
struct Theme;
FieldTypeDefaultDescription
textasw::ColorwhiteDefault text color
text_dimasw::Colorlight grayDimmed text color
panel_bgasw::Colordark grayPanel background
btn_bgasw::Colormedium grayButton background
btn_hoverasw::Colorlighter grayButton hover color
btn_pressedasw::Coloreven lighterButton pressed color
btn_focus_ringasw::ColorgoldFocus ring color
input_bgasw::Colorvery darkInput box background
paddingfloat10.0Default padding
gapfloat8.0Default gap between elements
show_focusboolfalseShow focus rings

Context

Shared state for the UI system.

cpp
class Context;
FieldTypeDescription
themeThemeThe current UI theme
focusFocusManagerFocus navigation manager
pointer_captureWidget*Widget that has captured pointer input
hoverWidget*Widget currently being hovered
pointer_downboolWhether the pointer is currently down
need_focus_rebuildboolWhether the focus list needs rebuilding

FocusManager

Manages keyboard and directional focus navigation.

cpp
class FocusManager;
MethodDescription
rebuild(Context& ctx, Widget& root)Rebuild the focusable widget list from the tree
focused()Get the currently focused widget (or nullptr)
set_focus(Context& ctx, Widget* w)Set focus to a specific widget
focus_next(Context& ctx)Move focus to the next widget
focus_prev(Context& ctx)Move focus to the previous widget
focus_dir(Context& ctx, int dx, int dy)Move focus in a 2D direction

Root

Top-level manager that owns the UI tree, dispatches input, and renders.

cpp
class Root;

Fields

FieldTypeDescription
ctxContextThe UI context
rootPanelThe root panel widget

Methods

MethodDescription
set_size(float w, float h)Set the size of the root panel
hit_test(Widget& w, const Vec2<float>& pos)Find the deepest widget at a position
dispatch_pointer(const UIEvent& e)Route a pointer event to the appropriate widget
dispatch_to_focused(const UIEvent& e)Dispatch an event to the focused widget
update()Process input and dispatch UI events
draw()Draw the UI tree

Widgets

Panel

A container widget with an optional background color or image.

cpp
class Panel : public Widget;
FieldTypeDefaultDescription
bgasw::ColortransparentBackground color
bg_imageasw::TexturenullptrBackground image texture

VBox

A vertical box layout container that arranges children top-to-bottom.

cpp
class VBox : public Widget;
FieldTypeDefaultDescription
gapfloat8.0Gap between child elements
paddingfloat10.0Padding around the content

Label

A non-focusable text display widget.

cpp
class Label : public Widget;
FieldTypeDefaultDescription
fontasw::FontFont for rendering
textstd::stringText to display
justifyasw::TextJustifyLeftText justification
colorasw::ColorText color

Button

An interactive button widget (focusable).

cpp
class Button : public Widget;
FieldTypeDescription
on_clickstd::function<void()>Callback invoked on click
fontasw::FontFont for the button text
textstd::stringButton label text

InputBox

A text input widget (focusable).

cpp
class InputBox : public Widget;
FieldTypeDescription
on_changestd::function<void(const std::string&)>Callback invoked when the value changes
fontasw::FontFont for the input text
valuestd::stringCurrent text value
placeholderstd::stringPlaceholder text shown when empty

Example

cpp
#include <asw/asw.h>
#include <asw/modules/ui/ui.h>

asw::core::init(640, 480, 2);
auto font = asw::assets::load_font("font.ttf", 16);

asw::ui::Root ui;
ui.set_size(640, 480);

// Customize theme
ui.ctx.theme.btn_bg = {60, 60, 80, 255};
ui.ctx.theme.padding = 12.0f;

// Build widget tree
auto& layout = ui.root.add_child<asw::ui::VBox>();
layout.transform = {10, 10, 300, 400};

auto& title = layout.add_child<asw::ui::Label>();
title.font = font;
title.text = "Settings";
title.color = asw::color::white;

auto& btn = layout.add_child<asw::ui::Button>();
btn.font = font;
btn.text = "Start Game";
btn.on_click = []() {
  // handle click
};

auto& input = layout.add_child<asw::ui::InputBox>();
input.font = font;
input.placeholder = "Enter name...";
input.on_change = [](const std::string& value) {
  // handle text change
};

// In game loop
while (!asw::core::exit) {
  asw::core::update();
  ui.update();

  asw::display::clear();
  ui.draw();
  asw::display::present();
}

Released under the MIT License.