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