KeyHook binds

physics
E. Almqvist 3 years ago
parent 3d5a8ecb96
commit 135e4cec87
  1. 34
      src/app.rb
  2. 8
      src/lib/keyhook.rb

@ -15,6 +15,16 @@ require_relative "lib/world.rb"
require_relative "ui/mainmenu.rb" require_relative "ui/mainmenu.rb"
def pause
puts "Hej"
end
def fire
end
def select
end
class Window < Gosu::Window class Window < Gosu::Window
attr_accessor :caption, :ui, :world, :mainmenu, :key_events attr_accessor :caption, :ui, :world, :mainmenu, :key_events
attr_reader :width, :height, :fonts attr_reader :width, :height, :fonts
@ -38,7 +48,7 @@ class Window < Gosu::Window
KEY_EVENTS[:general].each do |key, event| KEY_EVENTS[:general].each do |key, event|
puts "Binding key '#{key}' to '#{event}'" puts "Binding key '#{key}' to '#{event}'"
@up_keyhook.add(event, :main,) @up_keyhook.add(key, :main, event)
end end
@ui = [] @ui = []
@ -73,7 +83,7 @@ class Window < Gosu::Window
planet.vel = Vector[-2, 0] planet.vel = Vector[-2, 0]
planet.show_info = true planet.show_info = true
sol_orbiters = [cube, cube2, planet] sol_orbiters = [ply, cube2, planet]
sol.orbit(sol_orbiters) sol.orbit(sol_orbiters)
@world.planets << sol @world.planets << sol
@ -116,27 +126,14 @@ class Window < Gosu::Window
# end # end
end end
def button_up?(id)
super id
# if( @world && @world.controller ) then
# @world.controller.button_up?(id)
# end
end
def button_down?(id)
super id
# if( @world && @world.controller ) then
# @world.controller.button_down?(id)
# end
end
private def broadcast_event(event) private def broadcast_event(event)
end end
def update def update
# Call all keybind hooks # Call all keybind hooks
@key_events[:up].each do |key|
@up_keyhook.call(key)
end
if( @world ) then if( @world ) then
@world.tick @world.tick
@ -161,3 +158,4 @@ window.fullscreen = WINDOW_FULLSCREEN
window.mainmenu = MainMenu.new(window, true) window.mainmenu = MainMenu.new(window, true)
window.show window.show
# window.start_game

@ -24,7 +24,11 @@ class KeyHook
self.key_hooks[hook][event_name] = self.nullmethod_ptr self.key_hooks[hook][event_name] = self.nullmethod_ptr
end end
def call(hook, event_name, *args) def call(hook, *args)
self.key_hooks[hook][event_name].call(*args) self.key_hooks[hook].each { |h| h.call(*args) }
end
def get_hooks
return self.key_hooks
end end
end end

Loading…
Cancel
Save