e68ee8e44d8f4e7fc63535313c4da263631692d5
[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 SDLMouseWheelEvent
60 redef fun to_gamnit_event(buffer) do return new GamnitMouseWheelEvent(buffer, self)
61 end
62
63 redef class SDLKeyboardEvent
64 redef fun to_gamnit_event(buffer) do return new GamnitKeyEvent(buffer, self)
65 end
66
67 # ---
68 # Implement `mnit::inputs` interfaces
69
70 # SDL2 event wrapper implementing the `mnit::input` API
71 #
72 # There is two views to the underlying native object: `buffer` and `native`.
73 class GamnitInputEvent
74 super InputEvent
75
76 # Native SDL 2 event buffer with the pseudo class hierarchy metadata
77 var buffer: SDLEventBuffer
78
79 # Native SDL 2 event
80 var native: NATIVE
81
82 # Type of the `native` underlying SDL 2 event
83 type NATIVE: SDLEvent
84 end
85
86 # Event on user requested quit
87 class GamnitQuitEvent
88 super GamnitInputEvent
89 super QuitEvent
90
91 redef type NATIVE: SDLQuitEvent
92 end
93
94 # Event on keyboard input
95 class GamnitKeyEvent
96 super GamnitInputEvent
97 super KeyEvent
98
99 redef type NATIVE: SDLKeyboardEvent
100
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
109 end
110
111 # Event on pointer, mouse and finger input
112 class GamnitPointerEvent
113 super GamnitInputEvent
114 super PointerEvent
115
116 redef type NATIVE: SDLMouseEvent
117
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
121
122 redef fun pressed
123 do
124 var native = native
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
129 else abort
130 end
131 end
132
133 # Event on mouse wheel input
134 class GamnitMouseWheelEvent
135 super GamnitInputEvent
136
137 redef type NATIVE: SDLMouseWheelEvent
138
139 # Horizontal scroll amount
140 fun x: Float do return native.x.to_f
141
142 # Vertical scroll amount
143 fun y: Float do return native.y.to_f
144 end
145
146 # SDL2 event not handled by gamnit
147 class GamnitOtherEvent
148 super GamnitInputEvent
149 end