From f4a16a10d27ad18c514bd4415b8310a799ad52ff Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Thu, 6 May 2021 21:07:17 +0200 Subject: [PATCH] Add receivers to buttons actions --- .../snowygui/gui/elements/ClickBox.kt | 2 +- .../snowygui/gui/elements/ClickButton.kt | 20 ++++++++----------- .../snowygui/misc/addComponents.kt | 6 +++--- 3 files changed, 12 insertions(+), 16 deletions(-) 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 73f60d5..e6e701b 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 @@ -20,7 +20,7 @@ class ClickBox( private val name: TranslatableComponent? = null ): ColoredElement(x, y, 80, 10, color, 0.5F) { val buttons = mutableMapOf() // Can contain up to 16 buttons - fun addButtons(vararg collect: Pair Unit)?>, kind: Type = Type.TOGGLE): ClickBox { + fun addButtons(vararg collect: Pair Unit)?>, kind: Type = Type.TOGGLE): ClickBox { buttons.putAll( collect.map { it.first to ClickButton(title = it.first, action = it.second, getColorFrom = this, kind = kind) 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 0d22982..0df4502 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 @@ -6,8 +6,6 @@ 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 -import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking import net.minecraft.client.gui.components.events.GuiEventListener class ClickButton( @@ -15,7 +13,7 @@ class ClickButton( width: Int = 73, height: Int = 8, getColorFrom: ColoredElement? = null, private val kind: Type = Type.TOGGLE, private val title: String = "", - private val action: (() -> Unit)? = null, + private val action: (ClickButton.() -> Unit)? = null, ): ColoredElement(x, y, width, height, opacity = 0.60F), GuiEventListener { override var color: Int = getColorFrom?.color ?: super.color companion object { @@ -25,20 +23,18 @@ class ClickButton( } internal var lightningFactor: Float = 0.33F } - fun execAction() { - runBlocking { - action?.let { - launch { - it.invoke() - } - } + private fun execAction() { + action?.let { + this@ClickButton.it() } } private var wasWithinBounds: Boolean = false var toggled: Boolean = false; private set(value) { if (value) lightUp() else if (field) lightDown() - if ((!(!field && !value))) execAction() - field = value + if ((!(!field && !value))) { + field = value + execAction() + } else field = value } private fun lightUp() { opacity += lightningFactor } private fun lightDown() { opacity -= lightningFactor } 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 3c16601..b4761ca 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/misc/addComponents.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/misc/addComponents.kt @@ -25,7 +25,7 @@ object Storage { newBox("snowy.clickbox.misc").addButtons( "GammaBoost" to { with(Minecraft.getInstance().options) { - gamma = if (gamma != 1400.0) { + gamma = if (!toggled && gamma != 1400.0) { oldGamma = gamma 1400.0 } else oldGamma @@ -36,10 +36,10 @@ object Storage { if (riskyCheats) { addComps( newBox("snowy.clickbox.risky.camera", color = redColor).addButtons( - "NoHurtCamera" to { hurtCamera = !hurtCamera }, + "NoHurtCamera" to { hurtCamera = toggled }, ), newBox("snowy.clickbox.risky.movement", color = redColor).addButtons( - "NoGravity" to { Minecraft.getInstance().player?.let { it.isNoGravity = !it.isNoGravity } }, + "NoGravity" to { Minecraft.getInstance().player?.let { it.isNoGravity = toggled } }, ), ) }