-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsState.cpp
270 lines (219 loc) · 6.73 KB
/
SettingsState.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
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
#include "stdafx.h"
#include "SettingsState.h"
//private: Functions:
void SettingsState::initVariables()
{
// this->modes = sf::VideoMode::getFullscreenModes();
this->modes.push_back(sf::VideoMode(1920, 1080));
this->modes.push_back(sf::VideoMode(1904, 1071));
this->modes.push_back(sf::VideoMode(1600, 900));
this->modes.push_back(sf::VideoMode(1584, 891));
this->modes.push_back(sf::VideoMode(1280, 720));
this->modes.push_back(sf::VideoMode(1264, 711));
}
//Should use '../' before the file path to be able to play the game without Visual Studio.
void SettingsState::initBackground(const std::string file_path)
{
sf::VideoMode vm = this->stateData->gfxSettings->resolution;
//Background initialization
this->background.setSize
(
sf::Vector2f
(
static_cast<float>(vm.width),
static_cast<float>(vm.height)
)
);
if (!this->backgroundTexture.loadFromFile(file_path))
{
std::cout << "ERROR::SettingsState::initBackground::Failed to load: " << file_path << std::endl;
this->background.setFillColor(sf::Color(30, 60, 60, 200));
}
else
{
this->background.setTexture(&this->backgroundTexture);
}
//Container initialization
this->container.setSize(sf::Vector2f(gui::percentIntoX(44.f, vm), gui::percentIntoY(56.f, vm)));
this->container.setFillColor(sf::Color(0, 40, 80, 140));
this->container.setPosition(sf::Vector2f(gui::percentIntoX(30.f, vm), gui::percentIntoY(12.f, vm)));
}
void SettingsState::initGui()
{
const sf::VideoMode& vm = this->stateData->gfxSettings->resolution;
//Background
this->initBackground("Resources/Images/Backgrounds/background_1280x720.png");
//Buttons
this->buttons["APPLY"] = new gui::Button
(
gui::percentIntoX(50.f, vm), gui::percentIntoY(60.f, vm), gui::percentIntoX(9.f, vm), gui::percentIntoY(4.f, vm),
&this->font, "Apply", gui::calculateCharSize(3.f, vm),
sf::Color::Transparent, sf::Color::Transparent, sf::Color::Transparent,
sf::Color(204, 204, 204), sf::Color(255, 235, 0), sf::Color(255, 100, 0)
);
this->buttons["EXIT_STATE"] = new gui::Button
(
gui::percentIntoX(62.f, vm), gui::percentIntoY(60.f, vm), gui::percentIntoX(9.f, vm), gui::percentIntoY(4.f, vm),
&this->font, "Back", gui::calculateCharSize(3.f, vm),
sf::Color::Transparent, sf::Color::Transparent, sf::Color::Transparent,
sf::Color(204, 204, 204), sf::Color(255, 235, 0), sf::Color(255, 100, 0)
);
//Modes
std::vector<std::string> modes_str;
for (auto& i : this->modes)
{
modes_str.push_back(std::to_string(i.width) + " x " + std::to_string(i.height));
}
//Dropdown list
this->dropDownLists["RESOLUTION"] = new gui::DropDownList
(
gui::percentIntoX(55.f, vm), gui::percentIntoY(15.f, vm),
gui::percentIntoX(12.f, vm), gui::percentIntoY(3.5f, vm),
this->stateData->gfxSettings->resolution,
this->settingsFont, gui::calculateCharSize(2.625f, vm),
modes_str.data(), static_cast<unsigned>(modes_str.size())
);
//Text
for (int32_t i = 0; i < 4; i++)
{
this->optionsText[i].setFont(this->font);
this->optionsText[i].setPosition(sf::Vector2f(gui::percentIntoX(33.f, vm), gui::percentIntoY(15.f, vm) + (gui::calculateCharSize(6.f, vm) * static_cast<float>(i))));
this->optionsText[i].setCharacterSize(gui::calculateCharSize(3.f, vm));
this->optionsText[i].setFillColor(sf::Color(255, 255, 255));
}
this->optionsText[0].setString("Resolution");
this->optionsText[1].setString("Fullscreen");
this->optionsText[2].setString("Framerate");
this->optionsText[3].setString("Anti-aliasing");
}
//Clears the GUI elements and reinitializes the GUI.
//@return void
void SettingsState::resetGui()
{
auto it_1 = this->buttons.begin();
for (it_1 = this->buttons.begin(); it_1 != this->buttons.end(); ++it_1)
{
delete it_1->second;
}
this->buttons.clear();
auto it_2 = this->dropDownLists.begin();
for (it_2 = this->dropDownLists.begin(); it_2 != this->dropDownLists.end(); ++it_2)
{
delete it_2->second;
}
this->dropDownLists.clear();
this->initGui();
this->initFpsCounter();
}
//Constructors and Destructor:
SettingsState::SettingsState(StateData* state_data)
: State(state_data)
{
this->initVariables();
this->initFont(this->font, "Fonts/slkscr.ttf");
this->initFont(this->settingsFont, "Fonts/slkscr.ttf");
this->initKeybinds("Config/mainmenustate_keybinds.ini");
this->initGui();
}
SettingsState::~SettingsState()
{
//Deleting buttons
auto it_i = this->buttons.begin();
for (it_i = this->buttons.begin(); it_i != this->buttons.end(); ++it_i)
{
delete it_i->second;
}
//Deleting drowdown lists
auto it_j = this->dropDownLists.begin();
for (it_j = this->dropDownLists.begin(); it_j != this->dropDownLists.end(); ++it_j)
{
delete it_j->second;
}
}
//Accessors:
//Functions:
void SettingsState::updateInput(const float& dt)
{
}
//Updates all GUI elements in the state and handles theis functionality.
void SettingsState::updateGui(const float& dt)
{
//Updating buttons
for (auto& it : this->buttons)
{
it.second->update(this->mousePositionWindow, false, true);
it.second->updateButtonColor(dt);
}
//Buttons functionality:
//Back to the main menu
if (this->buttons["EXIT_STATE"]->isPressed() && this->getKeyTime())
{
this->endState();
}
//Apply selected settings
if (this->buttons["APPLY"]->isPressed() && this->getKeyTime())
{
//TEST, REMOVE LATER
this->stateData->gfxSettings->resolution = this->modes[this->dropDownLists["RESOLUTION"]->getActiveElementId()];
this->window->create
(
this->stateData->gfxSettings->resolution,
this->stateData->gfxSettings->title,
this->stateData->gfxSettings->style,
this->stateData->gfxSettings->contextSettings
);
this->window->setFramerateLimit(this->stateData->gfxSettings->frameRateLimit);
this->window->setVerticalSyncEnabled(this->stateData->gfxSettings->verticalSync);
//this->window->setMouseCursorGrabbed(true);
this->resetGui();
}
//Updating dropdown lists
for (auto& it : this->dropDownLists)
{
it.second->update(this->mousePositionWindow, dt);
}
//Dropdown lists functionality:
}
void SettingsState::update(const float& dt)
{
this->updateMousePositions();
this->updateInput(dt);
this->updateGui(dt);
//Debug:
this->updateFpsCounter(dt);
// this->updateMousePositionText();
}
void SettingsState::renderGui(sf::RenderTarget& target)
{
//Buttons
for (auto& it : this->buttons)
{
it.second->render(target);
}
//Dropdown lists
for (auto& it : this->dropDownLists)
{
it.second->render(target);
}
}
void SettingsState::render(sf::RenderTarget* target)
{
//Whole state
if (!target)
{
target = this->window;
}
//Background
target->draw(this->background);
target->draw(this->container);
//GUI
this->renderGui(*target);
//Options text
for (int32_t i = 0; i < 4; i++)
{
target->draw(this->optionsText[i]);
}
//Debug:
this->renderFpsCounter(target);
// this->renderMousePositionText(target);
}