Initial first steps in pulling the SFML event processing out of the gui::fsm so that I can handle more complex things like drag and drop.
This commit is contained in:
parent
0674908e49
commit
5aa54d875f
9 changed files with 194 additions and 146 deletions
116
gui/event_router.cpp
Normal file
116
gui/event_router.cpp
Normal file
|
@ -0,0 +1,116 @@
|
|||
#define FSM_DEBUG 1
|
||||
#include "event_router.hpp"
|
||||
#include "dbc.hpp"
|
||||
#include "events.hpp"
|
||||
|
||||
namespace gui {
|
||||
namespace routing {
|
||||
using enum Event;
|
||||
using enum State;
|
||||
|
||||
gui::Event Router::process_event(std::optional<sf::Event> ev) {
|
||||
$next_event = gui::Event::TICK;
|
||||
|
||||
if(ev->is<sf::Event::Closed>()) {
|
||||
return gui::Event::QUIT;
|
||||
}
|
||||
|
||||
if(const auto* mouse = ev->getIf<sf::Event::MouseButtonPressed>()) {
|
||||
if(mouse->button == sf::Mouse::Button::Left) {
|
||||
position = mouse->position;
|
||||
event(MOUSE_DOWN);
|
||||
}
|
||||
} else if(const auto* mouse = ev->getIf<sf::Event::MouseButtonReleased>()) {
|
||||
if(mouse->button == sf::Mouse::Button::Left) {
|
||||
position = mouse->position;
|
||||
event(MOUSE_UP);
|
||||
}
|
||||
} else if(const auto* mouse = ev->getIf<sf::Event::MouseMoved>()) {
|
||||
position = mouse->position;
|
||||
event(MOUSE_MOVE);
|
||||
}
|
||||
|
||||
if(const auto* key = ev->getIf<sf::Event::KeyPressed>()) {
|
||||
scancode = key->scancode;
|
||||
event(KEY_PRESS);
|
||||
}
|
||||
|
||||
return $next_event;
|
||||
}
|
||||
|
||||
void Router::event(Event ev) {
|
||||
switch($state) {
|
||||
FSM_STATE(State, START, ev);
|
||||
FSM_STATE(State, IDLE, ev);
|
||||
FSM_STATE(State, MOUSE_ACTIVE, ev);
|
||||
FSM_STATE(State, MOUSE_MOVING, ev);
|
||||
}
|
||||
}
|
||||
|
||||
void Router::START(Event ) {
|
||||
state(State::IDLE);
|
||||
}
|
||||
|
||||
void Router::IDLE(Event ev) {
|
||||
switch(ev) {
|
||||
case MOUSE_DOWN:
|
||||
set_event(gui::Event::TICK);
|
||||
state(State::MOUSE_ACTIVE);
|
||||
break;
|
||||
case MOUSE_UP:
|
||||
dbc::log("mouse up in IDLE");
|
||||
break;
|
||||
case MOUSE_MOVE:
|
||||
dbc::log("mouse move, send moved event");
|
||||
break;
|
||||
case KEY_PRESS:
|
||||
dbc::log("key pressed");
|
||||
break;
|
||||
default:
|
||||
dbc::sentinel("invalid event");
|
||||
}
|
||||
}
|
||||
|
||||
void Router::MOUSE_ACTIVE(Event ev) {
|
||||
switch(ev) {
|
||||
case MOUSE_DOWN:
|
||||
dbc::log("mouse down in MOUSE_ACTIVE");
|
||||
break;
|
||||
case MOUSE_UP:
|
||||
dbc::log("mouse up, send click event");
|
||||
state(State::IDLE);
|
||||
break;
|
||||
case MOUSE_MOVE:
|
||||
state(State::MOUSE_MOVING);
|
||||
break;
|
||||
case KEY_PRESS:
|
||||
dbc::log("send the key but cancel");
|
||||
state(State::IDLE);
|
||||
break;
|
||||
default:
|
||||
dbc::sentinel("invalid event");
|
||||
}
|
||||
}
|
||||
|
||||
void Router::MOUSE_MOVING(Event ev) {
|
||||
switch(ev) {
|
||||
case MOUSE_DOWN:
|
||||
dbc::log("mouse down in MOUSE_MOVING state");
|
||||
break;
|
||||
case MOUSE_UP:
|
||||
dbc::log("mouse up, send drop event");
|
||||
state(State::IDLE);
|
||||
break;
|
||||
case MOUSE_MOVE:
|
||||
dbc::log("mouse move, send drag event");
|
||||
break;
|
||||
case KEY_PRESS:
|
||||
dbc::log("send the key but cancel");
|
||||
state(State::IDLE);
|
||||
break;
|
||||
default:
|
||||
dbc::sentinel("invalid event");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue