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