added SDL_PeepEvents support
[sdlgit/SDL_perl.git] / lib / SDL / Event.pm
1 #       Event.pm
2 #
3 #       A package for handling SDL_Event *
4 #
5 #       Copyright (C) 2000,2001,2002 David J. Goehrig
6 #       Copyright (C) 2009 Kartik Thakore
7 #       see the file COPYING for terms of use
8 #
9
10 package SDL::Event;
11 use strict;
12 use SDL;
13
14 sub new {
15         my $proto = shift;
16         my $class = ref($proto) || $proto;
17         my $self;
18         $self = \SDL::NewEvent();
19         bless $self, $class;
20         return $self;
21 }
22
23 sub DESTROY {
24         my $self = shift;
25         SDL::FreeEvent($$self);
26 }
27
28 sub type {
29         my $self = shift;
30         if (@_) {
31                 SDL::SetEventType($$self,$_[0]);
32         }
33         return SDL::EventType($$self);
34 }
35
36 sub pump {
37         SDL::PumpEvents();
38 }
39
40 sub poll {
41         my $self = shift;
42         return SDL::PollEvent($$self);
43 }
44
45 sub peep($$$$)
46 {
47         my ($event, $numEvents, $action, $mask) = @_;
48         return SDL::PeepEvents($$event, $numEvents, $action, $mask);
49 }
50
51 sub push {
52         my $self = shift;
53         return SDL::PushEvent($$self);
54 }
55
56 sub wait {
57         my $self = shift;
58         return SDL::WaitEvent($$self);
59 }
60
61 sub set { 
62         my $self = shift;
63         my $state = shift;
64         return SDL::EventState($self->type(),$state);
65 }
66
67 sub set_unicode {
68         my $self = shift;
69         my $toggle = shift;
70         return SDL::EnableUnicode($toggle);
71 }
72
73 sub set_key_repeat {
74         my $self = shift;
75         my $delay = shift;
76         my $interval = shift;
77         return SDL::EnableKeyRepeat($delay,$interval);
78 }
79
80 sub active_gain {
81         my $self = shift;
82         return SDL::ActiveEventGain($$self);
83 }
84
85 sub active_state {
86         my $self = shift;
87         return SDL::ActiveEventState($$self);
88 }
89
90 sub key_state {
91         my $self = shift;
92         return SDL::KeyEventState($$self);
93 }
94
95 sub key_sym {
96         my $self = shift;
97         return SDL::KeyEventSym($$self);
98 }
99
100 sub key_name {
101         my $self = shift;
102         return SDL::GetKeyName(SDL::KeyEventSym($$self));
103 }
104
105 sub key_mod {
106         my $self = shift;
107         return SDL::KeyEventMod($$self);
108 }
109
110 sub key_unicode {
111         my $self = shift;
112         return SDL::KeyEventUnicode($$self);
113 }
114
115 sub key_scancode {
116         my $self = shift;
117         return SDL::KeyEventScanCode($$self);
118 }
119
120 sub motion_state {
121         my $self = shift;
122         return SDL::MouseMotionState($$self);
123 }
124
125 sub motion_x {
126         my $self = shift;
127         return SDL::MouseMotionX($$self);
128 }
129
130 sub motion_y {
131         my $self = shift;
132         return SDL::MouseMotionY($$self);
133 }
134
135 sub motion_xrel {
136         my $self = shift;
137         return SDL::MouseMotionXrel($$self);
138 }
139
140 sub motion_yrel {
141         my $self = shift;
142         return SDL::MouseMotionYrel($$self);
143 }
144
145 sub button_state {
146         my $self = shift;
147         return SDL::MouseButtonState($$self);
148 }
149
150 sub button_x {
151         my $self = shift;
152         return SDL::MouseButtonX($$self);
153 }
154
155 sub button_y {
156         my $self = shift;
157         return SDL::MouseButtonY($$self);
158 }
159
160 sub button {
161         my $self = shift;
162         return SDL::MouseButton($$self);
163 }
164
165 sub resize_w {
166         my $self = shift;
167         SDL::ResizeEventW($$self);
168 }
169
170 sub resize_h {
171         my $self = shift;
172         SDL::ResizeEventH($$self);
173 }
174
175 1;
176
177 __END__;
178
179 =pod
180
181 =head1 NAME
182
183 SDL::Event - a SDL perl extension
184
185 =head1 SYNOPSIS
186
187  use SDL::Event;
188  my $event = new SDL::Event;             # create a new event
189  while ($event->wait()) {
190         my $type = $event->type();      # get event type
191         # ... handle event
192         exit if $type == SDL_QUIT;
193  }
194  
195 =head1 DESCRIPTION
196
197 C<SDL::Event> offers an object-oriented approach to SDL events. By creating
198 an instance of SDL::Event via new() you can wait for events, and then determine
199 the type of the event and take an appropriate action.
200
201 =head1 EXAMPLE
202
203 Here is an example of a simple event handler loop routine.
204 See also L<SDL::App::loop>.
205
206        sub loop {
207                my ($self,$href) = @_;
208                my $event = new SDL::Event;
209                while ( $event->wait() ) {
210                        # ... insert here your event handling like:
211                        if ( ref($$href{$event->type()}) eq "CODE" ) {
212                                &{$$href{$event->type()}}($event);
213                                $self->sync();
214                        }
215                }
216        }
217
218 =head1 METHODS
219
220 =head2 new()
221
222 Create a new event object.
223
224 =head2 type()
225
226 Returns the type of the event, see list of exported symbols for which are
227 available.
228
229 =head2 pump()
230
231 Pumps the event loop, gathering events from the input devices.
232
233 =head2 poll()
234
235 Polls for currently pending events
236
237 =head2 peep()
238
239 Checks the event queue for messages and optionally returns them.
240
241 If action is SDL_ADDEVENT, up to numevents events will be added to the back of the event queue.
242
243 If action is SDL_PEEKEVENT, up to numevents events at the front of the event queue, matching mask, will be returned and will not be removed from the queue.
244
245 If action is SDL_GETEVENT, up to numevents events at the front of the event queue, matching mask, will be returned and will be removed from the queue.
246
247 The mask parameter is a bitwise OR of SDL_EVENTMASK(event_type), for all event types you are interested in
248
249
250 =head2 wait()
251
252 Waits for an event end returns then. Always returns true.
253
254 =head2 set( type, state )
255
256 Set the state for all events of the given event's type
257
258 =head2 set_unicode( toggle )
259
260 Toggle unicode on the event.
261
262 =head2 set_key_repeat( delay, interval)
263
264 Sets the delay and intervall of the key repeat rate (e.g. when a user
265 holds down a key on the keyboard).
266
267 =head2 active_gain()
268
269 =head2 active_state()
270
271 =head2 key_state()
272
273 =head2 key_sym()
274
275 =head2 key_name()
276
277 =head2 key_mod()
278
279 =head2 key_unicode()
280
281 =head2 key_scancode()
282
283 =head2 motion_state()
284
285 =head2 motion_x()
286
287 Returns the motion of the mouse in X direction as an absolute value.
288
289 =head2 motion_y()
290
291 Returns the motion of the mouse in Y direction as an absolute value.
292
293 =head2 motion_xrel()
294
295 Returns the motion of the mouse in X direction as a relative value.
296
297 =head2 motion_yrel()
298
299 Returns the motion of the mouse in Y direction as a relative value.
300
301 =head2 button_state()
302
303 Returns the state of the mouse buttons.
304
305 =head2 button_x()
306
307 =head2 button_y()
308
309 =head2 button()
310
311 =head1 AUTHOR
312
313 David J. Goehrig
314 Documentation by Tels <http://bloodgate.com/>
315
316 =head1 SEE ALSO
317
318 L<perl> L<SDL::App>
319
320 =cut