8eb14f21150597362b685ce817d072f2116af10e
[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_keydown then return to_keydown
51 if is_keyup then return to_keyup
52 return to_event_direct
53 end
54
55 private fun to_event_direct: SDLEvent `{ return self; `}
56
57 # Is this a quit event?
58 fun is_quit: Bool `{ return self->type == SDL_QUIT; `}
59
60 # Get a reference to data at `self` as a `SDLQuitEvent`
61 #
62 # Require: `is_quit`
63 fun to_quit: SDLQuitEvent `{ return self; `}
64
65 # Is this a mouse motion event?
66 fun is_mouse_motion: Bool `{ return self->type == SDL_MOUSEMOTION; `}
67
68 # Get a reference to data at `self` as a `SDLMouseMotionEvent`
69 #
70 # Require: `is_mouse_motion`
71 fun to_mouse_motion: SDLMouseMotionEvent `{ return self; `}
72
73 # Is this a mouse button down event?
74 fun is_mouse_button_down: Bool `{ return self->type == SDL_MOUSEBUTTONDOWN; `}
75
76 # Get a reference to data at `self` as a `SDLMouseButtonDownEvent`
77 #
78 # Require: `is_mouse_button_down`
79 fun to_mouse_button_down: SDLMouseButtonDownEvent `{ return self; `}
80
81 # Is this a mouse button up event?
82 fun is_mouse_button_up: Bool `{ return self->type == SDL_MOUSEBUTTONUP; `}
83
84 # Get a reference to data at `self` as a `SDLMouseButtonUpEvent`
85 #
86 # Require: `is_mouse_button_up`
87 fun to_mouse_button_up: SDLMouseButtonUpEvent `{ return self; `}
88
89 # Is this a key presse event?
90 fun is_keydown: Bool `{ return self->type == SDL_KEYDOWN; `}
91
92 # Get a reference to data at `self` as a `SDLKeyboardDownEvent`
93 #
94 # Require: `is_keydown`
95 fun to_keydown: SDLKeyboardDownEvent `{ return self; `}
96
97 # Is this a key release event?
98 fun is_keyup: Bool `{ return self->type == SDL_KEYUP; `}
99
100 # Get a reference to data at `self` as a `SDLKeyboardUpEvent`
101 #
102 # Require: `is_keyup`
103 fun to_keyup: SDLKeyboardUpEvent `{ return self; `}
104
105 # TODO other SDL events:
106 #
107 # SDL_CommonEvent common
108 # SDL_WindowEvent window
109 # SDL_TextEditingEvent edit
110 # SDL_TextInputEvent text
111 # SDL_MouseWheelEvent wheel
112 # SDL_JoyAxisEvent jaxis
113 # SDL_JoyBallEvent jball
114 # SDL_JoyHatEvent jhat;
115 # SDL_JoyButtonEvent jbutton
116 # SDL_JoyDeviceEvent jdevice
117 # SDL_ControllerAxisEvent caxis
118 # SDL_ControllerButtonEvent cbutton
119 # SDL_ControllerDeviceEvent cdevice
120 # SDL_UserEvent user
121 # SDL_SysWMEvent syswm
122 # SDL_TouchFingerEvent tfinger
123 # SDL_MultiGestureEvent mgesture
124 # SDL_DollarGestureEvent dgesture
125 # SDL_DropEvent drop
126 end
127
128 # SDL 2 event, only the data and no metadata
129 extern class SDLEvent `{SDL_Event *`}
130 end
131
132 # Quit event, usually from the close window button
133 extern class SDLQuitEvent
134 super SDLEvent
135 end
136
137 # Mouse event
138 extern class SDLMouseEvent
139 super SDLEvent
140
141 # Implementation note
142 #
143 # Even if the structures are different between the mouse events, the first
144 # four fields of each events are common to all of them.
145
146 # Which mouse, pointer or finger raised this event
147 fun which: Int `{ return self->motion.which; `}
148
149 # X coordinate on screen of this event
150 fun x: Int is abstract
151
152 # Y coordinate on screen of this event
153 fun y: Int is abstract
154 end
155
156 # Mouse motion event
157 extern class SDLMouseMotionEvent
158 super SDLMouseEvent
159
160 redef fun x `{ return self->motion.x; `}
161
162 redef fun y `{ return self->motion.y; `}
163
164 # State of the buttons
165 #
166 # ~~~raw
167 # state & 1 == 1 -> left button down
168 # state & 2 == 2 -> middle button down
169 # state & 4 == 4 -> right button down
170 # ~~~
171 fun state: Int `{ return self->motion.state; `}
172
173 # Difference on the X axis between this event and the previous one
174 fun xrel: Int `{ return self->motion.xrel; `}
175
176 # Difference on the Y axis between this event and the previous one
177 fun yrel: Int `{ return self->motion.yrel; `}
178 end
179
180 # Mouse button event
181 #
182 # This could as well be an abstract class. All instances of `SDLMouseButtonEvent`
183 # is either a `SDLMouseButtonUpEvent` or a `SDLMouseButtonDownEvent`.
184 extern class SDLMouseButtonEvent
185 super SDLMouseEvent
186
187 # Index of the button
188 #
189 # ~~~raw
190 # 1 -> left button
191 # 2 -> center button
192 # 3 -> right button
193 # ~~~
194 fun button: Int `{ return self->button.button; `}
195
196 # Is the button currently pressed down?
197 fun pressed: Bool `{ return self->button.state == SDL_PRESSED; `}
198
199 # Number of clicks (1 or 2)
200 fun clicks: Int `{ return self->button.clicks; `}
201
202 redef fun x `{ return self->button.x; `}
203
204 redef fun y `{ return self->button.y; `}
205 end
206
207 # Mouse button release event
208 extern class SDLMouseButtonUpEvent
209 super SDLMouseButtonEvent
210 end
211
212 # Mouse button click event
213 extern class SDLMouseButtonDownEvent
214 super SDLMouseButtonEvent
215 end
216
217 # Keyboard button event
218 extern class SDLKeyboardEvent
219 super SDLEvent
220
221 # Is this is a key repeat?
222 fun repeat: Bool `{ return self->key.repeat; `}
223
224 # The key that was pressed or released
225 fun keysym: SDLKeysym `{ return &self->key.keysym; `}
226
227 redef fun to_s do return native_to_s.to_s
228 private fun native_to_s: CString `{
229 return (char*)SDL_GetKeyName(self->key.keysym.sym);
230 `}
231 end
232
233 # Keyboard button release event
234 extern class SDLKeyboardUpEvent
235 super SDLKeyboardEvent
236 end
237
238 # Keyboard button click event
239 extern class SDLKeyboardDownEvent
240 super SDLKeyboardEvent
241 end
242
243 # Key information
244 extern class SDLKeysym `{ SDL_Keysym * `}
245 # Is this the arrow right key?
246 fun is_right: Bool `{ return self->sym == SDLK_RIGHT; `}
247
248 # Is this the arrow left key?
249 fun is_left: Bool `{ return self->sym == SDLK_LEFT; `}
250
251 # Is this the arrow down key?
252 fun is_down: Bool `{ return self->sym == SDLK_DOWN; `}
253
254 # Is this the arrow up key?
255 fun is_up: Bool `{ return self->sym == SDLK_UP; `}
256
257 # Modification keys
258 fun mod: Int `{ return self->mod; `}
259 # TODO related masks
260 end