Add EnumValue to possible Feature values

main
Charlotte Som 2022-03-08 00:03:54 +00:00
parent a1ea8ca51e
commit 135fb96f0b
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package codes.som.hibiscus.api.feature.values
import imgui.ImGui
import imgui.type.ImInt
class EnumValue<E : Enum<E>>(name: String, value: E) : RegisteredValue<E>(name, value) {
override fun convertValueFromString(representation: String): E {
return value
.javaClass
.enumConstants
.first { it.name.equals(representation, ignoreCase = true) }
}
override fun drawUIControl() {
val selectedValue = ImInt(value.ordinal ?: -1)
ImGui.combo(name, selectedValue, value.javaClass.enumConstants.map { it.name }.toTypedArray())
value = value.javaClass.enumConstants[selectedValue.get()]
}
}
class OptionalEnumValue<E : Enum<E>>(
name: String,
private val enumType: Class<E>,
value: E?
) : RegisteredValue<E?>(name, value) {
override fun convertValueFromString(representation: String): E {
return enumType.enumConstants.first { it.name.equals(representation, ignoreCase = true) }
}
override fun drawUIControl() {
val selectedValue = ImInt(value?.ordinal ?: -1)
ImGui.combo(name, selectedValue, arrayOf("<none>") + enumType.enumConstants.map { it.name }.toTypedArray())
value = enumType.enumConstants.getOrNull(selectedValue.get() - 1)
}
}

View File

@ -26,4 +26,13 @@ class ValueRegistry {
fun float(name: String, default: Float, min: Float = Float.MIN_VALUE, max: Float = Float.MAX_VALUE) = fun float(name: String, default: Float, min: Float = Float.MIN_VALUE, max: Float = Float.MAX_VALUE) =
FloatValue(name, default, min, max).also(this::register) FloatValue(name, default, min, max).also(this::register)
fun <E : Enum<E>> enum(name: String, default: E) =
EnumValue(name, default)
fun <E : Enum<E>> optionalEnum(name: String, type: Class<E>, default: E?) =
OptionalEnumValue(name, type, default)
inline fun <reified E : Enum<E>> optionalEnum(name: String, default: E?) =
optionalEnum(name, E::class.java, default)
} }