8eb14f21150597362b685ce817d072f2116af10e
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_keydown then return to_keydown
51 if is_keyup then return to_keyup
52 return to_event_direct
55 private fun to_event_direct: SDLEvent `{ return self; `}
57 # Is this a quit event?
58 fun is_quit
: Bool `{ return self->type == SDL_QUIT; `}
60 # Get a reference to data at `self` as a `SDLQuitEvent`
63 fun to_quit: SDLQuitEvent `{ return self; `}
65 # Is this a mouse motion event?
66 fun is_mouse_motion
: Bool `{ return self->type == SDL_MOUSEMOTION; `}
68 # Get a reference to data at `self` as a `SDLMouseMotionEvent`
70 # Require: `is_mouse_motion
`
71 fun to_mouse_motion: SDLMouseMotionEvent `{ return self; `}
73 # Is this a mouse button down event?
74 fun is_mouse_button_down
: Bool `{ return self->type == SDL_MOUSEBUTTONDOWN; `}
76 # Get a reference to data at `self` as a `SDLMouseButtonDownEvent`
78 # Require: `is_mouse_button_down
`
79 fun to_mouse_button_down: SDLMouseButtonDownEvent `{ return self; `}
81 # Is this a mouse button up event?
82 fun is_mouse_button_up
: Bool `{ return self->type == SDL_MOUSEBUTTONUP; `}
84 # Get a reference to data at `self` as a `SDLMouseButtonUpEvent`
86 # Require: `is_mouse_button_up
`
87 fun to_mouse_button_up: SDLMouseButtonUpEvent `{ return self; `}
89 # Is this a key presse event?
90 fun is_keydown
: Bool `{ return self->type == SDL_KEYDOWN; `}
92 # Get a reference to data at `self` as a `SDLKeyboardDownEvent`
94 # Require: `is_keydown
`
95 fun to_keydown: SDLKeyboardDownEvent `{ return self; `}
97 # Is this a key release event?
98 fun is_keyup
: Bool `{ return self->type == SDL_KEYUP; `}
100 # Get a reference to data at `self` as a `SDLKeyboardUpEvent`
102 # Require: `is_keyup
`
103 fun to_keyup: SDLKeyboardUpEvent `{ return self; `}
105 # TODO other SDL events:
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
121 # SDL_SysWMEvent syswm
122 # SDL_TouchFingerEvent tfinger
123 # SDL_MultiGestureEvent mgesture
124 # SDL_DollarGestureEvent dgesture
128 # SDL 2 event, only the data and no metadata
129 extern class SDLEvent `{SDL_Event *`}
132 # Quit event, usually from the close window button
133 extern class SDLQuitEvent
138 extern class SDLMouseEvent
141 # Implementation note
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.
146 # Which mouse, pointer or finger raised this event
147 fun which: Int `{ return self->motion.which; `}
149 # X coordinate on screen of this event
150 fun x
: Int is abstract
152 # Y coordinate on screen of this event
153 fun y
: Int is abstract
157 extern class SDLMouseMotionEvent
160 redef fun x
`{ return self->motion.x; `}
162 redef fun y `{ return self->motion.y; `}
164 # State of the buttons
167 # state & 1 == 1 -> left button down
168 # state & 2 == 2 -> middle button down
169 # state & 4 == 4 -> right button down
171 fun state
: Int `{ return self->motion.state; `}
173 # Difference on the X axis between this event and the previous one
174 fun xrel: Int `{ return self->motion.xrel; `}
176 # Difference on the Y axis between this event and the previous one
177 fun yrel
: Int `{ return self->motion.yrel; `}
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
187 # Index of the button
194 fun button: Int `{ return self->button.button; `}
196 # Is the button currently pressed down?
197 fun pressed
: Bool `{ return self->button.state == SDL_PRESSED; `}
199 # Number of clicks (1 or 2)
200 fun clicks: Int `{ return self->button.clicks; `}
202 redef fun x
`{ return self->button.x; `}
204 redef fun y `{ return self->button.y; `}
207 # Mouse button release event
208 extern class SDLMouseButtonUpEvent
209 super SDLMouseButtonEvent
212 # Mouse button click event
213 extern class SDLMouseButtonDownEvent
214 super SDLMouseButtonEvent
217 # Keyboard button event
218 extern class SDLKeyboardEvent
221 # Is this is a key repeat?
222 fun repeat
: Bool `{ return self->key.repeat; `}
224 # The key that was pressed or released
225 fun keysym: SDLKeysym `{ return &self->key.keysym; `}
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);
233 # Keyboard button release event
234 extern class SDLKeyboardUpEvent
235 super SDLKeyboardEvent
238 # Keyboard button click event
239 extern class SDLKeyboardDownEvent
240 super SDLKeyboardEvent
244 extern class SDLKeysym `{ SDL_Keysym * `}
245 # Is this the arrow right key?
246 fun is_right: Bool `{ return self->sym == SDLK_RIGHT; `}
248 # Is this the arrow left key?
249 fun is_left
: Bool `{ return self->sym == SDLK_LEFT; `}
251 # Is this the arrow down key?
252 fun is_down: Bool `{ return self->sym == SDLK_DOWN; `}
254 # Is this the arrow up key?
255 fun is_up
: Bool `{ return self->sym == SDLK_UP; `}
258 fun mod: Int `{ return self->mod; `}