Module: SFML::Joystick

Defined in:
lib/sfml/window/joystick.rb

Overview

Global gamepad / joystick state — peer to Keyboard and Mouse.

if SFML::Joystick.connected?(0)
  x = SFML::Joystick.axis_position(0, :x)        # -100.0 .. 100.0
  fire = SFML::Joystick.button_pressed?(0, 0)
  info = SFML::Joystick.identification(0)
  # => { name: "Xbox Controller", vendor_id: 0x045e, product_id: 0x028e }
end

SFML supports up to MAX_COUNT joysticks (0..MAX_COUNT-1). Axes are addressed by symbol (:x, :y, :z, :r, :u, :v, :pov_x, :pov_y) so callers don’t have to remember the sfJoystickAxis enum order.

Constant Summary collapse

AXES =

Order matches sfJoystickAxis in CSFML/Window/Joystick.h.

%i[x y z r u v pov_x pov_y].freeze
AXIS_INDEX =
AXES.each_with_index.to_h.freeze
AXIS_ALIASES =

Friendly aliases — POV axes are also called “hat” or “dpad” in some gamepad APIs.

{
  hat_x: :pov_x, hat_y: :pov_y,
  dpad_x: :pov_x, dpad_y: :pov_y,
}.freeze
MAX_COUNT =
8
MAX_BUTTON_COUNT =
32
MAX_AXIS_COUNT =
AXES.length

Class Method Summary collapse

Class Method Details

.axis_position(joystick, axis) ⇒ Object

Axis value in the range [-100.0, 100.0]. Returns 0.0 for axes that don’t exist on the device, so it’s safe to call without first checking has_axis?.



47
48
49
# File 'lib/sfml/window/joystick.rb', line 47

def axis_position(joystick, axis)
  C::Window.sfJoystick_getAxisPosition(_id(joystick), _axis_code(axis))
end

.button_count(joystick) ⇒ Object



36
37
38
# File 'lib/sfml/window/joystick.rb', line 36

def button_count(joystick)
  C::Window.sfJoystick_getButtonCount(_id(joystick))
end

.button_pressed?(joystick, button) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/sfml/window/joystick.rb', line 51

def button_pressed?(joystick, button)
  C::Window.sfJoystick_isButtonPressed(_id(joystick), Integer(button))
end

.connected?(joystick) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/sfml/window/joystick.rb', line 32

def connected?(joystick)
  C::Window.sfJoystick_isConnected(_id(joystick))
end

.has_axis?(joystick, axis) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/sfml/window/joystick.rb', line 40

def has_axis?(joystick, axis)
  C::Window.sfJoystick_hasAxis(_id(joystick), _axis_code(axis))
end

.identification(joystick) ⇒ Object

Returns a Hash vendor_id:, product_id: describing the device, or nil if the joystick isn’t connected.



57
58
59
60
61
62
63
64
65
66
# File 'lib/sfml/window/joystick.rb', line 57

def identification(joystick)
  return nil unless connected?(joystick)
  ident = C::Window.sfJoystick_getIdentification(_id(joystick))
  name_ptr = ident[:name]
  {
    name:       name_ptr.null? ? "" : name_ptr.read_string.force_encoding("UTF-8"),
    vendor_id:  ident[:vendor_id],
    product_id: ident[:product_id],
  }
end

.updateObject

Refresh joystick state. SFML auto-updates as part of pollEvent, so most code never needs this. Call it explicitly only if you’re polling joystick state without an active event loop.



71
72
73
# File 'lib/sfml/window/joystick.rb', line 71

def update
  C::Window.sfJoystick_update
end