6#include "TinyGPU/Color/RGB565.h"
7#include "TinyGPU/Color/RGB666.h"
8#include "TinyGPU/Color/RGB888.h"
9#include "TinyMaterialDesign/Core/Bounds.h"
10#include "TinyMaterialDesign/Core/Widget.h"
17
18
19
20
21
22
23
30 this->bounds = bounds;
35 void setLabel(
const char* label) { label_ = label; }
36 const std::string&
label()
const {
return label_; }
39 void draw(tinygpu::ISurface<RGB_T>& target)
override {
42 bool paintBackground =
true;
43 bool paintOutline =
false;
47 background =
this->theme().colors.primary;
48 foreground =
this->theme().colors.onPrimary;
51 background =
this->theme().colors.secondaryContainer;
52 foreground =
this->theme().colors.onSecondaryContainer;
55 background =
this->theme().colors.surface;
56 foreground =
this->theme().colors.primary;
59 background =
this->theme().colors.surface;
60 foreground =
this->theme().colors.primary;
65 background =
this->theme().colors.surface;
66 foreground =
this->theme().colors.primary;
67 paintBackground =
false;
72 foreground = blend(foreground,
this->theme().colors.onSurface, 0.5f);
73 background = blend(background,
this->theme().colors.onSurface, 0.7f);
76 const int32_t radius =
this->theme().shape.full;
79 const RGB_T shadow = blend(
this->theme().colors.surface,
this->theme().colors.onBackground, 0.35f);
80 target.fillRoundRect(toPx(
this->bounds.x + 1), toPx(
this->bounds.y + 2),
81 toPx(
this->bounds.w), toPx(
this->bounds.h), toPx(radius),
85 if (paintBackground) {
86 target.fillRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y),
87 toPx(
this->bounds.w), toPx(
this->bounds.h), toPx(radius),
91 const RGB_T outlineColor =
92 this->enabled ?
this->theme().colors.outline : blend(
this->theme().colors.outline,
this->theme().colors.surface, 0.5f);
93 target.drawRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y),
94 toPx(
this->bounds.w), toPx(
this->bounds.h), toPx(radius),
98 if (
this->theme().enableRipple && rippleActive_) {
99 drawRipple(target, background, foreground);
102 tinygpu::IFont<RGB_T>& font = *
this->theme().typography.label;
103 const size_t textWidth = font.measureTextWidth(label_.c_str());
104 const size_t textHeight = font.getHeight(1);
105 const int32_t textX =
this->bounds.centerX() -
static_cast<int32_t>(textWidth) / 2;
106 const int32_t textY =
this->bounds.centerY() -
static_cast<int32_t>(textHeight) / 2;
107 font.drawText(target,
static_cast<int16_t>(textX),
static_cast<int16_t>(textY),
108 label_.c_str(), foreground, background,
false);
111 bool onGesture(
const tinygpu::GestureEvent& event)
override {
112 if (!isTapGesture(event.type))
return false;
113 rippleOriginX_ = event.point.x;
114 rippleOriginY_ = event.point.y;
117 rippleStartMs_ = lastUpdateMs_;
118 rippleActive_ =
true;
119 if (onClick) onClick();
124 const bool wasActive = rippleActive_;
125 lastUpdateMs_ = nowMs;
126 if (rippleActive_ && (nowMs - rippleStartMs_) >= kRippleDurationMs) {
127 rippleActive_ =
false;
137 static constexpr uint32_t kRippleDurationMs = 260;
142 bool rippleActive_ =
false;
143 uint32_t rippleStartMs_ = 0;
144 uint32_t lastUpdateMs_ = 0;
145 int32_t rippleOriginX_ = 0;
146 int32_t rippleOriginY_ = 0;
153 void drawRipple(tinygpu::ISurface<RGB_T>& target, RGB_T background, RGB_T foreground) {
154 const uint32_t elapsed = lastUpdateMs_ - rippleStartMs_;
155 const float t = std::min(1.0f,
static_cast<
float>(elapsed) / kRippleDurationMs);
156 const float maxRadius =
157 static_cast<
float>(
this->bounds.w >
this->bounds.h ?
this->bounds.w :
this->bounds.h);
158 const float radius = t * maxRadius;
159 const RGB_T rippleColor = blend(background, foreground, 0.20f * (1.0f - t));
161 const int32_t left = std::max(
this->bounds.x,
static_cast<int32_t>(rippleOriginX_ - radius));
162 const int32_t right = std::min(
this->bounds.right(),
static_cast<int32_t>(rippleOriginX_ + radius) + 1);
163 const int32_t top = std::max(
this->bounds.y,
static_cast<int32_t>(rippleOriginY_ - radius));
164 const int32_t bottom = std::min(
this->bounds.bottom(),
static_cast<int32_t>(rippleOriginY_ + radius) + 1);
165 const float radiusSq = radius * radius;
167 for (int32_t y = top; y < bottom; ++y) {
168 for (int32_t x = left; x < right; ++x) {
169 const float dx =
static_cast<
float>(x - rippleOriginX_);
170 const float dy =
static_cast<
float>(y - rippleOriginY_);
171 if (dx * dx + dy * dy <= radiusSq) {
172 target.setPixel(toPx(x), toPx(y), rippleColor);
Definition: AnchorLayout.h:4
ButtonVariant
Definition: Button.h:14
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21