From bf9dc168d54adcf55fe9e98c69f654bea110cdcc Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Thu, 2 Mar 2023 02:16:23 +0100 Subject: [PATCH] Fix[control]: swipeable on multiple button/notch BREAKING CHANGE: Freeform mode may not like this --- .../customcontrols/ControlLayout.java | 67 ++++++++++--------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java index 3c6c0b314..303d31655 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/ControlLayout.java @@ -11,6 +11,7 @@ import com.google.gson.*; import java.io.*; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import net.kdt.pojavlaunch.*; @@ -28,6 +29,9 @@ public class ControlLayout extends FrameLayout { protected CustomControls mLayout; /* Accessible when inside the game by ControlInterface implementations, cached for perf. */ private MinecraftGLSurface mGameSurface = null; + + /* Cache to buttons for performance purposes */ + private List mButtons; private boolean mModifiable = false; private CustomControlsActivity mActivity; private boolean mControlVisible = false; @@ -90,6 +94,8 @@ public class ControlLayout extends FrameLayout { mLayout.scaledAt = LauncherPreferences.PREF_BUTTONSIZE; setModified(false); + mButtons = null; + getButtonChildren(); // Force refresh } // loadLayout //CONTROL BUTTON @@ -225,14 +231,17 @@ public class ControlLayout extends FrameLayout { } - public ArrayList getButtonChildren(){ - ArrayList children = new ArrayList<>(); - for(int i=0; i getButtonChildren(){ + if(mModifiable || mButtons == null){ + mButtons = new ArrayList<>(); + for(int i=0; i mapTable = new HashMap<>(); + int[] location = new int[2]; //While this is called onTouch, this should only be called from a ControlButton. public boolean onTouch(View v, MotionEvent ev) { - ControlInterface lastControlButton = mapTable.get(v); - //Check if the action is cancelling, reset the lastControl button associated to the view if(ev.getActionMasked() == MotionEvent.ACTION_UP || ev.getActionMasked() == MotionEvent.ACTION_CANCEL){ - if(lastControlButton != null) lastControlButton.sendKeyPresses(false); - mapTable.put(v, null); + for(ControlInterface control : mapTable.values()){ + control.sendKeyPresses(false); + } + mapTable.clear(); return true; } if(ev.getActionMasked() != MotionEvent.ACTION_MOVE) return false; - //Optimization pass to avoid looking at all children again - if(lastControlButton != null){ - if( ev.getRawX() > lastControlButton.getControlView().getX() && ev.getRawX() < lastControlButton.getControlView().getX() + lastControlButton.getControlView().getWidth() && - ev.getRawY() > lastControlButton.getControlView().getY() && ev.getRawY() < lastControlButton.getControlView().getY() + lastControlButton.getControlView().getHeight()){ - return true; - } - } - - //Release last keys - if (lastControlButton != null) lastControlButton.sendKeyPresses(false); - mapTable.put(v, null); - - //Look for another SWIPEABLE button + getLocationOnScreen(location); + // Update the state of all swipeable buttons for(ControlInterface button : getButtonChildren()){ if(!button.getProperties().isSwipeable) continue; - if( ev.getRawX() > button.getControlView().getX() && ev.getRawX() < button.getControlView().getX() + button.getControlView().getWidth() && - ev.getRawY() > button.getControlView().getY() && ev.getRawY() < button.getControlView().getY() + button.getControlView().getHeight()){ + if( ev.getRawX() > button.getControlView().getX() + location[0] + && ev.getRawX() - getGameSurface().getX() < button.getControlView().getX() + button.getControlView().getWidth() + location[0] + && ev.getRawY() > button.getControlView().getY() + && ev.getRawY() < button.getControlView().getY() + button.getControlView().getHeight()){ - //Press the new key - if(!button.equals(lastControlButton)){ + if(mapTable.get(button.getControlView()) == null){ button.sendKeyPresses(true); - - mapTable.put(v, button); + mapTable.put(button.getControlView(), button); + } + }else{ + if(mapTable.get(button.getControlView()) != null){ + button.sendKeyPresses(false); + mapTable.remove(button.getControlView()); } - return true; } } - return false; + return true; } @Override