struct CrystGLFW::Joystick
- CrystGLFW::Joystick
- Struct
- Value
- Object
Overview
A Joystick object wraps an underlying GLFW joystick and exposes its attributes.
Defined in:
crystglfw/joystick.crConstructors
-
.new(label : Symbol)
Creates a joystick object that wraps an underlying GLFW joystick, identified by the given label.
Class Method Summary
-
.on_toggle_connection(&callback : ToggleConnectionCallback)
Defines the behavior that gets triggered when a joystick is either connected or disconnected.
Instance Method Summary
-
#axes : Array(Float32)
Returns the values of all axes of this joystick.
-
#buttons : Array(Bool)
Returns the state of all buttons of this joystick as true or false - press or release.
- #code : Int32
-
#connected? : Bool
Returns true if the joystick is connected.
-
#is?(*labels : Symbol) : Bool
Returns true if the joystick is referenced by the given label.
-
#name : String
Retrieves the joystick's default name if the joystick is connected.
Constructor Detail
Creates a joystick object that wraps an underlying GLFW joystick, identified by the given label.
joystick = CrystGLFW::Joystick.new(:joystick_last)
puts joystick.name if joystick.connected?
This method accepts the following arguments:
- label, the Symbol that identifies the virtual GLFW joystick slot.
Class Method Detail
Defines the behavior that gets triggered when a joystick is either connected or disconnected.
When GLFW detects that a joystick has either been connected or disconnected, the logic contained in the block defined by this method gets triggered. The joystick that has been connected or disconnected is yielded to the block.
CrystGLFW::Joystick.on_toggle_connection do |event|
if event.connected?
puts "A new joystick has been connected: #{event.joystick.name}"
else
puts "A joystick has been disconnected: #{event.joystick.name}"
end
end
Instance Method Detail
Returns the values of all axes of this joystick.
joystick = CrystGLFW::Joystick.new(:joystick_1)
joystick.axes.each_with_index do |axis, i|
puts "The value of axis #{i} is #{axis}"
end
Returns the state of all buttons of this joystick as true or false - press or release.
joystick = CrystGLFW::Joystick.new(:joystick_1)
joystick.buttons.each_with_index do |button, i|
puts "button #{i} is pressed!" if button
end
Returns true if the joystick is connected. False otherwise.
CrystGLFW::Joystick.on_toggle_connection do |event|
if event.joystick.connected?
puts "A joystick was connected."
else
puts "A joystick was disconnected."
end
end
Returns true if the joystick is referenced by the given label. False otherwise.
CrystGLFW::Joystick.on_toggle_connection do |event|
if event.joystick.is? :joystick_last
puts "A joystick has been connected in the last joystick slot!"
end
end
Also accepts multiple labels and returns true if the joystick is matched by any of them.
CrystGLFW::Joystick.on_toggle_connection do |event|
if event.joystick.is? :joystick_1, :joystick_2, :joystick_3, :joystick_4
puts "One of the first four joystick slots has been used."
end
end
This method accepts the following arguments:
- labels, any number of labels against which the joystick will be matched.
Retrieves the joystick's default name if the joystick is connected. Otherwise, raises an exception.
if joystick.connected? puts joystick.name # prints the joystick's name end
NOTE This method must be called from within a run
block definition.