[android] Use spinbox setting type for CPU_TICKS (#2952)
Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/2952 Reviewed-by: crueter <crueter@eden-emu.dev> Reviewed-by: MaranBr <maranbr@eden-emu.dev> Reviewed-by: Caio Oliveira <caiooliveirafarias0@gmail.com> Co-authored-by: kleidis <kleidis1@protonmail.com> Co-committed-by: kleidis <kleidis1@protonmail.com>
This commit is contained in:
parent
ba9e03a612
commit
f51d61e4a4
|
|
@ -667,10 +667,11 @@ abstract class SettingsItem(
|
|||
)
|
||||
)
|
||||
put(
|
||||
SliderSetting(
|
||||
SpinBoxSetting(
|
||||
IntSetting.CPU_TICKS,
|
||||
titleId = R.string.cpu_ticks,
|
||||
descriptionId = 0,
|
||||
valueHint = R.string.cpu_ticks,
|
||||
min = 77,
|
||||
max = 65535
|
||||
)
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ import android.text.TextWatcher
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.MotionEvent
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.fragment.app.DialogFragment
|
||||
|
|
@ -197,6 +200,54 @@ class SettingsDialogFragment : DialogFragment(), DialogInterface.OnClickListener
|
|||
updateValidity(newValue)
|
||||
}
|
||||
|
||||
fun attachRepeat(button: View, delta: Int) {
|
||||
val handler = Handler(Looper.getMainLooper())
|
||||
var runnable: Runnable? = null
|
||||
val initialDelay = 400L
|
||||
val minDelay = 40L
|
||||
val accelerationFactor = 0.75f
|
||||
|
||||
button.setOnTouchListener { v, event ->
|
||||
when (event.action) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
val current = spinboxBinding.editValue.text.toString().toIntOrNull() ?: currentValue
|
||||
val newValue = (current + delta)
|
||||
spinboxBinding.editValue.setText(newValue.toString())
|
||||
updateValidity(newValue)
|
||||
|
||||
var delay = initialDelay
|
||||
runnable = object : Runnable {
|
||||
override fun run() {
|
||||
val curr = spinboxBinding.editValue.text.toString().toIntOrNull() ?: currentValue
|
||||
val next = curr + delta
|
||||
spinboxBinding.editValue.setText(next.toString())
|
||||
updateValidity(next)
|
||||
// accelerate
|
||||
delay = (delay * accelerationFactor).toLong().coerceAtLeast(minDelay)
|
||||
handler.postDelayed(this, delay)
|
||||
}
|
||||
}
|
||||
handler.postDelayed(runnable!!, initialDelay)
|
||||
true
|
||||
}
|
||||
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
if (runnable != null) {
|
||||
handler.removeCallbacks(runnable!!)
|
||||
runnable = null
|
||||
}
|
||||
v.performClick()
|
||||
true
|
||||
}
|
||||
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
attachRepeat(spinboxBinding.buttonDecrement, -1)
|
||||
attachRepeat(spinboxBinding.buttonIncrement, 1)
|
||||
|
||||
spinboxBinding.editValue.addTextChangedListener(object : TextWatcher {
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||||
|
|
|
|||
Loading…
Reference in New Issue