Skip to content

Commit c904475

Browse files
committed
Added in the game scaling handler and updated Stage
1 parent 13d6ab5 commit c904475

8 files changed

Lines changed: 89 additions & 43 deletions

File tree

examples/button1.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
(function () {
1414

15-
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, render: render });
15+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, render: render });
1616

1717
function preload() {
1818

@@ -26,6 +26,8 @@ function preload() {
2626

2727
function create() {
2828

29+
game.stage.backgroundColor = 0xefefef;
30+
2931
// This is just an image that we'll toggle the display of when you click the button
3032
image = game.add.sprite(game.world.centerX, 0, 'beast');
3133
image.anchor.setTo(0.5, 0);

examples/js.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<script src="../src/gameobjects/Text.js"></script>
6666
<script src="../src/gameobjects/Button.js"></script>
6767
<script src="../src/system/Canvas.js"></script>
68+
<script src="../src/system/StageScaleMode.js"></script>
6869
<script src="../src/system/Device.js"></script>
6970
<script src="../src/system/RequestAnimationFrame.js"></script>
7071
<script src="../src/math/RandomDataGenerator.js"></script>

src/core/Game.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,11 @@ Phaser.Game.prototype = {
286286
this.math = Phaser.Math;
287287
this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]);
288288

289+
this.stage = new Phaser.Stage(this, this.width, this.height);
290+
289291
this.setUpRenderer();
290292

291293
this.world = new Phaser.World(this);
292-
this.stage = new Phaser.Stage(this);
293294
this.add = new Phaser.GameObjectFactory(this);
294295
this.cache = new Phaser.Cache(this);
295296
this.load = new Phaser.Loader(this);
@@ -334,7 +335,7 @@ Phaser.Game.prototype = {
334335
if (this.device.canvas)
335336
{
336337
this.renderType = Phaser.CANVAS;
337-
this.renderer = new PIXI.CanvasRenderer(this.width, this.height, null, this.transparent);
338+
this.renderer = new PIXI.CanvasRenderer(this.width, this.height, this.stage.canvas, this.transparent);
338339
Phaser.Canvas.setSmoothingEnabled(this.renderer.context, this.antialias);
339340
this.canvas = this.renderer.view;
340341
this.context = this.renderer.context;
@@ -387,7 +388,7 @@ Phaser.Game.prototype = {
387388
this.state.update();
388389
this.plugins.update();
389390

390-
this.renderer.render(this.world._stage);
391+
this.renderer.render(this.stage._stage);
391392
this.state.render();
392393

393394
this.plugins.postRender();

src/core/LinkedList.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ Phaser.LinkedList.prototype = {
6868

6969
childPrev.next = child.next;
7070

71+
},
72+
73+
callAll: function (callback) {
74+
75+
var entity = this.first;
76+
77+
do
78+
{
79+
if (entity[callback])
80+
{
81+
entity[callback].call(entity);
82+
}
83+
84+
entity = entity.next;
85+
86+
}
87+
while(entity != this.last.next)
88+
7189
},
7290

7391
dump: function () {

src/core/Stage.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,33 @@
99
* @copyright 2013 Photon Storm Ltd.
1010
* @license https://github.com/photonstorm/phaser/blob/master/license.txt MIT License
1111
*/
12-
Phaser.Stage = function (game) {
12+
Phaser.Stage = function (game, width, height) {
1313

1414
this.game = game;
15-
this.canvas = game.renderer.view;
15+
16+
/**
17+
* Background color of the stage (defaults to black). Set via the public backgroundColor property.
18+
* @type {string}
19+
*/
20+
this._backgroundColor = 'rgb(0,0,0)';
1621

1722
// Get the offset values (for input and other things)
1823
this.offset = new Phaser.Point;
1924

25+
this.canvas = Phaser.Canvas.create(width, height);
26+
27+
// The Pixi Stage which is hooked to the renderer
28+
this._stage = new PIXI.Stage(0x000000, false);
29+
this._stage.name = '_stage_root';
30+
2031
Phaser.Canvas.getOffset(this.canvas, this.offset);
2132

2233
this.bounds = new Phaser.Rectangle(this.offset.x, this.offset.y, this.game.width, this.game.height);
2334

35+
this.scaleMode = Phaser.StageScaleMode.NO_SCALE;
36+
this.scale = new Phaser.StageScaleMode(this.game, width, height);
37+
this.aspectRatio = width / height;
38+
2439
var _this = this;
2540

2641
this._onChange = function (event) {
@@ -68,4 +83,19 @@ Phaser.Stage.prototype = {
6883

6984
},
7085

71-
};
86+
};
87+
88+
Object.defineProperty(Phaser.Stage.prototype, "backgroundColor", {
89+
90+
get: function () {
91+
return this._backgroundColor;
92+
},
93+
94+
set: function (color) {
95+
this._stage.setBackgroundColor(color);
96+
this._backgroundColor = color;
97+
},
98+
99+
enumerable: true,
100+
configurable: true
101+
});

src/core/World.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,12 @@ Phaser.World = function (game) {
22

33
this.game = game;
44

5-
this._stage = new PIXI.Stage(0x000000);
6-
this._stage.name = '_stage_root';
7-
85
this.bounds = new Phaser.Rectangle(0, 0, game.width, game.height);
96

107
};
118

129
Phaser.World.prototype = {
1310

14-
_stage: null,
15-
_stage: null,
16-
_length: 0,
17-
1811
bounds: null,
1912
camera: null,
2013

@@ -29,27 +22,27 @@ Phaser.World.prototype = {
2922

3023
add: function (gameobject) {
3124

32-
this._stage.addChild(gameobject);
25+
this.game.stage._stage.addChild(gameobject);
3326
return gameobject;
3427

3528
},
3629

3730
addAt: function (gameobject, index) {
3831

39-
this._stage.addChildAt(gameobject, index);
32+
this.game.stage._stage.addChildAt(gameobject, index);
4033
return gameobject;
4134

4235
},
4336

4437
getAt: function (index) {
4538

46-
return this._stage.getChildAt(index);
39+
return this.game.stage._stage.getChildAt(index);
4740

4841
},
4942

5043
remove: function (gameobject) {
5144

52-
this._stage.removeChild(gameobject);
45+
this.game.stage._stage.removeChild(gameobject);
5346
return gameobject;
5447

5548
},
@@ -60,9 +53,9 @@ Phaser.World.prototype = {
6053

6154
this.currentRenderOrderID = 0;
6255

63-
if (this._stage.first._iNext)
56+
if (this.game.stage._stage.first._iNext)
6457
{
65-
var currentNode = this._stage.first._iNext;
58+
var currentNode = this.game.stage._stage.first._iNext;
6659

6760
do
6861
{
@@ -73,7 +66,7 @@ Phaser.World.prototype = {
7366

7467
currentNode = currentNode._iNext;
7568
}
76-
while (currentNode != this._stage.last._iNext)
69+
while (currentNode != this.game.stage._stage.last._iNext)
7770
}
7871

7972
},
@@ -93,9 +86,9 @@ Phaser.World.prototype = {
9386

9487
bringToTop: function (child) {
9588

96-
if (child !== this._stage.last)
89+
if (child !== this.game.stage._stage.last)
9790
{
98-
this.swapChildren(child, this._stage.last);
91+
this.swapChildren(child, this.game.stage._stage.last);
9992
}
10093

10194
return child;
@@ -116,8 +109,8 @@ Phaser.World.prototype = {
116109
var child2Prev = child2._iPrev;
117110
var child2Next = child2._iNext;
118111

119-
var endNode = this._stage.last._iNext;
120-
var currentNode = this._stage.first;
112+
var endNode = this.game.stage._stage.last._iNext;
113+
var currentNode = this.game.stage._stage.first;
121114

122115
do
123116
{

src/input/Input.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
Phaser.Input = function (game) {
88

99
this.game = game;
10-
11-
// this.inputObjects = [];
12-
// this.totalTrackedObjects = 0;
1310

1411
};
1512

@@ -397,16 +394,7 @@ Phaser.Input.prototype = {
397394
this.onTap = new Phaser.Signal();
398395
this.onHold = new Phaser.Signal();
399396

400-
for (var i = 0; i < this.totalTrackedObjects; i++)
401-
{
402-
if (this.inputObjects[i] && this.inputObjects[i].input)
403-
{
404-
this.inputObjects[i].input.reset();
405-
}
406-
}
407-
408-
this.inputObjects.length = 0;
409-
this.totalTrackedObjects = 0;
397+
this.interactiveItems.callAll('reset');
410398
}
411399

412400
this._pollCounter = 0;

src/system/Canvas.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,25 @@
77

88
Phaser.Canvas = {
99

10+
create: function (width, height) {
11+
12+
width = width || 256;
13+
height = height || 256;
14+
15+
var canvas = document.createElement('canvas');
16+
canvas.width = width;
17+
canvas.height = height;
18+
19+
return canvas;
20+
21+
},
22+
1023
/**
1124
* Get the DOM offset values of any given element
1225
*/
1326
getOffset: function (element, point) {
1427

15-
if (typeof point === "undefined") { point = new Phaser.Point; }
28+
point = point || new Phaser.Point;
1629

1730
var box = element.getBoundingClientRect();
1831
var clientTop = element.clientTop || document.body.clientTop || 0;
@@ -48,7 +61,7 @@ Phaser.Canvas = {
4861
*/
4962
setBackgroundColor: function (canvas, color) {
5063

51-
if (typeof color === "undefined") { color = 'rgb(0,0,0)'; }
64+
color = color || 'rgb(0,0,0)';
5265

5366
canvas.style.backgroundColor = color;
5467

@@ -66,7 +79,7 @@ Phaser.Canvas = {
6679
*/
6780
setTouchAction: function (canvas, value) {
6881

69-
if (typeof value === "undefined") { value = 'none'; }
82+
value = value || 'none';
7083

7184
canvas.style.msTouchAction = value;
7285
canvas.style['ms-touch-action'] = value;
@@ -88,8 +101,8 @@ Phaser.Canvas = {
88101
*/
89102
addToDOM: function (canvas, parent, overflowHidden) {
90103

91-
if (typeof parent === "undefined") { parent = ''; }
92-
if (typeof overflowHidden === "undefined") { overflowHidden = true; }
104+
parent = parent || '';
105+
overflowHidden = overflowHidden || true;
93106

94107
if ((parent !== '' || parent !== null) && document.getElementById(parent))
95108
{

0 commit comments

Comments
 (0)