File: joystick.rsd

package info (click to toggle)
ruby-sdl 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 1,544 kB
  • ctags: 1,359
  • sloc: cpp: 7,598; ansic: 4,498; ruby: 2,246; makefile: 106; sh: 102
file content (401 lines) | stat: -rw-r--r-- 7,045 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
= Joystick
* ((<Joystick Overview>))
* ((<SDL::Joystick>))
* Methods for Joystick 
TOC

== Joystick Overview
Joysticks, and other similar input devices, have a very 
strong role
in game playing and SDL provides comprehensive support for them.
Axes, Buttons, POV Hats and trackballs are all supported.

Joystick support is initialized
by passed the SDL::INIT_JOYSTICK flag
to @[init]. Once initilized joysticks must be opened
using @[Joystick.open].

While using the functions describe in this secton 
may seem like the best way
to access and read from joysticks, in most cases they aren't.
Ideally joysticks should be read
using the @[Event] system.
To enable this, you must set the joystick event processing state
with @[Joystick.poll=].
Joysticks must be @[opened|Joystick.open]
before they can be used of course.

* NOTES

  If you are ((*not*)) handling the joystick
  via the event queue then you must explicitly 
  request a joystick update by calling @[Joystick.update_all].

  Force Feedback is not yet support.

== SDL::Joystick
The class represents one joystick.

METHODS(Joystick)

== Methods
%%%
NAME num
MOD Joystick
TYPE .
PURPOSE Count available joysticks.
RVAL Integer

PROTO
num

DESC
Counts the number of joysticks attached to the system.

SEEALSO
Joystick.index_name
Joystick.open

%%
NAME index_name
MOD Joystick
TYPE .
PURPOSE Get joystick name.
RVAL String

PROTO
index_name(index)
indexName(index)

DESC
Get the implementation dependent name of joystick. 
The $[index] parameter
refers to the N'th joystick on the system.

EXAMPLE
# Print the names of all attached joysticks
num_joy = SDL::Joystick.num
printf("%d joysticks found\n", num_joy)
num_joy.times do |i|
  puts SDL::Joystick.index_name(i)
end

SEEALSO
Joystick.open

%%
NAME open
MOD Joystick
TYPE .
PURPOSE Opens a joystick for use.
RVAL SDL::Joystick

PROTO
open(index)

DESC
Opens a joystick for use within SDL. The $[index] refers to the N'th
joystick in the system. A joystick must be opened before it can be
used.

RET
Returns a @[Joystick] instance on success.

EXCEPTION *

EXAMPLE
# Check for joystick
if SDL::Joystick.num > 0
  # Open joystick
  joy = SDL::Joystick.open(0)

  printf("Opened Joystick 0\n");
  printf("Name: %s\n", SDL::Joystick.name(0))
  printf("Number of Axes: %d\n", joy.num_axes)
  printf("Number of Buttons: %d\n", joy.num_buttons)
  printf("Number of Balls: %d\n", joy.num_balls)
end  

%%
NAME open?
MOD Joystick
TYPE .
PURPOSE Determine if a joystick has been opened
RVAL true/false

PROTO
open?(index)

DESC
Determines whether a joystick has already been opened within the
application. $[index] refers to the N'th joystick on the system.

RET
Returns true if the joystick has been opened, or false if it has not.

SEEALSO
Joystick.open

%%
NAME index
MOD Joystick
TYPE #
PURPOSE Get the index of an joystick.
RVAL Integer

PROTO
index

DESC
Returns the index of $[self].

%%
NAME num_axes
MOD Joystick
TYPE #
PURPOSE Get the number of joystick axes
RVAL Integer

PROTO
num_axes
numAxes

DESC
Return the number of axes available.

NOTES
This method counts two dimensional axes as two axes.

SEEALSO
Joystick#axis

%%
NAME num_balls
MOD Joystick
TYPE #
PURPOSE Get the number of joystick trackballs
RVAL Integer

PROTO
num_balls
numBalls

DESC
Return the number of trackballs available.

SEEALSO
Joystick#ball

%%
NAME num_hats
MOD Joystick
TYPE #
PURPOSE Get the number of joystick hats
RVAL Integer

PROTO
num_hats
numHats

DESC
Return the number of hats available.

SEEALSO
Joystick#hat

%%
NAME num_buttons
MOD Joystick
TYPE #
PURPOSE Get the number of joystick buttons
RVAL Integer

PROTO
num_buttons
numButtons

DESC
Return the number of buttons available.

SEEALSO
Joystick#button

%%
NAME poll=
MOD Joystick
TYPE .
PURPOSE Enable/disable joystick event polling

PROTO
poll=(enable)

DESC
This function is used to enable or disable joystick event
processing. With joystick event processing disabled you will have
to update joystick states with @[Joystick.update_all] and read the
joystick information manually.

Joystick event polling is enabled by default.

NOTE
Joystick event handling is preferred.

Even if joystick event processing is enabled,
individual joysticks must be opened before they generate
events.

Calling this method may delete all events
currently in SDL's event queue.

SEEALSO
Joystick.update_all
Joystick.poll
Event2::JoyAxis
Event2::JoyBall
Event2::JoyButtonDown
Event2::JoyButtonUp
Event2::JoyHat

%%
NAME poll
MOD Joystick
TYPE .
PURPOSE Gets the current state of joysick event polling
RVAL true/false

PROTO
poll

DESC
Returns true if joysick event polling is enabled, otherwise 
returns false. You will also read @[Joystick.poll=].

%%
NAME update_all
MOD Joystick
TYPE .
PURPOSE Updates the state of all joysticks

PROTO
update_all
updateAll

DESC
Updates the state(position, buttons, etc.) of all open joysticks.
If joystick events have been enabled with @[Joystick.poll=]
then this is called automatically in the event loop.

%%
NAME axis
MOD Joystick
TYPE #
PURPOSE Get the current state of an axis
RVAL Integer

PROTO
axis(axis_index)

DESC
Returns the current state of given $[axis_index] on $[self].

On most modern joysticks the X axis is usually represented by
axis 0 and the Y axis by axis 1. The value returned by
this method is a signed integer (-32768 to 32767)
representing the current position of the axis, it may be
necessary to impose certain tolerances on these values to account
for jitter.

EXAMPLE
joy = SDL::Joystick.open(0)
  .
  .
x_move = joy.axis(0)
y_move = joy.axis(1)

SEEALSO
Joystick#num_axes

%%
NAME hat
MOD Joystick
TYPE #
PURPOSE Get the current state of a joystick hat
RVAL UINT

PROTO
hat(hat_index)

DESC
Returns the current state of the given $[hat_index].

RET
The current state is returned as a unsinged integer
which is an OR'd combination of one or more of the following

* SDL::Joystick::HAT_CENTERED
* SDL::Joystick::HAT_UP
* SDL::Joystick::HAT_RIGHT
* SDL::Joystick::HAT_DOWN
* SDL::Joystick::HAT_LEFT
* SDL::Joystick::HAT_RIGHTUP
* SDL::Joystick::HAT_RIGHTDOWN
* SDL::Joystick::HAT_LEFTUP
* SDL::Joystick::HAT_LEFTDOWN

SEEALSO
Joystick#num_hats

%%
NAME button
MOD Joystick
TYPE #
PURPOSE Get the current state of a given button
RVAL true/false

PROTO
button(button_index)

DESC
returns the current state of the given $[button_index].
Returns true if the button is pressed, otherwise returns false.

SEEALSO
Joystick#num_buttons

%%
NAME ball
MOD Joystick
TYPE #
PURPOSE Get relative trackball motion
RVAL [Integer, Integer]

PROTO
ball(ball_index)

DESC
Get the ball axis change.
Trackballs can only return relative motion since the last call to
this method, these motion deltas are returned as 
an array of two elements, [dx, dy].

EXCEPTION *

EXAMPLE
delta_x, delta_y = joy.ball(0)
printf("Trackball Delta- X:%d, Y:%d\n", delta_x, delta_y)

%%
NAME close
MOD Joystick
TYPE #
PURPOSE Closes a previously opened joystick

PROTO
close

DESC
Close a joystick that was previously opened with @[Joystick.open].

SEEALSO
Joystick.open
Joystick.open?