Add receivers to buttons actions

This commit is contained in:
Username404-59 2021-05-06 21:07:17 +02:00
parent 290f521c82
commit f4a16a10d2
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 12 additions and 16 deletions

View File

@ -20,7 +20,7 @@ class ClickBox(
private val name: TranslatableComponent? = null private val name: TranslatableComponent? = null
): ColoredElement(x, y, 80, 10, color, 0.5F) { ): ColoredElement(x, y, 80, 10, color, 0.5F) {
val buttons = mutableMapOf<String, ClickButton>() // Can contain up to 16 buttons val buttons = mutableMapOf<String, ClickButton>() // Can contain up to 16 buttons
fun addButtons(vararg collect: Pair<String, (() -> Unit)?>, kind: Type = Type.TOGGLE): ClickBox { fun addButtons(vararg collect: Pair<String, (ClickButton.() -> Unit)?>, kind: Type = Type.TOGGLE): ClickBox {
buttons.putAll( buttons.putAll(
collect.map { collect.map {
it.first to ClickButton(title = it.first, action = it.second, getColorFrom = this, kind = kind) it.first to ClickButton(title = it.first, action = it.second, getColorFrom = this, kind = kind)

View File

@ -6,8 +6,6 @@ import fr.username404.snowygui.config.Configuration
import fr.username404.snowygui.gui.ColoredElement import fr.username404.snowygui.gui.ColoredElement
import fr.username404.snowygui.gui.FontUtil import fr.username404.snowygui.gui.FontUtil
import fr.username404.snowygui.gui.Renderable.Rendering.defaultRectFunc import fr.username404.snowygui.gui.Renderable.Rendering.defaultRectFunc
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.minecraft.client.gui.components.events.GuiEventListener import net.minecraft.client.gui.components.events.GuiEventListener
class ClickButton( class ClickButton(
@ -15,7 +13,7 @@ class ClickButton(
width: Int = 73, height: Int = 8, getColorFrom: ColoredElement? = null, width: Int = 73, height: Int = 8, getColorFrom: ColoredElement? = null,
private val kind: Type = Type.TOGGLE, private val kind: Type = Type.TOGGLE,
private val title: String = "", private val title: String = "",
private val action: (() -> Unit)? = null, private val action: (ClickButton.() -> Unit)? = null,
): ColoredElement(x, y, width, height, opacity = 0.60F), GuiEventListener { ): ColoredElement(x, y, width, height, opacity = 0.60F), GuiEventListener {
override var color: Int = getColorFrom?.color ?: super.color override var color: Int = getColorFrom?.color ?: super.color
companion object { companion object {
@ -25,20 +23,18 @@ class ClickButton(
} }
internal var lightningFactor: Float = 0.33F internal var lightningFactor: Float = 0.33F
} }
fun execAction() { private fun execAction() {
runBlocking {
action?.let { action?.let {
launch { this@ClickButton.it()
it.invoke()
}
}
} }
} }
private var wasWithinBounds: Boolean = false private var wasWithinBounds: Boolean = false
var toggled: Boolean = false; private set(value) { var toggled: Boolean = false; private set(value) {
if (value) lightUp() else if (field) lightDown() if (value) lightUp() else if (field) lightDown()
if ((!(!field && !value))) execAction() if ((!(!field && !value))) {
field = value field = value
execAction()
} else field = value
} }
private fun lightUp() { opacity += lightningFactor } private fun lightUp() { opacity += lightningFactor }
private fun lightDown() { opacity -= lightningFactor } private fun lightDown() { opacity -= lightningFactor }

View File

@ -25,7 +25,7 @@ object Storage {
newBox("snowy.clickbox.misc").addButtons( newBox("snowy.clickbox.misc").addButtons(
"GammaBoost" to { "GammaBoost" to {
with(Minecraft.getInstance().options) { with(Minecraft.getInstance().options) {
gamma = if (gamma != 1400.0) { gamma = if (!toggled && gamma != 1400.0) {
oldGamma = gamma oldGamma = gamma
1400.0 1400.0
} else oldGamma } else oldGamma
@ -36,10 +36,10 @@ object Storage {
if (riskyCheats) { if (riskyCheats) {
addComps( addComps(
newBox("snowy.clickbox.risky.camera", color = redColor).addButtons( newBox("snowy.clickbox.risky.camera", color = redColor).addButtons(
"NoHurtCamera" to { hurtCamera = !hurtCamera }, "NoHurtCamera" to { hurtCamera = toggled },
), ),
newBox("snowy.clickbox.risky.movement", color = redColor).addButtons( 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 } },
), ),
) )
} }