Skip to content

Commit

Permalink
Merge pull request #1640 from heinezen/fix/input-contexts-order
Browse files Browse the repository at this point in the history
Fix order of processing input contexts
  • Loading branch information
TheJJ authored May 2, 2024
2 parents f282086 + 51c8c4f commit b7ec91b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
6 changes: 4 additions & 2 deletions libopenage/input/input_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
// Copyright 2015-2024 the openage authors. See copying.md for legal info.

#include "input_manager.h"

Expand All @@ -9,6 +9,7 @@
#include "input/input_context.h"
#include "renderer/gui/guisys/public/gui_input.h"


namespace openage::input {

InputManager::InputManager() :
Expand Down Expand Up @@ -131,7 +132,8 @@ bool InputManager::process(const QEvent &ev) {
input::Event input_ev{ev};

// Check context list on top of the stack (most recent bound first)
for (auto const &ctx : this->active_contexts) {
for (size_t i = this->active_contexts.size(); i > 0; --i) {
auto &ctx = this->active_contexts.at(i - 1);
if (ctx->is_bound(input_ev)) {
auto &actions = ctx->lookup(input_ev);
for (auto const &action : actions) {
Expand Down
44 changes: 34 additions & 10 deletions libopenage/input/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,40 @@ void action_demo() {
input_action catch_all{input_action_t::CUSTOM, nop};

// events that map to specific keys/buttons
Event ev_up{event_class::KEYBOARD, Qt::Key_Up, Qt::NoModifier, QEvent::KeyRelease};
Event ev_down{event_class::KEYBOARD, Qt::Key_Down, Qt::NoModifier, QEvent::KeyRelease};

Event ev_w{event_class::KEYBOARD, Qt::Key_W, Qt::NoModifier, QEvent::KeyRelease};
Event ev_a{event_class::KEYBOARD, Qt::Key_A, Qt::NoModifier, QEvent::KeyRelease};
Event ev_s{event_class::KEYBOARD, Qt::Key_S, Qt::NoModifier, QEvent::KeyRelease};
Event ev_d{event_class::KEYBOARD, Qt::Key_D, Qt::NoModifier, QEvent::KeyRelease};

Event ev_lmb{event_class::MOUSE, Qt::LeftButton, Qt::NoModifier, QEvent::MouseButtonRelease};
Event ev_rmb{event_class::MOUSE, Qt::RightButton, Qt::NoModifier, QEvent::MouseButtonRelease};
Event ev_up{event_class::KEYBOARD,
Qt::Key::Key_Up,
Qt::KeyboardModifier::NoModifier,
QEvent::KeyRelease};
Event ev_down{event_class::KEYBOARD,
Qt::Key::Key_Down,
Qt::KeyboardModifier::NoModifier,
QEvent::KeyRelease};

Event ev_w{event_class::KEYBOARD,
Qt::Key::Key_W,
Qt::KeyboardModifier::NoModifier,
QEvent::KeyRelease};
Event ev_a{event_class::KEYBOARD,
Qt::Key::Key_A,
Qt::KeyboardModifier::NoModifier,
QEvent::KeyRelease};
Event ev_s{event_class::KEYBOARD,
Qt::Key::Key_S,
Qt::KeyboardModifier::NoModifier,
QEvent::KeyRelease};
Event ev_d{event_class::KEYBOARD,
Qt::Key::Key_D,
Qt::KeyboardModifier::NoModifier,
QEvent::KeyRelease};

Event ev_lmb{event_class::MOUSE_BUTTON,
Qt::MouseButton::LeftButton,
Qt::KeyboardModifier::NoModifier,
QEvent::MouseButtonRelease};
Event ev_rmb{event_class::MOUSE_BUTTON,
Qt::MouseButton::RightButton,
Qt::KeyboardModifier::NoModifier,
QEvent::MouseButtonRelease};

// bind events to actions in the contexts
mgr.get_global_context()->bind(ev_up, push_a);
Expand Down
3 changes: 3 additions & 0 deletions libopenage/presenter/presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ void Presenter::init_input() {
this->input_manager->process(ev);
});
this->window->add_mouse_button_callback([&](const QMouseEvent &ev) {
this->input_manager->process(ev);
});
this->window->add_mouse_move_callback([&](const QMouseEvent &ev) {
this->input_manager->set_mouse(ev.position().x(), ev.position().y());
this->input_manager->process(ev);
});
Expand Down
7 changes: 6 additions & 1 deletion libopenage/renderer/opengl/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ void GlWindow::update() {
} break;
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseMove:
case QEvent::MouseButtonDblClick: {
auto const ev = std::dynamic_pointer_cast<QMouseEvent>(event);
for (auto &cb : this->on_mouse_button) {
cb(*ev);
}
} break;
case QEvent::MouseMove: {
auto const ev = std::dynamic_pointer_cast<QMouseEvent>(event);
for (auto &cb : this->on_mouse_move) {
cb(*ev);
}
} break;
case QEvent::Wheel: {
auto const ev = std::dynamic_pointer_cast<QWheelEvent>(event);
for (auto &cb : this->on_mouse_wheel) {
Expand Down
4 changes: 4 additions & 0 deletions libopenage/renderer/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ void Window::add_mouse_button_callback(const mouse_button_cb_t &cb) {
this->on_mouse_button.push_back(cb);
}

void Window::add_mouse_move_callback(const mouse_move_cb_t &cb) {
this->on_mouse_move.push_back(cb);
}

void Window::add_mouse_wheel_callback(const mouse_wheel_cb_t &cb) {
this->on_mouse_wheel.push_back(cb);
}
Expand Down
13 changes: 13 additions & 0 deletions libopenage/renderer/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Window {

using key_cb_t = std::function<void(const QKeyEvent &)>;
using mouse_button_cb_t = std::function<void(const QMouseEvent &)>;
using mouse_move_cb_t = std::function<void(const QMouseEvent &)>;
using mouse_wheel_cb_t = std::function<void(const QWheelEvent &)>;
using resize_cb_t = std::function<void(size_t, size_t, double)>;

Expand All @@ -96,6 +97,13 @@ class Window {
*/
void add_mouse_button_callback(const mouse_button_cb_t &cb);

/**
* Register a function that executes when the mouse is moved.
*
* @param cb Callback function.
*/
void add_mouse_move_callback(const mouse_move_cb_t &cb);

/**
* Register a function that executes when a mouse wheel action is used.
*
Expand Down Expand Up @@ -173,6 +181,11 @@ class Window {
*/
std::vector<mouse_button_cb_t> on_mouse_button;

/**
* Callbacks for mouse move actions.
*/
std::vector<mouse_move_cb_t> on_mouse_move;

/**
* Callbacks for mouse wheel actions.
*/
Expand Down

0 comments on commit b7ec91b

Please sign in to comment.