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
21 intrude import display_linux
24 private var sdl_event_buffer
= new SDLEventBuffer.malloc
29 if display
== null then return
32 var avail
= sdl_event_buffer
.poll_event
33 if not avail
then break
35 # Convert to an SDL event with data
36 var sdl_event
= sdl_event_buffer
.to_event
37 if sdl_event
isa SDLWindowEvent and sdl_event
.is_resized
then
38 display
.width
= sdl_event
.data1
39 display
.height
= sdl_event
.data2
40 display
.aspect_ratio
= sdl_event
.data1
.to_f
/ sdl_event
.data2
.to_f
44 # Convert to `mnit::inputs` conforming objects
45 var gamnit_event
= sdl_event
.to_gamnit_event
(sdl_event_buffer
)
46 accept_event gamnit_event
59 redef class GamnitDisplay
60 redef var aspect_ratio
= super is lazy
64 # Redef services from `sdl2::events`
67 private fun to_gamnit_event
(buffer
: SDLEventBuffer): GamnitInputEvent
68 do return new GamnitOtherEvent(buffer
, self)
71 redef class SDLQuitEvent
72 redef fun to_gamnit_event
(buffer
) do return new GamnitQuitEvent(buffer
, self)
75 redef class SDLMouseEvent
76 redef fun to_gamnit_event
(buffer
) do return new GamnitPointerEvent(buffer
, self)
79 redef class SDLMouseWheelEvent
80 redef fun to_gamnit_event
(buffer
) do return new GamnitMouseWheelEvent(buffer
, self)
83 redef class SDLKeyboardEvent
84 redef fun to_gamnit_event
(buffer
) do return new GamnitKeyEvent(buffer
, self)
88 # Implement `mnit::inputs` interfaces
90 # SDL2 event wrapper implementing the `mnit::input` API
92 # There is two views to the underlying native object: `buffer` and `native`.
93 class GamnitInputEvent
96 # Native SDL 2 event buffer with the pseudo class hierarchy metadata
97 var buffer
: SDLEventBuffer
102 # Type of the `native` underlying SDL 2 event
103 type NATIVE: SDLEvent
106 # Event on user requested quit
107 class GamnitQuitEvent
108 super GamnitInputEvent
111 redef type NATIVE: SDLQuitEvent
114 # Event on keyboard input
116 super GamnitInputEvent
119 redef type NATIVE: SDLKeyboardEvent
121 redef fun is_down
do return buffer
.is_keydown
122 redef fun is_up
do return buffer
.is_keyup
123 redef fun is_arrow_up
do return native
.keysym
.is_up
124 redef fun is_arrow_left
do return native
.keysym
.is_left
125 redef fun is_arrow_down
do return native
.keysym
.is_down
126 redef fun is_arrow_right
do return native
.keysym
.is_right
127 redef fun to_c
do return native
.to_s
.chars
.first
128 redef fun name
do return native
.to_s
.to_lower
131 # Event on pointer, mouse and finger input
132 class GamnitPointerEvent
133 super GamnitInputEvent
136 redef type NATIVE: SDLMouseEvent
138 redef fun x
do return native
.x
.to_f
139 redef fun y
do return native
.y
.to_f
140 redef fun is_move
do return buffer
.is_mouse_motion
145 if native
isa SDLMouseButtonEvent then
146 return native
.pressed
and native
.button
== 1
147 else if native
isa SDLMouseMotionEvent then
148 return native
.state
& 1 == 1
153 # Event on mouse wheel input
154 class GamnitMouseWheelEvent
155 super GamnitInputEvent
157 redef type NATIVE: SDLMouseWheelEvent
159 # Horizontal scroll amount
160 fun x
: Float do return native
.x
.to_f
162 # Vertical scroll amount
163 fun y
: Float do return native
.y
.to_f
166 # SDL2 event not handled by gamnit
167 class GamnitOtherEvent
168 super GamnitInputEvent