-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRankView.cpp
More file actions
177 lines (149 loc) · 3.8 KB
/
RankView.cpp
File metadata and controls
177 lines (149 loc) · 3.8 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
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
#include "RankView.h"
#include <fstream>
#include <algorithm>
void RankView::draw(sf::RenderTarget & target, sf::RenderStates states) const
{
for (int i = 0; i < _rank.size(); i++)
target.draw(_rank[i]);
}
RankView::RankView(std::string src, int screen_x, int screen_y, int x, int y, double percent_size_x, sf::Font & font, int font_size, sf::Color color, sf::RenderWindow &window)
{
_source_file = src;
_screen_width = screen_x;
_screen_height = screen_y;
_x = x;
_y = y;
_font_size = font_size;
_size.x = (_screen_width * percent_size_x);
_size.y = (_font_size * 1.5);
_font = &font;
_color = color;
_window = &window;
}
RankView::~RankView()
{
}
void RankView::loadRankFromFile()
{
int lp = 1;
int temp_y = _y;
//load data in format "scrore name"
std::fstream in;
in.open(_source_file, std::ios::in);
if (!in.is_open()) //create file if doesn't exist
{
in.clear();
in.open(_source_file, std::ios::out);
in.close();
}
else
{
int val = 0;
std::string get = "", name = "";
while (std::getline(in, get))
{
val = atoi(get.c_str());
name = get.substr(get.find(" ") + 1);
RankPosition temp(lp, name, val, _size, _color, *_font, _font_size, _x, temp_y);
_rank.push_back(temp);
temp_y += _size.y + 20;
lp++;
}
}
last_y = temp_y;
return;
}
void RankView::saveRankToFile()
{
std::fstream out;
out.open(_source_file, std::ios::out);
for (int i = 0; i < _rank.size(); i++)
out << _rank[i].getScore() << " " << _rank[i].getNick() << std::endl;
}
void RankView::addToRank(int val, std::string name)
{
RankPosition temp(_rank.size(), name, val, _size, _color, *_font, _font_size, _x, last_y);
_rank.push_back(temp);
last_y += _size.y + 20;
}
void RankView::sortRank()
{
std::sort(_rank.begin(), _rank.end(), RankPosition::compScore);
while (_rank.size()> 50)
{
_rank.pop_back();
}
int temp_y = _y;
for (int i = 0; i < _rank.size(); i++)
{
_rank[i].updateLp(i + 1);
_rank[i].setPosY(temp_y);
temp_y += _size.y + 20;
}
}
void RankView::RankPosition::draw(sf::RenderTarget & target, sf::RenderStates states) const
{
target.draw(_back);
for (int i = 0; i < 3;i++)
target.draw(_text[i]);
}
RankView::RankPosition::RankPosition(int lp, std::string nick, int score, sf::Vector2f size, sf::Color color, sf::Font &font, int &font_size, int x , int y)
{
_back.setFillColor(color);
_back.setSize(size);
_back.setPosition(x, y);
_Lp = lp;
_nick = nick;
_Score = score;
_text[0].setString(std::to_string(_Lp));
_text[1].setString(_nick);
_text[2].setString(std::to_string(_Score));
for (int i = 0; i < 3;i++)
{
_text[i].setFont(font);
_text[i].setCharacterSize(font_size);
_text[i].setColor(sf::Color::Black);
sf::FloatRect title_pos = _text[i].getLocalBounds();
_text[i].setOrigin(title_pos.width / 2, title_pos.height / 2);
switch (i)
{
case 0:
_text[i].setPosition(x + title_pos.width + size.x*0.01, y + size.y/2);
break;
case 1:
_text[i].setPosition(x + (size.x * 0.2) + (title_pos.width/2), y + (size.y / 2));
break;
case 2:
_text[i].setPosition(x + size.x - (title_pos.width/2) - size.x*0.01, y + size.y / 2);
break;
}
}
}
void RankView::RankPosition::updateLp(int position)
{
_Lp = position;
_text[0].setString(std::to_string(_Lp));
}
int RankView::RankPosition::getScore()
{
return _Score;
}
std::string RankView::RankPosition::getNick()
{
return _nick;
}
int RankView::RankPosition::compScore(RankPosition a, RankPosition b)
{
return (a._Score > b._Score);
}
void RankView::RankPosition::setPosY(int y)
{
int size_y = _back.getGlobalBounds().height;
_back.setPosition(_back.getGlobalBounds().left, y);
for (int i = 0; i < 3;i++)
{
sf::FloatRect title_pos = _text[i].getLocalBounds();
_text[i].setOrigin(title_pos.width / 2, title_pos.height / 2);
_text[i].setPosition(_text[i].getGlobalBounds().left, y + size_y / 2);
}
}