Draw a box around disabled OSC buttons in edit mode

The box is also drawn if the alpha is configured too low (<5%).
This commit is contained in:
lynxnb 2023-04-02 23:21:11 +02:00 committed by Billy Laws
parent 659e090d38
commit 7e3d3bd281
2 changed files with 40 additions and 19 deletions

View File

@ -6,6 +6,7 @@
package emu.skyline.input.onscreen package emu.skyline.input.onscreen
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint import android.graphics.Paint
import android.graphics.PointF import android.graphics.PointF
import android.graphics.Rect import android.graphics.Rect
@ -13,7 +14,9 @@ import android.graphics.Typeface
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.LayerDrawable import android.graphics.drawable.LayerDrawable
import android.util.TypedValue
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import emu.skyline.R
import emu.skyline.input.ButtonId import emu.skyline.input.ButtonId
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -35,6 +38,18 @@ abstract class OnScreenButton(
* Aspect ratio the default values were based on * Aspect ratio the default values were based on
*/ */
const val CONFIGURED_ASPECT_RATIO = 2074f / 874f const val CONFIGURED_ASPECT_RATIO = 2074f / 874f
private var disabledSelectionPaint : Paint? = null
}
init {
if (disabledSelectionPaint == null) {
disabledSelectionPaint = Paint().apply {
color = onScreenControllerView.context.obtainStyledAttributes(intArrayOf(R.attr.colorTertiary)).use { it.getColor(0, Color.GREEN) }
style = Paint.Style.STROKE
strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, onScreenControllerView.context.resources.displayMetrics)
}
}
} }
final override val config = OnScreenConfigurationImpl(onScreenControllerView.context, buttonId, defaultRelativeX, defaultRelativeY, defaultEnabled) final override val config = OnScreenConfigurationImpl(onScreenControllerView.context, buttonId, defaultRelativeX, defaultRelativeY, defaultEnabled)
@ -121,26 +136,33 @@ abstract class OnScreenButton(
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)
} }
private fun applyColorsToDrawable(drawable : Drawable) {
when (drawable) {
is GradientDrawable -> drawable.setColor(config.backgroundColor)
is LayerDrawable -> for (i in 0 until drawable.numberOfLayers) applyColorsToDrawable(drawable.getDrawable(i))
else -> drawable.setTint(config.backgroundColor)
}
}
open fun render(canvas : Canvas) { open fun render(canvas : Canvas) {
val bounds = currentBounds var bounds : Rect? = null
if (config.enabled) {
bounds = currentBounds
val alpha = if (isPressed) config.alpha / 3 else config.alpha val alpha = if (isPressed) config.alpha / 3 else config.alpha
renderColors(drawable)
drawable.apply { drawable.apply {
this.bounds = bounds this.bounds = bounds
this.alpha = alpha this.alpha = alpha
applyColorsToDrawable(this)
draw(canvas) draw(canvas)
} }
renderCenteredText(canvas, buttonId.short!!, itemWidth.coerceAtMost(itemHeight) * 0.4f, bounds.centerX().toFloat(), bounds.centerY().toFloat(), alpha) renderCenteredText(canvas, buttonId.short!!, itemWidth.coerceAtMost(itemHeight) * 0.4f, bounds.centerX().toFloat(), bounds.centerY().toFloat(), alpha)
} }
}
private fun renderColors(drawable : Drawable) { // Draw a box around the button when in edit mode and it's either disabled or the alpha is too low (< 5%)
when (drawable) { if (editInfo.isEditing && (!config.enabled || config.alpha < 12))
is GradientDrawable -> drawable.setColor(config.backgroundColor) canvas.drawRect(bounds ?: currentBounds, disabledSelectionPaint ?: return)
is LayerDrawable -> {
for (i in 0 until drawable.numberOfLayers) renderColors(drawable.getDrawable(i))
}
else -> drawable.setTint(config.backgroundColor)
}
} }
abstract fun isTouched(x : Float, y : Float) : Boolean abstract fun isTouched(x : Float, y : Float) : Boolean

View File

@ -78,7 +78,7 @@ class OnScreenControllerView @JvmOverloads constructor(context : Context, attrs
} }
private val selectionPaint = Paint().apply { private val selectionPaint = Paint().apply {
color = context.obtainStyledAttributes(intArrayOf(R.attr.colorPrimary)).getColor(0, Color.RED) color = context.obtainStyledAttributes(intArrayOf(R.attr.colorPrimary)).use { it.getColor(0, Color.RED) }
style = Paint.Style.STROKE style = Paint.Style.STROKE
strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, context.resources.displayMetrics) strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, context.resources.displayMetrics)
} }
@ -95,8 +95,7 @@ class OnScreenControllerView @JvmOverloads constructor(context : Context, attrs
val allowedIds = controllerTypeMappings[controllerType] val allowedIds = controllerTypeMappings[controllerType]
controls.allButtons.forEach { button -> controls.allButtons.forEach { button ->
if (button.config.enabled if (allowedIds?.let { (buttonIds, stickIds) ->
&& allowedIds?.let { (buttonIds, stickIds) ->
if (button is JoystickButton) stickIds.contains(button.stickId) else buttonIds.contains(button.buttonId) if (button is JoystickButton) stickIds.contains(button.stickId) else buttonIds.contains(button.buttonId)
} != false } != false
) { ) {