0
0
mirror of https://github.com/Ishan09811/pine.git synced 2025-04-24 08:55:10 +00:00

Input: Allow binding emulator menu + add pause button binding (#45)

This commit is contained in:
Ishan09811 2025-01-02 18:49:42 +05:30 committed by GitHub
parent 8131163261
commit 58536da1a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 50 additions and 10 deletions

View File

@ -91,7 +91,7 @@ private const val ActionMute = "${BuildConfig.APPLICATION_ID}.ACTION_EMULATOR_MU
private val Number.toPx get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), Resources.getSystem().displayMetrics).toInt()
@AndroidEntryPoint
class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTouchListener, DisplayManager.DisplayListener {
class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTouchListener, DisplayManager.DisplayListener, InputHandler.OnButtonEventListener {
companion object {
private val Tag = EmulationActivity::class.java.simpleName
const val ReturnToMainTag = "returnToMain"
@ -519,7 +519,9 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
return true
}
})
}
}
inputHandler.setControllerButtonEventListener(this)
}
private fun setInsets() {
@ -580,6 +582,7 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
@SuppressWarnings("WeakerAccess")
fun resumeEmulator() {
if (!isEmulatorPaused) return
pauseEmulation(false)
changeAudioStatus(true)
isEmulatorPaused = false
@ -594,16 +597,28 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
pauseEmulator()
}
override fun onControllerButtonPressed(buttonId: ButtonId, PRESSED: Boolean) {
if (buttonId == ButtonId.Menu && PRESSED) {
if (binding.drawerLayout.isOpen) {
binding.drawerLayout.close()
} else {
binding.drawerLayout.open()
}
} else if (buttonId == ButtonId.Pause && PRESSED) {
if (!isEmulatorPaused) pauseEmulator() else resumeEmulator()
}
}
override fun onStart() {
super.onStart()
onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (binding.drawerLayout.isOpen) {
binding.drawerLayout.close()
} else {
binding.drawerLayout.open()
}
binding.drawerLayout.close()
} else {
binding.drawerLayout.open()
}
}
})
}

View File

@ -158,7 +158,11 @@ class ControllerActivity : AppCompatActivity() {
}
items.add(ControllerHeaderItem(getString(R.string.misc_buttons))) // The menu button will always exist
for (button in controller.type.buttons.filterNot { item -> buttonArrays.any { item in it.second } }.plus(ButtonId.Menu)) {
for (button in controller.type.buttons
.filterNot { item -> buttonArrays.any { item in it.second } }
.plus(ButtonId.Menu)
.plus(ButtonId.Pause)) {
val buttonItem = ControllerButtonViewItem(id, button, onControllerButtonClick)
items.add(buttonItem)

View File

@ -45,6 +45,7 @@ enum class ButtonId(val value : Long, val short : String? = null, val long : Int
RightSL(1 shl 26, "SL", string.left_shoulder),
RightSR(1 shl 27, "SR", string.right_shoulder),
Menu(1 shl 28, "⌂︎", string.emu_menu_button),
Pause(1 shl 29, "𓊕", string.emu_pause_button),
All(0x1FFFFFFF, "All");
}

View File

@ -74,6 +74,13 @@ class InputHandler(private val inputManager : InputManager, private val emulatio
* @param points An array of skyline::input::TouchScreenPoint in C++ represented as integers
*/
external fun setTouchState(points : IntArray)
fun isKotlinHandle(button: ButtonId): Boolean {
return when (button) {
ButtonId.Menu, ButtonId.Pause -> true // these needs to be handle kotlin side
else -> false
}
}
}
@Suppress("ArrayInDataClass")
@ -105,6 +112,7 @@ class InputHandler(private val inputManager : InputManager, private val emulatio
private val motionAcelOrientation : FloatArray = FloatArray(3)
private var motionAxisOrientationX = SensorManager.AXIS_Y
private var motionAxisOrientationY = SensorManager.AXIS_X
private var buttonEventListener: OnButtonEventListener? = null
/**
* Initializes all of the controllers from [InputManager] on the guest
@ -131,6 +139,10 @@ class InputHandler(private val inputManager : InputManager, private val emulatio
updateControllers()
}
fun setControllerButtonEventListener(listener: OnButtonEventListener?) {
buttonEventListener = listener
}
fun initialiseMotionSensors(context : Context) {
val sensorManager = context.getSystemService<SensorManager>() ?: return
val sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL)
@ -220,7 +232,9 @@ class InputHandler(private val inputManager : InputManager, private val emulatio
return when (val guestEvent = inputManager.eventMap[KeyHostEvent(event.device.descriptor, event.keyCode)]) {
is ButtonGuestEvent -> {
if (guestEvent.button != ButtonId.Menu)
if (isKotlinHandle(guestEvent.button))
buttonEventListener?.onControllerButtonPressed(guestEvent.button, action.state)
if (!isKotlinHandle(guestEvent.button))
setButtonState(guestEvent.id, guestEvent.button.value, action.state)
true
}
@ -273,7 +287,7 @@ class InputHandler(private val inputManager : InputManager, private val emulatio
when (guestEvent) {
is ButtonGuestEvent -> {
val action = if (abs(value) >= guestEvent.threshold) ButtonState.Pressed.state else ButtonState.Released.state
if (guestEvent.button != ButtonId.Menu)
if (!isKotlinHandle(guestEvent.button))
setButtonState(guestEvent.id, guestEvent.button.value, action)
}
@ -384,4 +398,8 @@ class InputHandler(private val inputManager : InputManager, private val emulatio
fun getFirstControllerType() : ControllerType {
return inputManager.controllers[0]?.type ?: ControllerType.None
}
interface OnButtonEventListener {
fun onControllerButtonPressed(buttonId: ButtonId, PRESSED: Boolean)
}
}

View File

@ -9,6 +9,7 @@ import android.content.Context
import android.util.Log
import dagger.hilt.android.qualifiers.ApplicationContext
import emu.skyline.R
import emu.skyline.input.InputHandler
import java.io.*
import javax.inject.Inject
import javax.inject.Singleton
@ -79,7 +80,7 @@ class InputManager @Inject constructor(@ApplicationContext context : Context) {
fun syncFile() {
for (controller in controllers.values) {
for (button in ButtonId.values()) {
if (button != ButtonId.Menu && !(controller.type.buttons.contains(button) || controller.type.sticks.any { it.button == button })) {
if (!InputHandler.isKotlinHandle(button) && !(controller.type.buttons.contains(button) || controller.type.sticks.any { it.button == button })) {
val guestEvent = ButtonGuestEvent(controller.id, button)
eventMap.filterValues { it is ButtonGuestEvent && it == guestEvent }.keys.forEach { eventMap.remove(it) }

View File

@ -269,6 +269,7 @@
<string name="plus_button">Plus</string>
<string name="minus_button">Minus</string>
<string name="emu_menu_button">Emulator Menu</string>
<string name="emu_pause_button">Pause</string>
<string name="stick_preview">Stick Preview</string>
<string name="done">Done</string>
<string name="use_non_stick">Use any unmapped button to finish</string>