diff --git a/src/main/kotlin/net/shadowfacts/phycon/screen/ActivatableDeviceConsoleScreen.kt b/src/main/kotlin/net/shadowfacts/phycon/screen/ActivatableDeviceConsoleScreen.kt index dedec57..b1ffa81 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/screen/ActivatableDeviceConsoleScreen.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/screen/ActivatableDeviceConsoleScreen.kt @@ -7,6 +7,7 @@ import net.minecraft.client.util.math.MatrixStack import net.shadowfacts.phycon.network.DeviceBlockEntity import net.shadowfacts.phycon.network.component.ActivationController import net.shadowfacts.phycon.networking.C2SConfigureActivationMode +import net.shadowfacts.phycon.util.ActivationMode import net.shadowfacts.phycon.util.next import org.lwjgl.glfw.GLFW @@ -28,10 +29,7 @@ class ActivatableDeviceConsoleScreen( val minX = (width - backgroundWidth) / 2 val minY = (height - backgroundHeight) / 2 - lateinit var mode: ButtonWidget - mode = ButtonWidget(minX + 5, minY + 25, 55, 20, device.controller.activationMode.friendlyName) { - device.controller.activationMode = device.controller.activationMode.next - mode.message = device.controller.activationMode.friendlyName + val mode = EnumButton(device.controller::activationMode, minX + 5, minY + 25, 55, 20) { client!!.player!!.networkHandler.sendPacket(C2SConfigureActivationMode(device)) } addButton(mode) diff --git a/src/main/kotlin/net/shadowfacts/phycon/screen/EnumButton.kt b/src/main/kotlin/net/shadowfacts/phycon/screen/EnumButton.kt new file mode 100644 index 0000000..79e2608 --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/screen/EnumButton.kt @@ -0,0 +1,49 @@ +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( + val prop: KMutableProperty, + x: Int, + y: Int, + width: Int, + height: Int, + val onChange: () -> Unit +): AbstractPressableButtonWidget( + x, + y, + width, + height, + prop.getter.call().friendlyName +) where E: Enum, 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() + } + +} diff --git a/src/main/kotlin/net/shadowfacts/phycon/screen/RedstoneControllerConsoleScreen.kt b/src/main/kotlin/net/shadowfacts/phycon/screen/RedstoneControllerConsoleScreen.kt index 1a6bea6..9f30544 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/screen/RedstoneControllerConsoleScreen.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/screen/RedstoneControllerConsoleScreen.kt @@ -33,10 +33,7 @@ class RedstoneControllerConsoleScreen( val minX = (width - backgroundWidth) / 2 val minY = (height - backgroundHeight) / 2 - lateinit var mode: ButtonWidget - mode = ButtonWidget(minX + 5, minY + 25, 75, 20, device.redstoneMode.friendlyName) { - device.redstoneMode = device.redstoneMode.next - mode.message = device.redstoneMode.friendlyName + val mode = EnumButton(device::redstoneMode, minX + 5, minY + 25, 75, 20) { client!!.player!!.networkHandler.sendPacket(C2SConfigureRedstoneController(device)) } addButton(mode) diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/ActivationMode.kt b/src/main/kotlin/net/shadowfacts/phycon/util/ActivationMode.kt index 3ab9b3d..2db1810 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/util/ActivationMode.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/util/ActivationMode.kt @@ -6,10 +6,10 @@ import net.minecraft.text.TranslatableText /** * @author shadowfacts */ -enum class ActivationMode: RotatableEnum { +enum class ActivationMode: RotatableEnum, FriendlyNameable { AUTOMATIC, MANAGED; - val friendlyName: Text + override val friendlyName: Text get() = TranslatableText("gui.phycon.activation_mode.${name.toLowerCase()}") } diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/FriendlyNameable.kt b/src/main/kotlin/net/shadowfacts/phycon/util/FriendlyNameable.kt new file mode 100644 index 0000000..978d21a --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/phycon/util/FriendlyNameable.kt @@ -0,0 +1,10 @@ +package net.shadowfacts.phycon.util + +import net.minecraft.text.Text + +/** + * @author shadowfacts + */ +interface FriendlyNameable { + val friendlyName: Text +} diff --git a/src/main/kotlin/net/shadowfacts/phycon/util/RedstoneMode.kt b/src/main/kotlin/net/shadowfacts/phycon/util/RedstoneMode.kt index a7882bf..0fe895a 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/util/RedstoneMode.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/util/RedstoneMode.kt @@ -6,7 +6,7 @@ import net.minecraft.text.TranslatableText /** * @author shadowfacts */ -enum class RedstoneMode: RotatableEnum { +enum class RedstoneMode: RotatableEnum, FriendlyNameable { HIGH, LOW, TOGGLE, @@ -25,6 +25,6 @@ enum class RedstoneMode: RotatableEnum { else -> false } - val friendlyName: Text + override val friendlyName: Text get() = TranslatableText("gui.phycon.redstone_mode.${name.toLowerCase()}") }