examples: annotate examples
[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 intrude import display_linux
22
23 redef class App
24 private var sdl_event_buffer = new SDLEventBuffer.malloc
25
26 redef fun feed_events
27 do
28 var display = display
29 if display == null then return
30
31 loop
32 var avail = sdl_event_buffer.poll_event
33 if not avail then break
34
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
41 on_resize display
42 end
43
44 # Convert to `mnit::inputs` conforming objects
45 var gamnit_event = sdl_event.to_gamnit_event(sdl_event_buffer)
46 accept_event gamnit_event
47 end
48 end
49
50 redef fun on_create
51 do
52 super
53 create_gamnit
54 create_scene
55 end
56 end
57
58 redef class GamnitDisplay
59 redef var aspect_ratio = super is lazy
60 end
61
62 # ---
63 # Redef services from `sdl2::events`
64
65 redef class SDLEvent
66 private fun to_gamnit_event(buffer: SDLEventBuffer): GamnitInputEvent
67 do return new GamnitOtherEvent(buffer, self)
68 end
69
70 redef class SDLQuitEvent
71 redef fun to_gamnit_event(buffer) do return new GamnitQuitEvent(buffer, self)
72 end
73
74 redef class SDLMouseEvent
75 redef fun to_gamnit_event(buffer) do return new GamnitPointerEvent(buffer, self)
76 end
77
78 redef class SDLMouseWheelEvent
79 redef fun to_gamnit_event(buffer) do return new GamnitMouseWheelEvent(buffer, self)
80 end
81
82 redef class SDLKeyboardEvent
83 redef fun to_gamnit_event(buffer) do return new GamnitKeyEvent(buffer, self)
84 end
85
86 # ---
87 # Implement `mnit::inputs` interfaces
88
89 # SDL2 event wrapper implementing the `mnit::input` API
90 #
91 # There is two views to the underlying native object: `buffer` and `native`.
92 class GamnitInputEvent
93 super InputEvent
94
95 # Native SDL 2 event buffer with the pseudo class hierarchy metadata
96 var buffer: SDLEventBuffer
97
98 # Native SDL 2 event
99 var native: NATIVE
100
101 # Type of the `native` underlying SDL 2 event
102 type NATIVE: SDLEvent
103 end
104
105 # Event on user requested quit
106 class GamnitQuitEvent
107 super GamnitInputEvent
108 super QuitEvent
109
110 redef type NATIVE: SDLQuitEvent
111 end
112
113 # Event on keyboard input
114 class GamnitKeyEvent
115 super GamnitInputEvent
116 super KeyEvent
117
118 redef type NATIVE: SDLKeyboardEvent
119
120 redef fun is_down do return buffer.is_keydown
121 redef fun is_up do return buffer.is_keyup
122 redef fun is_arrow_up do return native.keysym.is_up
123 redef fun is_arrow_left do return native.keysym.is_left
124 redef fun is_arrow_down do return native.keysym.is_down
125 redef fun is_arrow_right do return native.keysym.is_right
126 redef fun to_c do return native.to_s.chars.first
127 redef fun name do return native.to_s.to_lower
128 end
129
130 # Event on pointer, mouse and finger input
131 class GamnitPointerEvent
132 super GamnitInputEvent
133 super PointerEvent
134
135 redef type NATIVE: SDLMouseEvent
136
137 redef fun x do return native.x.to_f
138 redef fun y do return native.y.to_f
139 redef fun is_move do return buffer.is_mouse_motion
140
141 redef fun pressed
142 do
143 var native = native
144 if native isa SDLMouseButtonEvent then
145 return native.pressed and native.button == 1
146 else if native isa SDLMouseMotionEvent then
147 return native.state & 1 == 1
148 else abort
149 end
150 end
151
152 # Event on mouse wheel input
153 class GamnitMouseWheelEvent
154 super GamnitInputEvent
155
156 redef type NATIVE: SDLMouseWheelEvent
157
158 # Horizontal scroll amount
159 fun x: Float do return native.x.to_f
160
161 # Vertical scroll amount
162 fun y: Float do return native.y.to_f
163 end
164
165 # SDL2 event not handled by gamnit
166 class GamnitOtherEvent
167 super GamnitInputEvent
168 end