-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTitleMenu.cpp
62 lines (52 loc) · 1.78 KB
/
TitleMenu.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
#include "stdafx.h"
#include "TitleMenu.h"
#include "MyAlgorithms.h"
#include "ShowingDoors.h"
#include <utility>
GameState* TitleMenu::instance_ = 0;
GameState* TitleMenu::Instance(Game* game)
{
if(instance_ == 0)
instance_ = new TitleMenu(game);
return instance_;
}
TitleMenu::TitleMenu(Game* game) : BUTTON_SPACING(50)
{
titleMenuFont.loadFromFile("fonts/segoesc.ttf");
//create buttons and put them into the list
buttons.emplace_back(sf::Text("Play", titleMenuFont, 30), [this, game]{this->ChangeState(game,ShowingDoors::Instance(game));});
buttons.emplace_back(sf::Text("Exit", titleMenuFont, 30), [game]{game->mainWindow_.close();});
for (unsigned int i=0; i<buttons.size(); ++i)
{
MyAlgorithms::CenterOrigin(buttons[i].first);
buttons[i].first.setPosition(game->mainWindow_.getSize().x/2, game->mainWindow_.getSize().y/2 + i*(BUTTON_SPACING + buttons[i].first.getGlobalBounds().height));
}
}
void TitleMenu::HandleEvents(Game* game)
{
sf::Event someEvent;
if(game->mainWindow_.waitEvent(someEvent) && someEvent.type==sf::Event::MouseButtonPressed)
{
for (auto button : buttons)
if (MyAlgorithms::IsWithin(sf::Mouse::getPosition(game->mainWindow_), button.first))//if the click was on the button
button.second();//execute the button's function
}
if (someEvent.type==sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
game->mainWindow_.close();
}
void TitleMenu::Update(Game* game)
{
for (auto& button : buttons)
{
button.first.setColor(sf::Color::White);
if (MyAlgorithms::IsWithin(sf::Mouse::getPosition(game->mainWindow_), button.first))
button.first.setColor(sf::Color::Red);
}
}
void TitleMenu::Draw(Game* game)
{
game->mainWindow_.clear();
for (auto button : buttons)
game->mainWindow_.draw(button.first);
game->mainWindow_.display();
}