Fix[control]: swipeable on multiple button/notch

BREAKING CHANGE: Freeform mode may not like this
This commit is contained in:
Mathias-Boulay 2023-03-02 02:16:23 +01:00
parent 9a403f2926
commit bf9dc168d5

View File

@ -11,6 +11,7 @@ import com.google.gson.*;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import net.kdt.pojavlaunch.*; import net.kdt.pojavlaunch.*;
@ -28,6 +29,9 @@ public class ControlLayout extends FrameLayout {
protected CustomControls mLayout; protected CustomControls mLayout;
/* Accessible when inside the game by ControlInterface implementations, cached for perf. */ /* Accessible when inside the game by ControlInterface implementations, cached for perf. */
private MinecraftGLSurface mGameSurface = null; private MinecraftGLSurface mGameSurface = null;
/* Cache to buttons for performance purposes */
private List<ControlInterface> mButtons;
private boolean mModifiable = false; private boolean mModifiable = false;
private CustomControlsActivity mActivity; private CustomControlsActivity mActivity;
private boolean mControlVisible = false; private boolean mControlVisible = false;
@ -90,6 +94,8 @@ public class ControlLayout extends FrameLayout {
mLayout.scaledAt = LauncherPreferences.PREF_BUTTONSIZE; mLayout.scaledAt = LauncherPreferences.PREF_BUTTONSIZE;
setModified(false); setModified(false);
mButtons = null;
getButtonChildren(); // Force refresh
} // loadLayout } // loadLayout
//CONTROL BUTTON //CONTROL BUTTON
@ -225,14 +231,17 @@ public class ControlLayout extends FrameLayout {
} }
public ArrayList<ControlInterface> getButtonChildren(){ public List<ControlInterface> getButtonChildren(){
ArrayList<ControlInterface> children = new ArrayList<>(); if(mModifiable || mButtons == null){
for(int i=0; i<getChildCount(); ++i){ mButtons = new ArrayList<>();
View v = getChildAt(i); for(int i=0; i<getChildCount(); ++i){
if(v instanceof ControlInterface) View v = getChildAt(i);
children.add(((ControlInterface) v)); if(v instanceof ControlInterface)
mButtons.add(((ControlInterface) v));
}
} }
return children;
return mButtons;
} }
public void refreshControlButtonPositions(){ public void refreshControlButtonPositions(){
@ -290,48 +299,42 @@ public class ControlLayout extends FrameLayout {
HashMap<View, ControlInterface> mapTable = new HashMap<>(); HashMap<View, ControlInterface> mapTable = new HashMap<>();
int[] location = new int[2];
//While this is called onTouch, this should only be called from a ControlButton. //While this is called onTouch, this should only be called from a ControlButton.
public boolean onTouch(View v, MotionEvent ev) { 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 //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(ev.getActionMasked() == MotionEvent.ACTION_UP || ev.getActionMasked() == MotionEvent.ACTION_CANCEL){
if(lastControlButton != null) lastControlButton.sendKeyPresses(false); for(ControlInterface control : mapTable.values()){
mapTable.put(v, null); control.sendKeyPresses(false);
}
mapTable.clear();
return true; return true;
} }
if(ev.getActionMasked() != MotionEvent.ACTION_MOVE) return false; if(ev.getActionMasked() != MotionEvent.ACTION_MOVE) return false;
//Optimization pass to avoid looking at all children again getLocationOnScreen(location);
if(lastControlButton != null){ // Update the state of all swipeable buttons
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
for(ControlInterface button : getButtonChildren()){ for(ControlInterface button : getButtonChildren()){
if(!button.getProperties().isSwipeable) continue; if(!button.getProperties().isSwipeable) continue;
if( ev.getRawX() > button.getControlView().getX() && ev.getRawX() < button.getControlView().getX() + button.getControlView().getWidth() && if( ev.getRawX() > button.getControlView().getX() + location[0]
ev.getRawY() > button.getControlView().getY() && ev.getRawY() < button.getControlView().getY() + button.getControlView().getHeight()){ && 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(mapTable.get(button.getControlView()) == null){
if(!button.equals(lastControlButton)){
button.sendKeyPresses(true); button.sendKeyPresses(true);
mapTable.put(button.getControlView(), button);
mapTable.put(v, button); }
}else{
if(mapTable.get(button.getControlView()) != null){
button.sendKeyPresses(false);
mapTable.remove(button.getControlView());
} }
return true;
} }
} }
return false; return true;
} }
@Override @Override