KeyHook stuff

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

@ -89,10 +89,9 @@ class Window < Gosu::Window
print "up: "
p id
# No queue needed for up keys
@up_keyhook.call(KEY_EVENTS[id])
@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
# @world.controller.button_up(id)
@ -119,14 +118,11 @@ class Window < Gosu::Window
end
def update
# Call all keybind hooks
@key_events[:down].each do |key|
@down_keyhook.call(key)
# Call all down key events each tick
@key_events[:down].each do |keyid|
@down_keyhook.call(KEY_EVENTS[keyid])
end
@key_events[:up].each do |key|
@up_keyhook.call(key)
end
if( @world ) then
@world.tick

@ -6,15 +6,17 @@ class MethodContainer
@method_registry = []
end
# Empty method that does nothing (placeholder for keyhooks)
def nullmethod
return nil
end
def create_method(name, &block)
method_registry << name
self.class.send(:define_method, name, &block)
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
class KeyHook
@ -36,10 +38,12 @@ class KeyHook
end
def call(hook, *args)
if( @key_hooks.key? hook ) then
@key_hooks[hook].each do |event_name|
@method_container.send(event_name, *args)
end
end
end
def get_hooks
return self.key_hooks

Loading…
Cancel
Save