Skip to content

Commit

Permalink
Android overlay: possibility to adjust the scale of individual buttons (
Browse files Browse the repository at this point in the history
#69)

* Can adjust the scale of each button individually 4x
---------

Co-authored-by: João Vitor Polverari <69534455+Polarzincomfrio@users.noreply.github.com>
  • Loading branch information
gperrio and Polarzincomfrio committed Apr 13, 2024
1 parent 63c15b1 commit 2733189
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 12 deletions.
Expand Up @@ -619,8 +619,88 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
true
}

R.id.menu_emulation_adjust_scale_reset_all -> {
resetAllScales()
true
}

R.id.menu_emulation_adjust_scale -> {
showAdjustScaleDialog()
showAdjustScaleDialog("controlScale")
true
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down Expand Up @@ -774,16 +854,16 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
.show()
}

private fun showAdjustScaleDialog() {
private fun showAdjustScaleDialog(target: String) {
val sliderBinding = DialogSliderBinding.inflate(layoutInflater)

sliderBinding.apply {
slider.valueTo = 150f
slider.value = preferences.getInt("controlScale", 50).toFloat()
slider.value = preferences.getInt(target, 50).toFloat()
slider.addOnChangeListener(
Slider.OnChangeListener { slider: Slider, progress: Float, _: Boolean ->
textValue.text = (progress.toInt() + 50).toString()
setControlScale(slider.value.toInt())
setControlScale(slider.value.toInt(), target)
})
textValue.text = (sliderBinding.slider.value.toInt() + 50).toString()
textUnits.text = "%"
Expand All @@ -794,13 +874,13 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
.setTitle(R.string.emulation_control_scale)
.setView(sliderBinding.root)
.setNegativeButton(android.R.string.cancel) { _: DialogInterface?, _: Int ->
setControlScale(previousProgress)
setControlScale(previousProgress, target)
}
.setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int ->
setControlScale(sliderBinding.slider.value.toInt())
setControlScale(sliderBinding.slider.value.toInt(), target)
}
.setNeutralButton(R.string.slider_default) { _: DialogInterface?, _: Int ->
setControlScale(50)
setControlScale(50, target)
}
.show()
}
Expand Down Expand Up @@ -836,13 +916,40 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
.show()
}

private fun setControlScale(scale: Int) {
private fun setControlScale(scale: Int, target: String) {
preferences.edit()
.putInt("controlScale", scale)
.putInt(target, scale)
.apply()
binding.surfaceInputOverlay.refreshControls()
}

private fun resetScale(target: String) {
preferences.edit().putInt(
target,
50
).apply()
}

private fun resetAllScales() {
resetScale("controlScale")
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_A)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_B)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_X)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_Y)
resetScale("controlScale-" + NativeLibrary.ButtonType.TRIGGER_L)
resetScale("controlScale-" + NativeLibrary.ButtonType.TRIGGER_R)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_ZL)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_ZR)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_START)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_SELECT)
resetScale("controlScale-" + NativeLibrary.ButtonType.DPAD)
resetScale("controlScale-" + NativeLibrary.ButtonType.STICK_LEFT)
resetScale("controlScale-" + NativeLibrary.ButtonType.STICK_C)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_HOME)
resetScale("controlScale-" + NativeLibrary.ButtonType.BUTTON_SWAP)
binding.surfaceInputOverlay.refreshControls()
}

private fun setControlOpacity(opacity: Int) {
preferences.edit()
.putInt("controlOpacity", opacity)
Expand All @@ -861,8 +968,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
}

private fun resetInputOverlay() {
resetAllScales()
preferences.edit()
.putInt("controlScale", 50)
.putInt("controlOpacity", 50)
.apply()

Expand Down
Expand Up @@ -939,6 +939,10 @@ class InputOverlay(context: Context?, attrs: AttributeSet?) : SurfaceView(contex
scale *= (preferences.getInt("controlScale", 50) + 50).toFloat()
scale /= 100f


scale *= (preferences.getInt("controlScale-$buttonId", 50) + 50).toFloat()
scale /= 100f

val opacity: Int = preferences.getInt("controlOpacity", 50) * 255 / 100

// Initialize the InputOverlayDrawableButton.
Expand Down Expand Up @@ -997,6 +1001,13 @@ class InputOverlay(context: Context?, attrs: AttributeSet?) : SurfaceView(contex
scale *= (preferences.getInt("controlScale", 50) + 50).toFloat()
scale /= 100f

scale *= (preferences.getInt(
"controlScale-" + NativeLibrary.ButtonType.DPAD,
50
) + 50).toFloat()

scale /= 100f

val opacity: Int = preferences.getInt("controlOpacity", 50) * 255 / 100

// Initialize the InputOverlayDrawableDpad.
Expand Down Expand Up @@ -1057,6 +1068,9 @@ class InputOverlay(context: Context?, attrs: AttributeSet?) : SurfaceView(contex
scale *= (preferences.getInt("controlScale", 50) + 50).toFloat()
scale /= 100f

scale *= (preferences.getInt("controlScale-$joystick", 50) + 50).toFloat()
scale /= 100f

val opacity: Int = preferences.getInt("controlOpacity", 50) * 255 / 100

// Initialize the InputOverlayDrawableJoystick.
Expand Down
58 changes: 56 additions & 2 deletions src/android/app/src/main/res/menu/menu_overlay_options.xml
Expand Up @@ -25,8 +25,62 @@
android:title="@string/emulation_toggle_controls" />

<item
android:id="@+id/menu_emulation_adjust_scale"
android:title="@string/emulation_control_scale" />
android:id="@+id/menu_emulation_adjust_scales"
android:title="@string/emulation_control_scale">
<menu>
<item
android:id="@+id/menu_emulation_adjust_scale_reset_all"
android:title="@string/emulation_control_scale_reset_all" />
<item
android:id="@+id/menu_emulation_adjust_scale"
android:title="@string/emulation_control_scale_global" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_a"
android:title="@string/button_a" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_b"
android:title="@string/button_b" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_x"
android:title="@string/button_x" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_y"
android:title="@string/button_y" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_l"
android:title="@string/button_l" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_r"
android:title="@string/button_r" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_zl"
android:title="@string/button_zl" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_zr"
android:title="@string/button_zr" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_start"
android:title="@string/button_start" />
<item
android:id="@+id/menu_emulation_adjust_scale_button_select"
android:title="@string/button_select" />
<item
android:id="@+id/menu_emulation_adjust_scale_controller_dpad"
android:title="@string/controller_dpad" />
<item
android:id="@+id/menu_emulation_adjust_scale_controller_circlepad"
android:title="@string/controller_circlepad" />
<item
android:id="@+id/menu_emulation_adjust_scale_controller_c"
android:title="@string/controller_c" />
<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"
android:title="@string/button_swap" />
</menu>
</item>

<item
android:id="@+id/menu_emulation_adjust_opacity"
Expand Down
2 changes: 2 additions & 0 deletions src/android/app/src/main/res/values/strings.xml
Expand Up @@ -331,6 +331,8 @@
<string name="emulation_done">Done</string>
<string name="emulation_toggle_controls">Toggle Controls</string>
<string name="emulation_control_scale">Adjust Scale</string>
<string name="emulation_control_scale_global">Global Scale</string>
<string name="emulation_control_scale_reset_all">Reset All</string>
<string name="emulation_control_opacity">Adjust Opacity</string>
<string name="emulation_control_joystick_rel_center">Relative Stick Center</string>
<string name="emulation_control_dpad_slide_enable">D-Pad Sliding</string>
Expand Down

0 comments on commit 2733189

Please sign in to comment.