1
1
package flixel .input .touch ;
2
2
3
3
#if FLX_TOUCH
4
+ import flixel .util .FlxDestroyUtil ;
4
5
import openfl .Lib ;
5
6
import openfl .events .TouchEvent ;
6
7
import openfl .ui .Multitouch ;
@@ -9,49 +10,43 @@ import openfl.ui.MultitouchInputMode;
9
10
/**
10
11
* @author Zaphod
11
12
*/
13
+ @:nullSafety (Strict )
12
14
class FlxTouchManager implements IFlxInputManager
13
15
{
14
16
/**
15
17
* The maximum number of concurrent touch points supported by the current device.
16
18
*/
17
- public static var maxTouchPoints : Int = 0 ;
19
+ public static var maxTouchPoints ( default , null ) : Int = 0 ;
18
20
19
21
/**
20
22
* All active touches including just created, moving and just released.
21
23
*/
22
- public var list : Array <FlxTouch >;
24
+ public final list : Array <FlxTouch > = [] ;
23
25
24
26
/**
25
27
* Storage for inactive touches (some sort of cache for them).
26
28
*/
27
- var _inactiveTouches : Array <FlxTouch >;
29
+ final _inactiveTouches : Array <FlxTouch > = [] ;
28
30
29
31
/**
30
32
* Helper storage for active touches (for faster access)
31
33
*/
32
- var _touchesCache : Map <Int , FlxTouch >;
34
+ final _touchesCache : Map <Int , FlxTouch > = [] ;
33
35
34
36
/**
35
37
* WARNING: can be null if no active touch with the provided ID could be found
36
38
*/
37
- public inline function getByID (TouchPointID : Int ): FlxTouch
39
+ public inline function getByID (TouchPointID : Int ): Null < FlxTouch >
38
40
{
39
41
return _touchesCache .get (TouchPointID );
40
42
}
41
43
42
44
/**
43
45
* Return the first touch if there is one, beware of null
44
46
*/
45
- public function getFirst (): FlxTouch
47
+ public function getFirst (): Null < FlxTouch >
46
48
{
47
- if (list [0 ] != null )
48
- {
49
- return list [0 ];
50
- }
51
- else
52
- {
53
- return null ;
54
- }
49
+ return list [0 ];
55
50
}
56
51
57
52
/**
@@ -60,19 +55,9 @@ class FlxTouchManager implements IFlxInputManager
60
55
@:noCompletion
61
56
public function destroy (): Void
62
57
{
63
- for (touch in list )
64
- {
65
- touch .destroy ();
66
- }
67
- list = null ;
68
-
69
- for (touch in _inactiveTouches )
70
- {
71
- touch .destroy ();
72
- }
73
- _inactiveTouches = null ;
74
-
75
- _touchesCache = null ;
58
+ _touchesCache .clear ();
59
+ FlxDestroyUtil .destroyArray (list );
60
+ FlxDestroyUtil .destroyArray (_inactiveTouches );
76
61
}
77
62
78
63
/**
@@ -88,11 +73,10 @@ class FlxTouchManager implements IFlxInputManager
88
73
TouchArray = new Array <FlxTouch >();
89
74
}
90
75
91
- var touchLen : Int = TouchArray .length ;
92
-
76
+ final touchLen : Int = TouchArray .length ;
93
77
if (touchLen > 0 )
94
78
{
95
- TouchArray .splice ( 0 , touchLen );
79
+ TouchArray .resize ( 0 );
96
80
}
97
81
98
82
for (touch in list )
@@ -119,10 +103,10 @@ class FlxTouchManager implements IFlxInputManager
119
103
TouchArray = new Array <FlxTouch >();
120
104
}
121
105
122
- var touchLen : Int = TouchArray .length ;
106
+ final touchLen : Int = TouchArray .length ;
123
107
if (touchLen > 0 )
124
108
{
125
- TouchArray .splice ( 0 , touchLen );
109
+ TouchArray .resize ( 0 );
126
110
}
127
111
128
112
for (touch in list )
@@ -141,26 +125,20 @@ class FlxTouchManager implements IFlxInputManager
141
125
*/
142
126
public function reset (): Void
143
127
{
144
- for (key in _touchesCache .keys ())
145
- {
146
- _touchesCache .remove (key );
147
- }
128
+ _touchesCache .clear ();
148
129
149
130
for (touch in list )
150
131
{
151
132
touch .input .reset ();
152
133
_inactiveTouches .push (touch );
153
134
}
154
135
155
- list .splice ( 0 , list . length );
136
+ list .resize ( 0 );
156
137
}
157
138
158
139
@:allow (flixel. FlxG )
159
140
function new ()
160
141
{
161
- list = new Array <FlxTouch >();
162
- _inactiveTouches = new Array <FlxTouch >();
163
- _touchesCache = new Map <Int , FlxTouch >();
164
142
maxTouchPoints = Multitouch .maxTouchPoints ;
165
143
Multitouch .inputMode = MultitouchInputMode .TOUCH_POINT ;
166
144
@@ -174,7 +152,7 @@ class FlxTouchManager implements IFlxInputManager
174
152
*/
175
153
function handleTouchBegin (FlashEvent : TouchEvent ): Void
176
154
{
177
- var touch : FlxTouch = _touchesCache .get (FlashEvent .touchPointID );
155
+ var touch : Null < FlxTouch > = _touchesCache .get (FlashEvent .touchPointID );
178
156
if (touch != null )
179
157
{
180
158
touch .setXY (Std .int (FlashEvent .stageX ), Std .int (FlashEvent .stageY ));
@@ -192,7 +170,7 @@ class FlxTouchManager implements IFlxInputManager
192
170
*/
193
171
function handleTouchEnd (FlashEvent : TouchEvent ): Void
194
172
{
195
- var touch : FlxTouch = _touchesCache .get (FlashEvent .touchPointID );
173
+ final touch : Null < FlxTouch > = _touchesCache .get (FlashEvent .touchPointID );
196
174
197
175
if (touch != null )
198
176
{
@@ -205,7 +183,7 @@ class FlxTouchManager implements IFlxInputManager
205
183
*/
206
184
function handleTouchMove (FlashEvent : TouchEvent ): Void
207
185
{
208
- var touch : FlxTouch = _touchesCache .get (FlashEvent .touchPointID );
186
+ final touch : Null < FlxTouch > = _touchesCache .get (FlashEvent .touchPointID );
209
187
210
188
if (touch != null )
211
189
{
@@ -239,7 +217,8 @@ class FlxTouchManager implements IFlxInputManager
239
217
{
240
218
if (_inactiveTouches .length > 0 )
241
219
{
242
- var touch : FlxTouch = _inactiveTouches .pop ();
220
+ @:nullSafety (Off )
221
+ final touch : FlxTouch = _inactiveTouches .pop ();
243
222
touch .recycle (X , Y , PointID , pressure );
244
223
return add (touch );
245
224
}
0 commit comments