Android: Support settings with float values

We did support float settings before but we never showed anything past the decimal place before.
This commit is contained in:
Charles Lombardo 2023-08-11 17:11:15 -04:00
parent 80b329b77f
commit 53fc3446d5
9 changed files with 132 additions and 62 deletions

View File

@ -6,7 +6,8 @@ import android.content.Context
import org.dolphinemu.dolphinemu.features.settings.model.AbstractFloatSetting
import org.dolphinemu.dolphinemu.features.settings.model.AbstractSetting
import org.dolphinemu.dolphinemu.features.settings.model.Settings
import kotlin.math.roundToInt
import java.math.BigDecimal
import java.math.MathContext
open class FloatSliderSetting : SliderSetting {
var floatSetting: AbstractFloatSetting
@ -19,11 +20,12 @@ open class FloatSliderSetting : SliderSetting {
setting: AbstractFloatSetting,
titleId: Int,
descriptionId: Int,
min: Int,
max: Int,
min: Float,
max: Float,
units: String?,
stepSize: Int
) : super(context, titleId, descriptionId, min, max, units, stepSize) {
stepSize: Float,
showDecimal: Boolean
) : super(context, titleId, descriptionId, min, max, units, stepSize, showDecimal) {
floatSetting = setting
}
@ -31,17 +33,21 @@ open class FloatSliderSetting : SliderSetting {
setting: AbstractFloatSetting,
name: CharSequence,
description: CharSequence?,
min: Int,
max: Int,
units: String?
) : super(name, description, min, max, units) {
min: Float,
max: Float,
units: String?,
showDecimal: Boolean
) : super(name, description, min, max, units, showDecimal) {
floatSetting = setting
}
override val selectedValue: Int
get() = floatSetting.float.roundToInt()
override val selectedValue: Float
get() = floatSetting.float
open fun setSelectedValue(settings: Settings?, selection: Float) {
floatSetting.setFloat(settings!!, selection)
floatSetting.setFloat(
settings!!,
BigDecimal((selection).toDouble()).round(MathContext(3)).toFloat()
)
}
}

View File

@ -16,7 +16,7 @@ class IntSliderSetting(
max: Int,
units: String?,
stepSize: Int
) : SliderSetting(context, titleId, descriptionId, min, max, units, stepSize) {
) : SliderSetting(context, titleId, descriptionId, min, max, units, stepSize, false) {
override val setting: AbstractSetting
get() = intSetting

View File

@ -4,24 +4,38 @@ package org.dolphinemu.dolphinemu.features.settings.model.view
import android.content.Context
import org.dolphinemu.dolphinemu.features.settings.model.AbstractFloatSetting
import org.dolphinemu.dolphinemu.features.settings.model.AbstractSetting
import org.dolphinemu.dolphinemu.features.settings.model.Settings
import kotlin.math.roundToInt
import java.math.BigDecimal
import java.math.MathContext
class PercentSliderSetting(
context: Context,
override val setting: AbstractFloatSetting,
titleId: Int,
descriptionId: Int,
min: Int,
max: Int,
min: Float,
max: Float,
units: String?,
stepSize: Int
) : FloatSliderSetting(context, setting, titleId, descriptionId, min, max, units, stepSize) {
override val selectedValue: Int
get() = (floatSetting.float * 100).roundToInt()
stepSize: Float,
showDecimal: Boolean
) : FloatSliderSetting(
context,
setting,
titleId,
descriptionId,
min,
max,
units,
stepSize,
showDecimal
) {
override val selectedValue: Float
get() = (floatSetting.float * 100)
override fun setSelectedValue(settings: Settings?, selection: Float) {
floatSetting.setFloat(settings!!, selection / 100)
floatSetting.setFloat(
settings!!,
BigDecimal((selection / 100).toDouble()).round(MathContext(3)).toFloat()
)
}
}

View File

@ -4,44 +4,50 @@ package org.dolphinemu.dolphinemu.features.settings.model.view
import android.content.Context
abstract class SliderSetting : SettingsItem {
sealed class SliderSetting : SettingsItem {
override val type: Int = TYPE_SLIDER
var min: Int
var min: Any
private set
var max: Int
var max: Any
private set
var units: String?
private set
var stepSize = 0
var stepSize: Any = 0
private set
var showDecimal: Boolean = false
private set
constructor(
context: Context,
nameId: Int,
descriptionId: Int,
min: Int,
max: Int,
min: Any,
max: Any,
units: String?,
stepSize: Int
stepSize: Any,
showDecimal: Boolean
) : super(context, nameId, descriptionId) {
this.min = min
this.max = max
this.units = units
this.stepSize = stepSize
this.showDecimal = showDecimal
}
constructor(
name: CharSequence,
description: CharSequence?,
min: Int,
max: Int,
units: String?
min: Any,
max: Any,
units: String?,
showDecimal: Boolean
) : super(name, description) {
this.min = min
this.max = max
this.units = units
this.showDecimal = showDecimal
}
abstract val selectedValue: Int
abstract val selectedValue: Any
}

View File

@ -19,6 +19,7 @@ enum class MenuTag {
DEBUG("debug"),
GRAPHICS("graphics"),
ENHANCEMENTS("enhancements"),
COLOR_CORRECTION("color_correction"),
STEREOSCOPY("stereoscopy"),
HACKS("hacks"),
STATISTICS("statistics"),

View File

@ -51,7 +51,7 @@ class SettingsAdapter(
private var settingsList: ArrayList<SettingsItem>? = null
private var clickedItem: SettingsItem? = null
private var clickedPosition: Int = -1
private var seekbarProgress = 0
private var seekbarProgress: Float = 0f
private var dialog: AlertDialog? = null
private var textSliderValue: TextView? = null
@ -229,21 +229,37 @@ class SettingsAdapter(
fun onSliderClick(item: SliderSetting, position: Int) {
clickedItem = item
clickedPosition = position
seekbarProgress = item.selectedValue
seekbarProgress = when (item) {
is FloatSliderSetting -> item.selectedValue
is IntSliderSetting -> item.selectedValue.toFloat()
}
val inflater = LayoutInflater.from(fragmentView.fragmentActivity)
val binding = DialogSliderBinding.inflate(inflater)
textSliderValue = binding.textValue
textSliderValue!!.text = seekbarProgress.toString()
textSliderValue!!.text = if (item.showDecimal) {
String.format("%.2f", seekbarProgress)
} else {
seekbarProgress.toInt().toString()
}
binding.textUnits.text = item.units
val slider = binding.slider
slider.valueFrom = item.min.toFloat()
slider.valueTo = item.max.toFloat()
slider.value = seekbarProgress.toFloat()
slider.stepSize = item.stepSize.toFloat()
when (item) {
is FloatSliderSetting -> {
slider.valueFrom = item.min as Float
slider.valueTo = item.max as Float
slider.stepSize = item.stepSize as Float
}
is IntSliderSetting -> {
slider.valueFrom = (item.min as Int).toFloat()
slider.valueTo = (item.max as Int).toFloat()
slider.stepSize = (item.stepSize as Int).toFloat()
}
}
slider.value = seekbarProgress
slider.addOnChangeListener(this)
dialog = MaterialAlertDialogBuilder(fragmentView.fragmentActivity)
@ -480,8 +496,10 @@ class SettingsAdapter(
}
is IntSliderSetting -> {
val sliderSetting = clickedItem as IntSliderSetting
if (sliderSetting.selectedValue != seekbarProgress) fragmentView.onSettingChanged()
sliderSetting.setSelectedValue(settings, seekbarProgress)
if (sliderSetting.selectedValue != seekbarProgress.toInt()) {
fragmentView.onSettingChanged()
}
sliderSetting.setSelectedValue(settings, seekbarProgress.toInt())
closeDialog()
}
is FloatSliderSetting -> {
@ -489,13 +507,13 @@ class SettingsAdapter(
if (sliderSetting.selectedValue != seekbarProgress) fragmentView.onSettingChanged()
sliderSetting.setSelectedValue(settings, seekbarProgress.toFloat())
sliderSetting.setSelectedValue(settings, seekbarProgress)
closeDialog()
}
}
clickedItem = null
seekbarProgress = -1
seekbarProgress = -1f
}
fun closeDialog() {
@ -510,8 +528,12 @@ class SettingsAdapter(
}
override fun onValueChange(slider: Slider, progress: Float, fromUser: Boolean) {
seekbarProgress = progress.toInt()
textSliderValue!!.text = seekbarProgress.toString()
seekbarProgress = progress
textSliderValue!!.text = if ((clickedItem as SliderSetting).showDecimal) {
String.format("%.2f", seekbarProgress)
} else {
seekbarProgress.toInt().toString()
}
}
private fun getValueForSingleChoiceSelection(item: SingleChoiceSetting, which: Int): Int {

View File

@ -34,6 +34,7 @@ import org.dolphinemu.dolphinemu.model.GpuDriverMetadata
import org.dolphinemu.dolphinemu.ui.main.MainPresenter
import org.dolphinemu.dolphinemu.utils.*
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.ceil
import kotlin.math.floor
@ -119,6 +120,7 @@ class SettingsFragmentPresenter(
MenuTag.GCPAD_TYPE -> addGcPadSettings(sl)
MenuTag.WIIMOTE -> addWiimoteSettings(sl)
MenuTag.ENHANCEMENTS -> addEnhanceSettings(sl)
MenuTag.COLOR_CORRECTION -> addColorCorrectionSettings(sl)
MenuTag.STEREOSCOPY -> addStereoSettings(sl)
MenuTag.HACKS -> addHackSettings(sl)
MenuTag.STATISTICS -> addStatisticsSettings(sl)
@ -249,10 +251,11 @@ class SettingsFragmentPresenter(
FloatSetting.MAIN_EMULATION_SPEED,
R.string.speed_limit,
0,
0,
200,
0f,
200f,
"%",
1
1f,
false
)
)
sl.add(
@ -1000,10 +1003,11 @@ class SettingsFragmentPresenter(
FloatSetting.MAIN_OVERCLOCK,
R.string.overclock_title,
R.string.overclock_title_description,
0,
400,
0f,
400f,
"%",
1
1f,
false
)
)
@ -2349,9 +2353,10 @@ class SettingsFragmentPresenter(
InputMappingDoubleSetting(setting),
setting.getUiName(),
"",
ceil(setting.getDoubleMin()).toInt(),
floor(setting.getDoubleMax()).toInt(),
setting.getUiSuffix()
ceil(setting.getDoubleMin()).toFloat(),
floor(setting.getDoubleMax()).toFloat(),
setting.getUiSuffix(),
false
)
)

View File

@ -7,6 +7,7 @@ import android.text.TextUtils
import android.view.View
import org.dolphinemu.dolphinemu.R
import org.dolphinemu.dolphinemu.databinding.ListItemSettingBinding
import org.dolphinemu.dolphinemu.features.settings.model.view.IntSliderSetting
import org.dolphinemu.dolphinemu.features.settings.model.view.SettingsItem
import org.dolphinemu.dolphinemu.features.settings.model.view.SliderSetting
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsAdapter
@ -14,7 +15,7 @@ import org.dolphinemu.dolphinemu.features.settings.ui.SettingsAdapter
class SliderViewHolder(
private val binding: ListItemSettingBinding, adapter: SettingsAdapter?,
private val context: Context
) : SettingViewHolder(binding.getRoot(), adapter!!) {
) : SettingViewHolder(binding.root, adapter!!) {
private lateinit var setting: SliderSetting
override val item: SettingsItem
@ -28,11 +29,25 @@ class SliderViewHolder(
if (!TextUtils.isEmpty(item.description)) {
binding.textSettingDescription.text = item.description
} else {
binding.textSettingDescription.text = context.getString(
R.string.slider_setting_value,
setting.selectedValue,
setting.units
)
val selectedValue: Float = if (item is IntSliderSetting) {
(setting.selectedValue as Int).toFloat()
} else {
setting.selectedValue as Float
}
if (setting.showDecimal) {
binding.textSettingDescription.text = String.format(
context.getString(R.string.slider_setting_float_value),
selectedValue,
setting.units
)
} else {
binding.textSettingDescription.text = String.format(
context.getString(R.string.slider_setting_value),
selectedValue,
setting.units
)
}
}
setStyle(binding.textSettingName, setting)

View File

@ -632,7 +632,8 @@ It can efficiently compress both junk data and encrypted Wii data.
<!-- Misc -->
<string name="enabled">Enabled</string>
<string name="default_values">Default Values</string>
<string name="slider_setting_value">%1$d%2$s</string>
<string name="slider_setting_value">%.0f%2$s</string>
<string name="slider_setting_float_value">%.2f%2$s</string>
<string name="disc_number">Disc %1$d</string>
<string name="replug_gc_adapter">GameCube Adapter couldn\'t be opened. Please re-plug the device.</string>
<string name="disabled_gc_overlay_notice">The selected GameCube controller is set to \"None\"</string>