Fixed common issues with the Pointer Plugin

This commit is contained in:
KarimAkra
2025-04-16 16:05:20 +03:00
parent 6f9b8f0284
commit a289fb7f54
4 changed files with 69 additions and 21 deletions
+1 -1
View File
@@ -178,7 +178,7 @@ class InitState extends FlxState
funkin.util.plugins.ReloadAssetsDebugPlugin.initialize();
funkin.util.plugins.VolumePlugin.initialize();
funkin.util.plugins.WatchPlugin.initialize();
#if ios
#if mobile
funkin.util.plugins.TouchPointerPlugin.initialize();
#end
+2
View File
@@ -683,6 +683,7 @@ class PlayState extends MusicBeatSubState
#if mobile
// Force allowScreenTimeout to be disabled
lime.system.System.allowScreenTimeout = false;
funkin.util.plugins.TouchPointerPlugin.enabled = false;
#end
// Reduce physics accuracy (who cares!!!) to improve animation quality.
@@ -1643,6 +1644,7 @@ class PlayState extends MusicBeatSubState
#if mobile
// Syncing allowScreenTimeout with Preferences option.
lime.system.System.allowScreenTimeout = Preferences.screenTimeout;
funkin.util.plugins.TouchPointerPlugin.enabled = true;
#end
super.destroy();
+2 -2
View File
@@ -56,8 +56,8 @@ class MusicBeatState extends FlxTransitionableState implements IEventHandler
initCallbacks();
add(TouchPointerPlugin.pointerGrp);
FlxG.cameras.add(TouchPointerPlugin.pointerCamera, false);
// add(TouchPointerPlugin.pointerGrp);
// FlxG.cameras.add(TouchPointerPlugin.pointerCamera, false);
}
function initCallbacks()
@@ -18,23 +18,37 @@ import funkin.graphics.FunkinCamera;
* @author moondroidcoder
* Tracks your touch points in your game.
*/
class TouchPointerPlugin extends FlxBasic
class TouchPointerPlugin extends FlxTypedSpriteGroup<TouchPointer>
{
public static var pointerGrp:FlxTypedSpriteGroup<TouchPointer>;
public static var pointerCamera:FunkinCamera;
/**
* Whether the plugin is enabled.
*/
public static var enabled(default, set):Bool = true;
/**
* A singleton instance of the plugin.
*/
private static var instance:TouchPointerPlugin = null;
public function new()
{
super();
pointerGrp = new FlxTypedSpriteGroup<TouchPointer>();
pointerCamera = new FunkinCamera('pointerCamera');
pointerCamera.bgColor = 0x0;
pointerGrp.cameras = [pointerCamera];
}
public static function initialize()
{
FlxG.plugins.addPlugin(new TouchPointerPlugin());
final pointerPlugin = new TouchPointerPlugin();
function setCamera(camera:FlxCamera):Void
{
pointerPlugin.cameras = [FlxG.cameras.list[FlxG.cameras.list.length - 1]];
}
FlxG.cameras.cameraAdded.add(setCamera);
FlxG.cameras.cameraRemoved.add(setCamera);
FlxG.plugins.drawOnTop = true;
FlxG.plugins.addPlugin(pointerPlugin);
}
override public function update(elapsed:Float):Void
@@ -49,25 +63,25 @@ class TouchPointerPlugin extends FlxBasic
if (pointer == null)
{
pointer = pointerGrp.recycle(TouchPointer);
pointer = recycle(TouchPointer);
pointer.initialize(touch.touchPointID);
pointerGrp.add(pointer);
add(pointer);
}
pointer.updateFromTouch(touch);
}
for (pointer in pointerGrp.members)
for (pointer in members)
{
if (pointer == null || touchExists(pointer.touchId)) continue;
pointerGrp.remove(pointer, true);
remove(pointer, true);
}
}
private function findPointerByTouchId(touchId:Int):TouchPointer
{
for (pointer in pointerGrp.members)
for (pointer in members)
{
if (pointer == null || pointer.touchId != touchId) continue;
@@ -86,6 +100,27 @@ class TouchPointerPlugin extends FlxBasic
}
return false;
}
private static function set_enabled(value:Bool):Bool
{
if (value == enabled) return value;
enabled = value;
if (instance != null)
{
if (enabled)
{
instance.exists = instance.visible = instance.active = instance.alive = true;
}
else
{
instance.exists = instance.visible = instance.active = instance.alive = false;
}
}
return enabled;
}
}
class TouchPointer extends FlxSprite
@@ -99,7 +134,7 @@ class TouchPointer extends FlxSprite
super();
makeGraphic(16, 16, FlxColor.RED);
scrollFactor.set(0, 0);
lastPosition = new FlxPoint();
lastPosition = FlxPoint.get();
}
public function initialize(touchId:Int):Void
@@ -111,16 +146,27 @@ class TouchPointer extends FlxSprite
public function updateFromTouch(touch:FlxTouch):Void
{
// Update position
x = touch.viewX;
y = touch.viewY;
x = touch.viewX - width / 2;
y = touch.viewY - height / 2;
if (camera.target != null)
{
x -= camera.target.x;
y -= camera.target.y;
}
// Calculate angle if moving
if (lastPosition.x != touch.viewX || lastPosition.y != touch.viewY)
if (lastPosition.distanceTo(FlxPoint.weak(touch.viewX, touch.viewY)) > 3)
{
var angle = FlxAngle.angleBetweenPoints(lastPosition, new FlxPoint(touch.viewX, touch.viewY));
var angle = FlxAngle.angleBetweenPoint(this, lastPosition, true);
this.angle = angle;
loadGraphic("assets/images/cursor/kevin.png");
}
else
{
angle = 0;
loadGraphic("assets/images/cursor/michael.png");
}
lastPosition.set(touch.viewX, touch.viewY);
}