5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/Widget.h"
13
14
15
16
17
18
19
20
21
22
23
24
30 this->bounds = bounds;
38 void setImage(
const tinygpu::ISurface<RGB_T>* image) { image_ = image; }
39 void setCaption(
const char* caption) { caption_ = caption; }
45 void draw(tinygpu::ISurface<RGB_T>& target)
override {
46 const size_t radius = toPx(
this->theme().shape.medium);
47 RGB_T background = selected_ ?
this->theme().colors.secondaryContainer :
this->theme().colors.surface;
48 RGB_T outline = selected_ ?
this->theme().colors.primary :
this->theme().colors.outline;
49 RGB_T captionColor = selected_ ?
this->theme().colors.onSecondaryContainer :
this->theme().colors.onSurface;
51 background = blend(background,
this->theme().colors.surface, 0.5f);
52 outline = blend(outline,
this->theme().colors.surface, 0.5f);
53 captionColor = blend(captionColor,
this->theme().colors.surface, 0.5f);
56 target.fillRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
57 toPx(
this->bounds.h), radius, background);
58 target.drawRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
59 toPx(
this->bounds.h), radius, outline);
61 const int32_t pad = 4;
62 const int32_t captionHeight = 18;
63 const Bounds imageArea(
this->bounds.x + pad,
this->bounds.y + pad,
this->bounds.w - 2 * pad,
64 this->bounds.h - captionHeight - 2 * pad);
66 if (image_ !=
nullptr) {
67 target.drawSprite(toPx(imageArea.x), toPx(imageArea.y), *image_);
68 }
else if (imageArea
.w > 0 && imageArea
.h > 0) {
69 target.fillRect(toPx(imageArea.x), toPx(imageArea.y), toPx(imageArea.w), toPx(imageArea.h),
70 this->theme().colors.surfaceVariant);
73 tinygpu::IFont<RGB_T>& font = *
this->theme().typography.label;
74 const size_t textWidth = font.measureTextWidth(caption_.c_str());
75 const int32_t textX =
this->bounds.centerX() -
static_cast<int32_t>(textWidth) / 2;
76 const int32_t textY =
this->bounds.bottom() - captionHeight - pad + 2;
77 font.drawText(target,
static_cast<int16_t>(textX),
static_cast<int16_t>(textY),
78 caption_.c_str(), captionColor, background,
false);
81 bool onGesture(
const tinygpu::GestureEvent& event)
override {
82 if (!isTapGesture(event.type))
return false;
83 if (onClick) onClick();
88 const tinygpu::ISurface<RGB_T>* image_ =
nullptr;
90 bool selected_ =
false;
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t w
Definition: Bounds.h:24
int32_t h
Definition: Bounds.h:25