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