Class Rubygame::EventHandler
In: lib/rubygame/event_handler.rb
Parent: Object

EventHandler provides a simple, extensible system for hook-based event handling.

An EventHandler holds a list of EventHook objects. When the EventHandler receives a new event (passed to handle), it tests the event against each EventHook. If the event matches the EventHook, the EventHandler passes the event to the EventHook to perform an action (such as calling a method or executing a block).

Although the EventHandler and EventHook classes are very simple in themselves, they can be used as building blocks to create flexible and complex systems, whatever is needed by your application.

Here are a few ways you could use EventHandler:

  • One central EventHandler with EventHooks to perform all types of actions. This is good for simple apps.
  • Multiple EventHandlers, one for each category of event. For example, one for keyboard input, one for mouse input, one for game logic events, etc.
  • An EventHandler in every game object (using the HasEventHandler mixin module), being fed events from a central EventHandler.

You can also extend the possibilities of EventHandler and EventHook by creating your own event trigger and action classes. See EventHook for more information.


Classes and Modules

Module Rubygame::EventHandler::HasEventHandler


hooks  [RW] 

Public Class methods

Create a new EventHandler. The optional block can be used for further initializing the EventHandler.


# File lib/rubygame/event_handler.rb, line 67
        def initialize(&block)
                @hooks = []
                yield self if block_given?

Public Instance methods

Puts an EventHook at the bottom of the stack (it will be handled last). If the EventHandler already has that hook, it is moved to the bottom. See EventHook and handle.

This method has two distinct forms. The first form adds an existing Hook instance; the second form constructs a new EventHook instance and adds it.

hook:the hook to add. (EventHook, for first form only)
description:a Hash to initialize a new EventHook. (Hash, for second form only)
Returns:the hook that was added. (EventHook)

Contrast this method with prepend, which puts the EventHook at the top of the stack.


# File lib/rubygame/event_handler.rb, line 95
        def append_hook( hook )
                hook = hook ) if hook.kind_of?( Hash )
                @hooks = (@hooks - [hook]) | [hook]
                return hook

Triggers every hook in the stack which matches the given event. See EventHook.

If one of the matching hooks has @consumes enabled, no hooks after it will receive that event. (Example use: a mouse click that only affects the top-most object it hits, not any below it.)

event: the event to handle. (Object, required)



# File lib/rubygame/event_handler.rb, line 129
        def handle( event )
                matching_hooks = { |hook| hook.match?( event ) }
                catch :event_consumed do
                        matching_hooks.each do |hook|
                                hook.perform( event )
                                throw :event_consumed if hook.consumes
                return nil

Returns true if the given EventHook instance is on the stack.


# File lib/rubygame/event_handler.rb, line 143
        def has_hook?( hook )
                @hooks.include?( hook )

Exactly like append_hook, except that the EventHook is put at the top of the stack (it will be handled first). If the EventHandler already has that hook, it is moved to the top.


# File lib/rubygame/event_handler.rb, line 109
        def prepend_hook( hook )
                hook = hook ) if hook.kind_of?( Hash )
                @hooks = [hook] | @hooks
                return hook

Removes the given EventHook instance from the stack, if it exists on the stack.

Returns:the hook that was removed, or nil if the hook did not exist on the stack.

NOTE: You must pass the exact EventHook instance to remove it! Passing another EventHook that is “similar” will not work. So, you should store a reference to the hook when it is returned by append_hook or prepend_hook.


# File lib/rubygame/event_handler.rb, line 158
        def remove_hook( hook )
                @hooks.delete( hook )