-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha2d.js
334 lines (323 loc) · 10.4 KB
/
a2d.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
/*global document, window */
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
try {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
} catch(e) {
//catching what javascriptcore considers an illegal use of instanceof
return fToBind.apply(oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
}
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
/**
* A happy namespace for all game engine stuffs.
* @namespace
* @augments a2d.Events
* */
var a2d = {
/** a2d engine version */
version: "0.4.0.2",
/** @private */
a2dCanvas: null,
a2dRoot: null,
a2dFullSCreen: false,
//a2dOffset: null,
forceClear: false,
resources: [],
loaded: false,
logicInterval: 30,
/* offset: {
X: 0,
Y: 0
},*/
mute: false,
/**
* Load resources.
* @param {object} loadData A key->value collection where key is the name of the resource, and value the path.
*/
load: function (loadData) {
var name;
for (name in loadData) {
if(loadData.hasOwnProperty(name)) {
if (loadData[name].match(/png$|jpg$|jpeg$|gif$|bmp$/i)) {
this.resources[name] = new Image();
} else {
this.resources[name] = new a2d.Audio();
}
this.resources[name].onload = this.progress;
this.resources[name].onreadystatechange = function() { this.progress(); };
this.resources[name].src = loadData[name];
}
}
this.progress();
},
/** @private */
progress: function () {
var name,
total = 0,
c = 0;
if(!a2d.loaded) {
for (name in a2d.resources) {
if(a2d.resources.hasOwnProperty(name)) {
if((a2d.resources[name].width && a2d.resources[name].width > 0) || a2d.resources[name].audioLoaded /*|| (a2d.resources[name].play)*/) {
c++;
}
total++;
}
}
if(c == total) {
a2d.loaded = true;
a2d.fireEvent("load");
} else {
a2d.fireEvent("progress", { loaded : c, total: total });
}
}
},
/**
* Get or set the canvas.
* Assigning a string to this property will attempt to find the canvas with the given string as an ID.
* If no canvas is set, an attempt is made to "auto-detect" a canvas in the DOM. If not found, one will be
* created and added to the DOM.
* @property {HTMLCanvasElement} canvas
* @name a2d#canvas
*/
get canvas() {
if(!this.a2dCanvas){
console.log("acquire canvas");
if(document && document.getElementsByTagName) {
this.a2dCanvas = document.getElementsByTagName("canvas")[0];
}
//this is only the initial setup of the canvas element and its events
if(!this.a2dCanvas) {
this.a2dCanvas = document.createElement("canvas");
document.body.appendChild(this.a2dCanvas);
var windowSize = new a2d.Dimension(window.innerWidth, window.innerHeight);
//this.setSize(windowSize);
//this.a2dCanvas.setAttribute("width", windowSize.Width);
//this.a2dCanvas.setAttribute("height", windowSize.Height);
this.a2dCanvas.width = windowSize.Width;
this.a2dCanvas.height = windowSize.Height;
}
a2d.mousePosition = new a2d.Position(0, 0);
this.a2dCanvas.addEventListener("mousemove", function(e) {
var x = e.clientX + a2d.canvas.offsetLeft;
var y = e.clientY + a2d.canvas.offsetTop;
a2d.mousePosition = new a2d.Position(x, y);
//a2d.mousePosition.subtract(a2d.offset);
});
this.a2dCanvas.addEventListener("mousedown", function(e) {
var clickedNode = a2d.root.findNodeAt(a2d.mousePosition);
if(clickedNode) {
clickedNode.fireEvent.call(clickedNode, "mousedown");
}
});
this.a2dCanvas.addEventListener("mouseup", function(e) {
var clickedNode = a2d.root.findNodeAt(a2d.mousePosition);
if(clickedNode) {
clickedNode.fireEvent.call(clickedNode, "mouseup");
clickedNode.fireEvent.call(clickedNode, "click");
}
});
window.addEventListener("resize", this.onResize);
this.context = this.a2dCanvas.getContext("2d");
}
return this.a2dCanvas;
},
onResize: function() {
if(this.a2dCanvas) {
this.a2dCanvas.style.position = "absolute";
this.a2dCanvas.style.width = "100%";
this.a2dCanvas.style.height = "100%";
this.a2dCanvas.style.top = "0";
this.a2dCanvas.style.left = "0";
this.dimension = new a2d.Dimension( parseInt(getComputedStyle(canvas, null).getPropertyCSSValue("height").cssText, 10),
parseInt(getComputedStyle(canvas, null).getPropertyCSSValue("width").cssText, 10));
}
},
set canvas(canvasID){
this.a2dCanvas = document.getElementById(canvasID);
},
/**
* The canvas size. You can resize the canvas by setting this.
* @property {a2d.Dimension} the canvas size.
* @name a2d#dimension
*/
get dimension() {
return new a2d.Dimension(this.canvas.width, this.canvas.height);
},
set dimension(newsize) {
this.canvas.setAttribute("width", newsize.Width);
this.canvas.setAttribute("height", newsize.Height);
},
/**
* The root sceneNode. Attach nodes you want displayed to this.
* @property {a2d.SceneNode} the root scene node.
* @name a2d#root
*/
get root() {
if(!this.a2dRoot){
console.log("acquire root node");
this.a2dRoot = new a2d.Node();
this.frame();
//this.update();
}
return this.a2dRoot;
},
set fullscreen(b) {
this.a2dFullScreen = b;
if(b) {
if (this.canvas.requestFullScreen) {
this.canvas.requestFullScreen();
} else if (this.canvas.mozRequestFullScreen) {
this.canvas.mozRequestFullScreen();
} else if (this.canvas.webkitRequestFullScreen) {
this.canvas.webkitRequestFullScreen();
}
}
this.onResize();
},
get fullscreen() {
return this.a2dFullScreen;
},
set root(node) {
this.a2dRoot = node;
},
/** @private */
update: function() {
//a2d.fireEvent("update");
setTimeout(a2d.update, a2d.logicInterval);
},
/** @private */
frame : function () {
if(a2d.forceClear && a2d.context) {
a2d.context.clearRect(0, 0, a2d.dimension.Width, a2d.dimension.Height);
//a2d.canvas.width = a2d.canvas.width;
}
a2d.requestFrame(a2d.frame);
//window.webkitRequestAnimationFrame(a2d.frame);
a2d.root.draw();
a2d.fireEvent("draw");
},
/**
* Request animation frame.
* Browser-specific animation frame, or 60fps timeout callback.
* @param {Function} function to be executed when an animation frame is ready.
*/
requestFrame: (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
}()).bind(window),
/**
* named keys
* @namespace
*/
key: {
/** @constant */
BACKSPACE:8,
/** @constant */
TAB:9,
/** @constant */
ENTER:13,
/** @constant */
SHIFT:16,
/** @constant */
CTRL:17,
/** @constant */
ALT:18,
/** @constant */
PAUSE:19,
/** @constant */
CAPS_LOCK:20,
/** @constant */
ESC:27,
/** @constant */
SPACE:32,
/** @constant */
PAGEUP:33,
/** @constant */
PAGEDOWN:34,
/** @constant */
END:35,
/** @constant */
HOME:36,
/** @constant */
ARROW_LEFT:37,
/** @constant */
ARROW_UP:38,
/** @constant */
ARROW_RIGHT:39,
/** @constant */
ARROW_DOWN:40,
/** @constant */
INSERT:45,
/** @constant */
DELETE:46,
/** @constant */
F1:112,
/** @constant */
F2:113,
/** @constant */
F3:114,
/** @constant */
F4:115,
/** @constant */
F5:116,
/** @constant */
F6:117,
/** @constant */
F7:118,
/** @constant */
F8:119,
/** @constant */
F9:120,
/** @constant */
F10:121,
/** @constant */
F11:122,
/** @constant */
F12:123,
/** @constant */
NUM_LOCK:144,
/** @constant */
SCROLL_LOCK:145
}
};
/**
* fired when a frame is drawn.
* @event
* @name a2d#draw
*/
/**
* fired when all resources have been loaded.
* @event
* @name a2d#load
*/
/**
* fired when progress is made loading resources.
* @event
* @name a2d#progress
* @param {object} eventObject
* @param {number} eventObject.loaded the number of resources loaded
* @param {number} eventObject.total the total number of resources
*/