This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Window.js
328 lines (286 loc) · 10.3 KB
/
Window.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
var Context = require('pex-context/Context');
var isPlask = require('is-plask');
var EventDispatcher = require('./EventDispatcher');
var WindowEvent = require('./WindowEvent');
var ResourceLoader = require('./ResourceLoader');
var WindowImpl = isPlask ? require('./WindowImplPlask') : require('./WindowImplBrowser');
var Time = require('./Time');
var Mouse = require('./Mouse');
var MouseEvent = require('./MouseEvent');
var Keyboard = require('./Keyboard');
var KeyboardEvent = require('./KeyboardEvent');
var current = null;
var ListenerCallbackMethod = {
MOUSE_DOWN : 'onMouseDown',
MOUSE_UP : 'onMouseUp',
MOUSE_DRAG : 'onMouseDrag',
MOUSE_MOVE : 'onMouseMove',
MOUSE_SCROLL : 'onMouseScroll',
KEY_DOWN : 'onKeyDown',
KEY_PRESS : 'onKeyPress',
KEY_UP : 'onKeyUp',
WINDOW_RESIZE : 'onWindowResize'
};
function Window(){
EventDispatcher.call(this);
this._impl = null;
this._ctx = null;
this._resources = {};
this._time = new Time();
this._mouse = new Mouse();
this._keyboard = new Keyboard();
}
Window.prototype = Object.create(EventDispatcher.prototype);
Window.prototype.constructor = Window;
Window.prototype.setSize = function(width,height,pixelRatio){
this._impl.setSize(width,height,pixelRatio);
this.dispatchEvent(new WindowEvent(WindowEvent.WINDOW_RESIZE, {
width : this.getWidth(),
height : this.getHeight(),
pixelRatio: this.getPixelRatio()
}))
};
/**
* Get current window (or canvas) size
* @param {Array} array to put data into
* @return {Array} [width, height]
*/
Window.prototype.getSize = function(out){
out = out || new Array(2);
out[0] = this._impl.width;
out[1] = this._impl.height;
return out;
};
/**
* Get current window (or canvas) width in px
* @return {Number}
*/
Window.prototype.getWidth = function(){
return this._impl.width;
};
/**
* Get current window (or canvas) height in px
* @return {Number}
*/
Window.prototype.getHeight = function(){
return this._impl.height;
};
/**
* Get current window (or canvas) aspect ratio
* @return {Number}
*/
Window.prototype.getAspectRatio = function(){
return this._impl.width / this._impl.height;
};
Window.prototype.getPixelRatio = function(){
return this._impl.pixelRatio;
};
/**
* Get current window (or canvas) bounds
* @param {Array} array to put data into
* @return {Array} [0, 0, width, height]
*/
Window.prototype.getBounds = function(out){
out = out || new Array(4);
out[0] = out[1] = 0;
out[2] = this.getWidth();
out[3] = this.getHeight();
return out;
};
Window.prototype.setFullScreen = function(enable){
this._impl.setFullScreen(enable);
};
Window.prototype.isFullScreen = function(){
return this._impl.fullScreen;
};
Window.prototype.toggleFullScreen = function(){
this.isFullScreen() ? this.setFullScreen(false) : this.setFullScreen(true);
};
/**
* Get WebGL Context instance associated with this window or canvas
* @return {Context}
*/
Window.prototype.getContext = function(){
return this._ctx;
};
/**
* Get loaded resources
* @return {Object} @see ResourceLoader
*/
Window.prototype.getResources = function(){
return this._resources;
};
/**
* Get Time instance associated with this window or canvas
* @return {Time}
*/
Window.prototype.getTime = function(){
return this._time;
};
/**
* Get Mouse instance associated with this window or canvas
* @return {Mouse}
*/
Window.prototype.getMouse = function(){
return this._mouse;
};
/**
* Get Keyboard instance associated with this window or canvas
* @return {Keyboard}
*/
Window.prototype.getKeyboard = function(){
return this._keyboard;
};
Window.prototype._addEventListener = Window.prototype.addEventListener;
/**
* Helper method for registering multiple event listeners at once
* @param {Object|String} listenerObjOrType
* @param {Function} listenerObjOrType.onMouseDown
* @param {Function} listenerObjOrType.onMouseUp
* @param {Function} listenerObjOrType.onMouseDrag
* @param {Function} listenerObjOrType.onMouseMove
* @param {Function} listenerObjOrType.onMouseScroll
* @param {Function} listenerObjOrType.onKeyDown
* @param {Function} listenerObjOrType.onKeyPress
* @param {Function} listenerObjOrType.onKeyUp
* @param {Function} listenerObjOrType.onWindowResize
* @param {Function} [calback] callback function requried type is String
*/
Window.prototype.addEventListener = function(listenerObjOrType, method){
if(method === undefined){
if(listenerObjOrType === null || typeof listenerObjOrType !== 'object'){
throw new Error('Invalid listener object.');
}
var mouse = this._mouse;
var keyboard = this._keyboard;
for(var p in listenerObjOrType){
if(typeof listenerObjOrType[p] !== 'function'){
continue;
}
var func = listenerObjOrType[p];
switch (p){
case ListenerCallbackMethod.MOUSE_DOWN :
mouse.addEventListener(MouseEvent.MOUSE_DOWN,func.bind(listenerObjOrType));
break;
case ListenerCallbackMethod.MOUSE_UP :
mouse.addEventListener(MouseEvent.MOUSE_UP,func.bind(listenerObjOrType));
break;
case ListenerCallbackMethod.MOUSE_DRAG :
mouse.addEventListener(MouseEvent.MOUSE_DRAG,func.bind(listenerObjOrType));
break;
case ListenerCallbackMethod.MOUSE_MOVE :
mouse.addEventListener(MouseEvent.MOUSE_MOVE,func.bind(listenerObjOrType));
break;
case ListenerCallbackMethod.MOUSE_SCROLL:
mouse.addEventListener(MouseEvent.MOUSE_SCROLL,func.bind(listenerObjOrType));
break;
case ListenerCallbackMethod.KEY_DOWN :
keyboard.addEventListener(KeyboardEvent.KEY_DOWN,func.bind(listenerObjOrType));
break;
case ListenerCallbackMethod.KEY_PRESS :
keyboard.addEventListener(KeyboardEvent.KEY_PRESS,func.bind(listenerObjOrType));
break;
case ListenerCallbackMethod.KEY_UP :
keyboard.addEventListener(KeyboardEvent.KEY_UP,func.bind(listenerObjOrType));
break;
case ListenerCallbackMethod.WINDOW_RESIZE :
this.addEventListener(WindowEvent.WINDOW_RESIZE, func.bind(listenerObjOrType));
break;
}
}
return;
}
this._addEventListener(listenerObjOrType,method);
};
/**
* Create new window instance
* @param {Object} obj
* @param {Object} [obj.resources] - resources to load before init using @see ResourceLoader
* @param {Object} obj.init - Function called once after the resources finised loading and WebGL context has been created.
* @param {Object} obj.draw - Function called every frame
*/
Window.create = function(obj){
var window = new Window();
for(var p in obj){
window[p] = obj[p];
}
window.resources = window.resources || {};
var settings = obj.settings;
settings.width = settings.width || 1280;
settings.height = settings.height || 720;
settings.pixelRatio = settings.pixelRatio || 1;
settings.fullScreen = settings.fullScreen || false;
settings.debug = settings.debug || false;
function initWindowImpl(){
var mouse = window._mouse;
var keyboard = window._keyboard;
//Wait with adding event listeners until the app initializes
//This allow a GUI or an Arcball controller to get priority over e.g. onMouseDown
function addWindowEventListeners() {
if(window.onMouseDown){
mouse.addEventListener(MouseEvent.MOUSE_DOWN, window.onMouseDown.bind(window));
}
if(window.onMouseUp){
mouse.addEventListener(MouseEvent.MOUSE_UP, window.onMouseUp.bind(window));
}
if(window.onMouseMove){
mouse.addEventListener(MouseEvent.MOUSE_MOVE, window.onMouseMove.bind(window));
}
if(window.onMouseDrag){
mouse.addEventListener(MouseEvent.MOUSE_DRAG, window.onMouseDrag.bind(window));
}
if(window.onMouseScroll){
mouse.addEventListener(MouseEvent.MOUSE_SCROLL, window.onMouseScroll.bind(window));
}
if(window.onKeyDown){
keyboard.addEventListener(KeyboardEvent.KEY_DOWN, window.onKeyDown.bind(window));
}
if(window.onKeyPress){
keyboard.addEventListener(KeyboardEvent.KEY_PRESS, window.onKeyPress.bind(window));
}
if(window.onKeyUp){
keyboard.addEventListener(KeyboardEvent.KEY_UP, window.onKeyUp.bind(window));
}
if(window.onWindowResize) {
window.addEventListener(WindowEvent.WINDOW_RESIZE, window.onWindowResize.bind(window));
}
}
var impl = WindowImpl.create(window, settings);
impl.addEventListener(WindowEvent.WINDOW_RESIZE, function(e) {
window.dispatchEvent(new WindowEvent(WindowEvent.WINDOW_RESIZE, e))
});
impl.addEventListener(WindowEvent.WINDOW_READY, function() {
// start counting from init, not from window creation
window._time._restart()
if (settings.debug) {
try {
window.init();
}
catch (e) {
console.log('Window.init error:', e);
console.log('Window.init error stack:', e.stack);
if (isPlask) {
process.exit(-1)
}
}
}
else {
window.init()
}
addWindowEventListeners();
}.bind(this));
window._impl = impl;
}
ResourceLoader.load(window.resources, function(err, res){
if(err){
console.log('Window.create failed loading resources');
console.log(err);
}
else {
window._resources = res;
delete window.resources;
initWindowImpl();
}
});
};
module.exports = Window;