Repository navigation
Expand file tree
/
Copy pathMenuState.cpp
More file actions
executable file
·133 lines (113 loc) · 4.79 KB
/
Copy pathMenuState.cpp
File metadata and controls
executable file
·133 lines (113 loc) · 4.79 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
//
// Created by CommanderKeen on 15.12.24.
//
module;
#include <print>
#include <functional>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
module SharedState;
import Logger;
import Utils;
MenuState::MenuState(SharedContext* l_context)
:
BaseState(l_context)
, m_font(Utils::formatPath(Utils::get_project_path() + "\\" + "resources/fonts/arial_unicode.ttf"))
, m_selected_item(0){
Logger::getInstance().log("MenuState::MenuState");
//loading texture "main_menu_background"
m_shared_context->m_textureManager->loadResource("main_menu_background");
m_shared_context->m_eventManager->registerCallback(StateType::Menu,
"arrowKeyUp",
&MenuState::keyArrowUp,
this);
m_shared_context->m_eventManager->registerCallback(StateType::Menu,
"arrowKeyDown",
&MenuState::keyArrowDown,
this);
m_shared_context->m_eventManager->registerCallback(StateType::Menu,
"select",
&MenuState::select,
this);
m_button_container = std::make_unique<Gui_Container>();
setupGUI();
m_font.setSmooth(false);
}
MenuState::~MenuState() {
Logger::getInstance().log("MenuState::~MenuState");
m_shared_context->m_eventManager->removeCallback(StateType::Menu, "arrowKeyUp");
m_shared_context->m_eventManager->removeCallback(StateType::Menu, "arrowKeyDown");
m_shared_context->m_eventManager->removeCallback(StateType::Menu ,"select");
}
void MenuState::on_create() {
Logger::getInstance().log("MenuState::on_create()");
sf::Texture& background_texture = *m_shared_context->m_textureManager->get("main_menu_background");
m_backgroundSprite = std::make_unique<sf::Sprite>(background_texture);
sf::Vector2f center = {float(background_texture.getSize().x) / 2.f, float(background_texture.getSize().y) / 2.f};
sf::Vector2f size = {float(background_texture.getSize().x), float(background_texture.getSize().y)};
m_view = sf::View{center,size};
}
void MenuState::on_activate() {
Logger::getInstance().log("MenuState::on_activate()");
}
void MenuState::on_deactivate(){
Logger::getInstance().log("MenuState::on_deactivate()");
}
//TODO: on_destroy isn't invoked anywhere yet
void MenuState::on_destroy() {
Logger::getInstance().log("MenuState::on_destroy()");
}
void MenuState::update(float l_dt) {
m_button_container->update(l_dt);
}
void MenuState::draw(){
m_shared_context->m_window->getRenderWindow()->draw(*m_backgroundSprite);
m_button_container->render(m_shared_context->m_window->getRenderWindow());
}
void MenuState::setupGUI() {
std::unique_ptr<Button> new_game_button = std::make_unique<Button>(&m_font);
new_game_button->setText("New Game");
new_game_button->setTextSize(10);
new_game_button->setCallback([this]() {
startGame();
});
std::unique_ptr<Button> options_button = std::make_unique<Button>(&m_font);
options_button->setText("Options");
options_button->setTextSize(10);
options_button->setPosition({0.f, 5.f});
std::unique_ptr<Button> quit_button = std::make_unique<Button>(&m_font);
quit_button->setText("Quit");
quit_button->setTextSize(10);
quit_button->setPosition({0.f, 10.f});
quit_button->setCallback([this]() {
quitGame();
});
m_button_container->add_child(std::move(new_game_button));
m_button_container->add_child(std::move(options_button));
m_button_container->add_child(std::move(quit_button));
// select first option ("new game")
m_button_container->selectElementAt(0);
m_button_container->setPosition({100.f,100.f});
}
void MenuState::quitGame() {
Logger::getInstance().log("MenuState::quitGame");
m_shared_context->m_stateManager->switch_state(StateType::Quit);
}
void MenuState::startGame() {
Logger::getInstance().log("MenuState::startGame");
m_shared_context->m_stateManager->switch_state(StateType::Game);
}
void MenuState::keyArrowUp(EventDetails* l_details){
Logger::getInstance().log("MenuState::keyArrowUp");
m_button_container->select_previous_selectable();
}
void MenuState::keyArrowDown(EventDetails* l_details){
Logger::getInstance().log("MenuState::keyArrowDown");
m_button_container->select_next_selectable();
}
void MenuState::select(EventDetails* l_details){
Logger::getInstance().log("MenuState::select");
m_button_container->on_click();
// m_gui_buttons[m_selected_item]->on_click();
}