From ceb116c41c0b26036599df1fcfb90e7c615eea38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 4 Aug 2012 19:09:45 +0200 Subject: [PATCH] EglContext: keyboard and mouse handling. --- src/Contexts/EglContext.cpp | 24 +++++++++++ src/Contexts/EglContext.h | 86 +++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/src/Contexts/EglContext.cpp b/src/Contexts/EglContext.cpp index e3ab85d74..e66ff5036 100644 --- a/src/Contexts/EglContext.cpp +++ b/src/Contexts/EglContext.cpp @@ -17,6 +17,9 @@ #define None 0L // redef Xlib nonsense +/* Mask for X events */ +#define INPUT_MASK KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask + using namespace std; namespace Magnum { namespace Contexts { @@ -98,6 +101,9 @@ EglContext::EglContext(int&, char**, const string& title, const Math::Vector2(XLookupKeysym(&event.xkey, 0)), {event.xkey.x, event.xkey.y}); + break; + case KeyRelease: + keyReleaseEvent(static_cast(XLookupKeysym(&event.xkey, 0)), {event.xkey.x, event.xkey.y}); + break; + case ButtonPress: + mousePressEvent(static_cast(event.xbutton.button), {event.xbutton.x, event.xbutton.y}); + break; + case ButtonRelease: + mouseReleaseEvent(static_cast(event.xbutton.button), {event.xbutton.x, event.xbutton.y}); + break; + } + } + /** @todo Handle at least window closing and resizing */ drawEvent(); } diff --git a/src/Contexts/EglContext.h b/src/Contexts/EglContext.h index fe2e48cb2..b9c4edb57 100644 --- a/src/Contexts/EglContext.h +++ b/src/Contexts/EglContext.h @@ -62,6 +62,8 @@ class EglContext: public AbstractContext { int exec(); + /** @{ @name Drawing functions */ + protected: /** @copydoc GlutContext::viewportEvent() */ virtual void viewportEvent(const Math::Vector2& size) = 0; @@ -72,6 +74,84 @@ class EglContext: public AbstractContext { /** @copydoc GlutContext::swapBuffers() */ inline void swapBuffers() { eglSwapBuffers(display, surface); } + /*@}*/ + + /** @{ @name Keyboard handling */ + + public: + /** @brief Key */ + enum class Key: KeySym { + Up = XK_Up, /**< Up arrow */ + Down = XK_Down, /**< Down arrow */ + Left = XK_Left, /**< Left arrow */ + Right = XK_Right, /**< Right arrow */ + F1 = XK_F1, /**< F1 */ + F2 = XK_F2, /**< F2 */ + F3 = XK_F3, /**< F3 */ + F4 = XK_F4, /**< F4 */ + F5 = XK_F5, /**< F5 */ + F6 = XK_F6, /**< F6 */ + F7 = XK_F7, /**< F7 */ + F8 = XK_F8, /**< F8 */ + F9 = XK_F9, /**< F9 */ + F10 = XK_F10, /**< F10 */ + F11 = XK_F11, /**< F11 */ + F12 = XK_F12, /**< F12 */ + Home = XK_Home, /**< Home */ + End = XK_End, /**< End */ + PageUp = XK_Page_Up, /**< Page up */ + PageDown = XK_Page_Down /**< Page down */ + }; + + protected: + /** + * @brief Key press event + * @param key Key pressed + * @param position Cursor position + * + * Called when an key is pressed. Default implementation does nothing. + */ + virtual void keyPressEvent(Key key, const Math::Vector2& position) = 0; + + /** + * @brief Key press event + * @param key Key released + * @param position Cursor position + * + * Called when an key is released. Default implementation does nothing. + */ + virtual void keyReleaseEvent(Key key, const Math::Vector2& position) = 0; + + /*@}*/ + + /** @{ @name Mouse handling */ + public: + /** @brief Mouse button */ + enum class MouseButton: unsigned int { + Left = Button1, /**< Left button */ + Middle = Button2, /**< Middle button */ + Right = Button3, /**< Right button */ + WheelUp = Button4, /**< Wheel up */ + WheelDown = Button5 /**< Wheel down */ + }; + + protected: + /** + * @brief Mouse press event + * + * Called when mouse button is pressed. Default implementation does + * nothing. + */ + virtual void mousePressEvent(MouseButton button, const Math::Vector2& position); + + /** + * @brief Mouse release event + * + * Called when mouse button is released. Default implementation does + * nothing. + */ + virtual void mouseReleaseEvent(MouseButton button, const Math::Vector2& position); + private: Display* xDisplay; Window xWindow; @@ -84,6 +164,12 @@ class EglContext: public AbstractContext { Math::Vector2 viewportSize; }; +inline void EglContext::keyPressEvent(EglContext::Key, const Math::Vector2&) {} +inline void EglContext::keyReleaseEvent(EglContext::Key, const Math::Vector2&) {} +inline void EglContext::mousePressEvent(EglContext::MouseButton, const Math::Vector2&) {} +inline void EglContext::mouseReleaseEvent(EglContext::MouseButton, const Math::Vector2&) {} + + }} #endif