PhysicalConnectivity/src/main/kotlin/net/shadowfacts/phycon/client/screen/console/ProviderViewController.kt

101 lines
2.9 KiB
Kotlin
Raw Normal View History

2021-03-26 02:15:46 +00:00
package net.shadowfacts.phycon.client.screen.console
2021-03-04 03:00:21 +00:00
import net.minecraft.block.entity.BlockEntity
import net.minecraft.client.MinecraftClient
import net.minecraft.text.TranslatableText
import net.shadowfacts.cacao.util.Color
import net.shadowfacts.cacao.view.Label
2021-03-05 00:44:31 +00:00
import net.shadowfacts.cacao.view.button.ToggleButton
2021-03-04 03:00:21 +00:00
import net.shadowfacts.cacao.view.textfield.NumberField
import net.shadowfacts.cacao.viewcontroller.ViewController
import net.shadowfacts.kiwidsl.dsl
2021-03-05 00:44:31 +00:00
import net.shadowfacts.phycon.block.netinterface.InterfaceBlockEntity
2021-03-04 03:00:21 +00:00
import net.shadowfacts.phycon.component.NetworkStackProvider
import net.shadowfacts.phycon.networking.C2SConfigureDevice
/**
* @author shadowfacts
*/
class ProviderViewController<T>(
private val device: T
): ViewController() where T: BlockEntity, T: NetworkStackProvider {
2021-03-05 00:44:31 +00:00
private lateinit var field: NumberField
private var syncButton: ToggleButton? = null
2021-03-04 03:00:21 +00:00
override fun viewDidLoad() {
super.viewDidLoad()
val label = Label(TranslatableText("gui.phycon.console.provider.priority")).apply {
textColor = Color.TEXT
}
view.addSubview(label)
2021-03-05 00:44:31 +00:00
field = NumberField(device.providerPriority) {
2021-03-04 03:00:21 +00:00
if (it.number != null) {
device.providerPriority = it.number!!
2021-03-05 00:44:31 +00:00
if (device is InterfaceBlockEntity && device.syncPriorities) {
device.receiverPriority = it.number!!
}
2021-03-04 03:00:21 +00:00
MinecraftClient.getInstance().player!!.networkHandler.sendPacket(C2SConfigureDevice(device))
}
}
view.addSubview(field)
val desc = Label(TranslatableText("gui.phycon.console.provider.priority_desc")).apply {
textColor = Color.TEXT
}
view.addSubview(desc)
2021-03-05 00:44:31 +00:00
if (device is InterfaceBlockEntity) {
val syncLabel = Label(TranslatableText("gui.phycon.console.provider.sync")).apply {
textColor = Color.TEXT
textAlignment = Label.TextAlignment.RIGHT
}
view.addSubview(syncLabel)
val syncButton = ToggleButton(device.syncPriorities) {
device.syncPriorities = it.state
device.receiverPriority = device.providerPriority
}
this.syncButton = syncButton
view.addSubview(syncButton)
view.solver.dsl {
syncButton.topAnchor equalTo (desc.bottomAnchor + 4)
syncButton.leftAnchor equalTo field.leftAnchor
syncLabel.centerYAnchor equalTo syncButton.centerYAnchor
syncLabel.leftAnchor equalTo view.leftAnchor
syncLabel.rightAnchor equalTo (syncButton.leftAnchor - 4)
}
}
2021-03-04 03:00:21 +00:00
view.solver.dsl {
field.widthAnchor equalTo 100
field.heightAnchor equalTo 20
field.rightAnchor equalTo view.rightAnchor
field.topAnchor equalTo view.topAnchor
label.centerYAnchor equalTo field.centerYAnchor
label.rightAnchor equalTo (field.leftAnchor - 4)
desc.topAnchor equalTo (field.bottomAnchor + 4)
desc.leftAnchor equalTo view.leftAnchor
desc.rightAnchor equalTo view.rightAnchor
}
}
2021-03-05 00:44:31 +00:00
override fun viewWillAppear() {
super.viewWillAppear()
field.number = device.providerPriority
if (device is InterfaceBlockEntity) {
syncButton!!.state = device.syncPriorities
}
}
2021-03-04 03:00:21 +00:00
}