From 84a2b980a586e1db619c43415caad4545cdb1d23 Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Wed, 25 Aug 2021 13:18:45 -0400 Subject: [PATCH] multi touch / swipe (very buggy) --- source/Controls.hx | 91 ++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 31 deletions(-) diff --git a/source/Controls.hx b/source/Controls.hx index f47acbaa9..2e6ff9d99 100644 --- a/source/Controls.hx +++ b/source/Controls.hx @@ -852,6 +852,14 @@ typedef SaveInputLists = ?pad:Array }; +typedef Swipes = +{ + ?initTouchPos:FlxPoint, + ?touchAngle:Float, + ?touchLength:Float, + ?curTouchPos:FlxPoint +}; + class FlxActionInputDigitalMobileSwipeGameplay extends FlxActionInputDigital { public function new(swipeDir:Int = FlxObject.ANY, Trigger:FlxInputState) @@ -859,9 +867,10 @@ class FlxActionInputDigitalMobileSwipeGameplay extends FlxActionInputDigital super(MOBILE, swipeDir, Trigger); } - // multiple swipe support - // make datatype that has all 3 needed info and then make an array of them? - // + // fix right swipe + // fix down swipe on left side of screen? + var touchMap:Map = new Map(); + var initTouchPos:FlxPoint = new FlxPoint(); var touchAngle:Float = 0; @@ -882,30 +891,47 @@ class FlxActionInputDigitalMobileSwipeGameplay extends FlxActionInputDigital { if (touch.justPressed) { - initTouchPos.set(touch.screenX, touch.screenY); + var pos:FlxPoint = new FlxPoint(touch.screenX, touch.screenY); + var pos2:FlxPoint = new FlxPoint(touch.screenX, touch.screenY); + + var swp:Swipes = { + initTouchPos: pos, + curTouchPos: pos2, + touchAngle: 0, + touchLength: 0 + }; + touchMap[touch.touchPointID] = swp; + curStep = 1; Haptic.vibrate(40, 70); } if (touch.pressed) { - curTouchPos.set(touch.screenX, touch.screenY); + var daSwipe = touchMap[touch.touchPointID]; - var dx = initTouchPos.x - touch.screenX; - var dy = initTouchPos.y - touch.screenY; + daSwipe.curTouchPos.set(touch.screenX, touch.screenY); - touchAngle = Math.atan2(dy, dx); - touchLength = Math.sqrt(dx * dx + dy * dy); + var dx = daSwipe.initTouchPos.x - touch.screenX; + var dy = daSwipe.initTouchPos.y - touch.screenY; - FlxG.watch.addQuick("LENGTH", touchLength); - FlxG.watch.addQuick("ANGLE", FlxAngle.asDegrees(touchLength)); + daSwipe.touchAngle = Math.atan2(dy, dx); + daSwipe.touchLength = Math.sqrt(dx * dx + dy * dy); - if (touchLength >= (activateLength / vibrationSteps) * curStep) + // FlxG.watch.addQuick("LENGTH", touchLength); + // FlxG.watch.addQuick("ANGLE", FlxAngle.asDegrees(touchLength)); + + if (daSwipe.touchLength >= (activateLength / vibrationSteps) * curStep) { curStep += 1; // Haptic.vibrate(Std.int(hapticPressure / (curStep * 1.5)), 50); } } + if (touch.justReleased) + { + touchMap.remove(touch.touchPointID); + } + /* switch (inputID) { case FlxObject.UP: @@ -919,36 +945,39 @@ class FlxActionInputDigitalMobileSwipeGameplay extends FlxActionInputDigital override public function check(Action:FlxAction):Bool { - var degAngle = FlxAngle.asDegrees(touchAngle); - - switch (trigger) + for (swp in touchMap) { - case JUST_PRESSED: - if (touchLength >= activateLength) // 90 is random ass value lol - { - switch (inputID) + var degAngle = FlxAngle.asDegrees(swp.touchAngle); + + switch (trigger) + { + case JUST_PRESSED: + if (swp.touchLength >= activateLength) // 90 is random ass value lol { - case FlxObject.UP: - if (degAngle >= 45 && degAngle <= 90 + 45) return properTouch(); - case FlxObject.DOWN: - if (-degAngle >= 45 && -degAngle <= 90 + 45) return properTouch(); - case FlxObject.LEFT: - if (degAngle <= 45 && -degAngle <= 45) return properTouch(); - case FlxObject.RIGHT: - if (degAngle >= 90 + 45 && -degAngle >= 90 + 45) return properTouch(); + switch (inputID) + { + case FlxObject.UP: + if (degAngle >= 45 && degAngle <= 90 + 45) return properTouch(swp); + case FlxObject.DOWN: + if (-degAngle >= 45 && -degAngle <= 90 + 45) return properTouch(swp); + case FlxObject.LEFT: + if (degAngle <= 45 && -degAngle <= 45) return properTouch(swp); + case FlxObject.RIGHT: + if (degAngle >= 90 + 45 && -degAngle >= 90 + 45) return properTouch(swp); + } } - } - default: + default: + } } return false; } - function properTouch():Bool + function properTouch(swipe:Swipes):Bool { curStep = 1; Haptic.vibrate(100, 30); - initTouchPos.set(curTouchPos.x, curTouchPos.y); + swipe.initTouchPos.set(curTouchPos.x, curTouchPos.y); return true; } }