mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-15 15:48:26 -04:00
Feat[gamepad]: allow making buttons toggleable (exluding stick-emulated buttons)
This commit is contained in:
parent
b236447c42
commit
a6a6a2965e
@ -1,6 +1,5 @@
|
|||||||
package net.kdt.pojavlaunch.customcontrols.gamepad;
|
package net.kdt.pojavlaunch.customcontrols.gamepad;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@ -8,10 +7,12 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.CompoundButton;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.Spinner;
|
import android.widget.Spinner;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.appcompat.widget.SwitchCompat;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import net.kdt.pojavlaunch.EfficientAndroidLWJGLKeycode;
|
import net.kdt.pojavlaunch.EfficientAndroidLWJGLKeycode;
|
||||||
@ -152,15 +153,17 @@ public class GamepadMapperAdapter extends RecyclerView.Adapter<GamepadMapperAdap
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ViewHolder extends RecyclerView.ViewHolder implements AdapterView.OnItemSelectedListener, View.OnClickListener {
|
public class ViewHolder extends RecyclerView.ViewHolder implements AdapterView.OnItemSelectedListener, View.OnClickListener, CompoundButton.OnCheckedChangeListener {
|
||||||
private static final int COLOR_ACTIVE_BUTTON = 0x2000FF00;
|
private static final int COLOR_ACTIVE_BUTTON = 0x2000FF00;
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final ImageView mButtonIcon;
|
private final ImageView mButtonIcon;
|
||||||
private final ImageView mExpansionIndicator;
|
private final ImageView mExpansionIndicator;
|
||||||
private final Spinner[] mKeySpinners;
|
private final Spinner[] mKeySpinners;
|
||||||
private final View mExpandedView;
|
private final View mExpandedView;
|
||||||
|
private final SwitchCompat mToggleableSwitch;
|
||||||
private final TextView mKeycodeLabel;
|
private final TextView mKeycodeLabel;
|
||||||
private int mAttachedPosition = -1;
|
private int mAttachedPosition = -1;
|
||||||
|
private GamepadEmulatedButton mAttachedButton;
|
||||||
private short[] mKeycodes;
|
private short[] mKeycodes;
|
||||||
|
|
||||||
public ViewHolder(@NonNull View itemView) {
|
public ViewHolder(@NonNull View itemView) {
|
||||||
@ -170,6 +173,8 @@ public class GamepadMapperAdapter extends RecyclerView.Adapter<GamepadMapperAdap
|
|||||||
mExpandedView = itemView.findViewById(R.id.controller_mapper_expanded_view);
|
mExpandedView = itemView.findViewById(R.id.controller_mapper_expanded_view);
|
||||||
mExpansionIndicator = itemView.findViewById(R.id.controller_mapper_expand_button);
|
mExpansionIndicator = itemView.findViewById(R.id.controller_mapper_expand_button);
|
||||||
mKeycodeLabel = itemView.findViewById(R.id.controller_mapper_keycode_label);
|
mKeycodeLabel = itemView.findViewById(R.id.controller_mapper_keycode_label);
|
||||||
|
mToggleableSwitch = itemView.findViewById(R.id.controller_mapper_toggleable_switch);
|
||||||
|
mToggleableSwitch.setOnCheckedChangeListener(this);
|
||||||
View defaultView = itemView.findViewById(R.id.controller_mapper_default_view);
|
View defaultView = itemView.findViewById(R.id.controller_mapper_default_view);
|
||||||
defaultView.setOnClickListener(this);
|
defaultView.setOnClickListener(this);
|
||||||
mKeySpinners = new Spinner[4];
|
mKeySpinners = new Spinner[4];
|
||||||
@ -192,6 +197,15 @@ public class GamepadMapperAdapter extends RecyclerView.Adapter<GamepadMapperAdap
|
|||||||
|
|
||||||
GamepadEmulatedButton realButton = mRealButtons[index];
|
GamepadEmulatedButton realButton = mRealButtons[index];
|
||||||
|
|
||||||
|
mAttachedButton = realButton;
|
||||||
|
|
||||||
|
if(realButton instanceof GamepadButton) {
|
||||||
|
mToggleableSwitch.setChecked(((GamepadButton)realButton).isToggleable);
|
||||||
|
mToggleableSwitch.setVisibility(View.VISIBLE);
|
||||||
|
}else {
|
||||||
|
mToggleableSwitch.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
mKeycodes = realButton.keycodes;
|
mKeycodes = realButton.keycodes;
|
||||||
|
|
||||||
int spinnerIndex;
|
int spinnerIndex;
|
||||||
@ -217,6 +231,7 @@ public class GamepadMapperAdapter extends RecyclerView.Adapter<GamepadMapperAdap
|
|||||||
private void detach() {
|
private void detach() {
|
||||||
mRebinderButtons[mAttachedPosition].changeViewHolder(null);
|
mRebinderButtons[mAttachedPosition].changeViewHolder(null);
|
||||||
mAttachedPosition = -1;
|
mAttachedPosition = -1;
|
||||||
|
mAttachedButton = null;
|
||||||
}
|
}
|
||||||
private void setPressed(boolean pressed) {
|
private void setPressed(boolean pressed) {
|
||||||
itemView.setBackgroundColor(pressed ? COLOR_ACTIVE_BUTTON : Color.TRANSPARENT);
|
itemView.setBackgroundColor(pressed ? COLOR_ACTIVE_BUTTON : Color.TRANSPARENT);
|
||||||
@ -276,6 +291,17 @@ public class GamepadMapperAdapter extends RecyclerView.Adapter<GamepadMapperAdap
|
|||||||
mExpandedView.setVisibility(View.GONE);
|
mExpandedView.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
|
||||||
|
if(!(mAttachedButton instanceof GamepadButton)) return;
|
||||||
|
((GamepadButton)mAttachedButton).isToggleable = checked;
|
||||||
|
try {
|
||||||
|
GamepadMapStore.save();
|
||||||
|
}catch (Exception e) {
|
||||||
|
Tools.showError(compoundButton.getContext(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -299,15 +325,13 @@ public class GamepadMapperAdapter extends RecyclerView.Adapter<GamepadMapperAdap
|
|||||||
grabListener.onGrabState(mGrabState);
|
grabListener.onGrabState(mGrabState);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cannot do it another way
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
|
||||||
public void setGrabState(boolean newState) {
|
public void setGrabState(boolean newState) {
|
||||||
mGrabState = newState;
|
mGrabState = newState;
|
||||||
if(mGamepadGrabListener != null) mGamepadGrabListener.onGrabState(newState);
|
if(mGamepadGrabListener != null) mGamepadGrabListener.onGrabState(newState);
|
||||||
if(mGrabState == mOldState) return;
|
if(mGrabState == mOldState) return;
|
||||||
updateRealButtons();
|
updateRealButtons();
|
||||||
updateStickIcons();
|
updateStickIcons();
|
||||||
notifyDataSetChanged();
|
notifyItemRangeChanged(0, mRebinderButtons.length);
|
||||||
mOldState = mGrabState;
|
mOldState = mGrabState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -136,6 +136,18 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
app:layout_constraintGuide_percent="0.5" />
|
app:layout_constraintGuide_percent="0.5" />
|
||||||
|
|
||||||
|
<androidx.appcompat.widget.SwitchCompat
|
||||||
|
android:id="@+id/controller_mapper_toggleable_switch"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="48dp"
|
||||||
|
android:text="@string/customctrl_toggle"
|
||||||
|
android:layout_marginTop="@dimen/padding_moderate"
|
||||||
|
android:layout_marginHorizontal="@dimen/padding_moderate"
|
||||||
|
android:paddingHorizontal="@dimen/padding_medium"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/controller_mapper_key_spinner3" />
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user