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) ⇒ [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
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'joystick.c', line 65
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
105 106 107 108 |
# File 'joystick.c', line 105
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.
48 49 50 51 |
# File 'joystick.c', line 48
static VALUE Joystick_s_num_connected_joysticks(VALUE self)
{
return INT2FIX(HANDLE_ERROR(SDL_NumJoysticks()));
}
|
.open(device_index) ⇒ SDL2::Joystick
88 89 90 91 92 93 94 |
# File 'joystick.c', line 88
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.
113 114 115 116 117 118 119 |
# File 'joystick.c', line 113
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
218 219 220 221 |
# File 'joystick.c', line 218
static VALUE Joystick_axis(VALUE self, VALUE which)
{
return INT2FIX(SDL_JoystickGetAxis(Get_SDL_Joystick(self), NUM2INT(which)));
}
|
#ball(which) ⇒ [Integer,Integer]
231 232 233 234 235 236 |
# File 'joystick.c', line 231
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
246 247 248 249 |
# File 'joystick.c', line 246
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.
151 152 153 154 155 156 157 158 |
# File 'joystick.c', line 151
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
126 127 128 129 130 131 132 133 |
# File 'joystick.c', line 126
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
259 260 261 262 |
# File 'joystick.c', line 259
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
140 141 142 143 |
# File 'joystick.c', line 140
static VALUE Joystick_index(VALUE self)
{
return INT2NUM(HANDLE_ERROR(SDL_JoystickInstanceID(Get_SDL_Joystick(self))));
}
|
#name ⇒ String
Get the name of a joystick
165 166 167 168 |
# File 'joystick.c', line 165
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.
175 176 177 178 |
# File 'joystick.c', line 175
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
185 186 187 188 |
# File 'joystick.c', line 185
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
195 196 197 198 |
# File 'joystick.c', line 195
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
205 206 207 208 |
# File 'joystick.c', line 205
static VALUE Joystick_num_hats(VALUE self)
{
return INT2FIX(SDL_JoystickNumHats(Get_SDL_Joystick(self)));
}
|