-
Notifications
You must be signed in to change notification settings - Fork 0
/
State_MainMenu.cpp
102 lines (83 loc) · 2.96 KB
/
State_MainMenu.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
#include "State_MainMenu.hpp"
#include "StateManager.hpp"
State_MainMenu::State_MainMenu(StateManager* l_stateManager)
: BaseState(l_stateManager){}
State_MainMenu::~State_MainMenu(){}
void State_MainMenu::OnCreate(){
m_font.loadFromFile("arial.ttf");
m_text.setFont(m_font);
m_text.setString(sf::String("MAIN MENU:"));
m_text.setCharacterSize(18);
sf::FloatRect textRect = m_text.getLocalBounds();
m_text.setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
m_text.setPosition(400,100);
m_buttonSize = sf::Vector2f(300.0f,32.0f);
m_buttonPos = sf::Vector2f(400,200);
m_buttonPadding = 4; // 4px.
std::string str[3];
str[0] = "PLAY";
str[1] = "CREDITS";
str[2] = "EXIT";
for(int i = 0; i < 3; ++i){
sf::Vector2f buttonPosition(m_buttonPos.x,m_buttonPos.y +
(i * (m_buttonSize.y + m_buttonPadding)));
m_rects[i].setSize(m_buttonSize);
m_rects[i].setFillColor(sf::Color::Red);
m_rects[i].setOrigin(m_buttonSize.x / 2.0f, m_buttonSize.y / 2.0f);
m_rects[i].setPosition(buttonPosition);
m_labels[i].setFont(m_font);
m_labels[i].setString(sf::String(str[i]));
m_labels[i].setCharacterSize(12);
sf::FloatRect rect = m_labels[i].getLocalBounds();
m_labels[i].setOrigin(rect.left + rect.width / 2.0f,
rect.top + rect.height / 2.0f);
m_labels[i].setPosition(buttonPosition);
}
EventManager* evMgr = m_stateMgr->GetContext()->m_eventManager;
evMgr->AddCallback(StateType::MainMenu,"Mouse_Left",&State_MainMenu::MouseClick,this);
}
void State_MainMenu::OnDestroy(){
EventManager* evMgr = m_stateMgr->GetContext()->m_eventManager;
evMgr->RemoveCallback(StateType::MainMenu,"Mouse_Left");
}
void State_MainMenu::Activate(){
if(m_stateMgr->HasState(StateType::Game)
&& m_labels[0].getString() == "PLAY")
{
m_labels[0].setString(sf::String("RESUME"));
sf::FloatRect rect = m_labels[0].getLocalBounds();
m_labels[0].setOrigin(rect.left + rect.width / 2.0f,
rect.top + rect.height / 2.0f);
}
}
void State_MainMenu::MouseClick(EventDetails* l_details){
sf::Vector2i mousePos = l_details->m_mouse;
float halfX = m_buttonSize.x / 2.0f;
float halfY = m_buttonSize.y / 2.0f;
for(int i = 0; i < 3; ++i){
if(mousePos.x >= m_rects[i].getPosition().x - halfX &&
mousePos.x <= m_rects[i].getPosition().x + halfX &&
mousePos.y >= m_rects[i].getPosition().y - halfY &&
mousePos.y <= m_rects[i].getPosition().y + halfY)
{
if(i == 0){
m_stateMgr->SwitchTo(StateType::Game);
} else if(i == 1){
// Credits state.
} else if(i == 2){
m_stateMgr->GetContext()->m_wind->Close();
}
}
}
}
void State_MainMenu::Draw(){
sf::RenderWindow* window = m_stateMgr->GetContext()->m_wind->GetRenderWindow();
window->draw(m_text);
for(int i = 0; i < 3; ++i){
window->draw(m_rects[i]);
window->draw(m_labels[i]);
}
}
void State_MainMenu::Update(const sf::Time& l_time){}
void State_MainMenu::Deactivate(){}