canvasengine 1.3.0 → 2.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursorrules +0 -0
- package/.github/workflows/ci.yml +29 -0
- package/README.md +79 -0
- package/dist/compiler/vite.js +119 -0
- package/dist/compiler/vite.js.map +1 -0
- package/dist/index.d.ts +846 -0
- package/dist/index.js +3340 -0
- package/dist/index.js.map +1 -0
- package/index.d.ts +6 -0
- package/logo.png +0 -0
- package/package.json +84 -18
- package/src/compiler/grammar.pegjs +180 -0
- package/src/compiler/vite.ts +166 -0
- package/src/components/Canvas.ts +134 -0
- package/src/components/Container.ts +46 -0
- package/src/components/DisplayObject.ts +458 -0
- package/src/components/DrawMap/index.ts +65 -0
- package/src/components/Graphic.ts +147 -0
- package/src/components/NineSliceSprite.ts +46 -0
- package/src/components/ParticleEmitter.ts +39 -0
- package/src/components/Scene.ts +6 -0
- package/src/components/Sprite.ts +493 -0
- package/src/components/Text.ts +145 -0
- package/src/components/Tilemap/Tile.ts +79 -0
- package/src/components/Tilemap/TileGroup.ts +207 -0
- package/src/components/Tilemap/TileLayer.ts +163 -0
- package/src/components/Tilemap/TileSet.ts +41 -0
- package/src/components/Tilemap/index.ts +80 -0
- package/src/components/TilingSprite.ts +39 -0
- package/src/components/Viewport.ts +159 -0
- package/src/components/index.ts +12 -0
- package/src/components/types/DisplayObject.ts +68 -0
- package/src/components/types/MouseEvent.ts +3 -0
- package/src/components/types/Spritesheet.ts +389 -0
- package/src/components/types/index.ts +4 -0
- package/src/directives/Drag.ts +84 -0
- package/src/directives/KeyboardControls.ts +922 -0
- package/src/directives/Scheduler.ts +112 -0
- package/src/directives/Sound.ts +91 -0
- package/src/directives/Transition.ts +45 -0
- package/src/directives/ViewportCull.ts +40 -0
- package/src/directives/ViewportFollow.ts +26 -0
- package/src/directives/index.ts +7 -0
- package/src/engine/animation.ts +113 -0
- package/src/engine/bootstrap.ts +19 -0
- package/src/engine/directive.ts +23 -0
- package/src/engine/reactive.ts +379 -0
- package/src/engine/signal.ts +138 -0
- package/src/engine/trigger.ts +40 -0
- package/src/engine/utils.ts +135 -0
- package/src/hooks/addContext.ts +6 -0
- package/src/hooks/useProps.ts +155 -0
- package/src/hooks/useRef.ts +21 -0
- package/src/index.ts +14 -0
- package/src/presets/Bar.ts +89 -0
- package/src/presets/Button.ts +0 -0
- package/src/presets/Joystick.ts +286 -0
- package/src/presets/NightAmbiant.ts +122 -0
- package/src/presets/Particle.ts +53 -0
- package/src/utils/Ease.ts +33 -0
- package/src/utils/RadialGradient.ts +86 -0
- package/starter/assets/logo.png +0 -0
- package/starter/components/app.ce +18 -0
- package/starter/components/hello.ce +35 -0
- package/starter/index.html +21 -0
- package/starter/main.ts +6 -0
- package/starter/package.json +20 -0
- package/starter/tsconfig.json +32 -0
- package/starter/vite.config.ts +12 -0
- package/tsconfig.json +32 -0
- package/tsconfig.node.json +10 -0
- package/tsup.config.ts +28 -0
- package/vitest.config.ts +12 -0
- package/.gitattributes +0 -22
- package/.npmignore +0 -163
- package/canvasengine-1.3.0.all.min.js +0 -21
- package/canvasengine.js +0 -5802
- package/core/DB.js +0 -24
- package/core/ModelServer.js +0 -348
- package/core/Users.js +0 -190
- package/core/engine-common.js +0 -952
- package/doc/cocoonjs.md +0 -36
- package/doc/doc-lang.yml +0 -43
- package/doc/doc-router.yml +0 -14
- package/doc/doc-tuto.yml +0 -9
- package/doc/doc.yml +0 -39
- package/doc/element.md +0 -37
- package/doc/entity.md +0 -90
- package/doc/extend.md +0 -47
- package/doc/get_started.md +0 -19
- package/doc/images/entity.png +0 -0
- package/doc/multitouch.md +0 -58
- package/doc/nodejs.md +0 -142
- package/doc/scene.md +0 -44
- package/doc/text.md +0 -156
- package/examples/server/client.html +0 -31
- package/examples/server/server.js +0 -16
- package/examples/tiled_server/client.html +0 -52
- package/examples/tiled_server/images/tiles_spritesheet.png +0 -0
- package/examples/tiled_server/server/map.json +0 -50
- package/examples/tiled_server/server/map.tmx +0 -16
- package/examples/tiled_server/server/server.js +0 -16
- package/extends/Animation.js +0 -910
- package/extends/Effect.js +0 -252
- package/extends/Gleed2d.js +0 -252
- package/extends/Hit.js +0 -1509
- package/extends/Input.js +0 -699
- package/extends/Marshal.js +0 -716
- package/extends/Scrolling.js +0 -388
- package/extends/Soundmanager2.js +0 -5466
- package/extends/Spritesheet.js +0 -196
- package/extends/Text.js +0 -366
- package/extends/Tiled.js +0 -403
- package/extends/Window.js +0 -575
- package/extends/gamepad.js +0 -397
- package/extends/socket.io.min.js +0 -2
- package/extends/swf/soundmanager2.swf +0 -0
- package/extends/swf/soundmanager2_debug.swf +0 -0
- package/extends/swf/soundmanager2_flash9.swf +0 -0
- package/extends/swf/soundmanager2_flash9_debug.swf +0 -0
- package/extends/swf/soundmanager2_flash_xdomain.zip +0 -0
- package/extends/workers/transition.js +0 -43
- package/index.js +0 -46
- package/license.txt +0 -19
- package/readme.md +0 -483
package/extends/Input.js
DELETED
|
@@ -1,699 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright (C) 2012 by Samuel Ronce
|
|
3
|
-
|
|
4
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
in the Software without restriction, including without limitation the rights
|
|
7
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
furnished to do so, subject to the following conditions:
|
|
10
|
-
|
|
11
|
-
The above copyright notice and this permission notice shall be included in
|
|
12
|
-
all copies or substantial portions of the Software.
|
|
13
|
-
|
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
-
THE SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
@doc input
|
|
25
|
-
@class Input Keyboard, Gamepad et accelerometer
|
|
26
|
-
@example
|
|
27
|
-
Keyboard:
|
|
28
|
-
|
|
29
|
-
var canvas = CE.defines("canvas_id").
|
|
30
|
-
extend(Input).
|
|
31
|
-
ready(function() {
|
|
32
|
-
canvas.Scene.call("Map");
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
canvas.Scene.new({
|
|
36
|
-
name: "Map",
|
|
37
|
-
ready: function(stage) {
|
|
38
|
-
|
|
39
|
-
canvas.Input.keyDown(Input.Bottom);
|
|
40
|
-
canvas.Input.keyDown(Input.A, function() {
|
|
41
|
-
console.log("A pressed");
|
|
42
|
-
});
|
|
43
|
-
canvas.Input.keyUp(Input.A, function() {
|
|
44
|
-
console.log("A released");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
},
|
|
48
|
-
render: function(stage) {
|
|
49
|
-
|
|
50
|
-
if (canvas.Input.isPressed(Input.Bottom)) {
|
|
51
|
-
console.log("Bottom pressed");
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
stage.refresh();
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
Gamepad :
|
|
59
|
-
|
|
60
|
-
In header (http://www.gamepadjs.com)
|
|
61
|
-
|
|
62
|
-
<script src="extends/gamepad.js"></script>
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
Script :
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
var canvas = CE.defines("canvas_id").
|
|
69
|
-
extend(Input).
|
|
70
|
-
ready(function() {
|
|
71
|
-
canvas.Scene.call("Map");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
canvas.Scene.new({
|
|
75
|
-
name: "Map",
|
|
76
|
-
ready: function(stage) {
|
|
77
|
-
|
|
78
|
-
this.gamepad = canvas.Input.Gamepad.init(function() {
|
|
79
|
-
console.log("Gamepad connected");
|
|
80
|
-
}, function() {
|
|
81
|
-
console.log("Gamepad disconnected");
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
this.gamepad.addListener("faceButton0", function() {
|
|
85
|
-
console.log("A pressed");
|
|
86
|
-
}, function() {
|
|
87
|
-
console.log("A released");
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
canvas.Input.keyDown(Input.Right, function() {
|
|
91
|
-
// Code
|
|
92
|
-
});
|
|
93
|
-
this.gamepad.addListener("dpadRight", Input.Right);
|
|
94
|
-
|
|
95
|
-
},
|
|
96
|
-
render: function(stage) {
|
|
97
|
-
|
|
98
|
-
this.gamepad.update();
|
|
99
|
-
|
|
100
|
-
stage.refresh();
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
*/
|
|
106
|
-
|
|
107
|
-
var Input = {Input: {
|
|
108
|
-
|
|
109
|
-
keyBuffer: [],
|
|
110
|
-
cacheKeyBuffer: [],
|
|
111
|
-
_keyFunctions: {},
|
|
112
|
-
_keyPress: {},
|
|
113
|
-
_keyUp: {},
|
|
114
|
-
_keyType: {},
|
|
115
|
-
_keyPressed: {},
|
|
116
|
-
_lock: {},
|
|
117
|
-
_rules: {},
|
|
118
|
-
|
|
119
|
-
_key: function(e, key, callback) {
|
|
120
|
-
if (typeof key == "function") {
|
|
121
|
-
key(e);
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
if (key instanceof Array) {
|
|
125
|
-
for (var i=0 ; i < key.length ; i++) {
|
|
126
|
-
if (e.which == key[i]) {
|
|
127
|
-
if (callback) callback(e);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
else if (e.which == key) {
|
|
132
|
-
if (callback) callback(e);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
@doc keyboard/
|
|
139
|
-
@method press Calling a function when a key is pressed (only once)
|
|
140
|
-
@param {Integer|Array} key Value of the key (or keys if array). Input.A or [Input.A, Input.Z] for example
|
|
141
|
-
@param {Function} onPressKey Function called when a key is pressed. One parameter: the event of the button (Event Object)
|
|
142
|
-
*/
|
|
143
|
-
press: function(key, onPressKey) {
|
|
144
|
-
this._press('keyPress', key, onPressKey);
|
|
145
|
-
this.keyUp(key);
|
|
146
|
-
},
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
@doc keyboard/
|
|
150
|
-
@method clearKeys Clears the functions assigned to keys indicated
|
|
151
|
-
@param {Integer|Array} key Value of the key (or keys if array). Input.A or [Input.A, Input.Z] for example
|
|
152
|
-
*/
|
|
153
|
-
clearKeys: function(key) {
|
|
154
|
-
this.press(key, function() {});
|
|
155
|
-
},
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
@doc keyboard/
|
|
159
|
-
@method keyDown Calling a function when a key is down
|
|
160
|
-
@param {Integer} key Value of the key (or keys if array). Input.A or [Input.A, Input.Z] for example
|
|
161
|
-
@param {Function} onPressKey (optional) Function called. One parameter: the event of the button (Event Object)
|
|
162
|
-
@example
|
|
163
|
-
|
|
164
|
-
To avoid latency before repeating the movement, use `isPressed()` in render method. In the example below, use Left and Right keys :
|
|
165
|
-
|
|
166
|
-
<jsfiddle>WebCreative5/KCSu3</jsfiddle>
|
|
167
|
-
|
|
168
|
-
*/
|
|
169
|
-
keyDown: function(key, onPressKey) {
|
|
170
|
-
this._press('keyDown', key, onPressKey);
|
|
171
|
-
},
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
@doc keyboard/
|
|
175
|
-
@method keyUp Calling a function when a key is up
|
|
176
|
-
@param {Integer} key Value of the key (or keys if array). Input.A or [Input.A, Input.Z] for example
|
|
177
|
-
@param {Function} onKeyUp (optional) Function called. One parameter: the event of the button (Event Object)
|
|
178
|
-
*/
|
|
179
|
-
keyUp: function(key, onKeyUp) {
|
|
180
|
-
var self = this;
|
|
181
|
-
|
|
182
|
-
if (key instanceof Array) {
|
|
183
|
-
for (var i=0 ; i < key.length ; i++) {
|
|
184
|
-
self._keyUp[key[i]] = onKeyUp;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
self._keyUp[key] = onKeyUp;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
document.onkeyup = function(e) {
|
|
192
|
-
self._keyPress[e.which] = 0;
|
|
193
|
-
self._keyPressed[e.which] = false;
|
|
194
|
-
if (self._keyUp[e.which]) {
|
|
195
|
-
self._keyUp[e.which](e);
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
},
|
|
199
|
-
|
|
200
|
-
_press: function(type, key, onPressKey) {
|
|
201
|
-
var self = this;
|
|
202
|
-
if (typeof key == "string") {
|
|
203
|
-
key = this._rules[key];
|
|
204
|
-
}
|
|
205
|
-
if (key instanceof Array) {
|
|
206
|
-
for (var i=0 ; i < key.length ; i++) {
|
|
207
|
-
assignKey(key[i], type);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
else {
|
|
211
|
-
assignKey(key, type);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (this._lock.canvas) {
|
|
215
|
-
var canvas = this._lock.canvas;
|
|
216
|
-
canvas.onkeydown = onkeydown;
|
|
217
|
-
|
|
218
|
-
canvas.onfocus = function(e) {
|
|
219
|
-
document.onkeydown = function() {
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
if (self._lock.onFocus) self._lock.onFocus(e, canvas);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
canvas.onblur = function(e) {
|
|
226
|
-
document.onkeydown = null;
|
|
227
|
-
if (self._lock.onBlur) self._lock.onBlur(e, canvas);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
else {
|
|
231
|
-
document.onkeydown = onkeydown;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function onkeydown(e) {
|
|
235
|
-
if (!self._keyPress[e.which]) self._keyPress[e.which] = 0;
|
|
236
|
-
self._keyPress[e.which]++;
|
|
237
|
-
if (self._keyPress[e.which] > 1 && self._keyType[e.which] == 'keyPress') return;
|
|
238
|
-
self._keyPressed[e.which] = true;
|
|
239
|
-
// self.keyUp(e.which);
|
|
240
|
-
if (self._keyFunctions[e.which]) self._keyFunctions[e.which](e);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function assignKey(_key, type) {
|
|
244
|
-
self._keyType[_key] = type;
|
|
245
|
-
self._keyFunctions[_key] = onPressKey;
|
|
246
|
-
}
|
|
247
|
-
},
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
@doc keyboard/
|
|
251
|
-
@method reset Resets all keys. You must assign the buttons (press(), keyDown() and keyUp()) to restore movement and actions
|
|
252
|
-
@param {Array} keys (optional) Reset only the keys assigned in the array
|
|
253
|
-
@example
|
|
254
|
-
|
|
255
|
-
canvas.Input.reset([Input.Enter, Input.Space]);
|
|
256
|
-
*/
|
|
257
|
-
reset: function(keys) {
|
|
258
|
-
this._keyPressed = {};
|
|
259
|
-
if (keys) {
|
|
260
|
-
for (var i=0 ; i < keys.length ; i++) {
|
|
261
|
-
this._keyFunctions[keys[i]] = null;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
this._keyFunctions = {};
|
|
266
|
-
}
|
|
267
|
-
},
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
@doc keyboard/
|
|
271
|
-
@method lock Lock the keys on the canvas and avoid scrolling of the page
|
|
272
|
-
@param {HTMLCanvasElement} canvas Canvas
|
|
273
|
-
@param {Boolean} focus_start (optional) Place the focus on the canvas. false by default
|
|
274
|
-
@param {Function} onFocus (optional) Callback when the canvas is the focus
|
|
275
|
-
@param {Function} onBlur (optional) Callback when the canvas loses the focus
|
|
276
|
-
*/
|
|
277
|
-
lock: function(canvas, focus_start, onFocus, onBlur) {
|
|
278
|
-
var dom = document.getElementById(canvas);
|
|
279
|
-
dom.setAttribute('tabindex', 1);
|
|
280
|
-
if (focus_start) {
|
|
281
|
-
dom.focus();
|
|
282
|
-
document.onkeydown = function() {
|
|
283
|
-
return false;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
this._lock.canvas = dom;
|
|
287
|
-
this._lock.onFocus = onFocus;
|
|
288
|
-
this._lock.onBlur = onBlur;
|
|
289
|
-
},
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
@doc keyboard/
|
|
294
|
-
@method isPressed Whether a key is pressed
|
|
295
|
-
@param {Integer} key Value of the key. If it's an array, returns true if a key is pressed
|
|
296
|
-
@return Boolean true if pressed
|
|
297
|
-
*/
|
|
298
|
-
isPressed: function(key) {
|
|
299
|
-
if (!(key instanceof Array)) {
|
|
300
|
-
key = [key];
|
|
301
|
-
}
|
|
302
|
-
for (var i=0 ; i < key.length ; i++) {
|
|
303
|
-
if (this._keyPressed[key[i]]) {
|
|
304
|
-
return true;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
return false;
|
|
308
|
-
},
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
@doc keyboard/
|
|
312
|
-
@method addKey Add key (constant).
|
|
313
|
-
@param {String} id ID key
|
|
314
|
-
@param {Integer} keycode Key value
|
|
315
|
-
@example
|
|
316
|
-
|
|
317
|
-
canvas.Input.addKey("F", 70);
|
|
318
|
-
canvas.Input.press(Input.F, function(e) {
|
|
319
|
-
// Code
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
*/
|
|
323
|
-
addKey: function(id, keycode) {
|
|
324
|
-
Input[id] = keycode;
|
|
325
|
-
},
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
@doc keyboard/
|
|
329
|
-
@method memorize Stores the keys pressed
|
|
330
|
-
*/
|
|
331
|
-
memorize: function() {
|
|
332
|
-
this.cacheKeyBuffer = this.keyBuffer;
|
|
333
|
-
},
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
@doc keyboard/
|
|
337
|
-
@method restore Reassigns the keys pressed cached (see "Input.memorize()")
|
|
338
|
-
*/
|
|
339
|
-
restore: function() {
|
|
340
|
-
this.keyBuffer = this.cacheKeyBuffer;
|
|
341
|
-
},
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
@doc keyboard/
|
|
345
|
-
@method trigger Simulates the call of a key
|
|
346
|
-
@param {Integer} key Key Code (example: Input.Space)
|
|
347
|
-
@param {String} type Type of event key, "down", "up" or "press". "press" is the touch of a key (pressed and then released)
|
|
348
|
-
@param {HTMLCanvasElement} canvas (optional) Canvas. Allows to restore the focus to the canvas
|
|
349
|
-
@example
|
|
350
|
-
|
|
351
|
-
canvas.Input.trigger(Input.A, "press");
|
|
352
|
-
|
|
353
|
-
@example
|
|
354
|
-
|
|
355
|
-
canvas.Input.trigger(Input.Enter, "down", _canvas);
|
|
356
|
-
|
|
357
|
-
*/
|
|
358
|
-
trigger: function(key, type, canvas) {
|
|
359
|
-
var ev, element, dom;
|
|
360
|
-
if (type == "press") {
|
|
361
|
-
this.trigger(key, "down");
|
|
362
|
-
this.trigger(key, "up", canvas);
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
if (this._lock.canvas) {
|
|
366
|
-
element = this._lock.canvas;
|
|
367
|
-
}
|
|
368
|
-
else {
|
|
369
|
-
element = document;
|
|
370
|
-
}
|
|
371
|
-
if (document.createEventObject) {
|
|
372
|
-
ev = document.createEventObject();
|
|
373
|
-
ev.keyCode = key;
|
|
374
|
-
element.fireEvent("onkey" + type, ev);
|
|
375
|
-
}
|
|
376
|
-
else if (document.createEvent) {
|
|
377
|
-
ev = document.createEvent("Events");
|
|
378
|
-
ev.initEvent("key" + type, true, true);
|
|
379
|
-
ev.which = key;
|
|
380
|
-
element.dispatchEvent(ev);
|
|
381
|
-
}
|
|
382
|
-
if (canvas) {
|
|
383
|
-
dom = document.getElementById(canvas.id + '-dom');
|
|
384
|
-
dom.focus();
|
|
385
|
-
}
|
|
386
|
-
},
|
|
387
|
-
|
|
388
|
-
// TODO
|
|
389
|
-
addRule: function(name, inputs) {
|
|
390
|
-
this._rules[name] = inputs;
|
|
391
|
-
},
|
|
392
|
-
/**
|
|
393
|
-
@doc gamepad
|
|
394
|
-
@class Gamepad Can play with the gamepad
|
|
395
|
-
@example
|
|
396
|
-
In method "ready" of the scene :
|
|
397
|
-
|
|
398
|
-
var canvas = CE.defines("canvas_id").
|
|
399
|
-
extend(Input).
|
|
400
|
-
ready(function() {
|
|
401
|
-
canvas.Scene.call("MyScene");
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
canvas.Scene.new({
|
|
405
|
-
name: "MyScene",
|
|
406
|
-
ready: function(stage) {
|
|
407
|
-
this.gamepad = canvas.Input.Gamepad.init(function() {
|
|
408
|
-
console.log("Gamepad connected");
|
|
409
|
-
}, function() {
|
|
410
|
-
console.log("Gamepad disconnected");
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
this.gamepad.addListener("faceButton0", function() {
|
|
414
|
-
console.log("key A down");
|
|
415
|
-
}, function() {
|
|
416
|
-
console.log("key A up");
|
|
417
|
-
});
|
|
418
|
-
},
|
|
419
|
-
render: function(stage) {
|
|
420
|
-
this.gamepad.update();
|
|
421
|
-
}
|
|
422
|
-
});
|
|
423
|
-
|
|
424
|
-
*/
|
|
425
|
-
Gamepad: {
|
|
426
|
-
_listener: {},
|
|
427
|
-
gamepad: null,
|
|
428
|
-
_onConnect: null,
|
|
429
|
-
_onDisconnect: null,
|
|
430
|
-
_connectState: false,
|
|
431
|
-
/**
|
|
432
|
-
@doc gamepad/
|
|
433
|
-
@method init Initialize the gamepad
|
|
434
|
-
@param {Function} onConnect (optional) Callback function when the gamepad is connected
|
|
435
|
-
@param {Function} onDisconnect (optional) Callback function when the gamepad is disconnected
|
|
436
|
-
@return {CanvasEngine.Input.Gamepad}
|
|
437
|
-
*/
|
|
438
|
-
init: function(onConnect, onDisconnect) {
|
|
439
|
-
this._onConnect = onConnect;
|
|
440
|
-
this._onDisconnect = onDisconnect;
|
|
441
|
-
return this;
|
|
442
|
-
},
|
|
443
|
-
// Private
|
|
444
|
-
getState: function(pos) {
|
|
445
|
-
this.gamepad = Gamepad.getStates()[pos];
|
|
446
|
-
if (this.gamepad && !this._connectState) {
|
|
447
|
-
if (this._onConnect) this._onConnect();
|
|
448
|
-
this._connectState = true;
|
|
449
|
-
}
|
|
450
|
-
else if (!this.gamepad && this._connectState) {
|
|
451
|
-
if (this._onDisconnect) this._onDisconnect();
|
|
452
|
-
this._connectState = false;
|
|
453
|
-
}
|
|
454
|
-
},
|
|
455
|
-
/**
|
|
456
|
-
@doc gamepad/
|
|
457
|
-
@method addListener Adds a listener when a button is pressed or released
|
|
458
|
-
@param {String} Id button. See https://github.com/sgraham/gamepad.js
|
|
459
|
-
@param {Function} onDown Callback function when the button is pressed
|
|
460
|
-
@param {Function} onUp Callback function when the button is released
|
|
461
|
-
*/
|
|
462
|
-
/**
|
|
463
|
-
@doc gamepad/
|
|
464
|
-
@method addListener Execute a function already defined on the keyboard
|
|
465
|
-
@param {String} Id button. See https://github.com/sgraham/gamepad.js
|
|
466
|
-
@param {Integer} input Value of the key on the keyboard
|
|
467
|
-
@example
|
|
468
|
-
|
|
469
|
-
this.gamepad = canvas.Input.Gamepad.init();
|
|
470
|
-
canvas.Input.keyDown(Input.Right, function() {
|
|
471
|
-
// Code
|
|
472
|
-
});
|
|
473
|
-
this.gamepad.addListener("dpadRight", Input.Right);
|
|
474
|
-
|
|
475
|
-
*/
|
|
476
|
-
addListener: function(obj, onDown, onUp) {
|
|
477
|
-
var _Input = Input.Input;
|
|
478
|
-
if (typeof onDown != "function") {
|
|
479
|
-
var _input = onDown;
|
|
480
|
-
onDown = function() {
|
|
481
|
-
_Input.trigger(_input, "down");
|
|
482
|
-
};
|
|
483
|
-
onUp = function() {
|
|
484
|
-
_Input.trigger(_input, "up");
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
this._listener[obj] = {
|
|
488
|
-
onDown: onDown,
|
|
489
|
-
onUp: onUp,
|
|
490
|
-
state: false
|
|
491
|
-
};
|
|
492
|
-
},
|
|
493
|
-
/**
|
|
494
|
-
@doc gamepad/
|
|
495
|
-
@method update Updates the inputs of the gamepad
|
|
496
|
-
*/
|
|
497
|
-
update: function() {
|
|
498
|
-
this.getState(0);
|
|
499
|
-
if (!this.gamepad) return;
|
|
500
|
-
for (var key in this._listener) {
|
|
501
|
-
if (this.gamepad[key] == 1 && !this._listener[key].state) {
|
|
502
|
-
if (this._listener[key].onDown) this._listener[key].onDown();
|
|
503
|
-
this._listener[key].state = true;
|
|
504
|
-
}
|
|
505
|
-
else if (this.gamepad[key] == 0 && this._listener[key].state) {
|
|
506
|
-
if (this._listener[key].onUp) this._listener[key].onUp();
|
|
507
|
-
this._listener[key].state = false;
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
},
|
|
512
|
-
|
|
513
|
-
/**
|
|
514
|
-
@doc accelerometer/
|
|
515
|
-
@method accelerometer Assigns the accelerometer
|
|
516
|
-
@param {Function} callback Callback function called with "deviceorientation" listener. 3 parameters :
|
|
517
|
-
|
|
518
|
-
* x : Direction side to side
|
|
519
|
-
* y : Vertically oriented front-back
|
|
520
|
-
* z : Front and rear horizontal orientation
|
|
521
|
-
|
|
522
|
-
@example
|
|
523
|
-
|
|
524
|
-
canvas.Input.accelerometer(function(x, y, z) {
|
|
525
|
-
|
|
526
|
-
});
|
|
527
|
-
|
|
528
|
-
*/
|
|
529
|
-
accelerometer: function(callback) {
|
|
530
|
-
if (window.DeviceOrientationEvent) {
|
|
531
|
-
window.addEventListener('deviceorientation', function(eventData) {
|
|
532
|
-
orientationHandler(eventData.alpha, eventData.beta, eventData.gamma);
|
|
533
|
-
}, false);
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
}
|
|
537
|
-
else if (window.OrientationEvent) {
|
|
538
|
-
window.addEventListener('MozOrientation', function(eventData) {
|
|
539
|
-
orientationHandler(eventData.x, eventData.y, eventData.z);
|
|
540
|
-
}, false);
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
function orientationHandler(x, y, z) {
|
|
544
|
-
callback(x, y, z);
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
}};
|
|
548
|
-
|
|
549
|
-
/**
|
|
550
|
-
@doc keyboard/
|
|
551
|
-
@static
|
|
552
|
-
@property A Value of the A button
|
|
553
|
-
@type Integer
|
|
554
|
-
*/
|
|
555
|
-
Input.A = 65;
|
|
556
|
-
|
|
557
|
-
/**
|
|
558
|
-
@doc keyboard/
|
|
559
|
-
@static
|
|
560
|
-
@property Z Value of the Z button
|
|
561
|
-
@type Integer
|
|
562
|
-
*/
|
|
563
|
-
Input.Z = 90;
|
|
564
|
-
|
|
565
|
-
/**
|
|
566
|
-
@doc keyboard/
|
|
567
|
-
@static
|
|
568
|
-
@property E Value of the E button
|
|
569
|
-
@type Integer
|
|
570
|
-
*/
|
|
571
|
-
Input.E = 69;
|
|
572
|
-
|
|
573
|
-
/**
|
|
574
|
-
@doc keyboard/
|
|
575
|
-
@static
|
|
576
|
-
@property Q Value of the Q button
|
|
577
|
-
@type Integer
|
|
578
|
-
*/
|
|
579
|
-
Input.Q = 81;
|
|
580
|
-
|
|
581
|
-
/**
|
|
582
|
-
@doc keyboard/
|
|
583
|
-
@static
|
|
584
|
-
@property Esc Value of the Escape button
|
|
585
|
-
@type Integer
|
|
586
|
-
*/
|
|
587
|
-
Input.Esc = 27;
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
@doc keyboard/
|
|
591
|
-
@static
|
|
592
|
-
@property Enter Value of the Enter button
|
|
593
|
-
@type Integer
|
|
594
|
-
*/
|
|
595
|
-
Input.Enter = 13;
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
@doc keyboard/
|
|
599
|
-
@static
|
|
600
|
-
@property Shift Value of the Shift button
|
|
601
|
-
@type Integer
|
|
602
|
-
*/
|
|
603
|
-
Input.Shift = 16;
|
|
604
|
-
|
|
605
|
-
/**
|
|
606
|
-
@doc keyboard/
|
|
607
|
-
@static
|
|
608
|
-
@property Ctrl Value of the Ctrl button
|
|
609
|
-
@type Integer
|
|
610
|
-
*/
|
|
611
|
-
Input.Ctrl = 17;
|
|
612
|
-
|
|
613
|
-
/**
|
|
614
|
-
@doc keyboard/
|
|
615
|
-
@static
|
|
616
|
-
@property Alt Value of the Alt button
|
|
617
|
-
@type Integer
|
|
618
|
-
*/
|
|
619
|
-
Input.Alt = 18;
|
|
620
|
-
|
|
621
|
-
/**
|
|
622
|
-
@doc keyboard/
|
|
623
|
-
@static
|
|
624
|
-
@property Space Value of the Space button
|
|
625
|
-
@type Integer
|
|
626
|
-
*/
|
|
627
|
-
Input.Space = 32;
|
|
628
|
-
|
|
629
|
-
/**
|
|
630
|
-
@doc keyboard/
|
|
631
|
-
@static
|
|
632
|
-
@property Back Value of the Back button
|
|
633
|
-
@type Integer
|
|
634
|
-
*/
|
|
635
|
-
Input.Back = 8;
|
|
636
|
-
|
|
637
|
-
/**
|
|
638
|
-
@doc keyboard/
|
|
639
|
-
@static
|
|
640
|
-
@property F1 Value of the F1 button
|
|
641
|
-
@type Integer
|
|
642
|
-
*/
|
|
643
|
-
Input.F1 = 112;
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
@doc keyboard/
|
|
647
|
-
@static
|
|
648
|
-
@property F2 Value of the F2 button
|
|
649
|
-
@type Integer
|
|
650
|
-
*/
|
|
651
|
-
Input.F2 = 113;
|
|
652
|
-
|
|
653
|
-
/**
|
|
654
|
-
@doc keyboard/
|
|
655
|
-
@static
|
|
656
|
-
@property F11 Value of the F11 button
|
|
657
|
-
@type Integer
|
|
658
|
-
*/
|
|
659
|
-
Input.F11 = 122;
|
|
660
|
-
|
|
661
|
-
/**
|
|
662
|
-
@doc keyboard/
|
|
663
|
-
@static
|
|
664
|
-
@property F12 Value of the F12 button
|
|
665
|
-
@type Integer
|
|
666
|
-
*/
|
|
667
|
-
Input.F12 = 123;
|
|
668
|
-
|
|
669
|
-
/**
|
|
670
|
-
@doc keyboard/
|
|
671
|
-
@static
|
|
672
|
-
@property Left Value of the left button
|
|
673
|
-
@type Integer
|
|
674
|
-
*/
|
|
675
|
-
Input.Left = 37;
|
|
676
|
-
|
|
677
|
-
/**
|
|
678
|
-
@doc keyboard/
|
|
679
|
-
@static
|
|
680
|
-
@property Up Value of the Up button
|
|
681
|
-
@type Integer
|
|
682
|
-
*/
|
|
683
|
-
Input.Up = 38;
|
|
684
|
-
|
|
685
|
-
/**
|
|
686
|
-
@doc keyboard/
|
|
687
|
-
@static
|
|
688
|
-
@property Right Value of the Right button
|
|
689
|
-
@type Integer
|
|
690
|
-
*/
|
|
691
|
-
Input.Right = 39;
|
|
692
|
-
|
|
693
|
-
/**
|
|
694
|
-
@doc keyboard/
|
|
695
|
-
@static
|
|
696
|
-
@property Bottom Value of the Bottom button
|
|
697
|
-
@type Integer
|
|
698
|
-
*/
|
|
699
|
-
Input.Bottom = 40;
|