ManaPlus
progressbar.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "settings.h"
26 
27 #include "gui/gui.h"
28 #include "gui/skin.h"
29 
30 #include "gui/fonts/font.h"
31 
32 #include "utils/delete2.h"
33 
34 #include "render/graphics.h"
35 
37 
38 #include "debug.h"
39 
41 float ProgressBar::mAlpha = 1.0;
42 
43 ProgressBar::ProgressBar(const Widget2 *const widget,
44  float progress,
45  const int width,
46  const int height,
47  const ProgressColorIdT backColor,
48  const std::string &skin,
49  const std::string &skinFill) :
50  Widget(widget),
52  mFillRect(),
53  mTextChunk(),
54  mSkin(nullptr),
55  mProgress(progress),
56  mProgressToGo(progress),
57  mBackgroundColorToGo(),
58  mText(),
59  mVertexes(new ImageCollection),
60  mProgressPalette(backColor),
61  mPadding(2),
62  mFillPadding(3),
63  mFillImage(false),
64  mSmoothProgress(true),
65  mSmoothColorChange(true),
66  mTextChanged(true)
67 {
69  backColor >= ProgressColorId::PROG_HP
70  ? backColor : ProgressColorId::PROG_HP,
71  mProgress);
74  255U);
75 
76  // The progress value is directly set at load time:
77  if (mProgress > 1.0F || mProgress < 0.0F)
78  mProgress = 1.0F;
79 
81  addWidgetListener(this);
82  setSize(width, height);
83 
84  if (theme != nullptr)
85  {
86  mSkin = theme->load(skin,
87  "progressbar.xml",
88  true,
90  if (mSkin != nullptr)
91  {
93  mFillPadding = mSkin->getOption("fillPadding");
94  mFillImage = mSkin->getOption("fillImage") != 0;
95  if (mFillImage)
96  {
98  skinFill,
99  "progressbar_fill.xml",
100  0,
101  8);
102  }
103  }
104  setHeight(2 * mPadding + getFont()->getHeight() + 2);
105  }
106 
107  mInstances++;
108 }
109 
111 {
112  if (gui != nullptr)
113  gui->removeDragged(this);
114 
115  mInstances--;
116  if (mSkin != nullptr)
117  {
118  if (theme != nullptr)
119  theme->unload(mSkin);
120  mSkin = nullptr;
121  }
125 }
126 
128 {
129  BLOCK_START("ProgressBar::logic")
131  {
132  // Smoothly changing the color for a nicer effect.
145  mRedraw = true;
146  }
147 
149  {
150  // Smoothly showing the progressbar changes.
151  if (mProgressToGo > mProgress)
152  mProgress = std::min(1.0F, mProgress + 0.005F);
153  if (mProgressToGo < mProgress)
154  mProgress = std::max(0.0F, mProgress - 0.005F);
155  mRedraw = true;
156  }
157  BLOCK_END("ProgressBar::logic")
158 }
159 
161 {
162  const float alpha = std::max(settings.guiAlpha,
164  mAlpha = alpha;
165 }
166 
167 void ProgressBar::draw(Graphics *const graphics)
168 {
169  BLOCK_START("ProgressBar::draw")
170  if (mSkin == nullptr)
171  {
172  BLOCK_END("ProgressBar::draw")
173  return;
174  }
175 
176  updateAlpha();
178 
179  if (mRedraw || graphics->getRedraw())
180  {
181  mRedraw = false;
182  mVertexes->clear();
183  graphics->calcWindow(mVertexes, 0, 0,
185  if (mFillImage)
186  {
187  const unsigned int pad = 2 * mFillPadding;
188  const int maxWidth = mDimension.width - pad;
189  int width = CAST_S32(mProgress
190  * static_cast<float>(maxWidth));
191  if (width > 0)
192  {
193  if (width > maxWidth)
194  width = maxWidth;
196  width, mDimension.height - pad, mFillRect);
197  }
198  }
199  graphics->finalize(mVertexes);
200  }
201 
202  graphics->drawTileCollection(mVertexes);
203 
204  // The bar
205  if (!mFillImage && mProgress > 0)
206  {
207  graphics->setColor(mBackgroundColor);
208  const unsigned int pad = 2 * mFillPadding;
209  const int maxWidth = mDimension.width - pad;
210  int width = CAST_S32(mProgress * static_cast<float>(maxWidth));
211  if (width > 0)
212  {
213  if (width > maxWidth)
214  width = maxWidth;
216  width, mDimension.height - pad));
217  }
218  }
219 
220  // The label
221  if (!mText.empty())
222  {
223  Font *const font = gui->getFont();
224  if (mTextChanged)
225  {
231  font->generate(mTextChunk);
232  mTextChanged = false;
233  }
234 
235  const Image *const image = mTextChunk.img;
236  if (image != nullptr)
237  {
238  const int textX = (mDimension.width - font->getWidth(mText)) / 2;
239  const int textY = (mDimension.height - font->getHeight()) / 2;
240  graphics->drawImage(image, textX, textY);
241  }
242  }
243  BLOCK_END("ProgressBar::draw")
244 }
245 
246 void ProgressBar::safeDraw(Graphics *const graphics)
247 {
248  BLOCK_START("ProgressBar::safeDraw")
249  if (mSkin == nullptr)
250  {
251  BLOCK_END("ProgressBar::safeDraw")
252  return;
253  }
254 
255  updateAlpha();
257 
259  mSkin->getBorder());
260  if (mFillImage)
261  {
262  const unsigned int pad = 2 * mFillPadding;
263  const int maxWidth = mDimension.width - pad;
264  int width = CAST_S32(mProgress
265  * static_cast<float>(maxWidth));
266  if (width > 0)
267  {
268  if (width > maxWidth)
269  width = maxWidth;
271  width, mDimension.height - pad, mFillRect);
272  }
273  }
274 
275  // The bar
276  if (!mFillImage && mProgress > 0)
277  {
278  graphics->setColor(mBackgroundColor);
279  const unsigned int pad = 2 * mFillPadding;
280  const int maxWidth = mDimension.width - pad;
281  int width = CAST_S32(mProgress * static_cast<float>(maxWidth));
282  if (width > 0)
283  {
284  if (width > maxWidth)
285  width = maxWidth;
287  width, mDimension.height - pad));
288  }
289  }
290 
291  // The label
292  if (!mText.empty())
293  {
294  Font *const font = gui->getFont();
295  if (mTextChanged)
296  {
302  font->generate(mTextChunk);
303  mTextChanged = false;
304  }
305 
306  const Image *const image = mTextChunk.img;
307  if (image != nullptr)
308  {
309  const int textX = (mDimension.width - font->getWidth(mText)) / 2;
310  const int textY = (mDimension.height - font->getHeight()) / 2;
311  graphics->drawImage(image, textX, textY);
312  }
313  }
314  BLOCK_END("ProgressBar::safeDraw")
315 }
316 
317 void ProgressBar::setProgress(const float progress)
318 {
319  const float p = std::min(1.0F, std::max(0.0F, progress));
320  mProgressToGo = p;
321  mRedraw = true;
322 
323  if (!mSmoothProgress)
324  mProgress = p;
325 
327  {
329  mProgressPalette, progress);
330  }
331 }
332 
334 {
335  const ProgressColorIdT oldPalette = mProgressPalette;
336  mProgressPalette = progressPalette;
337  mRedraw = true;
338 
339  if (mProgressPalette != oldPalette &&
341  {
344  }
345 }
346 
348 {
349  mRedraw = true;
350  mBackgroundColorToGo = color;
351 
352  if (!mSmoothColorChange)
353  mBackgroundColor = color;
354 }
355 
356 void ProgressBar::setColor(const Color &color1, const Color &color2)
357 {
358  mForegroundColor = color1;
359  mForegroundColor2 = color2;
360  mTextChanged = true;
361 }
362 
364 {
365  mRedraw = true;
366 }
367 
369 {
370  mRedraw = true;
371 }
372 
373 void ProgressBar::setText(const std::string &str)
374 {
375  if (mText != str)
376  {
377  mText = str;
378  mTextChanged = true;
379  }
380 }
381 
383 {
384  mTextChanged = true;
386 }
void setSize(const int width, const int height)
Definition: widget.cpp:366
#define CAST_U32
Definition: cast.h:30
Font * getFont() const
Definition: widget.cpp:330
ProgressColorId ::T ProgressColorIdT
unsigned int mFillPadding
Definition: progressbar.h:155
void loadRect(ImageRect &image, const std::string &name, const std::string &name2, const int start, const int end)
Definition: theme.cpp:1092
unsigned int mPadding
Definition: progressbar.h:154
int width
Definition: rect.h:218
std::string text
Definition: textchunk.h:64
virtual void drawTileCollection(const ImageCollection *const vertCol)=0
ImageCollection * mVertexes
Definition: progressbar.h:152
float mProgress
Definition: progressbar.h:146
void unload(Skin *const skin)
Definition: theme.cpp:249
Gui * gui
Definition: gui.cpp:110
static int mInstances
Definition: progressbar.h:157
Definition: font.h:88
void widgetMoved(const Event &event)
unsigned int a
Definition: color.h:250
virtual void drawImageRect(const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
Color mBackgroundColorToGo
Definition: progressbar.h:149
float getMinimumOpacity() const
Definition: theme.h:123
unsigned int g
Definition: color.h:239
Definition: rect.h:72
Skin * mSkin
Definition: progressbar.h:145
#define BLOCK_START(name)
Definition: perfomance.h:78
bool mTextChanged
Definition: progressbar.h:163
static std::string getThemePath()
Definition: theme.h:66
Skin * load(const std::string &filename, const std::string &filename2, const bool full, const std::string &defaultPath)
Definition: theme.cpp:178
#define BLOCK_END(name)
Definition: perfomance.h:79
Font * getFont() const
Definition: gui.h:159
void safeDraw(Graphics *const graphics)
#define delete2(var)
Definition: delete2.h:24
TextChunk mTextChunk
Definition: progressbar.h:144
Color mForegroundColor
Definition: widget.h:1085
Font * textFont
Definition: textchunk.h:63
int getOption(const std::string &name) const
Definition: skin.h:105
virtual void finalize(ImageCollection *const col)
Definition: graphics.h:464
#define new
Definition: debug_new.h:147
Settings settings
Definition: settings.cpp:31
int getPadding() const
Definition: skin.h:99
void widgetHidden(const Event &event)
Color mBackgroundColor
Definition: widget.h:1090
virtual void setColor(const Color &color)
Definition: graphics.h:319
#define CAST_S32
Definition: cast.h:29
unsigned int b
Definition: color.h:244
const ImageRect & getBorder() const
Definition: skin.h:67
float mProgressToGo
Definition: progressbar.h:147
void setBackgroundColor(const Color &color)
bool mFillImage
Definition: progressbar.h:160
bool mSmoothColorChange
Definition: progressbar.h:162
#define nullptr
Definition: localconsts.h:44
int height
Definition: rect.h:223
std::string mText
Definition: progressbar.h:151
bool getRedraw() const
Definition: graphics.h:286
ImageRect mFillRect
Definition: progressbar.h:143
void setText(const std::string &str)
void setHeight(const int height)
Definition: widget.cpp:139
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
void setProgressPalette(const ProgressColorIdT progressPalette)
virtual void fillRectangle(const Rect &rectangle)=0
Theme * theme
Definition: theme.cpp:61
#define A_UNUSED
Definition: localconsts.h:171
Color mForegroundColor2
Definition: widget2.h:112
Image * img
Definition: textchunk.h:62
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1160
Definition: widget.h:97
bool mSmoothProgress
Definition: progressbar.h:161
void setPadding(unsigned int padding)
Definition: progressbar.h:139
int getWidth(const std::string &text) const
Definition: font.cpp:333
Definition: event.h:77
bool mRedraw
Definition: widget.h:1163
void widgetResized(const Event &event)
ProgressColorIdT mProgressPalette
Definition: progressbar.h:153
Definition: image.h:61
int getHeight() const
Definition: widget.h:239
Color color
Definition: textchunk.h:65
void draw(Graphics *const graphics)
Definition: color.h:74
static Color getProgressColor(const ProgressColorIdT type, const float progress)
Definition: theme.cpp:154
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
int getHeight() const
Definition: font.cpp:360
void deleteImage()
Definition: textchunk.cpp:192
static void unloadRect(const ImageRect &rect, const int start, const int end)
Definition: theme.cpp:1124
float guiAlpha
Definition: settings.h:129
Color color2
Definition: textchunk.h:66
Rect mDimension
Definition: widget.h:1100
void setColor(const Color &color1, const Color &color2)
ProgressBar(const Widget2 *const widget, float progress, const int width, const int height, const ProgressColorIdT backColor, const std::string &skin, const std::string &skinFill)
Definition: progressbar.cpp:43
static float mAlpha
Definition: progressbar.h:158
void updateAlpha()
void setProgress(const float progress)
virtual void calcWindow(ImageCollection *const vertCol, const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
unsigned int r
Definition: color.h:234
void generate(TextChunk &chunk)
Definition: font.cpp:428