Browse Source

Sdl2Application: using special classes for input events.

Will make lazy-loading possible and allows for less-constrained static
polymorphism.
pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
5354396af6
  1. 48
      src/Platform/Sdl2Application.cpp
  2. 219
      src/Platform/Sdl2Application.h

48
src/Platform/Sdl2Application.cpp

@ -82,6 +82,7 @@ int Sdl2Application::exec() {
_redraw = true; _redraw = true;
break; break;
} break; } break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
case SDL_KEYUP: { case SDL_KEYUP: {
/* /*
@ -92,31 +93,36 @@ int Sdl2Application::exec() {
* right were pressed, which is usually not what the * right were pressed, which is usually not what the
* developers wants. * developers wants.
*/ */
Modifiers modifiers(static_cast<Modifier>(event.key.keysym.mod)); InputEvent::Modifiers modifiers(static_cast<InputEvent::Modifier>(event.key.keysym.mod));
if(modifiers & Modifier::Shift) modifiers |= Modifier::Shift; if(modifiers & InputEvent::Modifier::Shift) modifiers |= InputEvent::Modifier::Shift;
if(modifiers & Modifier::Ctrl) modifiers |= Modifier::Ctrl; if(modifiers & InputEvent::Modifier::Ctrl) modifiers |= InputEvent::Modifier::Ctrl;
if(modifiers & Modifier::Alt) modifiers |= Modifier::Alt; if(modifiers & InputEvent::Modifier::Alt) modifiers |= InputEvent::Modifier::Alt;
if(event.type == SDL_KEYDOWN) KeyEvent e(static_cast<KeyEvent::Key>(event.key.keysym.sym), modifiers);
keyPressEvent(static_cast<Key>(event.key.keysym.sym), modifiers, {}); event.type == SDL_KEYDOWN ? keyPressEvent(e) : keyReleaseEvent(e);
else
keyReleaseEvent(static_cast<Key>(event.key.keysym.sym), modifiers, {});
break;
} case SDL_MOUSEBUTTONDOWN:
mousePressEvent(static_cast<MouseButton>(event.button.button), Modifiers(), {event.button.x, event.button.y});
break; break;
case SDL_MOUSEBUTTONUP: }
mouseReleaseEvent(static_cast<MouseButton>(event.button.button), Modifiers(), {event.button.x, event.button.y});
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP: {
MouseEvent e(static_cast<MouseEvent::Button>(event.button.button), {event.button.x, event.button.y});
event.type == SDL_MOUSEBUTTONDOWN ? mousePressEvent(e) : mouseReleaseEvent(e);
break; break;
}
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
if(event.wheel.y != 0) if(event.wheel.y != 0) {
mousePressEvent(event.wheel.y < 0 ? MouseButton::WheelUp : MouseButton::WheelDown, Modifiers(), {event.wheel.x, event.wheel.y}); MouseEvent e(event.wheel.y < 0 ? MouseEvent::Button::WheelUp : MouseEvent::Button::WheelDown, {event.wheel.x, event.wheel.y});
break; mousePressEvent(e);
case SDL_MOUSEMOTION: } break;
mouseMotionEvent(Modifiers(), {event.motion.x, event.motion.y});
case SDL_MOUSEMOTION: {
MouseMoveEvent e({event.motion.x, event.motion.y});
mouseMoveEvent(e);
break; break;
case SDL_QUIT: }
return 0;
case SDL_QUIT: return 0;
} }
} }

219
src/Platform/Sdl2Application.h

@ -54,6 +54,11 @@ MAGNUM_SDL2APPLICATION_MAIN(MyApplication)
*/ */
class Sdl2Application { class Sdl2Application {
public: public:
class InputEvent;
class KeyEvent;
class MouseEvent;
class MouseMoveEvent;
/** /**
* @brief Constructor * @brief Constructor
* @param argc Count of arguments of `main()` function * @param argc Count of arguments of `main()` function
@ -79,9 +84,10 @@ class Sdl2Application {
*/ */
int exec(); int exec();
protected:
/** @{ @name Drawing functions */ /** @{ @name Drawing functions */
protected:
/** @copydoc GlutApplication::viewportEvent() */ /** @copydoc GlutApplication::viewportEvent() */
virtual void viewportEvent(const Math::Vector2<GLsizei>& size) = 0; virtual void viewportEvent(const Math::Vector2<GLsizei>& size) = 0;
@ -98,11 +104,75 @@ class Sdl2Application {
/** @{ @name Keyboard handling */ /** @{ @name Keyboard handling */
/**
* @brief Key press event
*
* Called when an key is pressed. Default implementation does nothing.
*/
virtual void keyPressEvent(KeyEvent& event);
/**
* @brief Key release event
*
* Called when an key is released. Default implementation does nothing.
*/
virtual void keyReleaseEvent(KeyEvent& event);
/*@}*/
/** @{ @name Mouse handling */
/**
* @brief Mouse press event
*
* Called when mouse button is pressed. Default implementation does
* nothing.
*/
virtual void mousePressEvent(MouseEvent& event);
/**
* @brief Mouse release event
*
* Called when mouse button is released. Default implementation does
* nothing.
*/
virtual void mouseReleaseEvent(MouseEvent& event);
/**
* @brief Mouse move event
*
* Called when mouse is moved. Default implementation does nothing.
*/
virtual void mouseMoveEvent(MouseMoveEvent& event);
/*@}*/
private:
SDL_Window* window;
SDL_GLContext context;
Context* c;
bool _redraw;
};
/**
@brief Base for input events
@see KeyEvent, MouseEvent, MouseMoveEvent, keyPressEvent(), keyReleaseEvent(),
mousePressEvent(), mouseReleaseEvent(), mouseMoveEvent()
*/
class Sdl2Application::InputEvent {
InputEvent(const InputEvent& other) = delete;
InputEvent(InputEvent&& other) = delete;
InputEvent& operator=(const InputEvent& other) = delete;
InputEvent& operator=(InputEvent&& other) = delete;
public: public:
/** /**
* @brief %Modifier * @brief %Modifier
* *
* @see Modifiers, keyPressEvent(), keyReleaseEvent() * @see Modifiers, KeyEvent::modifiers()
*/ */
enum class Modifier: Uint16 { enum class Modifier: Uint16 {
Shift = KMOD_SHIFT, /**< Shift */ Shift = KMOD_SHIFT, /**< Shift */
@ -117,14 +187,47 @@ class Sdl2Application {
/** /**
* @brief Set of modifiers * @brief Set of modifiers
* *
* @see keyPressEvent(), keyReleaseEvent() * @see KeyEvent::modifiers()
*/ */
typedef Corrade::Containers::EnumSet<Modifier, Uint16> Modifiers; typedef Corrade::Containers::EnumSet<Modifier, Uint16> Modifiers;
inline virtual ~InputEvent() {}
/**
* @brief Set event as accepted
*
* Does nothing. Included only for compatibility with
* NaClApplication::InputEvent.
*/
inline void setAccepted(bool = true) {}
/**
* @brief Whether the event is accepted
*
* Always returns true. Included only for compatibility with
* NaClApplication::InputEvent.
*/
inline bool isAccepted() const { return true; }
#ifndef DOXYGEN_GENERATING_OUTPUT
protected:
inline InputEvent() {}
#endif
};
/**
@brief Key event
@see keyPressEvent(), keyReleaseEvent()
*/
class Sdl2Application::KeyEvent: public Sdl2Application::InputEvent {
friend class Sdl2Application;
public:
/** /**
* @brief Key * @brief Key
* *
* @see keyPressEvent(), keyReleaseEvent() * @see key()
*/ */
enum class Key: SDL_Keycode { enum class Key: SDL_Keycode {
Enter = SDLK_RETURN, /**< Enter */ Enter = SDLK_RETURN, /**< Enter */
@ -199,34 +302,34 @@ class Sdl2Application {
Z = SDLK_z /**< Letter Z */ Z = SDLK_z /**< Letter Z */
}; };
protected: /** @brief Key */
/** inline Key key() const { return _key; }
* @brief Key press event
* @param key Key pressed
* @param modifiers Active modifiers
* @param position Cursor position (not implemented)
*/
virtual void keyPressEvent(Key key, Modifiers modifiers, const Math::Vector2<int>& position);
/** /** @brief Modifiers */
* @brief Key release event inline Modifiers modifiers() const { return _modifiers; }
* @param key Key released
* @param modifiers Active modifiers
* @param position Cursor position (not implemented)
*/
virtual void keyReleaseEvent(Key key, Modifiers modifiers, const Math::Vector2<int>& position);
/*@}*/ private:
inline KeyEvent(Key key, Modifiers modifiers): _key(key), _modifiers(modifiers) {}
/** @{ @name Mouse handling */ const Key _key;
const Modifiers _modifiers;
};
/**
@brief Mouse event
@see MouseMoveEvent, mousePressEvent(), mouseReleaseEvent()
*/
class Sdl2Application::MouseEvent: public Sdl2Application::InputEvent {
friend class Sdl2Application;
public: public:
/** /**
* @brief Mouse button * @brief Mouse button
* *
* @see mousePressEvent(), mouseReleaseEvent() * @see button()
*/ */
enum class MouseButton: Uint8 { enum class Button: Uint8 {
Left = SDL_BUTTON_LEFT, /**< Left button */ Left = SDL_BUTTON_LEFT, /**< Left button */
Middle = SDL_BUTTON_MIDDLE, /**< Middle button */ Middle = SDL_BUTTON_MIDDLE, /**< Middle button */
Right = SDL_BUTTON_RIGHT, /**< Right button */ Right = SDL_BUTTON_RIGHT, /**< Right button */
@ -234,47 +337,35 @@ class Sdl2Application {
WheelDown = 5 /**< Wheel down */ WheelDown = 5 /**< Wheel down */
}; };
protected: /** @brief Button */
/** inline Button button() const { return _button; }
* @brief Mouse press event
* @param button Button pressed
* @param modifiers Active modifiers (not implemented)
* @param position Cursor position
*
* Called when mouse button is pressed. Default implementation does
* nothing.
*/
virtual void mousePressEvent(MouseButton button, Modifiers modifiers, const Math::Vector2<int>& position);
/** /** @brief Position */
* @brief Mouse release event inline Math::Vector2<int> position() const { return _position; }
* @param button Button released
* @param modifiers Active modifiers (not implemented)
* @param position Cursor position
*
* Called when mouse button is released. Default implementation does
* nothing.
*/
virtual void mouseReleaseEvent(MouseButton button, Modifiers modifiers, const Math::Vector2<int>& position);
/** private:
* @brief Mouse motion event inline MouseEvent(Button button, const Math::Vector2<int>& position): _button(button), _position(position) {}
* @param modifiers Active modifiers (not implemented)
* @param position Mouse position relative to the window
*
* Called when mouse is moved. Default implementation does nothing.
*/
virtual void mouseMotionEvent(Modifiers modifiers, const Math::Vector2<int>& position);
/*@}*/ const Button _button;
const Math::Vector2<int> _position;
};
private: /**
SDL_Window* window; @brief Mouse move event
SDL_GLContext context;
Context* c; @see MouseEvent, mouseMoveEvent()
*/
class Sdl2Application::MouseMoveEvent: public Sdl2Application::InputEvent {
friend class Sdl2Application;
bool _redraw; public:
/** @brief Position */
inline Math::Vector2<int> position() const { return _position; }
private:
inline MouseMoveEvent(const Math::Vector2<int>& position): _position(position) {}
const Math::Vector2<int> _position;
}; };
/** @hideinitializer /** @hideinitializer
@ -305,14 +396,14 @@ When no other application header is included this macro is also aliased to
#endif #endif
#endif #endif
CORRADE_ENUMSET_OPERATORS(Sdl2Application::Modifiers) CORRADE_ENUMSET_OPERATORS(Sdl2Application::InputEvent::Modifiers)
/* Implementations for inline functions with unused parameters */ /* Implementations for inline functions with unused parameters */
inline void Sdl2Application::keyPressEvent(Key, Modifiers, const Math::Vector2<int>&) {} inline void Sdl2Application::keyPressEvent(KeyEvent&) {}
inline void Sdl2Application::keyReleaseEvent(Key, Modifiers, const Math::Vector2<int>&) {} inline void Sdl2Application::keyReleaseEvent(KeyEvent&) {}
inline void Sdl2Application::mousePressEvent(MouseButton, Modifiers, const Math::Vector2<int>&) {} inline void Sdl2Application::mousePressEvent(MouseEvent&) {}
inline void Sdl2Application::mouseReleaseEvent(MouseButton, Modifiers, const Math::Vector2<int>&) {} inline void Sdl2Application::mouseReleaseEvent(MouseEvent&) {}
inline void Sdl2Application::mouseMotionEvent(Modifiers, const Math::Vector2<int>&) {} inline void Sdl2Application::mouseMoveEvent(MouseMoveEvent&) {}
}} }}

Loading…
Cancel
Save