Skip to content

Commit 0b1bcaf

Browse files
committed
Set the default camera to direct itself to the Stage.
1 parent f9776f3 commit 0b1bcaf

19 files changed

Lines changed: 375 additions & 171 deletions

File tree

Phaser/Game.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ var Phaser;
175175
//this.physics = new Phaser.Physics.Manager(this);
176176
this.plugins = new Phaser.PluginManager(this, this);
177177

178-
this.load.onLoadComplete.addOnce(this.loadComplete, this);
178+
this.load.onLoadComplete.add(this.loadComplete, this);
179179

180180
this.setRenderer(Phaser.Types.RENDERER_CANVAS);
181181

@@ -301,6 +301,7 @@ var Phaser;
301301
this._loadComplete = true;
302302
} else {
303303
// Start the loader going as we have something in the queue
304+
this.load.onLoadComplete.add(this.loadComplete, this);
304305
this.load.start();
305306
}
306307
} else {

Phaser/Game.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,6 @@ module Phaser {
387387

388388
}
389389

390-
private emptyCallback() {
391-
// Called by onUpdateCallback etc
392-
}
393-
394390
/**
395391
* Game loop method will be called when it's running.
396392
*/

Phaser/Stage.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,15 @@ var Phaser;
5656
this.canvas = document.createElement('canvas');
5757
this.canvas.width = width;
5858
this.canvas.height = height;
59+
this.context = this.canvas.getContext('2d');
5960

60-
if ((parent !== '' || parent !== null) && document.getElementById(parent)) {
61-
document.getElementById(parent).appendChild(this.canvas);
62-
document.getElementById(parent).style.overflow = 'hidden';
63-
} else {
64-
document.body.appendChild(this.canvas);
65-
}
61+
Phaser.CanvasUtils.addToDOM(this.canvas, parent, true);
62+
Phaser.CanvasUtils.setTouchAction(this.canvas);
6663

67-
// Consume default actions on the canvas
68-
this.canvas.style.msTouchAction = 'none';
69-
this.canvas.style['ms-touch-action'] = 'none';
70-
this.canvas.style['touch-action'] = 'none';
71-
this.canvas.style.backgroundColor = 'rgb(0,0,0)';
7264
this.canvas.oncontextmenu = function (event) {
7365
event.preventDefault();
7466
};
7567

76-
this.context = this.canvas.getContext('2d');
77-
7868
this.css3 = new Phaser.Display.CSS3Filters(this.canvas);
7969

8070
this.scaleMode = Phaser.StageScaleMode.NO_SCALE;
@@ -125,7 +115,7 @@ var Phaser;
125115

126116
if (this.clear || (this.game.paused && this.disablePauseScreen == false)) {
127117
if (this.game.device.patchAndroidClearRectBug) {
128-
this.context.fillStyle = 'rgb(0,0,0)';
118+
this.context.fillStyle = this._backgroundColor;
129119
this.context.fillRect(0, 0, this.width, this.height);
130120
} else {
131121
this.context.clearRect(0, 0, this.width, this.height);
@@ -185,13 +175,6 @@ var Phaser;
185175
}
186176
};
187177

188-
Stage.prototype.setImageRenderingCrisp = function () {
189-
this.canvas.style['image-rendering'] = 'crisp-edges';
190-
this.canvas.style['image-rendering'] = '-moz-crisp-edges';
191-
this.canvas.style['image-rendering'] = '-webkit-optimize-contrast';
192-
this.canvas.style['-ms-interpolation-mode'] = 'nearest-neighbor';
193-
};
194-
195178
Stage.prototype.pauseGame = function () {
196179
this.game.paused = true;
197180

@@ -250,7 +233,7 @@ var Phaser;
250233
this.context.fillStyle = this.fillStyle;
251234

252235
if (this.game.device.patchAndroidClearRectBug) {
253-
this.context.fillStyle = 'rgb(0,0,0)';
236+
this.context.fillStyle = this._backgroundColor;
254237
this.context.fillRect(0, 0, this.width, this.height);
255238
} else {
256239
this.context.clearRect(0, 0, this.width, this.height);

Phaser/Stage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ module Phaser {
188188
{
189189
if (this.game.device.patchAndroidClearRectBug)
190190
{
191-
this.context.fillStyle = 'rgb(0,0,0)';
191+
this.context.fillStyle = this._backgroundColor;
192192
this.context.fillRect(0, 0, this.width, this.height);
193193
}
194194
else
@@ -357,7 +357,7 @@ module Phaser {
357357

358358
if (this.game.device.patchAndroidClearRectBug)
359359
{
360-
this.context.fillStyle = 'rgb(0,0,0)';
360+
this.context.fillStyle = this._backgroundColor;
361361
this.context.fillRect(0, 0, this.width, this.height);
362362
}
363363
else

Phaser/cameras/Camera.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ var Phaser;
6565
this.texture = new Phaser.Display.Texture(this);
6666

6767
// We create a hidden canvas for our camera the size of the game (we use the screenView to clip the render to the camera size)
68-
this.texture.canvas = document.createElement('canvas');
69-
this.texture.canvas.width = width;
70-
this.texture.canvas.height = height;
68+
this._canvas = document.createElement('canvas');
69+
this._canvas.width = width;
70+
this._canvas.height = height;
71+
this._renderLocal = true;
72+
this.texture.canvas = this._canvas;
7173
this.texture.context = this.texture.canvas.getContext('2d');
74+
this.texture.backgroundColor = this.game.stage.backgroundColor;
7275

7376
// Handy proxies
7477
this.scale = this.transform.scale;
@@ -94,6 +97,24 @@ var Phaser;
9497
configurable: true
9598
});
9699

100+
Object.defineProperty(Camera.prototype, "directToStage", {
101+
set: function (value) {
102+
if (value) {
103+
this._renderLocal = false;
104+
this.texture.canvas = this.game.stage.canvas;
105+
Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.game.stage.backgroundColor);
106+
} else {
107+
this._renderLocal = true;
108+
this.texture.canvas = this._canvas;
109+
Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.texture.backgroundColor);
110+
}
111+
112+
this.texture.context = this.texture.canvas.getContext('2d');
113+
},
114+
enumerable: true,
115+
configurable: true
116+
});
117+
97118
/**
98119
* Hides an object from this Camera. Hidden objects are not rendered.
99120
* The object must implement a public cameraBlacklist property.

Phaser/cameras/Camera.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ module Phaser {
4545
this.texture = new Phaser.Display.Texture(this);
4646

4747
// We create a hidden canvas for our camera the size of the game (we use the screenView to clip the render to the camera size)
48-
this.texture.canvas = <HTMLCanvasElement> document.createElement('canvas');
49-
this.texture.canvas.width = width;
50-
this.texture.canvas.height = height;
48+
this._canvas = <HTMLCanvasElement> document.createElement('canvas');
49+
this._canvas.width = width;
50+
this._canvas.height = height;
51+
this._renderLocal = true;
52+
this.texture.canvas = this._canvas;
5153
this.texture.context = this.texture.canvas.getContext('2d');
54+
this.texture.backgroundColor = this.game.stage.backgroundColor;
5255

5356
// Handy proxies
5457
this.scale = this.transform.scale;
@@ -58,6 +61,8 @@ module Phaser {
5861

5962
}
6063

64+
private _renderLocal: boolean;
65+
private _canvas: HTMLCanvasElement;
6166
private _target: Sprite = null;
6267

6368
/**
@@ -160,6 +165,25 @@ module Phaser {
160165
*/
161166
public z: number = -1;
162167

168+
public set directToStage(value: boolean) {
169+
170+
if (value)
171+
{
172+
this._renderLocal = false;
173+
this.texture.canvas = this.game.stage.canvas;
174+
Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.game.stage.backgroundColor);
175+
}
176+
else
177+
{
178+
this._renderLocal = true;
179+
this.texture.canvas = this._canvas;
180+
Phaser.CanvasUtils.setBackgroundColor(this.texture.canvas, this.texture.backgroundColor);
181+
}
182+
183+
this.texture.context = this.texture.canvas.getContext('2d');
184+
185+
}
186+
163187
/**
164188
* Hides an object from this Camera. Hidden objects are not rendered.
165189
* The object must implement a public cameraBlacklist property.

Phaser/cameras/CameraManager.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ var Phaser;
2929

3030
this.defaultCamera = this.addCamera(x, y, width, height);
3131

32+
this.defaultCamera.directToStage = true;
33+
3234
this.current = this.defaultCamera;
3335
}
3436
/**

Phaser/cameras/CameraManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ module Phaser {
2929

3030
this.defaultCamera = this.addCamera(x, y, width, height);
3131

32+
this.defaultCamera.directToStage = true;
33+
3234
this.current = this.defaultCamera;
3335

3436
}

Phaser/gameobjects/Sprite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ var Phaser;
114114
*/
115115
Sprite.prototype.bringToTop = function () {
116116
if (this.group) {
117-
//this.group.bringToTop(this);
117+
this.group.bringToTop(this);
118118
}
119119
};
120120

Phaser/loader/Loader.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ var Phaser;
1919
* @type {string}
2020
*/
2121
this.crossOrigin = '';
22+
// If you want to append a URL before the path of any asset you can set this here.
23+
// Useful if you need to allow an asset url to be configured outside of the game code.
24+
// MUST have / on the end of it!
25+
this.baseURL = '';
2226
this.game = game;
2327

2428
this._keys = [];
@@ -225,15 +229,15 @@ var Phaser;
225229
return _this.fileError(file.key);
226230
};
227231
file.data.crossOrigin = this.crossOrigin;
228-
file.data.src = file.url;
232+
file.data.src = this.baseURL + file.url;
229233
break;
230234

231235
case 'audio':
232236
file.url = this.getAudioURL(file.url);
233237

234238
if (file.url !== null) {
235239
if (this.game.sound.usingWebAudio) {
236-
this._xhr.open("GET", file.url, true);
240+
this._xhr.open("GET", this.baseURL + file.url, true);
237241
this._xhr.responseType = "arraybuffer";
238242
this._xhr.onload = function () {
239243
return _this.fileComplete(file.key);
@@ -248,7 +252,7 @@ var Phaser;
248252
file.data = new Audio();
249253
file.data.name = file.key;
250254
file.data.preload = 'auto';
251-
file.data.src = file.url;
255+
file.data.src = this.baseURL + file.url;
252256
this.fileComplete(file.key);
253257
} else {
254258
file.data = new Audio();
@@ -257,7 +261,7 @@ var Phaser;
257261
return _this.fileError(file.key);
258262
};
259263
file.data.preload = 'auto';
260-
file.data.src = file.url;
264+
file.data.src = this.baseURL + file.url;
261265
file.data.addEventListener('canplaythrough', Phaser.GAMES[this.game.id].load.fileComplete(file.key), false);
262266
file.data.load();
263267
}
@@ -267,7 +271,7 @@ var Phaser;
267271
break;
268272

269273
case 'text':
270-
this._xhr.open("GET", file.url, true);
274+
this._xhr.open("GET", this.baseURL + file.url, true);
271275
this._xhr.responseType = "text";
272276
this._xhr.onload = function () {
273277
return _this.fileComplete(file.key);
@@ -341,7 +345,7 @@ var Phaser;
341345
} else {
342346
// Load the JSON or XML before carrying on with the next file
343347
loadNext = false;
344-
this._xhr.open("GET", file.atlasURL, true);
348+
this._xhr.open("GET", this.baseURL + file.atlasURL, true);
345349
this._xhr.responseType = "text";
346350

347351
if (file.format == Loader.TEXTURE_ATLAS_JSON_ARRAY) {

0 commit comments

Comments
 (0)