|
|
|
@ -1,33 +1,44 @@ |
|
|
|
|
# Empty method that does nothing (placeholder for keyhooks) |
|
|
|
|
def nullmethod |
|
|
|
|
return |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# TODO: use Object.create_method |
|
|
|
|
# https://apidock.com/ruby/Module/define_method |
|
|
|
|
|
|
|
|
|
# Handler for key events |
|
|
|
|
class KeyHook |
|
|
|
|
attr_reader :nullmethod_ptr |
|
|
|
|
attr_accessor :key_hooks |
|
|
|
|
|
|
|
|
|
class MethodContainer |
|
|
|
|
attr_reader :method_registry |
|
|
|
|
def initialize |
|
|
|
|
@nullmethod_ptr = method(:nullmethod) |
|
|
|
|
@method_registry = [] |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@key_hooks = Hash.new(Hash.new(@nullmethod_ptr)) |
|
|
|
|
# Each keyhook contains a hash of method pointers |
|
|
|
|
# Empty method that does nothing (placeholder for keyhooks) |
|
|
|
|
def nullmethod |
|
|
|
|
return nil |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def add(hook, event_name=:main, method_sym=:nullmethod) |
|
|
|
|
self.key_hooks[hook][event_name] = method(method_sym) |
|
|
|
|
def create_method(name, &block) |
|
|
|
|
method_registry << name |
|
|
|
|
self.class.send(:define_method, name, &block) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
class KeyHook |
|
|
|
|
attr_accessor :key_hooks, :method_container |
|
|
|
|
|
|
|
|
|
def initialize |
|
|
|
|
@method_container = MethodContainer.new |
|
|
|
|
@key_hooks = Hash.new([]) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def remove(hook, event_name=:main) |
|
|
|
|
self.key_hooks[hook][event_name] = self.nullmethod_ptr |
|
|
|
|
def add(hook, event_name=:main, &block) |
|
|
|
|
if( @key_hooks[hook].include? event_name ) then |
|
|
|
|
error("Duplicate event_name ('#{event_name}') for hook '#{hook}'!") |
|
|
|
|
else |
|
|
|
|
@method_container.create_method(event_name, &block) |
|
|
|
|
@key_hooks[hook] << event_name |
|
|
|
|
return event_name |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def call(hook, *args) |
|
|
|
|
self.key_hooks[hook].each { |h| h.call(*args) } |
|
|
|
|
@key_hooks[hook].each do |event_name| |
|
|
|
|
@method_container.send(event_name, *args) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def get_hooks |
|
|
|
|