Skip to content

add null-safety to FlxTouchManager #3440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 23 additions & 44 deletions flixel/input/touch/FlxTouchManager.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flixel.input.touch;

#if FLX_TOUCH
import flixel.util.FlxDestroyUtil;
import openfl.Lib;
import openfl.events.TouchEvent;
import openfl.ui.Multitouch;
Expand All @@ -9,49 +10,43 @@ import openfl.ui.MultitouchInputMode;
/**
* @author Zaphod
*/
@:nullSafety(Strict)
class FlxTouchManager implements IFlxInputManager
{
/**
* The maximum number of concurrent touch points supported by the current device.
*/
public static var maxTouchPoints:Int = 0;
public static var maxTouchPoints(default, null):Int = 0;

/**
* All active touches including just created, moving and just released.
*/
public var list:Array<FlxTouch>;
public final list:Array<FlxTouch> = [];

/**
* Storage for inactive touches (some sort of cache for them).
*/
var _inactiveTouches:Array<FlxTouch>;
final _inactiveTouches:Array<FlxTouch> = [];

/**
* Helper storage for active touches (for faster access)
*/
var _touchesCache:Map<Int, FlxTouch>;
final _touchesCache:Map<Int, FlxTouch> = [];

/**
* WARNING: can be null if no active touch with the provided ID could be found
*/
public inline function getByID(TouchPointID:Int):FlxTouch
public inline function getByID(TouchPointID:Int):Null<FlxTouch>
{
return _touchesCache.get(TouchPointID);
}

/**
* Return the first touch if there is one, beware of null
*/
public function getFirst():FlxTouch
public function getFirst():Null<FlxTouch>
{
if (list[0] != null)
{
return list[0];
}
else
{
return null;
}
return list[0];
}

/**
Expand All @@ -60,19 +55,9 @@ class FlxTouchManager implements IFlxInputManager
@:noCompletion
public function destroy():Void
{
for (touch in list)
{
touch.destroy();
}
list = null;

for (touch in _inactiveTouches)
{
touch.destroy();
}
_inactiveTouches = null;

_touchesCache = null;
_touchesCache.clear();
FlxDestroyUtil.destroyArray(list);
FlxDestroyUtil.destroyArray(_inactiveTouches);
}

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

var touchLen:Int = TouchArray.length;

final touchLen:Int = TouchArray.length;
if (touchLen > 0)
{
TouchArray.splice(0, touchLen);
TouchArray.resize(0);
}

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

var touchLen:Int = TouchArray.length;
final touchLen:Int = TouchArray.length;
if (touchLen > 0)
{
TouchArray.splice(0, touchLen);
TouchArray.resize(0);
}

for (touch in list)
Expand All @@ -141,26 +125,20 @@ class FlxTouchManager implements IFlxInputManager
*/
public function reset():Void
{
for (key in _touchesCache.keys())
{
_touchesCache.remove(key);
}
_touchesCache.clear();

for (touch in list)
{
touch.input.reset();
_inactiveTouches.push(touch);
}

list.splice(0, list.length);
list.resize(0);
}

@:allow(flixel.FlxG)
function new()
{
list = new Array<FlxTouch>();
_inactiveTouches = new Array<FlxTouch>();
_touchesCache = new Map<Int, FlxTouch>();
maxTouchPoints = Multitouch.maxTouchPoints;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

Expand All @@ -174,7 +152,7 @@ class FlxTouchManager implements IFlxInputManager
*/
function handleTouchBegin(FlashEvent:TouchEvent):Void
{
var touch:FlxTouch = _touchesCache.get(FlashEvent.touchPointID);
var touch:Null<FlxTouch> = _touchesCache.get(FlashEvent.touchPointID);
if (touch != null)
{
touch.setXY(Std.int(FlashEvent.stageX), Std.int(FlashEvent.stageY));
Expand All @@ -192,7 +170,7 @@ class FlxTouchManager implements IFlxInputManager
*/
function handleTouchEnd(FlashEvent:TouchEvent):Void
{
var touch:FlxTouch = _touchesCache.get(FlashEvent.touchPointID);
final touch:Null<FlxTouch> = _touchesCache.get(FlashEvent.touchPointID);

if (touch != null)
{
Expand All @@ -205,7 +183,7 @@ class FlxTouchManager implements IFlxInputManager
*/
function handleTouchMove(FlashEvent:TouchEvent):Void
{
var touch:FlxTouch = _touchesCache.get(FlashEvent.touchPointID);
final touch:Null<FlxTouch> = _touchesCache.get(FlashEvent.touchPointID);

if (touch != null)
{
Expand Down Expand Up @@ -239,7 +217,8 @@ class FlxTouchManager implements IFlxInputManager
{
if (_inactiveTouches.length > 0)
{
var touch:FlxTouch = _inactiveTouches.pop();
@:nullSafety(Off)
final touch:FlxTouch = _inactiveTouches.pop();
touch.recycle(X, Y, PointID, pressure);
return add(touch);
}
Expand Down