summaryrefslogtreecommitdiff
path: root/src/gui/widgets/progressbar.cpp
blob: 0e01876f5602bf547d250a5a955452e77c1c8568 (plain) (blame)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2017  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  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
 *  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/>.
 */

#include "gui/widgets/progressbar.h"

#include "settings.h"

#include "gui/gui.h"
#include "gui/skin.h"

#include "gui/fonts/font.h"

#include "utils/delete2.h"

#include "render/graphics.h"

#include "render/vertexes/imagecollection.h"

#include "debug.h"

int ProgressBar::mInstances = 0;
float ProgressBar::mAlpha = 1.0;

ProgressBar::ProgressBar(const Widget2 *const widget,
                         float progress,
                         const int width,
                         const int height,
                         const ProgressColorIdT backColor,
                         const std::string &skin,
                         const std::string &skinFill) :
    Widget(widget),
    WidgetListener(),
    mFillRect(),
    mTextChunk(),
    mSkin(nullptr),
    mProgress(progress),
    mProgressToGo(progress),
    mBackgroundColorToGo(),
    mText(),
    mVertexes(new ImageCollection),
    mProgressPalette(backColor),
    mPadding(2),
    mFillPadding(3),
    mFillImage(false),
    mSmoothProgress(true),
    mSmoothColorChange(true),
    mTextChanged(true)
{
    mBackgroundColor = Theme::getProgressColor(
        backColor >= ProgressColorId::PROG_HP
        ? backColor : ProgressColorId::PROG_HP,
        mProgress);
    mBackgroundColorToGo = mBackgroundColor;
    mForegroundColor2 = getThemeColor(ThemeColorId::PROGRESS_BAR_OUTLINE,
        255U);

    // The progress value is directly set at load time:
    if (mProgress > 1.0F || mProgress < 0.0F)
        mProgress = 1.0F;

    mForegroundColor = getThemeColor(ThemeColorId::PROGRESS_BAR, 255U);
    addWidgetListener(this);
    setSize(width, height);

    if (theme != nullptr)
    {
        mSkin = theme->load(skin,
            "progressbar.xml",
            true,
            theme->getThemePath());
        if (mSkin != nullptr)
        {
            setPadding(mSkin->getPadding());
            mFillPadding = mSkin->getOption("fillPadding");
            mFillImage = mSkin->getOption("fillImage") != 0;
            if (mFillImage)
            {
                theme->loadRect(mFillRect,
                    skinFill,
                    "progressbar_fill.xml",
                    0,
                    8);
            }
        }
        setHeight(2 * mPadding + getFont()->getHeight() + 2);
    }

    mInstances++;
}

ProgressBar::~ProgressBar()
{
    if (gui != nullptr)
        gui->removeDragged(this);

    mInstances--;
    if (mSkin != nullptr)
    {
        if (theme != nullptr)
            theme->unload(mSkin);
        mSkin = nullptr;
    }
    Theme::unloadRect(mFillRect, 0, 8);
    delete2(mVertexes);
    mTextChunk.deleteImage();
}

void ProgressBar::logic()
{
    BLOCK_START("ProgressBar::logic")
    if (mSmoothColorChange && mBackgroundColorToGo != mBackgroundColor)
    {
        // Smoothly changing the color for a nicer effect.
        if (mBackgroundColorToGo.r > mBackgroundColor.r)
            mBackgroundColor.r++;
        if (mBackgroundColorToGo.g > mBackgroundColor.g)
            mBackgroundColor.g++;
        if (mBackgroundColorToGo.b > mBackgroundColor.b)
            mBackgroundColor.b++;
        if (mBackgroundColorToGo.r < mBackgroundColor.r)
            mBackgroundColor.r--;
        if (mBackgroundColorToGo.g < mBackgroundColor.g)
            mBackgroundColor.g--;
        if (mBackgroundColorToGo.b < mBackgroundColor.b)
            mBackgroundColor.b--;
        mRedraw = true;
    }

    if (mSmoothProgress && mProgressToGo != mProgress)
    {
        // Smoothly showing the progressbar changes.
        if (mProgressToGo > mProgress)
            mProgress = std::min(1.0F, mProgress + 0.005F);
        if (mProgressToGo < mProgress)
            mProgress = std::max(0.0F, mProgress - 0.005F);
        mRedraw = true;
    }
    BLOCK_END("ProgressBar::logic")
}

void ProgressBar::updateAlpha()
{
    const float alpha = std::max(settings.guiAlpha,
        theme->getMinimumOpacity());
    mAlpha = alpha;
}

void ProgressBar::draw(Graphics *const graphics)
{
    BLOCK_START("ProgressBar::draw")
    if (mSkin == nullptr)
    {
        BLOCK_END("ProgressBar::draw")
        return;
    }

    updateAlpha();
    mBackgroundColor.a = CAST_U32(mAlpha * 255);

    if (mRedraw || graphics->getRedraw())
    {
        mRedraw = false;
        mVertexes->clear();
        graphics->calcWindow(mVertexes, 0, 0,
            mDimension.width, mDimension.height, mSkin->getBorder());
        if (mFillImage)
        {
            const unsigned int pad = 2 * mFillPadding;
            const int maxWidth = mDimension.width - pad;
            int width = CAST_S32(mProgress
                * static_cast<float>(maxWidth));
            if (width > 0)
            {
                if (width > maxWidth)
                    width = maxWidth;
                graphics->calcWindow(mVertexes, mFillPadding, mFillPadding,
                    width, mDimension.height - pad, mFillRect);
            }
        }
        graphics->finalize(mVertexes);
    }

    graphics->drawTileCollection(mVertexes);

    // The bar
    if (!mFillImage && mProgress > 0)
    {
        graphics->setColor(mBackgroundColor);
        const unsigned int pad = 2 * mFillPadding;
        const int maxWidth = mDimension.width - pad;
        int width = CAST_S32(mProgress * static_cast<float>(maxWidth));
        if (width > 0)
        {
            if (width > maxWidth)
                width = maxWidth;
            graphics->fillRectangle(Rect(mFillPadding, mFillPadding,
                width, mDimension.height - pad));
        }
    }

    // The label
    if (!mText.empty())
    {
        Font *const font = gui->getFont();
        if (mTextChanged)
        {
            mTextChunk.textFont = font;
            mTextChunk.deleteImage();
            mTextChunk.text = mText;
            mTextChunk.color = mForegroundColor;
            mTextChunk.color2 = mForegroundColor2;
            font->generate(mTextChunk);
            mTextChanged = false;
        }

        const Image *const image = mTextChunk.img;
        if (image != nullptr)
        {
            const int textX = (mDimension.width - font->getWidth(mText)) / 2;
            const int textY = (mDimension.height - font->getHeight()) / 2;
            graphics->drawImage(image, textX, textY);
        }
    }
    BLOCK_END("ProgressBar::draw")
}

void ProgressBar::safeDraw(Graphics *const graphics)
{
    BLOCK_START("ProgressBar::safeDraw")
    if (mSkin == nullptr)
    {
        BLOCK_END("ProgressBar::safeDraw")
        return;
    }

    updateAlpha();
    mBackgroundColor.a = CAST_U32(mAlpha * 255);

    graphics->drawImageRect(0, 0, mDimension.width, mDimension.height,
        mSkin->getBorder());
    if (mFillImage)
    {
        const unsigned int pad = 2 * mFillPadding;
        const int maxWidth = mDimension.width - pad;
        int width = CAST_S32(mProgress
            * static_cast<float>(maxWidth));
        if (width > 0)
        {
            if (width > maxWidth)
                width = maxWidth;
            graphics->drawImageRect(mFillPadding, mFillPadding,
                width, mDimension.height - pad, mFillRect);
        }
    }

    // The bar
    if (!mFillImage && mProgress > 0)
    {
        graphics->setColor(mBackgroundColor);
        const unsigned int pad = 2 * mFillPadding;
        const int maxWidth = mDimension.width - pad;
        int width = CAST_S32(mProgress * static_cast<float>(maxWidth));
        if (width > 0)
        {
            if (width > maxWidth)
                width = maxWidth;
            graphics->fillRectangle(Rect(mFillPadding, mFillPadding,
                width, mDimension.height - pad));
        }
    }

    // The label
    if (!mText.empty())
    {
        Font *const font = gui->getFont();
        if (mTextChanged)
        {
            mTextChunk.textFont = font;
            mTextChunk.deleteImage();
            mTextChunk.text = mText;
            mTextChunk.color = mForegroundColor;
            mTextChunk.color2 = mForegroundColor2;
            font->generate(mTextChunk);
            mTextChanged = false;
        }

        const Image *const image = mTextChunk.img;
        if (image != nullptr)
        {
            const int textX = (mDimension.width - font->getWidth(mText)) / 2;
            const int textY = (mDimension.height - font->getHeight()) / 2;
            graphics->drawImage(image, textX, textY);
        }
    }
    BLOCK_END("ProgressBar::safeDraw")
}

void ProgressBar::setProgress(const float progress)
{
    const float p = std::min(1.0F, std::max(0.0F, progress));
    mProgressToGo = p;
    mRedraw = true;

    if (!mSmoothProgress)
        mProgress = p;

    if (mProgressPalette >= ProgressColorId::PROG_HP)
    {
        mBackgroundColorToGo = Theme::getProgressColor(
            mProgressPalette, progress);
    }
}

void ProgressBar::setProgressPalette(const ProgressColorIdT progressPalette)
{
    const ProgressColorIdT oldPalette = mProgressPalette;
    mProgressPalette = progressPalette;
    mRedraw = true;

    if (mProgressPalette != oldPalette &&
        mProgressPalette >= ProgressColorId::PROG_HP)
    {
        mBackgroundColorToGo = Theme::getProgressColor(
            mProgressPalette, mProgressToGo);
    }
}

void ProgressBar::setBackgroundColor(const Color &color)
{
    mRedraw = true;
    mBackgroundColorToGo = color;

    if (!mSmoothColorChange)
        mBackgroundColor = color;
}

void ProgressBar::setColor(const Color &color1, const Color &color2)
{
    mForegroundColor = color1;
    mForegroundColor2 = color2;
    mTextChanged = true;
}

void ProgressBar::widgetResized(const Event &event A_UNUSED)
{
    mRedraw = true;
}

void ProgressBar::widgetMoved(const Event &event A_UNUSED)
{
    mRedraw = true;
}

void ProgressBar::setText(const std::string &str)
{
    if (mText != str)
    {
        mText = str;
        mTextChanged = true;
    }
}

void ProgressBar::widgetHidden(const Event &event A_UNUSED)
{
    mTextChanged = true;
    mTextChunk.deleteImage();
}