MyGUI 3.5.3
MyGUI_ProgressBar.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_ProgressBar.h"
10#include "MyGUI_Widget.h"
11#include "MyGUI_Gui.h"
12#include "MyGUI_SkinManager.h"
13
14namespace MyGUI
15{
16
17 const size_t PROGRESS_AUTO_WIDTH = 200;
18 const size_t PROGRESS_AUTO_RANGE = 1000;
19 const float PROGRESS_AUTO_COEF = 400;
20
21 void ProgressBar::initialiseOverride()
22 {
23 Base::initialiseOverride();
24
26 assignWidget(mTrackPlace, "TrackPlace");
27
28 if (nullptr == mTrackPlace)
29 {
30 //OBSOLETE
31 assignWidget(mTrackPlace, "Client");
32
33 if (nullptr == mTrackPlace)
34 mTrackPlace = this;
35 }
36
37 if (isUserString("TrackSkin"))
38 mTrackSkin = getUserString("TrackSkin");
39
40 if (isUserString("TrackWidth"))
41 mTrackWidth = utility::parseValue<int>(getUserString("TrackWidth"));
42
43 //OBSOLETE
44 if (isUserString("TrackMin"))
45 mTrackMin = utility::parseValue<int>(getUserString("TrackMin"));
46
47 else
48 mTrackMin = mTrackWidth;
49
50 if (isUserString("TrackStep"))
51 mTrackStep = utility::parseValue<int>(getUserString("TrackStep"));
52
53 if (isUserString("TrackFill"))
54 mFillTrack = utility::parseValue<bool>(getUserString("TrackFill"));
55
56 if (!isUserString("TrackStep"))
57 mTrackStep = mTrackWidth;
58
59 if (1 > mTrackWidth)
60 mTrackWidth = 1;
61 }
62
63 void ProgressBar::shutdownOverride()
64 {
65 mTrackPlace = nullptr;
66
67 Base::shutdownOverride();
68 }
69
70 void ProgressBar::setProgressRange(size_t _range)
71 {
72 if (mAutoTrack)
73 return;
74 mRange = _range;
75 if (mEndPosition > mRange)
76 mEndPosition = mRange;
77 if (mStartPosition > mRange)
78 mStartPosition = mRange;
79 updateTrack();
80 }
81
82 void ProgressBar::setProgressPosition(size_t _pos)
83 {
84 if (mAutoTrack)
85 return;
86 if (mEndPosition == _pos)
87 return;
88 mEndPosition = _pos;
89 if (mEndPosition > mRange)
90 mEndPosition = mRange;
91 updateTrack();
92 }
93
94 void ProgressBar::setProgressAutoTrack(bool _auto)
95 {
96 if (mAutoTrack == _auto)
97 return;
98 mAutoTrack = _auto;
99
100 if (mAutoTrack)
101 {
102 Gui::getInstance().eventFrameStart += newDelegate(this, &ProgressBar::frameEntered);
103 mRange = PROGRESS_AUTO_RANGE;
104 mEndPosition = mStartPosition = 0;
105 mAutoPosition = 0.0f;
106 }
107 else
108 {
109 Gui::getInstance().eventFrameStart -= newDelegate(this, &ProgressBar::frameEntered);
110 mRange = mEndPosition = mStartPosition = 0;
111 }
112 updateTrack();
113 }
114
115 void ProgressBar::frameEntered(float _time)
116 {
117 if (!mAutoTrack)
118 return;
119 mAutoPosition += (PROGRESS_AUTO_COEF * _time);
120 size_t pos = (size_t)mAutoPosition;
121
122 if (pos > (mRange + PROGRESS_AUTO_WIDTH))
123 {
124 mAutoPosition = 0.0f;
125 pos = 0;
126 }
127
128 if (pos > mRange)
129 mEndPosition = mRange;
130 else
131 mEndPosition = size_t(mAutoPosition);
132
133 if (pos < PROGRESS_AUTO_WIDTH)
134 mStartPosition = 0;
135 else
136 mStartPosition = pos - PROGRESS_AUTO_WIDTH;
137
138 updateTrack();
139 }
140
141 void ProgressBar::setPosition(const IntPoint& _point)
142 {
143 Base::setPosition(_point);
144 }
145
146 void ProgressBar::setSize(const IntSize& _size)
147 {
148 Base::setSize(_size);
149
150 updateTrack();
151 }
152
153 void ProgressBar::setCoord(const IntCoord& _coord)
154 {
155 Base::setCoord(_coord);
156
157 updateTrack();
158 }
159
160 void ProgressBar::updateTrack()
161 {
162 if ((0 == mRange) || (0 == mEndPosition))
163 {
164 for (auto& iter : mVectorTrack)
165 {
166 iter->setVisible(false);
167 }
168 return;
169 }
170
171 if (mFillTrack)
172 {
173 if (mVectorTrack.empty())
174 {
175 Widget* widget =
176 mTrackPlace->createWidget<Widget>(mTrackSkin, IntCoord(), Align::Left | Align::VStretch);
177 mVectorTrack.push_back(widget);
178 }
179 else
180 {
181 // show first, hide the rest
182 VectorWidgetPtr::iterator iter = mVectorTrack.begin();
183 (*iter)->setVisible(true);
184 (*iter)->setAlpha(ALPHA_MAX);
185
186 ++iter;
187 for (; iter != mVectorTrack.end(); ++iter)
188 {
189 (*iter)->setVisible(false);
190 }
191 }
192
193 Widget* wid = mVectorTrack.front();
194
195 if ((0 == mStartPosition) && (mRange == mEndPosition))
196 {
197 setTrackPosition(wid, 0, 0, getClientWidth(), getClientHeight());
198 }
199 else
200 {
201 int pos = (int)mStartPosition * (getClientWidth() - mTrackMin) / (int)mRange;
202 setTrackPosition(
203 wid,
204 pos,
205 0,
206 ((int)mEndPosition * (getClientWidth() - mTrackMin) / (int)mRange) - pos + mTrackMin,
207 getClientHeight());
208 }
209
210 return;
211 }
212
213 int width = getClientWidth() - mTrackWidth + mTrackStep;
214 int count = width / mTrackStep;
215 int ost = (width % mTrackStep);
216 if (ost > 0)
217 {
218 width += mTrackStep - ost;
219 count++;
220 }
221
222 while ((int)mVectorTrack.size() < count)
223 {
224 Widget* widget = mTrackPlace->createWidget<Widget>(
225 mTrackSkin,
226 IntCoord(/*(int)mVectorTrack.size() * mTrackStep, 0, mTrackWidth, getClientHeight()*/),
227 Align::Left | Align::VStretch);
228 widget->setVisible(false);
229 mVectorTrack.push_back(widget);
230 }
231
232 if ((0 == mStartPosition) && (mRange == mEndPosition))
233 {
234 int pos = 0;
235 for (auto& iter : mVectorTrack)
236 {
237 iter->setAlpha(ALPHA_MAX);
238 iter->setVisible(true);
239 setTrackPosition(iter, pos * mTrackStep, 0, mTrackWidth, getClientHeight());
240 pos++;
241 }
242 }
243 else
244 {
245 int hide_pix = (width * (int)mStartPosition / (int)mRange);
246 int hide_count = hide_pix / mTrackStep;
247 int show_pix = (width * (int)mEndPosition / (int)mRange);
248 int show_count = show_pix / mTrackStep;
249
250 int pos = 0;
251 for (auto& iter : mVectorTrack)
252 {
253 if (0 > show_count)
254 {
255 iter->setVisible(false);
256 }
257 else if (0 == show_count)
258 {
259 iter->setAlpha((float)(show_pix % mTrackStep) / (float)mTrackStep);
260 iter->setVisible(true);
261 setTrackPosition(iter, pos * mTrackStep, 0, mTrackWidth, getClientHeight());
262 }
263 else
264 {
265 if (0 < hide_count)
266 {
267 iter->setVisible(false);
268 }
269 else if (0 == hide_count)
270 {
271 iter->setAlpha(1.0f - ((float)(hide_pix % mTrackStep) / (float)mTrackStep));
272 iter->setVisible(true);
273 setTrackPosition(iter, pos * mTrackStep, 0, mTrackWidth, getClientHeight());
274 }
275 else
276 {
277 iter->setAlpha(ALPHA_MAX);
278 iter->setVisible(true);
279 setTrackPosition(iter, pos * mTrackStep, 0, mTrackWidth, getClientHeight());
280 }
281 }
282 hide_count--;
283 show_count--;
284 pos++;
285 }
286 }
287 }
288
289 void ProgressBar::setTrackPosition(Widget* _widget, int _left, int _top, int _width, int _height)
290 {
291 if (mFlowDirection == FlowDirection::LeftToRight)
292 _widget->setCoord(_left, _top, _width, _height);
293 else if (mFlowDirection == FlowDirection::RightToLeft)
294 _widget->setCoord(mTrackPlace->getWidth() - _left - _width, _top, _width, _height);
295 else if (mFlowDirection == FlowDirection::TopToBottom)
296 _widget->setCoord(_top, _left, _height, _width);
297 else if (mFlowDirection == FlowDirection::BottomToTop)
298 _widget->setCoord(_top, mTrackPlace->getHeight() - _left - _width, _height, _width);
299 }
300
301 int ProgressBar::getClientWidth() const
302 {
303 return mFlowDirection.isHorizontal() ? mTrackPlace->getWidth() : mTrackPlace->getHeight();
304 }
305
306 int ProgressBar::getClientHeight() const
307 {
308 return mFlowDirection.isHorizontal() ? mTrackPlace->getHeight() : mTrackPlace->getWidth();
309 }
310
311 void ProgressBar::setFlowDirection(FlowDirection _value)
312 {
313 mFlowDirection = _value;
314 updateTrack();
315 }
316
317 void ProgressBar::setPropertyOverride(std::string_view _key, std::string_view _value)
318 {
320 if (_key == "Range")
321 setProgressRange(utility::parseValue<size_t>(_value));
322
324 else if (_key == "RangePosition")
325 setProgressPosition(utility::parseValue<size_t>(_value));
326
328 else if (_key == "AutoTrack")
329 setProgressAutoTrack(utility::parseValue<bool>(_value));
330
332 else if (_key == "FlowDirection")
333 setFlowDirection(utility::parseValue<FlowDirection>(_value));
334
335 else
336 {
337 Base::setPropertyOverride(_key, _value);
338 return;
339 }
340
341 eventChangeProperty(this, _key, _value);
342 }
343
344 size_t ProgressBar::getProgressRange() const
345 {
346 return mRange;
347 }
348
349 size_t ProgressBar::getProgressPosition() const
350 {
351 return mEndPosition;
352 }
353
354 bool ProgressBar::getProgressAutoTrack() const
355 {
356 return mAutoTrack;
357 }
358
359 FlowDirection ProgressBar::getFlowDirection() const
360 {
361 return mFlowDirection;
362 }
363
364} // namespace MyGUI
template MYGUI_EXPORT int parseValue< int >(std::string_view)
T parseValue(std::string_view value)
constexpr float ALPHA_MAX
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
const size_t PROGRESS_AUTO_WIDTH
const float PROGRESS_AUTO_COEF
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
const size_t PROGRESS_AUTO_RANGE
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))