Skip to content

Commit

Permalink
Android: Implement swap screens hotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
gperrio authored and Gamer64ytb committed Jul 25, 2024
1 parent 6414b77 commit a27ce56
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ object NativeLibrary {
const val DPAD = 780
const val BUTTON_DEBUG = 781
const val BUTTON_GPIO14 = 782
const val BUTTON_SWAP_SCREENS = 783
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,11 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
true
}

R.id.menu_emulation_adjust_scale_button_swap_screens -> {
showAdjustScaleDialog("controlScale-" + NativeLibrary.ButtonType.BUTTON_SWAP_SCREENS)
true
}

R.id.menu_emulation_adjust_opacity -> {
showAdjustOpacityDialog()
true
Expand Down Expand Up @@ -848,12 +853,12 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram

private fun showToggleControlsDialog() {
val editor = preferences.edit()
val enabledButtons = BooleanArray(14)
val enabledButtons = BooleanArray(15)
enabledButtons.forEachIndexed { i: Int, _: Boolean ->
// Buttons that are disabled by default
var defaultValue = true
when (i) {
6, 7, 12, 13 -> defaultValue = false
6, 7, 12, 13, 14 -> defaultValue = false
}
enabledButtons[i] = preferences.getBoolean("buttonToggle$i", defaultValue)
}
Expand Down Expand Up @@ -971,6 +976,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
resetScale("controlScale-" + NativeLibrary.ButtonType.STICK_LEFT)
resetScale("controlScale-" + NativeLibrary.ButtonType.STICK_C)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_HOME)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_SWAP_SCREENS)
binding.surfaceInputOverlay.refreshControls()
}

Expand All @@ -991,10 +997,10 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
.apply()

val editor = preferences.edit()
for (i in 0 until 14) {
for (i in 0 until 15) {
var defaultValue = true
when (i) {
6, 7, 12, 13 -> defaultValue = false
6, 7, 12, 13, 14 -> defaultValue = false
}
editor.putBoolean("buttonToggle$i", defaultValue)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ class InputOverlay(context: Context?, attrs: AttributeSet?) : SurfaceView(contex
performHapticFeedback(type)
}

private fun swapScreens() {
val isSwapScreensEnabled = !EmulationMenuSettings.swapScreens
EmulationMenuSettings.swapScreens = isSwapScreensEnabled
NativeLibrary.swapScreens(
isSwapScreensEnabled,
(context as Activity).windowManager.defaultDisplay.rotation
)
}

override fun onTouch(v: View, event: MotionEvent): Boolean {
if (isInEditMode) {
return onTouchWhileEditing(event)
Expand All @@ -90,6 +99,11 @@ class InputOverlay(context: Context?, attrs: AttributeSet?) : SurfaceView(contex
if (!button.updateStatus(event, this)) {
continue
}

if (button.id == NativeLibrary.ButtonType.BUTTON_SWAP_SCREENS && button.status == NativeLibrary.ButtonState.PRESSED) {
swapScreens()
}

NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, button.id, button.status)
shouldUpdateView = true
}
Expand Down Expand Up @@ -442,6 +456,17 @@ class InputOverlay(context: Context?, attrs: AttributeSet?) : SurfaceView(contex
)
)
}
if (preferences.getBoolean("buttonToggle14", false)) {
overlayButtons.add(
initializeOverlayButton(
context,
R.drawable.button_swap_screens,
R.drawable.button_swap_screens_pressed,
NativeLibrary.ButtonType.BUTTON_SWAP_SCREENS,
orientation
)
)
}
}

fun refreshControls() {
Expand Down Expand Up @@ -639,6 +664,14 @@ class InputOverlay(context: Context?, attrs: AttributeSet?) : SurfaceView(contex
NativeLibrary.ButtonType.STICK_LEFT.toString() + "-Y",
resources.getInteger(R.integer.N3DS_STICK_MAIN_Y).toFloat() / 1000 * maxY
)
.putFloat(
NativeLibrary.ButtonType.BUTTON_SWAP_SCREENS.toString() + "-X",
resources.getInteger(R.integer.N3DS_BUTTON_SWAP_SCREENS_X).toFloat() / 1000 * maxX
)
.putFloat(
NativeLibrary.ButtonType.BUTTON_SWAP_SCREENS.toString() + "-Y",
resources.getInteger(R.integer.N3DS_BUTTON_SWAP_SCREENS_Y).toFloat() / 1000 * maxY
)
.apply()
}

Expand Down Expand Up @@ -774,6 +807,14 @@ class InputOverlay(context: Context?, attrs: AttributeSet?) : SurfaceView(contex
NativeLibrary.ButtonType.STICK_LEFT.toString() + portrait + "-Y",
resources.getInteger(R.integer.N3DS_STICK_MAIN_PORTRAIT_Y).toFloat() / 1000 * maxY
)
.putFloat(
NativeLibrary.ButtonType.BUTTON_SWAP_SCREENS.toString() + portrait + "-X",
resources.getInteger(R.integer.N3DS_BUTTON_SWAP_SCREENS_PORTRAIT_X).toFloat() / 1000 * maxX
)
.putFloat(
NativeLibrary.ButtonType.BUTTON_SWAP_SCREENS.toString() + portrait + "-Y",
resources.getInteger(R.integer.N3DS_BUTTON_SWAP_SCREENS_PORTRAIT_Y).toFloat() / 1000 * maxY
)
.apply()
}

Expand Down Expand Up @@ -884,7 +925,8 @@ class InputOverlay(context: Context?, attrs: AttributeSet?) : SurfaceView(contex
var scale: Float = when (buttonId) {
NativeLibrary.ButtonType.BUTTON_HOME,
NativeLibrary.ButtonType.BUTTON_START,
NativeLibrary.ButtonType.BUTTON_SELECT -> 0.08f
NativeLibrary.ButtonType.BUTTON_SELECT,
NativeLibrary.ButtonType.BUTTON_SWAP_SCREENS -> 0.08f

NativeLibrary.ButtonType.TRIGGER_L,
NativeLibrary.ButtonType.TRIGGER_R,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/android/app/src/main/res/menu/menu_overlay_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<item
android:id="@+id/menu_emulation_adjust_scale_button_home"
android:title="@string/button_home" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_swap_screens"
android:title="@string/button_swap_screens" />
</menu>
</item>

Expand Down
1 change: 1 addition & 0 deletions src/android/app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<item>@string/controller_circlepad</item>
<item>@string/controller_c</item>
<item>@string/button_home</item>
<item>@string/button_swap_screens</item>
</string-array>

<string-array name="cameraImageSourceNames">
Expand Down
4 changes: 4 additions & 0 deletions src/android/app/src/main/res/values/integers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<integer name="N3DS_BUTTON_START_Y">850</integer>
<integer name="N3DS_BUTTON_HOME_X">510</integer>
<integer name="N3DS_BUTTON_HOME_Y">850</integer>
<integer name="N3DS_BUTTON_SWAP_SCREENS_X">370</integer>
<integer name="N3DS_BUTTON_SWAP_SCREENS_Y">850</integer>

<!-- Default N3DS portrait layout -->
<integer name="N3DS_BUTTON_A_PORTRAIT_X">810</integer>
Expand Down Expand Up @@ -61,5 +63,7 @@
<integer name="N3DS_BUTTON_SELECT_PORTRAIT_Y">794</integer>
<integer name="N3DS_BUTTON_START_PORTRAIT_X">520</integer>
<integer name="N3DS_BUTTON_START_PORTRAIT_Y">794</integer>
<integer name="N3DS_BUTTON_SWAP_SCREENS_PORTRAIT_X">460</integer>
<integer name="N3DS_BUTTON_SWAP_SCREENS_PORTRAIT_Y">675</integer>

</resources>
1 change: 1 addition & 0 deletions src/android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<string name="button_select" translatable="false">SELECT</string>
<string name="button_start" translatable="false">START</string>
<string name="button_home">HOME</string>
<string name="button_swap_screens">Swap Screens</string>
<string name="button_x" translatable="false">X</string>
<string name="button_y" translatable="false">Y</string>
<string name="button_l" translatable="false">L</string>
Expand Down

0 comments on commit a27ce56

Please sign in to comment.