Skip to content

Commit

Permalink
Rework on Mapper of Physical Controllers (Now it's more user friendly)
Browse files Browse the repository at this point in the history
  • Loading branch information
KreitinnSoftware committed Jan 7, 2025
1 parent 1dbaf99 commit 2a473ce
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 163 deletions.
38 changes: 16 additions & 22 deletions app/src/main/java/com/micewine/emu/activities/ControllerMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
Expand All @@ -67,7 +67,7 @@ class ControllerMapper : AppCompatActivity() {
binding = ActivityControllerMapperBinding.inflate(layoutInflater)
setContentView(binding!!.root)

fragmentLoader(ControllerMapperFragment(), true)
fragmentLoader(ControllerMapperFragment())

findViewById<Toolbar>(R.id.controllerMapperToolbar).title = getString(R.string.controller_mapper_title)

Expand Down Expand Up @@ -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<*>?) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,8 +20,8 @@ class AdapterSettingsController(private val settingsControllerList: List<Setting
RecyclerView.Adapter<AdapterSettingsController.ViewHolder>() {

private val preferences = PreferenceManager.getDefaultSharedPreferences(context)!!
private val keyboardEntries: List<String> = XKeyCodes.getKeyNames()
private val mouseEntries: Array<String> = arrayOf("Null", "Left", "Middle", "Right")
private val allEntries: List<String> = XKeyCodes.getKeyNames(true)
private val keyEntries: List<String> = 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)
Expand All @@ -35,41 +33,18 @@ class AdapterSettingsController(private val settingsControllerList: List<Setting

holder.image.setImageResource(sList.image)

var mapping = getMapping(context, preferences.getString(ControllerMapper.SELECTED_CONTROLLER_PRESET_KEY, "default")!!, sList.key)
val mapping = getMapping(context, preferences.getString(ControllerMapper.SELECTED_CONTROLLER_PRESET_KEY, "default")!!, sList.key)

holder.mappingType.isChecked = mapping[1].toBoolean()

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]))
}

holder.mappingType.setOnClickListener {
val selectedItem = holder.keyBindSpinner.selectedItem.toString()

val mappingType = if (holder.mappingType.isChecked) {
"true"
} else {
""
when (sList.image) {
R.drawable.l_up, R.drawable.l_down, R.drawable.l_left, R.drawable.l_right,
R.drawable.r_up, R.drawable.r_down, R.drawable.r_left, R.drawable.r_right -> {
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]))
}
}

Expand All @@ -81,15 +56,7 @@ class AdapterSettingsController(private val settingsControllerList: List<Setting
position: Int,
id: Long
) {
val selectedItem = parent?.selectedItem.toString()

val mappingType = if (holder.mappingType.isChecked) {
"true"
} else {
""
}

editControllerPreset(context, preferences.getString(ControllerMapper.SELECTED_CONTROLLER_PRESET_KEY, "default")!!, sList.key, selectedItem, mappingType)
editControllerPreset(context, preferences.getString(ControllerMapper.SELECTED_CONTROLLER_PRESET_KEY, "default")!!, sList.key, parent?.selectedItem.toString())
}

override fun onNothingSelected(parent: AdapterView<*>?) {
Expand All @@ -104,8 +71,6 @@ class AdapterSettingsController(private val settingsControllerList: List<Setting
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
val keyBindSpinner: Spinner = itemView.findViewById(R.id.keyBindSpinner)
val image: ImageView = itemView.findViewById(R.id.buttonImageView)
val mappingType: SwitchCompat = itemView.findViewById(R.id.mappingType)
val mappingTypeText: TextView = itemView.findViewById(R.id.mappingTypeText)

init {
itemView.setOnClickListener(this)
Expand Down
38 changes: 22 additions & 16 deletions app/src/main/java/com/micewine/emu/controller/ControllerUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import android.view.MotionEvent.AXIS_RZ
import android.view.MotionEvent.AXIS_X
import android.view.MotionEvent.AXIS_Y
import android.view.MotionEvent.AXIS_Z
import android.view.PointerIcon
import androidx.drawerlayout.widget.DrawerLayout
import androidx.preference.PreferenceManager
import com.micewine.emu.LorieView
import com.micewine.emu.activities.ControllerMapper
Expand Down Expand Up @@ -50,7 +48,6 @@ import com.micewine.emu.activities.ControllerMapper.Companion.BUTTON_Y_KEY
import com.micewine.emu.activities.ControllerMapper.Companion.SELECTED_CONTROLLER_PRESET_KEY
import com.micewine.emu.activities.ControllerMapper.Companion.getDeadZone
import com.micewine.emu.activities.ControllerMapper.Companion.getMouseSensibility
import com.micewine.emu.activities.EmulationActivity
import com.micewine.emu.controller.XKeyCodes.getXKeyScanCodes
import com.micewine.emu.input.InputStub.BUTTON_LEFT
import com.micewine.emu.input.InputStub.BUTTON_MIDDLE
Expand Down Expand Up @@ -102,26 +99,35 @@ object ControllerUtils {
private const val RIGHT_UP = 7
private const val RIGHT_DOWN = 8

private fun detectKey(context: Context, key: String): MutableList<Int> {
private fun detectKey(context: Context, key: String): List<Int> {
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<Int>

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) {
Expand Down Expand Up @@ -196,7 +202,7 @@ object ControllerUtils {
private fun handleKey(lorieView: LorieView, pressed: Boolean, mapping: List<Int>) {
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)
}
}

Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/com/micewine/emu/controller/XKeyCodes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,27 @@ object XKeyCodes {
"9" to (73 to 153),
)

fun getKeyNames(): MutableList<String> {
fun getKeyNames(getMouseButtons: Boolean): List<String> {
val keyNames: MutableList<String> = 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)
}

return keyNames
}

fun getXKeyScanCodes(key: String): MutableList<Int> {
fun getXKeyScanCodes(key: String): List<Int> {
val scanCode = scanKeyCodes[key]?.first ?: 0
val keyCode = scanKeyCodes[key]?.second ?: 0
return mutableListOf(scanCode, keyCode, KEYBOARD)
return listOf(scanCode, keyCode, KEYBOARD)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<SettingsController> = ArrayList()
Expand All @@ -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()))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,27 @@ class EditVirtualButtonFragment : DialogFragment() {
})
}

val allKeyNames = getKeyNames(true)

val buttonSpinner = view.findViewById<Spinner>(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<Spinner>(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<Spinner>(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<Spinner>(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<Spinner>(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
Expand Down
Loading

0 comments on commit 2a473ce

Please sign in to comment.