Add touch gliding (#1723)

* Add touch gliding

* Add attributes for glidability

* Account for touch glide attributes

* Ditching glidabitliy arrays

* Change var name to be consistent with feature name
This commit is contained in:
Certainty Witch 2021-03-06 07:46:58 +02:00 committed by GitHub
parent 0db164bfb2
commit a6e497cf62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 17 deletions

View File

@ -28,6 +28,7 @@ public final class TouchscreenControllerButtonView extends View {
private Hotkey mHotkey = Hotkey.NONE;
private String mConfigName;
private boolean mDefaultVisibility = true;
private boolean mIsGlidable = true;
public TouchscreenControllerButtonView(Context context) {
super(context);
@ -115,6 +116,9 @@ public final class TouchscreenControllerButtonView extends View {
mConfigName = name;
}
public boolean getIsGlidable() { return mIsGlidable; }
public void setIsGlidable(boolean isGlidable) { mIsGlidable = isGlidable; }
public boolean getDefaultVisibility() { return mDefaultVisibility; }
public void setDefaultVisibility(boolean visibility) { mDefaultVisibility = visibility; }

View File

@ -20,6 +20,8 @@ import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.preference.PreferenceManager;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
/**
* TODO: document your custom view class.
@ -42,6 +44,7 @@ public class TouchscreenControllerView extends FrameLayout {
private float mMovingLastY = 0.0f;
private ConstraintLayout mEditLayout = null;
private int mOpacity = 100;
private Map<Integer, View> mGlidePairs = new HashMap<>();
public TouchscreenControllerView(Context context) {
super(context);
@ -229,20 +232,20 @@ public class TouchscreenControllerView extends FrameLayout {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
linkButton(mMainView, R.id.controller_button_up, "UpButton", "Up", true);
linkButton(mMainView, R.id.controller_button_right, "RightButton", "Right", true);
linkButton(mMainView, R.id.controller_button_down, "DownButton", "Down", true);
linkButton(mMainView, R.id.controller_button_left, "LeftButton", "Left", true);
linkButton(mMainView, R.id.controller_button_l1, "L1Button", "L1", true);
linkButton(mMainView, R.id.controller_button_l2, "L2Button", "L2", true);
linkButton(mMainView, R.id.controller_button_select, "SelectButton", "Select", true);
linkButton(mMainView, R.id.controller_button_start, "StartButton", "Start", true);
linkButton(mMainView, R.id.controller_button_triangle, "TriangleButton", "Triangle", true);
linkButton(mMainView, R.id.controller_button_circle, "CircleButton", "Circle", true);
linkButton(mMainView, R.id.controller_button_cross, "CrossButton", "Cross", true);
linkButton(mMainView, R.id.controller_button_square, "SquareButton", "Square", true);
linkButton(mMainView, R.id.controller_button_r1, "R1Button", "R1", true);
linkButton(mMainView, R.id.controller_button_r2, "R2Button", "R2", true);
linkButton(mMainView, R.id.controller_button_up, "UpButton", "Up", true, false);
linkButton(mMainView, R.id.controller_button_right, "RightButton", "Right", true, false);
linkButton(mMainView, R.id.controller_button_down, "DownButton", "Down", true, false);
linkButton(mMainView, R.id.controller_button_left, "LeftButton", "Left", true, false);
linkButton(mMainView, R.id.controller_button_l1, "L1Button", "L1", true, true);
linkButton(mMainView, R.id.controller_button_l2, "L2Button", "L2", true, true);
linkButton(mMainView, R.id.controller_button_select, "SelectButton", "Select", true, true);
linkButton(mMainView, R.id.controller_button_start, "StartButton", "Start", true, true);
linkButton(mMainView, R.id.controller_button_triangle, "TriangleButton", "Triangle", true, true);
linkButton(mMainView, R.id.controller_button_circle, "CircleButton", "Circle", true, true);
linkButton(mMainView, R.id.controller_button_cross, "CrossButton", "Cross", true, true);
linkButton(mMainView, R.id.controller_button_square, "SquareButton", "Square", true, true);
linkButton(mMainView, R.id.controller_button_r1, "R1Button", "R1", true, true);
linkButton(mMainView, R.id.controller_button_r2, "R2Button", "R2", true, true);
if (!linkAxis(mMainView, R.id.controller_axis_left, "LeftAxis", "Left", true))
linkAxisToButtons(mMainView, R.id.controller_axis_left, "LeftAxis", "");
@ -259,13 +262,14 @@ public class TouchscreenControllerView extends FrameLayout {
requestLayout();
}
private void linkButton(View view, int id, String configName, String buttonName, boolean defaultVisibility) {
private void linkButton(View view, int id, String configName, String buttonName, boolean defaultVisibility, boolean isGlidable) {
TouchscreenControllerButtonView buttonView = (TouchscreenControllerButtonView) view.findViewById(id);
if (buttonView == null)
return;
buttonView.setConfigName(configName);
buttonView.setDefaultVisibility(defaultVisibility);
buttonView.setIsGlidable(isGlidable);
mButtonViews.add(buttonView);
int code = AndroidHostInterface.getControllerButtonCode(mControllerType, buttonName);
@ -442,6 +446,8 @@ public class TouchscreenControllerView extends FrameLayout {
if (!AndroidHostInterface.hasInstanceAndEmulationThreadIsRunning())
return false;
mGlidePairs.clear();
for (TouchscreenControllerButtonView buttonView : mButtonViews) {
buttonView.setPressed(false);
}
@ -455,7 +461,13 @@ public class TouchscreenControllerView extends FrameLayout {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_POINTER_UP: {
int pointerID = event.getPointerId(event.getActionIndex());
if (mGlidePairs.containsKey(pointerID))
mGlidePairs.remove(pointerID);
return true;
}
case MotionEvent.ACTION_MOVE: {
if (!AndroidHostInterface.hasInstanceAndEmulationThreadIsRunning())
return false;
@ -477,12 +489,17 @@ public class TouchscreenControllerView extends FrameLayout {
final int y = (int) event.getY(i);
if (rect.contains(x, y)) {
buttonView.setPressed(true);
int pointerID = event.getPointerId(i);
if (!mGlidePairs.containsKey(pointerID) && !mGlidePairs.containsValue(buttonView)) {
if (buttonView.getIsGlidable())
mGlidePairs.put(pointerID, buttonView);
}
pressed = true;
break;
}
}
if (!pressed)
if (!pressed && !mGlidePairs.containsValue(buttonView))
buttonView.setPressed(pressed);
}