Parent

Class/Module Index [+]

Quicksearch

Rubygame::EventHandler

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:

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

Attributes

hooks[RW]

Public Class Methods

new { |handler| ... } → new_handler click to toggle source

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?
end

Public Instance Methods

append_hook( hook ) → hook click to toggle source
append_hook( description ) → hook

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 = Rubygame::EventHook.new( hook ) if hook.kind_of?( Hash )
        @hooks = (@hooks - [hook]) | [hook]
        return hook
end
handle( event ) → nil click to toggle source

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)

Returns

nil.

# File lib/rubygame/event_handler.rb, line 129
def handle( event )
        matching_hooks = @hooks.select { |hook| hook.match?( event ) }
        
        catch :event_consumed do
                matching_hooks.each do |hook|
                        hook.perform( event )
                        throw :event_consumed if hook.consumes
                end
        end
                
        return nil
end
has_hook?( hook ) click to toggle source

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 )
end
prepend_hook( hook ) → hook click to toggle source
prepend_hook( description ) → 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 = Rubygame::EventHook.new( hook ) if hook.kind_of?( Hash )
        @hooks = [hook] | @hooks
        return hook
end
remove_hook( hook ) click to toggle source

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 )
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.