1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 03:12:36 +01:00
openrw/rwgame/State.hpp

145 lines
2.6 KiB
C++
Raw Normal View History

2013-12-27 00:18:55 +01:00
#ifndef _GAME_STATE_HPP_
#define _GAME_STATE_HPP_
#include <functional>
#include <queue>
#include <SFML/Graphics/RenderWindow.hpp>
#include "MenuSystem.hpp"
struct State
{
// Helper for global menu behaviour
Menu* currentMenu;
2014-01-01 04:13:23 +01:00
Menu* nextMenu;
2013-12-27 00:18:55 +01:00
State()
2014-01-01 04:13:23 +01:00
: currentMenu(nullptr), nextMenu(nullptr) {}
2013-12-27 00:18:55 +01:00
virtual void enter() = 0;
virtual void exit() = 0;
virtual void tick(float dt) = 0;
virtual void draw(sf::RenderWindow& w)
{
2014-01-01 04:13:23 +01:00
if(getCurrentMenu()) {
getCurrentMenu()->draw(w);
}
}
virtual ~State() {
if(getCurrentMenu()) {
delete getCurrentMenu();
2013-12-27 00:18:55 +01:00
}
}
void enterMenu(Menu* menu)
{
2014-01-01 04:13:23 +01:00
nextMenu = menu;
}
Menu* getCurrentMenu()
{
if(nextMenu) {
if(currentMenu) {
delete currentMenu;
}
currentMenu = nextMenu;
nextMenu = nullptr;
}
return currentMenu;
2013-12-27 00:18:55 +01:00
}
virtual void handleEvent(const sf::Event& e)
{
2014-01-01 04:13:23 +01:00
auto m = getCurrentMenu();
if(! m) return;
2013-12-27 00:18:55 +01:00
switch(e.type) {
case sf::Event::MouseButtonReleased:
2014-01-01 04:13:23 +01:00
m->click(e.mouseButton.x, e.mouseButton.y);
2013-12-27 00:18:55 +01:00
break;
2014-01-01 01:37:16 +01:00
case sf::Event::MouseMoved:
2014-01-01 04:13:23 +01:00
m->hover(e.mouseMove.x, e.mouseMove.y);
2014-01-01 01:37:16 +01:00
break;
case sf::Event::KeyPressed:
2014-01-01 04:13:23 +01:00
switch(e.key.code) {
2014-05-25 23:30:50 +02:00
default: break;
2014-01-01 04:13:23 +01:00
case sf::Keyboard::Up:
m->move(-1);
break;
case sf::Keyboard::Down:
m->move(1);
break;
case sf::Keyboard::Return:
m->activate();
break;
2014-01-01 01:37:16 +01:00
}
2013-12-27 00:18:55 +01:00
default: break;
};
}
};
struct GenericState : public State
{
typedef std::function<void (State*)> StateChange;
typedef std::function<void (State*, float)> Tick;
typedef std::function<void (State*, const sf::Event&)> Event;
StateChange enter_lambda;
Tick tick_lambda;
StateChange exit_lambda;
Event event_lambda;
GenericState(StateChange start, Tick think, StateChange end, Event event)
: enter_lambda(start), tick_lambda(think),
exit_lambda(end), event_lambda(event) {}
virtual void enter() { enter_lambda(this); }
virtual void exit() { exit_lambda(this); }
virtual void tick(float dt) { tick_lambda(this, dt); }
virtual void handleEvent(const sf::Event& event) {
event_lambda(this, event);
State::handleEvent(event);
}
};
struct StateManager
{
static StateManager& get()
{
static StateManager m;
return m;
}
std::deque<State*> states;
void enter(State* state)
{
states.push_back(state);
state->enter();
}
void tick(float dt)
{
states.back()->tick(dt);
}
void draw(sf::RenderWindow& w)
{
states.back()->draw(w);
}
void exit()
{
// TODO: Resole states being leaked.
states.back()->exit();
states.pop_back();
2013-12-27 22:58:27 +01:00
if(states.size() > 0) {
2013-12-27 00:18:55 +01:00
states.back()->enter();
}
}
};
2014-05-25 23:30:50 +02:00
#endif