50 lines
1.1 KiB
Kotlin
50 lines
1.1 KiB
Kotlin
|
package net.shadowfacts.phycon.screen
|
||
|
|
||
|
import net.minecraft.client.gui.widget.AbstractPressableButtonWidget
|
||
|
import net.minecraft.text.Text
|
||
|
import net.shadowfacts.phycon.util.FriendlyNameable
|
||
|
import net.shadowfacts.phycon.util.RotatableEnum
|
||
|
import net.shadowfacts.phycon.util.next
|
||
|
import net.shadowfacts.phycon.util.prev
|
||
|
import kotlin.reflect.KMutableProperty
|
||
|
|
||
|
/**
|
||
|
* @author shadowfacts
|
||
|
*/
|
||
|
class EnumButton<E>(
|
||
|
val prop: KMutableProperty<E>,
|
||
|
x: Int,
|
||
|
y: Int,
|
||
|
width: Int,
|
||
|
height: Int,
|
||
|
val onChange: () -> Unit
|
||
|
): AbstractPressableButtonWidget(
|
||
|
x,
|
||
|
y,
|
||
|
width,
|
||
|
height,
|
||
|
prop.getter.call().friendlyName
|
||
|
) where E: Enum<E>, E: RotatableEnum, E: FriendlyNameable {
|
||
|
|
||
|
private var currentButton: Int? = null
|
||
|
|
||
|
override fun mouseClicked(d: Double, e: Double, button: Int): Boolean {
|
||
|
currentButton = button
|
||
|
val res = super.mouseClicked(d, e, button)
|
||
|
currentButton = null
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
override fun isValidClickButton(i: Int): Boolean {
|
||
|
return i == 0 || i == 1
|
||
|
}
|
||
|
|
||
|
override fun onPress() {
|
||
|
val newVal = if ((currentButton ?: 0) == 0) prop.getter.call().next else prop.getter.call().prev
|
||
|
prop.setter.call(newVal)
|
||
|
message = newVal.friendlyName
|
||
|
onChange()
|
||
|
}
|
||
|
|
||
|
}
|