diff options
author | nea <romangraef@gmail.com> | 2022-08-07 22:30:28 +0200 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-08-07 22:30:28 +0200 |
commit | dc4755eb79ab78f87b0e1e4ad6dac81912af0ee0 (patch) | |
tree | e3819f403e63fd79af08cccbf0e6c0c581336497 /src/main | |
parent | 05a4a5b3817ea3e70662d3834fd56a1b249fa70e (diff) | |
download | firmament-dc4755eb79ab78f87b0e1e4ad6dac81912af0ee0.tar.gz firmament-dc4755eb79ab78f87b0e1e4ad6dac81912af0ee0.tar.bz2 firmament-dc4755eb79ab78f87b0e1e4ad6dac81912af0ee0.zip |
better screen building
Diffstat (limited to 'src/main')
4 files changed, 132 insertions, 111 deletions
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt b/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt index a7a8a19..86baa8a 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt @@ -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 }) ) diff --git a/src/main/kotlin/moe/nea/notenoughupdates/gui/ConfigGui.kt b/src/main/kotlin/moe/nea/notenoughupdates/gui/ConfigGui.kt new file mode 100644 index 0000000..c6c2420 --- /dev/null +++ b/src/main/kotlin/moe/nea/notenoughupdates/gui/ConfigGui.kt @@ -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 + + +} diff --git a/src/main/kotlin/moe/nea/notenoughupdates/gui/RepoManagementGui.kt b/src/main/kotlin/moe/nea/notenoughupdates/gui/RepoManagementGui.kt deleted file mode 100644 index ecdb05b..0000000 --- a/src/main/kotlin/moe/nea/notenoughupdates/gui/RepoManagementGui.kt +++ /dev/null @@ -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) - } - } -}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/notenoughupdates/gui/repogui.kt b/src/main/kotlin/moe/nea/notenoughupdates/gui/repogui.kt new file mode 100644 index 0000000..40301fd --- /dev/null +++ b/src/main/kotlin/moe/nea/notenoughupdates/gui/repogui.kt @@ -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() + } + } +} |