Class: RGame::Core::Gamepad

Inherits:
Object
  • Object
show all
Defined in:
lib/rgame/core/gamepad.rb

Overview

Which controllers are plugged in, and what they are called.

pads = RGame::Core::Gamepad.new(app)
pads.count                  # => 1
pads.connected?(0)          # => true
pads.name(0)                # => "Xbox Controller"

This is a readout for menus — "Player 2: connect a controller" — not part of the frame path; reading a button goes through RGame::Core::Input.

Slots are player slots, stable across a momentary unplug: a pad that falls out and comes back returns to the slot it had, so player 2 stays player 2. The engine reports arrivals and departures through the App's gamepad_connected / gamepad_disconnected callbacks; this class answers the same question by polling, which is what a menu redraw wants.

Constant Summary collapse

Controls =
RGame::Util::Controls

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Gamepad

Returns a new instance of Gamepad.



26
27
28
# File 'lib/rgame/core/gamepad.rb', line 26

def initialize(app)
  @app = app
end

Instance Method Details

#connected?(slot) ⇒ Boolean

Returns:

  • (Boolean)


37
# File 'lib/rgame/core/gamepad.rb', line 37

def connected?(slot) = @app.gamepad_present?(slot)

#countObject

How many controllers are currently connected.



31
# File 'lib/rgame/core/gamepad.rb', line 31

def count = @app.gamepad_count

#device(slot) ⇒ Object

The input device id for a slot, so a menu that just found a pad can hand the right device to Input without knowing how devices are numbered.



44
# File 'lib/rgame/core/gamepad.rb', line 44

def device(slot) = Controls.gamepad(slot)

#each_connectedObject

Yields [slot, name] for each connected pad, lowest slot first. Allocates nothing, so it is safe to call from a menu that redraws every frame.



48
49
50
51
52
# File 'lib/rgame/core/gamepad.rb', line 48

def each_connected
  return enum_for(:each_connected) unless block_given?

  max_slots.times { |slot| yield slot, name(slot) if connected?(slot) }
end

#max_slotsObject

The number of player slots the engine supports, connected or not — the bound for a "controller setup" screen's loop.



35
# File 'lib/rgame/core/gamepad.rb', line 35

def max_slots = Controls::MAX_GAMEPADS

#name(slot) ⇒ Object

Human-readable name of the pad in slot, or nil when the slot is empty.



40
# File 'lib/rgame/core/gamepad.rb', line 40

def name(slot) = @app.gamepad_name(slot)