Refactor mouse and keyboard events
This commit is contained in:
parent
4a9498a520
commit
d924e7dbca
@ -19,45 +19,11 @@ UserInput::~UserInput() { LOG_DEBUG("."); };
|
||||
|
||||
std::expected<void, std::string> UserInput::Init() { return {}; }
|
||||
|
||||
const std::vector<UserAction> &UserInput::GetActions() {
|
||||
m_Actions.clear();
|
||||
SDL_Event event;
|
||||
|
||||
static WindowPos mouse_pan_start;
|
||||
void UserInput::GetActions_mouse(const SDL_Event& event)
|
||||
{
|
||||
static bool mouse_pan = false;
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
// TODO refactor mouse / kbd handling into separate functions
|
||||
if (event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP) {
|
||||
bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false;
|
||||
SDL_KeyboardEvent kbd_event = event.key;
|
||||
if (kbd_event.repeat) {
|
||||
// SDL repeats KEY_DOWN if key is held down, we ignore that
|
||||
continue;
|
||||
}
|
||||
LOG_DEBUG("Key '", static_cast<char>(kbd_event.key),
|
||||
key_down ? "' down" : "' up");
|
||||
|
||||
switch (kbd_event.key) {
|
||||
case 'q':
|
||||
m_Actions.emplace_back(UserAction::Type::EXIT);
|
||||
// further processing of inputs is not needed
|
||||
return m_Actions;
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
if (key_down) {
|
||||
int selection = kbd_event.key - '0';
|
||||
m_Actions.emplace_back(UserAction::Type::SELECT_PATHFINDER, selection);
|
||||
LOG_INFO("Pathfinder selected: ", selection);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
LOG_INFO("Key '", static_cast<char>(kbd_event.key), "' not mapped");
|
||||
break;
|
||||
}
|
||||
} else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
SDL_MouseButtonEvent mouse_event = event.button;
|
||||
MouseButton button = static_cast<MouseButton>(mouse_event.button);
|
||||
if (button == MouseButton::LEFT) {
|
||||
@ -80,7 +46,58 @@ const std::vector<UserAction> &UserInput::GetActions() {
|
||||
WindowPos{motion_event.xrel, motion_event.yrel});
|
||||
|
||||
}
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
void UserInput::GetActions_kbd(const SDL_Event& event)
|
||||
{
|
||||
bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false;
|
||||
SDL_KeyboardEvent kbd_event = event.key;
|
||||
if (kbd_event.repeat) {
|
||||
// SDL repeats KEY_DOWN if key is held down, we ignore that
|
||||
return;
|
||||
}
|
||||
LOG_DEBUG("Key '", static_cast<char>(kbd_event.key),
|
||||
key_down ? "' down" : "' up");
|
||||
|
||||
switch (kbd_event.key) {
|
||||
case 'q':
|
||||
m_Actions.emplace_back(UserAction::Type::EXIT);
|
||||
return;
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
if (key_down) {
|
||||
int selection = kbd_event.key - '0';
|
||||
m_Actions.emplace_back(UserAction::Type::SELECT_PATHFINDER, selection);
|
||||
LOG_INFO("Pathfinder selected: ", selection);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
LOG_INFO("Key '", static_cast<char>(kbd_event.key), "' not mapped");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<UserAction>& UserInput::GetActions() {
|
||||
m_Actions.clear();
|
||||
SDL_Event event;
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_EVENT_KEY_DOWN
|
||||
|| event.type == SDL_EVENT_KEY_UP)
|
||||
{
|
||||
GetActions_kbd(event);
|
||||
}
|
||||
else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN
|
||||
|| event.type == SDL_EVENT_MOUSE_BUTTON_UP
|
||||
|| event.type == SDL_EVENT_MOUSE_MOTION)
|
||||
{
|
||||
GetActions_mouse(event);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO uncomment, for now too much noise
|
||||
// LOG_WARNING("Action not processed");
|
||||
}
|
||||
|
@ -49,4 +49,7 @@ public:
|
||||
|
||||
private:
|
||||
std::vector<UserAction> m_Actions;
|
||||
|
||||
void GetActions_kbd(const SDL_Event&);
|
||||
void GetActions_mouse(const SDL_Event&);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user