Skip to content

Commit 41e64b9

Browse files
committed
Added time property, getDistance, getDuration and getAngle.
1 parent d2cb4a4 commit 41e64b9

1 file changed

Lines changed: 80 additions & 6 deletions

File tree

src/input/Pointer.js

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var Angle = require('../math/angle/Between');
78
var Class = require('../utils/Class');
89
var Distance = require('../math/distance/DistanceBetween');
910
var FuzzyEqual = require('../math/fuzzy/Equal');
@@ -449,7 +450,14 @@ var Pointer = new Class({
449450
*/
450451
this.active = (id === 0) ? true : false;
451452

452-
this.history = [];
453+
/**
454+
* Time when this Pointer was most recently updated by the Game step.
455+
*
456+
* @name Phaser.Input.Pointer#time
457+
* @type {number}
458+
* @since 3.16.0
459+
*/
460+
this.time = 0;
453461
},
454462

455463
/**
@@ -477,14 +485,16 @@ var Pointer = new Class({
477485
* @private
478486
* @since 3.0.0
479487
*/
480-
reset: function ()
488+
reset: function (time)
481489
{
482490
this.dirty = false;
483491

484492
this.justDown = false;
485493
this.justUp = false;
486494
this.justMoved = false;
487495

496+
this.time = time;
497+
488498
this.movementX = 0;
489499
this.movementY = 0;
490500
},
@@ -863,17 +873,81 @@ var Pointer = new Class({
863873
},
864874

865875
/**
866-
* Returns the distance between the Pointer's current position and where it was
867-
* first pressed down (the `downX` and `downY` properties)
876+
* If the Pointer has a button pressed down at the time this method is called, it will return the
877+
* distance between the Pointer's `downX` and `downY` values and the current position.
878+
*
879+
* If no button is held down, it will return the last recorded distance, based on where
880+
* the Pointer was when the button was released.
881+
*
882+
* If you wish to get the distance being travelled currently, based on the velocity of the Pointer,
883+
* then see the `Pointer.distance` property.
868884
*
869885
* @method Phaser.Input.Pointer#getDistance
870886
* @since 3.13.0
871887
*
872-
* @return {number} The distance the Pointer has moved since being pressed down.
888+
* @return {number} The distance the Pointer moved.
873889
*/
874890
getDistance: function ()
875891
{
876-
return Distance(this.downX, this.downY, this.x, this.y);
892+
if (this.isDown)
893+
{
894+
return Distance(this.downX, this.downY, this.x, this.y);
895+
}
896+
else
897+
{
898+
return Distance(this.downX, this.downY, this.upX, this.upY);
899+
}
900+
},
901+
902+
/**
903+
* If the Pointer has a button pressed down at the time this method is called, it will return the
904+
* duration since the Pointer's was pressed down.
905+
*
906+
* If no button is held down, it will return the last recorded duration, based on the time
907+
* the Pointer button was released.
908+
*
909+
* @method Phaser.Input.Pointer#getDuration
910+
* @since 3.16.0
911+
*
912+
* @return {number} The duration the Pointer was held down for in milliseconds.
913+
*/
914+
getDuration: function ()
915+
{
916+
if (this.isDown)
917+
{
918+
return (this.time - this.downTime);
919+
}
920+
else
921+
{
922+
return (this.upTime - this.downTime);
923+
}
924+
},
925+
926+
/**
927+
* If the Pointer has a button pressed down at the time this method is called, it will return the
928+
* angle between the Pointer's `downX` and `downY` values and the current position.
929+
*
930+
* If no button is held down, it will return the last recorded angle, based on where
931+
* the Pointer was when the button was released.
932+
*
933+
* If you wish to get the current angle, based on the velocity of the Pointer, then
934+
* see the `Pointer.angle` property.
935+
*
936+
* @method Phaser.Input.Pointer#getAngle
937+
* @since 3.16.0
938+
*
939+
* @return {number} The angle between the Pointer's 'up' and 'down' coordinates.
940+
*/
941+
getAngle: function ()
942+
{
943+
if (this.isDown)
944+
{
945+
return Angle(this.x, this.y, this.downX, this.downY);
946+
}
947+
else
948+
{
949+
return Angle(this.upX, this.upY, this.downX, this.downY);
950+
}
877951
},
878952

879953
/**

0 commit comments

Comments
 (0)