2013-10-02 14:05:55 +00:00
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< title > Phaser Source: input/InputHandler.js< / title >
<!-- [if lt IE 9]>
< script src = "//html5shiv.googlecode.com/svn/trunk/html5.js" > < / script >
<![endif]-->
< link type = "text/css" rel = "stylesheet" href = "styles/sunlight.default.css" >
< link type = "text/css" rel = "stylesheet" href = "styles/site.cerulean.css" >
< / head >
< body >
< div class = "container-fluid" >
< div class = "navbar navbar-fixed-top navbar-inverse" >
< div class = "navbar-inner" >
< a class = "brand" href = "index.html" > Phaser< / a >
< ul class = "nav" >
< li class = "dropdown" >
2013-10-03 01:38:35 +00:00
< a href = "namespaces.list.html" class = "dropdown-toggle" data-toggle = "dropdown" > Namespaces< b
2013-10-02 14:05:55 +00:00
class="caret">< / b > < / a >
< ul class = "dropdown-menu " >
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.html" > Phaser< / a >
2013-10-02 14:05:55 +00:00
< / li >
< / ul >
< / li >
< li class = "dropdown" >
< a href = "classes.list.html" class = "dropdown-toggle" data-toggle = "dropdown" > Classes< b
class="caret">< / b > < / a >
< ul class = "dropdown-menu " >
< li >
< a href = "Phaser.Animation.html" > Animation< / a >
< / li >
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.AnimationManager.html" > AnimationManager< / a >
2013-10-02 14:05:55 +00:00
< / li >
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.AnimationParser.html" > AnimationParser< / a >
2013-10-02 14:05:55 +00:00
< / li >
2013-11-28 15:57:09 +00:00
< li >
< a href = "Phaser.BitmapData.html" > BitmapData< / a >
< / li >
2013-10-25 15:54:40 +00:00
< li >
< a href = "Phaser.BitmapText.html" > BitmapText< / a >
< / li >
< li >
< a href = "Phaser.Button.html" > Button< / a >
< / li >
2013-10-02 14:05:55 +00:00
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.Cache.html" > Cache< / a >
2013-10-02 14:05:55 +00:00
< / li >
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.Camera.html" > Camera< / a >
2013-10-02 14:05:55 +00:00
< / li >
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.Canvas.html" > Canvas< / a >
2013-10-02 14:05:55 +00:00
< / li >
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.Circle.html" > Circle< / a >
2013-10-02 14:05:55 +00:00
< / li >
2013-10-03 00:21:08 +00:00
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.Color.html" > Color< / a >
2013-10-03 00:21:08 +00:00
< / li >
2013-10-02 14:05:55 +00:00
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.Device.html" > Device< / a >
2013-10-02 14:05:55 +00:00
< / li >
2013-10-03 00:21:08 +00:00
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.Easing.html" > Easing< / a >
< / li >
< li >
< a href = "Phaser.Easing.Back.html" > Back< / a >
< / li >
< li >
< a href = "Phaser.Easing.Bounce.html" > Bounce< / a >
< / li >
< li >
< a href = "Phaser.Easing.Circular.html" > Circular< / a >
< / li >
< li >
< a href = "Phaser.Easing.Cubic.html" > Cubic< / a >
< / li >
< li >
< a href = "Phaser.Easing.Elastic.html" > Elastic< / a >
< / li >
< li >
< a href = "Phaser.Easing.Exponential.html" > Exponential< / a >
< / li >
< li >
< a href = "Phaser.Easing.Linear.html" > Linear< / a >
< / li >
< li >
< a href = "Phaser.Easing.Quadratic.html" > Quadratic< / a >
< / li >
< li >
< a href = "Phaser.Easing.Quartic.html" > Quartic< / a >
< / li >
< li >
< a href = "Phaser.Easing.Quintic.html" > Quintic< / a >
< / li >
< li >
< a href = "Phaser.Easing.Sinusoidal.html" > Sinusoidal< / a >
< / li >
2014-02-18 03:01:51 +00:00
< li >
< a href = "Phaser.Ellipse.html" > Ellipse< / a >
< / li >
2013-10-25 15:54:40 +00:00
< li >
< a href = "Phaser.Events.html" > Events< / a >
< / li >
2013-11-28 15:57:09 +00:00
< li >
< a href = "Phaser.Filter.html" > Filter< / a >
< / li >
2013-10-03 01:38:35 +00:00
< li >
< a href = "Phaser.Frame.html" > Frame< / a >
< / li >
< li >
< a href = "Phaser.FrameData.html" > FrameData< / a >
2013-10-03 00:21:08 +00:00
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.Game.html" > Game< / a >
< / li >
2014-02-21 15:37:26 +00:00
< li >
< a href = "Phaser.GameObjectCreator.html" > GameObjectCreator< / a >
< / li >
2013-10-25 15:54:40 +00:00
< li >
< a href = "Phaser.GameObjectFactory.html" > GameObjectFactory< / a >
< / li >
2014-02-05 06:29:17 +00:00
< li >
< a href = "Phaser.Gamepad.html" > Gamepad< / a >
< / li >
< li >
< a href = "Phaser.GamepadButton.html" > GamepadButton< / a >
< / li >
2013-10-25 15:54:40 +00:00
< li >
< a href = "Phaser.Graphics.html" > Graphics< / a >
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.Group.html" > Group< / a >
< / li >
2014-02-08 07:24:22 +00:00
< li >
< a href = "Phaser.Image.html" > Image< / a >
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.Input.html" > Input< / a >
< / li >
< li >
< a href = "Phaser.InputHandler.html" > InputHandler< / a >
< / li >
< li >
< a href = "Phaser.Key.html" > Key< / a >
< / li >
< li >
< a href = "Phaser.Keyboard.html" > Keyboard< / a >
< / li >
2014-02-05 06:29:17 +00:00
< li >
< a href = "Phaser.Line.html" > Line< / a >
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.LinkedList.html" > LinkedList< / a >
< / li >
< li >
< a href = "Phaser.Loader.html" > Loader< / a >
< / li >
< li >
2013-10-03 01:38:35 +00:00
< a href = "Phaser.LoaderParser.html" > LoaderParser< / a >
2013-10-02 14:05:55 +00:00
< / li >
< li >
< a href = "Phaser.Math.html" > Math< / a >
< / li >
< li >
< a href = "Phaser.Mouse.html" > Mouse< / a >
< / li >
< li >
< a href = "Phaser.MSPointer.html" > MSPointer< / a >
< / li >
2013-10-02 19:18:24 +00:00
< li >
< a href = "Phaser.Net.html" > Net< / a >
< / li >
< li >
< a href = "Phaser.Particles.html" > Particles< / a >
< / li >
< li >
< a href = "Phaser.Particles.Arcade.Emitter.html" > Emitter< / a >
< / li >
2013-10-25 15:54:40 +00:00
< li >
< a href = "Phaser.Physics.html" > Physics< / a >
< / li >
< li >
< a href = "Phaser.Physics.Arcade.html" > Arcade< / a >
< / li >
2014-02-21 15:37:26 +00:00
< li >
2014-03-14 06:36:05 +00:00
< a href = "Phaser.Physics.Arcade.Body.html" > Body< / a >
< / li >
< li >
< a href = "Phaser.Physics.Ninja.html" > Ninja< / a >
< / li >
< li >
< a href = "Phaser.Physics.Ninja.AABB.html" > AABB< / a >
< / li >
< li >
< a href = "Phaser.Physics.Ninja.Body.html" > Body< / a >
< / li >
< li >
< a href = "Phaser.Physics.Ninja.Circle.html" > Circle< / a >
< / li >
< li >
< a href = "Phaser.Physics.Ninja.Tile.html" > Tile< / a >
< / li >
< li >
< a href = "Phaser.Physics.P2.html" > P2< / a >
< / li >
< li >
< a href = "Phaser.Physics.P2.Body.html" > Body< / a >
< / li >
< li >
< a href = "Phaser.Physics.P2.BodyDebug.html" > BodyDebug< / a >
< / li >
< li >
< a href = "Phaser.Physics.P2.CollisionGroup.html" > CollisionGroup< / a >
< / li >
< li >
< a href = "Phaser.Physics.P2.ContactMaterial.html" > ContactMaterial< / a >
< / li >
< li >
< a href = "Phaser.Physics.P2.DistanceConstraint.html" > DistanceConstraint< / a >
< / li >
< li >
< a href = "Phaser.Physics.P2.GearConstraint.html" > GearConstraint< / a >
2014-02-21 15:37:26 +00:00
< / li >
< li >
2014-03-14 06:36:05 +00:00
< a href = "Phaser.Physics.P2.InversePointProxy.html" > InversePointProxy< / a >
2014-02-21 15:37:26 +00:00
< / li >
< li >
2014-03-14 06:36:05 +00:00
< a href = "Phaser.Physics.P2.LockConstraint.html" > LockConstraint< / a >
2014-02-21 15:37:26 +00:00
< / li >
< li >
2014-03-14 06:36:05 +00:00
< a href = "Phaser.Physics.P2.Material.html" > Material< / a >
2014-02-21 15:37:26 +00:00
< / li >
< li >
2014-03-14 06:36:05 +00:00
< a href = "Phaser.Physics.P2.PointProxy.html" > PointProxy< / a >
2014-02-21 15:37:26 +00:00
< / li >
< li >
2014-03-14 06:36:05 +00:00
< a href = "Phaser.Physics.P2.PrismaticConstraint.html" > PrismaticConstraint< / a >
2014-02-21 15:37:26 +00:00
< / li >
< li >
2014-03-14 06:36:05 +00:00
< a href = "Phaser.Physics.P2.RevoluteConstraint.html" > RevoluteConstraint< / a >
2014-02-21 15:37:26 +00:00
< / li >
< li >
2014-03-14 06:36:05 +00:00
< a href = "Phaser.Physics.P2.Spring.html" > Spring< / a >
2014-02-21 15:37:26 +00:00
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.Plugin.html" > Plugin< / a >
< / li >
< li >
< a href = "Phaser.PluginManager.html" > PluginManager< / a >
< / li >
< li >
< a href = "Phaser.Point.html" > Point< / a >
< / li >
< li >
< a href = "Phaser.Pointer.html" > Pointer< / a >
< / li >
2013-11-07 06:10:15 +00:00
< li >
< a href = "Phaser.Polygon.html" > Polygon< / a >
< / li >
2014-03-14 06:36:05 +00:00
< li >
< a href = "Phaser.QuadTree.html" > QuadTree< / a >
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.RandomDataGenerator.html" > RandomDataGenerator< / a >
< / li >
< li >
< a href = "Phaser.Rectangle.html" > Rectangle< / a >
< / li >
2013-10-25 15:54:40 +00:00
< li >
< a href = "Phaser.RenderTexture.html" > RenderTexture< / a >
< / li >
2013-10-03 00:21:08 +00:00
< li >
< a href = "Phaser.RequestAnimationFrame.html" > RequestAnimationFrame< / a >
< / li >
2014-03-14 06:36:05 +00:00
< li >
< a href = "Phaser.RetroFont.html" > RetroFont< / a >
< / li >
< li >
< a href = "Phaser.ScaleManager.html" > ScaleManager< / a >
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.Signal.html" > Signal< / a >
< / li >
2014-02-05 06:29:17 +00:00
< li >
< a href = "Phaser.SinglePad.html" > SinglePad< / a >
< / li >
2013-10-03 00:21:08 +00:00
< li >
< a href = "Phaser.Sound.html" > Sound< / a >
< / li >
< li >
< a href = "Phaser.SoundManager.html" > SoundManager< / a >
< / li >
2013-10-25 15:54:40 +00:00
< li >
< a href = "Phaser.Sprite.html" > Sprite< / a >
< / li >
2014-02-18 03:01:51 +00:00
< li >
< a href = "Phaser.SpriteBatch.html" > SpriteBatch< / a >
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.Stage.html" > Stage< / a >
< / li >
< li >
< a href = "Phaser.State.html" > State< / a >
< / li >
< li >
< a href = "Phaser.StateManager.html" > StateManager< / a >
< / li >
2013-10-25 15:54:40 +00:00
< li >
< a href = "Phaser.Text.html" > Text< / a >
< / li >
2013-11-28 15:57:09 +00:00
< li >
< a href = "Phaser.Tile.html" > Tile< / a >
< / li >
< li >
< a href = "Phaser.Tilemap.html" > Tilemap< / a >
< / li >
< li >
< a href = "Phaser.TilemapLayer.html" > TilemapLayer< / a >
< / li >
< li >
< a href = "Phaser.TilemapParser.html" > TilemapParser< / a >
< / li >
< li >
< a href = "Phaser.Tileset.html" > Tileset< / a >
< / li >
2013-10-25 15:54:40 +00:00
< li >
< a href = "Phaser.TileSprite.html" > TileSprite< / a >
< / li >
2013-10-03 01:38:35 +00:00
< li >
< a href = "Phaser.Time.html" > Time< / a >
< / li >
2013-11-28 15:57:09 +00:00
< li >
< a href = "Phaser.Timer.html" > Timer< / a >
< / li >
2014-02-05 06:29:17 +00:00
< li >
< a href = "Phaser.TimerEvent.html" > TimerEvent< / a >
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.Touch.html" > Touch< / a >
< / li >
2013-10-03 01:38:35 +00:00
< li >
< a href = "Phaser.Tween.html" > Tween< / a >
< / li >
< li >
< a href = "Phaser.TweenManager.html" > TweenManager< / a >
< / li >
< li >
< a href = "Phaser.Utils.html" > Utils< / a >
< / li >
< li >
< a href = "Phaser.Utils.Debug.html" > Debug< / a >
< / li >
2013-10-02 14:05:55 +00:00
< li >
< a href = "Phaser.World.html" > World< / a >
< / li >
< li >
< a href = "SignalBinding.html" > SignalBinding< / a >
< / li >
< / ul >
< / li >
< / ul >
< / div >
< / div >
< div class = "row-fluid" >
< div class = "span12" >
< div id = "main" >
< h1 class = "page-title" > Source: input/InputHandler.js< / h1 >
< section >
< article >
< pre class = "sunlight-highlight-javascript linenums" > /**
* @author Richard Davey < rich@photonstorm.com>
2014-02-05 06:29:17 +00:00
* @copyright 2014 Photon Storm Ltd.
2013-10-02 14:05:55 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
2013-11-28 15:57:09 +00:00
* The Input Handler is bound to a specific Sprite and is responsible for managing all Input events on that Sprite.
2013-10-02 14:05:55 +00:00
* @class Phaser.InputHandler
* @constructor
2013-11-28 15:57:09 +00:00
* @param {Phaser.Sprite} sprite - The Sprite object to which this Input Handler belongs.
2013-10-02 14:05:55 +00:00
*/
Phaser.InputHandler = function (sprite) {
2013-11-28 15:57:09 +00:00
/**
* @property {Phaser.Sprite} sprite - The Sprite object to which this Input Handler belongs.
*/
this.sprite = sprite;
2013-10-02 14:05:55 +00:00
/**
2014-03-28 01:42:49 +00:00
* @property {Phaser.Game} game - A reference to the currently running game.
2013-10-02 14:05:55 +00:00
*/
this.game = sprite.game;
2013-11-28 15:57:09 +00:00
/**
* @property {boolean} enabled - If enabled the Input Handler will process input requests and monitor pointer activity.
* @default
*/
2013-10-02 14:05:55 +00:00
this.enabled = false;
2013-11-28 15:57:09 +00:00
/**
* @property {number} priorityID - The PriorityID controls which Sprite receives an Input event first if they should overlap.
* @default
*/
2013-10-02 14:05:55 +00:00
this.priorityID = 0;
2014-03-28 01:42:49 +00:00
2013-11-28 15:57:09 +00:00
/**
* @property {boolean} useHandCursor - On a desktop browser you can set the 'hand' cursor to appear when moving over the Sprite.
* @default
*/
2013-10-02 14:05:55 +00:00
this.useHandCursor = false;
2014-03-28 01:42:49 +00:00
2014-03-24 12:41:45 +00:00
/**
* @property {boolean} _setHandCursor - Did this Sprite trigger the hand cursor?
* @private
*/
this._setHandCursor = false;
2013-11-28 15:57:09 +00:00
/**
* @property {boolean} isDragged - true if the Sprite is being currently dragged.
* @default
*/
2013-10-02 14:05:55 +00:00
this.isDragged = false;
2014-03-28 01:42:49 +00:00
2013-11-28 15:57:09 +00:00
/**
* @property {boolean} allowHorizontalDrag - Controls if the Sprite is allowed to be dragged horizontally.
* @default
*/
2013-10-02 14:05:55 +00:00
this.allowHorizontalDrag = true;
2014-03-28 01:42:49 +00:00
2013-11-28 15:57:09 +00:00
/**
* @property {boolean} allowVerticalDrag - Controls if the Sprite is allowed to be dragged vertically.
* @default
*/
2013-10-02 14:05:55 +00:00
this.allowVerticalDrag = true;
2014-03-28 01:42:49 +00:00
2013-11-28 15:57:09 +00:00
/**
* @property {boolean} bringToTop - If true when this Sprite is clicked or dragged it will automatically be bought to the top of the Group it is within.
* @default
*/
2013-10-02 14:05:55 +00:00
this.bringToTop = false;
2013-11-28 15:57:09 +00:00
/**
* @property {Phaser.Point} snapOffset - A Point object that contains by how far the Sprite snap is offset.
* @default
*/
2013-10-02 14:05:55 +00:00
this.snapOffset = null;
2014-03-28 01:42:49 +00:00
2013-11-28 15:57:09 +00:00
/**
* @property {boolean} snapOnDrag - When the Sprite is dragged this controls if the center of the Sprite will snap to the pointer on drag or not.
* @default
*/
2013-10-02 14:05:55 +00:00
this.snapOnDrag = false;
2014-03-28 01:42:49 +00:00
2013-11-28 15:57:09 +00:00
/**
* @property {boolean} snapOnRelease - When the Sprite is dragged this controls if the Sprite will be snapped on release.
* @default
*/
2013-10-02 14:05:55 +00:00
this.snapOnRelease = false;
2014-03-28 01:42:49 +00:00
2013-11-28 15:57:09 +00:00
/**
* @property {number} snapX - When a Sprite has snapping enabled this holds the width of the snap grid.
* @default
*/
2013-10-02 14:05:55 +00:00
this.snapX = 0;
2014-03-28 01:42:49 +00:00
2013-11-28 15:57:09 +00:00
/**
* @property {number} snapY - When a Sprite has snapping enabled this holds the height of the snap grid.
* @default
*/
2013-10-02 14:05:55 +00:00
this.snapY = 0;
2014-02-05 06:29:17 +00:00
/**
* @property {number} snapOffsetX - This defines the top-left X coordinate of the snap grid.
* @default
*/
this.snapOffsetX = 0;
2014-03-28 01:42:49 +00:00
2014-02-05 06:29:17 +00:00
/**
* @property {number} snapOffsetY - This defines the top-left Y coordinate of the snap grid..
* @default
*/
this.snapOffsetY = 0;
2013-11-28 15:57:09 +00:00
/**
2014-02-18 03:01:51 +00:00
* Set to true to use pixel perfect hit detection when checking if the pointer is over this Sprite.
* The x/y coordinates of the pointer are tested against the image in combination with the InputHandler.pixelPerfectAlpha value.
* Warning: This is expensive, especially on mobile (where it's not even needed!) so only enable if required. Also see the less-expensive InputHandler.pixelPerfectClick.
* @property {number} pixelPerfectOver - Use a pixel perfect check when testing for pointer over.
2013-11-28 15:57:09 +00:00
* @default
*/
2014-02-18 03:01:51 +00:00
this.pixelPerfectOver = false;
/**
* Set to true to use pixel perfect hit detection when checking if the pointer is over this Sprite when it's clicked or touched.
* The x/y coordinates of the pointer are tested against the image in combination with the InputHandler.pixelPerfectAlpha value.
* Warning: This is expensive so only enable if you really need it.
* @property {number} pixelPerfectClick - Use a pixel perfect check when testing for clicks or touches on the Sprite.
* @default
*/
this.pixelPerfectClick = false;
2013-10-02 14:05:55 +00:00
/**
* @property {number} pixelPerfectAlpha - The alpha tolerance threshold. If the alpha value of the pixel matches or is above this value, it's considered a hit.
* @default
*/
this.pixelPerfectAlpha = 255;
/**
* @property {boolean} draggable - Is this sprite allowed to be dragged by the mouse? true = yes, false = no
2014-03-28 01:42:49 +00:00
* @default
2013-10-02 14:05:55 +00:00
*/
this.draggable = false;
/**
2013-11-28 15:57:09 +00:00
* @property {Phaser.Rectangle} boundsRect - A region of the game world within which the sprite is restricted during drag.
2014-03-28 01:42:49 +00:00
* @default
2013-10-02 14:05:55 +00:00
*/
this.boundsRect = null;
/**
2013-11-28 15:57:09 +00:00
* @property {Phaser.Sprite} boundsSprite - A Sprite the bounds of which this sprite is restricted during drag.
2013-10-02 14:05:55 +00:00
* @default
*/
this.boundsSprite = null;
/**
* If this object is set to consume the pointer event then it will stop all propogation from this object on.
* For example if you had a stack of 6 sprites with the same priority IDs and one consumed the event, none of the others would receive it.
* @property {boolean} consumePointerEvent
* @default
*/
this.consumePointerEvent = false;
/**
2014-02-18 03:01:51 +00:00
* @property {Phaser.Point} _tempPoint - Internal cache var.
2013-10-02 14:05:55 +00:00
* @private
*/
2013-11-28 15:57:09 +00:00
this._tempPoint = new Phaser.Point();
2013-10-02 14:05:55 +00:00
2014-02-18 03:01:51 +00:00
/**
* @property {array} _pointerData - Internal cache var.
* @private
*/
2013-10-02 14:05:55 +00:00
this._pointerData = [];
this._pointerData.push({
id: 0,
x: 0,
y: 0,
isDown: false,
isUp: false,
isOver: false,
isOut: false,
timeOver: 0,
timeOut: 0,
timeDown: 0,
timeUp: 0,
downDuration: 0,
isDragged: false
});
};
Phaser.InputHandler.prototype = {
2013-11-28 15:57:09 +00:00
/**
* Starts the Input Handler running. This is called automatically when you enable input on a Sprite, or can be called directly if you need to set a specific priority.
* @method Phaser.InputHandler#start
* @param {number} priority - Higher priority sprites take click priority over low-priority sprites when they are stacked on-top of each other.
* @param {boolean} useHandCursor - If true the Sprite will show the hand cursor on mouse-over (doesn't apply to mobile browsers)
* @return {Phaser.Sprite} The Sprite object to which the Input Handler is bound.
*/
start: function (priority, useHandCursor) {
priority = priority || 0;
if (typeof useHandCursor == 'undefined') { useHandCursor = false; }
2013-10-02 14:05:55 +00:00
// Turning on
2013-11-28 15:57:09 +00:00
if (this.enabled === false)
2013-10-02 14:05:55 +00:00
{
// Register, etc
2013-11-28 15:57:09 +00:00
this.game.input.interactiveItems.add(this);
2013-10-02 14:05:55 +00:00
this.useHandCursor = useHandCursor;
this.priorityID = priority;
for (var i = 0; i < 10; i++)
{
this._pointerData[i] = {
id: i,
x: 0,
y: 0,
isDown: false,
isUp: false,
isOver: false,
isOut: false,
timeOver: 0,
timeOut: 0,
timeDown: 0,
timeUp: 0,
downDuration: 0,
isDragged: false
};
}
2013-11-28 15:57:09 +00:00
this.snapOffset = new Phaser.Point();
2013-10-02 14:05:55 +00:00
this.enabled = true;
// Create the signals the Input component will emit
2014-02-08 07:24:22 +00:00
if (this.sprite.events & & this.sprite.events.onInputOver === null)
2013-10-02 14:05:55 +00:00
{
2013-11-28 15:57:09 +00:00
this.sprite.events.onInputOver = new Phaser.Signal();
this.sprite.events.onInputOut = new Phaser.Signal();
this.sprite.events.onInputDown = new Phaser.Signal();
this.sprite.events.onInputUp = new Phaser.Signal();
this.sprite.events.onDragStart = new Phaser.Signal();
this.sprite.events.onDragStop = new Phaser.Signal();
2013-10-02 14:05:55 +00:00
}
}
return this.sprite;
2013-11-28 15:57:09 +00:00
},
2013-10-02 14:05:55 +00:00
2013-11-28 15:57:09 +00:00
/**
* Resets the Input Handler and disables it.
* @method Phaser.InputHandler#reset
*/
2013-10-02 14:05:55 +00:00
reset: function () {
this.enabled = false;
for (var i = 0; i < 10; i++)
{
this._pointerData[i] = {
id: i,
x: 0,
y: 0,
isDown: false,
isUp: false,
isOver: false,
isOut: false,
timeOver: 0,
timeOut: 0,
timeDown: 0,
timeUp: 0,
downDuration: 0,
isDragged: false
};
}
},
2013-11-28 15:57:09 +00:00
/**
* Stops the Input Handler from running.
* @method Phaser.InputHandler#stop
*/
stop: function () {
2013-10-02 14:05:55 +00:00
// Turning off
2013-11-28 15:57:09 +00:00
if (this.enabled === false)
2013-10-02 14:05:55 +00:00
{
return;
}
else
{
// De-register, etc
this.enabled = false;
2013-11-28 15:57:09 +00:00
this.game.input.interactiveItems.remove(this);
2013-10-02 14:05:55 +00:00
}
2013-11-28 15:57:09 +00:00
},
2013-10-02 14:05:55 +00:00
2013-11-28 15:57:09 +00:00
/**
* Clean up memory.
* @method Phaser.InputHandler#destroy
*/
2013-10-02 14:05:55 +00:00
destroy: function () {
if (this.enabled)
{
2014-03-24 12:41:45 +00:00
if (this._setHandCursor)
{
this.game.canvas.style.cursor = "default";
this._setHandCursor = false;
}
2013-10-25 15:54:40 +00:00
this.enabled = false;
this.game.input.interactiveItems.remove(this);
2014-02-18 03:01:51 +00:00
this._pointerData.length = 0;
this.boundsRect = null;
this.boundsSprite = null;
2013-11-28 15:57:09 +00:00
this.sprite = null;
2013-10-02 14:05:55 +00:00
}
2014-02-18 03:01:51 +00:00
2013-10-02 14:05:55 +00:00
},
2014-03-24 12:41:45 +00:00
/**
* Checks if the object this InputHandler is bound to is valid for consideration in the Pointer move event.
* This is called by Phaser.Pointer and shouldn't typically be called directly.
*
* @method Phaser.InputHandler#validForInput
* @protected
* @param {number} highestID - The highest ID currently processed by the Pointer.
* @param {number} highestRenderID - The highest Render Order ID currently processed by the Pointer.
* @return {boolean} True if the object this InputHandler is bound to should be considered as valid for input detection.
*/
validForInput: function (highestID, highestRenderID) {
if (this.sprite.scale.x === 0 || this.sprite.scale.y === 0)
{
return false;
}
if (this.pixelPerfectClick || this.pixelPerfectOver)
{
return true;
}
if (this.priorityID > highestID || (this.priorityID === highestID & & this.sprite._cache[3] < highestRenderID))
{
return true;
}
return false;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* The x coordinate of the Input pointer, relative to the top-left of the parent Sprite.
* This value is only set when the pointer is over this Sprite.
* @method Phaser.InputHandler#pointerX
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {number} The x coordinate of the Input pointer.
2013-11-28 15:57:09 +00:00
*/
2013-10-02 14:05:55 +00:00
pointerX: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
return this._pointerData[pointer].x;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* The y coordinate of the Input pointer, relative to the top-left of the parent Sprite
* This value is only set when the pointer is over this Sprite.
* @method Phaser.InputHandler#pointerY
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {number} The y coordinate of the Input pointer.
*/
pointerY: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
return this._pointerData[pointer].y;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* If the Pointer is touching the touchscreen, or the mouse button is held down, isDown is set to true.
* @method Phaser.InputHandler#pointerDown
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {boolean}
*/
pointerDown: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
return this._pointerData[pointer].isDown;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* If the Pointer is not touching the touchscreen, or the mouse button is up, isUp is set to true
* @method Phaser.InputHandler#pointerUp
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {boolean}
*/
pointerUp: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
return this._pointerData[pointer].isUp;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* A timestamp representing when the Pointer first touched the touchscreen.
* @method Phaser.InputHandler#pointerTimeDown
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {number}
*/
pointerTimeDown: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
return this._pointerData[pointer].timeDown;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* A timestamp representing when the Pointer left the touchscreen.
* @method Phaser.InputHandler#pointerTimeUp
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {number}
*/
pointerTimeUp: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
return this._pointerData[pointer].timeUp;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Is the Pointer over this Sprite?
* @method Phaser.InputHandler#pointerOver
2013-11-07 06:10:15 +00:00
* @param {number} [index] - The ID number of a Pointer to check. If you don't provide a number it will check all Pointers.
* @return {boolean} True if the given pointer (if a index was given, or any pointer if not) is over this object.
2013-10-02 14:05:55 +00:00
*/
2013-11-07 06:10:15 +00:00
pointerOver: function (index) {
2013-10-02 14:05:55 +00:00
2013-11-07 06:10:15 +00:00
if (this.enabled)
{
if (typeof index === 'undefined')
{
for (var i = 0; i < 10; i++)
{
if (this._pointerData[i].isOver)
{
return true;
}
}
}
else
{
return this._pointerData[index].isOver;
}
}
2013-10-02 14:05:55 +00:00
2013-11-07 06:10:15 +00:00
return false;
2013-10-02 14:05:55 +00:00
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Is the Pointer outside of this Sprite?
* @method Phaser.InputHandler#pointerOut
2013-11-07 06:10:15 +00:00
* @param {number} [index] - The ID number of a Pointer to check. If you don't provide a number it will check all Pointers.
* @return {boolean} True if the given pointer (if a index was given, or any pointer if not) is out of this object.
2013-10-02 14:05:55 +00:00
*/
2013-11-28 15:57:09 +00:00
pointerOut: function (index) {
2013-10-02 14:05:55 +00:00
2013-11-07 06:10:15 +00:00
if (this.enabled)
{
if (typeof index === 'undefined')
{
for (var i = 0; i < 10; i++)
{
if (this._pointerData[i].isOut)
{
return true;
}
}
}
else
{
return this._pointerData[index].isOut;
}
}
2013-10-02 14:05:55 +00:00
2013-11-07 06:10:15 +00:00
return false;
2013-10-02 14:05:55 +00:00
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* A timestamp representing when the Pointer first touched the touchscreen.
* @method Phaser.InputHandler#pointerTimeOver
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {number}
*/
pointerTimeOver: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
return this._pointerData[pointer].timeOver;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* A timestamp representing when the Pointer left the touchscreen.
* @method Phaser.InputHandler#pointerTimeOut
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {number}
*/
pointerTimeOut: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
return this._pointerData[pointer].timeOut;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Is this sprite being dragged by the mouse or not?
2014-02-21 15:37:26 +00:00
* @method Phaser.InputHandler#pointerDragged
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2014-02-21 15:37:26 +00:00
* @return {boolean} True if the pointer is dragging an object, otherwise false.
2013-10-02 14:05:55 +00:00
*/
pointerDragged: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
return this._pointerData[pointer].isDragged;
},
2014-02-18 03:01:51 +00:00
/**
* Checks if the given pointer is over this Sprite and can click it.
* @method Phaser.InputHandler#checkPointerDown
* @param {Phaser.Pointer} pointer
2014-02-21 15:37:26 +00:00
* @return {boolean} True if the pointer is down, otherwise false.
2014-02-18 03:01:51 +00:00
*/
checkPointerDown: function (pointer) {
if (this.enabled === false || this.sprite.visible === false || this.sprite.parent.visible === false)
{
return false;
}
// Need to pass it a temp point, in case we need it again for the pixel check
if (this.game.input.hitTest(this.sprite, pointer, this._tempPoint))
{
if (this.pixelPerfectClick)
{
return this.checkPixel(this._tempPoint.x, this._tempPoint.y);
}
else
{
return true;
}
}
return false;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Checks if the given pointer is over this Sprite.
* @method Phaser.InputHandler#checkPointerOver
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {boolean}
*/
checkPointerOver: function (pointer) {
2014-02-08 07:24:22 +00:00
if (this.enabled === false || this.sprite.visible === false || this.sprite.parent.visible === false)
2013-10-02 14:05:55 +00:00
{
2013-11-28 15:57:09 +00:00
return false;
}
2014-02-08 07:24:22 +00:00
// Need to pass it a temp point, in case we need it again for the pixel check
if (this.game.input.hitTest(this.sprite, pointer, this._tempPoint))
2013-11-28 15:57:09 +00:00
{
2014-02-18 03:01:51 +00:00
if (this.pixelPerfectOver)
2013-10-02 14:05:55 +00:00
{
2013-11-28 15:57:09 +00:00
return this.checkPixel(this._tempPoint.x, this._tempPoint.y);
}
else
{
return true;
2013-10-02 14:05:55 +00:00
}
}
2014-02-08 07:24:22 +00:00
return false;
2013-10-02 14:05:55 +00:00
},
2013-11-28 15:57:09 +00:00
/**
2013-11-07 06:10:15 +00:00
* Runs a pixel perfect check against the given x/y coordinates of the Sprite this InputHandler is bound to.
* It compares the alpha value of the pixel and if >= InputHandler.pixelPerfectAlpha it returns true.
* @method Phaser.InputHandler#checkPixel
* @param {number} x - The x coordinate to check.
* @param {number} y - The y coordinate to check.
2014-02-18 03:01:51 +00:00
* @param {Phaser.Pointer} [pointer] - The pointer to get the x/y coordinate from if not passed as the first two parameters.
2013-11-07 06:10:15 +00:00
* @return {boolean} true if there is the alpha of the pixel is >= InputHandler.pixelPerfectAlpha
*/
2014-02-18 03:01:51 +00:00
checkPixel: function (x, y, pointer) {
2013-10-02 14:05:55 +00:00
// Grab a pixel from our image into the hitCanvas and then test it
if (this.sprite.texture.baseTexture.source)
{
this.game.input.hitContext.clearRect(0, 0, 1, 1);
2014-02-18 03:01:51 +00:00
if (x === null & & y === null)
{
// Use the pointer parameter
this.game.input.getLocalPosition(this.sprite, pointer, this._tempPoint);
var x = this._tempPoint.x;
var y = this._tempPoint.y;
}
if (this.sprite.anchor.x !== 0)
{
x -= -this.sprite.texture.frame.width * this.sprite.anchor.x;
}
if (this.sprite.anchor.y !== 0)
{
y -= -this.sprite.texture.frame.height * this.sprite.anchor.y;
}
2013-10-25 15:54:40 +00:00
x += this.sprite.texture.frame.x;
y += this.sprite.texture.frame.y;
2013-10-02 14:05:55 +00:00
this.game.input.hitContext.drawImage(this.sprite.texture.baseTexture.source, x, y, 1, 1, 0, 0, 1, 1);
2014-03-28 01:42:49 +00:00
2013-10-02 14:05:55 +00:00
var rgb = this.game.input.hitContext.getImageData(0, 0, 1, 1);
if (rgb.data[3] >= this.pixelPerfectAlpha)
{
return true;
}
}
return false;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Update.
* @method Phaser.InputHandler#update
2014-02-18 03:01:51 +00:00
* @protected
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
*/
update: function (pointer) {
2014-02-18 03:01:51 +00:00
if (this.sprite === null)
{
// Abort. We've been destroyed.
return;
}
2014-02-21 15:37:26 +00:00
if (!this.enabled || !this.sprite.visible || !this.sprite.parent.visible)
2013-10-02 14:05:55 +00:00
{
this._pointerOutHandler(pointer);
return false;
}
if (this.draggable & & this._draggedPointerID == pointer.id)
{
return this.updateDrag(pointer);
}
2013-11-28 15:57:09 +00:00
else if (this._pointerData[pointer.id].isOver === true)
2013-10-02 14:05:55 +00:00
{
if (this.checkPointerOver(pointer))
{
this._pointerData[pointer.id].x = pointer.x - this.sprite.x;
this._pointerData[pointer.id].y = pointer.y - this.sprite.y;
return true;
}
else
{
this._pointerOutHandler(pointer);
return false;
}
}
},
2013-11-28 15:57:09 +00:00
/**
* Internal method handling the pointer over event.
* @method Phaser.InputHandler#_pointerOverHandler
* @private
* @param {Phaser.Pointer} pointer
*/
2013-10-02 14:05:55 +00:00
_pointerOverHandler: function (pointer) {
2014-02-18 03:01:51 +00:00
if (this.sprite === null)
{
// Abort. We've been destroyed.
return;
}
2013-11-28 15:57:09 +00:00
if (this._pointerData[pointer.id].isOver === false)
2013-10-02 14:05:55 +00:00
{
this._pointerData[pointer.id].isOver = true;
this._pointerData[pointer.id].isOut = false;
this._pointerData[pointer.id].timeOver = this.game.time.now;
this._pointerData[pointer.id].x = pointer.x - this.sprite.x;
this._pointerData[pointer.id].y = pointer.y - this.sprite.y;
2013-11-28 15:57:09 +00:00
if (this.useHandCursor & & this._pointerData[pointer.id].isDragged === false)
2013-10-02 14:05:55 +00:00
{
2014-02-05 06:29:17 +00:00
this.game.canvas.style.cursor = "pointer";
2014-03-24 12:41:45 +00:00
this._setHandCursor = false;
2013-10-02 14:05:55 +00:00
}
this.sprite.events.onInputOver.dispatch(this.sprite, pointer);
}
2014-02-18 03:01:51 +00:00
2013-10-02 14:05:55 +00:00
},
2013-11-28 15:57:09 +00:00
/**
* Internal method handling the pointer out event.
* @method Phaser.InputHandler#_pointerOutHandler
* @private
* @param {Phaser.Pointer} pointer
*/
2013-10-02 14:05:55 +00:00
_pointerOutHandler: function (pointer) {
2014-02-18 03:01:51 +00:00
if (this.sprite === null)
{
// Abort. We've been destroyed.
return;
}
2013-10-02 14:05:55 +00:00
this._pointerData[pointer.id].isOver = false;
this._pointerData[pointer.id].isOut = true;
this._pointerData[pointer.id].timeOut = this.game.time.now;
2013-11-28 15:57:09 +00:00
if (this.useHandCursor & & this._pointerData[pointer.id].isDragged === false)
2013-10-02 14:05:55 +00:00
{
2014-02-05 06:29:17 +00:00
this.game.canvas.style.cursor = "default";
2014-03-24 12:41:45 +00:00
this._setHandCursor = false;
2013-10-02 14:05:55 +00:00
}
2013-10-25 15:54:40 +00:00
if (this.sprite & & this.sprite.events)
{
this.sprite.events.onInputOut.dispatch(this.sprite, pointer);
}
2013-10-02 14:05:55 +00:00
},
2013-11-28 15:57:09 +00:00
/**
* Internal method handling the touched event.
* @method Phaser.InputHandler#_touchedHandler
* @private
* @param {Phaser.Pointer} pointer
*/
2013-10-02 14:05:55 +00:00
_touchedHandler: function (pointer) {
2014-02-18 03:01:51 +00:00
if (this.sprite === null)
{
// Abort. We've been destroyed.
return;
}
2013-11-28 15:57:09 +00:00
if (this._pointerData[pointer.id].isDown === false & & this._pointerData[pointer.id].isOver === true)
2013-10-02 14:05:55 +00:00
{
2014-02-18 03:01:51 +00:00
if (this.pixelPerfectClick & & !this.checkPixel(null, null, pointer))
{
return;
}
2013-10-02 14:05:55 +00:00
this._pointerData[pointer.id].isDown = true;
this._pointerData[pointer.id].isUp = false;
this._pointerData[pointer.id].timeDown = this.game.time.now;
this.sprite.events.onInputDown.dispatch(this.sprite, pointer);
// Start drag
2013-11-28 15:57:09 +00:00
if (this.draggable & & this.isDragged === false)
2013-10-02 14:05:55 +00:00
{
this.startDrag(pointer);
}
if (this.bringToTop)
{
this.sprite.bringToTop();
2013-11-28 15:57:09 +00:00
}
2013-10-02 14:05:55 +00:00
}
// Consume the event?
return this.consumePointerEvent;
},
2013-11-28 15:57:09 +00:00
/**
* Internal method handling the pointer released event.
* @method Phaser.InputHandler#_releasedHandler
* @private
* @param {Phaser.Pointer} pointer
*/
2013-10-02 14:05:55 +00:00
_releasedHandler: function (pointer) {
2014-02-18 03:01:51 +00:00
if (this.sprite === null)
{
// Abort. We've been destroyed.
return;
}
2013-10-02 14:05:55 +00:00
// If was previously touched by this Pointer, check if still is AND still over this item
if (this._pointerData[pointer.id].isDown & & pointer.isUp)
{
this._pointerData[pointer.id].isDown = false;
this._pointerData[pointer.id].isUp = true;
this._pointerData[pointer.id].timeUp = this.game.time.now;
this._pointerData[pointer.id].downDuration = this._pointerData[pointer.id].timeUp - this._pointerData[pointer.id].timeDown;
// Only release the InputUp signal if the pointer is still over this sprite
if (this.checkPointerOver(pointer))
{
2014-02-05 06:29:17 +00:00
// Release the inputUp signal and provide optional parameter if pointer is still over the sprite or not
this.sprite.events.onInputUp.dispatch(this.sprite, pointer, true);
2013-10-02 14:05:55 +00:00
}
else
{
2014-02-05 06:29:17 +00:00
// Release the inputUp signal and provide optional parameter if pointer is still over the sprite or not
this.sprite.events.onInputUp.dispatch(this.sprite, pointer, false);
2013-10-02 14:05:55 +00:00
// Pointer outside the sprite? Reset the cursor
if (this.useHandCursor)
{
2014-02-05 06:29:17 +00:00
this.game.canvas.style.cursor = "default";
2014-03-24 12:41:45 +00:00
this._setHandCursor = false;
2013-10-02 14:05:55 +00:00
}
}
// Stop drag
if (this.draggable & & this.isDragged & & this._draggedPointerID == pointer.id)
{
this.stopDrag(pointer);
}
}
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Updates the Pointer drag on this Sprite.
* @method Phaser.InputHandler#updateDrag
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {boolean}
*/
updateDrag: function (pointer) {
if (pointer.isUp)
{
this.stopDrag(pointer);
return false;
}
2014-02-05 06:29:17 +00:00
if (this.sprite.fixedToCamera)
2013-10-02 14:05:55 +00:00
{
2014-02-05 06:29:17 +00:00
if (this.allowHorizontalDrag)
{
this.sprite.cameraOffset.x = pointer.x + this._dragPoint.x + this.dragOffset.x;
}
2013-10-02 14:05:55 +00:00
2014-02-05 06:29:17 +00:00
if (this.allowVerticalDrag)
{
this.sprite.cameraOffset.y = pointer.y + this._dragPoint.y + this.dragOffset.y;
}
2013-10-02 14:05:55 +00:00
2014-02-05 06:29:17 +00:00
if (this.boundsRect)
{
this.checkBoundsRect();
}
2013-10-02 14:05:55 +00:00
2014-02-05 06:29:17 +00:00
if (this.boundsSprite)
{
this.checkBoundsSprite();
}
2013-10-02 14:05:55 +00:00
2014-02-05 06:29:17 +00:00
if (this.snapOnDrag)
{
this.sprite.cameraOffset.x = Math.round((this.sprite.cameraOffset.x - (this.snapOffsetX % this.snapX)) / this.snapX) * this.snapX + (this.snapOffsetX % this.snapX);
this.sprite.cameraOffset.y = Math.round((this.sprite.cameraOffset.y - (this.snapOffsetY % this.snapY)) / this.snapY) * this.snapY + (this.snapOffsetY % this.snapY);
}
}
else
2013-10-02 14:05:55 +00:00
{
2014-02-05 06:29:17 +00:00
if (this.allowHorizontalDrag)
{
this.sprite.x = pointer.x + this._dragPoint.x + this.dragOffset.x;
}
if (this.allowVerticalDrag)
{
this.sprite.y = pointer.y + this._dragPoint.y + this.dragOffset.y;
}
if (this.boundsRect)
{
this.checkBoundsRect();
}
if (this.boundsSprite)
{
this.checkBoundsSprite();
}
if (this.snapOnDrag)
{
this.sprite.x = Math.round((this.sprite.x - (this.snapOffsetX % this.snapX)) / this.snapX) * this.snapX + (this.snapOffsetX % this.snapX);
this.sprite.y = Math.round((this.sprite.y - (this.snapOffsetY % this.snapY)) / this.snapY) * this.snapY + (this.snapOffsetY % this.snapY);
}
2013-10-02 14:05:55 +00:00
}
return true;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Returns true if the pointer has entered the Sprite within the specified delay time (defaults to 500ms, half a second)
* @method Phaser.InputHandler#justOver
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @param {number} delay - The time below which the pointer is considered as just over.
* @return {boolean}
*/
justOver: function (pointer, delay) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
delay = delay || 500;
2013-10-02 14:05:55 +00:00
return (this._pointerData[pointer].isOver & & this.overDuration(pointer) < delay);
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Returns true if the pointer has left the Sprite within the specified delay time (defaults to 500ms, half a second)
* @method Phaser.InputHandler#justOut
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @param {number} delay - The time below which the pointer is considered as just out.
* @return {boolean}
*/
justOut: function (pointer, delay) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
delay = delay || 500;
2013-10-02 14:05:55 +00:00
return (this._pointerData[pointer].isOut & & (this.game.time.now - this._pointerData[pointer].timeOut < delay));
},
2013-11-28 15:57:09 +00:00
/**
2014-02-18 03:01:51 +00:00
* Returns true if the pointer has touched or clicked on the Sprite within the specified delay time (defaults to 500ms, half a second)
2013-10-02 14:05:55 +00:00
* @method Phaser.InputHandler#justPressed
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @param {number} delay - The time below which the pointer is considered as just over.
* @return {boolean}
*/
justPressed: function (pointer, delay) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
delay = delay || 500;
2013-10-02 14:05:55 +00:00
return (this._pointerData[pointer].isDown & & this.downDuration(pointer) < delay);
},
2013-11-28 15:57:09 +00:00
/**
2014-02-18 03:01:51 +00:00
* Returns true if the pointer was touching this Sprite, but has been released within the specified delay time (defaults to 500ms, half a second)
2013-10-02 14:05:55 +00:00
* @method Phaser.InputHandler#justReleased
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @param {number} delay - The time below which the pointer is considered as just out.
* @return {boolean}
*/
justReleased: function (pointer, delay) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
delay = delay || 500;
2013-10-02 14:05:55 +00:00
return (this._pointerData[pointer].isUp & & (this.game.time.now - this._pointerData[pointer].timeUp < delay));
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* If the pointer is currently over this Sprite this returns how long it has been there for in milliseconds.
* @method Phaser.InputHandler#overDuration
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {number} The number of milliseconds the pointer has been over the Sprite, or -1 if not over.
*/
overDuration: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
if (this._pointerData[pointer].isOver)
{
return this.game.time.now - this._pointerData[pointer].timeOver;
}
return -1;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* If the pointer is currently over this Sprite this returns how long it has been there for in milliseconds.
* @method Phaser.InputHandler#downDuration
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
* @return {number} The number of milliseconds the pointer has been pressed down on the Sprite, or -1 if not over.
*/
downDuration: function (pointer) {
2013-11-28 15:57:09 +00:00
pointer = pointer || 0;
2013-10-02 14:05:55 +00:00
if (this._pointerData[pointer].isDown)
{
return this.game.time.now - this._pointerData[pointer].timeDown;
}
return -1;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Make this Sprite draggable by the mouse. You can also optionally set mouseStartDragCallback and mouseStopDragCallback
* @method Phaser.InputHandler#enableDrag
2013-11-28 15:57:09 +00:00
* @param {boolean} [lockCenter=false] - 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.
* @param {boolean} [bringToTop=false] - If true the Sprite will be bought to the top of the rendering list in its current Group.
* @param {boolean} [pixelPerfect=false] - If true it will use a pixel perfect test to see if you clicked the Sprite. False uses the bounding box.
* @param {boolean} [alphaThreshold=255] - If using pixel perfect collision this specifies the alpha level from 0 to 255 above which a collision is processed.
* @param {Phaser.Rectangle} [boundsRect=null] - If you want to restrict the drag of this sprite to a specific Rectangle, pass the Phaser.Rectangle here, otherwise it's free to drag anywhere.
* @param {Phaser.Sprite} [boundsSprite=null] - If you want to restrict the drag of this sprite to within the bounding box of another sprite, pass it here.
2013-10-02 14:05:55 +00:00
*/
enableDrag: function (lockCenter, bringToTop, pixelPerfect, alphaThreshold, boundsRect, boundsSprite) {
if (typeof lockCenter == 'undefined') { lockCenter = false; }
if (typeof bringToTop == 'undefined') { bringToTop = false; }
if (typeof pixelPerfect == 'undefined') { pixelPerfect = false; }
2013-11-28 15:57:09 +00:00
if (typeof alphaThreshold == 'undefined') { alphaThreshold = 255; }
if (typeof boundsRect == 'undefined') { boundsRect = null; }
if (typeof boundsSprite == 'undefined') { boundsSprite = null; }
2013-10-02 14:05:55 +00:00
this._dragPoint = new Phaser.Point();
this.draggable = true;
this.bringToTop = bringToTop;
this.dragOffset = new Phaser.Point();
this.dragFromCenter = lockCenter;
this.pixelPerfect = pixelPerfect;
this.pixelPerfectAlpha = alphaThreshold;
if (boundsRect)
{
this.boundsRect = boundsRect;
}
if (boundsSprite)
{
this.boundsSprite = boundsSprite;
}
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Stops this sprite from being able to be dragged. If it is currently the target of an active drag it will be stopped immediately. Also disables any set callbacks.
* @method Phaser.InputHandler#disableDrag
*/
disableDrag: function () {
if (this._pointerData)
{
for (var i = 0; i < 10; i++)
{
this._pointerData[i].isDragged = false;
}
}
this.draggable = false;
this.isDragged = false;
this._draggedPointerID = -1;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Called by Pointer when drag starts on this Sprite. Should not usually be called directly.
* @method Phaser.InputHandler#startDrag
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
*/
startDrag: function (pointer) {
this.isDragged = true;
this._draggedPointerID = pointer.id;
this._pointerData[pointer.id].isDragged = true;
2014-02-05 06:29:17 +00:00
if (this.sprite.fixedToCamera)
2013-10-02 14:05:55 +00:00
{
2014-02-05 06:29:17 +00:00
if (this.dragFromCenter)
{
this.sprite.centerOn(pointer.x, pointer.y);
this._dragPoint.setTo(this.sprite.cameraOffset.x - pointer.x, this.sprite.cameraOffset.y - pointer.y);
}
else
{
this._dragPoint.setTo(this.sprite.cameraOffset.x - pointer.x, this.sprite.cameraOffset.y - pointer.y);
}
2013-10-02 14:05:55 +00:00
}
else
{
2014-02-05 06:29:17 +00:00
if (this.dragFromCenter)
{
2014-03-24 12:41:45 +00:00
var bounds = this.sprite.getBounds();
this.sprite.x = pointer.x + (this.sprite.x - bounds.centerX);
this.sprite.y = pointer.y + (this.sprite.y - bounds.centerY);
2014-02-05 06:29:17 +00:00
this._dragPoint.setTo(this.sprite.x - pointer.x, this.sprite.y - pointer.y);
}
else
{
this._dragPoint.setTo(this.sprite.x - pointer.x, this.sprite.y - pointer.y);
}
2013-10-02 14:05:55 +00:00
}
this.updateDrag(pointer);
2014-03-28 01:42:49 +00:00
2013-10-02 14:05:55 +00:00
if (this.bringToTop)
{
this.sprite.bringToTop();
}
this.sprite.events.onDragStart.dispatch(this.sprite, pointer);
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Called by Pointer when drag is stopped on this Sprite. Should not usually be called directly.
* @method Phaser.InputHandler#stopDrag
2013-11-28 15:57:09 +00:00
* @param {Phaser.Pointer} pointer
2013-10-02 14:05:55 +00:00
*/
stopDrag: function (pointer) {
this.isDragged = false;
this._draggedPointerID = -1;
this._pointerData[pointer.id].isDragged = false;
2014-03-28 01:42:49 +00:00
2013-10-02 14:05:55 +00:00
if (this.snapOnRelease)
{
2014-02-05 06:29:17 +00:00
if (this.sprite.fixedToCamera)
{
this.sprite.cameraOffset.x = Math.round((this.sprite.cameraOffset.x - (this.snapOffsetX % this.snapX)) / this.snapX) * this.snapX + (this.snapOffsetX % this.snapX);
this.sprite.cameraOffset.y = Math.round((this.sprite.cameraOffset.y - (this.snapOffsetY % this.snapY)) / this.snapY) * this.snapY + (this.snapOffsetY % this.snapY);
}
else
{
this.sprite.x = Math.round((this.sprite.x - (this.snapOffsetX % this.snapX)) / this.snapX) * this.snapX + (this.snapOffsetX % this.snapX);
this.sprite.y = Math.round((this.sprite.y - (this.snapOffsetY % this.snapY)) / this.snapY) * this.snapY + (this.snapOffsetY % this.snapY);
}
2013-10-02 14:05:55 +00:00
}
this.sprite.events.onDragStop.dispatch(this.sprite, pointer);
2013-11-28 15:57:09 +00:00
if (this.checkPointerOver(pointer) === false)
2013-11-01 18:16:52 +00:00
{
this._pointerOutHandler(pointer);
}
2013-10-02 14:05:55 +00:00
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Restricts this sprite to drag movement only on the given axis. Note: If both are set to false the sprite will never move!
* @method Phaser.InputHandler#setDragLock
2013-11-28 15:57:09 +00:00
* @param {boolean} [allowHorizontal=true] - To enable the sprite to be dragged horizontally set to true, otherwise false.
* @param {boolean} [allowVertical=true] - To enable the sprite to be dragged vertically set to true, otherwise false.
2013-10-02 14:05:55 +00:00
*/
setDragLock: function (allowHorizontal, allowVertical) {
if (typeof allowHorizontal == 'undefined') { allowHorizontal = true; }
2013-11-28 15:57:09 +00:00
if (typeof allowVertical == 'undefined') { allowVertical = true; }
2013-10-02 14:05:55 +00:00
this.allowHorizontalDrag = allowHorizontal;
this.allowVerticalDrag = allowVertical;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Make this Sprite snap to the given grid either during drag or when it's released.
* For example 16x16 as the snapX and snapY would make the sprite snap to every 16 pixels.
* @method Phaser.InputHandler#enableSnap
2013-11-28 15:57:09 +00:00
* @param {number} snapX - The width of the grid cell to snap to.
* @param {number} snapY - The height of the grid cell to snap to.
* @param {boolean} [onDrag=true] - If true the sprite will snap to the grid while being dragged.
* @param {boolean} [onRelease=false] - If true the sprite will snap to the grid when released.
2014-02-05 06:29:17 +00:00
* @param {number} [snapOffsetX=0] - Used to offset the top-left starting point of the snap grid.
* @param {number} [snapOffsetX=0] - Used to offset the top-left starting point of the snap grid.
2013-10-02 14:05:55 +00:00
*/
2014-03-14 06:36:05 +00:00
enableSnap: function (snapX, snapY, onDrag, onRelease, snapOffsetX, snapOffsetY) {
2013-10-02 14:05:55 +00:00
if (typeof onDrag == 'undefined') { onDrag = true; }
if (typeof onRelease == 'undefined') { onRelease = false; }
2014-02-05 06:29:17 +00:00
if (typeof snapOffsetX == 'undefined') { snapOffsetX = 0; }
if (typeof snapOffsetY == 'undefined') { snapOffsetY = 0; }
2013-10-02 14:05:55 +00:00
this.snapX = snapX;
this.snapY = snapY;
2014-02-05 06:29:17 +00:00
this.snapOffsetX = snapOffsetX;
this.snapOffsetY = snapOffsetY;
2013-10-02 14:05:55 +00:00
this.snapOnDrag = onDrag;
this.snapOnRelease = onRelease;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Stops the sprite from snapping to a grid during drag or release.
* @method Phaser.InputHandler#disableSnap
*/
disableSnap: function () {
this.snapOnDrag = false;
this.snapOnRelease = false;
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Bounds Rect check for the sprite drag
* @method Phaser.InputHandler#checkBoundsRect
*/
checkBoundsRect: function () {
2014-02-05 06:29:17 +00:00
if (this.sprite.fixedToCamera)
2013-10-02 14:05:55 +00:00
{
2014-02-05 06:29:17 +00:00
if (this.sprite.cameraOffset.x < this.boundsRect.left)
{
this.sprite.cameraOffset.x = this.boundsRect.cameraOffset.x;
}
else if ((this.sprite.cameraOffset.x + this.sprite.width) > this.boundsRect.right)
{
this.sprite.cameraOffset.x = this.boundsRect.right - this.sprite.width;
}
2013-10-02 14:05:55 +00:00
2014-02-05 06:29:17 +00:00
if (this.sprite.cameraOffset.y < this.boundsRect.top)
{
this.sprite.cameraOffset.y = this.boundsRect.top;
}
else if ((this.sprite.cameraOffset.y + this.sprite.height) > this.boundsRect.bottom)
{
this.sprite.cameraOffset.y = this.boundsRect.bottom - this.sprite.height;
}
2013-10-02 14:05:55 +00:00
}
2014-02-05 06:29:17 +00:00
else
2013-10-02 14:05:55 +00:00
{
2014-02-05 06:29:17 +00:00
if (this.sprite.x < this.boundsRect.left)
{
this.sprite.x = this.boundsRect.x;
}
else if ((this.sprite.x + this.sprite.width) > this.boundsRect.right)
{
this.sprite.x = this.boundsRect.right - this.sprite.width;
}
if (this.sprite.y < this.boundsRect.top)
{
this.sprite.y = this.boundsRect.top;
}
else if ((this.sprite.y + this.sprite.height) > this.boundsRect.bottom)
{
this.sprite.y = this.boundsRect.bottom - this.sprite.height;
}
2013-10-02 14:05:55 +00:00
}
},
2013-11-28 15:57:09 +00:00
/**
2013-10-02 14:05:55 +00:00
* Parent Sprite Bounds check for the sprite drag.
* @method Phaser.InputHandler#checkBoundsSprite
*/
checkBoundsSprite: function () {
2014-02-05 06:29:17 +00:00
if (this.sprite.fixedToCamera & & this.boundsSprite.fixedToCamera)
2013-10-02 14:05:55 +00:00
{
2014-02-05 06:29:17 +00:00
if (this.sprite.cameraOffset.x < this.boundsSprite.camerOffset.x)
{
this.sprite.cameraOffset.x = this.boundsSprite.camerOffset.x;
}
else if ((this.sprite.cameraOffset.x + this.sprite.width) > (this.boundsSprite.camerOffset.x + this.boundsSprite.width))
{
this.sprite.cameraOffset.x = (this.boundsSprite.camerOffset.x + this.boundsSprite.width) - this.sprite.width;
}
2013-10-02 14:05:55 +00:00
2014-02-05 06:29:17 +00:00
if (this.sprite.cameraOffset.y < this.boundsSprite.camerOffset.y)
{
this.sprite.cameraOffset.y = this.boundsSprite.camerOffset.y;
}
else if ((this.sprite.cameraOffset.y + this.sprite.height) > (this.boundsSprite.camerOffset.y + this.boundsSprite.height))
{
this.sprite.cameraOffset.y = (this.boundsSprite.camerOffset.y + this.boundsSprite.height) - this.sprite.height;
}
2013-10-02 14:05:55 +00:00
}
2014-02-05 06:29:17 +00:00
else
2013-10-02 14:05:55 +00:00
{
2014-02-05 06:29:17 +00:00
if (this.sprite.x < this.boundsSprite.x)
{
this.sprite.x = this.boundsSprite.x;
}
else if ((this.sprite.x + this.sprite.width) > (this.boundsSprite.x + this.boundsSprite.width))
{
this.sprite.x = (this.boundsSprite.x + this.boundsSprite.width) - this.sprite.width;
}
if (this.sprite.y < this.boundsSprite.y)
{
this.sprite.y = this.boundsSprite.y;
}
else if ((this.sprite.y + this.sprite.height) > (this.boundsSprite.y + this.boundsSprite.height))
{
this.sprite.y = (this.boundsSprite.y + this.boundsSprite.height) - this.sprite.height;
}
2013-10-02 14:05:55 +00:00
}
}
2014-02-05 06:29:17 +00:00
};
Phaser.InputHandler.prototype.constructor = Phaser.InputHandler;
< / pre >
2013-10-02 14:05:55 +00:00
< / article >
< / section >
< / div >
< div class = "clearfix" > < / div >
< footer >
< span class = "copyright" >
2014-02-05 06:29:17 +00:00
Phaser Copyright © 2012-2014 Photon Storm Ltd.
2013-10-02 14:05:55 +00:00
< / span >
< br / >
< span class = "jsdoc-message" >
2013-10-25 15:54:40 +00:00
Documentation generated by < a href = "https://github.com/jsdoc3/jsdoc" > JSDoc 3.3.0-dev< / a >
2014-03-28 01:42:49 +00:00
on Fri Mar 28 2014 01:41:22 GMT-0000 (GMT) using the < a href = "https://github.com/terryweiss/docstrap" > DocStrap template< / a > .
2013-10-02 14:05:55 +00:00
< / span >
< / footer >
< / div >
< br clear = "both" >
< / div >
< / div >
< script src = "scripts/sunlight.js" > < / script >
< script src = "scripts/sunlight.javascript.js" > < / script >
< script src = "scripts/sunlight-plugin.doclinks.js" > < / script >
< script src = "scripts/sunlight-plugin.linenumbers.js" > < / script >
< script src = "scripts/sunlight-plugin.menu.js" > < / script >
< script src = "scripts/jquery.min.js" > < / script >
< script src = "scripts/jquery.scrollTo.js" > < / script >
< script src = "scripts/jquery.localScroll.js" > < / script >
< script src = "scripts/bootstrap-dropdown.js" > < / script >
< script src = "scripts/toc.js" > < / script >
< script > Sunlight . highlightAll ( { lineNumbers : true , showMenu : true , enableDoclinks : true } ) ; < / script >
< script >
$( function () {
$( "#toc" ).toc( {
2014-03-14 06:36:05 +00:00
anchorName : function(i, heading, prefix) {
return $(heading).attr("id") || ( prefix + i );
},
2013-10-02 14:05:55 +00:00
selectors : "h1,h2,h3,h4",
showAndHide : false,
scrollTo : 60
} );
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
} );
< / script >
< / body >
< / html >