Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 946 lines (838 sloc) 25.678 kb
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
1 #--
2 # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3 # Copyright (C) 2004-2008 John Croisant
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #++
19
6747f31 @jacius Cleaned up 'requires' for event handler files.
authored
20
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
21
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
22 # This module contains all the event trigger classes that
23 # come with Rubygame.
24 #
25 # An event trigger class is simply a class which can be
26 # used as a trigger an EventHook. The trigger is used to
27 # determine whether the EventHook matches a particular
28 # event that occurs.
29 #
30 # The only requirement for an event trigger is this:
31 #
32 # * It must have a #match? method which takes exactly one
33 # argument (an event) and always returns either true or
34 # false.
35 #
36 # You can make your own custom event trigger classes and
37 # use them in an EventHook if they meet that requirement.
38 #
39 # NOTE: The #match? method may be called many times every
40 # second, even if there is no matching event. So, you should
41 # try to keep the method simple and fast, to have the least
42 # impact on your game's framerate.
43 #
44 # Here is an overview of the event trigger classes that
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
45 # come with Rubygame as of version 2.7:
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
46 #
47 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
48 # AndTrigger::
49 # Holds multiple other triggers, and matches if ALL of the triggers
50 # match the event.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
51 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
52 # OrTrigger::
53 # Holds multiple other triggers, and matches if ONE OR MORE of the
54 # triggers match the event.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
55 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
56 # AttrTrigger::
57 # Matches if the event's attributes have the expected values.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
58 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
59 # BlockTrigger::
60 # Passes the event to a custom code block to check whether it matches.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
61 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
62 # InstanceOfTrigger::
63 # Matches if the event is an instance of a particular class.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
64 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
65 # JoystickAxisMoveTrigger::
66 # Matches Events::JoystickAxisMoved events.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
67 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
68 # JoystickBallMoveTrigger::
69 # Matches Events::JoystickBallMoved events.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
70 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
71 # JoystickButtonPressTrigger::
72 # Matches Events::JoystickButtonPressed events.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
73 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
74 # JoystickButtonReleaseTrigger::
75 # Matches Events::JoystickButtonReleased events.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
76 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
77 # JoystickHatMoveTrigger::
78 # Matches Events::JoystickHatMoved events.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
79 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
80 # KeyPressTrigger::
81 # Matches Events::KeyPressed events.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
82 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
83 # KeyReleaseTrigger::
84 # Matches Events::KeyReleased events.
af6751b @jacius Added TickTrigger.
authored
85 #
ea3af5e @jacius Updated list of triggers in EventTriggers doc comment.
authored
86 # KindOfTrigger::
87 # Matches if the event is #kind_of? a particular class or module.
88 #
89 # MousePressTrigger::
90 # Matches Events::MousePressed events.
91 #
92 # MouseMoveTrigger::
93 # Matches Events::MouseMoved events.
94 #
95 # MouseReleaseTrigger::
96 # Matches Events::MouseReleased events.
97 #
98 # TickTrigger::
99 # Matches Events::ClockTicked events.
100 #
101 # YesTrigger::
102 # Matches every event, no matter what.
e0614b7 @jacius Event triggers are now in Rubygame::EventTriggers.
authored
103 #
104 module Rubygame::EventTriggers
ee28e7e @jacius Added docs for AllTrigger.
authored
105
106 #
5ea2f70 @jacius Renamed AllTrigger to AndTrigger.
authored
107 # AndTrigger is an event trigger which contains one or
ee28e7e @jacius Added docs for AllTrigger.
authored
108 # more other triggers, and fires when an event matches
109 # all of its triggers. You can use this to create more
110 # complex logic than is possible with a single trigger.
111 #
d59ecc8 @jacius Renamed AnyTrigger to OrTrigger.
authored
112 # Contrast with OrTrigger.
ee28e7e @jacius Added docs for AllTrigger.
authored
113 #
5ea2f70 @jacius Renamed AllTrigger to AndTrigger.
authored
114 class AndTrigger
ee28e7e @jacius Added docs for AllTrigger.
authored
115
5ea2f70 @jacius Renamed AllTrigger to AndTrigger.
authored
116 # Initialize a new instance of AndTrigger, containing
ee28e7e @jacius Added docs for AllTrigger.
authored
117 # the given triggers.
118 #
7a5cbea @jacius Documentation formatting fixes.
authored
119 # \*triggers:: The triggers to contain.
120 # (Array of triggers, required)
ee28e7e @jacius Added docs for AllTrigger.
authored
121 #
122 # Example:
123 #
124 # gameover_trigger = InstanceOfTrigger.new( GameOver )
125 # won_trigger = AttrTrigger.new( :won_game => true )
126 #
127 # # Matches only an event which is BOTH:
128 # # 1. an instance of class GameOver, AND
129 # # 2. returns true when #won_game is called
5ea2f70 @jacius Renamed AllTrigger to AndTrigger.
authored
130 # AndTrigger.new( gameover_trigger, won_trigger )
ee28e7e @jacius Added docs for AllTrigger.
authored
131 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
132 def initialize( *triggers )
133 @triggers = triggers
134 end
135
ee28e7e @jacius Added docs for AllTrigger.
authored
136 # Returns true if the event matches all the triggers
5ea2f70 @jacius Renamed AllTrigger to AndTrigger.
authored
137 # that the AndTrigger contains.
ee28e7e @jacius Added docs for AllTrigger.
authored
138 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
139 def match?( event )
140 @triggers.all? { |trigger| trigger.match? event }
141 end
142 end
143
56f072d @jacius More whitespace.
authored
144
145
aa459a9 @jacius Added docs for AnyTrigger.
authored
146 #
d59ecc8 @jacius Renamed AnyTrigger to OrTrigger.
authored
147 # OrTrigger is an event trigger which contains one or
aa459a9 @jacius Added docs for AnyTrigger.
authored
148 # more other triggers, and fires when an event matches
149 # one or more of its triggers.
150 #
5ea2f70 @jacius Renamed AllTrigger to AndTrigger.
authored
151 # Contrast with AndTrigger.
aa459a9 @jacius Added docs for AnyTrigger.
authored
152 #
d59ecc8 @jacius Renamed AnyTrigger to OrTrigger.
authored
153 class OrTrigger
aa459a9 @jacius Added docs for AnyTrigger.
authored
154
d59ecc8 @jacius Renamed AnyTrigger to OrTrigger.
authored
155 # Initialize a new instance of OrTrigger, containing
aa459a9 @jacius Added docs for AnyTrigger.
authored
156 # the given triggers.
157 #
7a5cbea @jacius Documentation formatting fixes.
authored
158 # \*triggers:: The triggers to contain.
159 # (Array of triggers, required)
aa459a9 @jacius Added docs for AnyTrigger.
authored
160 #
161 # Example:
162 #
163 # is_red = AttrTrigger.new( :color => :red )
164 # is_blue = AttrTrigger.new( :color => :blue )
165 #
166 # # Matches only an event which has EITHER:
167 # # 1. #color == :red, OR
168 # # 2. #color == :blue
d59ecc8 @jacius Renamed AnyTrigger to OrTrigger.
authored
169 # is_red_or_blue = OrTrigger.new( is_red, is_blue )
aa459a9 @jacius Added docs for AnyTrigger.
authored
170 #
171 #
172 # # More complex example with nested logic triggers:
173 #
174 # changed = InstanceOfTrigger.new( ColorChanged )
175 #
176 # changed_to_red_or_blue =
5ea2f70 @jacius Renamed AllTrigger to AndTrigger.
authored
177 # AndTrigger.new( changed, is_red_or_blue )
aa459a9 @jacius Added docs for AnyTrigger.
authored
178 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
179 def initialize( *triggers )
180 @triggers = triggers
181 end
182
aa459a9 @jacius Added docs for AnyTrigger.
authored
183 # Returns true if the event matches one or more of
d59ecc8 @jacius Renamed AnyTrigger to OrTrigger.
authored
184 # the triggers that the OrTrigger contains.
aa459a9 @jacius Added docs for AnyTrigger.
authored
185 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
186 def match?( event )
187 @triggers.any? { |trigger| trigger.match? event }
188 end
189 end
190
56f072d @jacius More whitespace.
authored
191
192
3c09d27 @jacius Added docs for AttrTrigger.
authored
193 #
194 # AttrTrigger is an event trigger which fires when an event
195 # has the expected value(s) for one or more attributes.
196 #
197 # AttrTrigger stores a Hash of :attr => value pairs, and
198 # checks each event to see if event.attr returns value.
199 # If all attributes have the expected value, the trigger fires.
200 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
201 class AttrTrigger
3c09d27 @jacius Added docs for AttrTrigger.
authored
202
203 # Initialize a new instance of AttrTrigger with a
204 # Hash of one or more :attr => value pairs.
205 #
7a5cbea @jacius Documentation formatting fixes.
authored
206 # attributes:: The attributes / value pairs to check.
207 # (Hash, required)
3c09d27 @jacius Added docs for AttrTrigger.
authored
208 #
209 # Example:
210 #
211 # # Matches if event.color returns :red and
212 # # event.size returns :big
213 # AttrTrigger.new( :color => :red, :size => :big )
214 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
215 def initialize( attributes )
216 @attributes = attributes
217 end
3c09d27 @jacius Added docs for AttrTrigger.
authored
218
219 # Returns true if, for every :attr => value pair, the event
220 # responds to :attr and calling event.attr returns value.
221 #
222 # Returns false if any of the attributes is not the expected value.
223 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
224 def match?( event )
225 @attributes.all? { |key, value|
226 event.respond_to?(key) and (event.send(key) == value)
227 }
228 end
229 end
230
56f072d @jacius More whitespace.
authored
231
232
3c4e9c0 @jacius Added docs for BlockTrigger.
authored
233 #
234 # BlockTrigger is an event trigger which calls a block
235 # to check events. The trigger fires if the block returns
236 # true when called with the event as the only parameter.
237 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
238 class BlockTrigger
3c4e9c0 @jacius Added docs for BlockTrigger.
authored
239
240 # Initialize a new instance of BlockTrigger with the given
241 # block. The block should take only 1 parameter, the event,
242 # and return true for matching events.
243 #
7a5cbea @jacius Documentation formatting fixes.
authored
244 # &block:: The block to pass events to. (Proc, required)
3c4e9c0 @jacius Added docs for BlockTrigger.
authored
245 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
246 def initialize( &block )
247 raise ArgumentError, "BlockTrigger needs a block" unless block_given?
248 @block = block
249 end
250
3c4e9c0 @jacius Added docs for BlockTrigger.
authored
251 # Returns true if the block returns true when called
252 # with the event as the only parameter.
253 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
254 def match?( event )
255 @block.call( event ) == true
256 end
257 end
258
56f072d @jacius More whitespace.
authored
259
260
e0d2017 @jacius Commented out CollisionTrigger. It doesn't apply without Chipmunk integr...
authored
261 # class CollisionTrigger
262 #
263 # # type can be :start, :hold, :end, or :any
264 # def initialize( a=:any, b=:any, type=:any )
265 # @a, @b, @type = a, b, type
266 # end
267 #
268 # def match?( event )
269 # matching_types =
270 # case( event )
271 # when CollisionStartEvent
272 # [:start, :any]
273 # when CollisionEvent
274 # [:hold, :any]
275 # when CollisionEndEvent
276 # [:end, :any]
277 # else
278 # []
279 # end
280 #
281 # matching_types.include?(@type) and _has_objects?( event )
282 # end
283 #
284 # private
285 #
286 # # True if the event concerns the object(s) this trigger
287 # # is watching. It's not important that the event's pair order
288 # # matches the trigger's pair order.
289 # def _has_objects?( event )
290 # obs = [event.a, event.a.sprite, event.b, event.b.sprite]
291 #
292 # (@a == :any or obs.include?(@a)) and \
293 # (@b == :any or obs.include?(@b))
294 # end
295 #end
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
296
56f072d @jacius More whitespace.
authored
297
298
d46544c @jacius Added docs for InstanceOfTrigger.
authored
299 #
300 # InstanceOfTrigger is an event trigger which fires when
301 # the event is an instance of the given class. (In other
302 # words, when event.instance_of?( klass ) is true.)
303 #
304 # Contrast with KindOfTrigger.
305 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
306 class InstanceOfTrigger
d46544c @jacius Added docs for InstanceOfTrigger.
authored
307
308 # Initialize a new instance of InstanceOfTrigger with the
309 # given class.
310 #
7a5cbea @jacius Documentation formatting fixes.
authored
311 # klass:: The class to check for. (Class, required)
d46544c @jacius Added docs for InstanceOfTrigger.
authored
312 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
313 def initialize( klass )
314 @klass = klass
315 end
316
d46544c @jacius Added docs for InstanceOfTrigger.
authored
317 # Returns true if the event is an instance of the class.
318 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
319 def match?( event )
320 event.instance_of?( @klass )
321 end
322 end
323
56f072d @jacius More whitespace.
authored
324
325
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
326 #
5057f7f @jacius Added JoystickAxisMoveTrigger.
authored
327 # JoystickAxisMoveTrigger is an event trigger which fires when
328 # a joystick axis is moved (i.e. JoystickAxisMoved).
329 #
330 class JoystickAxisMoveTrigger
331
332 # Initialize a new instance of JoystickAxisMoveTrigger with the
333 # given axis and joystick.
334 #
335 # joystick:: The Rubygame::Joystick or integer ID of a joystick to
336 # detect, or :any to detect events for any joystick.
337 # axis:: The axis number to detect, or :any to detect events
338 # for any axis.
339 #
340 def initialize( joystick=:any, axis=:any )
341 @axis = axis
342 @id = case joystick
343 when Rubygame::Joystick
344 joystick.id
345 when Fixnum, :any
346 joystick
347 else
348 raise ArgumentError, "Invalid joystick: #{joystick.inspect}"
349 end
350 end
351
352 # Returns true if all of the following are true:
353 #
354 # * The event is a JoystickAxisMoved event.
355 # * The event's axis is the same as the trigger's axis, or the
356 # trigger's axis is :any.
357 # * The event's joystick ID is the same as the trigger's ID, or the
358 # trigger's ID is :any.
359 #
360 def match?( event )
361 event.kind_of?( Rubygame::Events::JoystickAxisMoved ) and
362 ((@axis == :any) or (event.axis == @axis)) and
363 ((@id == :any) or (event.joystick_id == @id))
364 end
365 end
366
367
368
369 #
ea6a75c @jacius Added JoystickBallMoveTrigger.
authored
370 # JoystickBallMoveTrigger is an event trigger which fires when
371 # a joystick ball is moved (i.e. JoystickBallMoved).
372 #
373 class JoystickBallMoveTrigger
374
375 # Initialize a new instance of JoystickBallMoveTrigger with the
376 # given ball and joystick.
377 #
378 # joystick:: The Rubygame::Joystick or integer ID of a joystick to
379 # detect, or :any to detect events for any joystick.
380 # ball:: The ball number to detect, or :any to detect events
381 # for any ball.
382 #
383 def initialize( joystick=:any, ball=:any )
384 @ball = ball
385 @id = case joystick
386 when Rubygame::Joystick
387 joystick.id
388 when Fixnum, :any
389 joystick
390 else
391 raise ArgumentError, "Invalid joystick: #{joystick.inspect}"
392 end
393 end
394
395 # Returns true if all of the following are true:
396 #
397 # * The event is a JoystickBallMoved event.
398 # * The event's ball is the same as the trigger's ball, or the
399 # trigger's ball is :any.
400 # * The event's joystick ID is the same as the trigger's ID, or the
401 # trigger's ID is :any.
402 #
403 def match?( event )
404 event.kind_of?( Rubygame::Events::JoystickBallMoved ) and
405 ((@ball == :any) or (event.ball == @ball)) and
406 ((@id == :any) or (event.joystick_id == @id))
407 end
408 end
409
410
411
412 #
5e23926 @jacius Added JoystickButtonPressTrigger.
authored
413 # JoystickButtonPressTrigger is an event trigger which fires when
414 # a joystick button is pressed (i.e. JoystickButtonPressed).
415 #
416 class JoystickButtonPressTrigger
417
418 # Initialize a new instance of JoystickButtonPressTrigger with the
419 # given button and joystick.
420 #
421 # joystick:: The Rubygame::Joystick or integer ID of a joystick to
422 # detect, or :any to detect events for any joystick.
423 # button:: The button number to detect, or :any to detect events
424 # for any button.
425 #
426 def initialize( joystick=:any, button=:any )
427 @button = button
428 @id = case joystick
429 when Rubygame::Joystick
430 joystick.id
431 when Fixnum, :any
432 joystick
433 else
434 raise ArgumentError, "Invalid joystick: #{joystick.inspect}"
435 end
436 end
437
438 # Returns true if all of the following are true:
439 #
440 # * The event is a JoystickButtonPressed event.
441 # * The event's button is the same as the trigger's button, or the
442 # trigger's button is :any.
443 # * The event's joystick ID is the same as the trigger's ID, or the
444 # trigger's ID is :any.
445 #
446 def match?( event )
447 event.kind_of?( Rubygame::Events::JoystickButtonPressed ) and
448 ((@button == :any) or (event.button == @button)) and
449 ((@id == :any) or (event.joystick_id == @id))
450 end
451 end
452
453
454
455 #
a138c59 @jacius Added JoystickButtonReleaseTrigger.
authored
456 # JoystickButtonReleaseTrigger is an event trigger which fires when
457 # a joystick button is released (i.e. JoystickButtonReleased).
458 #
459 class JoystickButtonReleaseTrigger
460
461 # Initialize a new instance of JoystickButtonReleaseTrigger with the
462 # given button and joystick.
463 #
464 # joystick:: The Rubygame::Joystick or integer ID of a joystick to
465 # detect, or :any to detect events for any joystick.
466 # button:: The button number to detect, or :any to detect events
467 # for any button.
468 #
469 def initialize( joystick=:any, button=:any )
470 @button = button
471 @id = case joystick
472 when Rubygame::Joystick
473 joystick.id
474 when Fixnum, :any
475 joystick
476 else
477 raise ArgumentError, "Invalid joystick: #{joystick.inspect}"
478 end
479 end
480
481 # Returns true if all of the following are true:
482 #
483 # * The event is a JoystickButtonReleased event.
484 # * The event's button is the same as the trigger's button, or the
485 # trigger's button is :any.
486 # * The event's joystick ID is the same as the trigger's ID, or the
487 # trigger's ID is :any.
488 #
489 def match?( event )
490 event.kind_of?( Rubygame::Events::JoystickButtonReleased ) and
491 ((@button == :any) or (event.button == @button)) and
492 ((@id == :any) or (event.joystick_id == @id))
493 end
494 end
495
496
497
498 #
3ef2e03 @jacius Added JoystickHatMoveTrigger.
authored
499 # JoystickHatMoveTrigger is an event trigger which fires when
500 # a joystick hat switch is moved (i.e. JoystickHatMoved).
501 #
502 class JoystickHatMoveTrigger
503
504 # Initialize a new instance of JoystickHatMoveTrigger with the
505 # given hat, direction, and joystick.
506 #
507 # joystick:: The Rubygame::Joystick or integer ID of a joystick to
508 # detect, or :any to detect events for any joystick.
509 # hat:: The hat switch number to detect, or :any to detect
510 # events for any hat switch.
511 # direction:: The hat direction to detect, or :any to detect events
512 # for any direction. Valid directions are :up, :up_left,
513 # :up_right, :left, :right, :down, :down_left,
514 # :down_right, or nil (meaning the center).
515 #
516 def initialize( joystick=:any, hat=:any, direction=:any )
517 @hat = hat
518 @dir = direction
519 @id = case joystick
520 when Rubygame::Joystick
521 joystick.id
522 when Fixnum, :any
523 joystick
524 else
525 raise ArgumentError, "Invalid joystick: #{joystick.inspect}"
526 end
527 end
528
529 # Returns true if all of the following are true:
530 #
531 # * The event is a JoystickHatMoved event.
532 # * The event's hat is the same as the trigger's hat, or the
533 # trigger's hat is :any.
534 # * The event's direction is the same as the trigger's direction, or
535 # the trigger's direction is :any.
536 # * The event's joystick ID is the same as the trigger's ID, or the
537 # trigger's ID is :any.
538 #
539 def match?( event )
540 event.kind_of?( Rubygame::Events::JoystickHatMoved ) and
541 ((@hat == :any) or (@hat == event.hat)) and
542 ((@dir == :any) or (@dir == event.direction)) and
543 ((@id == :any) or (@id == event.joystick_id))
544 end
545 end
546
547
548
549 #
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
550 # KeyPressTrigger is an event trigger which fires when
551 # a key on the keyboard is pressed down (i.e. KeyPressed).
552 # See also KeyReleaseTrigger.
553 #
554 # This trigger can be configured to fire for any key,
555 # or a specific key. It can also fire depending on which
556 # modifier keys are held (ctrl, shift, alt, etc.).
557 #
558 # NOTE: This trigger only works with the new-style KeyPressed
559 # event class, not with the older KeyDownEvent.
560 # See EventQueue#enable_new_style_events
561 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
562 class KeyPressTrigger
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
563
564 # Initialize a new instance of KeyPressTrigger with the
565 # given key and modifier keys.
566 #
7a5cbea @jacius Documentation formatting fixes.
authored
567 # key:: the key symbol to detect, or :any (default)
568 # to detect any key. (Symbol, optional)
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
569 #
7a5cbea @jacius Documentation formatting fixes.
authored
570 # mods:: an Array of one or more modifier key symbols, or
571 # :none to detect key presses with exactly no modifiers,
572 # or :any (default) to detect any key modifiers.
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
573 #
7a5cbea @jacius Documentation formatting fixes.
authored
574 # Valid modifiers are:
575 # * :alt, :left_alt, :right_alt,
576 # * :ctrl, :left_ctrl, :right_ctrl,
577 # * :shift, :left_shift, :right_shift,
578 # * :meta, :left_meta, :right_meta,
579 # * :numlock
580 # * :capslock
581 # * :mode
e706e76 @jacius Updated docs for KeyPressTrigger.new
authored
582 #
7a5cbea @jacius Documentation formatting fixes.
authored
583 # :alt, :ctrl, :shift, and :meta will match either the
584 # left version or right version (e.g. :left_alt or
585 # :right_alt).
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
586 #
587 # Example:
588 #
589 # # Matches any key press, regardless of the key or modifiers.
590 # KeyPressTrigger.new
591 #
592 # # Matches the 'A' key with any (or no) modifiers.
593 # KeyPressTrigger.new( :a )
594 #
e706e76 @jacius Updated docs for KeyPressTrigger.new
authored
595 # # Matches the 'A' with both Ctrl and Shift modifiers.
596 # KeyPressTrigger.new( :a, [:ctrl, :shift] )
597 #
598 # # Matches the 'A' with both Left Ctrl and Left Shift modifiers.
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
599 # KeyPressTrigger.new( :a, [:left_ctrl, :left_shift] )
600 #
601 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
602 def initialize( key=:any, mods=:any )
603 @key = key
604 @mods = mods
605 end
606
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
607 # Returns true if the event is a KeyPressed event and the event's
608 # key and mods BOTH match the trigger's expectations.
609 #
610 # Key matches if either of these is true:
7a5cbea @jacius Documentation formatting fixes.
authored
611 # * the trigger's key is the symbol :any
612 # * the event's key is the same as the trigger's key
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
613 #
614 # Modifiers matches if any of these is true:
7a5cbea @jacius Documentation formatting fixes.
authored
615 # * the trigger's @mods is the symbol :any
616 # * the event has no modifiers and the trigger's @mods is
617 # the symbol :none
618 # * every one of the trigger's @mods matches one of the event's
619 # modifiers. "Matches" means either it is the same symbol,
620 # or it is a more general version. For example, :alt will
621 # match either :left_alt or :right_alt.
e8d2a25 @jacius Added docs for KeyPressTrigger.
authored
622 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
623 def match?( event )
ed623ae @jacius Module scope problems in event triggers.
authored
624 if event.kind_of?( Rubygame::Events::KeyPressed )
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
625 ((@key == :any) or (event.key == @key)) and \
1f72933 @jacius Fixed key triggers: new keyboard events use @modifiers, not @mods.
authored
626 ((@mods == :any) or (@mods == :none and event.modifiers == [])\
29ccdbd @jacius Added KeyPressTrigger#_mods_match?. Mods matching behavior expanded.
authored
627 or (_mods_match?(event.modifiers)))
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
628 end
629 end
29ccdbd @jacius Added KeyPressTrigger#_mods_match?. Mods matching behavior expanded.
authored
630
631
632 private
633
634 # True if every modifier in @mods matches a modifier in
635 # evmods. :alt, :ctrl, :meta, and :shift match either
636 # the left or right versions (e.g. :left_alt, :right_alt).
637 # All other symbols match themselves.
638 #
639 def _mods_match?( evmods ) # :nodoc:
640 @mods.all? { |mod|
641 case mod
642 when :alt, :ctrl, :meta, :shift
643 evmods.include?("left_#{mod}".intern) or
644 evmods.include?("right_#{mod}".intern)
645 else
646 evmods.include?(mod)
647 end
648 }
649 end
650
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
651 end
652
56f072d @jacius More whitespace.
authored
653
654
ca592de @jacius Added docs for KeyReleaseTrigger.
authored
655 #
656 # KeyReleaseTrigger is an event trigger which fires when
657 # a key on the keyboard is released (i.e. KeyReleased).
658 #
659 # NOTE: This trigger is identical to KeyPressTrigger, except that
660 # it fires for KeyReleased instead of KeyPressed. Please
661 # see the documentation for KeyPressTrigger for info about
662 # the parameters and behavior of the trigger.
663 #
664 # NOTE: This trigger only works with the new-style KeyReleased
665 # event class, not with the older KeyUpEvent.
666 # See EventQueue#enable_new_style_events
667 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
668 class KeyReleaseTrigger
ca592de @jacius Added docs for KeyReleaseTrigger.
authored
669
670 # Initialize a new instance of KeyReleaseTrigger with the
671 # given key and modifier keys.
672 #
673 # See KeyPressTrigger#new for more information and examples.
674 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
675 def initialize( key=:any, mods=:any )
676 @key = key
677 @mods = mods
678 end
679
ca592de @jacius Added docs for KeyReleaseTrigger.
authored
680 # Returns true if the event is a KeyReleased event and the event's
681 # key and mods BOTH match the trigger's expectations.
682 #
683 # See KeyPressTrigger#match? for more information.
684 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
685 def match?( event )
ed623ae @jacius Module scope problems in event triggers.
authored
686 if event.kind_of?( Rubygame::Events::KeyReleased )
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
687 ((@key == :any) or (event.key == @key)) and \
1f72933 @jacius Fixed key triggers: new keyboard events use @modifiers, not @mods.
authored
688 ((@mods == :any) or (@mods == :none and event.modifiers == [])\
e818fe5 @jacius Updated KeyReleaseTrigger with the same mod-matching behavior as KeyPres...
authored
689 or (_mods_match?(event.modifiers)))
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
690 end
691 end
e818fe5 @jacius Updated KeyReleaseTrigger with the same mod-matching behavior as KeyPres...
authored
692
693
694 private
695
696 # True if every modifier in @mods matches a modifier in
697 # evmods. :alt, :ctrl, :meta, and :shift match either
698 # the left or right versions (e.g. :left_alt, :right_alt).
699 # All other symbols match themselves.
700 #
701 def _mods_match?( evmods ) # :nodoc:
702 @mods.all? { |mod|
703 case mod
704 when :alt, :ctrl, :meta, :shift
705 evmods.include?("left_#{mod}".intern) or
706 evmods.include?("right_#{mod}".intern)
707 else
708 evmods.include?(mod)
709 end
710 }
711 end
712
713
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
714 end
715
56f072d @jacius More whitespace.
authored
716
717
8460cdc @jacius Added docs for KindOfTrigger.
authored
718 #
719 # KindOfTrigger is an event trigger which fires when
720 # the event is kind of the given class or module.
721 # (In other words, when event.kind_of?( kind ) is
722 # true.)
723 #
724 # Contrast with InstanceOfTrigger.
725 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
726 class KindOfTrigger
8460cdc @jacius Added docs for KindOfTrigger.
authored
727
728 # Initialize a new instance of KindOfTrigger with the
729 # given class or module.
730 #
7a5cbea @jacius Documentation formatting fixes.
authored
731 # kind:: The class/module to check for.
732 # (Class or Module, required)
8460cdc @jacius Added docs for KindOfTrigger.
authored
733 #
734 def initialize( kind )
735 @kind = kind
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
736 end
737
8460cdc @jacius Added docs for KindOfTrigger.
authored
738 # Returns true if the event is kind of the class/module.
739 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
740 def match?( event )
8460cdc @jacius Added docs for KindOfTrigger.
authored
741 event.kind_of?( @kind )
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
742 end
743 end
744
56f072d @jacius More whitespace.
authored
745
746
600a60a @jacius Renamed MouseClickTrigger to MousePressTrigger, added docs.
authored
747 #
748 # MousePressTrigger is an event trigger which fires when
749 # a mouse button is pressed down (i.e. MousePressed).
750 #
751 # By default, this trigger fires for any mouse press, but
752 # it can be configured to fire for only a specific mouse
753 # button by passing a button symbol to #new.
754 #
755 # See also MousReleaseTrigger.
756 #
757 class MousePressTrigger
758
759
760 # Initialize a new instance of MousePressTrigger with
761 # the given mouse button.
762 #
7a5cbea @jacius Documentation formatting fixes.
authored
763 # button:: The mouse button symbol to detect, or :any
764 # to detect any button press.
600a60a @jacius Renamed MouseClickTrigger to MousePressTrigger, added docs.
authored
765 #
7a5cbea @jacius Documentation formatting fixes.
authored
766 # Valid mouse button symbols are: :mouse_left,
767 # :mouse_middle, :mouse_right, :mouse_wheel_up,
768 # and :mouse_wheel_down.
600a60a @jacius Renamed MouseClickTrigger to MousePressTrigger, added docs.
authored
769 #
3a4ba0a @jacius Removed the "area" parameter to all the mouse-related triggers.
authored
770 def initialize( button=:any )
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
771 @button = button
772 end
773
600a60a @jacius Renamed MouseClickTrigger to MousePressTrigger, added docs.
authored
774 # Returns true if the event is a MousePressed event and
775 # the event's button is the same as the trigger's button
776 # (or the trigger's button is :any).
777 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
778 def match?( event )
ed623ae @jacius Module scope problems in event triggers.
authored
779 if event.kind_of?( Rubygame::Events::MousePressed )
3a4ba0a @jacius Removed the "area" parameter to all the mouse-related triggers.
authored
780 ((@button == :any) or (event.button == @button))
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
781 else
782 false
783 end
784 end
785 end
786
56f072d @jacius More whitespace.
authored
787
788
18c6809 @jacius Expanded the behavior of MouseHoverTrigger's buttons parameter.
authored
789 #
1d1fdc5 @jacius Renamed MouseHoverTrigger to MouseMoveTrigger.
authored
790 # MouseMoveTrigger is an event trigger which fires when the
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
791 # mouse cursor is moved (MouseMoved). If buttons are given,
792 # it only matches events with those buttons. See #new for details.
18c6809 @jacius Expanded the behavior of MouseHoverTrigger's buttons parameter.
authored
793 #
1d1fdc5 @jacius Renamed MouseHoverTrigger to MouseMoveTrigger.
authored
794 class MouseMoveTrigger
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
795
796 #
1d1fdc5 @jacius Renamed MouseHoverTrigger to MouseMoveTrigger.
authored
797 # Create a new instance of MouseMoveTrigger.
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
798 #
799 # The buttons parameter determines which mouse buttons can
800 # be held down and still match this trigger. It can be one of:
801 #
802 # 1. +:any+. Matches if zero or more buttons are held.
803 # 2. +:none+. Matches when zero buttons are being held.
804 # 3. +:mouse_left+, etc. Matches when at least the given
805 # button is being held.
806 # 4. An array of +:mouse_*+ symbols. Matches when exactly all
807 # buttons in the Array are being held, and nothing else.
808 #
809 #
810 # Example:
811 #
812 # # Matches all MouseMoved events, regardless of buttons:
1d1fdc5 @jacius Renamed MouseHoverTrigger to MouseMoveTrigger.
authored
813 # MouseMoveTrigger.new()
814 # MouseMoveTrigger.new( :any )
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
815 #
816 #
817 # # Matches only if no buttons pressed:
1d1fdc5 @jacius Renamed MouseHoverTrigger to MouseMoveTrigger.
authored
818 # MouseMoveTrigger.new( :none )
819 # MouseMoveTrigger.new( [] )
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
820 #
821 #
822 # # Matches if left mouse is held down, maybe with others:
1d1fdc5 @jacius Renamed MouseHoverTrigger to MouseMoveTrigger.
authored
823 # MouseMoveTrigger.new( :mouse_left )
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
824 #
825 #
826 # # Matches if ONLY left mouse held down, nothing else:
1d1fdc5 @jacius Renamed MouseHoverTrigger to MouseMoveTrigger.
authored
827 # MouseMoveTrigger.new( [:mouse_left] )
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
828 #
829 #
830 # # Matches if BOTH left AND right mouse are held down, nothing else:
1d1fdc5 @jacius Renamed MouseHoverTrigger to MouseMoveTrigger.
authored
831 # MouseMoveTrigger.new( [:mouse_left, :mouse_right] )
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
832 #
833 #
834 # # Matches if EITHER left OR right mouse are held down:
d59ecc8 @jacius Renamed AnyTrigger to OrTrigger.
authored
835 # OrTrigger.new( MouseMoveTrigger.new(:mouse_left),
836 # MouseMoveTrigger.new(:mouse_right) )
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
837 #
838 #
18c6809 @jacius Expanded the behavior of MouseHoverTrigger's buttons parameter.
authored
839 def initialize( buttons=:any )
840 @buttons = buttons
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
841 end
842
7aec591 @jacius Added docs for MouseHoverTrigger.
authored
843 #
844 # Returns true if the given event matches this trigger.
845 # See #new for information about how events match.
846 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
847 def match?( event )
ed623ae @jacius Module scope problems in event triggers.
authored
848 if event.kind_of?( Rubygame::Events::MouseMoved )
18c6809 @jacius Expanded the behavior of MouseHoverTrigger's buttons parameter.
authored
849 ((@buttons == :any) or
850 (@buttons == :none and event.buttons == []) or
41bb60a @jacius MouseMoveTrigger does not care about buttons order.
authored
851 (_buttons_match?(event.buttons)) or
18c6809 @jacius Expanded the behavior of MouseHoverTrigger's buttons parameter.
authored
852 (event.buttons.include?(@buttons)))
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
853 else
854 false
855 end
856 end
41bb60a @jacius MouseMoveTrigger does not care about buttons order.
authored
857
858 private
859
860 # Returns true if evbuttons is the same as @buttons,
861 # ignoring the order of the symbols.
862 #
863 def _buttons_match?( evbuttons )
864 if( @buttons.kind_of? Symbol )
865 return false
866 end
867
868 e = evbuttons.sort_by { |button| button.to_s }
869 t = @buttons.sort_by { |button| button.to_s }
870 return (e == t)
871 end
872
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
873 end
874
56f072d @jacius More whitespace.
authored
875
876
156c5c2 @jacius Added docs for MouseReleaseTrigger.
authored
877 #
878 # MouseReleaseTrigger is an event trigger which fires when
879 # a mouse button is released (i.e. MouseReleased).
880 #
881 # By default, this trigger fires for any mouse release, but
882 # it can be configured to fire for only a specific mouse
883 # button by passing a button symbol to #new.
884 #
885 # See also MousePressTrigger.
886 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
887 class MouseReleaseTrigger
156c5c2 @jacius Added docs for MouseReleaseTrigger.
authored
888
889 # Initialize a new instance of MouseReleaseTrigger with
890 # the given mouse button.
891 #
7a5cbea @jacius Documentation formatting fixes.
authored
892 # button:: The mouse button symbol to detect, or :any
893 # to detect any button press.
156c5c2 @jacius Added docs for MouseReleaseTrigger.
authored
894 #
7a5cbea @jacius Documentation formatting fixes.
authored
895 # Valid mouse button symbols are: :mouse_left,
896 # :mouse_middle, :mouse_right, :mouse_wheel_up,
897 # and :mouse_wheel_down.
156c5c2 @jacius Added docs for MouseReleaseTrigger.
authored
898 #
3a4ba0a @jacius Removed the "area" parameter to all the mouse-related triggers.
authored
899 def initialize( button=:any )
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
900 @button = button
901 end
902
156c5c2 @jacius Added docs for MouseReleaseTrigger.
authored
903 # Returns true if the event is a MouseReleased event and
904 # the event's button is the same as the trigger's button
905 # (or the trigger's button is :any).
906 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
907 def match?( event )
ed623ae @jacius Module scope problems in event triggers.
authored
908 if event.kind_of?( Rubygame::Events::MouseReleased )
3a4ba0a @jacius Removed the "area" parameter to all the mouse-related triggers.
authored
909 ((@button == :any) or (event.button == @button))
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
910 else
911 false
912 end
913 end
914 end
915
56f072d @jacius More whitespace.
authored
916
917
af6751b @jacius Added TickTrigger.
authored
918 #
919 # TickTrigger is an event trigger which will fire
920 # when the Clock ticks (ClockTicked).
921 #
922 class TickTrigger
923
924 # Returns true if the event is a ClockTicked event.
925 def match?( event )
926 event.kind_of?( Rubygame::Events::ClockTicked )
927 end
928 end
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
929
56f072d @jacius More whitespace.
authored
930
931
70fe81a @jacius Added docs for YesTrigger.
authored
932 #
933 # YesTrigger is an event trigger which will fire
934 # when any event occurs, regardless of the event
935 # type or details.
936 #
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
937 class YesTrigger
70fe81a @jacius Added docs for YesTrigger.
authored
938
939 # Returns true every time.
950c91d @jacius Copied over event_actions.rb, event_handler.rb_event_hook.rb, and event_...
authored
940 def match?( event )
941 true
942 end
943 end
944
945 end
Something went wrong with that request. Please try again.