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