-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressBar.cpp
More file actions
128 lines (100 loc) · 2.83 KB
/
ProgressBar.cpp
File metadata and controls
128 lines (100 loc) · 2.83 KB
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
#include "stdafx.h"
#include "ProgressBar.h"
// Constructors and Destructor:
gui::ProgressBar::ProgressBar(float x, float y, float width, float height, sf::Font* font, unsigned int char_size)
{
this->widthMax = width;
this->back.setSize(sf::Vector2f(width, height));
this->back.setFillColor(sf::Color(50, 50, 50, 200));
this->back.setPosition(x, y);
this->inner.setSize(this->back.getSize());
this->inner.setFillColor(sf::Color(250, 20, 20, 200));
this->inner.setPosition(this->back.getPosition());
this->fontPointer = font;
if (font)
{
this->text.setFont(*font);
this->text.setCharacterSize(char_size);
this->text.setString("0");
this->text.setPosition
(
this->back.getPosition().x + (this->back.getGlobalBounds().width / 10.f),
this->back.getPosition().y + (this->back.getGlobalBounds().height / 10.f)
);
}
}
gui::ProgressBar::~ProgressBar()
{
}
// Modifiers:
void gui::ProgressBar::setBackColor(const sf::Color& color)
{
this->back.setFillColor(color);
}
void gui::ProgressBar::setBackTexture(const std::string& file_path)
{
this->backTexture.loadFromFile(file_path);
this->back.setTexture(&this->backTexture);
}
void gui::ProgressBar::setInnerColor(const sf::Color& color)
{
this->inner.setFillColor(color);
}
void gui::ProgressBar::setInnerTexture(const std::string& file_path)
{
this->innerTexture.loadFromFile(file_path);
this->inner.setTexture(&this->innerTexture);
}
void gui::ProgressBar::setPosition(const float x, const float y)
{
this->back.setPosition(x, y);
this->inner.setPosition(x, y);
}
void gui::ProgressBar::setPosition(const sf::Vector2f& position)
{
this->back.setPosition(position);
this->inner.setPosition(position);
}
void gui::ProgressBar::setTextString(const std::string& str)
{
this->text.setString(str);
}
void gui::ProgressBar::setTextPosition(const float x, const float y)
{
this->text.setPosition(x, y);
}
void gui::ProgressBar::setTextPosition(const sf::Vector2f& position)
{
this->text.setPosition(position);
}
// Functions:
void gui::ProgressBar::update(const int current_value, const int max_value, const float& dt, const bool update_color)
{
float percent = static_cast<float>(current_value / static_cast<float>(max_value));
this->inner.setSize
(
sf::Vector2f(
static_cast<float>(std::floor(this->widthMax * percent)),
this->inner.getSize().y
)
);
if (this->fontPointer)
{
this->barString = std::to_string(current_value) + " / " + std::to_string(max_value);
this->text.setString(this->barString);
}
if (update_color)
{
float color = 254.f;
this->inner.setFillColor(sf::Color(static_cast<unsigned char>(color - pow(color, percent)), static_cast<unsigned char>(color * (percent)), 20, 200));
}
}
void gui::ProgressBar::render(sf::RenderTarget& target)
{
target.draw(this->back);
target.draw(this->inner);
if (this->fontPointer)
{
target.draw(this->text);
}
}