lib/sdl: intro `grab_input`, `ignore_mouse_motion_events` and fix `show_cursor=`
[nit.git] / lib / sdl.nit
index df270e4..26ae459 100644 (file)
@@ -92,17 +92,14 @@ extern class SDLDisplay `{SDL_Surface *`}
                SDL_FillRect(recv, NULL, SDL_MapRGB(recv->format,ri,gi,bi));
        `}
 
+       # SDL events since the last call to this method
        fun events: Sequence[SDLInputEvent]
        do
-               var new_event: nullable Object = null
-               var events = new List[SDLInputEvent]
+               var events = new Array[SDLInputEvent]
                loop
-                       new_event = poll_event
-                       if new_event != null then # new_event isa Event then #
-                               events.add(new_event)
-                       else
-                               break
-                       end
+                       var new_event = poll_event
+                       if new_event == null then break
+                       events.add new_event
                end
                return events
        end
@@ -163,7 +160,26 @@ extern class SDLDisplay `{SDL_Surface *`}
        fun warp_mouse(x,y: Int) `{ SDL_WarpMouse(x, y); `}
 
        # Show or hide the cursor
-       fun show_cursor(show: Bool) `{ SDL_ShowCursor(show); `}
+       fun show_cursor=(val: Bool) `{ SDL_ShowCursor(val? SDL_ENABLE: SDL_DISABLE); `}
+
+       # Is the cursor visible?
+       fun show_cursor: Bool `{ SDL_ShowCursor(SDL_QUERY); `}
+
+       # Grab or release the input
+       fun grab_input=(val: Bool) `{ SDL_WM_GrabInput(val? SDL_GRAB_ON: SDL_GRAB_OFF); `}
+
+       # Is the input grabbed?
+       fun grab_input: Bool `{ SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON; `}
+
+       # Are instances of `SDLMouseMotionEvent` ignored?
+       fun ignore_mouse_motion_events: Bool `{
+               return SDL_EventState(SDL_MOUSEMOTION, SDL_QUERY);
+       `}
+
+       # Do not raise instances of `SDLMouseMotionEvent` if `val`
+       fun ignore_mouse_motion_events=(val: Bool) `{
+               SDL_EventState(SDL_MOUSEMOTION, val? SDL_IGNORE: SDL_ENABLE);
+       `}
 end
 
 # Basic Drawing figures
@@ -173,7 +189,7 @@ extern class SDLDrawable `{SDL_Surface*`}
        redef type I: SDLImage
 
        redef fun blit(img, x, y) do native_blit(img, x.to_i, y.to_i)
-       fun native_blit(img: I, x, y: Int) `{
+       private fun native_blit(img: I, x, y: Int) `{
                SDL_Rect dst;
                dst.x = x;
                dst.y = y;
@@ -283,6 +299,24 @@ class SDLMouseButtonEvent
        redef var pressed: Bool
        redef fun depressed: Bool do return not pressed
 
+       # Is this event raised by the left button?
+       fun is_left_button: Bool do return button == 1
+
+       # Is this event raised by the right button?
+       fun is_right_button: Bool do return button == 2
+
+       # Is this event raised by the middle button?
+       fun is_middle_button: Bool do return button == 3
+
+       # Is this event raised by the wheel going down?
+       fun is_down_wheel: Bool do return button == 4
+
+       # Is this event raised by the wheel going up?
+       fun is_up_wheel: Bool do return button == 5
+
+       # Is this event raised by the wheel?
+       fun is_wheel: Bool do return is_down_wheel or is_up_wheel
+
        init (x, y: Float, button: Int, pressed: Bool)
        do
                super(x, y)