Add option to clear key mappings

This commit is contained in:
Rafael Caetano 2020-05-29 00:33:42 +01:00
parent f737023795
commit 8bca195c5c
5 changed files with 40 additions and 2 deletions

View File

@ -3,6 +3,8 @@ package me.magnum.melonds.model;
import androidx.annotation.NonNull;
public class InputConfig {
public static final int KEY_NOT_SET = -1;
private Input input;
private int key;
@ -12,7 +14,7 @@ public class InputConfig {
}
public InputConfig(Input input) {
this(input, -1);
this(input, KEY_NOT_SET);
}
public Input getInput() {
@ -28,7 +30,7 @@ public class InputConfig {
}
public boolean hasKeyAssigned() {
return key != -1;
return key != KEY_NOT_SET;
}
@NonNull

View File

@ -5,6 +5,7 @@ import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
@ -45,6 +46,19 @@ public class InputSetupActivity extends AppCompatActivity {
waitingForInput = true;
inputUnderConfiguration = inputConfig;
}
@Override
public void onInputConfigCleared(InputConfig inputConfig) {
if (inputUnderConfiguration != null) {
if (inputConfig.getInput() == inputUnderConfiguration.getInput())
viewModel.stopUpdatingInputConfig(inputConfig.getInput());
inputUnderConfiguration = null;
waitingForInput = false;
}
viewModel.clearInput(inputConfig.getInput());
}
});
this.inputListAdapter.setHasStableIds(true);
@ -104,6 +118,7 @@ public class InputSetupActivity extends AppCompatActivity {
public class InputViewHolder extends RecyclerView.ViewHolder {
private TextView textInputName;
private TextView textAssignedInputName;
private ImageView imageClearInput;
private StatefulInputConfig inputConfig;
@ -111,6 +126,7 @@ public class InputSetupActivity extends AppCompatActivity {
super(itemView);
this.textInputName = itemView.findViewById(R.id.text_input_name);
this.textAssignedInputName = itemView.findViewById(R.id.text_assigned_input_name);
this.imageClearInput = itemView.findViewById(R.id.image_input_clear);
}
public void setInputConfig(StatefulInputConfig config) {
@ -166,6 +182,12 @@ public class InputSetupActivity extends AppCompatActivity {
inputConfigClickedListener.onInputConfigClicked(viewHolder.getInputConfig());
}
});
viewHolder.imageClearInput.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputConfigClickedListener.onInputConfigCleared(viewHolder.getInputConfig());
}
});
return viewHolder;
}
@ -188,5 +210,6 @@ public class InputSetupActivity extends AppCompatActivity {
private interface OnInputConfigClickedListener {
void onInputConfigClicked(InputConfig inputConfig);
void onInputConfigCleared(InputConfig inputConfig);
}
}

View File

@ -82,6 +82,10 @@ public class InputSetupViewModel extends ViewModel {
}
}
public void clearInput(Input input) {
updateInputConfig(input, InputConfig.KEY_NOT_SET);
}
private void onConfigsChanged() {
inputConfigsBehaviour.onNext(this.inputConfigs);

View File

@ -0,0 +1,5 @@
<vector android:height="36dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="36dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -32,10 +33,13 @@
</LinearLayout>
<ImageView
android:id="@+id/image_input_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="?attr/selectableItemBackground"
android:tint="@color/romConfigButtonDefault"
app:srcCompat="@drawable/ic_clear"
android:layout_centerVertical="true" />
</RelativeLayout>