Skip to content

Commit

Permalink
Some Code Improvement (Maybe just visual), Fix Mouse Buttons for Virt…
Browse files Browse the repository at this point in the history
…ual Controller
  • Loading branch information
KreitinnSoftware committed Jan 7, 2025
1 parent 2a473ce commit 390b072
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 90 deletions.
49 changes: 23 additions & 26 deletions app/src/main/java/com/micewine/emu/activities/ControllerMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class ControllerMapper : AppCompatActivity() {
private var deletePresetButton: ImageButton? = null
private var preferences: SharedPreferences? = null
private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
@SuppressLint("UnspecifiedRegisterReceiverFlag")
override fun onReceive(context: Context, intent: Intent) {
if (ACTION_UPDATE_CONTROLLER_MAPPER == intent.action) {
val name = intent.getStringExtra("name")
Expand Down Expand Up @@ -215,6 +214,8 @@ class ControllerMapper : AppCompatActivity() {
const val SELECTED_CONTROLLER_PRESET_KEY = "selectedControllerPreset"
const val ACTION_UPDATE_CONTROLLER_MAPPER = "com.micewine.emu.ACTION_UPDATE_CONTROLLER_MAPPER"

private val gson = Gson()

private val mappingMap = mapOf(
BUTTON_A_KEY to 1,
BUTTON_B_KEY to 2,
Expand Down Expand Up @@ -254,7 +255,7 @@ class ControllerMapper : AppCompatActivity() {

currentList[index][mappingMap[DEAD_ZONE]!!] = "$value"

val json = Gson().toJson(currentList)
val json = gson.toJson(currentList)

PreferenceManager.getDefaultSharedPreferences(context).apply {
edit().apply {
Expand All @@ -265,11 +266,7 @@ class ControllerMapper : AppCompatActivity() {
}

fun putMouseSensibility(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 @@ -280,16 +277,18 @@ class ControllerMapper : AppCompatActivity() {

currentList[index][mappingMap[MOUSE_SENSIBILITY]!!] = "$value"

val gson = Gson()
val json = gson.toJson(currentList)

editor.putString("controllerPresetList", json)
editor.apply()
PreferenceManager.getDefaultSharedPreferences(context).apply {
edit().apply {
putString("controllerPresetList", json)
apply()
}
}
}

fun getDeadZone(context: Context, name: String): Int {
val currentList = loadControllerPresets(context)

val index = currentList.indexOfFirst { it[0] == name }

if (index == -1) {
Expand All @@ -301,7 +300,6 @@ class ControllerMapper : AppCompatActivity() {

fun getMouseSensibility(context: Context, name: String): Int {
val currentList = loadControllerPresets(context)

val index = currentList.indexOfFirst { it[0] == name }

if (index == -1) {
Expand All @@ -313,7 +311,6 @@ class ControllerMapper : AppCompatActivity() {

fun getMapping(context: Context, name: String, key: String): List<String> {
val currentList = loadControllerPresets(context)

val index = currentList.indexOfFirst { it[0] == name }

if (index == -1) {
Expand All @@ -324,9 +321,6 @@ class ControllerMapper : AppCompatActivity() {
}

fun deleteControllerPreset(context: Context, name: String) {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val editor = preferences.edit()

val currentList = loadControllerPresets(context)

if (currentList.count() == 1) {
Expand All @@ -338,11 +332,14 @@ class ControllerMapper : AppCompatActivity() {

currentList.removeIf { it[0] == name }

val gson = Gson()
val json = gson.toJson(currentList)

editor.putString("controllerPresetList", json)
editor.apply()
PreferenceManager.getDefaultSharedPreferences(context).apply {
edit().apply {
putString("controllerPresetList", json)
apply()
}
}

val intent = Intent(ACTION_UPDATE_CONTROLLER_MAPPER)
intent.putExtra("name", "default")
Expand All @@ -360,20 +357,20 @@ class ControllerMapper : AppCompatActivity() {

currentList.add(defaultList)

val json = Gson().toJson(currentList)
val json = gson.toJson(currentList)

preferences.edit().apply {
putString("controllerPresetList", json)
putString(SELECTED_CONTROLLER_PRESET_KEY, name)
apply()
}

val intent = Intent(ACTION_UPDATE_CONTROLLER_MAPPER).apply {
putExtra("name", name)
}

context.apply {
sendBroadcast(intent)
sendBroadcast(
Intent(ACTION_UPDATE_CONTROLLER_MAPPER).apply {
putExtra("name", name)
}
)
}
}

Expand All @@ -390,7 +387,7 @@ class ControllerMapper : AppCompatActivity() {

currentList[index][mappingMap[key]!!] = selectedItem

val json = Gson().toJson(currentList)
val json = gson.toJson(currentList)

preferences.edit().apply {
putString("controllerPresetList", json)
Expand All @@ -403,7 +400,7 @@ class ControllerMapper : AppCompatActivity() {
val json = preferences.getString("controllerPresetList", "")
val listType = object : TypeToken<MutableList<List<String>>>() {}.type

return Gson().fromJson(json, listType) ?: mutableListOf(ArrayList(Collections.nCopies(25, ":")).apply {
return gson.fromJson(json, listType) ?: mutableListOf(ArrayList(Collections.nCopies(25, ":")).apply {
this[0] = "default"
this[mappingMap[DEAD_ZONE]!!] = "25"
this[mappingMap[MOUSE_SENSIBILITY]!!] = "100"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.micewine.emu.activities

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.KeyEvent
import android.widget.ImageButton
Expand All @@ -19,7 +18,6 @@ class DriverManagerActivity : AppCompatActivity() {
private var backButton: ImageButton? = null
private var ratManagerToolBar: Toolbar? = null

@SuppressLint("UnspecifiedRegisterReceiverFlag")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -34,7 +32,7 @@ class DriverManagerActivity : AppCompatActivity() {
ratManagerToolBar = findViewById(R.id.driverManagerToolbar)
ratManagerToolBar?.setTitle(R.string.driver_manager_title)

fragmentLoader(DriverListFragment(), true)
fragmentLoader(DriverListFragment())
}

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
Expand All @@ -50,16 +48,11 @@ class DriverManagerActivity : AppCompatActivity() {
setSharedVars(this)
}

private fun fragmentLoader(fragment: Fragment, appInit: Boolean) {
val fragmentTransaction = supportFragmentManager.beginTransaction()

fragmentTransaction.replace(R.id.rat_manager_content, fragment)

if (!appInit) {
fragmentTransaction.addToBackStack(null)
private fun fragmentLoader(fragment: Fragment) {
supportFragmentManager.beginTransaction().apply {
replace(R.id.rat_manager_content, fragment)
commit()
}

fragmentTransaction.commit()
}

companion object {
Expand Down
44 changes: 24 additions & 20 deletions app/src/main/java/com/micewine/emu/activities/EmulationActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
private var service: ICmdEntryInterface? = null
private var mClientConnected = false
private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
@SuppressLint("UnspecifiedRegisterReceiverFlag")
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
ACTION_START -> {
Expand Down Expand Up @@ -107,6 +106,7 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
private var mLorieKeyListener: View.OnKeyListener? = null
private var drawerLayout: DrawerLayout? = null
private var logsNavigationView: NavigationView? = null
private var overlayView: OverlayView? = null

init {
instance = this
Expand All @@ -117,9 +117,10 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
preferences.registerOnSharedPreferenceChangeListener { _: SharedPreferences?, _: String? ->
onPreferencesChanged()
val preferences = PreferenceManager.getDefaultSharedPreferences(this).apply {
registerOnSharedPreferenceChangeListener { _: SharedPreferences?, _: String? ->
onPreferencesChanged()
}
}

initSharedLogs(supportFragmentManager)
Expand All @@ -137,7 +138,10 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
}

prepareButtonsAxisValues(this)

val audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
val inputManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager

window.setFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 0)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setContentView(R.layout.activity_emulation)
Expand Down Expand Up @@ -179,16 +183,14 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
val lorieView = findViewById<LorieView>(R.id.lorieView)
val lorieParent = lorieView.parent as View

val overlayView: OverlayView = findViewById(R.id.overlayView)

overlayView.visibility = View.INVISIBLE
overlayView = findViewById(R.id.overlayView)
overlayView?.visibility = View.INVISIBLE

lifecycleScope.launch {
controllerMouseEmulation(lorieView)
}

val nav = findViewById<NavigationView>(R.id.NavigationView)
nav.setNavigationItemSelectedListener { item: MenuItem ->
findViewById<NavigationView>(R.id.NavigationView).setNavigationItemSelectedListener { item: MenuItem ->
when (item.itemId) {
R.id.exit -> {
drawerLayout?.closeDrawers()
Expand All @@ -207,7 +209,7 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
}

R.id.openCloseKeyboard -> {
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).apply {
inputManager.apply {
@Suppress("DEPRECATION")
toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0)
}
Expand All @@ -232,10 +234,10 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
}

R.id.openCloseOverlay -> {
if (overlayView.isVisible) {
overlayView.visibility = View.INVISIBLE
if (overlayView?.isVisible!!) {
overlayView?.visibility = View.INVISIBLE
} else {
overlayView.visibility = View.VISIBLE
overlayView?.visibility = View.VISIBLE
}

drawerLayout?.closeDrawers()
Expand Down Expand Up @@ -275,7 +277,7 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
drawerLayout?.closeDrawers()
}

(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).apply {
inputManager.apply {
hideSoftInputFromWindow(window.decorView.windowToken, 0)
}

Expand Down Expand Up @@ -317,10 +319,10 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
screenWidth: Int,
screenHeight: Int
) {
val framerate = (lorieView.display?.refreshRate ?: 30).toInt()
val frameRate = (lorieView.display?.refreshRate ?: 30).toInt()
mInputHandler?.handleHostSizeChanged(surfaceWidth, surfaceHeight)
mInputHandler?.handleClientSizeChanged(screenWidth, screenHeight)
LorieView.sendWindowChange(screenWidth, screenHeight, framerate)
LorieView.sendWindowChange(screenWidth, screenHeight, frameRate)
service?.let {
try {
it.windowChanged(sfc)
Expand All @@ -345,10 +347,10 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener

setSharedVars(this)

if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU && checkSelfPermission(permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED && !shouldShowRequestPermissionRationale(
permission.POST_NOTIFICATIONS
)
) {
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU &&
checkSelfPermission(permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED &&
!shouldShowRequestPermissionRationale(permission.POST_NOTIFICATIONS)
) {
requestPermissions(arrayOf(permission.POST_NOTIFICATIONS), 0)
}
}
Expand Down Expand Up @@ -443,6 +445,8 @@ class EmulationActivity : AppCompatActivity(), View.OnApplyWindowInsetsListener
public override fun onResume() {
super.onResume()
lorieView.requestFocus()
overlayView?.loadFromPreferences()

prepareButtonsAxisValues(this)

lifecycleScope.cancel()
Expand Down
19 changes: 9 additions & 10 deletions app/src/main/java/com/micewine/emu/activities/GeneralSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ class GeneralSettings : AppCompatActivity() {
private val displaySettingsFragment = DisplaySettingsFragment()
private val driversSettingsFragment = DriversSettingsFragment()
private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
@SuppressLint("UnspecifiedRegisterReceiverFlag")
override fun onReceive(context: Context, intent: Intent) {
val preference = intent.getStringExtra("preference")

if (intent.action == ACTION_PREFERENCE_SELECT) {
when (preference) {
getString(R.string.box64_settings_title) -> {
generalSettingsToolbar?.title = context.resources.getString(R.string.box64_settings_title)
generalSettingsToolbar?.title = getString(R.string.box64_settings_title)

fragmentLoader(box64SettingsFragment, false)
}
Expand All @@ -47,13 +46,13 @@ class GeneralSettings : AppCompatActivity() {
}

getString(R.string.display_settings_title) -> {
generalSettingsToolbar?.title = context.resources.getString(R.string.display_settings_title)
generalSettingsToolbar?.title = getString(R.string.display_settings_title)

fragmentLoader(displaySettingsFragment, false)
}

context.resources.getString(R.string.driver_settings_title) -> {
generalSettingsToolbar?.title = context.resources.getString(R.string.driver_settings_title)
generalSettingsToolbar?.title = getString(R.string.driver_settings_title)

fragmentLoader(driversSettingsFragment, false)
}
Expand Down Expand Up @@ -104,15 +103,15 @@ class GeneralSettings : AppCompatActivity() {
}

private fun fragmentLoader(fragment: Fragment, appInit: Boolean) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
supportFragmentManager.beginTransaction().apply {
replace(R.id.settings_content, fragment)

fragmentTransaction.replace(R.id.settings_content, fragment)
if (!appInit) {
addToBackStack(null)
}

if (!appInit) {
fragmentTransaction.addToBackStack(null)
commit()
}

fragmentTransaction.commit()
}

companion object {
Expand Down
Loading

0 comments on commit 390b072

Please sign in to comment.