From 3d5a8ecb9636499a8f7eca711e2d5637073c0f9a Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 15 Nov 2021 09:13:33 +0100 Subject: [PATCH] Keyhooks --- src/app.rb | 15 +++++++++++++++ src/config.rb | 25 ++++++++++++++++++------- src/lib/keyhook.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/lib/keyhook.rb diff --git a/src/app.rb b/src/app.rb index f621e31..cde47eb 100755 --- a/src/app.rb +++ b/src/app.rb @@ -6,6 +6,7 @@ require_relative "lib/gosu_plugin.rb" require_relative "config.rb" +require_relative "lib/keyhook.rb" require_relative "lib/ui.rb" require_relative "lib/physobj.rb" require_relative "lib/objects.rb" @@ -23,11 +24,23 @@ class Window < Gosu::Window @width, @height = width, height self.caption = "#{title}| #{width}x#{height}" + # Key event queue @key_events = { up: [], down: [] } + # Keyhook for up releases + @up_keyhook = KeyHook.new + + # Keyhook for down presses + @down_keyhook = KeyHook.new + + KEY_EVENTS[:general].each do |key, event| + puts "Binding key '#{key}' to '#{event}'" + @up_keyhook.add(event, :main,) + end + @ui = [] @fonts = { normal: Gosu::Font.new(self, MAIN_FONT, 18), @@ -123,6 +136,8 @@ class Window < Gosu::Window end def update + # Call all keybind hooks + if( @world ) then @world.tick end diff --git a/src/config.rb b/src/config.rb index d6ece28..f1a90d8 100644 --- a/src/config.rb +++ b/src/config.rb @@ -8,6 +8,11 @@ WINDOW_FULLSCREEN = false MAIN_FONT = "monospace" # Keybinds + # UI + BIND_FIRE = Gosu::MS_LEFT + BIND_FIRE2 = Gosu::MS_MIDDLE + BIND_SELECT = Gosu::MS_RIGHT + # General BIND_PAUSE = Gosu::KbEscape @@ -20,14 +25,20 @@ MAIN_FONT = "monospace" # Key events KEY_EVENTS = { - # UI - BIND_PAUSE => :pause, + # General + general: { + BIND_PAUSE => :pause, + BIND_FIRE => :fire, + BIND_SELECT => :select, + }, # Player Controlls - BIND_TURN_LEFT => :turn_left, - BIND_TURN_RIGHT => :turn_right, - BIND_INCREASE_THRUST => :increase_thrust, - BIND_DECREASE_THRUST => :decrease_thrust, - BIND_TOGGLE_ENGINE => :toggle_engine + player: { + BIND_TURN_LEFT => :turn_left, + BIND_TURN_RIGHT => :turn_right, + BIND_INCREASE_THRUST => :increase_thrust, + BIND_DECREASE_THRUST => :decrease_thrust, + BIND_TOGGLE_ENGINE => :toggle_engine + } } diff --git a/src/lib/keyhook.rb b/src/lib/keyhook.rb new file mode 100644 index 0000000..298435c --- /dev/null +++ b/src/lib/keyhook.rb @@ -0,0 +1,30 @@ +# Empty method that does nothing (placeholder for keyhooks) +def nullmethod + return +end + + +# Handler for key events +class KeyHook + attr_reader :nullmethod_ptr + attr_accessor :key_hooks + + def initialize + @nullmethod_ptr = method(:nullmethod) + + @key_hooks = Hash.new(Hash.new(@nullmethod_ptr)) + # Each keyhook contains a hash of method pointers + end + + def add(hook, event_name=:main, method_sym=:nullmethod) + self.key_hooks[hook][event_name] = method(method_sym) + end + + def remove(hook, event_name=:main) + self.key_hooks[hook][event_name] = self.nullmethod_ptr + end + + def call(hook, event_name, *args) + self.key_hooks[hook][event_name].call(*args) + end +end