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