Skip to content

Commit 7dac2b6

Browse files
committed
Added Group.bringToTop and updated Input component to use sprite getXY handler.
1 parent 2389c6c commit 7dac2b6

36 files changed

Lines changed: 627 additions & 762 deletions

Phaser/components/sprite/Input.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ module Phaser.Components.Sprite {
4848
*/
4949
public priorityID:number = 0;
5050

51+
/**
52+
* The index of this Input component entry in the Game.Input manager.
53+
*/
54+
public indexID:number = 0;
55+
5156
private _dragPoint: Point;
5257
private _draggedPointerID: number;
5358
public dragOffset: Point;
@@ -58,6 +63,7 @@ module Phaser.Components.Sprite {
5863

5964
public allowHorizontalDrag: bool = true;
6065
public allowVerticalDrag: bool = true;
66+
public bringToTop: bool = false;
6167

6268
public snapOnDrag: bool = false;
6369
public snapOnRelease: bool = false;
@@ -245,11 +251,26 @@ module Phaser.Components.Sprite {
245251
{
246252
// De-register, etc
247253
this.enabled = false;
248-
this.game.input.removeGameObject(this.sprite);
254+
this.game.input.removeGameObject(this.indexID);
249255
}
250256

251257
}
252258

259+
/**
260+
* Clean up memory.
261+
*/
262+
public destroy() {
263+
264+
if (this.enabled)
265+
{
266+
this.game.input.removeGameObject(this.indexID);
267+
}
268+
269+
}
270+
271+
/**
272+
* Checks if the given pointer is over this Sprite. All checks are done in world coordinates.
273+
*/
253274
public checkPointerOver(pointer: Phaser.Pointer): bool {
254275

255276
if (this.enabled == false || this.sprite.visible == false)
@@ -258,7 +279,7 @@ module Phaser.Components.Sprite {
258279
}
259280
else
260281
{
261-
return RectangleUtils.contains(this.sprite.worldView, pointer.worldX(), pointer.worldY());
282+
return SpriteUtils.overlapsXY(this.sprite, pointer.worldX(), pointer.worldY());
262283
}
263284

264285
}
@@ -279,7 +300,7 @@ module Phaser.Components.Sprite {
279300
}
280301
else if (this._pointerData[pointer.id].isOver == true)
281302
{
282-
if (RectangleUtils.contains(this.sprite.worldView, pointer.worldX(), pointer.worldY()))
303+
if (SpriteUtils.overlapsXY(this.sprite, pointer.worldX(), pointer.worldY()))
283304
{
284305
this._pointerData[pointer.id].x = pointer.x - this.sprite.x;
285306
this._pointerData[pointer.id].y = pointer.y - this.sprite.y;
@@ -350,6 +371,11 @@ module Phaser.Components.Sprite {
350371
this.startDrag(pointer);
351372
}
352373

374+
if (this.bringToTop)
375+
{
376+
this.sprite.group.bringToTop(this.sprite);
377+
}
378+
353379
}
354380

355381
// Consume the event?
@@ -493,16 +519,18 @@ module Phaser.Components.Sprite {
493519
* Make this Sprite draggable by the mouse. You can also optionally set mouseStartDragCallback and mouseStopDragCallback
494520
*
495521
* @param lockCenter If false the Sprite will drag from where you click it minus the dragOffset. If true it will center itself to the tip of the mouse pointer.
522+
* @param bringToTop If true the Sprite will be bought to the top of the rendering list in its current Group.
496523
* @param pixelPerfect If true it will use a pixel perfect test to see if you clicked the Sprite. False uses the bounding box.
497524
* @param alphaThreshold If using pixel perfect collision this specifies the alpha level from 0 to 255 above which a collision is processed (default 255)
498525
* @param boundsRect If you want to restrict the drag of this sprite to a specific FlxRect, pass the FlxRect here, otherwise it's free to drag anywhere
499526
* @param boundsSprite If you want to restrict the drag of this sprite to within the bounding box of another sprite, pass it here
500527
*/
501-
public enableDrag(lockCenter:bool = false, pixelPerfect:bool = false, alphaThreshold:number = 255, boundsRect:Rectangle = null, boundsSprite:Phaser.Sprite = null):void
528+
public enableDrag(lockCenter:bool = false, bringToTop:bool = false, pixelPerfect:bool = false, alphaThreshold:number = 255, boundsRect:Rectangle = null, boundsSprite:Phaser.Sprite = null):void
502529
{
503530
this._dragPoint = new Point;
504531

505532
this.draggable = true;
533+
this.bringToTop = bringToTop;
506534

507535
this.dragOffset = new Point;
508536
this.dragFromCenter = lockCenter;
@@ -559,6 +587,11 @@ module Phaser.Components.Sprite {
559587

560588
this.updateDrag(pointer);
561589

590+
if (this.bringToTop)
591+
{
592+
this.sprite.group.bringToTop(this.sprite);
593+
}
594+
562595
}
563596

564597
/**

Phaser/core/Group.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,14 @@ module Phaser {
583583

584584
}
585585

586+
/**
587+
* Swaps two existing game object in this Group with each other.
588+
*
589+
* @param {Basic} child1 The first object to swap.
590+
* @param {Basic} child2 The second object to swap.
591+
*
592+
* @return {Basic} True if the two objects successfully swapped position.
593+
*/
586594
public swap(child1, child2, sort?: bool = true): bool {
587595

588596
if (child1.group.ID != this.ID || child2.group.ID != this.ID || child1 === child2)
@@ -604,6 +612,43 @@ module Phaser {
604612

605613
}
606614

615+
public bringToTop(child): bool {
616+
617+
// If child not in this group, or is already at the top of the group, return false
618+
if (child.group.ID != this.ID || child.z == this._zCounter)
619+
{
620+
return false;
621+
}
622+
623+
this.sort();
624+
625+
// What's the z index of the top most child?
626+
var tempZ: number = child.z;
627+
var childIndex: number = this._zCounter;
628+
629+
this._i = 0;
630+
631+
while (this._i < this.length)
632+
{
633+
this._member = this.members[this._i++];
634+
635+
if (this._i > childIndex)
636+
{
637+
this._member.z--;
638+
}
639+
else if (this._member.z == child.z)
640+
{
641+
childIndex = this._i;
642+
this._member.z = this._zCounter;
643+
}
644+
}
645+
646+
this.sort();
647+
648+
return true;
649+
650+
}
651+
607652
/**
608653
* Call this function to sort the group according to a particular value and order.
609654
* For example, to sort game objects for Zelda-style overlaps you might call

Phaser/gameobjects/Sprite.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ module Phaser {
4040
this.z = -1;
4141
this.group = null;
4242

43-
// No dependencies
4443
this.animations = new Phaser.Components.AnimationManager(this);
4544
this.input = new Phaser.Components.Sprite.Input(this);
4645
this.events = new Phaser.Components.Sprite.Events(this);
@@ -74,6 +73,11 @@ module Phaser {
7473

7574
this.transform.setCache();
7675

76+
// Handy proxies
77+
this.scale = this.transform.scale;
78+
this.alpha = this.texture.alpha;
79+
this.origin = this.transform.origin;
80+
7781
}
7882

7983
/**
@@ -194,6 +198,23 @@ module Phaser {
194198
this.transform.rotation = this.game.math.wrap(value, 360, 0);
195199
}
196200

201+
/**
202+
* The scale of the Sprite. A value of 1 is original scale. 0.5 is half size. 2 is double the size.
203+
* This is a reference to Sprite.transform.scale
204+
*/
205+
public scale: Phaser.Vec2;
206+
207+
/**
208+
* The alpha of the Sprite between 0 and 1, a value of 1 being fully opaque.
209+
*/
210+
public alpha: number;
211+
212+
/**
213+
* The origin of the Sprite around which rotation and positioning takes place.
214+
* This is a reference to Sprite.transform.origin
215+
*/
216+
public origin: Phaser.Vec2;
217+
197218
/**
198219
* Set the animation frame by frame number.
199220
*/
@@ -318,8 +339,7 @@ module Phaser {
318339
*/
319340
public destroy() {
320341

321-
//this.input.destroy();
322-
342+
this.input.destroy();
323343

324344
}
325345

Phaser/input/Input.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -455,21 +455,45 @@ module Phaser {
455455
public inputObjects = [];
456456
public totalTrackedObjects: number = 0;
457457

458-
// Add Input Enabled array + add/remove methods and then iterate and update them during the main update
459-
// Clear down this array on State swap??? Maybe removed from it when Sprite is destroyed
460-
458+
/**
459+
* Adds a new game object to be tracked by the Input Manager. Called by the Sprite.Input component, should not usually be called directly.
460+
* @method addGameObject
461+
**/
461462
public addGameObject(object) {
462463

463-
// Lots more checks here
464+
// Find a spare slot
465+
for (var i = 0; i < this.inputObjects.length; i++)
466+
{
467+
if (this.inputObjects[i] == null)
468+
{
469+
this.inputObjects[i] = object;
470+
object.input.indexID = i;
471+
this.totalTrackedObjects++;
472+
return;
473+
}
474+
}
475+
476+
// If we got this far we need to push a new entry into the array
477+
object.input.indexID = this.inputObjects.length;
478+
464479
this.inputObjects.push(object);
480+
465481
this.totalTrackedObjects++;
466-
}
467482

468-
public removeGameObject(object) {
469-
// TODO
470483
}
471484

485+
/**
486+
* Removes a game object from the Input Manager. Called by the Sprite.Input component, should not usually be called directly.
487+
* @method removeGameObject
488+
**/
489+
public removeGameObject(index: number) {
472490

491+
if (this.inputObjects[index])
492+
{
493+
this.inputObjects[index] = null;
494+
}
495+
496+
}
473497

474498
/**
475499
* Updates the Input Manager. Called by the core Game loop.
@@ -521,7 +545,7 @@ module Phaser {
521545
if (this.pointer10) { this.pointer10.reset(); }
522546

523547
this.currentPointers = 0;
524-
548+
525549
this._game.stage.canvas.style.cursor = "default";
526550

527551
if (hard == true)
@@ -618,7 +642,7 @@ module Phaser {
618642
* @param {Any} event The event data from the Touch event
619643
* @return {Pointer} The Pointer object that was started or null if no Pointer object is available
620644
**/
621-
public startPointer(event):Pointer {
645+
public startPointer(event): Pointer {
622646

623647
if (this.maxPointers < 10 && this.totalActivePointers == this.maxPointers)
624648
{
@@ -677,7 +701,7 @@ module Phaser {
677701
* @param {Any} event The event data from the Touch event
678702
* @return {Pointer} The Pointer object that was updated or null if no Pointer object is available
679703
**/
680-
public updatePointer(event):Pointer {
704+
public updatePointer(event): Pointer {
681705

682706
// Unrolled for speed
683707
if (this.pointer1.active == true && this.pointer1.identifier == event.identifier)
@@ -731,7 +755,7 @@ module Phaser {
731755
* @param {Any} event The event data from the Touch event
732756
* @return {Pointer} The Pointer object that was stopped or null if no Pointer object is available
733757
**/
734-
public stopPointer(event):Pointer {
758+
public stopPointer(event): Pointer {
735759

736760
// Unrolled for speed
737761
if (this.pointer1.active == true && this.pointer1.identifier == event.identifier)

Phaser/input/Pointer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ module Phaser {
422422

423423
for (var i = 0; i < this.game.input.totalTrackedObjects; i++)
424424
{
425-
if (this.game.input.inputObjects[i].input.checkPointerOver(this) && this.game.input.inputObjects[i].renderOrderID > _highestRenderID)
425+
if (this.game.input.inputObjects[i] !== null && this.game.input.inputObjects[i].input.checkPointerOver(this) && this.game.input.inputObjects[i].renderOrderID > _highestRenderID)
426426
{
427427
_highestRenderID = this.game.input.inputObjects[i].renderOrderID;
428428
_highestRenderObject = i;
@@ -510,7 +510,7 @@ module Phaser {
510510

511511
for (var i = 0; i < this.game.input.totalTrackedObjects; i++)
512512
{
513-
if (this.game.input.inputObjects[i].input.enabled)
513+
if (this.game.input.inputObjects[i] !== null && this.game.input.inputObjects[i].input.enabled)
514514
{
515515
this.game.input.inputObjects[i].input._releasedHandler(this);
516516
}

Phaser/loader/Cache.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,57 @@ module Phaser {
261261

262262
}
263263

264+
/**
265+
* Returns an array containing all of the keys of Images in the Cache.
266+
* @return {Array} The string based keys in the Cache.
267+
*/
268+
public getImageKeys() {
269+
270+
var output = [];
271+
272+
for (var item in this._images)
273+
{
274+
output.push(item);
275+
}
276+
277+
return output;
278+
279+
}
280+
281+
/**
282+
* Returns an array containing all of the keys of Sounds in the Cache.
283+
* @return {Array} The string based keys in the Cache.
284+
*/
285+
public getSoundKeys() {
286+
287+
var output = [];
288+
289+
for (var item in this._sounds)
290+
{
291+
output.push(item);
292+
}
293+
294+
return output;
295+
296+
}
297+
298+
/**
299+
* Returns an array containing all of the keys of Text Files in the Cache.
300+
* @return {Array} The string based keys in the Cache.
301+
*/
302+
public getTextKeys() {
303+
304+
var output = [];
305+
306+
for (var item in this._text)
307+
{
308+
output.push(item);
309+
}
310+
311+
return output;
312+
313+
}
314+
264315
/**
265316
* Clean up cache memory.
266317
*/

0 commit comments

Comments
 (0)