Start a major refactor, split the ClickButton.kt class into the ButtonInfo annotation and the ButtonImpl.kt class

This commit is contained in:
Username404-59 2021-05-14 23:20:41 +02:00
parent 4305204898
commit 3ccee1aa6f
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
15 changed files with 143 additions and 129 deletions

View File

@ -104,6 +104,7 @@ allprojects {
apply(plugin = "architectury-plugin")
dependencies {
implementation(kotlin("stdlib-jdk8", kotlinVer))
implementation(kotlin("reflect", kotlinVer))
}
tasks {
withType(ShadowJar::class) {

View File

@ -1,5 +1,6 @@
architectury { common(); injectInjectables = false }
dependencies {
modImplementation("net.fabricmc:fabric-loader:${rootProject.property("fabric_loader_version")}")
implementation(kotlin("reflect"))
}

View File

@ -1,7 +1,7 @@
package fr.username404.snowygui.mixins;
import com.mojang.blaze3d.vertex.PoseStack;
import fr.username404.snowygui.misc.Storage;
import fr.username404.snowygui.gui.feature.NoHurtCamera;
import net.minecraft.client.renderer.GameRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
@ -12,6 +12,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
public class RendererMixin {
@Inject(method = "bobHurt", at = @At("HEAD"), cancellable = true)
private void onHurt(PoseStack poseStack, float f, CallbackInfo ci) {
if (!Storage.INSTANCE.getHurtCamera()) ci.cancel();
if (NoHurtCamera.INSTANCE.getToggled()) ci.cancel();
}
}

View File

@ -1,28 +1,18 @@
package fr.username404.snowygui
import fr.username404.snowygui.gui.ColoredElement
import fr.username404.snowygui.gui.Element
import fr.username404.snowygui.gui.SnowyScreen
import fr.username404.snowygui.gui.elements.ClickBox
import fr.username404.snowygui.gui.elements.ClickButton
import fr.username404.snowygui.misc.Storage.addComponents
import net.minecraft.network.chat.TranslatableComponent
private var baseXAxis: Double = 4.0
internal fun newBox(translationKey: String, color: Int = 0x6C9E9D): ClickBox {
val result = ClickBox(baseXAxis, 4.0, name = TranslatableComponent(translationKey), color = color)
baseXAxis += 86
return result
}
object ClickGui: SnowyScreen() {
private var GuiDragging: Boolean = false
override val components = mutableSetOf<Element>()
internal fun addComps(vararg toAdd: Element) = components.addAll(toAdd)
inline fun boxContext(args: ClickBox.() -> Unit) = components.filterIsInstance<ClickBox>().forEach(args)
private inline fun buttonsContext(args: ClickButton.() -> Unit) = boxContext { buttons.values.forEach(args) }
override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { buttonsContext { mouseClicked(d, e, i) }; return false }
override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { buttonsContext { mouseReleased(d, e, i) }; return false }
private inline fun buttonsContext(args: ColoredElement.() -> Unit) = boxContext { buttons.forEach(args) }
override fun mouseClicked(d: Double, e: Double, i: Int): Boolean { buttonsContext { this.mouseClicked(d, e, i) }; return false }
override fun mouseReleased(d: Double, e: Double, i: Int): Boolean { buttonsContext { this.mouseReleased(d, e, i) }; return false }
override fun mouseScrolled(d: Double, e: Double, f: Double): Boolean { boxContext { scroll(d, e, f) }; return false }
override fun mouseDragged(d: Double, e: Double, i: Int, f: Double, g: Double): Boolean {
if (i == 0) {
@ -36,5 +26,4 @@ object ClickGui: SnowyScreen() {
}
return super.mouseDragged(d, e, i, f, g)
}
init { addComponents() }
}

View File

@ -1,15 +1,11 @@
package fr.username404.snowygui.config
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.gui.Element
import fr.username404.snowygui.gui.SnowyScreen
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.TranslatableComponent
open class SnowyConfigScreen: SnowyScreen("screen.snowygui.config") { // TODO Actual config screen
open class SnowyConfigScreen: Screen(TranslatableComponent("screen.snowygui.config")) { // TODO Actual config screen
override fun render(poseStack: PoseStack?, i: Int, j: Int, f: Float) {
renderBackground(poseStack)
super.render(poseStack, i, j, f)
}
override val components: MutableSet<Element> = mutableSetOf(
// TODO Add components to the config gui
)
}

View File

@ -5,7 +5,6 @@ import com.typesafe.config.ConfigException
import com.typesafe.config.ConfigFactory.*
import com.typesafe.config.ConfigRenderOptions
import com.typesafe.config.ConfigValueFactory
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.Snowy
import io.github.config4k.extract
import kotlinx.coroutines.coroutineScope
@ -70,13 +69,15 @@ object Configuration {
Runtime.getRuntime().addShutdownHook(
Thread {
runBlocking {
/**
ClickGui.boxContext {
enabledFeatures.putAll(
buttons.map { button ->
button.key to button.value.toggled
this.buttons.map {
(it::class.java.getDeclaredField("name").get(this) as String) to (it::class.java.getDeclaredMethod("getToggled").invoke(it)) as Boolean // TODO Fix button configuration
}
)
}
**/
writeConfig(obtained.withValue("enabledFeatures", ConfigValueFactory.fromMap(enabledFeatures))).join() // TODO Fix formatting of enabledFeatures
}
}

View File

@ -2,6 +2,8 @@ package fr.username404.snowygui.gui
import com.mojang.blaze3d.vertex.*
import fr.username404.snowygui.Snowy
import fr.username404.snowygui.gui.feature.Colors
import net.minecraft.client.gui.components.events.GuiEventListener
import org.lwjgl.opengl.GL20
fun interface Renderable {
@ -25,7 +27,7 @@ fun interface Renderable {
abstract class Element(
@JvmField val xOrigin: Double, @JvmField val yOrigin: Double,
val originalWidth: Int, val originalHeight: Int
): Renderable {
): Renderable, GuiEventListener {
open var width = originalWidth; open var height = originalHeight
open var x = xOrigin; open var y = yOrigin
internal fun withinBounds(coordinateX: Double, coordinateY: Double, offsetWidth: Double = 0.0, offsetHeight: Double = 0.0): Boolean =
@ -55,18 +57,17 @@ abstract class Element(
abstract class ColoredElement(
x: Double, y: Double, width: Int, height: Int,
color: Int = TransparentColor, protected var opacity: Float,
color: Colors = Colors.TRANSPARENT, protected var opacity: Float,
) : Element(x, y, width, height) {
open var color = color; protected set
companion object {
const val TransparentColor: Int = -0x1
@JvmStatic protected fun VertexConsumer.colorIt(color: Int, opacity: Float = 1F): VertexConsumer {
with(hextoRGB(color)) {
return this@colorIt.color(get(0), get(1), get(2), opacity)
}
}
}
internal fun VertexConsumer.colorEnd(color: Int = this@ColoredElement.color) = colorIt(color, opacity).endVertex()
internal fun VertexConsumer.colorEnd(color: Int = this@ColoredElement.color.hexValue) = colorIt(color, opacity).endVertex()
}
fun hextoRGB(hex: Int): MutableList<Float> {

View File

@ -7,27 +7,21 @@ import fr.username404.snowygui.gui.ColoredElement
import fr.username404.snowygui.gui.Renderable.Rendering.buffer
import fr.username404.snowygui.gui.Renderable.Rendering.defaultRectFunc
import fr.username404.snowygui.gui.Renderable.Rendering.tessellator
import fr.username404.snowygui.gui.elements.ClickButton.Companion.Type
import fr.username404.snowygui.gui.feature.Colors
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.minecraft.client.Minecraft
import net.minecraft.network.chat.TranslatableComponent
import org.jetbrains.annotations.ApiStatus
import org.lwjgl.opengl.GL20
@ApiStatus.Internal
class ClickBox(
x: Double, y: Double,
color: Int = 0x6C9E9D,
private val name: TranslatableComponent? = null
x: Double = baseXAxis, y: Double,
color: Colors = Colors.BLUE,
val name: TranslatableComponent? = null
): ColoredElement(x, y, 80, 10, color, 0.5F) {
val buttons = mutableMapOf<String, ClickButton>() // Can contain up to 16 buttons
fun addButtons(vararg collect: Pair<String, (ClickButton.() -> Unit)?>, kind: Type = Type.TOGGLE): ClickBox {
buttons.putAll(
collect.map {
it.first to ClickButton(title = it.first, action = it.second, getColorFrom = this, kind = kind)
}
)
return this
}
val buttons = mutableSetOf<ColoredElement>()
@JvmField
val buttonsProgressBar: ColoredElement = object: ColoredElement(
(x + width), y + height + 3,
@ -59,6 +53,7 @@ class ClickBox(
}
companion object {
var baseXAxis: Double = 4.0; private set
const val clickboxHeightOffset: Int = 80
private const val inclination: Double = 2.5
}
@ -95,7 +90,7 @@ class ClickBox(
x = this@ClickBox.x + this@ClickBox.width - 3
y = this@ClickBox.y + this@ClickBox.height + 3
}.display(poseStack)
buttons.values.forEachIndexed { num, button ->
buttons.forEachIndexed { num, button ->
val fullHeight = (y + height.toDouble())..(y + height + clickboxHeightOffset)
button.also {
it.x = x + 3
@ -105,9 +100,10 @@ class ClickBox(
}
} else null
if ((name != null) && (poseStack != null)) {
Minecraft.getInstance().font.draw(poseStack, name.string, x.toFloat() + 5, y.toFloat() + 1.5F, TransparentColor)
Minecraft.getInstance().font.draw(poseStack, name.string, x.toFloat() + 5, y.toFloat() + 1.5F, Colors.TRANSPARENT.hexValue)
renderButtons?.join()
}
}
}
init { baseXAxis += 86 }
}

View File

@ -1,29 +1,24 @@
package fr.username404.snowygui.gui.elements
package fr.username404.snowygui.gui.feature
import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.ClickGui
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 net.minecraft.client.gui.components.events.GuiEventListener
import fr.username404.snowygui.gui.elements.ClickBox
import fr.username404.snowygui.gui.feature.ButtonInfo.Companion.Type
import org.jetbrains.annotations.ApiStatus
import kotlin.reflect.full.findAnnotation
class ClickButton(
x: Double = 0.0, y: Double = 0.0,
width: Int = 73, height: Int = 8, getColorFrom: ColoredElement? = null,
private val kind: Type = Type.TOGGLE,
private val title: String = "",
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 {
enum class Type {
TOGGLE,
CLICK // TODO Fix the CLICK behaviour
}
internal var lightningFactor: Float = 0.33F
}
private fun execAction() { action?.invoke(this) }
@ApiStatus.Internal
abstract class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) {
private val info = this::class.findAnnotation<ButtonInfo>() ?: throw Exception("Missing @Button annotaton")
override var color: Colors = info.color
val title = this@ButtonImpl::class.simpleName
protected open fun execAction() = Unit
private var wasWithinBounds: Boolean = false
var toggled: Boolean = false; private set(value) {
if (value) lightUp() else if (field) lightDown()
@ -32,11 +27,11 @@ class ClickButton(
execAction()
} else field = value
}
private fun lightUp() { opacity += lightningFactor }
private fun lightDown() { opacity -= lightningFactor }
private fun lightUp() { opacity += ButtonInfo.lightningFactor }
private fun lightDown() { opacity -= ButtonInfo.lightningFactor }
override fun mouseClicked(d: Double, e: Double, i: Int): Boolean {
wasWithinBounds = withinBounds(d, e).also {
if (it && (kind == Type.TOGGLE)) {
if (it && (info.kind == Type.TOGGLE)) {
toggled = !toggled
}
}
@ -44,7 +39,7 @@ class ClickButton(
}
override fun mouseReleased(d: Double, e: Double, i: Int): Boolean {
if (wasWithinBounds && (kind == Type.CLICK)) {
if (wasWithinBounds && (info.kind == Type.CLICK)) {
toggled = true
}; return false
}
@ -54,15 +49,20 @@ class ClickButton(
defaultRectFunc()
RenderSystem.enableTexture()
RenderSystem.disableBlend()
if (poseStack != null) {
if (poseStack != null && title != null) {
FontUtil.drawScaled(poseStack, title, x + 1, y + 1, 0.75F)
}
}
init {
if (kind == Type.TOGGLE) {
if (info.kind == Type.TOGGLE) {
Configuration.enabledFeatures[title]?.let {
toggled = it
}
}
ButtonInfo
ClickGui.components.filterIsInstance<ClickBox>().find {
it.name!!.key == info.parent.translationKey
}?.buttons!!.add(this)
}
}
}

View File

@ -0,0 +1,25 @@
package fr.username404.snowygui.gui.feature
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.gui.elements.ClickBox
import net.minecraft.network.chat.TranslatableComponent
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class ButtonInfo(
val parent: Category,
val kind: Type = Type.TOGGLE,
val color: Colors = Colors.BLUE
) {
companion object {
internal var lightningFactor: Float = 0.33F
enum class Type {
TOGGLE,
CLICK // TODO Fix the CLICK behaviour
}
}
}; enum class Category(val translationKey: String) {
MISC("snowy.clickbox.misc"),
RISKY("snowy.clickbox.risky");
init { ClickGui.components.add(ClickBox(y = 0.0, name = TranslatableComponent(translationKey))) }
}

View File

@ -0,0 +1,26 @@
package fr.username404.snowygui.gui.feature
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.Snowy.Companion.onEvent
import fr.username404.snowygui.gui.FontUtil
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import kotlinx.datetime.Clock as DatetimeClock
@ButtonInfo(Category.MISC)
object Clock: ButtonImpl() {
private val currentTimezone = TimeZone.currentSystemDefault()
init {
onEvent("HudRender") {
if (toggled) {
with(DatetimeClock.System.now().toLocalDateTime(currentTimezone)) {
FontUtil.drawScaled(it.first() as PoseStack,
"$hour:$minute:${second.let { if (it >= 10) it else "0$it" }}",
5.0, 5.0,
0.85F, color = 0xe69500
)
}
}
}
}
}

View File

@ -0,0 +1,7 @@
package fr.username404.snowygui.gui.feature
enum class Colors(val hexValue: Int) {
TRANSPARENT(-0x1),
BLUE(0x6C9E9D),
RED(0x660000);
}

View File

@ -0,0 +1,16 @@
package fr.username404.snowygui.gui.feature
import net.minecraft.client.Minecraft
@ButtonInfo(Category.MISC)
object GammaBoost: ButtonImpl() {
private var oldGamma = 0.0
override fun execAction() {
with(Minecraft.getInstance().options) {
gamma = if (toggled) {
if (gamma < 1400.0) oldGamma = gamma
1400.0
} else oldGamma
}
}
}

View File

@ -0,0 +1,13 @@
package fr.username404.snowygui.gui.feature
import net.minecraft.client.Minecraft
@ButtonInfo(Category.RISKY, color = Colors.RED)
object NoHurtCamera: ButtonImpl()
@ButtonInfo(Category.RISKY, color = Colors.RED)
object NoGravity: ButtonImpl() {
override fun execAction() {
Minecraft.getInstance().player?.isNoGravity = this.toggled
}
}

View File

@ -1,58 +0,0 @@
package fr.username404.snowygui.misc
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.Snowy.Companion.onEvent
import fr.username404.snowygui.config.Configuration
import fr.username404.snowygui.gui.FontUtil
import fr.username404.snowygui.newBox
import io.github.config4k.getValue
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import net.minecraft.client.Minecraft
object Storage {
val currentTimezone = TimeZone.currentSystemDefault()
private var oldGamma = 0.0
private const val redColor = 0x660000
var clock: Boolean = false; private set
var hurtCamera: Boolean = true; private set
private val riskyCheats: Boolean by Configuration.obtained
fun ClickGui.addComponents() {
addComps(
newBox("snowy.clickbox.misc").addButtons(
"GammaBoost" to {
with(Minecraft.getInstance().options) {
gamma = if (toggled) {
if (gamma < 1400.0) oldGamma = gamma
1400.0
} else oldGamma
}
}, "Clock" to { clock = toggled },
),
)
if (riskyCheats) {
addComps(
newBox("snowy.clickbox.risky", color = redColor).addButtons(
"NoGravity" to { Minecraft.getInstance().player?.let { it.isNoGravity = toggled } },
"NoHurtCamera" to { hurtCamera = !toggled },
)
)
}
}
init {
onEvent("HudRender") {
if (clock) {
with(Clock.System.now().toLocalDateTime(currentTimezone)) {
FontUtil.drawScaled(it.first() as PoseStack,
"$hour:$minute:${second.let { if (it >= 10) it else "0$it" }}",
5.0, 5.0,
0.85F, color = 0xe69500
)
}
}
}
}
}