Class: Ray::Joystick
- Inherits:
-
Object
- Object
- Ray::Joystick
- Extended by:
- Enumerable
- Defined in:
- lib/ray/joystick.rb,
ext/joystick.c
Constant Summary collapse
- @@joysticks =
Hash.new { |h, k| h[k] = new(k) }
Class Method Summary collapse
-
.[](id) ⇒ Ray::Joystick
An opened joystick.
-
.count ⇒ Integer
The number of joysticks available.
-
.each {|joystick| ... } ⇒ Object
Enumerates through all the joysitcks.
-
.handle_event ⇒ true, false
True if events coming from the joystick are handled.
-
.handle_event=(val) ⇒ Object
Sets whether we should handle events coming from the joystick.
Instance Method Summary collapse
-
#axe_count ⇒ Integer
The number of axes on this joystick.
-
#ball_count ⇒ Integer
The number of trackballs on this joystick.
-
#button_count ⇒ Integer
The number of buttons on this joystick.
-
#close ⇒ Object
Closes the joystick.
-
#closed? ⇒ true, false
True if the joystick is closed.
-
#hat_count ⇒ Integer
The number of hats on this joystick.
-
#initialize(stick_id) ⇒ Object
constructor
Creates a new joystick, and open it.
-
#open ⇒ Object
Opens the joystick.
Constructor Details
#initialize(stick_id) ⇒ Object
Creates a new joystick, and open it. You don’t need to close it explictly, although doing multiple call to
38 39 40 41 42 |
# File 'ext/joystick.c', line 38
VALUE ray_init_joystick(VALUE self, VALUE stick_id) {
rb_iv_set(self, "@id", stick_id);
rb_funcall2(self, RAY_METH("open"), 0, NULL);
return Qnil;
}
|
Class Method Details
.[](id) ⇒ Ray::Joystick
Returns an opened joystick.
7 8 9 10 11 |
# File 'lib/ray/joystick.rb', line 7 def [](id) joy = @@joysticks[id] joy.open if joy.closed? joy end |
.count ⇒ Integer
Returns the number of joysticks available.
45 46 47 |
# File 'ext/joystick.c', line 45 VALUE ray_joystick_count(VALUE self) { return INT2FIX(SDL_NumJoysticks()); } |
.each {|joystick| ... } ⇒ Object
Enumerates through all the joysitcks.
17 18 19 20 21 22 23 24 25 |
# File 'lib/ray/joystick.rb', line 17 def each return Enumerator.new(self, :each) unless block_given? (0...count).each do |i| yield self[i] end self end |
.handle_event ⇒ true, false
Returns True if events coming from the joystick are handled.
52 53 54 |
# File 'ext/joystick.c', line 52 VALUE ray_joystick_handle_event(VALUE self) { return SDL_JoystickEventState(SDL_QUERY) ? Qtrue : Qfalse; } |
.handle_event=(val) ⇒ Object
Sets whether we should handle events coming from the joystick.
59 60 61 62 |
# File 'ext/joystick.c', line 59
VALUE ray_joystick_set_handle_event(VALUE self, VALUE val) {
SDL_JoystickEventState(RTEST(val) ? SDL_ENABLE : SDL_DISABLE);
return val;
}
|
Instance Method Details
#axe_count ⇒ Integer
Returns The number of axes on this joystick.
111 112 113 |
# File 'ext/joystick.c', line 111 VALUE ray_joystick_axe_count(VALUE self) { return INT2FIX(SDL_JoystickNumAxes(ray_rb2joystick(self))); } |
#ball_count ⇒ Integer
Returns The number of trackballs on this joystick.
116 117 118 |
# File 'ext/joystick.c', line 116 VALUE ray_joystick_ball_count(VALUE self) { return INT2FIX(SDL_JoystickNumBalls(ray_rb2joystick(self))); } |
#button_count ⇒ Integer
Returns The number of buttons on this joystick.
106 107 108 |
# File 'ext/joystick.c', line 106 VALUE (VALUE self) { return INT2FIX(SDL_JoystickNumButtons(ray_rb2joystick(self))); } |
#close ⇒ Object
Closes the joystick. Can be called even if closed? is true.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'ext/joystick.c', line 91
VALUE ray_joystick_close(VALUE self) {
if (ray_joystick_closed(self) == Qtrue)
return Qnil;
SDL_JoystickClose(ray_rb2joystick(self));
ray_joystick *joy = NULL;
Data_Get_Struct(self, ray_joystick, joy);
joy->joystick = NULL;
return Qnil;
}
|
#closed? ⇒ true, false
Returns true if the joystick is closed.
83 84 85 86 87 88 |
# File 'ext/joystick.c', line 83
VALUE ray_joystick_closed(VALUE self) {
ray_joystick *joy = NULL;
Data_Get_Struct(self, ray_joystick, joy);
return joy->joystick == NULL ? Qtrue : Qfalse;
}
|
#hat_count ⇒ Integer
Returns The number of hats on this joystick.
121 122 123 |
# File 'ext/joystick.c', line 121 VALUE ray_joystick_hat_count(VALUE self) { return INT2FIX(SDL_JoystickNumHats(ray_rb2joystick(self))); } |
#open ⇒ Object
Opens the joystick
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'ext/joystick.c', line 65
VALUE ray_joystick_open(VALUE self) {
int id = NUM2INT(rb_iv_get(self, "@id"));
SDL_Joystick *ptr = SDL_JoystickOpen(id);
if (!ptr) {
rb_raise(rb_eArgError, "Failled to open joystick %d (%s)",
id, SDL_GetError());
}
ray_joystick *joy = NULL;
Data_Get_Struct(self, ray_joystick, joy);
joy->joystick = ptr;
return self;
}
|