Browse Source

Sdl2Context: mouse handling.

vectorfields
Vladimír Vondruš 14 years ago
parent
commit
9cb0846718
  1. 13
      src/Contexts/Sdl2Context.cpp
  2. 55
      src/Contexts/Sdl2Context.h

13
src/Contexts/Sdl2Context.cpp

@ -82,6 +82,19 @@ int Sdl2Context::exec() {
_redraw = true;
break;
} break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
mouseEvent(static_cast<MouseButton>(event.button.button),
static_cast<MouseState>(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;
}

55
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<int>& 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<int>& 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<int>& position, const Math::Vector2<int>& delta) {}
/*@}*/
private:
SDL_Window* window;
SDL_GLContext context;

Loading…
Cancel
Save