From 9cb08467184a6f7eeaec794cff3a76b83ff97a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 28 May 2012 14:37:07 +0200 Subject: [PATCH] Sdl2Context: mouse handling. --- src/Contexts/Sdl2Context.cpp | 13 +++++++++ src/Contexts/Sdl2Context.h | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/Contexts/Sdl2Context.cpp b/src/Contexts/Sdl2Context.cpp index 1654eb4a8..0ca70b5c5 100644 --- a/src/Contexts/Sdl2Context.cpp +++ b/src/Contexts/Sdl2Context.cpp @@ -82,6 +82,19 @@ int Sdl2Context::exec() { _redraw = true; break; } break; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + mouseEvent(static_cast(event.button.button), + static_cast(event.button.state), + {event.button.x, event.button.y}); + break; + case SDL_MOUSEWHEEL: + mouseWheelEvent({event.wheel.x, event.wheel.y}); + break; + case SDL_MOUSEMOTION: + mouseMotionEvent({event.motion.x, event.motion.y}, + {event.motion.xrel, event.motion.yrel}); + break; case SDL_QUIT: return 0; } diff --git a/src/Contexts/Sdl2Context.h b/src/Contexts/Sdl2Context.h index efa71f157..bbb3227eb 100644 --- a/src/Contexts/Sdl2Context.h +++ b/src/Contexts/Sdl2Context.h @@ -75,6 +75,61 @@ class Sdl2Context: public AbstractContext { /*@}*/ + /** @{ @name Mouse handling */ + + public: + /** + * @brief Mouse button + * @see mouseEvent() + */ + enum class MouseButton: Uint8 { + Left = SDL_BUTTON_LEFT, /**< Left button */ + Middle = SDL_BUTTON_MIDDLE, /**< Middle button */ + Right = SDL_BUTTON_RIGHT /**< Right button */ + }; + + /** + * @brief Mouse state + * @see mouseEvent() + */ + enum class MouseState: Uint8 { + Pressed = SDL_PRESSED, /**< Button pressed */ + Released = SDL_RELEASED /**< Button released */ + }; + + protected: + /** + * @brief Mouse event + * @param button Mouse button + * @param state Mouse state + * @param position Mouse position relative to the window + * + * Called when mouse button is pressed or released. Default + * implementation does nothing. + */ + virtual inline void mouseEvent(MouseButton button, MouseState state, const Math::Vector2& position) {} + + /** + * @brief Mouse wheel event + * @param direction Wheel rotation direction. Negative Y is up and + * positive X is right. + * + * Called when mouse wheel is rotated. Default implementation does + * nothing. + */ + virtual inline void mouseWheelEvent(const Math::Vector2& direction) {} + + /** + * @brief Mouse motion event + * @param position Mouse position relative to the window + * @param delta Mouse position relative to last motion event + * + * Called when mouse is moved. Default implementation does nothing. + */ + virtual inline void mouseMotionEvent(const Math::Vector2& position, const Math::Vector2& delta) {} + + /*@}*/ + private: SDL_Window* window; SDL_GLContext context;