KeyHook stuff

physics
E. Almqvist 3 years ago
parent 7b569eda1e
commit 8446013da9
  1. 12
      src/app.rb
  2. 18
      src/lib/keyhook.rb

@ -89,10 +89,9 @@ class Window < Gosu::Window
print "up: " print "up: "
p id p id
# No queue needed for up keys
@up_keyhook.call(KEY_EVENTS[id]) @up_keyhook.call(KEY_EVENTS[id])
@key_events[:down].delete(id) # when the key is released: stop holding it @key_events[:down].delete(id) # when the key is released: stop holding it
@key_events[:up] << id # append the key event to the queue
# if( @world && @world.controller ) then # if( @world && @world.controller ) then
# @world.controller.button_up(id) # @world.controller.button_up(id)
@ -119,14 +118,11 @@ class Window < Gosu::Window
end end
def update def update
# Call all keybind hooks # Call all down key events each tick
@key_events[:down].each do |key| @key_events[:down].each do |keyid|
@down_keyhook.call(key) @down_keyhook.call(KEY_EVENTS[keyid])
end end
@key_events[:up].each do |key|
@up_keyhook.call(key)
end
if( @world ) then if( @world ) then
@world.tick @world.tick

@ -6,15 +6,17 @@ class MethodContainer
@method_registry = [] @method_registry = []
end end
# Empty method that does nothing (placeholder for keyhooks)
def nullmethod
return nil
end
def create_method(name, &block) def create_method(name, &block)
method_registry << name method_registry << name
self.class.send(:define_method, name, &block) self.class.send(:define_method, name, &block)
end end
def call(method_name, *args)
debug("Calling method '#{method_name}' for #{self}")
if( @method_registry.include? method_name ) then
self.send(method_name, *args)
end
end
end end
class KeyHook class KeyHook
@ -36,8 +38,10 @@ class KeyHook
end end
def call(hook, *args) def call(hook, *args)
@key_hooks[hook].each do |event_name| if( @key_hooks.key? hook ) then
@method_container.send(event_name, *args) @key_hooks[hook].each do |event_name|
@method_container.send(event_name, *args)
end
end end
end end

Loading…
Cancel
Save