Skip to content

Commit fceeeb4

Browse files
committed
chore: add null-safety to FlxTouchManager
1 parent 2046936 commit fceeeb4

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

flixel/input/touch/FlxTouchManager.hx

Lines changed: 14 additions & 32 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,6 +10,7 @@ import openfl.ui.MultitouchInputMode;
910
/**
1011
* @author Zaphod
1112
*/
13+
@:nullSafety(Strict)
1214
class FlxTouchManager implements IFlxInputManager
1315
{
1416
/**
@@ -34,24 +36,17 @@ class FlxTouchManager implements IFlxInputManager
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,8 @@ 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+
FlxDestroyUtil.destroyArray(list);
59+
FlxDestroyUtil.destroyArray(_inactiveTouches);
7660
}
7761

7862
/**
@@ -88,7 +72,7 @@ class FlxTouchManager implements IFlxInputManager
8872
TouchArray = new Array<FlxTouch>();
8973
}
9074

91-
var touchLen:Int = TouchArray.length;
75+
final touchLen:Int = TouchArray.length;
9276

9377
if (touchLen > 0)
9478
{
@@ -119,7 +103,7 @@ 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
{
125109
TouchArray.splice(0, touchLen);
@@ -141,10 +125,7 @@ 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
{
@@ -174,7 +155,7 @@ class FlxTouchManager implements IFlxInputManager
174155
*/
175156
function handleTouchBegin(FlashEvent:TouchEvent):Void
176157
{
177-
var touch:FlxTouch = _touchesCache.get(FlashEvent.touchPointID);
158+
var touch:Null<FlxTouch> = _touchesCache.get(FlashEvent.touchPointID);
178159
if (touch != null)
179160
{
180161
touch.setXY(Std.int(FlashEvent.stageX), Std.int(FlashEvent.stageY));
@@ -192,7 +173,7 @@ class FlxTouchManager implements IFlxInputManager
192173
*/
193174
function handleTouchEnd(FlashEvent:TouchEvent):Void
194175
{
195-
var touch:FlxTouch = _touchesCache.get(FlashEvent.touchPointID);
176+
var touch:Null<FlxTouch> = _touchesCache.get(FlashEvent.touchPointID);
196177

197178
if (touch != null)
198179
{
@@ -205,7 +186,7 @@ class FlxTouchManager implements IFlxInputManager
205186
*/
206187
function handleTouchMove(FlashEvent:TouchEvent):Void
207188
{
208-
var touch:FlxTouch = _touchesCache.get(FlashEvent.touchPointID);
189+
var touch:Null<FlxTouch> = _touchesCache.get(FlashEvent.touchPointID);
209190

210191
if (touch != null)
211192
{
@@ -239,6 +220,7 @@ class FlxTouchManager implements IFlxInputManager
239220
{
240221
if (_inactiveTouches.length > 0)
241222
{
223+
@:nullSafety(Off)
242224
var touch:FlxTouch = _inactiveTouches.pop();
243225
touch.recycle(X, Y, PointID, pressure);
244226
return add(touch);

0 commit comments

Comments
 (0)