-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
progresswidget.cpp
242 lines (200 loc) · 6.33 KB
/
progresswidget.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/** @file progresswidget.cpp Progress indicator.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "ui/widgets/progresswidget.h"
#include "ui/widgets/guirootwidget.h"
#include <de/Animation>
#include <de/Lockable>
using namespace de;
DENG2_PIMPL(ProgressWidget), public Lockable
{
Mode mode;
Rangei range;
Rangef visualRange;
Animation pos;
float angle;
Id gearTex;
DotPath colorId;
DotPath shadowColorId;
Time updateAt;
Instance(Public *i)
: Base(i),
mode(Indefinite),
visualRange(0, 1),
pos(0, Animation::Linear),
angle(0),
colorId("progress.light.wheel"),
shadowColorId("progress.light.shadow"),
updateAt(Time::invalidTime())
{
updateStyle();
}
void updateStyle()
{
self.setImageColor(self.style().colors().colorf(colorId) * Vector4f(1, 1, 1, .5f));
}
void glInit()
{
gearTex = atlas().alloc(self.style().images().image("progress.gear"));
}
void glDeinit()
{
atlas().release(gearTex);
gearTex = Id::None;
}
AtlasTexture &atlas()
{
return self.root().atlas();
}
};
ProgressWidget::ProgressWidget(String const &name) : d(new Instance(this))
{
setTextGap("progress.textgap");
setSizePolicy(ui::Filled, ui::Filled);
// Set up the static progress ring image.
setImage(style().images().image("progress.wheel"));
setImageFit(ui::FitToSize | ui::OriginalAspectRatio);
setImageScale(.6f);
setAlignment(ui::AlignCenter, AlignOnlyByImage);
//setText("Loading Blah Blah...");
setTextAlignment(ui::AlignRight);
}
ProgressWidget::Mode ProgressWidget::mode() const
{
DENG2_GUARD(d);
return d->mode;
}
Rangei ProgressWidget::range() const
{
DENG2_GUARD(d);
return d->range;
}
bool ProgressWidget::isAnimating() const
{
DENG2_GUARD(d);
return !d->pos.done();
}
void ProgressWidget::setColor(DotPath const &styleId)
{
d->colorId = styleId;
d->updateStyle();
}
void ProgressWidget::setShadowColor(const DotPath &styleId)
{
d->shadowColorId = styleId;
d->updateStyle();
}
void ProgressWidget::setText(String const &text)
{
DENG2_GUARD(d);
LabelWidget::setText(text);
}
void ProgressWidget::setMode(ProgressWidget::Mode progressMode)
{
DENG2_GUARD(d);
d->mode = progressMode;
}
void ProgressWidget::setRange(Rangei const &range, Rangef const &visualRange)
{
DENG2_GUARD(d);
d->range = range;
d->visualRange = visualRange;
setMode(Ranged);
}
void ProgressWidget::setProgress(int currentProgress, TimeDelta const &transitionSpan)
{
DENG2_GUARD(d);
d->pos.setValue(float(currentProgress - d->range.start) / float(d->range.size()),
transitionSpan);
}
void ProgressWidget::update()
{
DENG2_GUARD(d);
LabelWidget::update();
// Keep rotating the wheel.
Time const now = Time();
if(!d->updateAt.isValid()) d->updateAt = now;
TimeDelta const elapsed = d->updateAt.since();
d->updateAt = now;
d->angle = de::wrap(d->angle + float(elapsed * 20), 0.f, 360.f);
requestGeometry();
}
void ProgressWidget::glInit()
{
DENG2_GUARD(d);
LabelWidget::glInit();
d->glInit();
}
void ProgressWidget::glDeinit()
{
DENG2_GUARD(d);
d->glDeinit();
LabelWidget::glDeinit();
}
void ProgressWidget::glMakeGeometry(DefaultVertexBuf::Builder &verts)
{
DENG2_GUARD(d);
ContentLayout layout;
contentLayout(layout);
// There is a shadow behind the wheel.
float shadowRadius = layout.image.width() / 2;
float ringRadius = layout.image.width() / 4;
float ringThick = layout.image.width() / 10;
verts.makeRing(layout.image.middle(), shadowRadius + ringRadius, shadowRadius + ringThick, 30,
style().colors().colorf(d->shadowColorId),
root().atlas().imageRectf(root().borderGlow()), 0);
verts.makeRing(layout.image.middle(), shadowRadius - ringThick, shadowRadius + ringThick, 30,
style().colors().colorf(d->shadowColorId),
root().atlas().imageRectf(root().borderGlow()).middle());
verts.makeRing(layout.image.middle(), shadowRadius - ringRadius, shadowRadius - ringThick, 30,
style().colors().colorf(d->shadowColorId),
root().atlas().imageRectf(root().borderGlow()), 0);
LabelWidget::glMakeGeometry(verts);
// Draw the rotating indicator on the label's image.
Rectanglef const tc = d->atlas().imageRectf(d->gearTex);
float pos = 1;
if(d->mode != Indefinite)
{
pos = de::clamp(0.f, d->pos.value(), 1.f);
}
// Map to the visual range.
pos = d->visualRange.start + pos * d->visualRange.size();
int const edgeCount = de::max(1, int(pos * 30));
float const radius = layout.image.width() / 2;
Matrix4f const rotation = Matrix4f::rotateAround(tc.middle(), -d->angle);
DefaultVertexBuf::Builder gear;
DefaultVertexBuf::Type v;
v.rgba = style().colors().colorf(d->colorId);
for(int i = 0; i <= edgeCount; ++i)
{
// Center vertex.
v.pos = layout.image.middle();
v.texCoord = tc.middle();
gear << v;
// Outer vertex.
float const angle = 2 * PI * pos * (i / (float)edgeCount) + PI/2;
v.pos = v.pos + Vector2f(cos(angle)*radius*1.05f, sin(angle)*radius*1.05f);
v.texCoord = rotation * (tc.topLeft + tc.size() * Vector2f(.5f + cos(angle)*.5f,
.5f + sin(angle)*.5f));
gear << v;
}
verts += gear;
}
void ProgressWidget::updateStyle()
{
d->updateStyle();
}