sdl2: implement more events
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 24 Feb 2017 01:38:40 +0000 (20:38 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Mon, 27 Feb 2017 19:50:11 +0000 (14:50 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/sdl2/events.nit

index 6da4c24..8eb14f2 100644 (file)
@@ -23,7 +23,7 @@ in "C Header" `{
        #include <SDL2/SDL_events.h>
 `}
 
-# A temporary buffer for a SDL 2 event
+# Temporary buffer for an SDL 2 event exposing the pseudo-class hierarchy metadata
 #
 # An instance of this class should be used to call `poll_event` and `to_event`.
 extern class SDLEventBuffer `{SDL_Event *`}
@@ -41,46 +41,71 @@ extern class SDLEventBuffer `{SDL_Event *`}
        #
        # Note: The returned `SDLEvent` is just a different Nit instance pointing to the same data.
        # A call to `poll_event` will invalidate any instances returned previously by `to_event`.
-       #
-       # TODO remove `nullable` from the return type once all cases are correctly handled.
-       fun to_event: nullable SDLEvent
+       fun to_event: SDLEvent
        do
                if is_quit then return to_quit
                if is_mouse_motion then return to_mouse_motion
                if is_mouse_button_down then return to_mouse_button_down
                if is_mouse_button_up then return to_mouse_button_up
-               return null
+               if is_keydown then return to_keydown
+               if is_keyup then return to_keyup
+               return to_event_direct
        end
 
+       private fun to_event_direct: SDLEvent `{ return self; `}
+
        # Is this a quit event?
        fun is_quit: Bool `{ return self->type == SDL_QUIT; `}
 
        # Get a reference to data at `self` as a `SDLQuitEvent`
+       #
+       # Require: `is_quit`
        fun to_quit: SDLQuitEvent `{ return self; `}
 
        # Is this a mouse motion event?
        fun is_mouse_motion: Bool `{ return self->type == SDL_MOUSEMOTION; `}
 
        # Get a reference to data at `self` as a `SDLMouseMotionEvent`
+       #
+       # Require: `is_mouse_motion`
        fun to_mouse_motion: SDLMouseMotionEvent `{ return self; `}
 
        # Is this a mouse button down event?
        fun is_mouse_button_down: Bool `{ return self->type == SDL_MOUSEBUTTONDOWN; `}
 
        # Get a reference to data at `self` as a `SDLMouseButtonDownEvent`
+       #
+       # Require: `is_mouse_button_down`
        fun to_mouse_button_down: SDLMouseButtonDownEvent `{ return self; `}
 
        # Is this a mouse button up event?
        fun is_mouse_button_up: Bool `{ return self->type == SDL_MOUSEBUTTONUP; `}
 
        # Get a reference to data at `self` as a `SDLMouseButtonUpEvent`
+       #
+       # Require: `is_mouse_button_up`
        fun to_mouse_button_up: SDLMouseButtonUpEvent `{ return self; `}
 
+       # Is this a key presse event?
+       fun is_keydown: Bool `{ return self->type == SDL_KEYDOWN; `}
+
+       # Get a reference to data at `self` as a `SDLKeyboardDownEvent`
+       #
+       # Require: `is_keydown`
+       fun to_keydown: SDLKeyboardDownEvent `{ return self; `}
+
+       #  Is this a key release event?
+       fun is_keyup: Bool `{ return self->type == SDL_KEYUP; `}
+
+       # Get a reference to data at `self` as a `SDLKeyboardUpEvent`
+       #
+       # Require: `is_keyup`
+       fun to_keyup: SDLKeyboardUpEvent `{ return self; `}
+
        # TODO other SDL events:
        #
        # SDL_CommonEvent common
        # SDL_WindowEvent window
-       # SDL_KeyboardEvent key
        # SDL_TextEditingEvent edit
        # SDL_TextInputEvent text
        # SDL_MouseWheelEvent wheel
@@ -92,7 +117,6 @@ extern class SDLEventBuffer `{SDL_Event *`}
        # SDL_ControllerAxisEvent caxis
        # SDL_ControllerButtonEvent cbutton
        # SDL_ControllerDeviceEvent cdevice
-       # SDL_QuitEvent quit
        # SDL_UserEvent user
        # SDL_SysWMEvent syswm
        # SDL_TouchFingerEvent tfinger
@@ -101,16 +125,16 @@ extern class SDLEventBuffer `{SDL_Event *`}
        # SDL_DropEvent drop
 end
 
-# An event from SDL 2
+# SDL 2 event, only the data and no metadata
 extern class SDLEvent `{SDL_Event *`}
 end
 
-# A quit event, usually from the close window button
+# Quit event, usually from the close window button
 extern class SDLQuitEvent
        super SDLEvent
 end
 
-# A mouse event
+# Mouse event
 extern class SDLMouseEvent
        super SDLEvent
 
@@ -121,17 +145,30 @@ extern class SDLMouseEvent
 
        # Which mouse, pointer or finger raised this event
        fun which: Int `{ return self->motion.which; `}
+
+       # X coordinate on screen of this event
+       fun x: Int is abstract
+
+       # Y coordinate on screen of this event
+       fun y: Int is abstract
 end
 
-# A mouse motion event
+# Mouse motion event
 extern class SDLMouseMotionEvent
        super SDLMouseEvent
 
-       # X coordinate on screen of this event
-       fun x: Int `{ return self->motion.x; `}
+       redef fun x `{ return self->motion.x; `}
 
-       # Y coordinate on screen of this event
-       fun y: Int `{ return self->motion.y; `}
+       redef fun y `{ return self->motion.y; `}
+
+       # State of the buttons
+       #
+       # ~~~raw
+       # state & 1 == 1 -> left button down
+       # state & 2 == 2 -> middle button down
+       # state & 4 == 4 -> right button down
+       # ~~~
+       fun state: Int `{ return self->motion.state; `}
 
        # Difference on the X axis between this event and the previous one
        fun xrel: Int `{ return self->motion.xrel; `}
@@ -140,26 +177,84 @@ extern class SDLMouseMotionEvent
        fun yrel: Int `{ return self->motion.yrel; `}
 end
 
-# A mouse button event
+# Mouse button event
 #
 # This could as well be an abstract class. All instances of `SDLMouseButtonEvent`
 # is either a `SDLMouseButtonUpEvent` or a `SDLMouseButtonDownEvent`.
 extern class SDLMouseButtonEvent
        super SDLMouseEvent
 
-       # X coordinate on screen of this event
-       fun x: Int `{ return self->button.x; `}
+       # Index of the button
+       #
+       # ~~~raw
+       # 1 -> left button
+       # 2 -> center button
+       # 3 -> right button
+       # ~~~
+       fun button: Int `{ return self->button.button; `}
 
-       # Y coordinate on screen of this event
-       fun y: Int `{ return self->button.y; `}
+       # Is the button currently pressed down?
+       fun pressed: Bool `{ return self->button.state == SDL_PRESSED; `}
+
+       # Number of clicks (1 or 2)
+       fun clicks: Int `{ return self->button.clicks; `}
+
+       redef fun x `{ return self->button.x; `}
+
+       redef fun y `{ return self->button.y; `}
 end
 
-# A mouse button release event
+# Mouse button release event
 extern class SDLMouseButtonUpEvent
        super SDLMouseButtonEvent
 end
 
-# A mouse button click event
+# Mouse button click event
 extern class SDLMouseButtonDownEvent
        super SDLMouseButtonEvent
 end
+
+# Keyboard button event
+extern class SDLKeyboardEvent
+       super SDLEvent
+
+       # Is this is a key repeat?
+       fun repeat: Bool `{ return self->key.repeat; `}
+
+       # The key that was pressed or released
+       fun keysym: SDLKeysym `{ return &self->key.keysym; `}
+
+       redef fun to_s do return native_to_s.to_s
+       private fun native_to_s: CString `{
+               return (char*)SDL_GetKeyName(self->key.keysym.sym);
+       `}
+end
+
+# Keyboard button release event
+extern class SDLKeyboardUpEvent
+       super SDLKeyboardEvent
+end
+
+# Keyboard button click event
+extern class SDLKeyboardDownEvent
+       super SDLKeyboardEvent
+end
+
+# Key information
+extern class SDLKeysym `{ SDL_Keysym * `}
+       # Is this the arrow right key?
+       fun is_right: Bool `{ return self->sym == SDLK_RIGHT; `}
+
+       # Is this the arrow left key?
+       fun is_left: Bool `{ return self->sym == SDLK_LEFT; `}
+
+       # Is this the arrow down key?
+       fun is_down: Bool `{ return self->sym == SDLK_DOWN; `}
+
+       # Is this the arrow up key?
+       fun is_up: Bool `{ return self->sym == SDLK_UP; `}
+
+       # Modification keys
+       fun mod: Int `{ return self->mod; `}
+       # TODO related masks
+end