1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Support services for Gamnit on GNU/Linux
23 private var sdl_event_buffer
= new SDLEventBuffer.malloc
28 if display
== null then return
31 var avail
= sdl_event_buffer
.poll_event
32 if not avail
then break
34 # Convert to an SDL event with data
35 var sdl_event
= sdl_event_buffer
.to_event
36 # Convert to `mnit::inputs` conforming objects
37 var gamnit_event
= sdl_event
.to_gamnit_event
(sdl_event_buffer
)
38 accept_event gamnit_event
44 # Redef services from `sdl2::events`
47 private fun to_gamnit_event
(buffer
: SDLEventBuffer): GamnitInputEvent
48 do return new GamnitOtherEvent(buffer
, self)
51 redef class SDLQuitEvent
52 redef fun to_gamnit_event
(buffer
) do return new GamnitQuitEvent(buffer
, self)
55 redef class SDLMouseEvent
56 redef fun to_gamnit_event
(buffer
) do return new GamnitPointerEvent(buffer
, self)
59 redef class SDLKeyboardEvent
60 redef fun to_gamnit_event
(buffer
) do return new GamnitKeyEvent(buffer
, self)
64 # Implement `mnit::inputs` interfaces
66 # SDL2 event wrapper implementing the `mnit::input` API
68 # There is two views to the underlying native object: `buffer` and `native`.
69 class GamnitInputEvent
72 # Native SDL 2 event buffer with the pseudo class hierarchy metadata
73 var buffer
: SDLEventBuffer
78 # Type of the `native` underlying SDL 2 event
82 # Event on user requested quit
84 super GamnitInputEvent
87 redef type NATIVE: SDLQuitEvent
90 # Event on keyboard input
92 super GamnitInputEvent
95 redef type NATIVE: SDLKeyboardEvent
97 redef fun is_down
do return buffer
.is_keydown
98 redef fun is_up
do return buffer
.is_keyup
99 redef fun is_arrow_up
do return native
.keysym
.is_up
100 redef fun is_arrow_left
do return native
.keysym
.is_left
101 redef fun is_arrow_down
do return native
.keysym
.is_down
102 redef fun is_arrow_right
do return native
.keysym
.is_right
103 redef fun to_c
do return native
.to_s
.chars
.first
104 redef fun name
do return native
.to_s
.to_lower
107 # Event on pointer, mouse and finger input
108 class GamnitPointerEvent
109 super GamnitInputEvent
112 redef type NATIVE: SDLMouseEvent
114 redef fun x
do return native
.x
.to_f
115 redef fun y
do return native
.y
.to_f
116 redef fun is_move
do return buffer
.is_mouse_motion
121 if native
isa SDLMouseButtonEvent then
122 return native
.pressed
and native
.button
== 1
123 else if native
isa SDLMouseMotionEvent then
124 return native
.state
& 1 == 1
129 # SDL2 event not handled by gamnit
130 class GamnitOtherEvent
131 super GamnitInputEvent