Get toggled buttons from Configuration.kt (WIP)

This commit is contained in:
Username404-59 2021-05-02 21:26:09 +02:00
parent 6381b0f6ca
commit 6471756b95
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
6 changed files with 50 additions and 19 deletions

View File

@ -19,7 +19,7 @@ object ClickGui: SnowyScreen() {
override val components = mutableSetOf<Element>() override val components = mutableSetOf<Element>()
internal fun addComps(vararg toAdd: Element) = components.addAll(toAdd) internal fun addComps(vararg toAdd: Element) = components.addAll(toAdd)
private inline fun boxContext(args: (ClickBox) -> Unit) = components.filterIsInstance<ClickBox>().forEach(args) inline fun boxContext(args: (ClickBox) -> Unit) = components.filterIsInstance<ClickBox>().forEach(args)
private inline fun buttonsContext(args: (ClickButton) -> Unit) = boxContext { it.buttons.values.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 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 } override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { buttonsContext { it.mouseReleased(d, e, i) }; return false }

View File

@ -4,6 +4,7 @@ import com.typesafe.config.Config
import com.typesafe.config.ConfigException import com.typesafe.config.ConfigException
import com.typesafe.config.ConfigFactory.* import com.typesafe.config.ConfigFactory.*
import com.typesafe.config.ConfigRenderOptions import com.typesafe.config.ConfigRenderOptions
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.Snowy import fr.username404.snowygui.Snowy
import io.github.config4k.extract import io.github.config4k.extract
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
@ -51,6 +52,9 @@ object Configuration {
""" """
|Snowy { |Snowy {
| displayInitMessage = true | displayInitMessage = true
| enabledFeatures {
| GammaBoost = false
| }
|} |}
""".trimMargin() """.trimMargin()
) )
@ -60,10 +64,20 @@ object Configuration {
writeConfig(it) writeConfig(it)
} }
} }
val enabledFeatures = obtained.extract<MutableMap<String, Boolean>>("enabledFeatures") // TODO Put enabledFeatures in the obtained config to save the toggled buttons
init { init {
Runtime.getRuntime().addShutdownHook( Runtime.getRuntime().addShutdownHook(
Thread { Thread {
runBlocking { writeConfig(obtained).join() } runBlocking {
ClickGui.boxContext {
enabledFeatures.putAll(
it.buttons.map { button ->
button.key to button.value.toggled
}
)
}
writeConfig(obtained).join()
}
} }
) )
} }

View File

@ -6,7 +6,7 @@ import net.minecraft.network.chat.TranslatableComponent
import kotlin.properties.Delegates import kotlin.properties.Delegates
abstract class SnowyScreen(translatableString: String = "screen.snowy.gui", private val willPauseScreen: Boolean = false): Screen(TranslatableComponent(translatableString)) { abstract class SnowyScreen(translatableString: String = "screen.snowy.gui", private val willPauseScreen: Boolean = false): Screen(TranslatableComponent(translatableString)) {
protected open val components: MutableSet<Element> by Delegates.notNull() open val components: MutableSet<Element> by Delegates.notNull()
override fun render(poseStack: PoseStack?, i: Int, j: Int, f: Float) { override fun render(poseStack: PoseStack?, i: Int, j: Int, f: Float) {
if (poseStack != null) { if (poseStack != null) {
components.forEach { components.forEach {

View File

@ -22,7 +22,7 @@ class ClickBox(
fun addButtons(vararg collect: Pair<String, (() -> Unit)?>, color: Int = 0x6C9E9D, kind: Type = Type.TOGGLE): ClickBox { fun addButtons(vararg collect: Pair<String, (() -> Unit)?>, color: Int = 0x6C9E9D, kind: Type = Type.TOGGLE): ClickBox {
buttons.putAll( buttons.putAll(
collect.map { 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 return this

View File

@ -2,6 +2,7 @@ package fr.username404.snowygui.gui.elements
import com.mojang.blaze3d.systems.RenderSystem import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.PoseStack import com.mojang.blaze3d.vertex.PoseStack
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
@ -13,7 +14,7 @@ class ClickButton(
x: Double = 0.0, y: Double = 0.0, x: Double = 0.0, y: Double = 0.0,
width: Int = 73, height: Int = 8, width: Int = 73, height: Int = 8,
color: Int, private val kind: Type = Type.TOGGLE, color: Int, private val kind: Type = Type.TOGGLE,
private val Title: String? = null, private val title: String = "",
private val action: (() -> Unit)? = null, private val action: (() -> Unit)? = null,
): ColoredElement(x, y, width, height, color, 0.60F), GuiEventListener { ): ColoredElement(x, y, width, height, color, 0.60F), GuiEventListener {
companion object { companion object {
@ -33,26 +34,25 @@ class ClickButton(
} }
} }
private var wasWithinBounds: Boolean = false 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 lightUp() { opacity += lightningFactor }
private fun lightDown() { opacity -= lightningFactor } private fun lightDown() { opacity -= lightningFactor }
override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { override fun mouseClicked(d: Double, e: Double, i: Int): Boolean {
wasWithinBounds = withinBounds(d, e).also { wasWithinBounds = withinBounds(d, e).also {
if (it) { if (it && (kind == Type.TOGGLE)) {
if (!toggled) lightUp() else lightDown() toggled = !toggled
if ((kind == Type.TOGGLE)) {
toggled = !toggled
execAction()
}
} }
} }
return false return false
} }
override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { override fun mouseReleased(d: Double, e: Double, i: Int): Boolean {
if (wasWithinBounds && (kind != Type.TOGGLE)) { if (wasWithinBounds && (kind == Type.CLICK)) {
lightDown() toggled = true
execAction()
}; return false }; return false
} }
override fun render(poseStack: PoseStack?) { override fun render(poseStack: PoseStack?) {
@ -61,8 +61,15 @@ class ClickButton(
defaultRectFunc() defaultRectFunc()
RenderSystem.enableTexture() RenderSystem.enableTexture()
RenderSystem.disableBlend() RenderSystem.disableBlend()
if (Title != null && poseStack != null) { if (poseStack != null) {
FontUtil.drawScaled(poseStack, Title, x + 1, y + 1, 0.75F) FontUtil.drawScaled(poseStack, title, x + 1, y + 1, 0.75F)
}
}
init {
if (kind == Type.TOGGLE) {
Configuration.enabledFeatures[title]?.let {
toggled = it
}
} }
} }
} }

View File

@ -2,12 +2,22 @@ package fr.username404.snowygui.misc
import fr.username404.snowygui.ClickGui import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.newBox import fr.username404.snowygui.newBox
import net.minecraft.client.Minecraft
private object Storage {
var oldGamma = -1.0
}
fun ClickGui.addComponents() { fun ClickGui.addComponents() {
addComps( addComps(
newBox("snowy.clickbox.misc").addButtons( newBox("snowy.clickbox.misc").addButtons(
"Fullbright" to { "GammaBoost" to {
// TODO Fullbright with(Minecraft.getInstance().options) {
gamma = if (gamma != 1400.0) {
Storage.oldGamma = gamma
1400.0
} else Storage.oldGamma
}
} }
), ),
newBox("snowy.clickbox.rendering").addButtons() newBox("snowy.clickbox.rendering").addButtons()