struct CrystGLFW::Joystick

Overview

A Joystick object wraps an underlying GLFW joystick and exposes its attributes.

Defined in:

crystglfw/joystick.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(label : Symbol) #

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.

[View source]

Class Method Detail

def self.on_toggle_connection(&callback : ToggleConnectionCallback) #

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

[View source]

Instance Method Detail

def axes : Array(Float32) #

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

[View source]
def buttons : Array(Bool) #

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

[View source]
def code : Int32 #

[View source]
def connected? : Bool #

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

[View source]
def is?(*labels : Symbol) : Bool #

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.

[View source]
def name : String #

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.


[View source]