54 lines
1.4 KiB
Kotlin
54 lines
1.4 KiB
Kotlin
package net.shadowfacts.cacao.view.button
|
|
|
|
import net.minecraft.text.Text
|
|
import net.shadowfacts.cacao.geometry.Point
|
|
import net.shadowfacts.cacao.util.EnumHelper
|
|
import net.shadowfacts.cacao.util.MouseButton
|
|
import net.shadowfacts.cacao.view.Label
|
|
|
|
/**
|
|
* A button that cycles through enum values.
|
|
* Left click: forwards
|
|
* Right click: backwards
|
|
* All other mouse buttons call the handler with the unchanged value
|
|
*
|
|
* @author shadowfacts
|
|
* @param initialValue The initial enum value for this button.
|
|
* @param localizer A function that takes an enum value and converts into a [Text] for the button's label.
|
|
*/
|
|
class EnumButton<E: Enum<E>>(
|
|
initialValue: E,
|
|
val localizer: (E) -> Text
|
|
): AbstractButton<EnumButton<E>>(
|
|
Label(localizer(initialValue), shadow = true)
|
|
) {
|
|
|
|
private val label: Label
|
|
get() = content as Label
|
|
|
|
/**
|
|
* The current value of the enum button.
|
|
* Updating this property will use the [localizer] to update the label.
|
|
*/
|
|
var value: E = initialValue
|
|
set(value) {
|
|
field = value
|
|
label.text = localizer(value)
|
|
}
|
|
|
|
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
|
|
if (!disabled) {
|
|
value = when (mouseButton) {
|
|
MouseButton.LEFT -> EnumHelper.next(value)
|
|
MouseButton.RIGHT -> EnumHelper.previous(value)
|
|
else -> {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
return super.mouseClicked(point, mouseButton)
|
|
}
|
|
|
|
}
|