TinyMaterialDesign
Loading...
Searching...
No Matches
ProgressIndicator.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3
4#include "TinyGPU/Color/RGB565.h"
5#include "TinyGPU/Color/RGB666.h"
6#include "TinyGPU/Color/RGB888.h"
7#include "TinyMaterialDesign/Core/Widget.h"
8
9namespace tinymd {
10
11/// @brief Horizontal progress bar. Determinate (value in [0,1]) or indeterminate
12/// (a segment sweeps back and forth, driven by update()).
13template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
14class LinearProgressIndicator : public Widget<RGB_T> {
15 public:
17 explicit LinearProgressIndicator(Bounds bounds, float value = 0.0f, bool indeterminate = false)
18 : value_(clamp01(value)), indeterminate_(indeterminate) {
19 this->bounds = bounds;
20 }
21
22 float value() const { return value_; }
23 void setValue(float value) { value_ = clamp01(value); }
24 void setIndeterminate(bool indeterminate) { indeterminate_ = indeterminate; }
25
26 void draw(tinygpu::ISurface<RGB_T>& target) override {
27 RGB_T trackColor = this->theme().colors.surfaceVariant;
28 RGB_T activeColor = this->enabled ? this->theme().colors.primary
29 : blend(this->theme().colors.primary, this->theme().colors.surface, 0.5f);
30 const size_t radius = toPx(this->bounds.h / 2);
31
32 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
33 toPx(this->bounds.h), radius, trackColor);
34
35 if (indeterminate_) {
36 constexpr float kSegmentFraction = 0.35f;
37 float start = phase_ * (1.0f + kSegmentFraction) - kSegmentFraction;
38 float end = start + kSegmentFraction;
39 start = std::max(0.0f, start);
40 end = std::min(1.0f, end);
41 if (end > start) {
42 const int32_t x0 = this->bounds.x + static_cast<int32_t>(start * this->bounds.w);
43 const int32_t x1 = this->bounds.x + static_cast<int32_t>(end * this->bounds.w);
44 target.fillRoundRect(toPx(x0), toPx(this->bounds.y), toPx(x1 - x0), toPx(this->bounds.h),
45 radius, activeColor);
46 }
47 } else if (value_ > 0.0f) {
48 const int32_t width = static_cast<int32_t>(value_ * this->bounds.w);
49 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(width),
50 toPx(this->bounds.h), radius, activeColor);
51 }
52 }
53
54 bool update(uint32_t nowMs) override {
55 if (!indeterminate_) return false;
56 if (!started_) {
57 started_ = true;
58 startMs_ = nowMs;
59 }
60 const uint32_t elapsed = (nowMs - startMs_) % kCycleMs;
61 phase_ = static_cast<float>(elapsed) / static_cast<float>(kCycleMs);
62 return true;
63 }
64
65 private:
66 static constexpr uint32_t kCycleMs = 1200;
67
68 float value_ = 0.0f;
69 bool indeterminate_ = false;
70 bool started_ = false;
71 uint32_t startMs_ = 0;
72 float phase_ = 0.0f;
73
74 static float clamp01(float value) { return std::min(1.0f, std::max(0.0f, value)); }
75};
76
77/// @brief Circular "spinner" ring. Determinate (value in [0,1], drawn as a sweep
78/// starting at 12 o'clock) or indeterminate (a fixed-length arc rotates
79/// continuously, driven by update()). Both use TinyGPU's ISurface::drawArc.
80template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
81class CircularProgressIndicator : public Widget<RGB_T> {
82 public:
84 explicit CircularProgressIndicator(Bounds bounds, float value = 0.0f, bool indeterminate = false)
85 : value_(clamp01(value)), indeterminate_(indeterminate) {
86 this->bounds = bounds;
87 }
88
89 float value() const { return value_; }
90 void setValue(float value) { value_ = clamp01(value); }
91 void setIndeterminate(bool indeterminate) { indeterminate_ = indeterminate; }
92 void setThickness(uint8_t thickness) { thickness_ = thickness; }
93
94 void draw(tinygpu::ISurface<RGB_T>& target) override {
95 RGB_T trackColor = this->theme().colors.surfaceVariant;
96 RGB_T activeColor = this->enabled ? this->theme().colors.primary
97 : blend(this->theme().colors.primary, this->theme().colors.surface, 0.5f);
98
99 const int32_t diameter = this->bounds.w < this->bounds.h ? this->bounds.w : this->bounds.h;
100 const size_t radius = toPx(diameter / 2 - thickness_ / 2);
101 const size_t cx = toPx(this->bounds.centerX());
102 const size_t cy = toPx(this->bounds.centerY());
103
104 target.drawArc(cx, cy, radius, 0.0f, 359.9f, trackColor, thickness_);
105
106 constexpr float kStartDeg = -90.0f; // 12 o'clock
107 if (indeterminate_) {
108 constexpr float kSweepDeg = 110.0f;
109 const float start = kStartDeg + phase_ * 360.0f;
110 target.drawArc(cx, cy, radius, start, start + kSweepDeg, activeColor, thickness_);
111 } else if (value_ > 0.0f) {
112 target.drawArc(cx, cy, radius, kStartDeg, kStartDeg + value_ * 360.0f, activeColor,
113 thickness_);
114 }
115 }
116
117 bool update(uint32_t nowMs) override {
118 if (!indeterminate_) return false;
119 if (!started_) {
120 started_ = true;
121 startMs_ = nowMs;
122 }
123 const uint32_t elapsed = (nowMs - startMs_) % kCycleMs;
124 phase_ = static_cast<float>(elapsed) / static_cast<float>(kCycleMs);
125 return true;
126 }
127
128 private:
129 static constexpr uint32_t kCycleMs = 900;
130
131 float value_ = 0.0f;
132 bool indeterminate_ = false;
133 uint8_t thickness_ = 4;
134 bool started_ = false;
135 uint32_t startMs_ = 0;
136 float phase_ = 0.0f;
137
138 static float clamp01(float value) { return std::min(1.0f, std::max(0.0f, value)); }
139};
140
147
148} // namespace tinymd
Circular "spinner" ring. Determinate (value in [0,1], drawn as a sweep starting at 12 o'clock) or ind...
Definition: ProgressIndicator.h:81
void setThickness(uint8_t thickness)
Definition: ProgressIndicator.h:92
CircularProgressIndicator(Bounds bounds, float value=0.0f, bool indeterminate=false)
Definition: ProgressIndicator.h:84
void setValue(float value)
Definition: ProgressIndicator.h:90
void setIndeterminate(bool indeterminate)
Definition: ProgressIndicator.h:91
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: ProgressIndicator.h:94
float value() const
Definition: ProgressIndicator.h:89
bool update(uint32_t nowMs) override
Definition: ProgressIndicator.h:117
Horizontal progress bar. Determinate (value in [0,1]) or indeterminate (a segment sweeps back and for...
Definition: ProgressIndicator.h:14
void setValue(float value)
Definition: ProgressIndicator.h:23
void setIndeterminate(bool indeterminate)
Definition: ProgressIndicator.h:24
LinearProgressIndicator(Bounds bounds, float value=0.0f, bool indeterminate=false)
Definition: ProgressIndicator.h:17
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: ProgressIndicator.h:26
float value() const
Definition: ProgressIndicator.h:22
bool update(uint32_t nowMs) override
Definition: ProgressIndicator.h:54
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