From 4f9e13b2cb14654f97c9e39ec3fcff629700af6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 15 Nov 2012 20:56:16 +0100 Subject: [PATCH] Sdl2Application: implemented modifiers. In SDL they are only for keyboard events and there is no support for mouse modifiers like in X. Also reordered the enum in AbstractXApplication to make it consistent. --- src/Platform/AbstractXApplication.h | 7 ++++--- src/Platform/Sdl2Application.cpp | 24 +++++++++++++++++++----- src/Platform/Sdl2Application.h | 25 ++++++++++++++++--------- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/Platform/AbstractXApplication.h b/src/Platform/AbstractXApplication.h index 3dc104f17..3f52e5300 100644 --- a/src/Platform/AbstractXApplication.h +++ b/src/Platform/AbstractXApplication.h @@ -105,15 +105,16 @@ class AbstractXApplication { */ enum class Modifier: unsigned int { Shift = ShiftMask, /**< Shift */ - CapsLock = LockMask, /**< Caps lock */ Ctrl = ControlMask, /**< Ctrl */ Alt = Mod1Mask, /**< Alt */ - NumLock = Mod2Mask, /**< Num lock */ AltGr = Mod5Mask, /**< AltGr */ LeftButton = Button1Mask, /**< Left mouse button */ MiddleButton = Button2Mask, /**< Middle mouse button */ - RightButton = Button3Mask /**< Right mouse button */ + RightButton = Button3Mask, /**< Right mouse button */ + + CapsLock = LockMask, /**< Caps lock */ + NumLock = Mod2Mask /**< Num lock */ }; /** diff --git a/src/Platform/Sdl2Application.cpp b/src/Platform/Sdl2Application.cpp index 00dfaee4d..4f6091493 100644 --- a/src/Platform/Sdl2Application.cpp +++ b/src/Platform/Sdl2Application.cpp @@ -83,12 +83,26 @@ int Sdl2Application::exec() { break; } break; case SDL_KEYDOWN: - keyPressEvent(static_cast(event.key.keysym.sym), Modifiers(), {}); + case SDL_KEYUP: { + /* + * Fix up the modifiers -- we want >= operator to work + * properly on Shift, Ctrl, Alt, but SDL generates + * different event for left / right keys, thus + * (modifiers >= Shift) would pass only if both left and + * right were pressed, which is usually not what the + * developers wants. + */ + Modifiers modifiers(static_cast(event.key.keysym.mod)); + if(modifiers & Modifier::Shift) modifiers |= Modifier::Shift; + if(modifiers & Modifier::Ctrl) modifiers |= Modifier::Ctrl; + if(modifiers & Modifier::Alt) modifiers |= Modifier::Alt; + + if(event.type == SDL_KEYDOWN) + keyPressEvent(static_cast(event.key.keysym.sym), modifiers, {}); + else + keyReleaseEvent(static_cast(event.key.keysym.sym), modifiers, {}); break; - case SDL_KEYUP: - keyReleaseEvent(static_cast(event.key.keysym.sym), Modifiers(), {}); - break; - case SDL_MOUSEBUTTONDOWN: + } case SDL_MOUSEBUTTONDOWN: mousePressEvent(static_cast(event.button.button), Modifiers(), {event.button.x, event.button.y}); break; case SDL_MOUSEBUTTONUP: diff --git a/src/Platform/Sdl2Application.h b/src/Platform/Sdl2Application.h index 1304d16b1..ee930c0db 100644 --- a/src/Platform/Sdl2Application.h +++ b/src/Platform/Sdl2Application.h @@ -100,17 +100,24 @@ class Sdl2Application { /** * @brief %Modifier * - * @see Modifiers, keyPressEvent(), keyReleaseEvent(), - * mousePressEvent(), mouseReleaseEvent(), mouseMotionEvent() + * @see Modifiers, keyPressEvent(), keyReleaseEvent() */ - enum class Modifier: unsigned int {}; + enum class Modifier: Uint16 { + Shift = KMOD_SHIFT, /**< Shift */ + Ctrl = KMOD_CTRL, /**< Ctrl */ + Alt = KMOD_ALT, /**< Alt */ + AltGr = KMOD_MODE, /**< AltGr */ + + CapsLock = KMOD_CAPS, /**< Caps lock */ + NumLock = KMOD_NUM /**< Num lock */ + }; /** * @brief Set of modifiers * * @see keyPressEvent(), keyReleaseEvent() */ - typedef Corrade::Containers::EnumSet Modifiers; + typedef Corrade::Containers::EnumSet Modifiers; /** * @brief Key @@ -130,7 +137,7 @@ class Sdl2Application { /** * @brief Key press event * @param key Key pressed - * @param modifiers Active modifiers (not yet implemented) + * @param modifiers Active modifiers * @param position Cursor position (not yet implemented) */ virtual void keyPressEvent(Key key, Modifiers modifiers, const Math::Vector2& position); @@ -138,7 +145,7 @@ class Sdl2Application { /** * @brief Key release event * @param key Key released - * @param modifiers Active modifiers (not yet implemented) + * @param modifiers Active modifiers * @param position Cursor position (not yet implemented) */ virtual void keyReleaseEvent(Key key, Modifiers modifiers, const Math::Vector2& position); @@ -175,7 +182,7 @@ class Sdl2Application { /** * @brief Mouse press event * @param button Button pressed - * @param modifiers Active modifiers (not yet implemented) + * @param modifiers Active modifiers (not implemented) * @param position Cursor position * * Called when mouse button is pressed. Default implementation does @@ -186,7 +193,7 @@ class Sdl2Application { /** * @brief Mouse release event * @param button Button released - * @param modifiers Active modifiers (not yet implemented) + * @param modifiers Active modifiers (not implemented) * @param position Cursor position * * Called when mouse button is released. Default implementation does @@ -196,7 +203,7 @@ class Sdl2Application { /** * @brief Mouse motion event - * @param modifiers Active modifiers (not yet implemented) + * @param modifiers Active modifiers (not implemented) * @param position Mouse position relative to the window * * Called when mouse is moved. Default implementation does nothing.