TinyMaterialDesign
Loading...
Searching...
No Matches
Card.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3
4#include "TinyGPU/Color/RGB565.h"
5#include "TinyGPU/Color/RGB666.h"
6#include "TinyGPU/Color/RGB888.h"
7#include "TinyGPU/Font/LinePrinter.h"
8#include "TinyMaterialDesign/Core/Widget.h"
9
10namespace tinymd {
11
12/// @brief Elevated rounded-rect container with an optional title and
13/// word-wrapped body text.
14template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
15class Card : public Widget<RGB_T> {
16 public:
17 Card() = default;
18 Card(Bounds bounds, const char* title = nullptr, const char* body = nullptr) {
19 setBounds(bounds);
20 if (title != nullptr) setTitle(title);
21 if (body != nullptr) setBody(body);
22 }
23
24 /// Cheap elevation approximation: an offset, darker rounded rect drawn
25 /// behind the card (see MaterialTheme's blend()). Set false for a flat card.
26 bool elevated = true;
27
28 void setTitle(const char* title) {
29 title_ = title;
30 hasTitle_ = true;
31 }
32
33 void setBody(const char* body) {
34 body_ = body;
35 hasBody_ = true;
36 }
37
38 void setBounds(Bounds bounds) {
39 this->bounds = bounds;
40 }
41
42 /// Color of the border (default: the outline color of the theme), e.g. an
43 /// accent for a highlighted card.
44 void setOutlineColor(RGB_T color) {
45 outline_ = color;
46 hasOutline_ = true;
47 }
48 void clearOutlineColor() { hasOutline_ = false; }
49
50 /// Corner radius from the theme's large shape instead of the medium one.
51 bool largeCorners = false;
52
53 void draw(tinygpu::ISurface<RGB_T>& target) override {
54 const size_t radius = toPx(largeCorners ? this->theme().shape.large : this->theme().shape.medium);
55
56 if (elevated) {
57 const RGB_T shadow = blend(this->theme().colors.surface, this->theme().colors.onBackground, 0.30f);
58 target.fillRoundRect(toPx(this->bounds.x + 1), toPx(this->bounds.y + 2),
59 toPx(this->bounds.w), toPx(this->bounds.h), radius, shadow);
60 }
61
62 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
63 toPx(this->bounds.h), radius, this->theme().colors.surface);
64 target.drawRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
65 toPx(this->bounds.h), radius, hasOutline_ ? outline_ : this->theme().colors.outline);
66
67 const int32_t pad = this->theme().spacing;
68 int32_t cursorY = this->bounds.y + pad;
69
70 if (hasTitle_) {
71 tinygpu::IFont<RGB_T>& titleFont = *this->theme().typography.title;
72 titleFont.drawText(target, static_cast<int16_t>(this->bounds.x + pad),
73 static_cast<int16_t>(cursorY), title_.c_str(), this->theme().colors.onSurface,
74 this->theme().colors.surface, false);
75 cursorY += static_cast<int32_t>(titleFont.getHeight(1)) + pad / 2;
76 }
77
78 if (hasBody_) {
79 // LinePrinter's borders are measured from the *target surface's*
80 // edges (it's designed for "print with margins", not "wrap inside an
81 // arbitrary rect") - so the right border has to be back-computed from
82 // the card's right edge rather than set directly.
83 const int32_t rightEdge = this->bounds.right() - pad;
84 const size_t rightBorder =
85 rightEdge < static_cast<int32_t>(target.width())
86 ? target.width() - static_cast<size_t>(rightEdge)
87 : 0;
88
89 tinygpu::LinePrinter<RGB_T> printer;
90 printer.setFont(*this->theme().typography.body);
91 printer.setTarget(target);
92 printer.setColor(this->theme().colors.onSurfaceVariant);
93 printer.setTopBorder(toPx(cursorY));
94 printer.setLeftBorder(toPx(this->bounds.x + pad));
95 printer.setRightBorder(rightBorder);
96 printer.print(body_.c_str());
97 }
98 }
99
100 private:
101 std::string title_;
102 std::string body_;
103 bool hasTitle_ = false;
104 bool hasBody_ = false;
105 RGB_T outline_{};
106 bool hasOutline_ = false;
107};
108
112
113} // namespace tinymd
Elevated rounded-rect container with an optional title and word-wrapped body text.
Definition: Card.h:15
void clearOutlineColor()
Definition: Card.h:48
void setBody(const char *body)
Definition: Card.h:33
bool largeCorners
Corner radius from the theme's large shape instead of the medium one.
Definition: Card.h:51
bool elevated
Definition: Card.h:26
Card(Bounds bounds, const char *title=nullptr, const char *body=nullptr)
Definition: Card.h:18
void setBounds(Bounds bounds)
Definition: Card.h:38
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Card.h:53
Card()=default
void setOutlineColor(RGB_T color)
Definition: Card.h:44
void setTitle(const char *title)
Definition: Card.h:28
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