SnowyGUI/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt
Username404-59 bf317e38f0
Finish porting to 1.21
Signed-off-by: Username404-59 <w.iron.zombie@gmail.com>
2025-02-02 15:58:22 +01:00

109 lines
6.3 KiB
Kotlin

package fr.username404.snowygui.config
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.gui.elements.ClickBox
import fr.username404.snowygui.gui.elements.ClickBox.Companion.buttonsMax
import fr.username404.snowygui.gui.elements.ClickBox.Companion.sortAlphabetically
import fr.username404.snowygui.gui.feature.*
import fr.username404.snowygui.utils.FontUtil
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.Component.translatable
import java.util.Optional
private const val confPrefix: String = "screen.snowy.config"
private val translationComponent = translatable(confPrefix)
var configScreenParent: Screen? = null
private fun supplyComponent(string: String?): Optional<Component> = string?.run {
Optional.of(translatable(string))
} ?: Optional.empty()
val SnowyConfigScreen: Screen = object: Screen(translationComponent) {
override fun isPauseScreen(): Boolean = false
override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, pTick: Float) {
super.renderBackground(guiGraphics, mouseX, mouseY, pTick)
FontUtil.drawScaled(guiGraphics,
text = "An appropriate version of the Cloth Config mod is required for the configuration of snowygui.", 16.0, 16.0,
color = Colors.WHITE, scaleFactor = 0.85F
)
}
override fun onClose() { minecraft?.screen = configScreenParent }
}; get() = try {
Class.forName("me.shedaniel.clothconfig2.api.ConfigBuilder")
val macrosBox: ClickBox? = ClickGui.components.find {
(it is ClickBox) && it.isCategory(Category.MACROS)
} as? ClickBox
@Suppress("UNCHECKED_CAST")
val macrosButtons = (macrosBox?.buttons ?: LinkedHashSet<Macro>()) as MutableSet<Macro>
fun Collection<Macro>.getTitleCommand(): MutableList<String> = map { it.run { "$title: $command" } }.toMutableList()
me.shedaniel.clothconfig2.api.ConfigBuilder.create().setParentScreen(configScreenParent).transparentBackground()
.setShouldListSmoothScroll(true)
.setTitle(translationComponent).apply {
with(entryBuilder()) {
getOrCreateCategory(translatable("$confPrefix.general")).addEntry(startSubCategory(translatable("$confPrefix.behavior")).apply {
addAll(
setOf(
startBooleanToggle(translatable("$confPrefix.behavior.sortalphabetically"), sortAlphabetically)
.setDefaultValue(true).requireRestart()
.setSaveConsumer { sortAlphabetically = it }.build(),
startDoubleField(translatable("$confPrefix.behavior.zoom.factor"), Zoom.zoomFactor).setSaveConsumer {
Zoom.zoomFactor = it
}.setMin(1.1).build(),
startBooleanToggle(translatable("$confPrefix.behavior.zoom.smoothcamera"), Zoom.smoothCameraOnZoom).setSaveConsumer {
Zoom.smoothCameraOnZoom = it
Zoom.execAction()
}.build(),
startEnumSelector(translatable("$confPrefix.behavior.keystrokes.position"), Keystrokes.Position::class.java, Keystrokes.Position.values().find {
it.value == Keystrokes.currentPosition
}).setSaveConsumer {
Keystrokes.currentPosition = it.value
}.build()
)
)
}.build())
.addEntry(startSubCategory(translatable("$confPrefix.colors")).also { builder ->
builder.addAll(
ClickGui.clickBoxes.map { box ->
startColorField(box.name, box.color).setSaveConsumer {
box.color = it
}.setDefaultValue(Category.fromBox(box)?.categoryColor ?: box.color).build()
}
)
}.build()).addEntry(startStrList(
Category.MACROS.box.name,
macrosButtons.getTitleCommand()
).setInsertInFront(false).setDefaultValue(Configuration.foundMacros.getTitleCommand()).setErrorSupplier { list ->
supplyComponent(if (list.size > buttonsMax) "$confPrefix.general.macros.toomuchbuttons" else null)
}.setCellErrorSupplier { cell ->
with(cell.split(":")) {
supplyComponent(
when {
isEmpty() || !cell.contains(":") -> "$confPrefix.general.macros.missingdelimiter"
size < 2 -> "$confPrefix.general.macros.missingelement"
size > 2 -> "$confPrefix.general.macros.toomuchdelimiters"
first().count() > 16 -> "$confPrefix.general.macros.toomuchcharacters"
else -> null
}
)
}
}.setTooltip(translatable("$confPrefix.general.macros.tooltip")).setSaveConsumer { it.forEach { string ->
with(string.split(":")) {
if (size == 2) Macro(title = component1().trimStart(), command = component2().trim()).let { newMacro ->
macrosButtons.removeIf { existingMacro ->
(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().also { empty -> macrosBox?.hidden = empty })
macrosButtons.clear()
}.build()
)
}
}.build()
} catch (_: ClassNotFoundException) { field }