1+ /// <reference path="../../Game.ts" />
2+ /// <reference path="../../gameobjects/DynamicTexture.ts" />
3+ /// <reference path="../../utils/SpriteUtils.ts" />
4+ /// <reference path="../../utils/RectangleUtils.ts" />
5+
6+ /**
7+ * Phaser - Components - Input
8+ *
9+ * Input detection component
10+ */
11+
12+ module Phaser . Components {
13+
14+ export class Input {
15+
16+ constructor ( parent : Sprite ) {
17+
18+ this . game = parent . game ;
19+ this . _sprite = parent ;
20+
21+ this . enabled = false ;
22+
23+ this . checkBody = false ;
24+ this . useHandCursor = false ;
25+
26+ }
27+
28+ /**
29+ *
30+ */
31+ public game : Game ;
32+
33+ /**
34+ * Reference to the Image stored in the Game.Cache that is used as the texture for the Sprite.
35+ */
36+ private _sprite : Sprite ;
37+
38+ public enabled : bool ;
39+ public checkBody : bool ;
40+ public useHandCursor : bool ;
41+
42+ public oldX : number ;
43+ public oldY : number ;
44+
45+ public x : number = 0 ;
46+ public y : number = 0 ;
47+
48+ /**
49+ * If the Pointer is touching the touchscreen, or the mouse button is held down, isDown is set to true
50+ * @property isDown
51+ * @type {Boolean }
52+ **/
53+ public isDown : bool = false ;
54+
55+ /**
56+ * If the Pointer is not touching the touchscreen, or the mouse button is up, isUp is set to true
57+ * @property isUp
58+ * @type {Boolean }
59+ **/
60+ public isUp : bool = true ;
61+
62+ /**
63+ * A timestamp representing when the Pointer first touched the touchscreen.
64+ * @property timeDown
65+ * @type {Number }
66+ **/
67+ public timeOver : number = 0 ;
68+
69+ /**
70+ * A timestamp representing when the Pointer left the touchscreen.
71+ * @property timeUp
72+ * @type {Number }
73+ **/
74+ public timeOut : number = 0 ;
75+
76+ /**
77+ * Is the Pointer over this Sprite
78+ * @property isOver
79+ * @type {Boolean }
80+ **/
81+ public isOver : bool = false ;
82+
83+ /**
84+ * Is the Pointer outside of this Sprite
85+ * @property isOut
86+ * @type {Boolean }
87+ **/
88+ public isOut : bool = true ;
89+
90+ /**
91+ * Update
92+ */
93+ public update ( ) {
94+
95+ if ( this . enabled == false )
96+ {
97+ return ;
98+ }
99+
100+ if ( this . game . input . x != this . oldX || this . game . input . y != this . oldY )
101+ {
102+ this . oldX = this . game . input . x ;
103+ this . oldY = this . game . input . y ;
104+
105+ if ( RectangleUtils . contains ( this . _sprite . frameBounds , this . game . input . x , this . game . input . y ) )
106+ {
107+ this . x = this . game . input . x - this . _sprite . x ;
108+ this . y = this . game . input . y - this . _sprite . y ;
109+
110+ if ( this . isOver == false )
111+ {
112+ this . isOver = true ;
113+ this . isOut = false ;
114+ this . timeOver = this . game . time . now ;
115+
116+ if ( this . useHandCursor )
117+ {
118+ this . _sprite . game . stage . canvas . style . cursor = "pointer" ;
119+ }
120+
121+ this . _sprite . events . onInputOver . dispatch ( this . _sprite , this . x , this . y , this . timeOver ) ;
122+ }
123+ }
124+ else
125+ {
126+ if ( this . isOver )
127+ {
128+ this . isOver = false ;
129+ this . isOut = true ;
130+ this . timeOut = this . game . time . now ;
131+
132+ if ( this . useHandCursor )
133+ {
134+ this . _sprite . game . stage . canvas . style . cursor = "default" ;
135+ }
136+
137+ this . _sprite . events . onInputOut . dispatch ( this . _sprite , this . timeOut ) ;
138+ }
139+ }
140+
141+ }
142+
143+ }
144+
145+ public justOver ( delay ?:number = 500 ) : bool {
146+ return ( this . isOver && this . duration < delay ) ;
147+ }
148+
149+ public justOut ( delay ?:number = 500 ) : bool {
150+ return ( this . isOut && ( this . game . time . now - this . timeOut < delay ) ) ;
151+ }
152+
153+ public get duration ( ) : number {
154+
155+ if ( this . isOver )
156+ {
157+ return this . game . time . now - this . timeOver ;
158+ }
159+
160+ return - 1 ;
161+
162+ }
163+
164+ /**
165+ * Render debug infos. (including name, bounds info, position and some other properties)
166+ * @param x {number} X position of the debug info to be rendered.
167+ * @param y {number} Y position of the debug info to be rendered.
168+ * @param [color] {number} color of the debug info to be rendered. (format is css color string)
169+ */
170+ public renderDebugInfo ( x : number , y : number , color ?: string = 'rgb(255,255,255)' ) {
171+
172+ this . _sprite . texture . context . font = '14px Courier' ;
173+ this . _sprite . texture . context . fillStyle = color ;
174+ this . _sprite . texture . context . fillText ( 'Sprite Input: (' + this . _sprite . frameBounds . width + ' x ' + this . _sprite . frameBounds . height + ')' , x , y ) ;
175+ this . _sprite . texture . context . fillText ( 'x: ' + this . x . toFixed ( 1 ) + ' y: ' + this . y . toFixed ( 1 ) , x , y + 14 ) ;
176+ this . _sprite . texture . context . fillText ( 'over: ' + this . isOver + ' duration: ' + this . duration . toFixed ( 0 ) , x , y + 28 ) ;
177+ this . _sprite . texture . context . fillText ( 'just over: ' + this . justOver ( ) + ' just out: ' + this . justOut ( ) , x , y + 42 ) ;
178+
179+ }
180+
181+ }
182+
183+ }
0 commit comments