Module: Chingu::Helpers::InputClient

Included in:
ClassicGameObject, GameObject, GameState, SimpleMenu, Window
Defined in:
lib/chingu/helpers/input_client.rb

Overview

Input-handler mixin that adds #input= and #input

#input= does 2 things: 1) Initialized an inputmap 2) notifies the parent (could be main Window or a GameState) that the object wants input

In Chingu this is mixed into Window, GameState and GameObject.

You can specify input in 3 different natural formats, the bellow 3 lines does the same thing:

The normal way, this makes left_arrow key call method “left”, and the same thing for right.

self.input = {:left => :left, :right => :right}

The shortened way, does exaclty as the above.

self.input = [:left, :right]

The multi-way, adds :a as trigger for method left, and :d as trigger for method :right

self.input = {[:a, :left] => :left, [:right, :d] => :right}

Instance Method Summary (collapse)

Instance Method Details

- (Object) add_inputs(*input_list)



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/chingu/helpers/input_client.rb', line 174

def add_inputs(*input_list)
  raise "Need to provide inputs as either parameters or hashed parameters" if input_list.empty?
  input_list = input_list[0] if input_list[0].is_a? Hash

  case input_list
    when Array
      #
      # Un-nest input_map [:left, :right, :space]
      # Into: { :left => :left, :right => :right, :space => :space }
      #
      input_list.each { |symbol| on_input(symbol) }
      
    when Hash
      #
      # Un-nest input:  { [:pad_left, :arrow_left, :a] => :move_left }
      # Into:  { :pad_left => :move_left, :arrow_left => :move_left, :a => :move_left }
      #
      input_list.each_pair do |possible_array, action|
        case possible_array
          when Array
            possible_array.each { |symbol| on_input(symbol, action) }
          when Symbol
            on_input(possible_array, action)
        end
    end
  end

  self
end

- (Boolean) holding?(key)

Returns:

  • (Boolean)


59
60
61
# File 'lib/chingu/helpers/input_client.rb', line 59

def holding?(key)
  $window.button_down?(Chingu::Input::SYMBOL_TO_CONSTANT[key])
end

- (Boolean) holding_all?(*keys)

Returns:

  • (Boolean)


85
86
87
# File 'lib/chingu/helpers/input_client.rb', line 85

def holding_all?(*keys)
  keys.all? { |key| holding?(key) }
end

- (Boolean) holding_any?(*keys)

Returns:

  • (Boolean)


72
73
74
# File 'lib/chingu/helpers/input_client.rb', line 72

def holding_any?(*keys)
  keys.any? { |key| holding?(key) }
end

- (Object) input

The current input handlers. This may contain data which is different to the input set, but it will be equivalent in function [Hash].



49
50
51
# File 'lib/chingu/helpers/input_client.rb', line 49

def input
  @input ||= Hash.new { |hash, key| hash[key] = [] }
end

- (Object) input=(input_list)

Backwards compatibility function. Use on_inputs or on_input instead. On object.input = nil we make object not respond to any key.



206
207
208
209
210
# File 'lib/chingu/helpers/input_client.rb', line 206

def input=(input_list)
  @input = nil
  return if input_list == nil
  input_list.is_a?(Array) ? add_inputs(*input_list) : add_inputs(input_list)
end

- (Object) on_input(input, action = nil, &block)

Raises:

  • (ArgumentError)


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/chingu/helpers/input_client.rb', line 241

def on_input(input, action = nil, &block)
  raise ArgumentError, "#{self.class}#on_input takes either an input OR an input and action OR an input with a block" if action and block

  inputs = input.is_a?(Array) ? input : [input]  # Can be a single input or an array of them.
  action = block if block
  action = input unless action

  @input ||= Hash.new { |hash, key| hash[key] = [] }

  # Ensure that the new input array is reasonable.
  inputs.each do |input|
    standardised_symbol, action = validate_input(input, action)
    @input[standardised_symbol] << action
  end

  if @parent
    if @input.empty?
      @parent.remove_input_client(self)
    else
      @parent.add_input_client(self)
    end
  end
  
  self
end