Skip to content

Commit 2e8a38b

Browse files
authored
chore: add null-safety to FlxTouchManager (#3440)
1 parent 2046936 commit 2e8a38b

File tree

1 file changed

+23
-44
lines changed

1 file changed

+23
-44
lines changed

flixel/input/touch/FlxTouchManager.hx

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package flixel.input.touch;
22

33
#if FLX_TOUCH
4+
import flixel.util.FlxDestroyUtil;
45
import openfl.Lib;
56
import openfl.events.TouchEvent;
67
import openfl.ui.Multitouch;
@@ -9,49 +10,43 @@ import openfl.ui.MultitouchInputMode;
910
/**
1011
* @author Zaphod
1112
*/
13+
@:nullSafety(Strict)
1214
class FlxTouchManager implements IFlxInputManager
1315
{
1416
/**
1517
* The maximum number of concurrent touch points supported by the current device.
1618
*/
17-
public static var maxTouchPoints:Int = 0;
19+
public static var maxTouchPoints(default, null):Int = 0;
1820

1921
/**
2022
* All active touches including just created, moving and just released.
2123
*/
22-
public var list:Array<FlxTouch>;
24+
public final list:Array<FlxTouch> = [];
2325

2426
/**
2527
* Storage for inactive touches (some sort of cache for them).
2628
*/
27-
var _inactiveTouches:Array<FlxTouch>;
29+
final _inactiveTouches:Array<FlxTouch> = [];
2830

2931
/**
3032
* Helper storage for active touches (for faster access)
3133
*/
32-
var _touchesCache:Map<Int, FlxTouch>;
34+
final _touchesCache:Map<Int, FlxTouch> = [];
3335

3436
/**
3537
* WARNING: can be null if no active touch with the provided ID could be found
3638
*/
37-
public inline function getByID(TouchPointID:Int):FlxTouch
39+
public inline function getByID(TouchPointID:Int):Null<FlxTouch>
3840
{
3941
return _touchesCache.get(TouchPointID);
4042
}
4143

4244
/**
4345
* Return the first touch if there is one, beware of null
4446
*/
45-
public function getFirst():FlxTouch
47+
public function getFirst():Null<FlxTouch>
4648
{
47-
if (list[0] != null)
48-
{
49-
return list[0];
50-
}
51-
else
52-
{
53-
return null;
54-
}
49+
return list[0];
5550
}
5651

5752
/**
@@ -60,19 +55,9 @@ class FlxTouchManager implements IFlxInputManager
6055
@:noCompletion
6156
public function destroy():Void
6257
{
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);
7661
}
7762

7863
/**
@@ -88,11 +73,10 @@ class FlxTouchManager implements IFlxInputManager
8873
TouchArray = new Array<FlxTouch>();
8974
}
9075

91-
var touchLen:Int = TouchArray.length;
92-
76+
final touchLen:Int = TouchArray.length;
9377
if (touchLen > 0)
9478
{
95-
TouchArray.splice(0, touchLen);
79+
TouchArray.resize(0);
9680
}
9781

9882
for (touch in list)
@@ -119,10 +103,10 @@ class FlxTouchManager implements IFlxInputManager
119103
TouchArray = new Array<FlxTouch>();
120104
}
121105

122-
var touchLen:Int = TouchArray.length;
106+
final touchLen:Int = TouchArray.length;
123107
if (touchLen > 0)
124108
{
125-
TouchArray.splice(0, touchLen);
109+
TouchArray.resize(0);
126110
}
127111

128112
for (touch in list)
@@ -141,26 +125,20 @@ class FlxTouchManager implements IFlxInputManager
141125
*/
142126
public function reset():Void
143127
{
144-
for (key in _touchesCache.keys())
145-
{
146-
_touchesCache.remove(key);
147-
}
128+
_touchesCache.clear();
148129

149130
for (touch in list)
150131
{
151132
touch.input.reset();
152133
_inactiveTouches.push(touch);
153134
}
154135

155-
list.splice(0, list.length);
136+
list.resize(0);
156137
}
157138

158139
@:allow(flixel.FlxG)
159140
function new()
160141
{
161-
list = new Array<FlxTouch>();
162-
_inactiveTouches = new Array<FlxTouch>();
163-
_touchesCache = new Map<Int, FlxTouch>();
164142
maxTouchPoints = Multitouch.maxTouchPoints;
165143
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
166144

@@ -174,7 +152,7 @@ class FlxTouchManager implements IFlxInputManager
174152
*/
175153
function handleTouchBegin(FlashEvent:TouchEvent):Void
176154
{
177-
var touch:FlxTouch = _touchesCache.get(FlashEvent.touchPointID);
155+
var touch:Null<FlxTouch> = _touchesCache.get(FlashEvent.touchPointID);
178156
if (touch != null)
179157
{
180158
touch.setXY(Std.int(FlashEvent.stageX), Std.int(FlashEvent.stageY));
@@ -192,7 +170,7 @@ class FlxTouchManager implements IFlxInputManager
192170
*/
193171
function handleTouchEnd(FlashEvent:TouchEvent):Void
194172
{
195-
var touch:FlxTouch = _touchesCache.get(FlashEvent.touchPointID);
173+
final touch:Null<FlxTouch> = _touchesCache.get(FlashEvent.touchPointID);
196174

197175
if (touch != null)
198176
{
@@ -205,7 +183,7 @@ class FlxTouchManager implements IFlxInputManager
205183
*/
206184
function handleTouchMove(FlashEvent:TouchEvent):Void
207185
{
208-
var touch:FlxTouch = _touchesCache.get(FlashEvent.touchPointID);
186+
final touch:Null<FlxTouch> = _touchesCache.get(FlashEvent.touchPointID);
209187

210188
if (touch != null)
211189
{
@@ -239,7 +217,8 @@ class FlxTouchManager implements IFlxInputManager
239217
{
240218
if (_inactiveTouches.length > 0)
241219
{
242-
var touch:FlxTouch = _inactiveTouches.pop();
220+
@:nullSafety(Off)
221+
final touch:FlxTouch = _inactiveTouches.pop();
243222
touch.recycle(X, Y, PointID, pressure);
244223
return add(touch);
245224
}

0 commit comments

Comments
 (0)