Skip to content

Commit

Permalink
Add change gamepad positions: part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBRDeveloper committed Jan 2, 2024
1 parent 0811e17 commit 7c1623c
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.panda3ds.pandroid.app.preferences;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.app.BaseActivity;
import com.panda3ds.pandroid.app.base.BottomAlertDialog;
import com.panda3ds.pandroid.view.controller.map.ControllerMapper;
import com.panda3ds.pandroid.view.controller.map.ControllerProfileManager;
import com.panda3ds.pandroid.view.controller.map.NodeID;
import com.panda3ds.pandroid.view.controller.map.Profile;
import com.panda3ds.pandroid.view.controller.mapping.ControllerMapper;
import com.panda3ds.pandroid.view.controller.mapping.ControllerProfileManager;
import com.panda3ds.pandroid.view.controller.mapping.ControllerItem;
import com.panda3ds.pandroid.view.controller.mapping.Profile;

public class ControllerMapperPreferences extends Fragment {

Expand Down Expand Up @@ -47,13 +47,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
BottomAlertDialog builder = new BottomAlertDialog(v.getContext());
builder.setTitle("Visibility");
boolean[] visibleList = {
currentProfile.isVisible(NodeID.START),
currentProfile.isVisible(NodeID.SELECT),
currentProfile.isVisible(NodeID.L),
currentProfile.isVisible(NodeID.R),
currentProfile.isVisible(NodeID.DPAD),
currentProfile.isVisible(NodeID.JOYSTICK),
currentProfile.isVisible(NodeID.GAMEPAD),
currentProfile.isVisible(ControllerItem.START),
currentProfile.isVisible(ControllerItem.SELECT),
currentProfile.isVisible(ControllerItem.L),
currentProfile.isVisible(ControllerItem.R),
currentProfile.isVisible(ControllerItem.DPAD),
currentProfile.isVisible(ControllerItem.JOYSTICK),
currentProfile.isVisible(ControllerItem.GAMEPAD),
};
builder.setMultiChoiceItems(new CharSequence[]{
"Start", "Select", "L", "R", "Dpad", getString(R.string.axis), "A/B/X/Y"
Expand All @@ -63,13 +63,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

saveButton.setVisibility(View.VISIBLE);

currentProfile.setVisible(NodeID.START, visibleList[0]);
currentProfile.setVisible(NodeID.SELECT, visibleList[1]);
currentProfile.setVisible(NodeID.L, visibleList[2]);
currentProfile.setVisible(NodeID.R, visibleList[3]);
currentProfile.setVisible(NodeID.DPAD, visibleList[4]);
currentProfile.setVisible(NodeID.JOYSTICK, visibleList[5]);
currentProfile.setVisible(NodeID.GAMEPAD, visibleList[6]);
currentProfile.setVisible(ControllerItem.START, visibleList[0]);
currentProfile.setVisible(ControllerItem.SELECT, visibleList[1]);
currentProfile.setVisible(ControllerItem.L, visibleList[2]);
currentProfile.setVisible(ControllerItem.R, visibleList[3]);
currentProfile.setVisible(ControllerItem.DPAD, visibleList[4]);
currentProfile.setVisible(ControllerItem.JOYSTICK, visibleList[5]);
currentProfile.setVisible(ControllerItem.GAMEPAD, visibleList[6]);

mapper.refreshLayout();
}).setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss());
Expand All @@ -88,16 +88,16 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
requireActivity().finish();
});

view.findViewById(R.id.rotate).setOnClickListener(v->{
requireActivity().setRequestedOrientation(mapper.getCurrentWidth() > mapper.getCurrentHeight() ? ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
view.findViewById(R.id.rotate).setOnClickListener(v -> {
requireActivity().setRequestedOrientation(mapper.getCurrentWidth() > mapper.getCurrentHeight() ? ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
});

view.findViewById(R.id.delete).setVisibility(ControllerProfileManager.getProfileCount() > 1 ? View.VISIBLE : View.GONE);

saveButton.setVisibility(View.GONE);
}

public void onLocationChanged(NodeID id) {
public void onLocationChanged(ControllerItem id) {
saveButton.setVisibility(View.VISIBLE);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.panda3ds.pandroid.app.preferences;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
Expand All @@ -16,18 +13,24 @@
import com.panda3ds.pandroid.app.PreferenceActivity;
import com.panda3ds.pandroid.app.base.BasePreferenceFragment;
import com.panda3ds.pandroid.app.base.BottomAlertDialog;
import com.panda3ds.pandroid.view.controller.map.ControllerProfileManager;
import com.panda3ds.pandroid.view.controller.map.Profile;
import com.panda3ds.pandroid.view.controller.mapping.ControllerProfileManager;
import com.panda3ds.pandroid.view.controller.mapping.Profile;

import java.util.List;
import java.util.Objects;

public class InputPreferences extends BasePreferenceFragment {

public static final String ID_DEFAULT_CONTROLLER_PROFILE = "defaultControllerProfile";
public static final String ID_INPUT_MAP = "inputMap";
public static final String ID_CREATE_PROFILE = "createProfile";
private static final CharSequence ID_GAMEPAD_PROFILE_LIST = "gamepadProfileList";

@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
setPreferencesFromResource(R.xml.input_preference, rootKey);
setItemClick("inputMap", (item) -> PreferenceActivity.launch(requireContext(), InputMapPreferences.class));
setItemClick("add_screen_profile", (item) -> {
setItemClick(ID_INPUT_MAP, (item) -> PreferenceActivity.launch(requireContext(), InputMapPreferences.class));
setItemClick(ID_CREATE_PROFILE, (item) -> {
new BottomAlertDialog(requireContext())
.setTextInput(getString(R.string.name), (name) -> {
name = formatName(name);
Expand All @@ -40,33 +43,26 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable S
}).setTitle(R.string.create_profile).show();
});

setItemClick("defaultControllerProfile", (item)->{
setItemClick(ID_DEFAULT_CONTROLLER_PROFILE, (item) -> {
List<Profile> profiles = ControllerProfileManager.listAll();
String defaultProfileId = ControllerProfileManager.getDefaultProfile().getId();
int defaultProfileIndex = 0;
CharSequence[] names = new CharSequence[profiles.size()];
for (int i = 0; i < names.length; i++){
for (int i = 0; i < names.length; i++) {
names[i] = profiles.get(i).getName();
if (Objects.equals(profiles.get(i).getId(), defaultProfileId)){
if (Objects.equals(profiles.get(i).getId(), defaultProfileId)) {
defaultProfileIndex = i;
}
}
new BottomAlertDialog(item.getContext())
.setSingleChoiceItems(names, defaultProfileIndex, (dialog, which) -> {
dialog.dismiss();
ControllerProfileManager.setDefaultId(profiles.get(which).getId());
ControllerProfileManager.setDefaultProfileId(profiles.get(which).getId());
item.setSummary(profiles.get(which).getName());
}).setTitle(R.string.pref_default_controller_title).show();
});

((BaseActivity)requireActivity()).getSupportActionBar().setTitle(R.string.input);
}


@Override
public void onResume() {
super.onResume();
refresh();
((BaseActivity) requireActivity()).getSupportActionBar().setTitle(R.string.input);
}

public String formatName(String name) {
Expand All @@ -82,13 +78,13 @@ public String formatName(String name) {
}

private void refresh() {
findPreference("defaultControllerProfile").setSummary(ControllerProfileManager.getDefaultProfile().getName());
findPreference(ID_DEFAULT_CONTROLLER_PROFILE).setSummary(ControllerProfileManager.getDefaultProfile().getName());
refreshScreenProfileList();
}

@SuppressLint("RestrictedApi")
private void refreshScreenProfileList() {
PreferenceCategory category = findPreference("screenGamepadProfiles");
PreferenceCategory category = findPreference(ID_GAMEPAD_PROFILE_LIST);
Preference add = category.getPreference(category.getPreferenceCount() - 1);
category.removeAll();
category.setOrderingAsAdded(true);
Expand All @@ -109,4 +105,10 @@ private void refreshScreenProfileList() {
add.setOrder(category.getPreferenceCount());
category.addPreference(add);
}
}

@Override
public void onResume() {
super.onResume();
refresh();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public class Constants {
public static final String PREF_GLOBAL_CONFIG = "app.GlobalConfig";
public static final String PREF_GAME_UTILS = "app.GameUtils";
public static final String PREF_INPUT_MAP = "app.InputMap";
public static final String PREF_SCREEN_CONTROLLER_PROFILES = "app.input.overlay";
public static final String PREF_SCREEN_CONTROLLER_PROFILES = "app.input.ScreenControllerManager";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.utils.Constants;
import com.panda3ds.pandroid.view.controller.ControllerLayout;
import com.panda3ds.pandroid.view.controller.map.ControllerProfileManager;
import com.panda3ds.pandroid.view.controller.map.NodeID;
import com.panda3ds.pandroid.view.controller.map.Profile;
import com.panda3ds.pandroid.view.controller.mapping.ControllerProfileManager;
import com.panda3ds.pandroid.view.controller.mapping.ControllerItem;
import com.panda3ds.pandroid.view.controller.mapping.Profile;
import com.panda3ds.pandroid.view.controller.nodes.Button;
import com.panda3ds.pandroid.view.controller.nodes.Joystick;

Expand Down Expand Up @@ -68,12 +68,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

private void applyProfileMap() {
Profile profile = ControllerProfileManager.getDefaultProfile();
profile.applyToView(NodeID.L,findViewById(R.id.button_l), width, height);
profile.applyToView(NodeID.R, findViewById(R.id.button_r), width, height);
profile.applyToView(NodeID.START, findViewById(R.id.button_start), width, height);
profile.applyToView(NodeID.SELECT, findViewById(R.id.button_select), width, height);
profile.applyToView(NodeID.JOYSTICK, findViewById(R.id.left_analog), width, height);
profile.applyToView(NodeID.GAMEPAD, findViewById(R.id.gamepad), width, height);
profile.applyToView(NodeID.DPAD, findViewById(R.id.dpad), width, height);
profile.applyToView(ControllerItem.L,findViewById(R.id.button_l), width, height);
profile.applyToView(ControllerItem.R, findViewById(R.id.button_r), width, height);
profile.applyToView(ControllerItem.START, findViewById(R.id.button_start), width, height);
profile.applyToView(ControllerItem.SELECT, findViewById(R.id.button_select), width, height);
profile.applyToView(ControllerItem.JOYSTICK, findViewById(R.id.left_analog), width, height);
profile.applyToView(ControllerItem.GAMEPAD, findViewById(R.id.gamepad), width, height);
profile.applyToView(ControllerItem.DPAD, findViewById(R.id.dpad), width, height);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.panda3ds.pandroid.view.controller.mapping;

public enum ControllerItem {
START,
SELECT,
L,R,
GAMEPAD,
DPAD, JOYSTICK
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.panda3ds.pandroid.view.controller.map;
package com.panda3ds.pandroid.view.controller.mapping;

import android.annotation.SuppressLint;
import android.content.Context;
Expand Down Expand Up @@ -28,7 +28,7 @@ public class ControllerMapper extends FrameLayout {
private final Paint selectionPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int width = -1;
private int height = -1;
private Function<NodeID> changeListener;
private Function<ControllerItem> changeListener;

public ControllerMapper(@NonNull Context context) {
this(context, null);
Expand All @@ -53,19 +53,19 @@ public ControllerMapper(@NonNull Context context, @Nullable AttributeSet attrs,
selectionPaint.setPathEffect(new DashPathEffect(new float[]{dp * 10, dp * 10}, 0.0f));
}

public void initialize(Function<NodeID> changeListener, Profile profile) {
public void initialize(Function<ControllerItem> changeListener, Profile profile) {
this.profile = profile;
this.changeListener = changeListener;

measure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);

new MoveElementListener(NodeID.L, findViewById(R.id.button_l));
new MoveElementListener(NodeID.R, findViewById(R.id.button_r));
new MoveElementListener(NodeID.START, findViewById(R.id.button_start));
new MoveElementListener(NodeID.SELECT, findViewById(R.id.button_select));
new MoveElementListener(NodeID.DPAD, findViewById(R.id.dpad));
new MoveElementListener(NodeID.GAMEPAD, findViewById(R.id.gamepad));
new MoveElementListener(NodeID.JOYSTICK, findViewById(R.id.left_analog));
new MoveElementListener(ControllerItem.L, findViewById(R.id.button_l));
new MoveElementListener(ControllerItem.R, findViewById(R.id.button_r));
new MoveElementListener(ControllerItem.START, findViewById(R.id.button_start));
new MoveElementListener(ControllerItem.SELECT, findViewById(R.id.button_select));
new MoveElementListener(ControllerItem.DPAD, findViewById(R.id.dpad));
new MoveElementListener(ControllerItem.GAMEPAD, findViewById(R.id.gamepad));
new MoveElementListener(ControllerItem.JOYSTICK, findViewById(R.id.left_analog));
}

@Override
Expand Down Expand Up @@ -134,13 +134,13 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

public void refreshLayout() {
if (profile != null) {
profile.applyToView(NodeID.L, findViewById(R.id.button_l), width, height);
profile.applyToView(NodeID.R, findViewById(R.id.button_r), width, height);
profile.applyToView(NodeID.START, findViewById(R.id.button_start), width, height);
profile.applyToView(NodeID.SELECT, findViewById(R.id.button_select), width, height);
profile.applyToView(NodeID.DPAD, findViewById(R.id.dpad), width, height);
profile.applyToView(NodeID.GAMEPAD, findViewById(R.id.gamepad), width, height);
profile.applyToView(NodeID.JOYSTICK, findViewById(R.id.left_analog), width, height);
profile.applyToView(ControllerItem.L, findViewById(R.id.button_l), width, height);
profile.applyToView(ControllerItem.R, findViewById(R.id.button_r), width, height);
profile.applyToView(ControllerItem.START, findViewById(R.id.button_start), width, height);
profile.applyToView(ControllerItem.SELECT, findViewById(R.id.button_select), width, height);
profile.applyToView(ControllerItem.DPAD, findViewById(R.id.dpad), width, height);
profile.applyToView(ControllerItem.GAMEPAD, findViewById(R.id.gamepad), width, height);
profile.applyToView(ControllerItem.JOYSTICK, findViewById(R.id.left_analog), width, height);
}
}

Expand All @@ -153,12 +153,12 @@ public int getCurrentHeight() {
}

public class MoveElementListener implements OnTouchListener {
private final NodeID id;
private final ControllerItem id;
private final View view;
private final Vector2 downPosition = new Vector2(0.0f, 0.0f);
private boolean down = false;

public MoveElementListener(NodeID id, View view) {
public MoveElementListener(ControllerItem id, View view) {
this.view = view;
this.id = id;
this.view.setOnTouchListener(this);
Expand All @@ -181,7 +181,7 @@ public boolean onTouch(View v, MotionEvent event) {
profile.setLocation(id, x, y, width, height);
profile.applyToView(id, view, width, height);

if(changeListener != null){
if (changeListener != null) {
changeListener.run(id);
}

Expand Down
Loading

0 comments on commit 7c1623c

Please sign in to comment.