-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneMenu.cpp
More file actions
239 lines (199 loc) · 8.1 KB
/
Copy pathSceneMenu.cpp
File metadata and controls
239 lines (199 loc) · 8.1 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
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
#include "SceneMenu.hpp"
#include "Game.hpp"
SceneMenu::SceneMenu(Game* g, sf::RenderWindow* w) :
Scene(g,w,sceneTypes::menu, "menu"),
_menu(*w,_view),
_options(*w,_view) {
_game = g;
sf::Vector2u targetResolution(1920,1080);
initView(&_view, sf::Vector2i(targetResolution));
_buttonSelected = -1;
_menuLayout = nullptr;
_optionsLayout = nullptr;
initButtons();
}
SceneMenu::~SceneMenu() {
}
void SceneMenu::init(sf::Vector2f ) {
_elapsed = 0;
resetMenuPosition();
_selectedLayout = _menuLayout;
initButtons();
}
void SceneMenu::processInput() {
_window->setView(_view);
sf::Event event;
while (_window->pollEvent(event)) {
if (_selectedLayout == _menuLayout) _menu.processEvent(event);
else if(_selectedLayout == _optionsLayout) _options.processEvent(event);
if (event.type == sf::Event::Closed) {_window->close(); exit(0);}
else if (event.type == sf::Event::MouseMoved) {
_window->setMouseCursorVisible(true);
resetMenuPosition();
}
else if (event.type == sf::Event::LostFocus) _focus = false;
else if (event.type == sf::Event::Resized) resizing();
}
InputManager::update();
if (_elapsed < 0.2) return;
if (InputManager::action(InputAction::menuDown)) {
_elapsed = 0;
_window->setMouseCursorVisible(false);
changeButton(+1);
}
if (InputManager::action(InputAction::menuUp)) {
_elapsed = 0;
_window->setMouseCursorVisible(false);
changeButton(-1);
}
if (InputManager::action(InputAction::menuEnter)) {
if (_buttonSelected == -1) _buttonSelected = 0;
if (dynamic_cast<Button*>(_selectedLayout->at(_buttonSelected)) != nullptr)
dynamic_cast<Button*>(_selectedLayout->at(_buttonSelected))->onClick(sf::Event(),*dynamic_cast<Button*>(_selectedLayout->at(_buttonSelected)));
}
float axis = InputManager::action(InputAction::menuMovement);
if (std::abs(axis) > 0.5) {
_elapsed = 0;
_window->setMouseCursorVisible(false);
if (axis < 0) changeButton(-1);
else changeButton(+1);
}
_window->setView(_window->getDefaultView());
}
void SceneMenu::update(float deltaTime) {
_elapsed += deltaTime;
}
void SceneMenu::render(sf::RenderTarget*) {
_window->setView(_view);
if (_selectedLayout == _menuLayout) _menu.draw();
else if(_selectedLayout == _optionsLayout) _options.draw();
}
void SceneMenu::resetMenuPosition() {
if (_buttonSelected >= 0 && dynamic_cast<TextButton*>(_selectedLayout->at(_buttonSelected)) != nullptr) dynamic_cast<TextButton*>(_selectedLayout->at(_buttonSelected))->onMouseLeft();
_buttonSelected = -1;
}
void SceneMenu::initButtons() {
delete _menuLayout;
_menuLayout = new VLayout;
_selectedLayout = _menuLayout;
_menuLayout->setSpace(5);
if (DataManager::getBool("game", false)) {
TextButton* resB;
resB = new TextButton("Continue", Resources::pauseMenuFont);
resB->onClick = [this](const sf::Event&, Button&) {
sf::Vector2f playerPos = DataManager::getVector2f("playerPos");
std::string nextScene = DataManager::getString("playerScene");
ScenePlayable* aux = dynamic_cast<ScenePlayable*>( (*_game->_scenes.find(nextScene)).second );
aux->setPlayer(new Player());
aux->getPlayer()->setPosition(playerPos);
changeScene(new SceneChanger(sf::Vector2f(0,0), nextScene, playerPos));
};
_menuLayout->add(resB);
}
TextButton* resB;
resB = new TextButton("New Game", Resources::pauseMenuFont); // New game
resB->onClick = [this](const sf::Event&, Button&) {
changeScene(new SceneChanger(sf::Vector2f(0,0), "cutScene", sf::Vector2f(0,0)));
};
TextButton* resB2;
resB2 = new TextButton("Options", Resources::pauseMenuFont);
resB2->onClick = [this](const sf::Event&, Button&) {
_selectedLayout = _optionsLayout;
_buttonSelected = 1;
};
TextButton* exitB;
exitB = new TextButton("Exit", Resources::pauseMenuFont);
exitB->onClick = [this](const sf::Event&, Button&){ exit(0); };
_menuLayout->add(resB);
_menuLayout->add(resB2);
_menuLayout->add(exitB);
_menu.setLayout(_menuLayout);
// Options
delete _optionsLayout;
_optionsLayout = new VLayout;
_options.setLayout(_optionsLayout);
_optionsLayout->setSpace(10);
Label* musicLabel;
musicLabel = new Label("Music Volume", Resources::pauseMenuFont);
HLayout* musicLayout = new HLayout();
musicLayout->setSpace(5);
Label* soundLabel;
soundLabel = new Label("Sound Volume", Resources::pauseMenuFont);
HLayout* soundLayout = new HLayout();
soundLayout->setSpace(5);
TextButton* returnToMenu = new TextButton(" Return", Resources::pauseMenuFont);
returnToMenu->onClick = [this](const sf::Event&, Button&) {
init();
};
ImgButton* spacer = new ImgButton(Resources::emptyHeart,Resources::emptyHeart);
spacer->setSize(sf::Vector2f(0, 330));
spacer->setPosition(200,0);
// _optionsLayout->add(musicLabel);
// _optionsLayout->add(musicLayout);
// _optionsLayout->add(soundLabel);
// _optionsLayout->add(soundLayout);
// _optionsLayout->add(spacer);
// _optionsLayout->add(returnToMenu);
TextButton* musicMute;
musicMute = new TextButton(" Mute", Resources::pauseMenuFont);
musicMute->onClick = [this](const sf::Event&, Button&) {
SoundManager::setGlobalMusicVolumen(0);
DataManager::setFloat("MusicVolumen",0.0f);
DataManager::save();
};
TextButton* music50;
music50 = new TextButton(" 50%", Resources::pauseMenuFont);
music50->onClick = [this](const sf::Event&, Button&) {
SoundManager::setGlobalMusicVolumen(50);
DataManager::setFloat("MusicVolumen",50.0f);
DataManager::save();
};
TextButton* music100;
music100 = new TextButton(" 100%", Resources::pauseMenuFont);
music100->onClick = [this](const sf::Event&, Button&) {
SoundManager::setGlobalMusicVolumen(100);
DataManager::setFloat("MusicVolumen",100.0f);
DataManager::save();
};
// musicLayout->add(musicMute);
// musicLayout->add(music50);
// musicLayout->add(music100);
TextButton* soundMute;
soundMute = new TextButton(" Mute", Resources::pauseMenuFont);
soundMute->onClick = [this](const sf::Event&, Button&) {
SoundManager::setGlobalSoundVolumen(0);
DataManager::setFloat("SoundVolumen",0.0f);
DataManager::save();
};
TextButton* sound50;
sound50 = new TextButton(" 50", Resources::pauseMenuFont);
sound50->onClick = [this](const sf::Event&, Button&) {
SoundManager::setGlobalSoundVolumen(50);
DataManager::setFloat("SoundVolumen",50.0f);
DataManager::save();
};
TextButton* sound100;
sound100 = new TextButton(" 100", Resources::pauseMenuFont);
sound100->onClick = [this](const sf::Event&, Button&) {
SoundManager::setGlobalSoundVolumen(100);
DataManager::setFloat("SoundVolumen",100.0f);
DataManager::save();
};
// soundLayout->add(soundMute);
// soundLayout->add(sound50);
// soundLayout->add(sound100);
_optionsLayout->add(musicLabel);
_optionsLayout->add(musicMute);
_optionsLayout->add(music50);
_optionsLayout->add(music100);
_optionsLayout->add(soundLabel);
_optionsLayout->add(soundMute);
_optionsLayout->add(sound50);
_optionsLayout->add(sound100);
_optionsLayout->add(returnToMenu);
}
void SceneMenu::changeButton(int offset) {
if (_buttonSelected >= 0 && dynamic_cast<TextButton*>(_selectedLayout->at(_buttonSelected)) != nullptr) dynamic_cast<TextButton*>(_selectedLayout->at(_buttonSelected))->onMouseLeft();
_buttonSelected = (((_buttonSelected+offset)%_selectedLayout->getNWidgets()+_selectedLayout->getNWidgets())%_selectedLayout->getNWidgets());
if (dynamic_cast<TextButton*>(_selectedLayout->at(_buttonSelected)) != nullptr) dynamic_cast<TextButton*>(_selectedLayout->at(_buttonSelected))->onMouseEntered();
}