Improve the configuration screen

This commit is contained in:
Username404 2021-05-31 18:30:09 +02:00
parent 8d9016153e
commit 4144d3bd5a
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
4 changed files with 42 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.gui.FontUtil
import fr.username404.snowygui.gui.elements.ClickBox
import fr.username404.snowygui.gui.elements.ClickBox.Companion.buttonsMax
import fr.username404.snowygui.gui.feature.Category
import fr.username404.snowygui.gui.feature.Colors
import fr.username404.snowygui.gui.feature.Macro
@ -11,19 +12,25 @@ import fr.username404.snowygui.gui.feature.riskyCheatsEnabled
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.TranslatableComponent
import java.util.*
private const val confPrefix: String = "screen.snowy.config"
private val translationComponent = TranslatableComponent(confPrefix)
var configScreenParent: Screen? = null
private fun supplyComponent(string: String?): Optional<Component> = string?.run {
Optional.of(TranslatableComponent(string))
} ?: Optional.empty()
val SnowyConfigScreen: Screen get() {
return try {
Class.forName("me.shedaniel.clothconfig2.api.ConfigBuilder")
val macrosBox: ClickBox = ClickGui.components.find {
(it is ClickBox) && it.isCategory(Category.MACROS)
} as ClickBox
val macrosButtons = macrosBox.buttons
@Suppress("UNCHECKED_CAST")
val macrosButtons = macrosBox.buttons as MutableSet<Macro>
me.shedaniel.clothconfig2.api.ConfigBuilder.create().setParentScreen(configScreenParent).transparentBackground()
.setShouldListSmoothScroll(true)
.setTitle(translationComponent).apply {
@ -36,21 +43,38 @@ val SnowyConfigScreen: Screen get() {
.setSaveConsumer {
riskyCheatsEnabled = it
}.setTooltip(Component.nullToEmpty("WARNING: Do not use this on servers or you might get banned.")).build()
).addEntry(startStrList(
).addEntry(startStrList(
TranslatableComponent(Category.MACROS.translationKey),
macrosButtons.map { (it as Macro).run { "$title: $command" } }
).setDefaultValue(listOf()).setSaveConsumer { it.forEach { string ->
macrosButtons.map { it.run { "$title: $command" } }
).setDefaultValue(Configuration.foundMacros.map { "${it.title}: ${it.command}" }).setErrorSupplier { list ->
supplyComponent(if (list.size > buttonsMax) "$confPrefix.general.macros.toomuchbuttons" else null)
}.setCellErrorSupplier {
with(it.split(":")) {
supplyComponent(
when {
isEmpty() || !it.contains(":") -> "$confPrefix.general.macros.missingdelimiter"
size < 2 -> "$confPrefix.general.macros.missingelement"
size > 2 -> "$confPrefix.general.macros.toomuchdelimiters"
it.count() > 16 -> "$confPrefix.general.macros.toomuchcharacters"
else -> null
}
)
}
}.setTooltip(TranslatableComponent("$confPrefix.general.macros.tooltip")).setSaveConsumer { it.forEach { string ->
val splitString: Pair<String, String>? = string.split(":").map { toTrim -> toTrim.trimStart() }.let { element ->
if (element.size == 2) element.component1() to element.component2() else null
}; if (splitString != null) {
Macro(title = splitString.first, command = splitString.second).let { newMacro ->
macrosButtons.removeIf { existingMacro ->
existingMacro.title == newMacro.title
(existingMacro.title == newMacro.title)
|| (existingMacro.command == newMacro.command)
|| !it.any { currentString -> currentString.startsWith(existingMacro.title) } // Needed to remove duplicates from the config screen
}
macrosButtons.add(newMacro)
}
}
}
if (it.isEmpty()) macrosButtons.clear()
}.build())
}
}.build()

View File

@ -60,6 +60,7 @@ class ClickBox(
}
companion object {
const val buttonsMax: Short = 16 // TODO Remove the buttons limit
const val clickboxHeightOffset: Int = 80
private const val inclination: Double = 2.5
}

View File

@ -2,6 +2,12 @@
"screen.snowy.gui": "Snowy Interface",
"screen.snowy.config": "Snowy configuration screen",
"screen.snowy.config.general": "General",
"screen.snowy.config.general.macros.tooltip": "List of macros buttons;\nAfter creating a new entry, you can add a name and a command to execute to your macro by typing yourbutton:yourcommand",
"screen.snowy.config.general.macros.missingdelimiter": "Missing separator (:)",
"screen.snowy.config.general.macros.missingelement": "Missing element(s) next to the separator",
"screen.snowy.config.general.macros.toomuchdelimiters": "Too much separators (only one is allowed)",
"screen.snowy.config.general.macros.toomuchcharacters": "Too much characters (16 is the maximum)",
"screen.snowy.config.general.macros.toomuchbuttons": "Too much entries (16 is the maximum)",
"screen.snowy.config.behavior": "Behavior",
"category.snowy.keycategory": "SnowyGUI",
"key.snowy.opengui": "Open the snowy gui",

View File

@ -2,6 +2,12 @@
"screen.snowy.gui": "Interface de Snowy",
"screen.snowy.config": "Configuration de snowy",
"screen.snowy.config.general": "Général",
"screen.snowy.config.general.macros.tooltip": "Liste de macros;\nAprès avoir créé une nouvelle entrée, vous pouvez ajouter un nom et une commande à éxécuter à votre macro en écrivant votrebouton:votrecommande",
"screen.snowy.config.general.macros.missingdelimiter": "Séparateur manquant (:)",
"screen.snowy.config.general.macros.missingelement": "Élément(s) manquant(s) avant ou après le séparateur",
"screen.snowy.config.general.macros.toomuchdelimiters": "Séparateurs trop nombreux (un seul est autorisé)",
"screen.snowy.config.general.macros.toomuchcharacters": "Trop de caractères (le maximum est de 16)",
"screen.snowy.config.general.macros.toomuchbuttons": "Trop d'entrées (le maximum est de 16)",
"screen.snowy.config.behavior": "Comportement",
"category.snowy.keycategory": "SnowyGUI",
"key.snowy.opengui": "Ouvrir l'interface de snowy",