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.

### Methods

append_hook handle has_hook? new prepend_hook remove_hook

### Classes and Modules

Module Rubygame::EventHandler::HasEventHandler

### Attributes

hooks | [RW] |

### Public Class methods

EventHandler.new { |handler| ... } → new_handler

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

### Public Instance methods

append_hook( hook ) → hook
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.

handle( event ) → nil

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.

has_hook?( hook )

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

prepend_hook( hook ) → hook
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.

remove_hook( 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.

[Validate]