-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.h
More file actions
317 lines (268 loc) · 13.3 KB
/
widgets.h
File metadata and controls
317 lines (268 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
MIT License
Copyright (c) 2022-2026 Kim Kulling
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include "tinyui.h"
namespace tinyui {
// Forward declarations -------------------------------------------------------
struct Context;
/// @brief This enum is used to descripe the widget type-
enum class WidgetType {
Invalid = -1, ///< Not initialized
Container = 0, ///< A container widget
Button, ///< A button widget
Label, ///< A label widget
InputField, ///< An input field widget
Panel, ///< A panel widget
Box, ///< A box widget
ImageBox, ///< An image box widget
TreeView, ///< A treeview widget
ProgressBar, ///< A status bar widget
Count ///< The number of widgets
};
/// @brief This enum is used to descripe the alignment of a widget.
enum class LayoutPolicy {
Invalid = -1, ///< Not initialized
Absolute, ///< Absolute layout
Relative, ///< Relative layout
Count ///< The number of layouts
};
struct FilledState {
uint32_t filledState{0}; ///< The filled state in percent (0-100)
};
/// @brief This enum is used to describe the alignment of a widget.
enum class WidgetStyle {
Invalid = -1,
BorderStyle, ///< The widget has a border
Count
};
/// @brief This enum is used to descripe the alignment of a widget.
using WidgetArray = std::vector<Widget*>;
/// @brief This struct contains all the data which is needed to describe a widget.
struct Widget {
Id mId{0}; ///< The unique id of the widget
WidgetType mType{WidgetType::Invalid}; ///< The type of the widget
Widget *mParent{nullptr}; ///< The parent widget
bool mEnabled{true}; ///< The enabled state of the widget
Rect mRect{}; ///< The rectangle of the widget
bool mFilledRect{true}; ///< The filled rectangle state
uint32_t mStyles{0u}; ///< The style of the widget
Alignment mAlignment{Alignment::Left}; ///< The alignment of the widget
std::string mText{}; ///< The label text
Image *mImage{nullptr}; ///< The image of the widget
WidgetArray mChildren{}; ///< The children of the widget
CallbackI *mCallback{nullptr}; ///< The callback of the widget
uint8_t *mContent{nullptr}; ///< The content of the widget
uint32_t mIntention{0}; ///< The interaction intention.
/// @brief The default class constructor.
Widget() = default;
/// @brief The class destructor.
~Widget() {
if (mContent != nullptr) {
delete [] mContent;
}
if (mCallback != nullptr) {
mCallback->decRef();
}
}
/// @brief Check if the widget has a specific style.
/// @param style The style to check.
/// @return true if the widget has the style, false if not.
bool hasStyle(WidgetStyle style) const {
return mStyles & static_cast<uint32_t>(style);
}
/// @brief Set the style of the widget.
/// @param style The style to set.
void setStyle(WidgetStyle style) {
mStyles = mStyles | static_cast<uint32_t>(style);
}
/// @brief Enables the widget
void enable() {
mEnabled = true;
}
/// @brief Disables the widget.
void disable() {
mEnabled = false;
}
/// @brief Check if the widget is enabled.
/// @return true if the widget is enabled, false if not.
bool isEnabled() const {
return mEnabled;
}
Widget(const Widget &) = delete;
Widget &operator=(const Widget &) = delete;
};
/// @brief The widgets access wrapper class.
/// This class is used to create and manage widgets.
struct Widgets {
/// @brief The root item id.
static constexpr Id RootItem = 0;
/// @brief The default class constructor.
Widgets() = default;
/// @brief The class destructor.
~Widgets() = default;
/// @brief Create a new widget from the type container.
/// @param ctx The context to create the widget in.
/// @param id The unique id of the widget.
/// @param parentId The parent id of the widget.
/// @param text The text of the widget.
/// @param rect The rect of the widget.
/// @return ResultOk if the widget was created, ErrorCode if not.
static ret_code container(Id id, Id parentId, const char *text, const Rect &rect);
/// @brief Create a new widget from the type box.
/// @param ctx The context to create the widget in.
/// @param id The unique id of the widget.
/// @param parentId The parent id of the widget.
/// @param rect The rect of the widget.
/// @param filled The filled state of the widget.
/// @return ResultOk if the widget was created, ErrorCode if not.
static ret_code box(Id id, Id parentId, const Rect &rect, bool filled);
static ret_code imageBox(Id id, Id parentId, const char *image, const Rect &rect, bool filled);
/// @brief Will look for a widget by its id.
/// @param id The id of the widget to look for.
/// @param root The root widget to start the search.
/// @return The found widget or nullptr if not found.
static Widget *findWidget(Id id, Widget *root);
/// @brief Will look for a widget by its id.
/// @param x The x-coordinate of the point.
/// @param y The y-coordinate of the point.
/// @param root The root widget to start the search.
/// @param found The found widget.
static void findSelectedWidget(int x, int y, Widget *currentChild, Widget **found);
/// @brief Creates a new widget from the type label.
/// @param ctx The context to create the widget in.
/// @param id The unique id of the widget.
/// @param parentId The parent id of the widget.
/// @param text The text of the widget.
/// @param rect The rect of the widget.
/// @param alignment The alignment of the widget.
/// @return ResultOk if the widget was created, ErrorCode if not.
static ret_code label(Id id, Id parentId, const char *text, const Rect &rect, Alignment alignment);
/// @brief Creates a new widget from the type textfield.
/// @param ctx The context to create the widget in.
/// @param id The unique id of the widget.
/// @param parentId The parent id of the widget.
/// @param rect The rect of the widget.
/// @param alignment The alignment of the widget.
/// @return ResultOk if the widget was created, ErrorCode if not.
static ret_code inputText(Id id, Id parentId, const Rect &rect, Alignment alignment);
/// @brief Creates a new text button.
/// @param ctx The context to create the widget in.
/// @param id The unique id of the widget.
/// @param parentId The parent id of the widget.
/// @param text The text of the widget.
/// @param rect The rect of the widget.
/// @param callback The callback for the widget.
/// @return ResultOk if the widget was created, ErrorCode if not.
static ret_code button(Id id, Id parentId, const char *text, const Rect &rect, CallbackI *callback);
/// @brief Creates a new image button.
/// @param ctx The context to create the widget in.
/// @param id The unique id of the widget.
/// @param parentId The parent id of the widget.
/// @param image The image of the widget.
/// @param rect The rect of the widget.
/// @param callback The callback for the widget.
/// @return ResultOk if the widget was created, ErrorCode if not.
static ret_code imageButton(Id id, Id parentId, const char *image, const Rect &rect, CallbackI *callback);
/// @brief Creates a new widget from the type panel.
/// @param ctx The context to create the widget in.
/// @param id The unique id of the widget.
/// @param parentId The parent id of the widget.
/// @param title The title of the widget.
/// @param rect The rect of the widget.
/// @param callback The callback of the widget.
/// @return ResultOk if the widget was created, ErrorCode if not.
static ret_code panel(Id id, Id parentId, const char *title, const Rect &rect, CallbackI *callback);
/// @brief Creates a new widget from the type treeview.
/// @param ctx The context to create the widget in.
/// @param id The unique id of the widget.
/// @param parentId The parent id of the widget.
/// @param title The title of the widget.
/// @param rect The rect of the widget.
/// @param callback The callback of the widget.
/// @return ResultOk if the widget was created, ErrorCode if not.
static ret_code treeView(Id id, Id parentId, const char *title, const Rect &rect);
/// @brief Creates a new tree item.
/// @param id The unique id of the tree item.
/// @param parentItemId The parent item id of the tree item.
/// @param title The title of the tree item.
static ret_code treeItem(Id id, Id parentItemId, const char *title);
/// @brief Creates a new widget from the type status bar.
/// @param ctx The context to create the widget in.
/// @param id The unique id of the widget.
/// @param parentId The parent id of the widget.
/// @param title The title of the widget.
/// @param rect The rect of the widget.
/// @param fillRate The fill rate of the widget.
/// @param callback The callback of the widget.
/// @return ResultOk if the widget was created, ErrorCode if not.
static ret_code progressBar(Id id, Id parentId, const Rect &rect, int fillRate, CallbackI *callback);
/// @brief Will render all widgets.
/// @param ctx The context to render the widgets in.
static void renderWidgets();
/// @brief The on-mouse-button-down event handler.
/// @param ctx The context to handle the event in.
/// @param x The x-coordinate of the mouse.
/// @param y The y-coordinate of the mouse.
/// @param eventType The event type.
/// @param state The mouse state.
static void onMouseButton(int x, int y, int eventType, MouseState state);
/// @brief The on-mouse-move event handler.
/// @param ctx The context to handle the event in.
/// @param x The x-coordinate of the mouse.
/// @param y The y-coordinate of the mouse.
/// @param eventType The event type.
/// @param state The mouse state.
static void onMouseMove(int x, int y, int eventType, MouseState state);
/// @brief The on-key event handler.
/// @param ctx The context to handle the event in.
/// @param key The key to handle.
/// @param isDown The key state.
static void onKey(const char *key, bool isDown);
/// @brief Will clear all widgets.
static void clear();
/// @brief Will clear a widget by its id.
/// @param id The id of the widget to clear.
/// @param recursive If true, all child widgets will also be cleared.
/// @return true if the widget was cleared, false if not.
static bool clearItem(Id id, bool recursive);
/// @brief The widget enabler
/// @param ctx The context to enable the widget in.
/// @param id The id of the widget to enable.
/// @param enabled The enabled state of the widget.
static void setEnableState(Id id, bool enabled);
/// @brief Will return true if the widget is enabled.
/// @param ctx The context to check the widget in.
/// @param id The id of the widget to check.
/// @return true if the widget is enabled, false if not.
static bool isEnabled(Id id);
/// @brief Will set the focus to the widget by its id.
/// @param ctx The context to set the focus in.
/// @param id The id of the widget to set the focus to.
/// @return ResultOk if the focus was set, ErrorCode if not.
static ret_code setFocus(Id id);
/// @brief Will return the widget by its id.
/// @param ctx The context to get the widget from.
/// @param id The id of the widget to get.
/// @return The widget or nullptr if not found.
static Widget *getWidgetById(Id id);
static bool beginChild();
static bool endChild();
};
} // namespace tinyui