Skip to content

Commit 7b876d5

Browse files
committed
ScaleManager.bounds is a Rectangle object that holds the exact size of the game canvas, taking DOM offset and game scale into account.
Pointer.withinGame is now accurate based on game scale and updated as the Pointer moves. Stage.bounds is now updated if the game canvas offset changes position. Note that it gives the un-scaled game dimensions.
1 parent 2edcd1f commit 7b876d5

8 files changed

Lines changed: 92 additions & 33 deletions

File tree

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Version 2.0.5 - "Tanchico" - in development
5858

5959
### Updates
6060

61-
* TypeScript definitions fixes and updates (thanks @luispedrofonseca @clark-stevenson @Anahkiasen @adamholdenyall @luispedrofonseca)
61+
* TypeScript definitions fixes and updates (thanks @luispedrofonseca @clark-stevenson @Anahkiasen @adamholdenyall @luispedrofonseca @WillHuxtable)
6262
* Input.getPointerFromIdentifier docs update to reflect where the identifier comes from. Pointer properties now set to give it fixed defaults (thanks @JirkaDellOro, #793)
6363
* Pointer.pointerId added which is set by the DOM event (if present in the browser). Note that browsers can and do recycle pointer IDs.
6464
* Pointer.type and Pointer.exists properties added.
@@ -69,6 +69,8 @@ Version 2.0.5 - "Tanchico" - in development
6969
* PIXI.InteractionManager is no longer over-written if the object already exists (thanks @georgiee, #818)
7070
* Key.justPressed and justReleased incorrectly set the delay value to 2500ms. Now defaults to 50ms (thanks @draklaw, fix #797)
7171
* Stage.backgroundColor can now accept short-code hex values: `#222`, `#334`, etc.
72+
* Pointer.withinGame is now accurate based on game scale and updated as the Pointer moves.
73+
* Stage.bounds is now updated if the game canvas offset changes position. Note that it gives the un-scaled game dimensions.
7274

7375

7476
### New Features
@@ -79,7 +81,7 @@ Version 2.0.5 - "Tanchico" - in development
7981
* Input.getPointerFromId will return a pointer with a matching pointerId value, if any. pointerId is a value set by the browser in the DOM event.
8082
* ArcadePhysics.getObjectsUnderPointer will return all children from a Group that overlap with the given Pointer.
8183
* InputManager.minPriorityID lets you set the minimum priority level an object needs to be to be checked by a Pointer. Useful for UI layer stacking.
82-
* New consts: Phaser.Tilemap.NORTH, SOUTH, EAST and WEST to use with the TileMapWalker Plugin.
84+
* New consts: Phaser.Tilemap.NORTH, SOUTH, EAST and WEST to use with the TileMapWalker Plugin but generally just handy to have anyway.
8385
* BitmapData.processPixelRGB added undefined check (thanks @muclemente, fix #808)
8486
* Phaser.Utils.transposeArray will transpose the given array and return it.
8587
* Phaser.Utils.rotateArray will rotate the given array by 90 or 180 degrees in either direction and return it.
@@ -111,6 +113,8 @@ Version 2.0.5 - "Tanchico" - in development
111113
* Cache.checkBitmapFontKey(key) - Check if a BitmapFont key exists in the cache (thanks to @delta11 for the proposal)
112114
* Cache.checkJSONKey(key) - Check if a JSON key exists in the cache (thanks to @delta11 for the proposal)
113115
* New movement data added for a Pointer Locked mouse (Pointer.movementX/Y) (thanks @woutercommandeur, #831)
116+
* ScaleManager.bounds is a Rectangle object that holds the exact size of the game canvas, taking DOM offset and game scale into account.
117+
114118

115119

116120
### New Plugins
@@ -178,7 +182,9 @@ Note: Some of you may not be aware, but the `phaser.min.js` file in the build fo
178182

179183
## Koding
180184

181-
You can [![clone the Phaser repo in Koding](http://learn.koding.com/btn/clone_d.png)][koding] and then start editing and previewing code right away using their web based VM development system.
185+
![Koding](https://koding.com/Teamwork?import=https://github.com/photonstorm/phaser/archive/master.zip&c=git1)
186+
187+
You can [clone the Phaser repo in Koding](http://learn.koding.com/btn/clone_d.png) and then start editing and previewing code right away using their web based VM development system.
182188

183189
![div](http://phaser.io/images/div5.png)
184190

@@ -393,6 +399,5 @@ Phaser is released under the [MIT License](http://opensource.org/licenses/MIT).
393399
[contribute]: https://github.com/photonstorm/phaser/blob/master/CONTRIBUTING.md
394400
[phaser]: https://github.com/photonstorm/phaser
395401
[forum]: http://www.html5gamedevs.com/forum/14-phaser/
396-
[koding]: https://koding.com/Teamwork?import=https://github.com/photonstorm/phaser/archive/master.zip&c=git1
397402

398403
[![Analytics](https://ga-beacon.appspot.com/UA-44006568-2/phaser/index)](https://github.com/igrigorik/ga-beacon)

build/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4216,6 +4216,7 @@ declare module Phaser {
42164216
static SHOW_ALL: number;
42174217

42184218
aspectRatio: number;
4219+
bounds: Phaser.Rectangle;
42194220
enterFullScreen: Phaser.Signal;
42204221
enterIncorrectOrientation: Phaser.Signal;
42214222
enterLandscape: Phaser.Signal;

src/core/ScaleManager.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ Phaser.ScaleManager = function (game, width, height) {
173173
*/
174174
this.margin = new Phaser.Point(0, 0);
175175

176+
/**
177+
* @property {Phaser.Rectangle} bounds - The bounds of the scaled game. The x/y will match the offset of the canvas element and the width/height the scaled width and height.
178+
* @readonly
179+
*/
180+
this.bounds = new Phaser.Rectangle(0, 0, width, height);
181+
176182
/**
177183
* @property {number} aspectRatio - The aspect ratio of the scaled game.
178184
* @readonly
@@ -685,6 +691,8 @@ Phaser.ScaleManager.prototype = {
685691

686692
Phaser.Canvas.getOffset(this.game.canvas, this.game.stage.offset);
687693

694+
this.bounds.setTo(this.game.stage.offset.x, this.game.stage.offset.y, this.width, this.height);
695+
688696
this.aspectRatio = this.width / this.height;
689697

690698
this.scaleFactor.x = this.game.width / this.width;

src/core/Stage.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ Phaser.Stage = function (game, width, height) {
2727
*/
2828
this.offset = new Phaser.Point();
2929

30+
/**
31+
* @property {Phaser.Rectangle} bounds - The bounds of the Stage. Typically x/y = Stage.offset.x/y and the width/height match the game width and height.
32+
*/
33+
this.bounds = new Phaser.Rectangle(0, 0, width, height);
34+
3035
PIXI.Stage.call(this, 0x000000, false);
3136

3237
/**
@@ -177,6 +182,8 @@ Phaser.Stage.prototype.postUpdate = function () {
177182
if (this.game.time.now > this._nextOffsetCheck)
178183
{
179184
Phaser.Canvas.getOffset(this.game.canvas, this.offset);
185+
this.bounds.x = this.offset.x;
186+
this.bounds.y = this.offset.y;
180187
this._nextOffsetCheck = this.game.time.now + this.checkOffsetInterval;
181188
}
182189
}
@@ -245,7 +252,7 @@ Phaser.Stage.prototype.boot = function () {
245252

246253
Phaser.Canvas.getOffset(this.game.canvas, this.offset);
247254

248-
this.bounds = new Phaser.Rectangle(this.offset.x, this.offset.y, this.game.width, this.game.height);
255+
this.bounds.setTo(this.offset.x, this.offset.y, this.game.width, this.game.height);
249256

250257
var _this = this;
251258

src/input/Mouse.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ Phaser.Mouse = function (game) {
4343
*/
4444
this.mouseOutCallback = null;
4545

46+
/**
47+
* @property {function} mouseOverCallback - A callback that can be fired when the mouse enters the game canvas (usually after a mouseout).
48+
*/
49+
this.mouseOverCallback = null;
50+
4651
/**
4752
* @property {boolean} capture - If true the DOM mouse events will have event.preventDefault applied to them, if false they will propogate fully.
4853
*/
@@ -108,6 +113,12 @@ Phaser.Mouse = function (game) {
108113
*/
109114
this._onMouseOut = null;
110115

116+
/**
117+
* @property {function} _onMouseOver - Internal event handler reference.
118+
* @private
119+
*/
120+
this._onMouseOver = null;
121+
111122
};
112123

113124
/**
@@ -172,9 +183,14 @@ Phaser.Mouse.prototype = {
172183
return _this.onMouseOut(event);
173184
};
174185

186+
this._onMouseOver = function (event) {
187+
return _this.onMouseOver(event);
188+
};
189+
175190
this.game.canvas.addEventListener('mousedown', this._onMouseDown, true);
176191
this.game.canvas.addEventListener('mousemove', this._onMouseMove, true);
177192
this.game.canvas.addEventListener('mouseup', this._onMouseUp, true);
193+
this.game.canvas.addEventListener('mouseover', this._onMouseOver, true);
178194
this.game.canvas.addEventListener('mouseout', this._onMouseOut, true);
179195

180196
},
@@ -309,6 +325,35 @@ Phaser.Mouse.prototype = {
309325

310326
},
311327

328+
/**
329+
* The internal method that handles the mouse over event from the browser.
330+
*
331+
* @method Phaser.Mouse#onMouseOver
332+
* @param {MouseEvent} event - The native event from the browser. This gets stored in Mouse.event.
333+
*/
334+
onMouseOver: function (event) {
335+
336+
this.event = event;
337+
338+
if (this.capture)
339+
{
340+
event.preventDefault();
341+
}
342+
343+
if (this.mouseOverCallback)
344+
{
345+
this.mouseOverCallback.call(this.callbackContext, event);
346+
}
347+
348+
if (this.game.input.disabled || this.disabled)
349+
{
350+
return;
351+
}
352+
353+
this.game.input.mousePointer.withinGame = true;
354+
355+
},
356+
312357
/**
313358
* If the browser supports it you can request that the pointer be locked to the browser window.
314359
* This is classically known as 'FPS controls', where the pointer can't leave the browser until the user presses an exit key.
@@ -387,6 +432,7 @@ Phaser.Mouse.prototype = {
387432
this.game.canvas.removeEventListener('mousedown', this._onMouseDown, true);
388433
this.game.canvas.removeEventListener('mousemove', this._onMouseMove, true);
389434
this.game.canvas.removeEventListener('mouseup', this._onMouseUp, true);
435+
this.game.canvas.removeEventListener('mouseover', this._onMouseOver, true);
390436
this.game.canvas.removeEventListener('mouseout', this._onMouseOut, true);
391437

392438
}

src/input/Pointer.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,90 +77,82 @@ Phaser.Pointer = function (game, id) {
7777
/**
7878
* @property {number} _nextDrop - Local private variable storing the time at which the next history drop should occur.
7979
* @private
80-
* @default
8180
*/
8281
this._nextDrop = 0;
8382

8483
/**
8584
* @property {boolean} _stateReset - Monitor events outside of a state reset loop.
8685
* @private
87-
* @default
8886
*/
8987
this._stateReset = false;
9088

9189
/**
92-
* @property {boolean} withinGame - true if the Pointer is within the game area, otherwise false.
90+
* @property {boolean} withinGame - true if the Pointer is over the game canvas, otherwise false.
9391
*/
9492
this.withinGame = false;
9593

9694
/**
97-
* @property {number} clientX - The horizontal coordinate of point relative to the viewport in pixels, excluding any scroll offset.
98-
* @default
95+
* @property {number} clientX - The horizontal coordinate of the Pointer within the application's client area at which the event occurred (as opposed to the coordinates within the page).
9996
*/
10097
this.clientX = -1;
10198

10299
/**
103-
* @property {number} clientY - The vertical coordinate of point relative to the viewport in pixels, excluding any scroll offset.
104-
* @default
100+
* @property {number} clientY - The vertical coordinate of the Pointer within the application's client area at which the event occurred (as opposed to the coordinates within the page).
105101
*/
106102
this.clientY = -1;
107103

108104
/**
109-
* @property {number} pageX - The horizontal coordinate of point relative to the viewport in pixels, including any scroll offset.
110-
* @default
105+
* @property {number} pageX - The horizontal coordinate of the Pointer relative to whole document.
111106
*/
112107
this.pageX = -1;
113108

114109
/**
115-
* @property {number} pageY - The vertical coordinate of point relative to the viewport in pixels, including any scroll offset.
116-
* @default
110+
* @property {number} pageY - The vertical coordinate of the Pointer relative to whole document.
117111
*/
118112
this.pageY = -1;
119113

120114
/**
121-
* @property {number} screenX - The horizontal coordinate of point relative to the screen in pixels.
122-
* @default
115+
* @property {number} screenX - The horizontal coordinate of the Pointer relative to the screen.
123116
*/
124117
this.screenX = -1;
125118

126119
/**
127-
* @property {number} screenY - The vertical coordinate of point relative to the screen in pixels.
128-
* @default
120+
* @property {number} screenY - The vertical coordinate of the Pointer relative to the screen.
129121
*/
130122
this.screenY = -1;
131123

132124
/**
133-
* @property {number} rawMovementX - The horizontal raw relative movement of pointer in pixels since last event.
125+
* @property {number} rawMovementX - The horizontal raw relative movement of the Pointer in pixels since last event.
134126
* @default
135127
*/
136128
this.rawMovementX = 0;
137129

138130
/**
139-
* @property {number} rawMovementY - The vertical raw relative movement of pointer in pixels since last event.
131+
* @property {number} rawMovementY - The vertical raw relative movement of the Pointer in pixels since last event.
140132
* @default
141133
*/
142134
this.rawMovementY = 0;
143135

144136
/**
145-
* @property {number} movementX - The horizontal processed relative movement of pointer in pixels since last event.
137+
* @property {number} movementX - The horizontal processed relative movement of the Pointer in pixels since last event.
146138
* @default
147139
*/
148140
this.movementX = 0;
149141

150142
/**
151-
* @property {number} movementY - The vertical processed relative movement of pointer in pixels since last event.
143+
* @property {number} movementY - The vertical processed relative movement of the Pointer in pixels since last event.
152144
* @default
153145
*/
154146
this.movementY = 0;
155147

156148
/**
157-
* @property {number} x - The horizontal coordinate of point relative to the game element. This value is automatically scaled based on game size.
149+
* @property {number} x - The horizontal coordinate of the Pointer. This value is automatically scaled based on the game scale.
158150
* @default
159151
*/
160152
this.x = -1;
161153

162154
/**
163-
* @property {number} y - The vertical coordinate of point relative to the game element. This value is automatically scaled based on game size.
155+
* @property {number} y - The vertical coordinate of the Pointer. This value is automatically scaled based on the game scale.
164156
* @default
165157
*/
166158
this.y = -1;
@@ -208,7 +200,7 @@ Phaser.Pointer = function (game, id) {
208200
this.totalTouches = 0;
209201

210202
/**
211-
* @property {number} msSinceLastClick - The number of miliseconds since the last click.
203+
* @property {number} msSinceLastClick - The number of milliseconds since the last click or touch event.
212204
* @default
213205
*/
214206
this.msSinceLastClick = Number.MAX_VALUE;
@@ -401,7 +393,7 @@ Phaser.Pointer.prototype = {
401393
this.circle.x = this.x;
402394
this.circle.y = this.y;
403395

404-
if (this.game.input.multiInputOverride == Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride == Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride == Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers === 0))
396+
if (this.game.input.multiInputOverride === Phaser.Input.MOUSE_OVERRIDES_TOUCH || this.game.input.multiInputOverride === Phaser.Input.MOUSE_TOUCH_COMBINE || (this.game.input.multiInputOverride === Phaser.Input.TOUCH_OVERRIDES_MOUSE && this.game.input.currentPointers === 0))
405397
{
406398
this.game.input.activePointer = this;
407399
this.game.input.x = this.x;
@@ -411,6 +403,8 @@ Phaser.Pointer.prototype = {
411403
this.game.input.circle.y = this.game.input.y;
412404
}
413405

406+
this.withinGame = this.game.scale.bounds.contains(this.pageX, this.pageY);
407+
414408
// If the game is paused we don't process any target objects or callbacks
415409
if (this.game.paused)
416410
{

src/input/Touch.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ Phaser.Touch.prototype = {
258258
*/
259259
onTouchEnter: function (event) {
260260

261+
console.log('touch enter', event);
262+
261263
this.event = event;
262264

263265
if (this.touchEnterCallback)
@@ -297,11 +299,6 @@ Phaser.Touch.prototype = {
297299
event.preventDefault();
298300
}
299301

300-
// for (var i = 0; i < event.changedTouches.length; i++)
301-
// {
302-
//this.game.input.updatePointer(event.changedTouches[i]);
303-
// }
304-
305302
},
306303

307304
/**

src/loader/Loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Phaser.Loader.prototype = {
163163
* You can set a Sprite to be a "preload" sprite by passing it to this method.
164164
* A "preload" sprite will have its width or height crop adjusted based on the percentage of the loader in real-time.
165165
* This allows you to easily make loading bars for games. Note that Sprite.visible = true will be set when calling this.
166+
* Note: The Sprite should use a single image and not use a texture that is part of a Texture Atlas or Sprite Sheet.
166167
*
167168
* @method Phaser.Loader#setPreloadSprite
168169
* @param {Phaser.Sprite|Phaser.Image} sprite - The sprite or image that will be cropped during the load.

0 commit comments

Comments
 (0)