sdl2: intro window events
[nit.git] / lib / sdl2 / events.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # SDL 2 events and related services
18 module events is pkgconfig "sdl2"
19
20 import sdl2_base
21
22 in "C Header" `{
23 #include <SDL2/SDL_events.h>
24 `}
25
26 # Temporary buffer for an SDL 2 event exposing the pseudo-class hierarchy metadata
27 #
28 # An instance of this class should be used to call `poll_event` and `to_event`.
29 extern class SDLEventBuffer `{SDL_Event *`}
30 # Allocate memory for a new `SDLEventBuffer`
31 new malloc `{ return malloc(sizeof(SDL_Event)); `}
32
33 # Poll and event into `self`
34 #
35 # Returns `true` if an event was available.
36 fun poll_event: Bool `{ return SDL_PollEvent(self); `}
37
38 # Get a reference to the data at `self` as a precise `SDLEvent`
39 #
40 # Returns `null` if the event is unknown.
41 #
42 # Note: The returned `SDLEvent` is just a different Nit instance pointing to the same data.
43 # A call to `poll_event` will invalidate any instances returned previously by `to_event`.
44 fun to_event: SDLEvent
45 do
46 if is_quit then return to_quit
47 if is_mouse_motion then return to_mouse_motion
48 if is_mouse_button_down then return to_mouse_button_down
49 if is_mouse_button_up then return to_mouse_button_up
50 if is_mouse_wheel then return to_mouse_wheel
51 if is_keydown then return to_keydown
52 if is_keyup then return to_keyup
53 if is_window then return to_window
54 return to_event_direct
55 end
56
57 private fun to_event_direct: SDLEvent `{ return self; `}
58
59 # Is this a quit event?
60 fun is_quit: Bool `{ return self->type == SDL_QUIT; `}
61
62 # Get a reference to data at `self` as a `SDLQuitEvent`
63 #
64 # Require: `is_quit`
65 fun to_quit: SDLQuitEvent `{ return self; `}
66
67 # Is this a mouse motion event?
68 fun is_mouse_motion: Bool `{ return self->type == SDL_MOUSEMOTION; `}
69
70 # Get a reference to data at `self` as a `SDLMouseMotionEvent`
71 #
72 # Require: `is_mouse_motion`
73 fun to_mouse_motion: SDLMouseMotionEvent `{ return self; `}
74
75 # Is this a mouse button down event?
76 fun is_mouse_button_down: Bool `{ return self->type == SDL_MOUSEBUTTONDOWN; `}
77
78 # Get a reference to data at `self` as a `SDLMouseButtonDownEvent`
79 #
80 # Require: `is_mouse_button_down`
81 fun to_mouse_button_down: SDLMouseButtonDownEvent `{ return self; `}
82
83 # Is this a mouse button up event?
84 fun is_mouse_button_up: Bool `{ return self->type == SDL_MOUSEBUTTONUP; `}
85
86 # Get a reference to data at `self` as a `SDLMouseButtonUpEvent`
87 #
88 # Require: `is_mouse_button_up`
89 fun to_mouse_button_up: SDLMouseButtonUpEvent `{ return self; `}
90
91 # Is this a mouse wheel event?
92 fun is_mouse_wheel: Bool `{ return self->type == SDL_MOUSEWHEEL; `}
93
94 # Get a reference to data at `self` as a `SDLMouseWheelEvent`
95 #
96 # Require: `is_mouse_wheel`
97 fun to_mouse_wheel: SDLMouseWheelEvent `{ return self; `}
98
99 # Is this a key presse event?
100 fun is_keydown: Bool `{ return self->type == SDL_KEYDOWN; `}
101
102 # Get a reference to data at `self` as a `SDLKeyboardDownEvent`
103 #
104 # Require: `is_keydown`
105 fun to_keydown: SDLKeyboardDownEvent `{ return self; `}
106
107 # Is this a key release event?
108 fun is_keyup: Bool `{ return self->type == SDL_KEYUP; `}
109
110 # Get a reference to data at `self` as a `SDLKeyboardUpEvent`
111 #
112 # Require: `is_keyup`
113 fun to_keyup: SDLKeyboardUpEvent `{ return self; `}
114
115 # Is this a window event?
116 fun is_window: Bool `{ return self->type == SDL_WINDOWEVENT; `}
117
118 # Get a reference to data at `self` as a `SDLWindowEvent`
119 #
120 # Require: `is_window`
121 fun to_window: SDLWindowEvent `{ return self; `}
122
123 # TODO other SDL events:
124 #
125 # SDL_CommonEvent common
126 # SDL_WindowEvent window
127 # SDL_TextEditingEvent edit
128 # SDL_TextInputEvent text
129 # SDL_JoyAxisEvent jaxis
130 # SDL_JoyBallEvent jball
131 # SDL_JoyHatEvent jhat;
132 # SDL_JoyButtonEvent jbutton
133 # SDL_JoyDeviceEvent jdevice
134 # SDL_ControllerAxisEvent caxis
135 # SDL_ControllerButtonEvent cbutton
136 # SDL_ControllerDeviceEvent cdevice
137 # SDL_UserEvent user
138 # SDL_SysWMEvent syswm
139 # SDL_TouchFingerEvent tfinger
140 # SDL_MultiGestureEvent mgesture
141 # SDL_DollarGestureEvent dgesture
142 # SDL_DropEvent drop
143 end
144
145 # SDL 2 event, only the data and no metadata
146 extern class SDLEvent `{SDL_Event *`}
147 end
148
149 # Quit event, usually from the close window button
150 extern class SDLQuitEvent
151 super SDLEvent
152 end
153
154 # Mouse event
155 extern class SDLMouseEvent
156 super SDLEvent
157
158 # Implementation note
159 #
160 # Even if the structures are different between the mouse events, the first
161 # four fields of each events are common to all of them.
162
163 # Which mouse, pointer or finger raised this event
164 fun which: Int `{ return self->motion.which; `}
165
166 # X coordinate on screen of this event
167 fun x: Int is abstract
168
169 # Y coordinate on screen of this event
170 fun y: Int is abstract
171 end
172
173 # Mouse motion event
174 extern class SDLMouseMotionEvent
175 super SDLMouseEvent
176
177 redef fun x `{ return self->motion.x; `}
178
179 redef fun y `{ return self->motion.y; `}
180
181 # State of the buttons
182 #
183 # ~~~raw
184 # state & 1 == 1 -> left button down
185 # state & 2 == 2 -> middle button down
186 # state & 4 == 4 -> right button down
187 # ~~~
188 fun state: Int `{ return self->motion.state; `}
189
190 # Difference on the X axis between this event and the previous one
191 fun xrel: Int `{ return self->motion.xrel; `}
192
193 # Difference on the Y axis between this event and the previous one
194 fun yrel: Int `{ return self->motion.yrel; `}
195 end
196
197 # Mouse button event
198 #
199 # This could as well be an abstract class. All instances of `SDLMouseButtonEvent`
200 # is either a `SDLMouseButtonUpEvent` or a `SDLMouseButtonDownEvent`.
201 extern class SDLMouseButtonEvent
202 super SDLMouseEvent
203
204 # Index of the button
205 #
206 # ~~~raw
207 # 1 -> left button
208 # 2 -> center button
209 # 3 -> right button
210 # ~~~
211 fun button: Int `{ return self->button.button; `}
212
213 # Is the button currently pressed down?
214 fun pressed: Bool `{ return self->button.state == SDL_PRESSED; `}
215
216 # Number of clicks (1 or 2)
217 fun clicks: Int `{ return self->button.clicks; `}
218
219 redef fun x `{ return self->button.x; `}
220
221 redef fun y `{ return self->button.y; `}
222 end
223
224 # Mouse button release event
225 extern class SDLMouseButtonUpEvent
226 super SDLMouseButtonEvent
227 end
228
229 # Mouse button click event
230 extern class SDLMouseButtonDownEvent
231 super SDLMouseButtonEvent
232 end
233
234 # Mouse wheel event
235 extern class SDLMouseWheelEvent
236 super SDLEvent
237
238 # Horizontal scroll amount
239 fun x: Int `{ return self->wheel.x; `}
240
241 # Vertical scroll amount
242 fun y: Int `{ return self->wheel.y; `}
243 end
244
245 # Keyboard button event
246 extern class SDLKeyboardEvent
247 super SDLEvent
248
249 # Is this is a key repeat?
250 fun repeat: Bool `{ return self->key.repeat; `}
251
252 # The key that was pressed or released
253 fun keysym: SDLKeysym `{ return &self->key.keysym; `}
254
255 redef fun to_s do return native_to_s.to_s
256 private fun native_to_s: CString `{
257 return (char*)SDL_GetKeyName(self->key.keysym.sym);
258 `}
259 end
260
261 # Keyboard button release event
262 extern class SDLKeyboardUpEvent
263 super SDLKeyboardEvent
264 end
265
266 # Keyboard button click event
267 extern class SDLKeyboardDownEvent
268 super SDLKeyboardEvent
269 end
270
271 # Key information
272 extern class SDLKeysym `{ SDL_Keysym * `}
273 # Is this the arrow right key?
274 fun is_right: Bool `{ return self->sym == SDLK_RIGHT; `}
275
276 # Is this the arrow left key?
277 fun is_left: Bool `{ return self->sym == SDLK_LEFT; `}
278
279 # Is this the arrow down key?
280 fun is_down: Bool `{ return self->sym == SDLK_DOWN; `}
281
282 # Is this the arrow up key?
283 fun is_up: Bool `{ return self->sym == SDLK_UP; `}
284
285 # Modification keys
286 fun mod: Int `{ return self->mod; `}
287 # TODO related masks
288 end
289
290 # Window event
291 extern class SDLWindowEvent
292 super SDLEvent
293
294 # Window identifier
295 fun id: Int `{ return self->window.windowID; `}
296
297 # Is `self` a resized event?
298 fun is_resized: Bool `{ return self->window.event == SDL_WINDOWEVENT_RESIZED; `}
299
300 # Is `self` a size changed event?
301 fun is_size_changed: Bool `{ return self->window.event == SDL_WINDOWEVENT_SIZE_CHANGED; `}
302
303 # `data1` field, depends on the event kind
304 fun data1: Int `{ return self->window.data1; `}
305
306 # `data2` field, depends on the event kind
307 fun data2: Int `{ return self->window.data2; `}
308 end