-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpinner.cpp
More file actions
168 lines (140 loc) · 4.61 KB
/
Copy pathSpinner.cpp
File metadata and controls
168 lines (140 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "Spinner.h"
#include "Backplate.h"
#include "Util.h"
#include <algorithm>
#include <cmath>
#include <windows.h>
namespace FD2D
{
Spinner::Spinner()
: Wnd()
{
}
Spinner::Spinner(const std::wstring& name)
: Wnd(name)
{
}
Size Spinner::Measure(Size available)
{
UNREFERENCED_PARAMETER(available);
m_desired = { 0.0f, 0.0f };
return m_desired;
}
Size Spinner::MinSize() const
{
return { 0.0f, 0.0f };
}
void Spinner::SetActive(bool active)
{
if (m_active == active)
{
return;
}
m_active = active;
m_lastAnimMs = 0;
Invalidate();
// Ensure the application loop treats animations as active immediately, even before the first paint.
if (m_active && BackplateRef() != nullptr)
{
BackplateRef()->RequestAnimationFrame();
}
}
void Spinner::SetStyle(const Style& style)
{
m_style = style;
// Recreate brushes next draw (color may have changed).
m_brush.Reset();
m_dimBrush.Reset();
Invalidate();
}
void Spinner::OnRender(ID2D1RenderTarget* target)
{
if (target == nullptr)
{
return;
}
// Smooth fade in/out to avoid abrupt dim-overlay flashes.
const unsigned long long now = Util::NowMs();
if (m_lastAnimMs == 0)
{
m_lastAnimMs = now;
}
const unsigned long long dtMs = now - m_lastAnimMs;
m_lastAnimMs = now;
const unsigned int fadeMs = (m_style.fadeMs > 0) ? m_style.fadeMs : 100U;
const float step = static_cast<float>(dtMs) / static_cast<float>(fadeMs);
if (m_active)
{
m_opacity = (std::min)(1.0f, m_opacity + step);
}
else
{
m_opacity = (std::max)(0.0f, m_opacity - step);
}
if (m_opacity <= 0.0f)
{
return;
}
if (!m_brush)
{
(void)target->CreateSolidColorBrush(m_style.color, &m_brush);
}
if (!m_brush)
{
return;
}
const D2D1_RECT_F r = LayoutRect();
const float w = r.right - r.left;
const float h = r.bottom - r.top;
if (!(w > 0.0f && h > 0.0f))
{
return;
}
if (m_style.dimBackground)
{
if (!m_dimBrush)
{
(void)target->CreateSolidColorBrush(D2D1::ColorF(0.0f, 0.0f, 0.0f, 1.0f), &m_dimBrush);
}
if (m_dimBrush)
{
const float a = Util::Clamp01(m_style.dimAlpha) * m_opacity;
m_dimBrush->SetColor(D2D1::ColorF(0.0f, 0.0f, 0.0f, a));
target->FillRectangle(r, m_dimBrush.Get());
}
}
const float cx = r.left + w * 0.5f;
const float cy = r.top + h * 0.5f;
const float baseRadius = (std::max)(
m_style.minRadius,
(std::min)(m_style.maxRadius, (std::min)(w, h) * m_style.radiusScaleOfMinDim));
const float innerRadius = baseRadius * m_style.innerRadiusRatio;
const float outerRadius = baseRadius;
const unsigned int period = (m_style.periodMs > 0) ? m_style.periodMs : 900U;
const unsigned long long t = Util::NowMs();
const float phase = static_cast<float>(t % static_cast<unsigned long long>(period)) / static_cast<float>(period);
const float baseAngle = phase * 6.28318530718f;
const int ticks = (m_style.ticks >= 3) ? m_style.ticks : 12;
for (int i = 0; i < ticks; ++i)
{
const float a = baseAngle + (static_cast<float>(i) * (6.28318530718f / static_cast<float>(ticks)));
const float s = std::sinf(a);
const float c = std::cosf(a);
// Tail effect: the "leading" tick is brightest.
const float alpha = 0.15f + (0.85f * (static_cast<float>(i) / static_cast<float>(ticks - 1)));
m_brush->SetColor(D2D1::ColorF(
m_style.color.r,
m_style.color.g,
m_style.color.b,
alpha * m_style.color.a * m_opacity));
const D2D1_POINT_2F p0 { cx + c * innerRadius, cy + s * innerRadius };
const D2D1_POINT_2F p1 { cx + c * outerRadius, cy + s * outerRadius };
target->DrawLine(p0, p1, m_brush.Get(), m_style.thickness);
}
// Keep animating: request a 60fps tick from the application loop.
if (BackplateRef() != nullptr)
{
BackplateRef()->RequestAnimationFrame();
}
}
}