Class: SDL2::Joystick
- Inherits:
-
Object
- Object
- SDL2::Joystick
- Defined in:
- joystick.c,
joystick.c
Overview
This class represents a joystick connected to the machine.
In order to use joystick subsystem, init must have been called with the SDL2::INIT_JOYSTICK flag.
Defined Under Namespace
Modules: Hat Classes: DeviceInfo
Class Method Summary collapse
-
.devices ⇒ Array<SDL2::Joystick::DeviceInfo>
Get the information of connected joysticks.
-
.game_controller?(index) ⇒ Boolean
Return true if the joystick of given index supports the game controller interface.
-
.num_connected_joysticks ⇒ Integer
Get the number of connected joysticks.
-
.open(device_index) ⇒ SDL2::Joystick
Open a joystick for use.
Instance Method Summary collapse
-
#attached? ⇒ Boolean
Return true a joystick has been opened and currently connected.
-
#axis(which) ⇒ Integer
Get the current state of an axis control on a joystick.
-
#ball(which) ⇒ Array(Integer,Integer)
Get the current state of a trackball on a joystick.
-
#button(which) ⇒ Boolean
Get the current state of a button on a joystick.
-
#destroy ⇒ nil
(also: #close)
Close a joystick device.
-
#destroy? ⇒ Boolean
(also: #close?)
Return true if the device is alread closed.
-
#GUID ⇒ String
Get the joystick GUID.
-
#hat(which) ⇒ Integer
Get the current state of a POV hat on a joystick.
-
#index ⇒ Integer
Get the index of a joystick.
-
#name ⇒ String
Get the name of a joystick.
-
#num_axes ⇒ Integer
Get the number of general axis controls on a joystick.
-
#num_balls ⇒ Integer
Get the number of trackball on a joystick.
-
#num_buttons ⇒ Integer
Get the number of button on a joystick.
-
#num_hats ⇒ Integer
Get the number of POV hats on a joystick.
Class Method Details
.devices ⇒ Array<SDL2::Joystick::DeviceInfo>
Get the information of connected joysticks
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'joystick.c', line 68
static VALUE Joystick_s_devices(VALUE self)
{
int num_joysticks = SDL_NumJoysticks();
int i;
VALUE devices = rb_ary_new2(num_joysticks);
for (i=0; i<num_joysticks; ++i) {
VALUE device = rb_obj_alloc(cDeviceInfo);
rb_iv_set(device, "@GUID", GUID_to_String(SDL_JoystickGetDeviceGUID(i)));
rb_iv_set(device, "@name", utf8str_new_cstr(SDL_JoystickNameForIndex(i)));
rb_ary_push(devices, device);
}
return devices;
}
|
.game_controller?(index) ⇒ Boolean
109 110 111 112 |
# File 'joystick.c', line 109
static VALUE Joystick_s_game_controller_p(VALUE self, VALUE index)
{
return INT2BOOL(SDL_IsGameController(NUM2INT(index)));
}
|
.num_connected_joysticks ⇒ Integer
Get the number of connected joysticks.
51 52 53 54 |
# File 'joystick.c', line 51
static VALUE Joystick_s_num_connected_joysticks(VALUE self)
{
return INT2FIX(HANDLE_ERROR(SDL_NumJoysticks()));
}
|
.open(device_index) ⇒ SDL2::Joystick
91 92 93 94 95 96 97 |
# File 'joystick.c', line 91
static VALUE Joystick_s_open(VALUE self, VALUE device_index)
{
SDL_Joystick* joystick = SDL_JoystickOpen(NUM2INT(device_index));
if (!joystick)
SDL_ERROR();
return Joystick_new(joystick);
}
|
Instance Method Details
#attached? ⇒ Boolean
Return true a joystick has been opened and currently connected.
117 118 119 120 121 122 123 |
# File 'joystick.c', line 117
static VALUE Joystick_attached_p(VALUE self)
{
Joystick* j = Get_Joystick(self);
if (!j->joystick)
return Qfalse;
return INT2BOOL(SDL_JoystickGetAttached(j->joystick));
}
|
#axis(which) ⇒ Integer
222 223 224 225 |
# File 'joystick.c', line 222
static VALUE Joystick_axis(VALUE self, VALUE which)
{
return INT2FIX(SDL_JoystickGetAxis(Get_SDL_Joystick(self), NUM2INT(which)));
}
|
#ball(which) ⇒ Array(Integer,Integer)
235 236 237 238 239 240 |
# File 'joystick.c', line 235
static VALUE Joystick_ball(VALUE self, VALUE which)
{
int dx, dy;
HANDLE_ERROR(SDL_JoystickGetBall(Get_SDL_Joystick(self), NUM2INT(which), &dx, &dy));
return rb_ary_new3(2, INT2NUM(dx), INT2NUM(dy));
}
|
#button(which) ⇒ Boolean
250 251 252 253 |
# File 'joystick.c', line 250
static VALUE Joystick_button(VALUE self, VALUE which)
{
return INT2BOOL(SDL_JoystickGetButton(Get_SDL_Joystick(self), NUM2INT(which)));
}
|
#destroy ⇒ nil Also known as: close
Close a joystick device.
155 156 157 158 159 160 161 162 |
# File 'joystick.c', line 155
static VALUE Joystick_destroy(VALUE self)
{
Joystick* j = Get_Joystick(self);
if (j->joystick)
SDL_JoystickClose(j->joystick);
j->joystick = NULL;
return Qnil;
}
|
#destroy? ⇒ Boolean Also known as: close?
Return true if the device is alread closed.
#GUID ⇒ String
Get the joystick GUID
130 131 132 133 134 135 136 137 |
# File 'joystick.c', line 130
static VALUE Joystick_GUID(VALUE self)
{
SDL_JoystickGUID guid;
char buf[128];
guid = SDL_JoystickGetGUID(Get_SDL_Joystick(self));
SDL_JoystickGetGUIDString(guid, buf, sizeof(buf));
return rb_usascii_str_new_cstr(buf);
}
|
#hat(which) ⇒ Integer
263 264 265 266 |
# File 'joystick.c', line 263
static VALUE Joystick_hat(VALUE self, VALUE which)
{
return UINT2NUM(SDL_JoystickGetHat(Get_SDL_Joystick(self), NUM2INT(which)));
}
|
#index ⇒ Integer
Get the index of a joystick
144 145 146 147 |
# File 'joystick.c', line 144
static VALUE Joystick_index(VALUE self)
{
return INT2NUM(HANDLE_ERROR(SDL_JoystickInstanceID(Get_SDL_Joystick(self))));
}
|
#name ⇒ String
Get the name of a joystick
169 170 171 172 |
# File 'joystick.c', line 169
static VALUE Joystick_name(VALUE self)
{
return utf8str_new_cstr(SDL_JoystickName(Get_SDL_Joystick(self)));
}
|
#num_axes ⇒ Integer
Get the number of general axis controls on a joystick.
179 180 181 182 |
# File 'joystick.c', line 179
static VALUE Joystick_num_axes(VALUE self)
{
return INT2FIX(SDL_JoystickNumAxes(Get_SDL_Joystick(self)));
}
|
#num_balls ⇒ Integer
Get the number of trackball on a joystick
189 190 191 192 |
# File 'joystick.c', line 189
static VALUE Joystick_num_balls(VALUE self)
{
return INT2FIX(SDL_JoystickNumBalls(Get_SDL_Joystick(self)));
}
|
#num_buttons ⇒ Integer
Get the number of button on a joystick
199 200 201 202 |
# File 'joystick.c', line 199
static VALUE Joystick_num_buttons(VALUE self)
{
return INT2FIX(SDL_JoystickNumButtons(Get_SDL_Joystick(self)));
}
|
#num_hats ⇒ Integer
Get the number of POV hats on a joystick
209 210 211 212 |
# File 'joystick.c', line 209
static VALUE Joystick_num_hats(VALUE self)
{
return INT2FIX(SDL_JoystickNumHats(Get_SDL_Joystick(self)));
}
|