Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow making arbitrary buttons toggleable #6090

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package net.kdt.pojavlaunch.customcontrols.gamepad;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SwitchCompat;
import androidx.recyclerview.widget.RecyclerView;

import net.kdt.pojavlaunch.EfficientAndroidLWJGLKeycode;
Expand Down Expand Up @@ -152,15 +153,17 @@ protected void onDownStateChanged(boolean isDown) {
}
}

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 final Context mContext;
private final ImageView mButtonIcon;
private final ImageView mExpansionIndicator;
private final Spinner[] mKeySpinners;
private final View mExpandedView;
private final SwitchCompat mToggleableSwitch;
private final TextView mKeycodeLabel;
private int mAttachedPosition = -1;
private GamepadEmulatedButton mAttachedButton;
private short[] mKeycodes;

public ViewHolder(@NonNull View itemView) {
Expand All @@ -170,6 +173,8 @@ public ViewHolder(@NonNull View itemView) {
mExpandedView = itemView.findViewById(R.id.controller_mapper_expanded_view);
mExpansionIndicator = itemView.findViewById(R.id.controller_mapper_expand_button);
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);
defaultView.setOnClickListener(this);
mKeySpinners = new Spinner[4];
Expand All @@ -192,6 +197,15 @@ private void attach(int 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;

int spinnerIndex;
Expand All @@ -217,6 +231,7 @@ private void attach(int index) {
private void detach() {
mRebinderButtons[mAttachedPosition].changeViewHolder(null);
mAttachedPosition = -1;
mAttachedButton = null;
}
private void setPressed(boolean pressed) {
itemView.setBackgroundColor(pressed ? COLOR_ACTIVE_BUTTON : Color.TRANSPARENT);
Expand Down Expand Up @@ -276,6 +291,17 @@ public void onClick(View view) {
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
Expand All @@ -299,15 +325,13 @@ public void attachGrabListener(GrabListener grabListener) {
grabListener.onGrabState(mGrabState);
}

// Cannot do it another way
@SuppressLint("NotifyDataSetChanged")
public void setGrabState(boolean newState) {
mGrabState = newState;
if(mGamepadGrabListener != null) mGamepadGrabListener.onGrabState(newState);
if(mGrabState == mOldState) return;
updateRealButtons();
updateStickIcons();
notifyDataSetChanged();
notifyItemRangeChanged(0, mRebinderButtons.length);
mOldState = mGrabState;
}
}
12 changes: 12 additions & 0 deletions app_pojavlauncher/src/main/res/layout/item_controller_mapping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@
android:layout_height="wrap_content"
android:orientation="vertical"
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>
</LinearLayout>

Loading