ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
widget.h
Go to the documentation of this file.
1
8
9#ifndef ASW_MODULES_UI_WIDGET_H
10#define ASW_MODULES_UI_WIDGET_H
11
12#include <memory>
13#include <type_traits>
14#include <vector>
15
16#include "event.h"
17
18namespace asw::ui {
19
20class Context;
21
24using WidgetId = uint32_t;
25
28class Widget {
29public:
33 : _id(generate_id()) { };
34
37 virtual ~Widget() = default;
38
41 Widget(Widget&&) = default;
42
45 Widget& operator=(Widget&&) = default;
46
47 Widget(const Widget&) = delete;
48 Widget& operator=(const Widget&) = delete;
49
54 WidgetId id() const
55 {
56 return _id;
57 }
58
60 bool visible = true;
61
63 bool enabled = true;
64
66 bool focusable = false;
67
69 Widget* parent = nullptr;
70
72 std::vector<std::unique_ptr<Widget>> children;
73
78 virtual void layout(Context& ctx);
79
86 virtual bool on_event(Context& ctx, const UIEvent& e);
87
93 virtual void on_focus_changed(Context& ctx, bool focused);
94
99 virtual void draw(Context& ctx);
100
108 template <class T, class... Args> T& add_child(Args&&... args)
109 {
110 static_assert(std::is_base_of_v<Widget, T>, "add_child<T>: T must derive from Widget");
111 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
112 ptr->parent = this;
113 auto& ref = *ptr;
114 children.emplace_back(std::move(ptr));
115 return ref;
116 }
117
120
122 bool is_hovered() const { return _hovered; }
123
125 bool is_pressed() const { return _pressed; }
126
128 bool is_focused() const { return _focused; }
129
130protected:
131 bool _hovered = false;
132 bool _pressed = false;
133 bool _focused = false;
134
135private:
136 static inline int _id_counter { 1 };
137
138 static int generate_id()
139 {
140 return _id_counter++;
141 }
142
144};
145
146} // namespace asw::ui
147
148#endif // ASW_MODULES_UI_WIDGET_H
A 2D rectangle in space.
Definition geometry.h:433
Shared state for the UI system.
Definition context.h:75
Base class for all UI widgets.
Definition widget.h:28
asw::Quad< float > transform
The transform (position and size) of the widget.
Definition widget.h:119
static int _id_counter
Definition widget.h:136
static int generate_id()
Definition widget.h:138
Widget(const Widget &)=delete
bool is_pressed() const
Whether this widget is currently being pressed.
Definition widget.h:125
bool is_hovered() const
Whether the pointer is currently over this widget.
Definition widget.h:122
virtual void on_focus_changed(Context &ctx, bool focused)
Called when focus state changes.
Definition widget.cpp:20
bool is_focused() const
Whether this widget currently holds focus.
Definition widget.h:128
bool enabled
Whether the widget is enabled.
Definition widget.h:63
virtual ~Widget()=default
Default virtual destructor.
T & add_child(Args &&... args)
Add a child widget.
Definition widget.h:108
WidgetId id() const
Get the unique identifier for this widget.
Definition widget.h:54
virtual bool on_event(Context &ctx, const UIEvent &e)
Handle a UI event.
Definition widget.cpp:13
virtual void draw(Context &ctx)
Draw this widget and its children.
Definition widget.cpp:26
Widget & operator=(const Widget &)=delete
bool focusable
Whether the widget can receive focus.
Definition widget.h:66
Widget & operator=(Widget &&)=default
Move assignment operator.
std::vector< std::unique_ptr< Widget > > children
Child widgets.
Definition widget.h:72
bool visible
Whether the widget is visible.
Definition widget.h:60
virtual void layout(Context &ctx)
Lay out this widget and its children.
Definition widget.cpp:5
Widget * parent
Pointer to the parent widget.
Definition widget.h:69
Widget()
Default constructor for Widget.
Definition widget.h:32
WidgetId _id
Definition widget.h:143
Widget(Widget &&)=default
Move constructor.
UI event types for the ASW UI module.
uint32_t WidgetId
Unique identifier type for widgets.
Definition widget.h:24
Event structure for UI interactions.
Definition event.h:21