7#include "TinyGPU/Color/RGB565.h"
8#include "TinyGPU/Color/RGB666.h"
9#include "TinyGPU/Color/RGB888.h"
10#include "TinyGPU/Font/LinePrinter.h"
11#include "TinyMaterialDesign/Core/Widget.h"
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
34 using ActionCountFn = std::function<
int()>;
35 using ActionAtFn = std::function<
Widget<RGB_T>&(
int index)>;
42 this->bounds = cardBounds;
45 void setTitle(
const char* title) { title_ = title; }
46 void setMessage(
const char* message) { message_ = message; }
51 actions_.push_back(&action);
56 if (
this->theme_ !=
nullptr) action.setTheme(*
this->theme_);
61 actionCountFn_ = std::move(count);
62 actionAtFn_ = std::move(at);
69 Widget<RGB_T>::setTheme(theme);
70 for (Widget<RGB_T>* action : actions_) {
71 if (action !=
nullptr) action->setTheme(theme);
77 Bounds actionRect(
int index,
int count, int32_t width = 80, int32_t height = 36)
const {
78 const int32_t pad = 8;
79 const int32_t totalWidth = count * width + (count - 1) * pad;
80 const int32_t startX =
this->bounds.right() - pad - totalWidth;
81 const int32_t y =
this->bounds.bottom() - pad - height;
82 return Bounds(startX + index * (width + pad)
, y
, width
, height
);
85 void draw(tinygpu::ISurface<RGB_T>& target)
override {
88 const size_t radius = toPx(
this->theme().shape.large);
89 target.fillRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
90 toPx(
this->bounds.h), radius,
this->theme().colors.surface);
91 target.drawRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
92 toPx(
this->bounds.h), radius,
this->theme().colors.outline);
94 const int32_t pad =
this->theme().spacing;
95 int32_t cursorY =
this->bounds.y + pad * 2;
97 tinygpu::IFont<RGB_T>& titleFont = *
this->theme().typography.title;
98 titleFont.drawText(target,
static_cast<int16_t>(
this->bounds.x + pad),
99 static_cast<int16_t>(cursorY), title_.c_str(),
this->theme().colors.onSurface,
100 this->theme().colors.surface,
false);
101 cursorY +=
static_cast<int32_t>(titleFont.getHeight(1)) + pad;
103 const int32_t rightEdge =
this->bounds.right() - pad;
104 const size_t rightBorder = rightEdge <
static_cast<int32_t>(target.width())
105 ? target.width() -
static_cast<size_t>(rightEdge)
110 target.pushClipRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
111 toPx(
this->bounds.h));
112 tinygpu::LinePrinter<RGB_T> printer;
113 printer.setFont(*
this->theme().typography.body);
114 printer.setTarget(target);
115 printer.setColor(
this->theme().colors.onSurfaceVariant);
116 printer.setTopBorder(toPx(cursorY));
117 printer.setLeftBorder(toPx(
this->bounds.x + pad));
118 printer.setRightBorder(rightBorder);
119 printer.print(message_.c_str());
120 target.popClipRect();
122 const int count = actionCount();
123 for (
int i = 0; i < count; ++i) {
124 Widget<RGB_T>* action = actionAt(i);
125 if (action->visible) action->draw(target);
133 bool onGesture(
const tinygpu::GestureEvent& event)
override {
134 const int count = actionCount();
135 for (
int i = 0; i < count; ++i) {
136 Widget<RGB_T>* action = actionAt(i);
137 if (action->enabled && action->bounds.contains(event.point.x, event.point.y)) {
138 return action->onGesture(event);
145 bool changed =
false;
146 const int count = actionCount();
147 for (
int i = 0; i < count; ++i) changed |= actionAt(i)->update(nowMs);
155 const int count = actionCount();
156 for (
int i = 0; i < count; ++i) {
157 Widget<RGB_T>* action = actionAt(i);
158 if (action->enabled && action->bounds.contains(x, y))
return action->isDraggableAt(x, y);
164 int actionCount()
const {
165 return actionCountFn_ ? actionCountFn_() :
static_cast<
int>(actions_.size());
168 Widget<RGB_T>* actionAt(
int index)
const {
170 Widget<RGB_T>& action = actionAtFn_(index);
171 if (
this->theme_ !=
nullptr) action.setTheme(*
this->theme_);
174 return actions_[
static_cast<size_t>(index)];
178 std::string message_;
179 std::vector<Widget<RGB_T>*> actions_;
180 ActionCountFn actionCountFn_;
181 ActionAtFn actionAtFn_;
Modal alert dialog: full-screen scrim + centered card + title + wrapped message + action widgets (typ...
Definition: Dialog.h:32
bool isDraggableAt(int32_t x, int32_t y) const override
Definition: Dialog.h:154
Dialog(Bounds cardBounds, const char *title, const char *message)
Definition: Dialog.h:40
bool onGesture(const tinygpu::GestureEvent &event) override
Definition: Dialog.h:133
Bounds actionRect(int index, int count, int32_t width=80, int32_t height=36) const
Definition: Dialog.h:77
void setActionProvider(ActionCountFn count, ActionAtFn at)
Switches to callback-driven actions - see the class comment.
Definition: Dialog.h:60
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Dialog.h:85
void setMessage(const char *message)
Definition: Dialog.h:46
void setTitle(const char *title)
Definition: Dialog.h:45
bool update(uint32_t nowMs) override
Definition: Dialog.h:144
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: Dialog.h:68
void addAction(Widget< RGB_T > &action)
Definition: Dialog.h:50
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50