Skip to content

Commit

Permalink
Fix[gamepad_remapper]: general fixes
Browse files Browse the repository at this point in the history
- Moved exit logic from mapperadapter to mapperfragment
- Replaced color indicator with row highlight
- Removed reloadGamepadMaps()
- Changed some design elements
  • Loading branch information
artdeell committed Aug 25, 2024
1 parent a0ff100 commit 012e71e
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
public class DefaultDataProvider implements GamepadDataProvider {
public static final DefaultDataProvider INSTANCE = new DefaultDataProvider();

private DefaultDataProvider() {
reloadGamepadMaps();
}
// Cannot instantiate this class publicly
private DefaultDataProvider() {}

@Override
public GamepadMap getGameMap() {
Expand All @@ -31,9 +30,4 @@ public boolean isGrabbing() {
public void attachGrabListener(GrabListener grabListener) {
CallbackBridge.addGrabListener(grabListener);
}

@Override
public void reloadGamepadMaps() {
GamepadMapStore.load();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.core.content.res.ResourcesCompat;
import androidx.core.math.MathUtils;
import androidx.fragment.app.Fragment;

import net.kdt.pojavlaunch.GrabListener;
import net.kdt.pojavlaunch.LwjglGlfwKeycode;
Expand Down Expand Up @@ -126,7 +124,7 @@ public void doFrame(long frameTimeNanos) {
CallbackBridge.sendCursorPos(CallbackBridge.windowWidth/2f, CallbackBridge.windowHeight/2f);

if(showCursor) {
((FrameLayout)contextView.getParent()).addView(mPointerImageView);
((ViewGroup)contextView.getParent()).addView(mPointerImageView);
}


Expand All @@ -140,7 +138,7 @@ public void doFrame(long frameTimeNanos) {
public void reloadGamepadMaps() {
if(mGameMap != null) mGameMap.resetPressedState();
if(mMenuMap != null) mMenuMap.resetPressedState();
mMapProvider.reloadGamepadMaps();
GamepadMapStore.load();
mGameMap = mMapProvider.getGameMap();
mMenuMap = mMapProvider.getMenuMap();
mCurrentMap = mGameMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ public interface GamepadDataProvider {
GamepadMap getGameMap();
boolean isGrabbing();
void attachGrabListener(GrabListener grabListener);
void reloadGamepadMaps();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Looper;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,9 +19,7 @@
import net.kdt.pojavlaunch.GrabListener;
import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.customcontrols.EditorExitable;

import android.os.Handler;
import android.widget.TextView;

public class GamepadMapperAdapter extends RecyclerView.Adapter<GamepadMapperAdapter.ViewHolder> implements GamepadDataProvider {
Expand All @@ -30,22 +28,19 @@ public class GamepadMapperAdapter extends RecyclerView.Adapter<GamepadMapperAdap
private GamepadMap mSimulatedGamepadMap;
private RebinderButton[] mRebinderButtons;
private GamepadEmulatedButton[] mRealButtons;
private final Handler mExitHandler = new Handler(Looper.getMainLooper());
private final Runnable mExitRunnable;
private final ArrayAdapter<String> mKeyAdapter;
private final int mSpecialKeycodeCount;
private GrabListener mGamepadGrabListener;
private boolean mGrabState = false;
private boolean mOldState = false;

public GamepadMapperAdapter(Context context, EditorExitable exitable) {
public GamepadMapperAdapter(Context context) {
GamepadMapStore.load();
mKeyAdapter = new ArrayAdapter<>(context, R.layout.item_centered_textview);
mKeyAdapter = new ArrayAdapter<>(context, R.layout.item_centered_textview_large);
String[] specialKeycodeNames = GamepadMap.getSpecialKeycodeNames();
mSpecialKeycodeCount = specialKeycodeNames.length;
mKeyAdapter.addAll(specialKeycodeNames);
mKeyAdapter.addAll(EfficientAndroidLWJGLKeycode.generateKeyName());
this.mExitRunnable = exitable::exitEditor;
createRebinderMap();
updateRealButtons();
}
Expand All @@ -67,8 +62,8 @@ private void createRebinderMap() {
mSimulatedGamepadMap.SHOULDER_LEFT = mRebinderButtons[index++] = new RebinderButton(R.drawable.shoulder_left, R.string.controller_button_shoulder_left);
mSimulatedGamepadMap.DIRECTION_FORWARD = mRebinderButtons[index++] = new RebinderButton(R.drawable.stick_right, R.string.controller_direction_forward);
mSimulatedGamepadMap.DIRECTION_RIGHT = mRebinderButtons[index++] = new RebinderButton(R.drawable.stick_right, R.string.controller_direction_right);
mSimulatedGamepadMap.DIRECTION_LEFT = mRebinderButtons[index++] = new RebinderButton(R.drawable.stick_right, R.string.controller_direction_left);
mSimulatedGamepadMap.DIRECTION_BACKWARD = mRebinderButtons[index++] = new RebinderButton(R.drawable.stick_right, R.string.controller_direction_backward);
mSimulatedGamepadMap.DIRECTION_LEFT = mRebinderButtons[index++] = new RebinderButton(R.drawable.stick_right, R.string.controller_direction_left);
mSimulatedGamepadMap.DIRECTION_BACKWARD = mRebinderButtons[index++] = new RebinderButton(R.drawable.stick_right, R.string.controller_direction_backward);
mSimulatedGamepadMap.THUMBSTICK_RIGHT = mRebinderButtons[index++] = new RebinderButton(R.drawable.stick_right_click, R.string.controller_stick_press_r);
mSimulatedGamepadMap.THUMBSTICK_LEFT = mRebinderButtons[index++] = new RebinderButton(R.drawable.stick_left_click, R.string.controller_stick_press_l);
mSimulatedGamepadMap.DPAD_UP = mRebinderButtons[index++] = new RebinderButton(R.drawable.dpad_up, R.string.controller_dpad_up);
Expand Down Expand Up @@ -136,7 +131,7 @@ private void updateStickIcons() {
((RebinderButton)mSimulatedGamepadMap.DIRECTION_LEFT).iconResourceId = stickIcon;
}

private class RebinderButton extends GamepadButton {
private static class RebinderButton extends GamepadButton {
public int iconResourceId;
public final int localeResourceId;
private GamepadMapperAdapter.ViewHolder mButtonHolder;
Expand All @@ -153,31 +148,29 @@ public void changeViewHolder(GamepadMapperAdapter.ViewHolder viewHolder) {

@Override
protected void onDownStateChanged(boolean isDown) {
if(iconResourceId == R.drawable.button_select) {
if(isDown) mExitHandler.postDelayed(mExitRunnable, 400);
else mExitHandler.removeCallbacks(mExitRunnable);
}
if(mButtonHolder == null) return;
mButtonHolder.setPressed(isDown);
}
}

public class ViewHolder extends RecyclerView.ViewHolder implements AdapterView.OnItemSelectedListener, View.OnClickListener {
private static final int COLOR_ACTIVE_BUTTON = 0x2000FF00;
private final Context mContext;
private final ColorDrawable mBackgroundDrawable;
private final ImageView mButtonIcon;
private final ImageView mExpansionIndicator;
private final Spinner[] mKeySpinners;
private final View mClickIndicator;
private final View mExpandedView;
private final TextView mKeycodeLabel;
private int mAttachedPosition = -1;
private short[] mKeycodes;

public ViewHolder(@NonNull View itemView) {
super(itemView);
mBackgroundDrawable = new ColorDrawable(Color.TRANSPARENT);
itemView.setBackground(mBackgroundDrawable);
mContext = itemView.getContext();
mButtonIcon = itemView.findViewById(R.id.controller_mapper_button);
mClickIndicator = itemView.findViewById(R.id.controller_mapper_indicator);
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);
Expand Down Expand Up @@ -230,7 +223,7 @@ private void detach() {
mAttachedPosition = -1;
}
private void setPressed(boolean pressed) {
mClickIndicator.setBackgroundColor(pressed ? Color.GREEN : Color.DKGRAY);
mBackgroundDrawable.setColor(pressed ? COLOR_ACTIVE_BUTTON : Color.TRANSPARENT);
}

private void updateKeycodeLabel() {
Expand Down Expand Up @@ -310,10 +303,6 @@ public void attachGrabListener(GrabListener grabListener) {
grabListener.onGrabState(mGrabState);
}

@Override
public void reloadGamepadMaps() {
}

// Cannot do it another way
@SuppressLint("NotifyDataSetChanged")
public void setGrabState(boolean newState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.os.Handler;
import android.os.Looper;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
Expand All @@ -18,16 +19,14 @@
import androidx.recyclerview.widget.RecyclerView;

import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.customcontrols.EditorExitable;
import net.kdt.pojavlaunch.customcontrols.gamepad.Gamepad;
import net.kdt.pojavlaunch.customcontrols.gamepad.GamepadMapperAdapter;

import fr.spse.gamepad_remapper.RemapperManager;
import fr.spse.gamepad_remapper.RemapperView;

public class GamepadMapperFragment extends Fragment implements
View.OnKeyListener, View.OnGenericMotionListener,
EditorExitable, AdapterView.OnItemSelectedListener {
View.OnKeyListener, View.OnGenericMotionListener, AdapterView.OnItemSelectedListener {
public static final String TAG = "GamepadMapperFragment";
private final RemapperView.Builder mRemapperViewBuilder = new RemapperView.Builder(null)
.remapA(true)
Expand All @@ -42,9 +41,13 @@ public class GamepadMapperFragment extends Fragment implements
.remapRightShoulder(true)
.remapLeftTrigger(true)
.remapRightTrigger(true);
private final Handler mExitHandler = new Handler(Looper.getMainLooper());
private final Runnable mExitRunnable = () -> {
Activity activity = getActivity();
if(activity == null) return;
activity.onBackPressed();
};
private RemapperManager mInputManager;
private RecyclerView mButtonRecyclerView;
private Spinner mGrabStateSpinner;
private GamepadMapperAdapter mMapperAdapter;
private Gamepad mGamepad;
public GamepadMapperFragment() {
Expand All @@ -54,29 +57,41 @@ public GamepadMapperFragment() {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mButtonRecyclerView = view.findViewById(R.id.gamepad_remapper_recycler);
mMapperAdapter = new GamepadMapperAdapter(view.getContext(), this);
mButtonRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
mButtonRecyclerView.setAdapter(mMapperAdapter);
mButtonRecyclerView.setOnKeyListener(this);
mButtonRecyclerView.setOnGenericMotionListener(this);
mButtonRecyclerView.requestFocus();
RecyclerView buttonRecyclerView = view.findViewById(R.id.gamepad_remapper_recycler);
mMapperAdapter = new GamepadMapperAdapter(view.getContext());
buttonRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
buttonRecyclerView.setAdapter(mMapperAdapter);
buttonRecyclerView.setOnKeyListener(this);
buttonRecyclerView.setOnGenericMotionListener(this);
buttonRecyclerView.requestFocus();
mInputManager = new RemapperManager(view.getContext(), mRemapperViewBuilder);
mGrabStateSpinner = view.findViewById(R.id.gamepad_remapper_mode_spinner);
ArrayAdapter<String> mGrabStateAdapter = new ArrayAdapter<>(view.getContext(), R.layout.item_centered_textview);
Spinner grabStateSpinner = view.findViewById(R.id.gamepad_remapper_mode_spinner);
ArrayAdapter<String> mGrabStateAdapter = new ArrayAdapter<>(view.getContext(), R.layout.support_simple_spinner_dropdown_item);
mGrabStateAdapter.addAll(getString(R.string.customctrl_visibility_in_menus), getString(R.string.customctrl_visibility_ingame));
mGrabStateSpinner.setAdapter(mGrabStateAdapter);
mGrabStateSpinner.setSelection(0);
mGrabStateSpinner.setOnItemSelectedListener(this);
grabStateSpinner.setAdapter(mGrabStateAdapter);
grabStateSpinner.setSelection(0);
grabStateSpinner.setOnItemSelectedListener(this);
}

private void createGamepad(View mainView, InputDevice inputDevice) {
mGamepad = new Gamepad(mainView, inputDevice, mMapperAdapter, false);
mGamepad = new Gamepad(mainView, inputDevice, mMapperAdapter, false) {
@Override
public void handleGamepadInput(int keycode, float value) {
if(keycode == KeyEvent.KEYCODE_BUTTON_SELECT) {
handleExitButton(value > 0.5);
}
super.handleGamepadInput(keycode, value);
}
};
}

private void handleExitButton(boolean isPressed) {
if(isPressed) mExitHandler.postDelayed(mExitRunnable, 400);
else mExitHandler.removeCallbacks(mExitRunnable);
}

@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
Log.i("onKey", keyEvent.toString());
View mainView = getView();
if(!Gamepad.isGamepadEvent(keyEvent) || mainView == null) return false;
if(mGamepad == null) createGamepad(mainView, keyEvent.getDevice());
Expand All @@ -86,21 +101,13 @@ public boolean onKey(View view, int i, KeyEvent keyEvent) {

@Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
Log.i("onGenericMotion", motionEvent.toString());
View mainView = getView();
if(!Gamepad.isGamepadEvent(motionEvent) || mainView == null) return false;
if(mGamepad == null) createGamepad(mainView, motionEvent.getDevice());
mInputManager.handleMotionEventInput(mainView.getContext(), motionEvent, mGamepad);
return true;
}

@Override
public void exitEditor() {
Activity activity = getActivity();
if(activity == null) return;
activity.onBackPressed();
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
boolean grab = i == 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:background="@color/background_app">
Expand All @@ -27,6 +26,7 @@
android:layout_width="30sp"
android:layout_height="30sp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/controller_button_select"
app:srcCompat="@drawable/button_select" />

<TextView
Expand All @@ -35,38 +35,43 @@
android:layout_gravity="center_vertical"
android:textColor="?android:attr/textColorPrimary"
android:text="@string/controller_remapper_exit_part2" />

</LinearLayout>

<TextView
android:id="@+id/gamepad_remapper_mode_label"
<LinearLayout
android:id="@+id/gamepad_remapper_mode_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/controller_remapper_operating_mode"
android:textColor="?android:attr/textColorPrimary"
android:orientation="vertical"
android:layout_marginTop="@dimen/padding_heavy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/guideline4"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintStart_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/gamepad_remapper_mode_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/controller_remapper_operating_mode"
android:textColor="?android:attr/textColorPrimary" />

<Spinner
android:id="@+id/gamepad_remapper_mode_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp" />
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/gamepad_remapper_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginTop="@dimen/padding_heavy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/gamepad_remapper_mode_spinner" />

<Spinner
android:id="@+id/gamepad_remapper_mode_spinner"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginTop="11dp"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="@+id/gamepad_remapper_mode_label"
app:layout_constraintStart_toStartOf="@+id/gamepad_remapper_mode_label"
app:layout_constraintTop_toBottomOf="@+id/gamepad_remapper_mode_label" />
app:layout_constraintTop_toBottomOf="@+id/gamepad_remapper_mode_container" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:gravity="center"
android:minHeight="48dp"/>
Loading

0 comments on commit 012e71e

Please sign in to comment.