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