Class: Rubygame::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygame/event_handler.rb

Overview

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.

Defined Under Namespace

Modules: HasEventHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ EventHandler

call-seq:

  EventHandler.new { |handler| ... }  ->  new_handler

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

Yields:

  • (_self)

Yield Parameters:



67
68
69
70
# File 'lib/rubygame/event_handler.rb', line 67

def initialize(&block)
	@hooks = []
	yield self if block_given?
end

Instance Attribute Details

#hooksObject

Returns the value of attribute hooks.



72
73
74
# File 'lib/rubygame/event_handler.rb', line 72

def hooks
  @hooks
end

Instance Method Details

#append_hook(hook) ⇒ Object

call-seq:

  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.


95
96
97
98
99
# 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) ⇒ Object

call-seq:

  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.


129
130
131
132
133
134
135
136
137
138
139
140
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


143
144
145
# File 'lib/rubygame/event_handler.rb', line 143

def has_hook?( hook )
	@hooks.include?( hook )
end

#prepend_hook(hook) ⇒ Object

call-seq:

  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.


109
110
111
112
113
# 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) ⇒ Object

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.


158
159
160
# File 'lib/rubygame/event_handler.rb', line 158

def remove_hook( hook )
	@hooks.delete( hook )
end