Add EnumButton

This commit is contained in:
Shadowfacts 2021-02-24 22:47:07 -05:00
parent 9252a35dae
commit 1f078c671f
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 66 additions and 12 deletions

View File

@ -7,6 +7,7 @@ import net.minecraft.client.util.math.MatrixStack
import net.shadowfacts.phycon.network.DeviceBlockEntity import net.shadowfacts.phycon.network.DeviceBlockEntity
import net.shadowfacts.phycon.network.component.ActivationController import net.shadowfacts.phycon.network.component.ActivationController
import net.shadowfacts.phycon.networking.C2SConfigureActivationMode import net.shadowfacts.phycon.networking.C2SConfigureActivationMode
import net.shadowfacts.phycon.util.ActivationMode
import net.shadowfacts.phycon.util.next import net.shadowfacts.phycon.util.next
import org.lwjgl.glfw.GLFW import org.lwjgl.glfw.GLFW
@ -28,10 +29,7 @@ class ActivatableDeviceConsoleScreen<T>(
val minX = (width - backgroundWidth) / 2 val minX = (width - backgroundWidth) / 2
val minY = (height - backgroundHeight) / 2 val minY = (height - backgroundHeight) / 2
lateinit var mode: ButtonWidget val mode = EnumButton(device.controller::activationMode, minX + 5, minY + 25, 55, 20) {
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
client!!.player!!.networkHandler.sendPacket(C2SConfigureActivationMode(device)) client!!.player!!.networkHandler.sendPacket(C2SConfigureActivationMode(device))
} }
addButton(mode) addButton(mode)

View File

@ -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<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()
}
}

View File

@ -33,10 +33,7 @@ class RedstoneControllerConsoleScreen(
val minX = (width - backgroundWidth) / 2 val minX = (width - backgroundWidth) / 2
val minY = (height - backgroundHeight) / 2 val minY = (height - backgroundHeight) / 2
lateinit var mode: ButtonWidget val mode = EnumButton(device::redstoneMode, minX + 5, minY + 25, 75, 20) {
mode = ButtonWidget(minX + 5, minY + 25, 75, 20, device.redstoneMode.friendlyName) {
device.redstoneMode = device.redstoneMode.next
mode.message = device.redstoneMode.friendlyName
client!!.player!!.networkHandler.sendPacket(C2SConfigureRedstoneController(device)) client!!.player!!.networkHandler.sendPacket(C2SConfigureRedstoneController(device))
} }
addButton(mode) addButton(mode)

View File

@ -6,10 +6,10 @@ import net.minecraft.text.TranslatableText
/** /**
* @author shadowfacts * @author shadowfacts
*/ */
enum class ActivationMode: RotatableEnum { enum class ActivationMode: RotatableEnum, FriendlyNameable {
AUTOMATIC, AUTOMATIC,
MANAGED; MANAGED;
val friendlyName: Text override val friendlyName: Text
get() = TranslatableText("gui.phycon.activation_mode.${name.toLowerCase()}") get() = TranslatableText("gui.phycon.activation_mode.${name.toLowerCase()}")
} }

View File

@ -0,0 +1,10 @@
package net.shadowfacts.phycon.util
import net.minecraft.text.Text
/**
* @author shadowfacts
*/
interface FriendlyNameable {
val friendlyName: Text
}

View File

@ -6,7 +6,7 @@ import net.minecraft.text.TranslatableText
/** /**
* @author shadowfacts * @author shadowfacts
*/ */
enum class RedstoneMode: RotatableEnum { enum class RedstoneMode: RotatableEnum, FriendlyNameable {
HIGH, HIGH,
LOW, LOW,
TOGGLE, TOGGLE,
@ -25,6 +25,6 @@ enum class RedstoneMode: RotatableEnum {
else -> false else -> false
} }
val friendlyName: Text override val friendlyName: Text
get() = TranslatableText("gui.phycon.redstone_mode.${name.toLowerCase()}") get() = TranslatableText("gui.phycon.redstone_mode.${name.toLowerCase()}")
} }