From a289fb7f54cb627451ec0d6e1bfcd6b485533ada Mon Sep 17 00:00:00 2001 From: KarimAkra Date: Tue, 24 Dec 2024 15:22:24 +0200 Subject: [PATCH] Fixed common issues with the Pointer Plugin --- source/funkin/InitState.hx | 2 +- source/funkin/play/PlayState.hx | 2 + source/funkin/ui/MusicBeatState.hx | 4 +- .../funkin/util/plugins/TouchPointerPlugin.hx | 82 +++++++++++++++---- 4 files changed, 69 insertions(+), 21 deletions(-) diff --git a/source/funkin/InitState.hx b/source/funkin/InitState.hx index eef35633b..48d75670f 100644 --- a/source/funkin/InitState.hx +++ b/source/funkin/InitState.hx @@ -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 diff --git a/source/funkin/play/PlayState.hx b/source/funkin/play/PlayState.hx index d36e510b1..b877c4716 100644 --- a/source/funkin/play/PlayState.hx +++ b/source/funkin/play/PlayState.hx @@ -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(); diff --git a/source/funkin/ui/MusicBeatState.hx b/source/funkin/ui/MusicBeatState.hx index 50f820a28..d7047b961 100644 --- a/source/funkin/ui/MusicBeatState.hx +++ b/source/funkin/ui/MusicBeatState.hx @@ -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() diff --git a/source/funkin/util/plugins/TouchPointerPlugin.hx b/source/funkin/util/plugins/TouchPointerPlugin.hx index 02e728df7..673b513a1 100644 --- a/source/funkin/util/plugins/TouchPointerPlugin.hx +++ b/source/funkin/util/plugins/TouchPointerPlugin.hx @@ -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 { - public static var pointerGrp:FlxTypedSpriteGroup; - 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(); - 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); }