Class: Ray::Joystick

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • stick_id (Integer)

    Identifier for this stick. Should be smaller than Ray::Joystick.count.



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.

Returns:



7
8
9
10
11
# File 'lib/ray/joystick.rb', line 7

def [](id)
  joy = @@joysticks[id]
  joy.open if joy.closed?
  joy
end

.countInteger

Returns the number of joysticks available.

Returns:

  • (Integer)

    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.

Yields:

  • (joystick)

Yield Parameters:



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_eventtrue, false

Returns True if events coming from the joystick are handled.

Returns:

  • (true, false)

    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_countInteger

Returns The number of axes on this joystick.

Returns:

  • (Integer)

    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_countInteger

Returns The number of trackballs on this joystick.

Returns:

  • (Integer)

    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_countInteger

Returns The number of buttons on this joystick.

Returns:

  • (Integer)

    The number of buttons on this joystick



106
107
108
# File 'ext/joystick.c', line 106

VALUE ray_joystick_button_count(VALUE self) {
   return INT2FIX(SDL_JoystickNumButtons(ray_rb2joystick(self)));
}

#closeObject

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.

Returns:

  • (true, false)

    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_countInteger

Returns The number of hats on this joystick.

Returns:

  • (Integer)

    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)));
}

#openObject

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;
}