From 725e726c1e84840f41ae08541315d71a1824e8db Mon Sep 17 00:00:00 2001 From: Squareys Date: Thu, 21 Apr 2016 09:44:50 +0200 Subject: [PATCH] Platform: Implement modifiers for GlfwApplication Signed-off-by: Squareys --- src/Magnum/Platform/GlfwApplication.cpp | 37 ++++++++++++++----- src/Magnum/Platform/GlfwApplication.h | 49 +++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/Magnum/Platform/GlfwApplication.cpp b/src/Magnum/Platform/GlfwApplication.cpp index b0c2e5ba2..48dcc7890 100644 --- a/src/Magnum/Platform/GlfwApplication.cpp +++ b/src/Magnum/Platform/GlfwApplication.cpp @@ -150,8 +150,8 @@ int GlfwApplication::exec() { return 0; } -void GlfwApplication::staticKeyEvent(GLFWwindow*, int key, int, int action, int) { - KeyEvent e(static_cast(key)); +void GlfwApplication::staticKeyEvent(GLFWwindow*, int key, int, int action, int mods) { + KeyEvent e(static_cast(key), {static_cast(mods)}); if(action == GLFW_PRESS) { _instance->keyPressEvent(e); @@ -160,13 +160,13 @@ void GlfwApplication::staticKeyEvent(GLFWwindow*, int key, int, int action, int) } /* we don't handle GLFW_REPEAT */ } -void GlfwApplication::staticMouseMoveEvent(GLFWwindow*, double x, double y) { - MouseMoveEvent e{Vector2i{Int(x), Int(y)}}; +void GlfwApplication::staticMouseMoveEvent(GLFWwindow* window, double x, double y) { + MouseMoveEvent e{Vector2i{Int(x), Int(y)}, KeyEvent::getCurrentGlfwModifiers(window)}; _instance->mouseMoveEvent(e); } -void GlfwApplication::staticMouseEvent(GLFWwindow*, int button, int action, int) { - MouseEvent e(static_cast(button)); +void GlfwApplication::staticMouseEvent(GLFWwindow*, int button, int action, int mods) { + MouseEvent e(static_cast(button), {static_cast(mods)}); if(action == GLFW_PRESS) { _instance->mousePressEvent(e); @@ -175,13 +175,12 @@ void GlfwApplication::staticMouseEvent(GLFWwindow*, int button, int action, int) } /* we don't handle GLFW_REPEAT */ } -void GlfwApplication::staticMouseScrollEvent(GLFWwindow*, double xoffset, double yoffset) { - MouseScrollEvent e(Vector2d{xoffset, yoffset}); +void GlfwApplication::staticMouseScrollEvent(GLFWwindow* window, double xoffset, double yoffset) { + MouseScrollEvent e(Vector2d{xoffset, yoffset}, KeyEvent::getCurrentGlfwModifiers(window)); _instance->mouseScrollEvent(e); if(yoffset != 0.0) { - MouseEvent e1((yoffset > 0.0) ? MouseEvent::Button::WheelUp : MouseEvent::Button::WheelDown); - _instance->mousePressEvent(e); + MouseEvent e1((yoffset > 0.0) ? MouseEvent::Button::WheelUp : MouseEvent::Button::WheelDown, KeyEvent::getCurrentGlfwModifiers(window)); _instance->mousePressEvent(e1); } } @@ -190,6 +189,24 @@ void GlfwApplication::staticErrorCallback(int, const char* description) { Error() << description; } +GlfwApplication::InputEvent::Modifiers GlfwApplication::KeyEvent::getCurrentGlfwModifiers(GLFWwindow* window) { + static_assert(GLFW_PRESS == true && GLFW_RELEASE == false, "GLFW press and release constants do not correspond to bool values."); + + Modifiers mods = (glfwGetKey(window, Int(Key::LeftShift)) || glfwGetKey(window, Int(Key::RightShift))) + ? Modifiers{Modifier::Shift} : Modifiers{}; + if(glfwGetKey(window, Int(Key::LeftAlt)) || glfwGetKey(window, Int(Key::RightAlt))) { + mods |= Modifier::Alt; + } + if(glfwGetKey(window, Int(Key::LeftCtrl)) || glfwGetKey(window, Int(Key::RightCtrl))) { + mods |= Modifier::Ctrl; + } + if(glfwGetKey(window, Int(Key::RightSuper))) { + mods |= Modifier::AltGr; + } + + return mods; +} + void GlfwApplication::viewportEvent(const Vector2i&) {} void GlfwApplication::keyPressEvent(KeyEvent&) {} void GlfwApplication::keyReleaseEvent(KeyEvent&) {} diff --git a/src/Magnum/Platform/GlfwApplication.h b/src/Magnum/Platform/GlfwApplication.h index 45f6ed24b..17e882e8d 100644 --- a/src/Magnum/Platform/GlfwApplication.h +++ b/src/Magnum/Platform/GlfwApplication.h @@ -477,6 +477,27 @@ CORRADE_ENUMSET_OPERATORS(GlfwApplication::Configuration::WindowFlags) */ class GlfwApplication::InputEvent { public: + /** + * @brief Modifier + * + * @see @ref Modifiers, @ref KeyEvent::modifiers(), + * @ref MouseEvent::modifiers() + */ + enum class Modifier: Int { + Shift = GLFW_MOD_SHIFT, /**< Shift */ + Ctrl = GLFW_MOD_CONTROL, /**< Ctrl */ + Alt = GLFW_MOD_ALT, /**< Alt */ + AltGr = GLFW_MOD_SUPER, /**< AltGr */ + }; + + /** + * @brief Set of modifiers + * + * @see @ref KeyEvent::modifiers(), @ref MouseEvent::modifiers(), + * @ref MouseMoveEvent::modifiers() + */ + typedef Containers::EnumSet Modifiers; + /** @brief Copying is not allowed */ InputEvent(const InputEvent&) = delete; @@ -504,6 +525,8 @@ class GlfwApplication::InputEvent { bool _accepted; }; +CORRADE_ENUMSET_OPERATORS(GlfwApplication::InputEvent::Modifiers) + /** @brief Key event @@ -633,10 +656,16 @@ class GlfwApplication::KeyEvent: public GlfwApplication::InputEvent { /** @brief Key */ constexpr Key key() const { return _key; } + /** @brief Modifiers */ + constexpr Modifiers modifiers() const { return _modifiers; } + private: - constexpr KeyEvent(Key key): _key(key) {} + static Modifiers getCurrentGlfwModifiers(GLFWwindow* window); + + constexpr KeyEvent(Key key, Modifiers modifiers): _key(key), _modifiers(modifiers) {} const Key _key; + const Modifiers _modifiers; }; /** @@ -673,10 +702,14 @@ class GlfwApplication::MouseEvent: public GlfwApplication::InputEvent { /** @brief Button */ constexpr Button button() const { return _button; } + /** @brief Modifiers */ + constexpr Modifiers modifiers() const { return _modifiers; } + private: - constexpr MouseEvent(Button button): _button(button) {} + constexpr MouseEvent(Button button, Modifiers modifiers): _button(button), _modifiers(modifiers) {} const Button _button; + const Modifiers _modifiers; }; /** @@ -692,10 +725,14 @@ class GlfwApplication::MouseMoveEvent: public GlfwApplication::InputEvent { /** @brief Position */ constexpr Vector2i position() const { return _position; } + /** @brief Modifiers */ + constexpr Modifiers modifiers() const { return _modifiers; } + private: - constexpr MouseMoveEvent(const Vector2i& position): _position(position) {} + constexpr MouseMoveEvent(const Vector2i& position, Modifiers modifiers): _position(position), _modifiers(modifiers) {} const Vector2i _position; + const Modifiers _modifiers; }; /** @@ -711,10 +748,14 @@ class GlfwApplication::MouseScrollEvent: public GlfwApplication::InputEvent { /** @brief Scroll offset */ constexpr Vector2d offset() const { return _offset; } + /** @brief Modifiers */ + constexpr Modifiers modifiers() const { return _modifiers; } + private: - constexpr MouseScrollEvent(const Vector2d& offset): _offset(offset) {} + constexpr MouseScrollEvent(const Vector2d& offset, Modifiers modifiers): _offset(offset), _modifiers(modifiers) {} const Vector2d _offset; + const Modifiers _modifiers; }; /** @hideinitializer