Add change gamepad positions: part 2

This commit is contained in:
Gabriel 2024-01-01 21:49:53 -04:00
commit 7c1623c2cf
15 changed files with 163 additions and 160 deletions

View file

@ -1,6 +1,5 @@
package com.panda3ds.pandroid.app.preferences; package com.panda3ds.pandroid.app.preferences;
import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;

View file

@ -14,10 +14,10 @@ import androidx.fragment.app.Fragment;
import com.panda3ds.pandroid.R; import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.app.BaseActivity; import com.panda3ds.pandroid.app.BaseActivity;
import com.panda3ds.pandroid.app.base.BottomAlertDialog; import com.panda3ds.pandroid.app.base.BottomAlertDialog;
import com.panda3ds.pandroid.view.controller.map.ControllerMapper; import com.panda3ds.pandroid.view.controller.mapping.ControllerMapper;
import com.panda3ds.pandroid.view.controller.map.ControllerProfileManager; import com.panda3ds.pandroid.view.controller.mapping.ControllerProfileManager;
import com.panda3ds.pandroid.view.controller.map.NodeID; import com.panda3ds.pandroid.view.controller.mapping.ControllerItem;
import com.panda3ds.pandroid.view.controller.map.Profile; import com.panda3ds.pandroid.view.controller.mapping.Profile;
public class ControllerMapperPreferences extends Fragment { public class ControllerMapperPreferences extends Fragment {
@ -47,13 +47,13 @@ public class ControllerMapperPreferences extends Fragment {
BottomAlertDialog builder = new BottomAlertDialog(v.getContext()); BottomAlertDialog builder = new BottomAlertDialog(v.getContext());
builder.setTitle("Visibility"); builder.setTitle("Visibility");
boolean[] visibleList = { boolean[] visibleList = {
currentProfile.isVisible(NodeID.START), currentProfile.isVisible(ControllerItem.START),
currentProfile.isVisible(NodeID.SELECT), currentProfile.isVisible(ControllerItem.SELECT),
currentProfile.isVisible(NodeID.L), currentProfile.isVisible(ControllerItem.L),
currentProfile.isVisible(NodeID.R), currentProfile.isVisible(ControllerItem.R),
currentProfile.isVisible(NodeID.DPAD), currentProfile.isVisible(ControllerItem.DPAD),
currentProfile.isVisible(NodeID.JOYSTICK), currentProfile.isVisible(ControllerItem.JOYSTICK),
currentProfile.isVisible(NodeID.GAMEPAD), currentProfile.isVisible(ControllerItem.GAMEPAD),
}; };
builder.setMultiChoiceItems(new CharSequence[]{ builder.setMultiChoiceItems(new CharSequence[]{
"Start", "Select", "L", "R", "Dpad", getString(R.string.axis), "A/B/X/Y" "Start", "Select", "L", "R", "Dpad", getString(R.string.axis), "A/B/X/Y"
@ -63,13 +63,13 @@ public class ControllerMapperPreferences extends Fragment {
saveButton.setVisibility(View.VISIBLE); saveButton.setVisibility(View.VISIBLE);
currentProfile.setVisible(NodeID.START, visibleList[0]); currentProfile.setVisible(ControllerItem.START, visibleList[0]);
currentProfile.setVisible(NodeID.SELECT, visibleList[1]); currentProfile.setVisible(ControllerItem.SELECT, visibleList[1]);
currentProfile.setVisible(NodeID.L, visibleList[2]); currentProfile.setVisible(ControllerItem.L, visibleList[2]);
currentProfile.setVisible(NodeID.R, visibleList[3]); currentProfile.setVisible(ControllerItem.R, visibleList[3]);
currentProfile.setVisible(NodeID.DPAD, visibleList[4]); currentProfile.setVisible(ControllerItem.DPAD, visibleList[4]);
currentProfile.setVisible(NodeID.JOYSTICK, visibleList[5]); currentProfile.setVisible(ControllerItem.JOYSTICK, visibleList[5]);
currentProfile.setVisible(NodeID.GAMEPAD, visibleList[6]); currentProfile.setVisible(ControllerItem.GAMEPAD, visibleList[6]);
mapper.refreshLayout(); mapper.refreshLayout();
}).setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss()); }).setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss());
@ -88,8 +88,8 @@ public class ControllerMapperPreferences extends Fragment {
requireActivity().finish(); requireActivity().finish();
}); });
view.findViewById(R.id.rotate).setOnClickListener(v->{ view.findViewById(R.id.rotate).setOnClickListener(v -> {
requireActivity().setRequestedOrientation(mapper.getCurrentWidth() > mapper.getCurrentHeight() ? ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 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); view.findViewById(R.id.delete).setVisibility(ControllerProfileManager.getProfileCount() > 1 ? View.VISIBLE : View.GONE);
@ -97,7 +97,7 @@ public class ControllerMapperPreferences extends Fragment {
saveButton.setVisibility(View.GONE); saveButton.setVisibility(View.GONE);
} }
public void onLocationChanged(NodeID id) { public void onLocationChanged(ControllerItem id) {
saveButton.setVisibility(View.VISIBLE); saveButton.setVisibility(View.VISIBLE);
} }
} }

View file

@ -1,12 +1,9 @@
package com.panda3ds.pandroid.app.preferences; package com.panda3ds.pandroid.app.preferences;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
@ -16,18 +13,24 @@ import com.panda3ds.pandroid.app.BaseActivity;
import com.panda3ds.pandroid.app.PreferenceActivity; import com.panda3ds.pandroid.app.PreferenceActivity;
import com.panda3ds.pandroid.app.base.BasePreferenceFragment; import com.panda3ds.pandroid.app.base.BasePreferenceFragment;
import com.panda3ds.pandroid.app.base.BottomAlertDialog; import com.panda3ds.pandroid.app.base.BottomAlertDialog;
import com.panda3ds.pandroid.view.controller.map.ControllerProfileManager; import com.panda3ds.pandroid.view.controller.mapping.ControllerProfileManager;
import com.panda3ds.pandroid.view.controller.map.Profile; import com.panda3ds.pandroid.view.controller.mapping.Profile;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
public class InputPreferences extends BasePreferenceFragment { 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 @Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) { public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
setPreferencesFromResource(R.xml.input_preference, rootKey); setPreferencesFromResource(R.xml.input_preference, rootKey);
setItemClick("inputMap", (item) -> PreferenceActivity.launch(requireContext(), InputMapPreferences.class)); setItemClick(ID_INPUT_MAP, (item) -> PreferenceActivity.launch(requireContext(), InputMapPreferences.class));
setItemClick("add_screen_profile", (item) -> { setItemClick(ID_CREATE_PROFILE, (item) -> {
new BottomAlertDialog(requireContext()) new BottomAlertDialog(requireContext())
.setTextInput(getString(R.string.name), (name) -> { .setTextInput(getString(R.string.name), (name) -> {
name = formatName(name); name = formatName(name);
@ -40,33 +43,26 @@ public class InputPreferences extends BasePreferenceFragment {
}).setTitle(R.string.create_profile).show(); }).setTitle(R.string.create_profile).show();
}); });
setItemClick("defaultControllerProfile", (item)->{ setItemClick(ID_DEFAULT_CONTROLLER_PROFILE, (item) -> {
List<Profile> profiles = ControllerProfileManager.listAll(); List<Profile> profiles = ControllerProfileManager.listAll();
String defaultProfileId = ControllerProfileManager.getDefaultProfile().getId(); String defaultProfileId = ControllerProfileManager.getDefaultProfile().getId();
int defaultProfileIndex = 0; int defaultProfileIndex = 0;
CharSequence[] names = new CharSequence[profiles.size()]; 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(); names[i] = profiles.get(i).getName();
if (Objects.equals(profiles.get(i).getId(), defaultProfileId)){ if (Objects.equals(profiles.get(i).getId(), defaultProfileId)) {
defaultProfileIndex = i; defaultProfileIndex = i;
} }
} }
new BottomAlertDialog(item.getContext()) new BottomAlertDialog(item.getContext())
.setSingleChoiceItems(names, defaultProfileIndex, (dialog, which) -> { .setSingleChoiceItems(names, defaultProfileIndex, (dialog, which) -> {
dialog.dismiss(); dialog.dismiss();
ControllerProfileManager.setDefaultId(profiles.get(which).getId()); ControllerProfileManager.setDefaultProfileId(profiles.get(which).getId());
item.setSummary(profiles.get(which).getName()); item.setSummary(profiles.get(which).getName());
}).setTitle(R.string.pref_default_controller_title).show(); }).setTitle(R.string.pref_default_controller_title).show();
}); });
((BaseActivity)requireActivity()).getSupportActionBar().setTitle(R.string.input); ((BaseActivity) requireActivity()).getSupportActionBar().setTitle(R.string.input);
}
@Override
public void onResume() {
super.onResume();
refresh();
} }
public String formatName(String name) { public String formatName(String name) {
@ -82,13 +78,13 @@ public class InputPreferences extends BasePreferenceFragment {
} }
private void refresh() { private void refresh() {
findPreference("defaultControllerProfile").setSummary(ControllerProfileManager.getDefaultProfile().getName()); findPreference(ID_DEFAULT_CONTROLLER_PROFILE).setSummary(ControllerProfileManager.getDefaultProfile().getName());
refreshScreenProfileList(); refreshScreenProfileList();
} }
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
private void refreshScreenProfileList() { private void refreshScreenProfileList() {
PreferenceCategory category = findPreference("screenGamepadProfiles"); PreferenceCategory category = findPreference(ID_GAMEPAD_PROFILE_LIST);
Preference add = category.getPreference(category.getPreferenceCount() - 1); Preference add = category.getPreference(category.getPreferenceCount() - 1);
category.removeAll(); category.removeAll();
category.setOrderingAsAdded(true); category.setOrderingAsAdded(true);
@ -109,4 +105,10 @@ public class InputPreferences extends BasePreferenceFragment {
add.setOrder(category.getPreferenceCount()); add.setOrder(category.getPreferenceCount());
category.addPreference(add); category.addPreference(add);
} }
@Override
public void onResume() {
super.onResume();
refresh();
}
} }

View file

@ -25,5 +25,5 @@ public class Constants {
public static final String PREF_GLOBAL_CONFIG = "app.GlobalConfig"; public static final String PREF_GLOBAL_CONFIG = "app.GlobalConfig";
public static final String PREF_GAME_UTILS = "app.GameUtils"; public static final String PREF_GAME_UTILS = "app.GameUtils";
public static final String PREF_INPUT_MAP = "app.InputMap"; 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";
} }

View file

@ -6,9 +6,9 @@ import com.panda3ds.pandroid.AlberDriver;
import com.panda3ds.pandroid.R; import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.utils.Constants; import com.panda3ds.pandroid.utils.Constants;
import com.panda3ds.pandroid.view.controller.ControllerLayout; import com.panda3ds.pandroid.view.controller.ControllerLayout;
import com.panda3ds.pandroid.view.controller.map.ControllerProfileManager; import com.panda3ds.pandroid.view.controller.mapping.ControllerProfileManager;
import com.panda3ds.pandroid.view.controller.map.NodeID; import com.panda3ds.pandroid.view.controller.mapping.ControllerItem;
import com.panda3ds.pandroid.view.controller.map.Profile; import com.panda3ds.pandroid.view.controller.mapping.Profile;
import com.panda3ds.pandroid.view.controller.nodes.Button; import com.panda3ds.pandroid.view.controller.nodes.Button;
import com.panda3ds.pandroid.view.controller.nodes.Joystick; import com.panda3ds.pandroid.view.controller.nodes.Joystick;
@ -68,12 +68,12 @@ public class PandaLayoutController extends ControllerLayout {
private void applyProfileMap() { private void applyProfileMap() {
Profile profile = ControllerProfileManager.getDefaultProfile(); Profile profile = ControllerProfileManager.getDefaultProfile();
profile.applyToView(NodeID.L,findViewById(R.id.button_l), width, height); profile.applyToView(ControllerItem.L,findViewById(R.id.button_l), width, height);
profile.applyToView(NodeID.R, findViewById(R.id.button_r), width, height); profile.applyToView(ControllerItem.R, findViewById(R.id.button_r), width, height);
profile.applyToView(NodeID.START, findViewById(R.id.button_start), width, height); profile.applyToView(ControllerItem.START, findViewById(R.id.button_start), width, height);
profile.applyToView(NodeID.SELECT, findViewById(R.id.button_select), width, height); profile.applyToView(ControllerItem.SELECT, findViewById(R.id.button_select), width, height);
profile.applyToView(NodeID.JOYSTICK, findViewById(R.id.left_analog), width, height); profile.applyToView(ControllerItem.JOYSTICK, findViewById(R.id.left_analog), width, height);
profile.applyToView(NodeID.GAMEPAD, findViewById(R.id.gamepad), width, height); profile.applyToView(ControllerItem.GAMEPAD, findViewById(R.id.gamepad), width, height);
profile.applyToView(NodeID.DPAD, findViewById(R.id.dpad), width, height); profile.applyToView(ControllerItem.DPAD, findViewById(R.id.dpad), width, height);
} }
} }

View file

@ -1,35 +0,0 @@
package com.panda3ds.pandroid.view.controller.map;
import androidx.annotation.NonNull;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class Layout {
private final Map<NodeID, Location> mapLocations = new HashMap<>();
public void setLocation(NodeID id, Location location) {
mapLocations.put(id, location);
}
@NotNull
public Location getLocation(NodeID id) {
if (!mapLocations.containsKey(id)) {
setLocation(id, new Location());
}
return Objects.requireNonNull(mapLocations.get(id));
}
@NonNull
@Override
public Layout clone() {
Layout cloned = new Layout();
for (NodeID key : mapLocations.keySet()){
cloned.setLocation(key, getLocation(key).clone());
}
return cloned;
}
}

View file

@ -1,9 +0,0 @@
package com.panda3ds.pandroid.view.controller.map;
public enum NodeID {
START,
SELECT,
L,R,
GAMEPAD,
DPAD, JOYSTICK
}

View file

@ -0,0 +1,9 @@
package com.panda3ds.pandroid.view.controller.mapping;
public enum ControllerItem {
START,
SELECT,
L,R,
GAMEPAD,
DPAD, JOYSTICK
}

View file

@ -1,4 +1,4 @@
package com.panda3ds.pandroid.view.controller.map; package com.panda3ds.pandroid.view.controller.mapping;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
@ -28,7 +28,7 @@ public class ControllerMapper extends FrameLayout {
private final Paint selectionPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private final Paint selectionPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int width = -1; private int width = -1;
private int height = -1; private int height = -1;
private Function<NodeID> changeListener; private Function<ControllerItem> changeListener;
public ControllerMapper(@NonNull Context context) { public ControllerMapper(@NonNull Context context) {
this(context, null); this(context, null);
@ -53,19 +53,19 @@ public class ControllerMapper extends FrameLayout {
selectionPaint.setPathEffect(new DashPathEffect(new float[]{dp * 10, dp * 10}, 0.0f)); 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.profile = profile;
this.changeListener = changeListener; this.changeListener = changeListener;
measure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY); measure(MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);
new MoveElementListener(NodeID.L, findViewById(R.id.button_l)); new MoveElementListener(ControllerItem.L, findViewById(R.id.button_l));
new MoveElementListener(NodeID.R, findViewById(R.id.button_r)); new MoveElementListener(ControllerItem.R, findViewById(R.id.button_r));
new MoveElementListener(NodeID.START, findViewById(R.id.button_start)); new MoveElementListener(ControllerItem.START, findViewById(R.id.button_start));
new MoveElementListener(NodeID.SELECT, findViewById(R.id.button_select)); new MoveElementListener(ControllerItem.SELECT, findViewById(R.id.button_select));
new MoveElementListener(NodeID.DPAD, findViewById(R.id.dpad)); new MoveElementListener(ControllerItem.DPAD, findViewById(R.id.dpad));
new MoveElementListener(NodeID.GAMEPAD, findViewById(R.id.gamepad)); new MoveElementListener(ControllerItem.GAMEPAD, findViewById(R.id.gamepad));
new MoveElementListener(NodeID.JOYSTICK, findViewById(R.id.left_analog)); new MoveElementListener(ControllerItem.JOYSTICK, findViewById(R.id.left_analog));
} }
@Override @Override
@ -134,13 +134,13 @@ public class ControllerMapper extends FrameLayout {
public void refreshLayout() { public void refreshLayout() {
if (profile != null) { if (profile != null) {
profile.applyToView(NodeID.L, findViewById(R.id.button_l), width, height); profile.applyToView(ControllerItem.L, findViewById(R.id.button_l), width, height);
profile.applyToView(NodeID.R, findViewById(R.id.button_r), width, height); profile.applyToView(ControllerItem.R, findViewById(R.id.button_r), width, height);
profile.applyToView(NodeID.START, findViewById(R.id.button_start), width, height); profile.applyToView(ControllerItem.START, findViewById(R.id.button_start), width, height);
profile.applyToView(NodeID.SELECT, findViewById(R.id.button_select), width, height); profile.applyToView(ControllerItem.SELECT, findViewById(R.id.button_select), width, height);
profile.applyToView(NodeID.DPAD, findViewById(R.id.dpad), width, height); profile.applyToView(ControllerItem.DPAD, findViewById(R.id.dpad), width, height);
profile.applyToView(NodeID.GAMEPAD, findViewById(R.id.gamepad), width, height); profile.applyToView(ControllerItem.GAMEPAD, findViewById(R.id.gamepad), width, height);
profile.applyToView(NodeID.JOYSTICK, findViewById(R.id.left_analog), width, height); profile.applyToView(ControllerItem.JOYSTICK, findViewById(R.id.left_analog), width, height);
} }
} }
@ -153,12 +153,12 @@ public class ControllerMapper extends FrameLayout {
} }
public class MoveElementListener implements OnTouchListener { public class MoveElementListener implements OnTouchListener {
private final NodeID id; private final ControllerItem id;
private final View view; private final View view;
private final Vector2 downPosition = new Vector2(0.0f, 0.0f); private final Vector2 downPosition = new Vector2(0.0f, 0.0f);
private boolean down = false; private boolean down = false;
public MoveElementListener(NodeID id, View view) { public MoveElementListener(ControllerItem id, View view) {
this.view = view; this.view = view;
this.id = id; this.id = id;
this.view.setOnTouchListener(this); this.view.setOnTouchListener(this);
@ -181,7 +181,7 @@ public class ControllerMapper extends FrameLayout {
profile.setLocation(id, x, y, width, height); profile.setLocation(id, x, y, width, height);
profile.applyToView(id, view, width, height); profile.applyToView(id, view, width, height);
if(changeListener != null){ if (changeListener != null) {
changeListener.run(id); changeListener.run(id);
} }

View file

@ -1,4 +1,4 @@
package com.panda3ds.pandroid.view.controller.map; package com.panda3ds.pandroid.view.controller.mapping;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.view.Gravity; import android.view.Gravity;
@ -19,35 +19,35 @@ public class ControllerProfileManager {
private static final DataModel data; private static final DataModel data;
static { static {
parser = new GsonConfigParser(Constants.PREF_SCREEN_CONTROLLER_PROFILES); parser = new GsonConfigParser(Constants.PREF_SCREEN_CONTROLLER_PROFILES);
data = parser.load(DataModel.class); data = parser.load(DataModel.class);
if (data.profiles.size() == 0){ if (data.profiles.size() == 0) {
add(makeDefaultProfile()); add(makeDefaultProfile());
} }
} }
public static void remove(String id){ public static void remove(String id) {
data.profiles.remove(id); data.profiles.remove(id);
save(); save();
} }
public static void add(Profile profile){ public static void add(Profile profile) {
data.profiles.put(profile.getId(), profile); data.profiles.put(profile.getId(), profile);
save(); save();
} }
public static List<Profile> listAll(){ public static List<Profile> listAll() {
return new ArrayList<>(data.profiles.values()); return new ArrayList<>(data.profiles.values());
} }
public static int getProfileCount(){ public static int getProfileCount() {
return data.profiles.size(); return data.profiles.size();
} }
public static Profile getDefaultProfile(){ public static Profile getDefaultProfile() {
if (data.profiles.containsKey(data.profileId)){ if (data.profiles.containsKey(data.profileId)) {
return data.profiles.get(data.profileId); return data.profiles.get(data.profileId);
} else if (getProfileCount() > 0){ } else if (getProfileCount() > 0) {
data.profileId = data.profiles.keySet().iterator().next(); data.profileId = data.profiles.keySet().iterator().next();
save(); save();
return getDefaultProfile(); return getDefaultProfile();
@ -57,36 +57,40 @@ public class ControllerProfileManager {
} }
} }
private static void save(){ private static void save() {
if ((!data.profiles.containsKey(data.profileId)) && getProfileCount() > 0){ if ((!data.profiles.containsKey(data.profileId)) && getProfileCount() > 0) {
data.profileId = data.profiles.keySet().iterator().next(); data.profileId = data.profiles.keySet().iterator().next();
} }
parser.save(data); parser.save(data);
} }
@SuppressLint("RtlHardcoded")
public static Profile makeDefaultProfile() { public static Profile makeDefaultProfile() {
return new Profile(UUID.randomUUID().toString(), "Default", createDefaultLayout(), createDefaultLayout());
}
@SuppressLint("RtlHardcoded")
public static Layout createDefaultLayout() {
Layout layout = new Layout(); Layout layout = new Layout();
layout.setLocation(NodeID.L, new Location(39, 145, Gravity.LEFT, true)); layout.setLocation(ControllerItem.L, new Location(39, 145, Gravity.LEFT, true));
layout.setLocation(NodeID.R, new Location(39, 145, Gravity.RIGHT, true)); layout.setLocation(ControllerItem.R, new Location(39, 145, Gravity.RIGHT, true));
layout.setLocation(NodeID.SELECT, new Location(32, 131, Gravity.LEFT, true)); layout.setLocation(ControllerItem.SELECT, new Location(32, 131, Gravity.LEFT, true));
layout.setLocation(NodeID.START, new Location(32, 131, Gravity.RIGHT, true)); layout.setLocation(ControllerItem.START, new Location(32, 131, Gravity.RIGHT, true));
layout.setLocation(NodeID.DPAD, new Location(42, 90, Gravity.LEFT, true)); layout.setLocation(ControllerItem.DPAD, new Location(42, 90, Gravity.LEFT, true));
layout.setLocation(NodeID.JOYSTICK, new Location(74, 45, Gravity.LEFT, true)); layout.setLocation(ControllerItem.JOYSTICK, new Location(74, 45, Gravity.LEFT, true));
layout.setLocation(NodeID.GAMEPAD, new Location(42, 75, Gravity.RIGHT, true)); layout.setLocation(ControllerItem.GAMEPAD, new Location(42, 75, Gravity.RIGHT, true));
return new Profile(UUID.randomUUID().toString(),"Default",layout, layout); return layout;
} }
public static Profile get(String profile) { public static Profile get(String profile) {
return data.profiles.getOrDefault(profile, null); return data.profiles.getOrDefault(profile, null);
} }
public static void setDefaultId(String id) { public static void setDefaultProfileId(String id) {
if (data.profiles.containsKey(id) && !Objects.equals(id, data.profileId)){ if (data.profiles.containsKey(id) && !Objects.equals(id, data.profileId)) {
data.profileId = id; data.profileId = id;
save(); save();
} }

View file

@ -0,0 +1,36 @@
package com.panda3ds.pandroid.view.controller.mapping;
import androidx.annotation.NonNull;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class Layout {
private final Map<ControllerItem, Location> mapLocations = new HashMap<>();
public void setLocation(ControllerItem item, Location location) {
mapLocations.put(item, location);
}
@NotNull
public Location getLocation(ControllerItem item) {
if (!mapLocations.containsKey(item)) {
setLocation(item, new Location());
}
return Objects.requireNonNull(mapLocations.get(item));
}
@NonNull
@Override
public Layout clone() {
Layout cloned = new Layout();
for (ControllerItem key : mapLocations.keySet()) {
cloned.setLocation(key, getLocation(key).clone());
}
return cloned;
}
}

View file

@ -1,4 +1,4 @@
package com.panda3ds.pandroid.view.controller.map; package com.panda3ds.pandroid.view.controller.mapping;
import android.view.Gravity; import android.view.Gravity;

View file

@ -1,4 +1,4 @@
package com.panda3ds.pandroid.view.controller.map; package com.panda3ds.pandroid.view.controller.mapping;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
@ -17,10 +17,6 @@ public class Profile {
private final Layout portraitLayout; private final Layout portraitLayout;
private String name; private String name;
public Profile() {
this(UUID.randomUUID().toString(), PandroidApplication.getAppContext().getString(R.string.unknown), new Layout(), new Layout());
}
public Profile(String id, String name, Layout landscape, Layout portrait) { public Profile(String id, String name, Layout landscape, Layout portrait) {
this.id = id; this.id = id;
this.name = name; this.name = name;
@ -28,7 +24,7 @@ public class Profile {
this.portraitLayout = portrait; this.portraitLayout = portrait;
} }
public void applyToView(NodeID id, View view, int viewportWidth, int viewportHeight) { public void applyToView(ControllerItem id, View view, int viewportWidth, int viewportHeight) {
float pt = view.getResources().getDimension(R.dimen.SizePt); float pt = view.getResources().getDimension(R.dimen.SizePt);
int width = view.getLayoutParams().width; int width = view.getLayoutParams().width;
@ -43,7 +39,7 @@ public class Profile {
int y = Math.round(location.getY() * pt); int y = Math.round(location.getY() * pt);
params.gravity = location.getGravity() | Gravity.BOTTOM; params.gravity = location.getGravity() | Gravity.BOTTOM;
params.bottomMargin = Math.max(Math.min(y - (height / 2), viewportHeight-height), 0); params.bottomMargin = Math.max(Math.min(y - (height / 2), viewportHeight - height), 0);
int gravity = location.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK; int gravity = location.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK;
if (gravity == Gravity.RIGHT) { if (gravity == Gravity.RIGHT) {
@ -56,11 +52,11 @@ public class Profile {
view.setLayoutParams(params); view.setLayoutParams(params);
} }
public void setLocation(NodeID id, int x, int y, int viewportWidth, int viewportHeight) { public void setLocation(ControllerItem item, int x, int y, int viewportWidth, int viewportHeight) {
float pt = PandroidApplication.getAppContext().getResources().getDimension(R.dimen.SizePt); float pt = PandroidApplication.getAppContext().getResources().getDimension(R.dimen.SizePt);
Layout layout = getLayoutBySize(viewportWidth, viewportHeight); Layout layout = getLayoutBySize(viewportWidth, viewportHeight);
Location location = layout.getLocation(id); Location location = layout.getLocation(item);
y = viewportHeight - y; y = viewportHeight - y;
@ -68,7 +64,7 @@ public class Profile {
location.setGravity(Gravity.LEFT); location.setGravity(Gravity.LEFT);
location.setPosition(x / pt, y / pt); location.setPosition(x / pt, y / pt);
} else { } else {
x = (viewportWidth/2) - (x - (viewportWidth / 2)); x = (viewportWidth / 2) - (x - (viewportWidth / 2));
location.setGravity(Gravity.RIGHT); location.setGravity(Gravity.RIGHT);
location.setPosition(x / pt, y / pt); location.setPosition(x / pt, y / pt);
} }
@ -79,10 +75,11 @@ public class Profile {
this.name = name; this.name = name;
} }
public void setVisible(NodeID id, boolean visible) { public void setVisible(ControllerItem id, boolean visible) {
landscapeLayout.getLocation(id).setVisible(visible); landscapeLayout.getLocation(id).setVisible(visible);
portraitLayout.getLocation(id).setVisible(visible); portraitLayout.getLocation(id).setVisible(visible);
} }
private Layout getLayoutBySize(int width, int height) { private Layout getLayoutBySize(int width, int height) {
return width > height ? landscapeLayout : portraitLayout; return width > height ? landscapeLayout : portraitLayout;
} }
@ -101,7 +98,7 @@ public class Profile {
return new Profile(id, name, landscapeLayout.clone(), portraitLayout.clone()); return new Profile(id, name, landscapeLayout.clone(), portraitLayout.clone());
} }
public boolean isVisible(NodeID id) { public boolean isVisible(ControllerItem id) {
return landscapeLayout.getLocation(id).isVisible(); return landscapeLayout.getLocation(id).isVisible();
} }
} }

View file

@ -5,7 +5,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<com.panda3ds.pandroid.view.controller.map.ControllerMapper <com.panda3ds.pandroid.view.controller.mapping.ControllerMapper
android:id="@+id/mapper" android:id="@+id/mapper"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
@ -26,7 +26,7 @@
</FrameLayout> </FrameLayout>
</com.panda3ds.pandroid.view.controller.map.ControllerMapper> </com.panda3ds.pandroid.view.controller.mapping.ControllerMapper>
<androidx.appcompat.widget.LinearLayoutCompat <androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content" android:layout_width="wrap_content"

View file

@ -15,12 +15,12 @@
android:summary="-"/> android:summary="-"/>
<PreferenceCategory <PreferenceCategory
android:key="screenGamepadProfiles" android:key="gamepadProfileList"
android:title="@string/pref_screen_controllers_title" android:title="@string/pref_screen_controllers_title"
app:iconSpaceReserved="false"> app:iconSpaceReserved="false">
<Preference <Preference
android:key="add_screen_profile" android:key="createProfile"
app:icon="@drawable/ic_add" app:icon="@drawable/ic_add"
app:title="@string/create_profile" app:title="@string/create_profile"
app:allowDividerBelow="true" app:allowDividerBelow="true"