Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/ext/object.rb,
lib/rubydraw/events.rb
Instance Method Summary collapse
-
#event? ⇒ Boolean
Returns false by default.
-
#register_action(event, window = self, &block) ⇒ Object
(also: #whenever)
Execute the given block on the appearance of an instance of
event
and pass that instance to the block. -
#unregister_action(event, window = self) ⇒ Object
Adds the ability to remove actions from registered_actions.
Instance Method Details
#event? ⇒ Boolean
Returns false by default. Is overridden in Rubydraw::Events::Event#is_event?
3 4 5 |
# File 'lib/rubydraw/events.rb', line 3 def event? false end |
#register_action(event, window = self, &block) ⇒ Object Also known as: whenever
Execute the given block on the appearance of an instance of event
and pass that instance to the block.
Example:
class MyWindow < Rubydraw::Window
def initialize
super(300, 300)
whenever(Rubydraw::Events::QuitRequest) do
puts "Goodbye!"
close
end
whenever(Rubydraw::Events::MouseMove) do |event|
new_pos = event.position
puts "Mouse moved to #{new_pos.x}, #{new_pos.y}.}"
end
end
end
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ext/object.rb', line 19 def register_action(event, window=self, &block) # A very simple rule: +window+ must be a window. Therefore, since +window+ is self # by default, instances of Rubydraw::Window calling this method don't have to set # that parameter. unless window.is_a?(Rubydraw::Window) raise ArgumentError, "window must be a Rubydraw::Window" end event_block = window.registered_actions[event] # If nobody has registered a block for this event, prepare the way. if event_block.nil? window.registered_actions[event] = {} end # Now add the block. window.registered_actions[event][self] = block end |
#unregister_action(event, window = self) ⇒ Object
Adds the ability to remove actions from registered_actions. Useful if, for example, one were to implement a button that only works if it is shown.
39 40 41 42 43 44 |
# File 'lib/ext/object.rb', line 39 def unregister_action(event, window=self) unless window.is_a?(Rubydraw::Window) raise ArgumentError, "window must be a Rubydraw::Window" end window.registered_actions[event].delete(self) end |