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