Commit | Line | Data |
8fde61e3 |
1 | # Event.pm |
2 | # |
3 | # A package for handling SDL_Event * |
4 | # |
5 | # Copyright (C) 2000,2001,2002 David J. Goehrig |
6 | # |
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 | =head2 poll() |
226 | |
227 | =head2 wait() |
228 | |
229 | Waits for an event end returns then. Always returns true. |
230 | |
231 | =head2 set( type, state ) |
232 | |
233 | Set the state for all events of the given event's type |
234 | |
235 | =head2 set_unicode( toggle ) |
236 | |
237 | Toggle unicode on the event. |
238 | |
239 | =head2 set_key_repeat( delay, interval) |
240 | |
241 | Sets the delay and intervall of the key repeat rate (e.g. when a user |
242 | holds down a key on the keyboard). |
243 | |
244 | =head2 active_gain() |
245 | |
246 | =head2 active_state() |
247 | |
248 | =head2 key_state() |
249 | |
250 | =head2 key_sym() |
251 | |
252 | =head2 key_name() |
253 | |
254 | =head2 key_mod() |
255 | |
256 | =head2 key_unicode() |
257 | |
258 | =head2 key_scancode() |
259 | |
260 | =head2 motion_state() |
261 | |
262 | =head2 motion_x() |
263 | |
264 | Returns the motion of the mouse in X direction as an absolute value. |
265 | |
266 | =head2 motion_y() |
267 | |
268 | Returns the motion of the mouse in Y direction as an absolute value. |
269 | |
270 | =head2 motion_xrel() |
271 | |
272 | Returns the motion of the mouse in X direction as a relative value. |
273 | |
274 | =head2 motion_yrel() |
275 | |
276 | Returns the motion of the mouse in Y direction as a relative value. |
277 | |
278 | =head2 button_state() |
279 | |
280 | Returns the state of the mouse buttons. |
281 | |
282 | =head2 button_x() |
283 | |
284 | =head2 button_y() |
285 | |
286 | =head2 button() |
287 | |
288 | =head1 AUTHOR |
289 | |
290 | David J. Goehrig |
291 | Documentation by Tels <http://bloodgate.com/> |
292 | |
293 | =head1 SEE ALSO |
294 | |
295 | L<perl> L<SDL::App> |
296 | |
297 | =cut |