45 lines
1.3 KiB
Kotlin
45 lines
1.3 KiB
Kotlin
package net.shadowfacts.cacao.view.button
|
|
|
|
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 string for the button's label.
|
|
*/
|
|
class EnumButton<E: Enum<E>>(initialValue: E, val localizer: (E) -> String, padding: Double = 4.0): AbstractButton<EnumButton<E>>(Label(localizer(initialValue)), padding) {
|
|
|
|
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) {
|
|
if (!disabled) {
|
|
value = when (mouseButton) {
|
|
MouseButton.LEFT -> EnumHelper.next(value)
|
|
MouseButton.RIGHT -> EnumHelper.previous(value)
|
|
else -> value
|
|
}
|
|
}
|
|
|
|
super.mouseClicked(point, mouseButton)
|
|
}
|
|
|
|
} |