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 # A temporary buffer for a SDL 2 event
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(recv); `}
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
`.
45 # TODO remove `nullable` from the return type once all cases are correctly handled.
46 fun to_event: nullable SDLEvent
48 if is_quit then return to_quit
49 if is_mouse_motion then return to_mouse_motion
50 if is_mouse_button_down then return to_mouse_button_down
51 if is_mouse_button_up then return to_mouse_button_up
55 # Is this a quit event?
56 fun is_quit: Bool `{ return recv->type == SDL_QUIT; `}
58 # Get a reference to data at `self` as a `SDLQuitEvent`
59 fun to_quit
: SDLQuitEvent `{ return recv; `}
61 # Is this a mouse motion event?
62 fun is_mouse_motion: Bool `{ return recv->type == SDL_MOUSEMOTION; `}
64 # Get a reference to data at `self` as a `SDLMouseMotionEvent`
65 fun to_mouse_motion
: SDLMouseMotionEvent `{ return recv; `}
67 # Is this a mouse button down event?
68 fun is_mouse_button_down: Bool `{ return recv->type == SDL_MOUSEBUTTONDOWN; `}
70 # Get a reference to data at `self` as a `SDLMouseButtonDownEvent`
71 fun to_mouse_button_down
: SDLMouseButtonDownEvent `{ return recv; `}
73 # Is this a mouse button up event?
74 fun is_mouse_button_up: Bool `{ return recv->type == SDL_MOUSEBUTTONUP; `}
76 # Get a reference to data at `self` as a `SDLMouseButtonUpEvent`
77 fun to_mouse_button_up
: SDLMouseButtonUpEvent `{ return recv; `}
79 # TODO other SDL events:
81 # SDL_CommonEvent common
82 # SDL_WindowEvent window
83 # SDL_KeyboardEvent key
84 # SDL_TextEditingEvent edit
85 # SDL_TextInputEvent text
86 # SDL_MouseWheelEvent wheel
87 # SDL_JoyAxisEvent jaxis
88 # SDL_JoyBallEvent jball
89 # SDL_JoyHatEvent jhat;
90 # SDL_JoyButtonEvent jbutton
91 # SDL_JoyDeviceEvent jdevice
92 # SDL_ControllerAxisEvent caxis
93 # SDL_ControllerButtonEvent cbutton
94 # SDL_ControllerDeviceEvent cdevice
97 # SDL_SysWMEvent syswm
98 # SDL_TouchFingerEvent tfinger
99 # SDL_MultiGestureEvent mgesture
100 # SDL_DollarGestureEvent dgesture
104 # An event from SDL 2
105 extern class SDLEvent `{SDL_Event *`}
108 # A quit event, usually from the close window button
109 extern class SDLQuitEvent
114 extern class SDLMouseEvent
117 # Implementation note
119 # Even if the structures are different between the mouse events, the first
120 # four fields of each events are common to all of them.
122 # Which mouse, pointer or finger raised this event
123 fun which
: Int `{ return recv->motion.which; `}
126 # A mouse motion event
127 extern class SDLMouseMotionEvent
130 # X coordinate on screen of this event
131 fun x: Int `{ return recv->motion.x; `}
133 # Y coordinate on screen of this event
134 fun y
: Int `{ return recv->motion.y; `}
136 # Difference on the X axis between this event and the previous one
137 fun xrel: Int `{ return recv->motion.xrel; `}
139 # Difference on the Y axis between this event and the previous one
140 fun yrel
: Int `{ return recv->motion.yrel; `}
143 # A mouse button event
145 # This could as well be an abstract class. All instances of `SDLMouseButtonEvent`
146 # is either a `SDLMouseButtonUpEvent` or a `SDLMouseButtonDownEvent`.
147 extern class SDLMouseButtonEvent
150 # X coordinate on screen of this event
151 fun x: Int `{ return recv->button.x; `}
153 # Y coordinate on screen of this event
154 fun y
: Int `{ return recv->button.y; `}
157 # A mouse button release event
158 extern class SDLMouseButtonUpEvent
159 super SDLMouseButtonEvent
162 # A mouse button click event
163 extern class SDLMouseButtonDownEvent
164 super SDLMouseButtonEvent