5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/Widget.h"
18 Chip(
Bounds bounds,
const char* label,
bool selectable =
false,
bool selected =
false)
19 :
label_(
label), selectable_(selectable), selected_(selected) {
20 this->bounds = bounds;
31 void draw(tinygpu::ISurface<RGB_T>& target)
override {
32 RGB_T background = selected_ ?
this->theme().colors.secondaryContainer :
this->theme().colors.surfaceVariant;
33 RGB_T foreground = selected_ ?
this->theme().colors.onSecondaryContainer :
this->theme().colors.onSurfaceVariant;
34 RGB_T outline =
this->theme().colors.outline;
36 background = blend(background,
this->theme().colors.surface, 0.5f);
37 foreground = blend(foreground,
this->theme().colors.surface, 0.5f);
38 outline = blend(outline,
this->theme().colors.surface, 0.5f);
41 const size_t radius = toPx(
this->bounds.h / 2);
42 target.fillRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
43 toPx(
this->bounds.h), radius, background);
45 target.drawRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
46 toPx(
this->bounds.h), radius, outline);
49 tinygpu::IFont<RGB_T>& font = *
this->theme().typography.label;
50 const size_t textWidth = font.measureTextWidth(label_.c_str());
51 const size_t textHeight = font.getHeight(1);
52 const int32_t textX =
this->bounds.centerX() -
static_cast<int32_t>(textWidth) / 2;
53 const int32_t textY =
this->bounds.centerY() -
static_cast<int32_t>(textHeight) / 2;
54 font.drawText(target,
static_cast<int16_t>(textX),
static_cast<int16_t>(textY),
55 label_.c_str(), foreground, background,
false);
58 bool onGesture(
const tinygpu::GestureEvent& event)
override {
59 if (!isTapGesture(event.type))
return false;
61 selected_ = !selected_;
62 if (onChange) onChange(selected_);
64 if (onClick) onClick();
70 bool selectable_ =
false;
71 bool selected_ =
false;
Small rounded-rect label, optionally a toggle (filter chip) rather than a one-shot action (assist chi...
Definition: Chip.h:15
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Chip.h:58
std::function< void(bool)> onChange
Fires only when selectable == true, with the new selected state.
Definition: Chip.h:26
Chip(Bounds bounds, const char *label, bool selectable=false, bool selected=false)
Definition: Chip.h:18
bool selected() const
Definition: Chip.h:28
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Chip.h:31
void setSelected(bool selected)
Definition: Chip.h:29
std::function< void()> onClick
Fires on every tap.
Definition: Chip.h:24
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21