better screen building

This commit is contained in:
nea
2022-08-07 22:30:28 +02:00
parent 05a4a5b381
commit dc4755eb79
6 changed files with 144 additions and 112 deletions

10
.editorconfig Normal file
View File

@@ -0,0 +1,10 @@
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 120
[{*.kt,*.kts}]

View File

@@ -1,6 +1,7 @@
- translations
- recipes
- fairy souls
- easy config gui builder
- and much more that i will add as i go along
- and much more that i will add as i go along

View File

@@ -10,7 +10,7 @@ import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.coroutines.*
import kotlinx.serialization.json.Json
import moe.nea.notenoughupdates.gui.RepoManagementGui
import moe.nea.notenoughupdates.gui.repoGui
import moe.nea.notenoughupdates.repo.RepoManager
import moe.nea.notenoughupdates.util.ConfigHolder
import moe.nea.notenoughupdates.util.ScreenUtil.setScreenLater
@@ -80,7 +80,7 @@ object NotEnoughUpdates : ModInitializer, ClientModInitializer {
dispatcher.register(
ClientCommandManager.literal("neu")
.then(ClientCommandManager.literal("repo").executes {
setScreenLater(CottonClientScreen(RepoManagementGui()))
setScreenLater(CottonClientScreen(repoGui()))
Command.SINGLE_SUCCESS
})
)

View File

@@ -0,0 +1,94 @@
package moe.nea.notenoughupdates.gui
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription
import io.github.cottonmc.cotton.gui.widget.WButton
import io.github.cottonmc.cotton.gui.widget.WLabel
import io.github.cottonmc.cotton.gui.widget.WTextField
import io.github.cottonmc.cotton.gui.widget.WToggleButton
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment
import io.github.cottonmc.cotton.gui.widget.data.Insets
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
import moe.nea.notenoughupdates.NotEnoughUpdates
import moe.nea.notenoughupdates.util.ConfigHolder
import net.minecraft.network.chat.Component
import kotlin.reflect.KMutableProperty1
class ConfigGui<K>(val holder: ConfigHolder<K>, val build: ConfigGui<K>.() -> Unit) : LightweightGuiDescription() {
private val root = WGridPanelWithPadding(verticalPadding = 4)
private val reloadables = mutableListOf<(() -> Unit)>()
init {
setRootPanel(root)
root.insets = Insets.ROOT_PANEL
build()
reload()
}
fun title(component: Component) {
if (col != 0) {
NotEnoughUpdates.logger.warn("Set title not at the top of the ConfigGui")
}
val label = WLabel(component)
label.verticalAlignment = VerticalAlignment.TOP
label.horizontalAlignment = HorizontalAlignment.CENTER
root.add(label, 0, col, 11, 1)
col++
}
private fun label(component: Component) {
val label = WLabel(component)
label.verticalAlignment = VerticalAlignment.CENTER
root.add(label, 0, col, 5, 1)
}
fun toggle(component: Component, prop: KMutableProperty1<K, Boolean>) {
val toggle = WToggleButton(component)
reloadables.add { toggle.toggle = prop.get(holder.config) }
toggle.setOnToggle {
prop.set(holder.config, true)
holder.markDirty()
}
root.add(toggle, 5, col, 6, 1)
label(component)
col++
}
fun button(component: Component, buttonText: Component, runnable: () -> Unit) {
val button = WButton(buttonText)
button.setOnClick {
runnable.invoke()
}
root.add(button, 5, col, 6, 1)
label(component)
col++
}
fun textfield(
component: Component,
background: Component,
prop: KMutableProperty1<K, String>,
maxLength: Int = 255
) {
val textfield = WTextField(background)
textfield.isEditable = true
reloadables.add {
textfield.text = prop.get(holder.config)
}
textfield.maxLength = maxLength
textfield.setChangedListener {
prop.set(holder.config, it)
holder.markDirty()
}
root.add(textfield, 5, col, 6, 11)
label(component)
col++
}
fun reload() {
reloadables.forEach { it.invoke() }
}
private var col = 0
}

View File

@@ -1,109 +0,0 @@
package moe.nea.notenoughupdates.gui
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription
import io.github.cottonmc.cotton.gui.widget.WButton
import io.github.cottonmc.cotton.gui.widget.WLabel
import io.github.cottonmc.cotton.gui.widget.WTextField
import io.github.cottonmc.cotton.gui.widget.WToggleButton
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment
import io.github.cottonmc.cotton.gui.widget.data.Insets
import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment
import moe.nea.notenoughupdates.repo.RepoManager
import net.minecraft.network.chat.Component
import java.util.function.Consumer
class RepoManagementGui : LightweightGuiDescription() {
init {
val root = WGridPanelWithPadding(verticalPadding = 5)
setRootPanel(root)
root.setSize(0, 0)
root.insets = Insets.ROOT_PANEL
var col = 0
WLabel(Component.literal("NotEnoughUpdates Repo Settings")).apply {
root.add(this, 0, col, 11, 1)
this.verticalAlignment = VerticalAlignment.TOP
this.horizontalAlignment = HorizontalAlignment.CENTER
}
col += 1
WLabel(Component.literal("Auto Update")).apply {
root.add(this, 0, col, 5, 1)
this.verticalAlignment = VerticalAlignment.CENTER
}
WToggleButton(Component.literal("Auto Update")).apply {
this.toggle = RepoManager.config.autoUpdate
this.onToggle = Consumer {
RepoManager.config.autoUpdate = it
RepoManager.markDirty()
}
root.add(this, 5, col, 1, 1)
}
col += 1
WLabel(Component.literal("Repo Username")).apply {
root.add(this, 0, col, 5, 1)
this.verticalAlignment = VerticalAlignment.CENTER
}
val userName = WTextField(Component.literal("username")).apply {
this.isEditable = true
this.text = RepoManager.config.user
this.setChangedListener {
RepoManager.config.user = it
RepoManager.markDirty()
}
root.add(this, 5, col, 6, 1)
}
col += 1
WLabel(Component.literal("Repo Name")).apply {
root.add(this, 0, col, 5, 1)
this.verticalAlignment = VerticalAlignment.CENTER
}
val repoName = WTextField(Component.literal("repo name")).apply {
this.isEditable = true
this.text = RepoManager.config.repo
this.setChangedListener {
RepoManager.config.repo = it
RepoManager.markDirty()
}
root.add(this, 5, col, 6, 1)
}
col += 1
WLabel(Component.literal("Repo Branch")).apply {
root.add(this, 0, col, 5, 1)
this.verticalAlignment = VerticalAlignment.CENTER
}
val branchName = WTextField(Component.literal("repo branch")).apply {
this.isEditable = true
this.text = RepoManager.config.branch
this.setChangedListener {
RepoManager.config.branch = it
RepoManager.markDirty()
}
root.add(this, 5, col, 6, 1)
}
col += 1
WLabel(Component.literal("Reset to Defaults")).apply {
root.add(this, 0, col, 5, 1)
this.verticalAlignment = VerticalAlignment.CENTER
}
WButton(Component.literal("Reset")).apply {
this.setOnClick {
branchName.text = "master"
userName.text = "NotEnoughUpdates"
repoName.text = "NotEnoughUpdates-REPO"
RepoManager.markDirty()
}
root.add(this, 5, col, 6, 1)
}
}
}

View File

@@ -0,0 +1,36 @@
package moe.nea.notenoughupdates.gui
import moe.nea.notenoughupdates.repo.RepoManager
import net.minecraft.network.chat.Component
fun repoGui(): ConfigGui<RepoManager.Config> {
return ConfigGui(RepoManager) {
title(Component.literal("NotEnoughUpdates Repo Settings"))
toggle(Component.literal("Auto Update"), RepoManager.Config::autoUpdate)
textfield(
Component.literal("Repo Username"),
Component.literal("<github user>"),
RepoManager.Config::user,
maxLength = 255
)
textfield(
Component.literal("Repo Name"),
Component.literal("<repo name>"),
RepoManager.Config::repo
)
textfield(
Component.literal("Repo Branch"),
Component.literal("<repo branch>"),
RepoManager.Config::branch
)
button(
Component.literal("Reset to Defaults"),
Component.literal("Reset"),
) {
RepoManager.config.user = "NotEnoughUpdates"
RepoManager.config.repo = "NotEnoughUpdates-REPO"
RepoManager.config.branch = "dangerous"
reload()
}
}
}