My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
import flash.external.*;

/**
* Holds the data from a real joystick (or simulates one using keys).
* Communication with the browser is via ExternalInterface. Any doubt that a
* joystick isn't available (for example, one isn't connected or the plug-in
* isn't installed) then a fallback to the keyboard occurs.
*/
class Joystick {
/**
* Maximum value for the joystick's axes.
*/
public static var MAX_AXIS:Number = 8;

/**
* Internal flag to denote that no joystick was found and values are taken
* from the keyboard fallback.
*/
private static var USE_KEYS:Number = Number.MIN_VALUE;

/**
* ID by which the browser recognises this Flash wrapper.
*/
private var id;

/**
* State of the joystick's x-axis (from -MAX_AXIS to MAX_AXIS).
*/
private var _x:Number;

/**
* State of the joystick's y-axis (from -MAX_AXIS to MAX_AXIS).
*/
private var _y:Number;

/**
* State of the joystick's designated 'a' button.
*/
private var _a:Boolean;

/**
* State of the joystick's designated 'b' button.
*/
private var _b:Boolean;

/**
* Creates the joystick wrapper.
*
* @param idx joystick plug-in index on the browser side (defaults to zero)
*/
public function Joystick(idx:Number) {
addBrowserScripts();
if (ExternalInterface.available) {
try {
this.id = ExternalInterface.call("this._register_", (idx) ? idx : 0);
} catch (e) {
this.id = Joystick.USE_KEYS;
}
if (this.id == null) {
this.id = Joystick.USE_KEYS;
}
} else {
this.id = Joystick.USE_KEYS;
}
/*
* The joysticks properties are shadowed and exposed via getters (effectively
* to make them read-only properties).
*/
this._x = 0;
this._y = 0;
this._a = false;
this._b = false;
}

/**
* Exposes the joystick's x-axis property as read-only.
*/
public function get x():Number {
return this._x;
}

/**
* Exposes the joystick's y-axis property as read-only.
*/
public function get y():Number {
return this._y;
}

/**
* Exposes the joystick's 'a' button property as read-only.
*/
public function get a():Boolean {
return this._a;
}

/**
* Exposes the joystick's 'b' button property as read-only.
*/
public function get b():Boolean {
return this._b;
}

/**
* Polls the joystick plug-in (or reads the keys).
*/
public function poll():Void {
if (this.id == Joystick.USE_KEYS) {
this.readKeys();
} else {
this._x = this.readStick("x", true);
this._y = this.readStick("y", true);
this._a = Boolean(this.readStick("a", false));
this._b = Boolean(this.readStick("b", false));
}
}

/**
* Reads the named property from the browser plug-in.
*/
private function readStick(prop:String, axis:Boolean):Number {
var res = ExternalInterface.call("this._read_", this.id, prop);
if (typeof res == "number" && axis) {
return res / (32768 / Joystick.MAX_AXIS) - Joystick.MAX_AXIS;
}
return Number(res);
}

/**
* Fallback to read the joystick values from the keyboard (cursor keys for
* direction, SPACE for button 'a', and ENTER for button 'b').
*/
private function readKeys():Void {
if (Key.isDown(Key.LEFT)) {
if (this._x > -Joystick.MAX_AXIS) {
this._x -= 1;
}
} else {
if (Key.isDown(Key.RIGHT)) {
if (this._x < Joystick.MAX_AXIS) {
this._x += 1;
}
} else {
/*
* If neither left nor right is pressed start returning the x-axis to the
* centre (incrementing or decrementing per tick).
*/
if (this._x != 0) {
if (this._x < 0) {
this._x += 1;
} else {
this._x -= 1;
}
}
}
}

if (Key.isDown(Key.UP)) {
if (this._y > -Joystick.MAX_AXIS) {
this._y -= 1;
}
} else {
if (Key.isDown(Key.DOWN)) {
if (this._y < Joystick.MAX_AXIS) {
this._y += 1;
}
} else {
/*
* As per the x-axis, start centering the y-axis if no key is pressed.
*/
if (this._y != 0) {
if (this._y < 0) {
this._y += 1;
} else {
this._y -= 1;
}
}
}
}

this._a = Key.isDown(Key.SPACE);
this._b = Key.isDown(Key.ENTER);
}

/**
* Returns a debug string.
*/
public function toString():String {
return "joystick wrapper object";
}

//**************************** Browser Helpers *****************************/

/**
* Flags if the helper scripts need adding to the browser.
*/
private static var needScripts:Boolean = true;

/**
* Adds the scripts into the owning web page (and they're only added once).
*/
private static function addBrowserScripts():Void {
if (ExternalInterface.available && needScripts) {
/*
* See the Joystick.createPlugin in joystick.js for how this works.
*/
ExternalInterface.call("eval", "if (!this._createPlugin_) {this._createPlugin_ = function()\
{\
var ctrlIE = document.createElement('object');\
if (ctrlIE) {\
try {\
ctrlIE.classid = 'CLSID:3AE9ED90-4B59-47A0-873B-7B71554B3C3E';\
if (ctrlIE.setDevice(0) != null) {\
return ctrlIE;\
}\
} catch (e) {}\
}\
var ctrlFF = document.createElement('embed');\
if (ctrlFF) {\
if (navigator && navigator.plugins) {\
var found = false;\
for (var n = 0; n < navigator.plugins.length; n++) {\
if (navigator.plugins[n].name.indexOf('Joystick') != -1) {\
found = true;\
break;\
}\
}\
if (!found) {\
return null;\
}\
}\
try {\
ctrlFF.type = 'application/x-vnd.numfum-joystick';\
ctrlFF.width = 0;\
ctrlFF.height = 0;\
document.body.appendChild(ctrlFF, document.body);\
if (ctrlFF.setDevice(0) != null) {\
return ctrlFF;\
}\
} catch (e) {}\
document.body.removeChild(ctrlFF);\
}\
return null;\
}}");
/*
* See the Joystick.register in joystick.js for how this works.
*/
ExternalInterface.call("eval", "if (!this._register_) {this._register_ = function(idx)\
{\
if (!this._registered_) {\
this._registered_ = [];\
}\
idx = Number(idx);\
if (idx < 0 || isNaN(idx)) {\
var stick = this._createPlugin_();\
if (stick) {\
return this._registered_[stick] - 1;\
}\
} else {\
if (this._registered_[idx]) {\
return idx;\
}\
var stick = this._createPlugin_();\
if (stick) {\
this._registered_[idx] = stick;\
return idx;\
}\
}\
return -1;\
}}");
ExternalInterface.call("eval", "if (!this._read_) {this._read_ = function(idx, prop)\
{\
if (!this._registered_) {\
return null;\
}\
var stick = this._registered_[Number(idx)];\
if (stick) {\
switch (prop) {\
case 'x':\
return stick.x;\
case 'y':\
return stick.y;\
case 'a':\
return stick.a;\
case 'b':\
return stick.b;\
default:\
return 0;\
}\
}\
return undefined;\
}}");
}
needScripts = false;
}
}

Change log

r34 by cwoffenden on Jul 6, 2010   Diff
Fixed bug in Safari whereby joysticks were
registered as NaN.
Go to: 
Project members, sign in to write a code review

Older revisions

r33 by cwoffenden on Jul 6, 2010   Diff
Removed dependency on external JS
file.
r28 by cwoffenden on Jun 22, 2010   Diff
Updated MIME types (in an attempt to
serve the samples).
r25 by cwoffenden on Jun 21, 2010   Diff
Added Flash example (for ActionScript
2.0).
All revisions of this file

File info

Size: 7011 bytes, 295 lines

File properties

svn:mime-type
text/x-actionscript
Powered by Google Project Hosting