module SFML::Joystick
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.
Constants
- AXES
-
Order matches sfJoystickAxis in CSFML/Window/Joystick.h.
- AXIS_ALIASES
-
Friendly aliases — POV axes are also called “hat” or “dpad” in some gamepad APIs.
- AXIS_INDEX
- MAX_AXIS_COUNT
- MAX_BUTTON_COUNT
- MAX_COUNT
Public Instance Methods
Source
# File lib/sfml/window/joystick.rb, line 87 def _axis_code(axis) sym = AXIS_ALIASES.fetch(axis, axis) AXIS_INDEX.fetch(sym) do raise ArgumentError, "Unknown joystick axis: #{axis.inspect}. Expected: #{AXES.inspect}" end end
@!visibility private
Source
# File lib/sfml/window/joystick.rb, line 80 def _id(joystick) n = Integer(joystick) raise ArgumentError, "Joystick id must be in 0..#{MAX_COUNT - 1}, got #{n}" if n < 0 || n >= MAX_COUNT n end
@!visibility private
Source
# File lib/sfml/window/joystick.rb, line 50 def axis_position(joystick, axis) C::Window.sfJoystick_getAxisPosition(_id(joystick), _axis_code(axis)) end
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?.
Source
# File lib/sfml/window/joystick.rb, line 33 def connected?(joystick) C::Window.sfJoystick_isConnected(_id(joystick)) end
true if connected.
Source
# File lib/sfml/window/joystick.rb, line 43 def has_axis?(joystick, axis) C::Window.sfJoystick_hasAxis(_id(joystick), _axis_code(axis)) end
true if has axis.
Source
# File lib/sfml/window/joystick.rb, line 61 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
Returns a Hash {name:, vendor_id:, product_id:} describing the device, or nil if the joystick isn’t connected.