projects: update some short descriptions
[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 # A temporary buffer for a SDL 2 event
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 #
45 # TODO remove `nullable` from the return type once all cases are correctly handled.
46 fun to_event: nullable SDLEvent
47 do
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
52 return null
53 end
54
55 # Is this a quit event?
56 fun is_quit: Bool `{ return self->type == SDL_QUIT; `}
57
58 # Get a reference to data at `self` as a `SDLQuitEvent`
59 fun to_quit: SDLQuitEvent `{ return self; `}
60
61 # Is this a mouse motion event?
62 fun is_mouse_motion: Bool `{ return self->type == SDL_MOUSEMOTION; `}
63
64 # Get a reference to data at `self` as a `SDLMouseMotionEvent`
65 fun to_mouse_motion: SDLMouseMotionEvent `{ return self; `}
66
67 # Is this a mouse button down event?
68 fun is_mouse_button_down: Bool `{ return self->type == SDL_MOUSEBUTTONDOWN; `}
69
70 # Get a reference to data at `self` as a `SDLMouseButtonDownEvent`
71 fun to_mouse_button_down: SDLMouseButtonDownEvent `{ return self; `}
72
73 # Is this a mouse button up event?
74 fun is_mouse_button_up: Bool `{ return self->type == SDL_MOUSEBUTTONUP; `}
75
76 # Get a reference to data at `self` as a `SDLMouseButtonUpEvent`
77 fun to_mouse_button_up: SDLMouseButtonUpEvent `{ return self; `}
78
79 # TODO other SDL events:
80 #
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
95 # SDL_QuitEvent quit
96 # SDL_UserEvent user
97 # SDL_SysWMEvent syswm
98 # SDL_TouchFingerEvent tfinger
99 # SDL_MultiGestureEvent mgesture
100 # SDL_DollarGestureEvent dgesture
101 # SDL_DropEvent drop
102 end
103
104 # An event from SDL 2
105 extern class SDLEvent `{SDL_Event *`}
106 end
107
108 # A quit event, usually from the close window button
109 extern class SDLQuitEvent
110 super SDLEvent
111 end
112
113 # A mouse event
114 extern class SDLMouseEvent
115 super SDLEvent
116
117 # Implementation note
118 #
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.
121
122 # Which mouse, pointer or finger raised this event
123 fun which: Int `{ return self->motion.which; `}
124 end
125
126 # A mouse motion event
127 extern class SDLMouseMotionEvent
128 super SDLMouseEvent
129
130 # X coordinate on screen of this event
131 fun x: Int `{ return self->motion.x; `}
132
133 # Y coordinate on screen of this event
134 fun y: Int `{ return self->motion.y; `}
135
136 # Difference on the X axis between this event and the previous one
137 fun xrel: Int `{ return self->motion.xrel; `}
138
139 # Difference on the Y axis between this event and the previous one
140 fun yrel: Int `{ return self->motion.yrel; `}
141 end
142
143 # A mouse button event
144 #
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
148 super SDLMouseEvent
149
150 # X coordinate on screen of this event
151 fun x: Int `{ return self->button.x; `}
152
153 # Y coordinate on screen of this event
154 fun y: Int `{ return self->button.y; `}
155 end
156
157 # A mouse button release event
158 extern class SDLMouseButtonUpEvent
159 super SDLMouseButtonEvent
160 end
161
162 # A mouse button click event
163 extern class SDLMouseButtonDownEvent
164 super SDLMouseButtonEvent
165 end