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"
11#include "TinyMaterialDesign/Widgets/IconButton.h"
20
21
22
23
24
25
26
27
35 this->bounds = bounds;
36 if (label !=
nullptr) {
44 void setIcon(IconPainter<RGB_T> icon) { icon_ = icon; }
46 label_ = label !=
nullptr ? label :
"";
47 extended_ = !label_.empty();
51 void draw(tinygpu::ISurface<RGB_T>& target)
override {
56 background =
this->theme().colors.primaryContainer;
57 foreground =
this->theme().colors.onPrimaryContainer;
60 background =
this->theme().colors.secondaryContainer;
61 foreground =
this->theme().colors.onSecondaryContainer;
64 background =
this->theme().colors.secondary;
65 foreground =
this->theme().colors.onSecondary;
69 background =
this->theme().colors.surfaceVariant;
70 foreground =
this->theme().colors.primary;
74 background = blend(background,
this->theme().colors.surface, 0.6f);
75 foreground = blend(foreground,
this->theme().colors.surface, 0.5f);
78 const size_t radius = extended_ ? toPx(
this->theme().shape.large) : toPx(
this->bounds.h / 2);
81 const RGB_T shadow = blend(
this->theme().colors.surface,
this->theme().colors.onBackground, 0.4f);
82 target.fillRoundRect(toPx(
this->bounds.x + 1), toPx(
this->bounds.y + 2), toPx(
this->bounds.w),
83 toPx(
this->bounds.h), radius, shadow);
85 target.fillRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
86 toPx(
this->bounds.h), radius, background);
88 if (
this->theme().enableRipple && rippleActive_) {
89 const uint32_t elapsed = lastUpdateMs_ - rippleStartMs_;
90 const float t = std::min(1.0f,
static_cast<
float>(elapsed) / kRippleDurationMs);
91 const int32_t maxRadius =
this->bounds.w >
this->bounds.h ?
this->bounds.w :
this->bounds.h;
92 const RGB_T rippleColor = blend(background, foreground, 0.18f * (1.0f - t));
93 target.fillCircle(toPx(
this->bounds.centerX()), toPx(
this->bounds.centerY()),
94 toPx(
static_cast<int32_t>(t * maxRadius)), rippleColor);
97 const int32_t iconSize = std::min<int32_t>(
this->bounds.h - 16, 32);
98 const int32_t iconPad = extended_ ?
this->theme().spacing * 2 : (
this->bounds.w - iconSize) / 2;
99 const Bounds iconRect(
this->bounds.x + iconPad,
this->bounds.centerY() - iconSize / 2, iconSize,
101 if (icon_ !=
nullptr) icon_(target, iconRect, foreground, 2);
104 tinygpu::IFont<RGB_T>& font = *
this->theme().typography.label;
105 const int32_t textX = icon_ !=
nullptr ? iconRect.right() +
this->theme().spacing
106 :
this->bounds.x +
this->theme().spacing * 2;
107 const int32_t textY =
this->bounds.centerY() -
static_cast<int32_t>(font.getHeight(1)) / 2;
108 font.drawText(target,
static_cast<int16_t>(textX),
static_cast<int16_t>(textY), label_.c_str(),
109 foreground, background,
false);
113 bool onGesture(
const tinygpu::GestureEvent& event)
override {
114 if (!isTapGesture(event.type))
return false;
115 rippleStartMs_ = lastUpdateMs_;
116 rippleActive_ =
true;
117 if (onClick) onClick();
122 const bool wasActive = rippleActive_;
123 lastUpdateMs_ = nowMs;
124 if (rippleActive_ && (nowMs - rippleStartMs_) >= kRippleDurationMs) rippleActive_ =
false;
129 static constexpr uint32_t kRippleDurationMs = 260;
131 IconPainter<RGB_T> icon_ =
nullptr;
133 bool extended_ =
false;
136 bool rippleActive_ =
false;
137 uint32_t rippleStartMs_ = 0;
138 uint32_t lastUpdateMs_ = 0;
Definition: AnchorLayout.h:4
FabColor
Definition: Fab.h:17
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21