fe6cdac82f4ba9aa5b8798c9c6d333686db3913f
[nit.git] / lib / gamnit / gamnit_linux.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Support services for Gamnit on GNU/Linux
16 module gamnit_linux
17
18 import sdl2::events
19
20 intrude import gamnit
21
22 redef class App
23 private var sdl_event_buffer = new SDLEventBuffer.malloc
24
25 redef fun feed_events
26 do
27 var display = display
28 if display == null then return
29
30 loop
31 var avail = sdl_event_buffer.poll_event
32 if not avail then break
33
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
39 end
40 end
41 end
42
43 # ---
44 # Redef services from `sdl2::events`
45
46 redef class SDLEvent
47 private fun to_gamnit_event(buffer: SDLEventBuffer): GamnitInputEvent
48 do return new GamnitOtherEvent(buffer, self)
49 end
50
51 redef class SDLQuitEvent
52 redef fun to_gamnit_event(buffer) do return new GamnitQuitEvent(buffer, self)
53 end
54
55 redef class SDLMouseEvent
56 redef fun to_gamnit_event(buffer) do return new GamnitPointerEvent(buffer, self)
57 end
58
59 redef class SDLKeyboardEvent
60 redef fun to_gamnit_event(buffer) do return new GamnitKeyEvent(buffer, self)
61 end
62
63 # ---
64 # Implement `mnit::inputs` interfaces
65
66 # SDL2 event wrapper implementing the `mnit::input` API
67 #
68 # There is two views to the underlying native object: `buffer` and `native`.
69 class GamnitInputEvent
70 super InputEvent
71
72 # Native SDL 2 event buffer with the pseudo class hierarchy metadata
73 var buffer: SDLEventBuffer
74
75 # Native SDL 2 event
76 var native: NATIVE
77
78 # Type of the `native` underlying SDL 2 event
79 type NATIVE: SDLEvent
80 end
81
82 # Event on user requested quit
83 class GamnitQuitEvent
84 super GamnitInputEvent
85 super QuitEvent
86
87 redef type NATIVE: SDLQuitEvent
88 end
89
90 # Event on keyboard input
91 class GamnitKeyEvent
92 super GamnitInputEvent
93 super KeyEvent
94
95 redef type NATIVE: SDLKeyboardEvent
96
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
105 end
106
107 # Event on pointer, mouse and finger input
108 class GamnitPointerEvent
109 super GamnitInputEvent
110 super PointerEvent
111
112 redef type NATIVE: SDLMouseEvent
113
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
117
118 redef fun pressed
119 do
120 var native = native
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
125 else abort
126 end
127 end
128
129 # SDL2 event not handled by gamnit
130 class GamnitOtherEvent
131 super GamnitInputEvent
132 end