1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # SDL 2 events and related services
18 module events
is pkgconfig
"sdl2"
23 #include <SDL2/SDL_events.h>
26 # Temporary buffer for an SDL 2 event exposing the pseudo-class hierarchy metadata
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)); `}
33 # Poll and event into `self`
35 # Returns `true` if an event was available.
36 fun poll_event
: Bool `{ return SDL_PollEvent(self); `}
38 # Get a reference to the data at `self` as a precise `SDLEvent`
40 # Returns `null` if the event is unknown.
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
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
56 private fun to_event_direct: SDLEvent `{ return self; `}
58 # Is this a quit event?
59 fun is_quit
: Bool `{ return self->type == SDL_QUIT; `}
61 # Get a reference to data at `self` as a `SDLQuitEvent`
64 fun to_quit: SDLQuitEvent `{ return self; `}
66 # Is this a mouse motion event?
67 fun is_mouse_motion
: Bool `{ return self->type == SDL_MOUSEMOTION; `}
69 # Get a reference to data at `self` as a `SDLMouseMotionEvent`
71 # Require: `is_mouse_motion
`
72 fun to_mouse_motion: SDLMouseMotionEvent `{ return self; `}
74 # Is this a mouse button down event?
75 fun is_mouse_button_down
: Bool `{ return self->type == SDL_MOUSEBUTTONDOWN; `}
77 # Get a reference to data at `self` as a `SDLMouseButtonDownEvent`
79 # Require: `is_mouse_button_down
`
80 fun to_mouse_button_down: SDLMouseButtonDownEvent `{ return self; `}
82 # Is this a mouse button up event?
83 fun is_mouse_button_up
: Bool `{ return self->type == SDL_MOUSEBUTTONUP; `}
85 # Get a reference to data at `self` as a `SDLMouseButtonUpEvent`
87 # Require: `is_mouse_button_up
`
88 fun to_mouse_button_up: SDLMouseButtonUpEvent `{ return self; `}
90 # Is this a mouse wheel event?
91 fun is_mouse_wheel
: Bool `{ return self->type == SDL_MOUSEWHEEL; `}
93 # Get a reference to data at `self` as a `SDLMouseWheelEvent`
95 # Require: `is_mouse_wheel
`
96 fun to_mouse_wheel: SDLMouseWheelEvent `{ return self; `}
98 # Is this a key presse event?
99 fun is_keydown
: Bool `{ return self->type == SDL_KEYDOWN; `}
101 # Get a reference to data at `self` as a `SDLKeyboardDownEvent`
103 # Require: `is_keydown
`
104 fun to_keydown: SDLKeyboardDownEvent `{ return self; `}
106 # Is this a key release event?
107 fun is_keyup
: Bool `{ return self->type == SDL_KEYUP; `}
109 # Get a reference to data at `self` as a `SDLKeyboardUpEvent`
111 # Require: `is_keyup
`
112 fun to_keyup: SDLKeyboardUpEvent `{ return self; `}
114 # TODO other SDL events:
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
129 # SDL_SysWMEvent syswm
130 # SDL_TouchFingerEvent tfinger
131 # SDL_MultiGestureEvent mgesture
132 # SDL_DollarGestureEvent dgesture
136 # SDL 2 event, only the data and no metadata
137 extern class SDLEvent `{SDL_Event *`}
140 # Quit event, usually from the close window button
141 extern class SDLQuitEvent
146 extern class SDLMouseEvent
149 # Implementation note
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.
154 # Which mouse, pointer or finger raised this event
155 fun which: Int `{ return self->motion.which; `}
157 # X coordinate on screen of this event
158 fun x
: Int is abstract
160 # Y coordinate on screen of this event
161 fun y
: Int is abstract
165 extern class SDLMouseMotionEvent
168 redef fun x
`{ return self->motion.x; `}
170 redef fun y `{ return self->motion.y; `}
172 # State of the buttons
175 # state & 1 == 1 -> left button down
176 # state & 2 == 2 -> middle button down
177 # state & 4 == 4 -> right button down
179 fun state
: Int `{ return self->motion.state; `}
181 # Difference on the X axis between this event and the previous one
182 fun xrel: Int `{ return self->motion.xrel; `}
184 # Difference on the Y axis between this event and the previous one
185 fun yrel
: Int `{ return self->motion.yrel; `}
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
195 # Index of the button
202 fun button: Int `{ return self->button.button; `}
204 # Is the button currently pressed down?
205 fun pressed
: Bool `{ return self->button.state == SDL_PRESSED; `}
207 # Number of clicks (1 or 2)
208 fun clicks: Int `{ return self->button.clicks; `}
210 redef fun x
`{ return self->button.x; `}
212 redef fun y `{ return self->button.y; `}
215 # Mouse button release event
216 extern class SDLMouseButtonUpEvent
217 super SDLMouseButtonEvent
220 # Mouse button click event
221 extern class SDLMouseButtonDownEvent
222 super SDLMouseButtonEvent
226 extern class SDLMouseWheelEvent
229 # Horizontal scroll amount
230 fun x
: Int `{ return self->wheel.x; `}
232 # Vertical scroll amount
233 fun y: Int `{ return self->wheel.y; `}
236 # Keyboard button event
237 extern class SDLKeyboardEvent
240 # Is this is a key repeat?
241 fun repeat
: Bool `{ return self->key.repeat; `}
243 # The key that was pressed or released
244 fun keysym: SDLKeysym `{ return &self->key.keysym; `}
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);
252 # Keyboard button release event
253 extern class SDLKeyboardUpEvent
254 super SDLKeyboardEvent
257 # Keyboard button click event
258 extern class SDLKeyboardDownEvent
259 super SDLKeyboardEvent
263 extern class SDLKeysym `{ SDL_Keysym * `}
264 # Is this the arrow right key?
265 fun is_right: Bool `{ return self->sym == SDLK_RIGHT; `}
267 # Is this the arrow left key?
268 fun is_left
: Bool `{ return self->sym == SDLK_LEFT; `}
270 # Is this the arrow down key?
271 fun is_down: Bool `{ return self->sym == SDLK_DOWN; `}
273 # Is this the arrow up key?
274 fun is_up
: Bool `{ return self->sym == SDLK_UP; `}
277 fun mod: Int `{ return self->mod; `}