Skip to content

Commit 8dce20e

Browse files
committed
starting to refactor main menu;
1 parent 9ff789a commit 8dce20e

File tree

2 files changed

+193
-175
lines changed

2 files changed

+193
-175
lines changed

src/Screens/IntroScreen.cpp

Lines changed: 185 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,9 @@
1212
namespace tt
1313
{
1414

15-
namespace
16-
{
17-
18-
void createMenu(TextList& menuItems,
19-
const std::vector<std::string>& textlist,
20-
sf::RenderTarget& window,
21-
sf::Font& font)
22-
{
23-
24-
for (const auto& text : textlist)
25-
{
26-
auto temp = std::make_shared<sf::Text>(text, font);
27-
menuItems.push_back(temp);
28-
}
29-
30-
auto ypos = 300.0f;
31-
32-
for (const auto& item: menuItems)
33-
{
34-
item->setCharacterSize(40);
35-
36-
//
37-
// Calculate xpos (right align)
38-
//
39-
auto winWidth = window.getSize().x;
40-
auto itemWidth = item->getLocalBounds().width;
41-
auto xpos = (winWidth - itemWidth) - 50.f;
42-
43-
item->setPosition(xpos, ypos);
44-
45-
item->setOutlineColor(sf::Color(0,0,0,255));
46-
item->setOutlineThickness(5);
47-
48-
//
49-
// Increment ypos to the next item position
50-
//
51-
ypos += item->getLocalBounds().height + 20.0f;
52-
}
53-
54-
}
55-
56-
void updateMenu(std::uint16_t selection, TextList& menuItems)
57-
{
58-
std::for_each(menuItems.begin(), menuItems.end(),
59-
[](TextPtr item) { item->setFillColor(sf::Color(64, 64, 64)); });
60-
61-
if (selection < menuItems.size())
62-
{
63-
menuItems.at(selection)->setFillColor(sf::Color::Yellow);
64-
}
65-
}
66-
67-
}
15+
constexpr auto BUTTON_HEIGHT = 35u;
16+
constexpr auto BUTTON_WIDTH = 125u;
17+
constexpr auto BUTTON_PADDING = 25u;
6818

6919
IntroScreen::IntroScreen( ResourceManager& resmgr,
7020
sf::RenderTarget& target )
@@ -171,26 +121,85 @@ IntroScreen::IntroScreen( ResourceManager& resmgr,
171121
//
172122
addDrawable(textobj);
173123

174-
//
175-
// Add the menu items
176-
//
177-
createMenu(_menuItems,
178-
{
179-
"New Game",
180-
"Load Game",
181-
"Settings",
182-
"Exit Game"
183-
}, _window, _font);
124+
tt::AudioLocator::sound()->cacheAudio(SELECTOR_SOUND);
125+
tt::AudioLocator::sound()->cacheAudio(TOMWILLKILL_SOUND);
184126

185-
updateMenu(_selected, _menuItems);
127+
initGui();
128+
}
186129

187-
for (const auto& item: _menuItems)
130+
void IntroScreen::initGui()
131+
{
132+
auto xloc = _window.getSize().x - (BUTTON_WIDTH + BUTTON_PADDING);
133+
auto yloc = (_window.getSize().y / 2) - ((BUTTON_HEIGHT + BUTTON_PADDING) * 2);
134+
135+
_gui = std::make_unique<tgui::Gui>(_window);
136+
137+
// New Game
138+
auto tempbtn = tgui::Button::create();
139+
tempbtn->setText("New Game");
140+
tempbtn->setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
141+
tempbtn->setPosition(xloc, yloc);
142+
tempbtn->getRenderer()->setBackgroundColor(tgui::Color::White);
143+
tempbtn->getRenderer()->setBackgroundColorHover(tgui::Color::Green);
144+
tempbtn->onMouseEnter([this]() { tt::AudioLocator::sound()->play(SELECTOR_SOUND); });
145+
tempbtn->onPress([=]
188146
{
189-
addDrawable(item);
190-
}
147+
_menuAction.action.type = ScreenActionType::CHANGE_SCREEN;
148+
_menuAction.action.data = SCREEN_LOADING;
149+
});
150+
_gui->add(tempbtn);
151+
152+
// Load Game
153+
tempbtn = tgui::Button::create();
154+
tempbtn->setText("Load Game");
155+
tempbtn->setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
156+
yloc += BUTTON_HEIGHT + BUTTON_PADDING;
157+
tempbtn->setPosition(xloc, yloc);
158+
tempbtn->getRenderer()->setBackgroundColor(tgui::Color::White);
159+
tempbtn->getRenderer()->setBackgroundColorHover(tgui::Color::Green);
160+
tempbtn->onMouseEnter([this]() { tt::AudioLocator::sound()->play(SELECTOR_SOUND); });
161+
_gui->add(tempbtn);
162+
163+
// Options
164+
tempbtn = tgui::Button::create();
165+
tempbtn->setText("Options");
166+
tempbtn->setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
167+
yloc += BUTTON_HEIGHT + BUTTON_PADDING;
168+
tempbtn->setPosition(xloc, yloc);
169+
tempbtn->getRenderer()->setBackgroundColor(tgui::Color::White);
170+
tempbtn->getRenderer()->setBackgroundColorHover(tgui::Color::Green);
171+
tempbtn->onMouseEnter([this]() { tt::AudioLocator::sound()->play(SELECTOR_SOUND); });
172+
tempbtn->onPress([=]
173+
{
174+
_menuAction.action.type = ScreenActionType::CHANGE_SCREEN;
175+
_menuAction.action.data = SCREEN_SETTINGS;
176+
});
177+
_gui->add(tempbtn);
178+
179+
// Exit
180+
tempbtn = tgui::Button::create();
181+
tempbtn->setText("Exit Game");
182+
tempbtn->setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
183+
yloc += BUTTON_HEIGHT + BUTTON_PADDING;
184+
tempbtn->setPosition(xloc, yloc);
185+
tempbtn->getRenderer()->setBackgroundColor(tgui::Color::White);
186+
tempbtn->getRenderer()->setBackgroundColorHover(tgui::Color::Green);
187+
tempbtn->onMouseEnter([this]() { tt::AudioLocator::sound()->play(SELECTOR_SOUND); });
188+
tempbtn->onPress([=]
189+
{
190+
tt::AudioLocator::music()->stop(BACKGROUND_SONG);
191+
tt::AudioLocator::sound()->play(TOMWILLKILL_SOUND);
191192

192-
tt::AudioLocator::sound()->cacheAudio(SELECTOR_SOUND);
193-
tt::AudioLocator::sound()->cacheAudio(TOMWILLKILL_SOUND);
193+
while (tt::AudioLocator::sound()->getStatus(TOMWILLKILL_SOUND) == sf::Sound::Playing)
194+
{
195+
std::this_thread::yield();
196+
}
197+
198+
// super hack! I'm too lazy and don't care enough right now
199+
sf::RenderWindow* window = dynamic_cast<sf::RenderWindow*>(&_window);
200+
window->close();
201+
});
202+
_gui->add(tempbtn);
194203
}
195204

196205
void IntroScreen::draw()
@@ -211,114 +220,117 @@ PollResult IntroScreen::poll(const sf::Event& e)
211220
_gui.reset();
212221
}
213222

214-
return {};
215-
}
216-
217-
if (e.type == sf::Event::KeyReleased
218-
&& e.key.code == sf::Keyboard::Up)
219-
{
220-
if (_selected > 0)
223+
if (_menuAction.action.type != tt::ScreenActionType::NONE)
221224
{
222-
_selected--;
223-
updateMenu(_selected, _menuItems);
224-
tt::AudioLocator::sound()->play(SELECTOR_SOUND);
225+
return { true, _menuAction.action };
225226
}
226227
}
227-
else if (e.type == sf::Event::KeyReleased
228-
&& e.key.code == sf::Keyboard::Down)
229-
{
230-
if (_selected < _menuItems.size() - 1)
231-
{
232-
_selected++;
233-
updateMenu(_selected, _menuItems);
234-
235-
tt::AudioLocator::sound()->play(SELECTOR_SOUND);
236-
}
237-
}
238-
else if (e.type == sf::Event::KeyPressed
239-
&& (e.key.code == sf::Keyboard::Space
240-
|| e.key.code == sf::Keyboard::Enter))
241-
{
242-
switch (_selected)
243-
{
244-
default:
245-
break;
246228

247-
case 0: // new game
248-
{
249-
_gui = std::make_unique<tgui::Gui>(_window);
250-
251-
auto windowSize = _window.getSize();
252-
auto halfWindowWidth = windowSize.x / 2;
253-
254-
auto child = tgui::ChildWindow::create();
255-
child->setClientSize({500, 200});
256-
auto xpos = (_window.getSize().x / 2) - (child->getSize().x / 2);
257-
auto ypos = (_window.getSize().y / 2) - (child->getSize().y / 2);
258-
child->setPosition(xpos, ypos);
259-
child->setTitleButtons(tgui::ChildWindow::TitleButton::None);
260-
_gui->add(child);
261-
262-
auto editLbl = tgui::Label::create("Enter name for new game");
263-
editLbl->setPosition(20,20);
264-
editLbl->setTextSize(30);
265-
child->add(editLbl);
266-
267-
auto editBox = tgui::EditBox::create();
268-
editBox->setPosition(20,75);
269-
editBox->setTextSize(30);
270-
editBox->setSize(460,35);
271-
child->add(editBox);
272-
273-
auto okBtn = tgui::Button::create("Ok");
274-
okBtn->setWidgetName("sds");
275-
okBtn->setSize(50, 45);
276-
xpos = (child->getSize().x / 2) - (okBtn->getSize().x + 20);
277-
okBtn->setPosition(xpos, 125);
278-
child->add(okBtn);
279-
280-
auto cancelBtn = tgui::Button::create("Cancel");
281-
cancelBtn->setSize(65, 45);
282-
xpos = (child->getSize().x / 2) + 20;
283-
cancelBtn->setPosition(xpos, 125);
284-
cancelBtn->onPress([=]
285-
{
286-
_gui.reset();
287-
});
288-
child->add(cancelBtn);
289-
290-
editBox->setFocused(true);
291-
292-
break;
293-
}
294-
295-
case 1: // load game
296-
{
297-
return {true, { ScreenActionType::CHANGE_SCREEN, SCREEN_LOADING }};
298-
}
299-
300-
case 2: // settings
301-
{
302-
return {true, { ScreenActionType::CHANGE_SCREEN, SCREEN_SETTINGS }};
303-
}
304-
305-
case 3: // exit
306-
{
307-
tt::AudioLocator::music()->stop(BACKGROUND_SONG);
308-
tt::AudioLocator::sound()->play(TOMWILLKILL_SOUND);
309-
310-
while (tt::AudioLocator::sound()->getStatus(TOMWILLKILL_SOUND) == sf::Sound::Playing)
311-
{
312-
std::this_thread::yield();
313-
}
314-
315-
// super hack! I'm too lazy and don't care enough right now
316-
sf::RenderWindow* window = dynamic_cast<sf::RenderWindow*>(&_window);
317-
window->close();
318-
}
319-
break;
320-
}
321-
}
229+
// if (e.type == sf::Event::KeyReleased
230+
// && e.key.code == sf::Keyboard::Up)
231+
// {
232+
// if (_selected > 0)
233+
// {
234+
// _selected--;
235+
// updateMenu(_selected, _menuItems);
236+
// tt::AudioLocator::sound()->play(SELECTOR_SOUND);
237+
// }
238+
// }
239+
// else if (e.type == sf::Event::KeyReleased
240+
// && e.key.code == sf::Keyboard::Down)
241+
// {
242+
// if (_selected < _menuItems.size() - 1)
243+
// {
244+
// _selected++;
245+
// updateMenu(_selected, _menuItems);
246+
//
247+
// tt::AudioLocator::sound()->play(SELECTOR_SOUND);
248+
// }
249+
// }
250+
// else if (e.type == sf::Event::KeyPressed
251+
// && (e.key.code == sf::Keyboard::Space
252+
// || e.key.code == sf::Keyboard::Enter))
253+
// {
254+
// switch (_selected)
255+
// {
256+
// default:
257+
// break;
258+
//
259+
// case 0: // new game
260+
// {
261+
// _gui = std::make_unique<tgui::Gui>(_window);
262+
//
263+
// auto windowSize = _window.getSize();
264+
// auto halfWindowWidth = windowSize.x / 2;
265+
//
266+
// auto child = tgui::ChildWindow::create();
267+
// child->setClientSize({500, 200});
268+
// auto xpos = (_window.getSize().x / 2) - (child->getSize().x / 2);
269+
// auto ypos = (_window.getSize().y / 2) - (child->getSize().y / 2);
270+
// child->setPosition(xpos, ypos);
271+
// child->setTitleButtons(tgui::ChildWindow::TitleButton::None);
272+
// _gui->add(child);
273+
//
274+
// auto editLbl = tgui::Label::create("Enter name for new game");
275+
// editLbl->setPosition(20,20);
276+
// editLbl->setTextSize(30);
277+
// child->add(editLbl);
278+
//
279+
// auto editBox = tgui::EditBox::create();
280+
// editBox->setPosition(20,75);
281+
// editBox->setTextSize(30);
282+
// editBox->setSize(460,35);
283+
// child->add(editBox);
284+
//
285+
// auto okBtn = tgui::Button::create("Ok");
286+
// okBtn->setWidgetName("sds");
287+
// okBtn->setSize(50, 45);
288+
// xpos = (child->getSize().x / 2) - (okBtn->getSize().x + 20);
289+
// okBtn->setPosition(xpos, 125);
290+
// child->add(okBtn);
291+
//
292+
// auto cancelBtn = tgui::Button::create("Cancel");
293+
// cancelBtn->setSize(65, 45);
294+
// xpos = (child->getSize().x / 2) + 20;
295+
// cancelBtn->setPosition(xpos, 125);
296+
// cancelBtn->onPress([=]
297+
// {
298+
// _gui.reset();
299+
// });
300+
// child->add(cancelBtn);
301+
//
302+
// editBox->setFocused(true);
303+
//
304+
// break;
305+
// }
306+
//
307+
// case 1: // load game
308+
// {
309+
// return {true, { ScreenActionType::CHANGE_SCREEN, SCREEN_LOADING }};
310+
// }
311+
//
312+
// case 2: // settings
313+
// {
314+
// return {true, { ScreenActionType::CHANGE_SCREEN, SCREEN_SETTINGS }};
315+
// }
316+
//
317+
// case 3: // exit
318+
// {
319+
// tt::AudioLocator::music()->stop(BACKGROUND_SONG);
320+
// tt::AudioLocator::sound()->play(TOMWILLKILL_SOUND);
321+
//
322+
// while (tt::AudioLocator::sound()->getStatus(TOMWILLKILL_SOUND) == sf::Sound::Playing)
323+
// {
324+
// std::this_thread::yield();
325+
// }
326+
//
327+
// // super hack! I'm too lazy and don't care enough right now
328+
// sf::RenderWindow* window = dynamic_cast<sf::RenderWindow*>(&_window);
329+
// window->close();
330+
// }
331+
// break;
332+
// }
333+
// }
322334

323335
return {};
324336
}

0 commit comments

Comments
 (0)