gigawidgets 0.3.55
A wxWidgets-style UI library for the Arduino Giga Display Shield.
 
Loading...
Searching...
No Matches
widget.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <ctime>
5#include <functional>
6
7#include "alignment.hpp"
8#include "bounds.hpp"
9#include "coords.hpp"
10#include "event.hpp"
11#include "event_handlers.hpp"
12#include "id.hpp"
13#include "padding.hpp"
14#include "position.hpp"
15#include "size.hpp"
16
17#ifndef ONHOLD_EVENT_DEBOUNCE
18// The number of milliseconds to wait after pressing a widget before the "onhold" event gets sent.
19#define ONHOLD_EVENT_DEBOUNCE 200
20#endif
21
22namespace ui {
23
27class Widget : public EventHandlers<Widget> {
29 Widget *parent;
30
32 std::function<void(Widget &element, const Event &event)> callbackPress;
34 std::function<void(Widget &element, const Event &event, time_t time)> callbackHold;
36 std::function<void(Widget &element, const Event &event)> callbackRelease;
38 std::function<void(Widget &element, const Event &event)> callbackBlur;
39
41 bool initialRender;
43 Position pos;
45 Alignment align;
47 bool pressed;
48
49protected:
54
55public:
58
64
71 Widget(const Position &pos = {0, 0}, const Alignment &align = {ALIGN_LEFT, ALIGN_TOP}, const Padding &padding = {0});
72
79 virtual void draw() const = 0;
80
89 virtual void render(bool force) const;
90
94 virtual ~Widget();
95
100 virtual void update(time_t time);
101
107 virtual Size size() const = 0;
108
114 virtual Bounds bounds() const;
115
120 inline void setPosition(const Position &pos) {
121 this->pos = pos;
123 }
124
129 inline Position getPosition() const {
130 return pos;
131 }
132
137 inline void setAlign(const Alignment &align) {
138 this->align = align;
140 }
141
149 inline void requestRedraw() {
150 redrawSelf = true;
151 }
152
159 inline void requestParentRedraw() {
160 if (parent) {
161 parent->requestRedraw();
162 }
163 }
164
169 inline bool needsRedraw() const {
170 return redrawSelf;
171 }
172
176 virtual void drawDone();
177
182 void setParent(Widget *parent);
183
189
195 void onpress(std::function<void(Widget &, const Event &)> callback) override;
196
202 void onblur(std::function<void(Widget &, const Event &)> callback) override;
203
209 void onhold(std::function<void(Widget &, const Event &, time_t)> callback) override;
210
216 void onrelease(std::function<void(Widget &, const Event &)> callback) override;
217
221 void click();
222
226 void press();
227
231 void blur();
232
236 void release();
237
242 void hold(time_t time);
243
249 virtual bool handleEvent(Event &event);
250
251#ifdef DEBUG
256 virtual void drawBoundingBox(time_t time) const;
257#endif
258
269 virtual Widget *getWidgetById(id_t id) noexcept;
270};
271
272} // namespace ui
Extended definitions for widget event handler functions.
Definition event_handlers.hpp:97
void onclick(std::function< void(Widget &)> callback)
Register a touchscreen event handler that triggers on release.
Definition event_handlers.hpp:248
The common interface for all UI objects.
Definition widget.hpp:27
void setPosition(const Position &pos)
Set the position of this widget.
Definition widget.hpp:120
void onblur(std::function< void(Widget &, const Event &)> callback) override
Register a touchscreen event handler that triggers when the widget stops being pressed.
void press()
Manually trigger the onpress event handler.
void onpress(std::function< void(Widget &, const Event &)> callback) override
Register a touchscreen event handler that triggers on press.
void onhold(std::function< void(Widget &, const Event &, time_t)> callback) override
Register a touchscreen event handler that repeatedly triggers when the widget is held for a while.
void click()
Manually trigger the onrelease event handler.
virtual void drawDone()
Reset any state variables after rendering has finished.
virtual Bounds bounds() const
Get the rendering bounds of this widget.
bool redrawSelf
If true, force a redraw of this widget and any child widgets.
Definition widget.hpp:53
virtual ~Widget()
The default widget destructor.
Bounds parentBounds() const
Get the bounds of the parent widget, if any.
virtual Size size() const =0
Get the size of the widget.
void requestRedraw()
Tell this widget that it needs to be re-rendered.
Definition widget.hpp:149
void requestParentRedraw()
Tell the parent widget that it needs to be re-rendered.
Definition widget.hpp:159
virtual void draw() const =0
Render the widget to the screen.
Position getPosition() const
Get the position of this widget.
Definition widget.hpp:129
virtual void update(time_t time)
Update any internal state of the widget, and check if it needs to be re-rendered.
bool needsRedraw() const
Check if this widget has requested to be redrawn.
Definition widget.hpp:169
void setAlign(const Alignment &align)
Set the alignment of this widget.
Definition widget.hpp:137
Padding padding
The padding that will be applied to any child widgets.
Definition widget.hpp:51
void release()
Manually trigger the onrelease event handler.
virtual bool handleEvent(Event &event)
Handle events from the touchscreen.
void onrelease(std::function< void(Widget &, const Event &)> callback) override
Register a touchscreen event handler that triggers on release.
Widget(const Position &pos={0, 0}, const Alignment &align={ALIGN_LEFT, ALIGN_TOP}, const Padding &padding={0})
The default widget constructor.
void blur()
Manually trigger the onblur event handler.
virtual void render(bool force) const
Recursively render this widget and any child widgets that need it.
virtual Widget * getWidgetById(id_t id) noexcept
Get the first widget (this or any children) that has the given ID.
void hold(time_t time)
Manually trigger the onhold event handler.
id_t id
An optional (usually unique) identifier that can be used to search for this widget.
Definition widget.hpp:57
virtual void drawBoundingBox(time_t time) const
Render the bounding box of this and any child widgets.
void setParent(Widget *parent)
Set the parent widget.
uint32_t id_t
A (typically unique) identifier for widgets.
Definition id.hpp:13
A struct that holds a widget's vertical and horizontal alignment.
Definition alignment.hpp:22
A struct that holds the pixel space that a widget would take up on the screen.
Definition bounds.hpp:11
A touchscreen event.
Definition event.hpp:26
A struct that holds the padding that a parent widget may impose on a child widget.
Definition padding.hpp:13
A struct that holds the normalized position of a widget.
Definition position.hpp:15
A struct that holds the number of pixels that a widget would take up on the screen.
Definition size.hpp:17