diff options
Diffstat (limited to 'src/main/kotlin/gui/config')
-rw-r--r-- | src/main/kotlin/gui/config/AllConfigsGui.kt | 24 | ||||
-rw-r--r-- | src/main/kotlin/gui/config/BuiltInConfigScreenProvider.kt | 2 | ||||
-rw-r--r-- | src/main/kotlin/gui/config/ColourHandler.kt | 82 | ||||
-rw-r--r-- | src/main/kotlin/gui/config/FirmamentConfigScreenProvider.kt | 2 | ||||
-rw-r--r-- | src/main/kotlin/gui/config/KeyBindingHandler.kt | 29 | ||||
-rw-r--r-- | src/main/kotlin/gui/config/KeyBindingStateManager.kt | 44 | ||||
-rw-r--r-- | src/main/kotlin/gui/config/ManagedConfig.kt | 8 | ||||
-rw-r--r-- | src/main/kotlin/gui/config/ManagedOption.kt | 2 |
8 files changed, 157 insertions, 36 deletions
diff --git a/src/main/kotlin/gui/config/AllConfigsGui.kt b/src/main/kotlin/gui/config/AllConfigsGui.kt index 73ff444..f9ffd2d 100644 --- a/src/main/kotlin/gui/config/AllConfigsGui.kt +++ b/src/main/kotlin/gui/config/AllConfigsGui.kt @@ -4,6 +4,12 @@ import io.github.notenoughupdates.moulconfig.observer.ObservableList import io.github.notenoughupdates.moulconfig.xml.Bind import net.minecraft.client.gui.screen.Screen import net.minecraft.text.Text +import moe.nea.firmament.annotations.Subscribe +import moe.nea.firmament.commands.RestArgumentType +import moe.nea.firmament.commands.get +import moe.nea.firmament.commands.thenArgument +import moe.nea.firmament.commands.thenExecute +import moe.nea.firmament.events.CommandEvent import moe.nea.firmament.util.MC import moe.nea.firmament.util.MoulConfigUtils import moe.nea.firmament.util.ScreenUtil.setScreenLater @@ -18,6 +24,7 @@ object AllConfigsGui { object ConfigConfig : ManagedConfig("configconfig", Category.META) { val enableYacl by toggle("enable-yacl") { false } val enableMoulConfig by toggle("enable-moulconfig") { true } + val enableWideMC by toggle("wide-moulconfig") { false } } fun <T> List<T>.toObservableList(): ObservableList<T> = ObservableList(this) @@ -66,7 +73,7 @@ object AllConfigsGui { return MoulConfigUtils.loadScreen("config/main", CategoryView(), parent) } - fun makeScreen(parent: Screen? = null): Screen { + fun makeScreen(search: String? = null, parent: Screen? = null): Screen { val wantedKey = when { ConfigConfig.enableMoulConfig -> "moulconfig" ConfigConfig.enableYacl -> "yacl" @@ -74,10 +81,23 @@ object AllConfigsGui { } val provider = FirmamentConfigScreenProvider.providers.find { it.key == wantedKey } ?: FirmamentConfigScreenProvider.providers.first() - return provider.open(parent) + return provider.open(search, parent) } fun showAllGuis() { setScreenLater(makeScreen()) } + + @Subscribe + fun registerCommands(event: CommandEvent.SubCommand) { + event.subcommand("search") { + thenArgument("search", RestArgumentType) { search -> + thenExecute { + val search = this[search] + setScreenLater(makeScreen(search = search)) + } + } + } + } + } diff --git a/src/main/kotlin/gui/config/BuiltInConfigScreenProvider.kt b/src/main/kotlin/gui/config/BuiltInConfigScreenProvider.kt index 19e7383..8ecdfa2 100644 --- a/src/main/kotlin/gui/config/BuiltInConfigScreenProvider.kt +++ b/src/main/kotlin/gui/config/BuiltInConfigScreenProvider.kt @@ -8,7 +8,7 @@ class BuiltInConfigScreenProvider : FirmamentConfigScreenProvider { override val key: String get() = "builtin" - override fun open(parent: Screen?): Screen { + override fun open(search: String?, parent: Screen?): Screen { return AllConfigsGui.makeBuiltInScreen(parent) } } diff --git a/src/main/kotlin/gui/config/ColourHandler.kt b/src/main/kotlin/gui/config/ColourHandler.kt new file mode 100644 index 0000000..7d121ab --- /dev/null +++ b/src/main/kotlin/gui/config/ColourHandler.kt @@ -0,0 +1,82 @@ +package moe.nea.firmament.gui.config + +import io.github.notenoughupdates.moulconfig.ChromaColour +import io.github.notenoughupdates.moulconfig.gui.component.ColorSelectComponent +import kotlinx.serialization.KSerializer +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonElement + +class ColourHandler(val config: ManagedConfig) : + ManagedConfig.OptionHandler<ChromaColour> { + @Serializable + data class ChromaDelegate( + @SerialName("h") + val hue: Float, + @SerialName("s") + val saturation: Float, + @SerialName("b") + val brightness: Float, + @SerialName("a") + val alpha: Int, + @SerialName("c") + val timeForFullRotationInMillis: Int, + ) { + constructor(delegate: ChromaColour) : this( + delegate.hue, + delegate.saturation, + delegate.brightness, + delegate.alpha, + delegate.timeForFullRotationInMillis + ) + + fun into(): ChromaColour = ChromaColour(hue, saturation, brightness, alpha, timeForFullRotationInMillis) + } + + object ChromaSerializer : KSerializer<ChromaColour> { + override val descriptor: SerialDescriptor + get() = SerialDescriptor("FirmChromaColour", ChromaDelegate.serializer().descriptor) + + override fun serialize( + encoder: Encoder, + value: ChromaColour + ) { + encoder.encodeSerializableValue(ChromaDelegate.serializer(), ChromaDelegate(value)) + } + + override fun deserialize(decoder: Decoder): ChromaColour { + return decoder.decodeSerializableValue(ChromaDelegate.serializer()).into() + } + } + + override fun toJson(element: ChromaColour): JsonElement? { + return Json.encodeToJsonElement(ChromaSerializer, element) + } + + override fun fromJson(element: JsonElement): ChromaColour { + return Json.decodeFromJsonElement(ChromaSerializer, element) + } + + override fun emitGuiElements( + opt: ManagedOption<ChromaColour>, + guiAppender: GuiAppender + ) { + guiAppender.appendLabeledRow( + opt.labelText, + ColorSelectComponent( + 0, + 0, + opt.value.toLegacyString(), + { + opt.value = ChromaColour.forLegacyString(it) + config.save() + }, + { } + ) + ) + } +} diff --git a/src/main/kotlin/gui/config/FirmamentConfigScreenProvider.kt b/src/main/kotlin/gui/config/FirmamentConfigScreenProvider.kt index faad1cc..8700ffa 100644 --- a/src/main/kotlin/gui/config/FirmamentConfigScreenProvider.kt +++ b/src/main/kotlin/gui/config/FirmamentConfigScreenProvider.kt @@ -7,7 +7,7 @@ interface FirmamentConfigScreenProvider { val key: String val isEnabled: Boolean get() = true - fun open(parent: Screen?): Screen + fun open(search: String?, parent: Screen?): Screen companion object : CompatLoader<FirmamentConfigScreenProvider>(FirmamentConfigScreenProvider::class) { val providers by lazy { diff --git a/src/main/kotlin/gui/config/KeyBindingHandler.kt b/src/main/kotlin/gui/config/KeyBindingHandler.kt index d7d0b47..14a4b32 100644 --- a/src/main/kotlin/gui/config/KeyBindingHandler.kt +++ b/src/main/kotlin/gui/config/KeyBindingHandler.kt @@ -40,34 +40,7 @@ class KeyBindingHandler(val name: String, val managedConfig: ManagedConfig) : { button.blur() }, { button.requestFocus() } ) - button = object : FirmButtonComponent( - TextComponent( - IMinecraft.instance.defaultFontRenderer, - { sm.label.string }, - 130, - TextComponent.TextAlignment.LEFT, - false, - false - ), action = { - sm.onClick() - }) { - override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean { - if (event is KeyboardEvent.KeyPressed) { - return sm.keyboardEvent(event.keycode, event.pressed) - } - return super.keyboardEvent(event, context) - } - - override fun getBackground(context: GuiImmediateContext): NinePatch<MyResourceLocation> { - if (sm.editing) return activeBg - return super.getBackground(context) - } - - - override fun onLostFocus() { - sm.onLostFocus() - } - } + button = sm.createButton() sm.updateLabel() return button } diff --git a/src/main/kotlin/gui/config/KeyBindingStateManager.kt b/src/main/kotlin/gui/config/KeyBindingStateManager.kt index cc8178d..1528ac4 100644 --- a/src/main/kotlin/gui/config/KeyBindingStateManager.kt +++ b/src/main/kotlin/gui/config/KeyBindingStateManager.kt @@ -1,8 +1,15 @@ package moe.nea.firmament.gui.config +import io.github.notenoughupdates.moulconfig.common.IMinecraft +import io.github.notenoughupdates.moulconfig.common.MyResourceLocation +import io.github.notenoughupdates.moulconfig.deps.libninepatch.NinePatch +import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext +import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent +import io.github.notenoughupdates.moulconfig.gui.component.TextComponent import org.lwjgl.glfw.GLFW import net.minecraft.text.Text import net.minecraft.util.Formatting +import moe.nea.firmament.gui.FirmButtonComponent import moe.nea.firmament.keybindings.SavedKeyBinding class KeyBindingStateManager( @@ -51,9 +58,11 @@ class KeyBindingStateManager( ) { lastPressed = ch } else { - setValue(SavedKeyBinding( - ch, modifiers - )) + setValue( + SavedKeyBinding( + ch, modifiers + ) + ) editing = false blur() lastPressed = 0 @@ -104,5 +113,34 @@ class KeyBindingStateManager( label = stroke } + fun createButton(): FirmButtonComponent { + return object : FirmButtonComponent( + TextComponent( + IMinecraft.instance.defaultFontRenderer, + { this@KeyBindingStateManager.label.string }, + 130, + TextComponent.TextAlignment.LEFT, + false, + false + ), action = { + this@KeyBindingStateManager.onClick() + }) { + override fun keyboardEvent(event: KeyboardEvent, context: GuiImmediateContext): Boolean { + if (event is KeyboardEvent.KeyPressed) { + return this@KeyBindingStateManager.keyboardEvent(event.keycode, event.pressed) + } + return super.keyboardEvent(event, context) + } + + override fun getBackground(context: GuiImmediateContext): NinePatch<MyResourceLocation> { + if (this@KeyBindingStateManager.editing) return activeBg + return super.getBackground(context) + } + + override fun onLostFocus() { + this@KeyBindingStateManager.onLostFocus() + } + } + } } diff --git a/src/main/kotlin/gui/config/ManagedConfig.kt b/src/main/kotlin/gui/config/ManagedConfig.kt index 7ddda9e..12b82d6 100644 --- a/src/main/kotlin/gui/config/ManagedConfig.kt +++ b/src/main/kotlin/gui/config/ManagedConfig.kt @@ -1,6 +1,7 @@ package moe.nea.firmament.gui.config import com.mojang.serialization.Codec +import io.github.notenoughupdates.moulconfig.ChromaColour import io.github.notenoughupdates.moulconfig.gui.CloseEventListener import io.github.notenoughupdates.moulconfig.gui.GuiComponentWrapper import io.github.notenoughupdates.moulconfig.gui.GuiContext @@ -38,7 +39,9 @@ abstract class ManagedConfig( MISC, CHAT, INVENTORY, + ITEMS, MINING, + GARDEN, EVENTS, INTEGRATIONS, META, @@ -69,6 +72,7 @@ abstract class ManagedConfig( category.configs.add(this) } + // TODO: warn if two files use the same config file name :( val file = Firmament.CONFIG_DIR.resolve("$name.json") val data: JsonObject by lazy { try { @@ -115,6 +119,10 @@ abstract class ManagedConfig( return option(propertyName, default, BooleanHandler(this)) } + protected fun colour(propertyName: String, default: ()-> ChromaColour) : ManagedOption<ChromaColour> { + return option(propertyName, default, ColourHandler(this)) + } + protected fun <E> choice( propertyName: String, enumClass: Class<E>, diff --git a/src/main/kotlin/gui/config/ManagedOption.kt b/src/main/kotlin/gui/config/ManagedOption.kt index 383f392..830086c 100644 --- a/src/main/kotlin/gui/config/ManagedOption.kt +++ b/src/main/kotlin/gui/config/ManagedOption.kt @@ -49,7 +49,7 @@ class ManagedOption<T : Any>( value = handler.fromJson(root[propertyName]!!) return } catch (e: Exception) { - ErrorUtil.softError( + ErrorUtil.logError( "Exception during loading of config file ${element.name}. This will reset this config.", e ) |