Draw a selection rectangle around the active button while editing

This commit is contained in:
lynxnb 2023-03-31 00:47:56 +02:00 committed by Billy Laws
parent e91993698f
commit 1d6b075d5c
2 changed files with 17 additions and 1 deletions

View File

@ -83,7 +83,7 @@ abstract class OnScreenButton(
private val left get() = currentX - itemWidth / 2f private val left get() = currentX - itemWidth / 2f
private val top get() = currentY - itemHeight / 2f private val top get() = currentY - itemHeight / 2f
protected val currentBounds get() = Rect(left.roundToInt(), top.roundToInt(), (left + itemWidth).roundToInt(), (top + itemHeight).roundToInt()) val currentBounds get() = Rect(left.roundToInt(), top.roundToInt(), (left + itemWidth).roundToInt(), (top + itemHeight).roundToInt())
/** /**
* Keeps track of finger when there are multiple touches * Keeps track of finger when there are multiple touches

View File

@ -10,14 +10,18 @@ import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator import android.animation.ValueAnimator
import android.content.Context import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PointF import android.graphics.PointF
import android.os.VibrationEffect import android.os.VibrationEffect
import android.os.Vibrator import android.os.Vibrator
import android.util.AttributeSet import android.util.AttributeSet
import android.util.TypedValue
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.View.OnTouchListener import android.view.View.OnTouchListener
import androidx.annotation.IntRange import androidx.annotation.IntRange
import emu.skyline.R
import emu.skyline.input.ButtonId import emu.skyline.input.ButtonId
import emu.skyline.input.ButtonState import emu.skyline.input.ButtonState
import emu.skyline.input.ControllerType import emu.skyline.input.ControllerType
@ -73,6 +77,12 @@ class OnScreenControllerView @JvmOverloads constructor(context : Context, attrs
editInfo.onEditButtonChangedListener = listener editInfo.onEditButtonChangedListener = listener
} }
private val selectionPaint = Paint().apply {
color = context.obtainStyledAttributes(intArrayOf(R.attr.colorPrimary)).getColor(0, Color.RED)
style = Paint.Style.STROKE
strokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, context.resources.displayMetrics)
}
// Populated externally by the activity, as retrieving the vibrator service inside the view crashes the layout editor // Populated externally by the activity, as retrieving the vibrator service inside the view crashes the layout editor
lateinit var vibrator : Vibrator lateinit var vibrator : Vibrator
private val effectClick = VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK) private val effectClick = VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK)
@ -95,6 +105,10 @@ class OnScreenControllerView @JvmOverloads constructor(context : Context, attrs
button.render(canvas) button.render(canvas)
} }
} }
// Draw the selection box around the edit button
if (editInfo.isEditing && editInfo.editButton is OnScreenButton)
canvas.drawRect((editInfo.editButton as OnScreenButton).currentBounds, selectionPaint)
} }
private val playingTouchHandler = OnTouchListener { _, event -> private val playingTouchHandler = OnTouchListener { _, event ->
@ -248,6 +262,7 @@ class OnScreenControllerView @JvmOverloads constructor(context : Context, attrs
activeEditButtonChanged = true activeEditButtonChanged = true
editInfo.editButton = touchedButton editInfo.editButton = touchedButton
performClick() performClick()
invalidate()
return@run return@run
} }
@ -292,6 +307,7 @@ class OnScreenControllerView @JvmOverloads constructor(context : Context, attrs
fun selectAllButtons() { fun selectAllButtons() {
editInfo.editButton = allButtonsProxy editInfo.editButton = allButtonsProxy
invalidate()
} }
fun setButtonEnabled(enabled : Boolean) { fun setButtonEnabled(enabled : Boolean) {