From 6471756b95af7f5587eccb1dbc3991efd3f03797 Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Sun, 2 May 2021 21:26:09 +0200 Subject: [PATCH] Get toggled buttons from Configuration.kt (WIP) --- .../fr/username404/snowygui/ClickGui.kt | 2 +- .../snowygui/config/Configuration.kt | 16 ++++++++- .../username404/snowygui/gui/SnowyScreen.kt | 2 +- .../snowygui/gui/elements/ClickBox.kt | 2 +- .../snowygui/gui/elements/ClickButton.kt | 33 +++++++++++-------- .../snowygui/misc/addComponents.kt | 14 ++++++-- 6 files changed, 50 insertions(+), 19 deletions(-) diff --git a/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt b/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt index ca8bc55..f25f473 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt @@ -19,7 +19,7 @@ object ClickGui: SnowyScreen() { override val components = mutableSetOf() internal fun addComps(vararg toAdd: Element) = components.addAll(toAdd) - private inline fun boxContext(args: (ClickBox) -> Unit) = components.filterIsInstance().forEach(args) + inline fun boxContext(args: (ClickBox) -> Unit) = components.filterIsInstance().forEach(args) private inline fun buttonsContext(args: (ClickButton) -> Unit) = boxContext { it.buttons.values.forEach(args) } override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { buttonsContext { it.mouseClicked(d, e, i) }; return false } override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { buttonsContext { it.mouseReleased(d, e, i) }; return false } diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt index 3427b9a..38ac89b 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -4,6 +4,7 @@ import com.typesafe.config.Config import com.typesafe.config.ConfigException import com.typesafe.config.ConfigFactory.* import com.typesafe.config.ConfigRenderOptions +import fr.username404.snowygui.ClickGui import fr.username404.snowygui.Snowy import io.github.config4k.extract import kotlinx.coroutines.coroutineScope @@ -51,6 +52,9 @@ object Configuration { """ |Snowy { | displayInitMessage = true + | enabledFeatures { + | GammaBoost = false + | } |} """.trimMargin() ) @@ -60,10 +64,20 @@ object Configuration { writeConfig(it) } } + val enabledFeatures = obtained.extract>("enabledFeatures") // TODO Put enabledFeatures in the obtained config to save the toggled buttons init { Runtime.getRuntime().addShutdownHook( Thread { - runBlocking { writeConfig(obtained).join() } + runBlocking { + ClickGui.boxContext { + enabledFeatures.putAll( + it.buttons.map { button -> + button.key to button.value.toggled + } + ) + } + writeConfig(obtained).join() + } } ) } diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/SnowyScreen.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/SnowyScreen.kt index f54b76c..923724d 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/SnowyScreen.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/SnowyScreen.kt @@ -6,7 +6,7 @@ import net.minecraft.network.chat.TranslatableComponent import kotlin.properties.Delegates abstract class SnowyScreen(translatableString: String = "screen.snowy.gui", private val willPauseScreen: Boolean = false): Screen(TranslatableComponent(translatableString)) { - protected open val components: MutableSet by Delegates.notNull() + open val components: MutableSet by Delegates.notNull() override fun render(poseStack: PoseStack?, i: Int, j: Int, f: Float) { if (poseStack != null) { components.forEach { diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt index 9edfb08..809dddb 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt @@ -22,7 +22,7 @@ class ClickBox( fun addButtons(vararg collect: Pair Unit)?>, color: Int = 0x6C9E9D, kind: Type = Type.TOGGLE): ClickBox { buttons.putAll( collect.map { - it.first to ClickButton(Title = it.first, action = it.second, color = color, kind = kind) + it.first to ClickButton(title = it.first, action = it.second, color = color, kind = kind) } ) return this diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickButton.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickButton.kt index 0893bc9..1c8f798 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickButton.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickButton.kt @@ -2,6 +2,7 @@ package fr.username404.snowygui.gui.elements import com.mojang.blaze3d.systems.RenderSystem import com.mojang.blaze3d.vertex.PoseStack +import fr.username404.snowygui.config.Configuration import fr.username404.snowygui.gui.ColoredElement import fr.username404.snowygui.gui.FontUtil import fr.username404.snowygui.gui.Renderable.Rendering.defaultRectFunc @@ -13,7 +14,7 @@ class ClickButton( x: Double = 0.0, y: Double = 0.0, width: Int = 73, height: Int = 8, color: Int, private val kind: Type = Type.TOGGLE, - private val Title: String? = null, + private val title: String = "", private val action: (() -> Unit)? = null, ): ColoredElement(x, y, width, height, color, 0.60F), GuiEventListener { companion object { @@ -33,26 +34,25 @@ class ClickButton( } } private var wasWithinBounds: Boolean = false - private var toggled: Boolean = false + var toggled: Boolean = false; private set(value) { + if (value) lightUp() else if (field) lightDown() + if ((!(!field && !value))) execAction() + field = value + } private fun lightUp() { opacity += lightningFactor } private fun lightDown() { opacity -= lightningFactor } override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { wasWithinBounds = withinBounds(d, e).also { - if (it) { - if (!toggled) lightUp() else lightDown() - if ((kind == Type.TOGGLE)) { - toggled = !toggled - execAction() - } + if (it && (kind == Type.TOGGLE)) { + toggled = !toggled } } return false } override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { - if (wasWithinBounds && (kind != Type.TOGGLE)) { - lightDown() - execAction() + if (wasWithinBounds && (kind == Type.CLICK)) { + toggled = true }; return false } override fun render(poseStack: PoseStack?) { @@ -61,8 +61,15 @@ class ClickButton( defaultRectFunc() RenderSystem.enableTexture() RenderSystem.disableBlend() - if (Title != null && poseStack != null) { - FontUtil.drawScaled(poseStack, Title, x + 1, y + 1, 0.75F) + if (poseStack != null) { + FontUtil.drawScaled(poseStack, title, x + 1, y + 1, 0.75F) + } + } + init { + if (kind == Type.TOGGLE) { + Configuration.enabledFeatures[title]?.let { + toggled = it + } } } } \ No newline at end of file diff --git a/common/src/main/kotlin/fr/username404/snowygui/misc/addComponents.kt b/common/src/main/kotlin/fr/username404/snowygui/misc/addComponents.kt index da5060d..beb9505 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/misc/addComponents.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/misc/addComponents.kt @@ -2,12 +2,22 @@ package fr.username404.snowygui.misc import fr.username404.snowygui.ClickGui import fr.username404.snowygui.newBox +import net.minecraft.client.Minecraft + +private object Storage { + var oldGamma = -1.0 +} fun ClickGui.addComponents() { addComps( newBox("snowy.clickbox.misc").addButtons( - "Fullbright" to { - // TODO Fullbright + "GammaBoost" to { + with(Minecraft.getInstance().options) { + gamma = if (gamma != 1400.0) { + Storage.oldGamma = gamma + 1400.0 + } else Storage.oldGamma + } } ), newBox("snowy.clickbox.rendering").addButtons()