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.(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
-
.axis_position(joystick, axis) ⇒ Object
Axis value in the range [-100.0, 100.0].
- .button_count(joystick) ⇒ Object
- .button_pressed?(joystick, button) ⇒ Boolean
- .connected?(joystick) ⇒ Boolean
- .has_axis?(joystick, axis) ⇒ Boolean
-
.identification(joystick) ⇒ Object
Returns a Hash vendor_id:, product_id: describing the device, or nil if the joystick isn’t connected.
-
.update ⇒ Object
Refresh joystick state.
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 (joystick) C::Window.sfJoystick_getButtonCount(_id(joystick)) end |
.button_pressed?(joystick, button) ⇒ Boolean
51 52 53 |
# File 'lib/sfml/window/joystick.rb', line 51 def (joystick, ) C::Window.sfJoystick_isButtonPressed(_id(joystick), Integer()) end |
.connected?(joystick) ⇒ 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
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 |