-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatsBar.cpp
More file actions
97 lines (80 loc) · 2.13 KB
/
Copy pathStatsBar.cpp
File metadata and controls
97 lines (80 loc) · 2.13 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
#include "StatsBar.hpp"
#include <iostream>
StatsBar::StatsBar(int maxHP, sf::Texture heart, sf::Texture halfHeart, sf::Texture emptyHeart){
_space = 0;
size.x = maxHP;
size.y = maxHP;
_maxHP = maxHP;
_actualHP = maxHP;
_heart = new ImgButton(heart, heart, this);
_halfHeart = new ImgButton(halfHeart, halfHeart, this);;
_emptyHeart = new ImgButton(emptyHeart, emptyHeart, this);;
updatePics();
}
StatsBar::~StatsBar(){
delete _heart;
delete _halfHeart;
delete _emptyHeart;
}
void StatsBar::updatePics() {
sf::Vector2f butSize(size.y, size.y);
_heart->setSize(butSize);
_halfHeart->setSize(butSize);
_emptyHeart->setSize(butSize);
for(Widget* widget : _widgets) {
delete widget;
}
_widgets.clear();
for(int i = 0; i < _maxHP; ++i ){
if(i+1 <= _actualHP){
add(new ImgButton(*_heart));
}
else {
if( i < _actualHP && _actualHP < i+1){
add(new ImgButton(*_halfHeart));
}
else {
add(new ImgButton(*_emptyHeart));
}
}
}
StatsBar::updateShape();
}
void StatsBar::updateShape(){
float pos_x = _space;
//if(_parent)
// pos_x = (_parent->getSize().x - getSize().x)/2.f;
for(Widget* widget : _widgets) {
sf::Vector2f auxsize = widget->getSize();
widget->setPosition(pos_x,0);
pos_x += auxsize.x + _space;
}
Widget::updateShape();
}
void StatsBar::setMaxHP(int value){
if(_maxHP == value) return;
_maxHP = value;
updatePics();
}
void StatsBar::setActualHP(float value){
if( _actualHP == value) return;
_actualHP = value;
updatePics();
}
/*sf::Vector2f StatsBar::getSize(){
return size;
}*/
sf::Vector2f StatsBar::getSize() const {
float max_y = 0;
float x = 0;
for(Widget* widget : _widgets) {
sf::Vector2f size = widget->getSize();
if(size.y > max_y) max_y = size.y;
x += _space + size.x;
}
return sf::Vector2f(x+_space, max_y+_space*2);
}
void StatsBar::setSize(const sf::Vector2f &value){
size = value;
updatePics();
}