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
121private:
122 static inline int id_counter_ { 1 };
123
124 static int generate_id()
125 {
126 return id_counter_++;
127 }
128
130};
131
132} // namespace asw::ui
133
134#endif // ASW_MODULES_UI_WIDGET_H
A 2D rectangle in space.
Definition geometry.h:388
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 generate_id()
Definition widget.h:124
Widget(const Widget &)=delete
virtual void on_focus_changed(Context &ctx, bool focused)
Called when focus state changes.
Definition widget.cpp:20
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
WidgetId id_
Definition widget.h:129
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
static int id_counter_
Definition widget.h:122
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
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