Fix OSC alpha not changing on button press

This commit is contained in:
lynxnb 2022-08-20 17:00:40 +02:00
parent 8fb4e62c28
commit cfa5f0e030
5 changed files with 45 additions and 45 deletions

View File

@ -72,14 +72,16 @@ abstract class OnScreenButton(
var touchPointerId = -1 var touchPointerId = -1
var partnerPointerId = -1 var partnerPointerId = -1
var isPressed = false
var isEditing = false var isEditing = false
private set private set
protected open fun renderCenteredText(canvas : Canvas, text : String, size : Float, x : Float, y : Float) { protected open fun renderCenteredText(canvas : Canvas, text : String, size : Float, x : Float, y : Float, alpha : Int) {
buttonSymbolPaint.apply { buttonSymbolPaint.apply {
textSize = size textSize = size
textAlign = Paint.Align.LEFT textAlign = Paint.Align.LEFT
alpha = (config.opacity / 100.0 * 255).toInt() this.alpha = alpha
getTextBounds(text, 0, text.length, textBoundsRect) getTextBounds(text, 0, text.length, textBoundsRect)
} }
canvas.drawText(text, x - textBoundsRect.width() / 2f - textBoundsRect.left, y + textBoundsRect.height() / 2f - textBoundsRect.bottom, buttonSymbolPaint) canvas.drawText(text, x - textBoundsRect.width() / 2f - textBoundsRect.left, y + textBoundsRect.height() / 2f - textBoundsRect.bottom, buttonSymbolPaint)
@ -87,20 +89,25 @@ abstract class OnScreenButton(
open fun render(canvas : Canvas) { open fun render(canvas : Canvas) {
val bounds = currentBounds val bounds = currentBounds
val alpha = if (isPressed) (config.alpha - 130).coerceIn(30..255) else config.alpha
drawable.apply { drawable.apply {
this.bounds = bounds this.bounds = bounds
alpha = (config.opacity / 100.0 * 255).toInt() this.alpha = alpha
draw(canvas) draw(canvas)
} }
renderCenteredText(canvas, buttonId.short!!, itemWidth.coerceAtMost(itemHeight) * 0.4f, bounds.centerX().toFloat(), bounds.centerY().toFloat()) renderCenteredText(canvas, buttonId.short!!, itemWidth.coerceAtMost(itemHeight) * 0.4f, bounds.centerX().toFloat(), bounds.centerY().toFloat(), alpha)
} }
abstract fun isTouched(x : Float, y : Float) : Boolean abstract fun isTouched(x : Float, y : Float) : Boolean
abstract fun onFingerDown(x : Float, y : Float) open fun onFingerDown(x : Float, y : Float) {
isPressed = true
}
abstract fun onFingerUp(x : Float, y : Float) open fun onFingerUp(x : Float, y : Float) {
isPressed = false
}
fun loadConfigValues() { fun loadConfigValues() {
relativeX = config.relativeX relativeX = config.relativeX

View File

@ -10,7 +10,7 @@ import emu.skyline.input.ButtonId
import emu.skyline.utils.sharedPreferences import emu.skyline.utils.sharedPreferences
interface ControllerConfiguration { interface ControllerConfiguration {
var opacity : Int var alpha : Int
var enabled : Boolean var enabled : Boolean
var globalScale : Float var globalScale : Float
var relativeX : Float var relativeX : Float
@ -21,7 +21,7 @@ interface ControllerConfiguration {
* Dummy implementation so layout editor is able to render [OnScreenControllerView] when [android.view.View.isInEditMode] is true * Dummy implementation so layout editor is able to render [OnScreenControllerView] when [android.view.View.isInEditMode] is true
*/ */
class ControllerConfigurationDummy(defaultRelativeX : Float, defaultRelativeY : Float) : ControllerConfiguration { class ControllerConfigurationDummy(defaultRelativeX : Float, defaultRelativeY : Float) : ControllerConfiguration {
override var opacity : Int = 100 override var alpha : Int = 255
override var enabled = true override var enabled = true
override var globalScale = 1f override var globalScale = 1f
override var relativeX = defaultRelativeX override var relativeX = defaultRelativeX
@ -31,7 +31,7 @@ class ControllerConfigurationDummy(defaultRelativeX : Float, defaultRelativeY :
class ControllerConfigurationImpl(private val context : Context, private val buttonId : ButtonId, defaultRelativeX : Float, defaultRelativeY : Float) : ControllerConfiguration { class ControllerConfigurationImpl(private val context : Context, private val buttonId : ButtonId, defaultRelativeX : Float, defaultRelativeY : Float) : ControllerConfiguration {
private inline fun <reified T> config(default : T, prefix : String = "${buttonId.name}_") = sharedPreferences(context, default, prefix, "controller_config") private inline fun <reified T> config(default : T, prefix : String = "${buttonId.name}_") = sharedPreferences(context, default, prefix, "controller_config")
override var opacity by config(100) override var alpha by config(255, "")
override var enabled by config(true) override var enabled by config(true)
override var globalScale by config(1.15f, "") override var globalScale by config(1.15f, "")
override var relativeX by config(defaultRelativeX) override var relativeX by config(defaultRelativeX)

View File

@ -35,6 +35,10 @@ class OnScreenControllerView @JvmOverloads constructor(context : Context, attrs
private val controllerTypeMappings = mapOf(*ControllerType.values().map { private val controllerTypeMappings = mapOf(*ControllerType.values().map {
it to (setOf(*it.buttons) to setOf(*it.sticks)) it to (setOf(*it.buttons) to setOf(*it.sticks))
}.toTypedArray()) }.toTypedArray())
private const val SCALE_STEP = 0.05f
private const val ALPHA_STEP = 25
private val ALPHA_RANGE = 55..255
} }
private val controls = Controls(this) private val controls = Controls(this)
@ -249,22 +253,22 @@ class OnScreenControllerView @JvmOverloads constructor(context : Context, attrs
} }
fun increaseScale() { fun increaseScale() {
controls.globalScale += 0.05f controls.globalScale += SCALE_STEP
invalidate() invalidate()
} }
fun decreaseScale() { fun decreaseScale() {
controls.globalScale -= 0.05f controls.globalScale -= SCALE_STEP
invalidate() invalidate()
} }
fun changeOpacity(delta : Int) { fun increaseOpacity() {
controls.allButtons.forEach { controls.alpha = (controls.alpha + ALPHA_STEP).coerceIn(ALPHA_RANGE)
val newOpacity = (it.config.opacity + delta).coerceIn(0, 100) invalidate()
it.config.opacity = newOpacity
if (it is JoystickButton)
it.innerButton.config.opacity = newOpacity
} }
fun decreaseOpacity() {
controls.alpha = (controls.alpha - ALPHA_STEP).coerceIn(ALPHA_RANGE)
invalidate() invalidate()
} }

View File

@ -13,7 +13,6 @@ import androidx.core.content.ContextCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.floatingactionbutton.FloatingActionButton
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.MainViewModel
import emu.skyline.R import emu.skyline.R
import emu.skyline.databinding.OnScreenEditActivityBinding import emu.skyline.databinding.OnScreenEditActivityBinding
import emu.skyline.utils.PreferenceSettings import emu.skyline.utils.PreferenceSettings
@ -21,9 +20,6 @@ import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class OnScreenEditActivity : AppCompatActivity() { class OnScreenEditActivity : AppCompatActivity() {
companion object {
private const val OPACITY_DELTA_STEP = 10
}
private val binding by lazy { OnScreenEditActivityBinding.inflate(layoutInflater) } private val binding by lazy { OnScreenEditActivityBinding.inflate(layoutInflater) }
private var fullEditVisible = true private var fullEditVisible = true
@ -84,8 +80,8 @@ class OnScreenEditActivity : AppCompatActivity() {
Pair(R.drawable.ic_edit, editAction), Pair(R.drawable.ic_edit, editAction),
Pair(R.drawable.ic_zoom_out, { binding.onScreenControllerView.decreaseScale() }), Pair(R.drawable.ic_zoom_out, { binding.onScreenControllerView.decreaseScale() }),
Pair(R.drawable.ic_zoom_in, { binding.onScreenControllerView.increaseScale() }), Pair(R.drawable.ic_zoom_in, { binding.onScreenControllerView.increaseScale() }),
Pair(R.drawable.ic_opacity_minus) { binding.onScreenControllerView.changeOpacity(-OPACITY_DELTA_STEP) }, Pair(R.drawable.ic_opacity_minus) { binding.onScreenControllerView.decreaseOpacity() },
Pair(R.drawable.ic_opacity_plus) { binding.onScreenControllerView.changeOpacity(OPACITY_DELTA_STEP) }, Pair(R.drawable.ic_opacity_plus) { binding.onScreenControllerView.increaseOpacity() },
Pair(R.drawable.ic_close, closeAction) Pair(R.drawable.ic_close, closeAction)
) )

View File

@ -40,14 +40,6 @@ open class CircularButton(
* Checks if [x] and [y] are within circle * Checks if [x] and [y] are within circle
*/ */
override fun isTouched(x : Float, y : Float) : Boolean = (PointF(currentX, currentY) - (PointF(x, y))).length() <= radius override fun isTouched(x : Float, y : Float) : Boolean = (PointF(currentX, currentY) - (PointF(x, y))).length() <= radius
override fun onFingerDown(x : Float, y : Float) {
drawable.alpha = 125
}
override fun onFingerUp(x : Float, y : Float) {
drawable.alpha = 255
}
} }
class JoystickButton( class JoystickButton(
@ -64,7 +56,7 @@ class JoystickButton(
defaultRelativeRadiusToX, defaultRelativeRadiusToX,
R.drawable.ic_button R.drawable.ic_button
) { ) {
internal val innerButton = CircularButton(onScreenControllerView, buttonId, config.relativeX, config.relativeY, defaultRelativeRadiusToX * 0.75f, R.drawable.ic_stick) private val innerButton = CircularButton(onScreenControllerView, buttonId, config.relativeX, config.relativeY, defaultRelativeRadiusToX * 0.75f, R.drawable.ic_stick)
var recenterSticks = false var recenterSticks = false
private lateinit var initialTapPosition : PointF private lateinit var initialTapPosition : PointF
@ -73,7 +65,7 @@ class JoystickButton(
var shortDoubleTapped = false var shortDoubleTapped = false
private set private set
override fun renderCenteredText(canvas : Canvas, text : String, size : Float, x : Float, y : Float) = Unit override fun renderCenteredText(canvas : Canvas, text : String, size : Float, x : Float, y : Float, alpha : Int) = Unit
override fun render(canvas : Canvas) { override fun render(canvas : Canvas) {
super.render(canvas) super.render(canvas)
@ -101,7 +93,7 @@ class JoystickButton(
if (firstTapDiff in 0..500 && secondTapDiff in 0..500) { if (firstTapDiff in 0..500 && secondTapDiff in 0..500) {
shortDoubleTapped = true shortDoubleTapped = true
// Indicate stick being pressed with lower alpha value // Indicate stick being pressed with lower alpha value
drawable.alpha = 50 isPressed = true
} }
fingerDownTime = currentTime fingerDownTime = currentTime
} }
@ -113,7 +105,7 @@ class JoystickButton(
fingerUpTime = SystemClock.elapsedRealtime() fingerUpTime = SystemClock.elapsedRealtime()
shortDoubleTapped = false shortDoubleTapped = false
drawable.alpha = 255 isPressed = false
} }
fun onFingerMoved(x : Float, y : Float, manualMove : Boolean = true) : PointF { fun onFingerMoved(x : Float, y : Float, manualMove : Boolean = true) : PointF {
@ -174,14 +166,6 @@ open class RectangularButton(
drawableId drawableId
) { ) {
override fun isTouched(x : Float, y : Float) = currentBounds.contains(x.roundToInt(), y.roundToInt()) override fun isTouched(x : Float, y : Float) = currentBounds.contains(x.roundToInt(), y.roundToInt())
override fun onFingerDown(x : Float, y : Float) {
drawable.alpha = 125
}
override fun onFingerUp(x : Float, y : Float) {
drawable.alpha = 255
}
} }
class TriggerButton( class TriggerButton(
@ -248,11 +232,20 @@ class Controls(onScreenControllerView : OnScreenControllerView) {
val allButtons = circularButtons + joysticks + rectangularButtons + triggerButtons val allButtons = circularButtons + joysticks + rectangularButtons + triggerButtons
/** /**
* We can take any of the global scale variables from the buttons * We can take any of the global scale variables from the buttons since the value is shared across all buttons
*/ */
var globalScale var globalScale
get() = circularButtons.first().config.globalScale get() = circularButtons.first().config.globalScale
set(value) { set(value) {
circularButtons.first().config.globalScale = value circularButtons.first().config.globalScale = value
} }
/**
* We can take any of the alpha variables from the buttons since the value is shared across all buttons
*/
var alpha
get() = circularButtons.first().config.alpha
set(value) {
circularButtons.first().config.alpha = value
}
} }