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
Returns the button count.
-
.button_pressed?(joystick, button) ⇒ Boolean
trueif button pressed. -
.connected?(joystick) ⇒ Boolean
trueif connected. -
.has_axis?(joystick, axis) ⇒ Boolean
trueif has axis. -
.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?.
50 51 52 |
# File 'lib/sfml/window/joystick.rb', line 50 def axis_position(joystick, axis) C::Window.sfJoystick_getAxisPosition(_id(joystick), _axis_code(axis)) end |
.button_count(joystick) ⇒ Object
Returns the button count.
38 39 40 |
# File 'lib/sfml/window/joystick.rb', line 38 def (joystick) C::Window.sfJoystick_getButtonCount(_id(joystick)) end |
.button_pressed?(joystick, button) ⇒ Boolean
true if button pressed.
55 56 57 |
# File 'lib/sfml/window/joystick.rb', line 55 def (joystick, ) C::Window.sfJoystick_isButtonPressed(_id(joystick), Integer()) end |
.connected?(joystick) ⇒ Boolean
true if connected.
33 34 35 |
# File 'lib/sfml/window/joystick.rb', line 33 def connected?(joystick) C::Window.sfJoystick_isConnected(_id(joystick)) end |
.has_axis?(joystick, axis) ⇒ Boolean
true if has axis.
43 44 45 |
# File 'lib/sfml/window/joystick.rb', line 43 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.
61 62 63 64 65 66 67 68 69 70 |
# 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 |