This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Freewill.js
376 lines (321 loc) · 13.3 KB
/
Freewill.js
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
/*
- Copyright (c) 2012 Research In Motion Limited.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
*/
/*global window, document, console */
/**
* Freewill.js handles our multitouch input controls allowing for easy addition
* of Joysticks and Buttons to the DOM.
*/
var Freewill = function (params) {
'use strict';
var Freewill, controls;
/* We require a container when initailizing Freewill. Ironic. */
if (typeof params.container === 'undefined') {
console.log('Missing required parameter: container');
} else {
Freewill = this;
/* Initialize our controls arrays. */
controls = this.controls = [];
this.container = params.container;
this.container.style.position = 'fixed';
this.container.style.left = '0px'; this.container.style.right = '0px';
this.container.style.top = '0px'; this.container.style.bottom = '0px';
this.container.style.border = '0px'; this.container.style.margin = '0px'; this.container.style.padding = '0px';
this.container.style.overflow = 'hidden';
this.container.style.zIndex = '999';
/* The global 'touchstart' listener for the container. Will cycle through all of our
* controls to find the appropriate owner.
*/
this.container.addEventListener('touchstart', function (e) {
var touches, touch, point, control, m, n;
touches = e.changedTouches;
for (n = 0; n < touches.length; n = n + 1) {
touch = touches[n];
point = [touch.clientX, touch.clientY];
for (m = 0; m < controls.length; m = m + 1) {
control = controls[m];
if (Freewill.contains(control.trigger, point) === true) {
control.identifier = touch.identifier;
control._onTouchStart(touch, point);
}
}
}
}, false);
/* The global 'touchmove' listener for the container. Will cycle through all of our
* controls to find the appropriate owner.
*/
this.container.addEventListener('touchmove', function (e) {
var touches, touch, point, control, m, n;
touches = e.changedTouches;
for (n = 0; n < touches.length; n = n + 1) {
touch = touches[n];
point = [touch.clientX, touch.clientY];
for (m = 0; m < controls.length; m = m + 1) {
control = controls[m];
if (touch.identifier === control.identifier) {
control._onTouchMove(touch, point);
}
}
}
}, false);
/* The global 'touchend' listener for the container. Will cycle through all of our
* controls to find the appropriate owner.
*/
this.container.addEventListener('touchend', function (e) {
var touches, touch, point, control, m, n;
touches = e.changedTouches;
for (n = 0; n < touches.length; n = n + 1) {
touch = touches[n];
point = [touch.clientX, touch.clientY];
for (m = 0; m < controls.length; m = m + 1) {
control = controls[m];
if (touch.identifier === control.identifier) {
control._onTouchEnd(touch, point);
control.identifier = -1;
}
}
}
}, false);
}
};
/**
* REQUIRED params
* imageBase - String: Path to image resource for joystick base.
* imagePad - String: Path to image resource for joystick pad.
* fixed - boolean: true if the joystick is stationary, false if the repositions to the touch point.
*
* OPTIONAL params
* pos - []: Position on <canvas> to place this joystick. Default is [0, 0].
* trigger - []: Bounding box where touch points will trigger this joystick. Default area of joystick.
* opacLow - number: Minimum opacity of this control (0.0 - 1.0). Default 1.0.
* opacHigh - number: Maximum opacity of this control (0.0 - 1.0). Default 1.0.
**/
Freewill.prototype.addJoystick = function (params) {
'use strict';
var _this, joystick, base, pad;
/* Ensure that our required parameters are present. */
if (typeof params.imageBase === 'undefined') {
console.log('Missing required parameter: imageBase');
} else if (typeof params.imagePad === 'undefined') {
console.log('Missing required parameter: imagePad');
} else if (typeof params.fixed === 'undefined') {
console.log('Missing required parameter: fixed');
} else {
_this = this;
joystick = document.createElement('canvas');
joystick.className = 'freewill joystick';
base = document.createElement('img');
base.onload = function () {
joystick.width = base.width;
joystick.height = base.height;
joystick.style.position = 'absolute';
joystick.style.width = base.width;
joystick.style.height = base.height;
pad = document.createElement('img');
pad.onload = function () {
joystick.fixed = params.fixed;
joystick.identifier = -1;
joystick.velocity = [0.0, 0.0];
joystick.direction = 0;
joystick.context = joystick.getContext('2d');
joystick.context.drawImage(base, 0.0, 0.0);
joystick.context.drawImage(pad, (base.width - pad.width) / 2.0, (base.height - pad.height) / 2.0);
/* Set the Joystick's position. */
params.pos = typeof params.pos !== 'undefined' ? params.pos : [0.0, 0.0];
joystick.style.left = params.pos[0] + 'px';
joystick.style.top = params.pos[1] + 'px';
/* Set the Joystick's trigger rectangle. */
params.trigger = typeof params.trigger !== 'undefined' ? params.trigger : [params.pos[0], params.pos[1], base.width, base.height];
joystick.trigger = params.trigger;
/* Set the low and high opacity limits for the Joystick. */
params.opacLow = typeof params.opacLow !== 'undefined' ? params.opacLow : 1.0;
params.opacHigh = typeof params.opacHigh !== 'undefined' ? params.opacHigh : 1.0;
joystick.opacLow = params.opacLow;
joystick.opacHigh = params.opacHigh;
joystick.style.opacity = joystick.opacLow;
/* Default behaviour when a touchstart event occurs for a Joystick. */
joystick._onTouchStart = function (touch, point) {
/* Start fading in the control. */
this.style.opacity = this.opacHigh;
/* If this Joystick is free-floating, reposition it to this touch point's coordinates. */
if (this.fixed === false) {
this.style.left = (point[0] - base.width / 2.0) + 'px';
this.style.top = (point[1] - base.height / 2.0) + 'px';
this.x = point[0];
this.y = point[1];
/* Draw the control. */
this.context.clearRect(0.0, 0.0, this.width, this.height);
this.context.drawImage(base, 0.0, 0.0);
this.context.drawImage(pad, (base.width - pad.width) / 2.0, (base.height - pad.height) / 2.0);
} else {
this.x = parseFloat(this.style.left.replace('px', '')) + base.width / 2.0;
this.y = parseFloat(this.style.top.replace('px', '')) + base.height / 2.0;
/* Draw the control based on movement from center. */
this._onTouchMove(touch, point);
}
/* If there is a user-defined onTouchStart function, invoke it now. */
if (this.onTouchStart) {
this.onTouchStart(touch, point);
}
};
/* Default behaviour when a touchmove event occurs for a Joystick. */
joystick._onTouchMove = function (touch, point) {
/* We will use these variables to calculate where the touch is in relation to the Joystick base. */
var dx, dy, r, _r;
/* The delta X and delta Y values between the touch point and Joystick base. */
dx = point[0] - this.x;
dy = point[1] - this.y;
/* The absolute radius (distance) between the Joystick base and the touch point. */
r = Math.sqrt(dx * dx + dy * dy);
/* Our Joystick pad's position will not be allowed to exceed 40.0 pixels from the center of the Joystick base. */
_r = Math.min(r, 40.0);
/* Recalculate the delta X based on the shortened radius. */
dx = dx * _r / r;
if (isNaN(dx) === true) {
dx = 0.0;
}
/* Recalculate the delta Y based on the shortened radius. */
dy = dy * _r / r;
if (isNaN(dy) === true) {
dy = 0.0;
}
/* We keep track of a velocity for this Joystick (i.e. how far away from the center is the Joystick pad.) */
this.velocity = [dx, dy];
/**
* Each Joystick is 8-directional. The direction is an integer corresponding to a compass direction:
* 0: West, 1: NorthWest, 2: North, 3: NorthEast, 4: East, 5: SouthEast, 6: South, 7: SouthWest
* The formula below leverages the dy and dx values calculated to find which region the touch point inhabits,
* in relation to the Joystick.
*/
this.direction = Math.floor(4 + (-Math.atan2(dy, dx) + Math.PI / 8) * 4 / Math.PI) % 8;
/* Draw the control. */
this.context.clearRect(0.0, 0.0, this.width, this.height);
this.context.drawImage(base, 0.0, 0.0);
this.context.drawImage(pad, dx + (base.width - pad.width) / 2.0, dy + (base.height - pad.height) / 2.0);
/* If there is user-defined onTouchMove function, invoke it now. */
if (this.onTouchMove) {
this.onTouchMove(touch, point);
}
};
/* Default behaviour when a touchend event occurs for a Joystick. */
joystick._onTouchEnd = function (touch, point) {
/* Reset the velocity of this Joystick to zeros. */
this.velocity = [0.0, 0.0];
/* Draw the control. */
this.context.clearRect(0.0, 0.0, this.width, this.height);
this.context.drawImage(base, 0.0, 0.0);
this.context.drawImage(pad, (base.width - pad.width) / 2.0, (base.height - pad.height) / 2.0);
/* If there is a user-defined onTouchEnd function, invoke it now. */
if (this.onTouchEnd) {
this.onTouchEnd(touch, point);
}
/* Start fading out the control. */
this.style.opacity = this.opacLow;
};
/* Store our control. */
_this.controls.push(joystick);
_this.container.appendChild(joystick);
};
pad.src = params.imagePad;
};
base.src = params.imageBase;
return joystick;
}
};
/**
* REQUIRED params
* image - String: Path to image resource for the button.
* pos - []: Position on <canvas> to place this button. Default is [0, 0].
*
* OPTIONAL params
* trigger - []: Bounding box where touch points will trigger this button. Default area of button.
* opacLow - number: Minimum opacity of this control (0.0 - 1.0). Default 1.0.
* opacHigh - number: Maximum opacity of this control (0.0 - 1.0). Default 1.0.
**/
Freewill.prototype.addButton = function (params) {
'use strict';
var _this, image, button;
/* Ensure that our required parameters are present. */
if (typeof params.image === 'undefined') {
console.log('Missing required parameter: image');
} else if (typeof params.pos === 'undefined') {
console.log('Missing required parameter: pos');
} else {
_this = this;
button = document.createElement('canvas');
button.className = 'freewill button';
image = document.createElement('img');
image.onload = function () {
button.width = image.width;
button.height = image.height;
button.style.position = 'absolute';
button.style.width = image.width + 'px';
button.style.height = image.height + 'px';
button.getContext('2d').drawImage(image, 0, 0);
button.identifier = -1;
/* Set the Button's position. */
params.pos = typeof params.pos !== 'undefined' ? params.pos : [0.0, 0.0];
button.style.left = params.pos[0] + 'px';
button.style.top = params.pos[1] + 'px';
/* Set the Button's trigger rectangle. */
params.trigger = typeof params.trigger !== 'undefined' ? params.trigger : [params.pos[0], params.pos[1], image.width, image.height];
button.trigger = params.trigger;
/* Set the low and high opacity limits for the Button. */
params.opacLow = typeof params.opacLow !== 'undefined' ? params.opacLow : 1.0;
params.opacHigh = typeof params.opacHigh !== 'undefined' ? params.opacHigh : 1.0;
button.opacLow = params.opacLow;
button.opacHigh = params.opacHigh;
button.style.opacity = button.opacLow;
/* Default behaviour when a touchstart event occurs for a Button. */
button._onTouchStart = function (touch, point) {
/* Start fading in the button. */
this.style.opacity = this.opacHigh;
/* If there is a user-defined onTouchStart function, invoke it now. */
if (this.onTouchStart) {
this.onTouchStart(touch, point);
}
};
/* Default behaviour when a touchmove event occurs for a Button. */
button._onTouchMove = function (touch, point) {
/* If there is a user-defined onTouchMove function, invoke it now. */
if (this.onTouchMove) {
this.onTouchMove(touch, point);
}
};
/* Default behaviour when a touchend event occurs for a Button. */
button._onTouchEnd = function (touch, point) {
/* If there is a user-defined onTouchEnd function, invoke it now. */
if (this.onTouchEnd) {
this.onTouchEnd(touch, point);
}
/* Start fading out our button. */
this.style.opacity = this.opacLow;
};
/* Store our control. */
_this.controls.push(button);
_this.container.appendChild(button);
};
image.src = params.image;
return button;
}
};
/* Check whether a point exists within an area. */
Freewill.prototype.contains = function (area, point) {
if (point[0] < area[0]) { return false; }
if (point[0] > area[0] + area[2]) { return false; }
if (point[1] < area[1]) { return false; }
if (point[1] > area[1] + area[3]) { return false; }
return true;
};