1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 18:32:44 +01:00

Improved menu management

This commit is contained in:
Daniel Evans 2014-01-01 03:13:23 +00:00
parent 4c595d0439
commit 3f0030d3e2
2 changed files with 39 additions and 25 deletions

View File

@ -9,9 +9,10 @@ struct State
{ {
// Helper for global menu behaviour // Helper for global menu behaviour
Menu* currentMenu; Menu* currentMenu;
Menu* nextMenu;
State() State()
: currentMenu(nullptr) {} : currentMenu(nullptr), nextMenu(nullptr) {}
virtual void enter() = 0; virtual void enter() = 0;
virtual void exit() = 0; virtual void exit() = 0;
@ -20,42 +21,56 @@ struct State
virtual void draw(sf::RenderWindow& w) virtual void draw(sf::RenderWindow& w)
{ {
if(currentMenu) { if(getCurrentMenu()) {
currentMenu->draw(w); getCurrentMenu()->draw(w);
}
}
virtual ~State() {
if(getCurrentMenu()) {
delete getCurrentMenu();
} }
} }
void enterMenu(Menu* menu) void enterMenu(Menu* menu)
{ {
currentMenu = menu; nextMenu = menu;
}
Menu* getCurrentMenu()
{
if(nextMenu) {
if(currentMenu) {
delete currentMenu;
}
currentMenu = nextMenu;
nextMenu = nullptr;
}
return currentMenu;
} }
virtual void handleEvent(const sf::Event& e) virtual void handleEvent(const sf::Event& e)
{ {
auto m = getCurrentMenu();
if(! m) return;
switch(e.type) { switch(e.type) {
case sf::Event::MouseButtonReleased: case sf::Event::MouseButtonReleased:
if(currentMenu) { m->click(e.mouseButton.x, e.mouseButton.y);
currentMenu->click(e.mouseButton.x, e.mouseButton.y);
}
break; break;
case sf::Event::MouseMoved: case sf::Event::MouseMoved:
if(currentMenu) { m->hover(e.mouseMove.x, e.mouseMove.y);
currentMenu->hover(e.mouseMove.x, e.mouseMove.y);
}
break; break;
case sf::Event::KeyPressed: case sf::Event::KeyPressed:
if(currentMenu) { switch(e.key.code) {
switch(e.key.code) { case sf::Keyboard::Up:
case sf::Keyboard::Up: m->move(-1);
currentMenu->move(-1); break;
break; case sf::Keyboard::Down:
case sf::Keyboard::Down: m->move(1);
currentMenu->move(1); break;
break; case sf::Keyboard::Return:
case sf::Keyboard::Return: m->activate();
currentMenu->activate(); break;
break;
}
} }
default: break; default: break;
}; };

View File

@ -653,7 +653,7 @@ GenericState pauseState(
m->addEntry(Menu::lambda("Continue", [] { StateManager::get().exit(); })); m->addEntry(Menu::lambda("Continue", [] { StateManager::get().exit(); }));
m->addEntry(Menu::lambda("Options", [] { std::cout << "Options" << std::endl; })); m->addEntry(Menu::lambda("Options", [] { std::cout << "Options" << std::endl; }));
m->addEntry(Menu::lambda("Exit", [] { window.close(); })); m->addEntry(Menu::lambda("Exit", [] { window.close(); }));
self->currentMenu = m; self->enterMenu(m);
lockCursor(false); lockCursor(false);
}, },
[](State* self, float dt) [](State* self, float dt)
@ -723,7 +723,7 @@ GenericState menuState(
m->addEntry(Menu::lambda("Test", [] { StateManager::get().enter(&gameState); })); m->addEntry(Menu::lambda("Test", [] { StateManager::get().enter(&gameState); }));
m->addEntry(Menu::lambda("Options", [] { std::cout << "Options" << std::endl; })); m->addEntry(Menu::lambda("Options", [] { std::cout << "Options" << std::endl; }));
m->addEntry(Menu::lambda("Exit", [] { window.close(); })); m->addEntry(Menu::lambda("Exit", [] { window.close(); }));
self->currentMenu = m; self->enterMenu(m);
lockCursor(false); lockCursor(false);
}, },
[](State* self, float dt) [](State* self, float dt)
@ -732,7 +732,6 @@ GenericState menuState(
}, },
[](State* self) [](State* self)
{ {
delete self->currentMenu;
}, },
[](State* self, const sf::Event& e) [](State* self, const sf::Event& e)
{ {