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 SDLMouseWheelEvent
60 redef fun to_gamnit_event
(buffer
) do return new GamnitMouseWheelEvent(buffer
, self)
63 redef class SDLKeyboardEvent
64 redef fun to_gamnit_event
(buffer
) do return new GamnitKeyEvent(buffer
, self)
68 # Implement `mnit::inputs` interfaces
70 # SDL2 event wrapper implementing the `mnit::input` API
72 # There is two views to the underlying native object: `buffer` and `native`.
73 class GamnitInputEvent
76 # Native SDL 2 event buffer with the pseudo class hierarchy metadata
77 var buffer
: SDLEventBuffer
82 # Type of the `native` underlying SDL 2 event
86 # Event on user requested quit
88 super GamnitInputEvent
91 redef type NATIVE: SDLQuitEvent
94 # Event on keyboard input
96 super GamnitInputEvent
99 redef type NATIVE: SDLKeyboardEvent
101 redef fun is_down
do return buffer
.is_keydown
102 redef fun is_up
do return buffer
.is_keyup
103 redef fun is_arrow_up
do return native
.keysym
.is_up
104 redef fun is_arrow_left
do return native
.keysym
.is_left
105 redef fun is_arrow_down
do return native
.keysym
.is_down
106 redef fun is_arrow_right
do return native
.keysym
.is_right
107 redef fun to_c
do return native
.to_s
.chars
.first
108 redef fun name
do return native
.to_s
.to_lower
111 # Event on pointer, mouse and finger input
112 class GamnitPointerEvent
113 super GamnitInputEvent
116 redef type NATIVE: SDLMouseEvent
118 redef fun x
do return native
.x
.to_f
119 redef fun y
do return native
.y
.to_f
120 redef fun is_move
do return buffer
.is_mouse_motion
125 if native
isa SDLMouseButtonEvent then
126 return native
.pressed
and native
.button
== 1
127 else if native
isa SDLMouseMotionEvent then
128 return native
.state
& 1 == 1
133 # Event on mouse wheel input
134 class GamnitMouseWheelEvent
135 super GamnitInputEvent
137 redef type NATIVE: SDLMouseWheelEvent
139 # Horizontal scroll amount
140 fun x
: Float do return native
.x
.to_f
142 # Vertical scroll amount
143 fun y
: Float do return native
.y
.to_f
146 # SDL2 event not handled by gamnit
147 class GamnitOtherEvent
148 super GamnitInputEvent