Get toggled buttons from Configuration.kt (WIP)
This commit is contained in:
parent
6381b0f6ca
commit
6471756b95
|
@ -19,7 +19,7 @@ object ClickGui: SnowyScreen() {
|
|||
override val components = mutableSetOf<Element>()
|
||||
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) }
|
||||
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 }
|
||||
|
|
|
@ -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<MutableMap<String, Boolean>>("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()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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<Element> by Delegates.notNull()
|
||||
open val components: MutableSet<Element> by Delegates.notNull()
|
||||
override fun render(poseStack: PoseStack?, i: Int, j: Int, f: Float) {
|
||||
if (poseStack != null) {
|
||||
components.forEach {
|
||||
|
|
|
@ -22,7 +22,7 @@ class ClickBox(
|
|||
fun addButtons(vararg collect: Pair<String, (() -> 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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue