From 2a473cea9b3553bab9a3167b6e805468109bfb5a Mon Sep 17 00:00:00 2001 From: KreitinnSoftware Date: Mon, 6 Jan 2025 21:20:20 -0300 Subject: [PATCH] Rework on Mapper of Physical Controllers (Now it's more user friendly) --- .../emu/activities/ControllerMapper.kt | 38 +++++----- .../emu/adapters/AdapterSettingsController.kt | 59 ++++----------- .../emu/controller/ControllerUtils.kt | 38 +++++----- .../com/micewine/emu/controller/XKeyCodes.kt | 14 +++- .../emu/fragments/ControllerMapperFragment.kt | 12 ---- .../fragments/EditVirtualButtonFragment.kt | 22 +++--- .../adapter_settings_controller_item.xml | 72 +++++-------------- 7 files changed, 92 insertions(+), 163 deletions(-) diff --git a/app/src/main/java/com/micewine/emu/activities/ControllerMapper.kt b/app/src/main/java/com/micewine/emu/activities/ControllerMapper.kt index 2c4482b..76fdef3 100644 --- a/app/src/main/java/com/micewine/emu/activities/ControllerMapper.kt +++ b/app/src/main/java/com/micewine/emu/activities/ControllerMapper.kt @@ -53,7 +53,7 @@ class ControllerMapper : AppCompatActivity() { selectedControllerPresetSpinner?.adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, getControllerPresetsName(context)) selectedControllerPresetSpinner?.setSelection(getControllerPresetsName(context).indexOf(name)) - fragmentLoader(ControllerMapperFragment(), true) + fragmentLoader(ControllerMapperFragment()) } } } @@ -67,7 +67,7 @@ class ControllerMapper : AppCompatActivity() { binding = ActivityControllerMapperBinding.inflate(layoutInflater) setContentView(binding!!.root) - fragmentLoader(ControllerMapperFragment(), true) + fragmentLoader(ControllerMapperFragment()) findViewById(R.id.controllerMapperToolbar).title = getString(R.string.controller_mapper_title) @@ -147,7 +147,7 @@ class ControllerMapper : AppCompatActivity() { putString(SELECTED_CONTROLLER_PRESET_KEY, parent?.selectedItem.toString()) apply() } - fragmentLoader(ControllerMapperFragment(), true) + fragmentLoader(ControllerMapperFragment()) } override fun onNothingSelected(parent: AdapterView<*>?) { @@ -176,16 +176,11 @@ class ControllerMapper : AppCompatActivity() { return super.onOptionsItemSelected(item) } - private fun fragmentLoader(fragment: Fragment, appInit: Boolean) { - val fragmentTransaction = supportFragmentManager.beginTransaction() - - fragmentTransaction.replace(R.id.controller_mapper_content, fragment) - - if (!appInit) { - fragmentTransaction.addToBackStack(null) + private fun fragmentLoader(fragment: Fragment) { + supportFragmentManager.beginTransaction().apply { + replace(R.id.controller_mapper_content, fragment) + commit() } - - fragmentTransaction.commit() } override fun onDestroy() { @@ -248,11 +243,7 @@ class ControllerMapper : AppCompatActivity() { ) fun putDeadZone(context: Context, name: String, value: Int) { - val preferences = PreferenceManager.getDefaultSharedPreferences(context) - val editor = preferences.edit() - val currentList = loadControllerPresets(context) - var index = currentList.indexOfFirst { it[0] == name } if (index == -1) { @@ -263,11 +254,14 @@ class ControllerMapper : AppCompatActivity() { currentList[index][mappingMap[DEAD_ZONE]!!] = "$value" - val gson = Gson() - val json = gson.toJson(currentList) + val json = Gson().toJson(currentList) - editor.putString("controllerPresetList", json) - editor.apply() + PreferenceManager.getDefaultSharedPreferences(context).apply { + edit().apply { + putString("controllerPresetList", json) + apply() + } + } } fun putMouseSensibility(context: Context, name: String, value: Int) { @@ -383,7 +377,7 @@ class ControllerMapper : AppCompatActivity() { } } - fun editControllerPreset(context: Context, name: String, key: String, selectedItem: String, mappingType: String) { + fun editControllerPreset(context: Context, name: String, key: String, selectedItem: String) { val preferences = PreferenceManager.getDefaultSharedPreferences(context) val currentList = loadControllerPresets(context) @@ -394,7 +388,7 @@ class ControllerMapper : AppCompatActivity() { index = 0 } - currentList[index][mappingMap[key]!!] = "$selectedItem:$mappingType" + currentList[index][mappingMap[key]!!] = selectedItem val json = Gson().toJson(currentList) diff --git a/app/src/main/java/com/micewine/emu/adapters/AdapterSettingsController.kt b/app/src/main/java/com/micewine/emu/adapters/AdapterSettingsController.kt index dc859e9..ec08148 100644 --- a/app/src/main/java/com/micewine/emu/adapters/AdapterSettingsController.kt +++ b/app/src/main/java/com/micewine/emu/adapters/AdapterSettingsController.kt @@ -8,8 +8,6 @@ import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.ImageView import android.widget.Spinner -import android.widget.TextView -import androidx.appcompat.widget.SwitchCompat import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView import com.micewine.emu.R @@ -22,8 +20,8 @@ class AdapterSettingsController(private val settingsControllerList: List() { private val preferences = PreferenceManager.getDefaultSharedPreferences(context)!! - private val keyboardEntries: List = XKeyCodes.getKeyNames() - private val mouseEntries: Array = arrayOf("Null", "Left", "Middle", "Right") + private val allEntries: List = XKeyCodes.getKeyNames(true) + private val keyEntries: List = XKeyCodes.getKeyNames(false) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val itemView = LayoutInflater.from(parent.context).inflate(R.layout.adapter_settings_controller_item, parent, false) @@ -35,41 +33,18 @@ class AdapterSettingsController(private val settingsControllerList: List { + holder.keyBindSpinner.adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, keyEntries) + holder.keyBindSpinner.setSelection(keyEntries.indexOf(mapping[0])) } - editControllerPreset(context, preferences.getString(ControllerMapper.SELECTED_CONTROLLER_PRESET_KEY, "default")!!, sList.key, selectedItem, mappingType) - - mapping = getMapping(context, preferences.getString(ControllerMapper.SELECTED_CONTROLLER_PRESET_KEY, "default")!!, sList.key) - - if (mapping[1].toBoolean()) { - holder.mappingTypeText.text = context.getString(R.string.mouse_text) - holder.keyBindSpinner.adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, mouseEntries) - holder.keyBindSpinner.setSelection(mouseEntries.indexOf(mapping[0])) - } else { - holder.mappingTypeText.text = context.getString(R.string.keyboard_text) - holder.keyBindSpinner.adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, keyboardEntries) - holder.keyBindSpinner.setSelection(keyboardEntries.indexOf(mapping[0])) + else -> { + holder.keyBindSpinner.adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, allEntries) + holder.keyBindSpinner.setSelection(allEntries.indexOf(mapping[0])) } } @@ -81,15 +56,7 @@ class AdapterSettingsController(private val settingsControllerList: List?) { @@ -104,8 +71,6 @@ class AdapterSettingsController(private val settingsControllerList: List { + private fun detectKey(context: Context, key: String): List { val preferences = PreferenceManager.getDefaultSharedPreferences(context)!! val mapping = ControllerMapper.getMapping(context, preferences.getString(SELECTED_CONTROLLER_PRESET_KEY, "default")!!, key) - val list = getXKeyScanCodes(mapping[0]) - when (mapping[1].toBoolean()) { - false -> list[2] = KEYBOARD + val keyList: List - true -> { - when (mapping[0]) { - "Left" -> list[1] = BUTTON_LEFT - "Right" -> list[1] = BUTTON_RIGHT - "Middle" -> list[1] = BUTTON_MIDDLE - } + when (mapping[0]) { + "M_Left" -> { + keyList = listOf(BUTTON_LEFT, BUTTON_LEFT, MOUSE) + } - list[2] = MOUSE + "M_Middle" -> { + keyList = listOf(BUTTON_MIDDLE, BUTTON_MIDDLE, MOUSE) + } + + "M_Right" -> { + keyList = listOf(BUTTON_RIGHT, BUTTON_RIGHT, MOUSE) + } + + "Mouse" -> { + keyList = listOf(MOUSE, MOUSE, MOUSE) + } + + else -> { + keyList = getXKeyScanCodes(mapping[0]) } } - return list + return keyList } fun prepareButtonsAxisValues(context: Context) { @@ -196,7 +202,7 @@ object ControllerUtils { private fun handleKey(lorieView: LorieView, pressed: Boolean, mapping: List) { when (mapping[2]) { KEYBOARD -> lorieView.sendKeyEvent(mapping[0], mapping[1], pressed) - MOUSE -> lorieView.sendMouseEvent(0F, 0F, mapping[1], pressed, true) + MOUSE -> lorieView.sendMouseEvent(0F, 0F, mapping[0], pressed, true) } } diff --git a/app/src/main/java/com/micewine/emu/controller/XKeyCodes.kt b/app/src/main/java/com/micewine/emu/controller/XKeyCodes.kt index 13a5eaf..7716e7b 100644 --- a/app/src/main/java/com/micewine/emu/controller/XKeyCodes.kt +++ b/app/src/main/java/com/micewine/emu/controller/XKeyCodes.kt @@ -75,9 +75,17 @@ object XKeyCodes { "9" to (73 to 153), ) - fun getKeyNames(): MutableList { + fun getKeyNames(getMouseButtons: Boolean): List { val keyNames: MutableList = mutableListOf("Null") + if (getMouseButtons) { + keyNames.plusAssign("M_Left") + keyNames.plusAssign("M_Middle") + keyNames.plusAssign("M_Right") + } else { + keyNames.plusAssign("Mouse") + } + for (i in scanKeyCodes.keys) { keyNames.plusAssign(i) } @@ -85,9 +93,9 @@ object XKeyCodes { return keyNames } - fun getXKeyScanCodes(key: String): MutableList { + fun getXKeyScanCodes(key: String): List { val scanCode = scanKeyCodes[key]?.first ?: 0 val keyCode = scanKeyCodes[key]?.second ?: 0 - return mutableListOf(scanCode, keyCode, KEYBOARD) + return listOf(scanCode, keyCode, KEYBOARD) } } diff --git a/app/src/main/java/com/micewine/emu/fragments/ControllerMapperFragment.kt b/app/src/main/java/com/micewine/emu/fragments/ControllerMapperFragment.kt index 022d0c8..458ae93 100644 --- a/app/src/main/java/com/micewine/emu/fragments/ControllerMapperFragment.kt +++ b/app/src/main/java/com/micewine/emu/fragments/ControllerMapperFragment.kt @@ -1,6 +1,5 @@ package com.micewine.emu.fragments -import android.content.Context import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -33,7 +32,6 @@ import com.micewine.emu.activities.ControllerMapper.Companion.BUTTON_X_KEY import com.micewine.emu.activities.ControllerMapper.Companion.BUTTON_Y_KEY import com.micewine.emu.adapters.AdapterSettingsController import com.micewine.emu.adapters.AdapterSettingsController.SettingsController -import kotlin.math.max class ControllerMapperFragment : Fragment() { private val settingsList: MutableList = ArrayList() @@ -53,19 +51,9 @@ class ControllerMapperFragment : Fragment() { layoutManager = recyclerView?.layoutManager as GridLayoutManager? - val spanCount = max(1F,requireActivity().resources.displayMetrics.widthPixels / dpToPx(150, requireContext())).toInt() - - recyclerView?.layoutManager = GridLayoutManager(requireContext(), spanCount) - recyclerView?.addItemDecoration(HomeFragment.GridSpacingItemDecoration(spanCount, 40)) - return rootView } - private fun dpToPx(dp: Int, context: Context): Float { - val density = context.resources.displayMetrics.density - return dp * density - } - private fun setAdapter() { recyclerView?.setAdapter(AdapterSettingsController(settingsList, requireContext())) diff --git a/app/src/main/java/com/micewine/emu/fragments/EditVirtualButtonFragment.kt b/app/src/main/java/com/micewine/emu/fragments/EditVirtualButtonFragment.kt index 7ff1077..fe1f217 100644 --- a/app/src/main/java/com/micewine/emu/fragments/EditVirtualButtonFragment.kt +++ b/app/src/main/java/com/micewine/emu/fragments/EditVirtualButtonFragment.kt @@ -53,25 +53,27 @@ class EditVirtualButtonFragment : DialogFragment() { }) } + val allKeyNames = getKeyNames(true) + val buttonSpinner = view.findViewById(R.id.buttonSpinner).apply { - adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, getKeyNames()) - setSelection(getKeyNames().indexOf(selectedButtonKeyName)) + adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, allKeyNames) + setSelection(allKeyNames.indexOf(selectedButtonKeyName)) } val analogUpKeySpinner = view.findViewById(R.id.analogUpKeySpinner).apply { - adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, getKeyNames()) - setSelection(getKeyNames().indexOf(selectedAnalogUpKeyName)) + adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, allKeyNames) + setSelection(allKeyNames.indexOf(selectedAnalogUpKeyName)) } val analogDownKeySpinner = view.findViewById(R.id.analogDownKeySpinner).apply { - adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, getKeyNames()) - setSelection(getKeyNames().indexOf(selectedAnalogDownKeyName)) + adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, allKeyNames) + setSelection(allKeyNames.indexOf(selectedAnalogDownKeyName)) } val analogLeftKeySpinner = view.findViewById(R.id.analogLeftKeySpinner).apply { - adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, getKeyNames()) - setSelection(getKeyNames().indexOf(selectedAnalogLeftKeyName)) + adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, allKeyNames) + setSelection(allKeyNames.indexOf(selectedAnalogLeftKeyName)) } val analogRightKeySpinner = view.findViewById(R.id.analogRightKeySpinner).apply { - adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, getKeyNames()) - setSelection(getKeyNames().indexOf(selectedAnalogRightKeyName)) + adapter = ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, allKeyNames) + setSelection(allKeyNames.indexOf(selectedAnalogRightKeyName)) } radiusSeekbar.progress = selectedButtonRadius diff --git a/app/src/main/res/layout/adapter_settings_controller_item.xml b/app/src/main/res/layout/adapter_settings_controller_item.xml index 6a6cc84..dfcd674 100644 --- a/app/src/main/res/layout/adapter_settings_controller_item.xml +++ b/app/src/main/res/layout/adapter_settings_controller_item.xml @@ -1,7 +1,6 @@ - - - - - - - - - - - - - - - - - + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:padding="8dp" + android:orientation="horizontal"> + + + + android:layout_height="match_parent" + android:layout_gravity="center" + android:dropDownWidth="wrap_content">