forked from arvindrajayadav/Good-Robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RectangleProgressBar.cpp
122 lines (95 loc) · 2.85 KB
/
RectangleProgressBar.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
#include "master.h"
#include "RectangleProgressBar.h"
using namespace pyrodactyl;
void RectangleProgressBar::Load(rapidxml::xml_node<char> *node)
{
Element::Load(node);
timer.Load(node, "delta_time");
if (NodeValid("bg", node))
bg.Load(node->first_node("bg"), this);
if (NodeValid("caption", node))
caption.Load(node->first_node("caption"), this);
if (NodeValid("bar", node))
{
rapidxml::xml_node<char> *barnode = node->first_node("bar");
if (NodeValid("max", barnode))
max.Load(barnode->first_node("max"));
if (NodeValid("cur", barnode))
cur.Load(barnode->first_node("cur"));
if (NodeValid("inc", barnode))
change_inc.Load(barnode->first_node("inc"));
if (NodeValid("dec", barnode))
change_dec.Load(barnode->first_node("dec"));
if (NodeValid("offset", barnode))
offset.Load(barnode->first_node("offset"));
}
}
void RectangleProgressBar::Draw(const int &value, const int &maximum, const char *title, bool draw_value)
{
//Prevent divide by zero
if (maximum == 0)
return;
bg.Draw();
if (!init)
{
init = true;
prev = value;
}
//Calculate pixels per unit according to the size IF the bar is of constant width
pixels_per_unit = w / (float)maximum;
//Draw the outline bar that depends on the maximum value of health
max.Draw(x, y, h, pixels_per_unit, maximum, false);
//Add the offset for all other bars
int X = x + offset.x, Y = y + offset.y;
if (prev > value)
{
//The value decreased, draw the decrease bar
change_dec.Draw(X + static_cast<int>(value * pixels_per_unit), Y, h, pixels_per_unit, prev - value, true);
//Decrease the bar value so it moves 1px forward every X ms, and eventually becomes equal to value
if (timer.TargetReached())
{
prev -= 1 + static_cast<int>(pixels_per_unit / 2);
if (prev < value)
prev = value;
//Reset the timer
timer.Start();
}
//Draw the current bar
cur.Draw(X, Y, h, pixels_per_unit, value, true);
}
else if (prev < value)
{
//Draw the current bar
cur.Draw(X, Y, h, pixels_per_unit, value, true);
//The value increased, draw the increase bar
change_inc.Draw(X + static_cast<int>(prev * pixels_per_unit), Y, h, pixels_per_unit, value - prev, true);
//Increase the bar value so it moves 1px forward every X ms, and eventually becomes equal to value
if (timer.TargetReached())
{
prev += 1 + static_cast<int>(pixels_per_unit / 2);
if (prev > value)
prev = value;
//Reset the timer
timer.Start();
}
}
else
cur.Draw(X, Y, h, pixels_per_unit, value, true);
//Draw the caption
if (draw_value)
caption.text = title + NumberToString<int>(value) +" / " + NumberToString<int>(maximum);
else
caption.text = title;
caption.Draw();
}
void RectangleProgressBar::SetUI(pyroRect *parent)
{
Element::SetUI(parent);
bg.SetUI(this);
caption.SetUI(this);
offset.SetUI();
max.SetUI();
cur.SetUI();
change_inc.SetUI();
change_dec.SetUI();
}