Class: SpriteKit::HotkeyHandler

Inherits:
Object
  • Object
show all
Defined in:
mygame/lib/sprite_kit/hotkey_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mappings, current_input: :keyboard) ⇒ HotkeyHandler

Returns a new instance of HotkeyHandler.



4
5
6
7
# File 'mygame/lib/sprite_kit/hotkey_handler.rb', line 4

def initialize(mappings, current_input: :keyboard)
  @mappings = mappings
  @current_input = current_input
end

Instance Attribute Details

#current_input ⇒ Object

Returns the value of attribute current_input.



3
4
5
# File 'mygame/lib/sprite_kit/hotkey_handler.rb', line 3

def current_input
  @current_input
end

Instance Method Details

#tick(args) ⇒ Object

}, controller: {} } HotkeyHandler.new(@input_mappings).tick(args)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'mygame/lib/sprite_kit/hotkey_handler.rb', line 39

def tick(args)
  @inputs = args.inputs
  # determine what the current input is based on what was last used
  if @inputs.last_active == :controller
    @current_input = :controller_one
  elsif @inputs.last_active == :keyboard
    @current_input = :keyboard
  end

  @mappings[@current_input].each_value do |config|
    config.keybindings.each do |keys|
      if !keys.is_a?(Array)
        keys = [keys]
      end

      keys.each_with_index do |key, index|
        if index == keys.length - 1
          # Convert symbol to string, add a "?" and then check.
          sym = ((config.on || :key_down).to_s + "?").to_sym

          if @inputs.send(@current_input).send(sym, key)
            # Yay. we did it!
            config.action.call
            # Now don't iterate more.
            # We could have multiple running at once?
            # return
          end

          break
        end

        # Just check the key is either down or held.
        break if !keyboard.send(key)
      end
    end
  end
end