2019-06-24 01:55:42 +00:00
|
|
|
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.
|
|
|
|
*/
|
2019-06-24 02:21:59 +00:00
|
|
|
class EnumButton<E: Enum<E>>(initialValue: E, val localizer: (E) -> String): AbstractButton<EnumButton<E>>(Label(localizer(initialValue))) {
|
2019-06-24 01:55:42 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-24 02:34:12 +00:00
|
|
|
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
|
2019-06-24 01:55:42 +00:00
|
|
|
if (!disabled) {
|
|
|
|
value = when (mouseButton) {
|
|
|
|
MouseButton.LEFT -> EnumHelper.next(value)
|
|
|
|
MouseButton.RIGHT -> EnumHelper.previous(value)
|
|
|
|
else -> value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 02:34:12 +00:00
|
|
|
return super.mouseClicked(point, mouseButton)
|
2019-06-24 01:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|