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 if is_window then return to_window
54 return to_event_direct
57 private fun to_event_direct: SDLEvent `{ return self; `}
59 # Is this a quit event?
60 fun is_quit
: Bool `{ return self->type == SDL_QUIT; `}
62 # Get a reference to data at `self` as a `SDLQuitEvent`
65 fun to_quit: SDLQuitEvent `{ return self; `}
67 # Is this a mouse motion event?
68 fun is_mouse_motion
: Bool `{ return self->type == SDL_MOUSEMOTION; `}
70 # Get a reference to data at `self` as a `SDLMouseMotionEvent`
72 # Require: `is_mouse_motion
`
73 fun to_mouse_motion: SDLMouseMotionEvent `{ return self; `}
75 # Is this a mouse button down event?
76 fun is_mouse_button_down
: Bool `{ return self->type == SDL_MOUSEBUTTONDOWN; `}
78 # Get a reference to data at `self` as a `SDLMouseButtonDownEvent`
80 # Require: `is_mouse_button_down
`
81 fun to_mouse_button_down: SDLMouseButtonDownEvent `{ return self; `}
83 # Is this a mouse button up event?
84 fun is_mouse_button_up
: Bool `{ return self->type == SDL_MOUSEBUTTONUP; `}
86 # Get a reference to data at `self` as a `SDLMouseButtonUpEvent`
88 # Require: `is_mouse_button_up
`
89 fun to_mouse_button_up: SDLMouseButtonUpEvent `{ return self; `}
91 # Is this a mouse wheel event?
92 fun is_mouse_wheel
: Bool `{ return self->type == SDL_MOUSEWHEEL; `}
94 # Get a reference to data at `self` as a `SDLMouseWheelEvent`
96 # Require: `is_mouse_wheel
`
97 fun to_mouse_wheel: SDLMouseWheelEvent `{ return self; `}
99 # Is this a key presse event?
100 fun is_keydown
: Bool `{ return self->type == SDL_KEYDOWN; `}
102 # Get a reference to data at `self` as a `SDLKeyboardDownEvent`
104 # Require: `is_keydown
`
105 fun to_keydown: SDLKeyboardDownEvent `{ return self; `}
107 # Is this a key release event?
108 fun is_keyup
: Bool `{ return self->type == SDL_KEYUP; `}
110 # Get a reference to data at `self` as a `SDLKeyboardUpEvent`
112 # Require: `is_keyup
`
113 fun to_keyup: SDLKeyboardUpEvent `{ return self; `}
115 # Is this a window event?
116 fun is_window
: Bool `{ return self->type == SDL_WINDOWEVENT; `}
118 # Get a reference to data at `self` as a `SDLWindowEvent`
120 # Require: `is_window
`
121 fun to_window: SDLWindowEvent `{ return self; `}
123 # TODO other SDL events:
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
138 # SDL_SysWMEvent syswm
139 # SDL_TouchFingerEvent tfinger
140 # SDL_MultiGestureEvent mgesture
141 # SDL_DollarGestureEvent dgesture
145 # SDL 2 event, only the data and no metadata
146 extern class SDLEvent `{SDL_Event *`}
149 # Quit event, usually from the close window button
150 extern class SDLQuitEvent
155 extern class SDLMouseEvent
158 # Implementation note
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.
163 # Which mouse, pointer or finger raised this event
164 fun which: Int `{ return self->motion.which; `}
166 # X coordinate on screen of this event
167 fun x
: Int is abstract
169 # Y coordinate on screen of this event
170 fun y
: Int is abstract
174 extern class SDLMouseMotionEvent
177 redef fun x
`{ return self->motion.x; `}
179 redef fun y `{ return self->motion.y; `}
181 # State of the buttons
184 # state & 1 == 1 -> left button down
185 # state & 2 == 2 -> middle button down
186 # state & 4 == 4 -> right button down
188 fun state
: Int `{ return self->motion.state; `}
190 # Difference on the X axis between this event and the previous one
191 fun xrel: Int `{ return self->motion.xrel; `}
193 # Difference on the Y axis between this event and the previous one
194 fun yrel
: Int `{ return self->motion.yrel; `}
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
204 # Index of the button
211 fun button: Int `{ return self->button.button; `}
213 # Is the button currently pressed down?
214 fun pressed
: Bool `{ return self->button.state == SDL_PRESSED; `}
216 # Number of clicks (1 or 2)
217 fun clicks: Int `{ return self->button.clicks; `}
219 redef fun x
`{ return self->button.x; `}
221 redef fun y `{ return self->button.y; `}
224 # Mouse button release event
225 extern class SDLMouseButtonUpEvent
226 super SDLMouseButtonEvent
229 # Mouse button click event
230 extern class SDLMouseButtonDownEvent
231 super SDLMouseButtonEvent
235 extern class SDLMouseWheelEvent
238 # Horizontal scroll amount
239 fun x
: Int `{ return self->wheel.x; `}
241 # Vertical scroll amount
242 fun y: Int `{ return self->wheel.y; `}
245 # Keyboard button event
246 extern class SDLKeyboardEvent
249 # Is this is a key repeat?
250 fun repeat
: Bool `{ return self->key.repeat; `}
252 # The key that was pressed or released
253 fun keysym: SDLKeysym `{ return &self->key.keysym; `}
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);
261 # Keyboard button release event
262 extern class SDLKeyboardUpEvent
263 super SDLKeyboardEvent
266 # Keyboard button click event
267 extern class SDLKeyboardDownEvent
268 super SDLKeyboardEvent
272 extern class SDLKeysym `{ SDL_Keysym * `}
273 # Is this the arrow right key?
274 fun is_right: Bool `{ return self->sym == SDLK_RIGHT; `}
276 # Is this the arrow left key?
277 fun is_left
: Bool `{ return self->sym == SDLK_LEFT; `}
279 # Is this the arrow down key?
280 fun is_down: Bool `{ return self->sym == SDLK_DOWN; `}
282 # Is this the arrow up key?
283 fun is_up
: Bool `{ return self->sym == SDLK_UP; `}
286 fun mod: Int `{ return self->mod; `}
291 extern class SDLWindowEvent
295 fun id
: Int `{ return self->window.windowID; `}
297 # Is `self` a resized event?
298 fun is_resized: Bool `{ return self->window.event == SDL_WINDOWEVENT_RESIZED; `}
300 # Is `self` a size changed event?
301 fun is_size_changed
: Bool `{ return self->window.event == SDL_WINDOWEVENT_SIZE_CHANGED; `}
303 # `data1
` field, depends on the event kind
304 fun data1: Int `{ return self->window.data1; `}
306 # `data2` field, depends on the event kind
307 fun data2
: Int `{ return self->window.data2; `}