5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/Widget.h"
9#include "TinyMaterialDesign/Draw/Icons.h"
18template <
typename RGB_T>
19using IconPainter =
void (*)(tinygpu::ISurface<RGB_T>&,
const Bounds&, RGB_T, uint8_t);
40 this->bounds = bounds;
45 void setIcon(IconPainter<RGB_T> icon) { icon_ = icon; }
52 hasColorOverride_ =
true;
53 backgroundOverride_ = background;
54 foregroundOverride_ = foreground;
61 hasAmbientTint_ =
true;
62 ambientBackground_ = background;
63 ambientForeground_ = foreground;
66 void draw(tinygpu::ISurface<RGB_T>& target)
override {
67 RGB_T background =
this->theme().colors.surface;
68 RGB_T foreground =
this->theme().colors.onSurfaceVariant;
73 background =
this->theme().colors.primary;
74 foreground =
this->theme().colors.onPrimary;
77 background =
this->theme().colors.secondaryContainer;
78 foreground =
this->theme().colors.onSecondaryContainer;
86 if (hasAmbientTint_) {
87 background = ambientBackground_;
88 foreground = ambientForeground_;
90 if (hasColorOverride_) {
91 background = backgroundOverride_;
92 foreground = foregroundOverride_;
95 background = blend(background,
this->theme().colors.surface, 0.6f);
96 foreground = blend(foreground,
this->theme().colors.surface, 0.5f);
99 const int32_t diameter =
this->bounds.w <
this->bounds.h ?
this->bounds.w :
this->bounds.h;
100 const size_t radius = toPx(diameter / 2);
102 if (paintBackground) {
104 const RGB_T shadow = blend(
this->theme().colors.surface,
this->theme().colors.onBackground, 0.35f);
105 target.fillCircle(toPx(
this->bounds.centerX() + 1), toPx(
this->bounds.centerY() + 2),
108 target.fillCircle(toPx(
this->bounds.centerX()), toPx(
this->bounds.centerY()), radius,
112 if (
this->theme().enableRipple && rippleActive_) {
113 const uint32_t elapsed = lastUpdateMs_ - rippleStartMs_;
114 const float t = std::min(1.0f,
static_cast<
float>(elapsed) / kRippleDurationMs);
115 const size_t rippleRadius = toPx(
static_cast<int32_t>(t * diameter / 2));
116 const RGB_T rippleColor = blend(background, foreground, 0.15f * (1.0f - t));
117 target.fillCircle(toPx(
this->bounds.centerX()), toPx(
this->bounds.centerY()), rippleRadius,
121 if (icon_ !=
nullptr) {
122 const Bounds iconRect =
this->bounds.inset(
this->bounds.w / 4);
123 icon_(target, iconRect, foreground, 2);
127 bool onGesture(
const tinygpu::GestureEvent& event)
override {
128 if (!isTapGesture(event.type))
return false;
129 rippleStartMs_ = lastUpdateMs_;
130 rippleActive_ =
true;
131 if (onClick) onClick();
136 const bool wasActive = rippleActive_;
137 lastUpdateMs_ = nowMs;
138 if (rippleActive_ && (nowMs - rippleStartMs_) >= kRippleDurationMs) {
139 rippleActive_ =
false;
145 static constexpr uint32_t kRippleDurationMs = 220;
147 IconPainter<RGB_T> icon_ =
nullptr;
149 bool hasColorOverride_ =
false;
150 RGB_T backgroundOverride_{};
151 RGB_T foregroundOverride_{};
152 bool hasAmbientTint_ =
false;
153 RGB_T ambientBackground_{};
154 RGB_T ambientForeground_{};
155 bool rippleActive_ =
false;
156 uint32_t rippleStartMs_ = 0;
157 uint32_t lastUpdateMs_ = 0;
Definition: AnchorLayout.h:4
IconButtonVariant
Definition: IconButton.h:28
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21