TinyMaterialDesign
Loading...
Searching...
No Matches
MediaCard.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <string>
4
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
10namespace tinymd {
11
12/**
13 * @brief Tappable card with a thumbnail image and a caption - e.g. one tile
14 * in a grid of stations/genres/countries. Pair with GridLayout
15 * (Core/GridLayout.h) to lay out several of these.
16 *
17 * The image is any TinyGPU ISurface<RGB_T> (a Sprite, a decoded BMP, ...)
18 * you already have - this widget only blits it via ISurface::drawSprite(),
19 * it doesn't fetch or decode anything itself (TinyGPU has no HTTP/network
20 * layer). Not owned: pass a pointer that outlives the card, or nullptr for
21 * a placeholder fill. There's no scaling either - drawSprite() blits 1:1,
22 * so pre-size the image to fit the card's image area (bounds minus the
23 * caption strip and padding) before handing it over.
24 */
25template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
26class MediaCard : public Widget<RGB_T> {
27 public:
28 MediaCard() = default;
29 MediaCard(Bounds bounds, const char* caption = nullptr) {
30 this->bounds = bounds;
31 if (caption != nullptr) setCaption(caption);
32 }
33
34 std::function<void()> onClick;
35
36 /// Not owned - must outlive the card, or be reset to nullptr/another
37 /// image before it doesn't. nullptr draws a plain placeholder fill.
38 void setImage(const tinygpu::ISurface<RGB_T>* image) { image_ = image; }
39 void setCaption(const char* caption) { caption_ = caption; }
40
41 /// "Now playing"/highlighted state: filled tint + primary-colored outline.
42 bool selected() const { return selected_; }
43 void setSelected(bool selected) { selected_ = selected; }
44
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;
50 if (!this->enabled) {
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);
54 }
55
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);
60
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);
65
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);
71 }
72
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);
79 }
80
81 bool onGesture(const tinygpu::GestureEvent& event) override {
82 if (!isTapGesture(event.type)) return false;
83 if (onClick) onClick();
84 return true;
85 }
86
87 private:
88 const tinygpu::ISurface<RGB_T>* image_ = nullptr;
89 std::string caption_;
90 bool selected_ = false;
91};
92
96
97} // namespace tinymd
Tappable card with a thumbnail image and a caption - e.g. one tile in a grid of stations/genres/count...
Definition: MediaCard.h:26
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: MediaCard.h:81
MediaCard(Bounds bounds, const char *caption=nullptr)
Definition: MediaCard.h:29
void setCaption(const char *caption)
Definition: MediaCard.h:39
bool selected() const
"Now playing"/highlighted state: filled tint + primary-colored outline.
Definition: MediaCard.h:42
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: MediaCard.h:45
void setSelected(bool selected)
Definition: MediaCard.h:43
std::function< void()> onClick
Definition: MediaCard.h:34
MediaCard()=default
void setImage(const tinygpu::ISurface< RGB_T > *image)
Definition: MediaCard.h:38
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
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