Add RenderUtil.kt, fix a logic issue in the setter of ButtonImpl.toggled, add keystrokes and set the version number to 0.3.0

This commit is contained in:
Username404 2021-07-17 22:58:21 +02:00
parent 38e0989358
commit e5f0cd0ab2
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
15 changed files with 153 additions and 57 deletions

View File

@ -26,7 +26,7 @@ plugins {
}
group = "fr.username404"
version = "0.2.7"
version = "0.3.0"
val groupAndName = "${rootProject.group}.${rootProject.name.toLowerCase()}"
architectury {

View File

@ -16,8 +16,9 @@ abstract class Snowy {
private val displayInitMessage: Boolean by Configuration
companion object {
val MissingComponent: TranslatableComponent = object: TranslatableComponent(null) { override fun getString(): String = "MISSING_COMPONENT" }
@Suppress("JVM_STATIC_ON_CONST_OR_JVM_FIELD") // See KT-39868
@JvmStatic
protected val FeaturePackage: String = ButtonInfo::class.java.packageName
protected const val FeaturePackage: String = "fr.username404.snowygui.gui.feature"
lateinit var annotatedButtons: Set<Class<out ButtonImpl>>
fun onEvent(e: Any, lambda: argsLambda) = useKey(e.toString()).add(lambda)
@JvmField

View File

@ -2,7 +2,7 @@ package fr.username404.snowygui.config
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.gui.FontUtil
import fr.username404.snowygui.utils.FontUtil
import fr.username404.snowygui.gui.elements.ClickBox
import fr.username404.snowygui.gui.elements.ClickBox.Companion.buttonsMax
import fr.username404.snowygui.gui.elements.ClickBox.Companion.sortAlphabetically

View File

@ -1,32 +1,20 @@
package fr.username404.snowygui.gui
import com.mojang.blaze3d.vertex.*
import com.mojang.blaze3d.vertex.PoseStack
import com.mojang.blaze3d.vertex.VertexConsumer
import fr.username404.snowygui.Snowy
import fr.username404.snowygui.gui.feature.Colors
import fr.username404.snowygui.utils.RenderingUtil
import fr.username404.snowygui.utils.RenderingUtil.colorEnd
import net.minecraft.client.gui.components.events.GuiEventListener
import org.lwjgl.opengl.GL20
fun interface Renderable {
companion object Rendering {
@JvmStatic val tessellator: Tesselator = Tesselator.getInstance()
@JvmStatic val buffer: BufferBuilder = tessellator.builder
fun ColoredElement.defaultRectFunc() {
with(buffer) {
begin(GL20.GL_QUADS, DefaultVertexFormat.POSITION_COLOR)
vertex(x, y + height, 0.0).colorEnd()
vertex(x + width, y + height, 0.0).colorEnd()
vertex(x + width, y, 0.0).colorEnd()
vertex(x, y, 0.0).colorEnd()
tessellator.end()
}
}
}
fun render(poseStack: PoseStack?)
}
abstract class Element(
@JvmField val xOrigin: Double, @JvmField val yOrigin: Double,
val originalWidth: Int, val originalHeight: Int
@JvmField val originalWidth: Int, @JvmField val originalHeight: Int
): Renderable, GuiEventListener {
open var width = originalWidth; open var height = originalHeight
open var x = xOrigin; open var y = yOrigin
@ -34,11 +22,6 @@ abstract class Element(
(coordinateX in x..(x + width + offsetWidth)) && (coordinateY in y..(y + height + offsetHeight))
companion object {
private var caughtError: Boolean = false
fun fromRenderable(r: Renderable, x: Double, y: Double, width: Int, height: Int): Element {
return object: Element(x, y, width, height) {
override fun render(poseStack: PoseStack?) = r.render(poseStack)
}
}
}
open fun display(stack: PoseStack? = null) {
if (!hidden && !caughtError) try {
@ -59,14 +42,8 @@ abstract class ColoredElement(
x: Double, y: Double, width: Int, height: Int,
open val color: Int = Colors.TRANSPARENT.hexValue, protected var opacity: Float,
) : Element(x, y, width, height) {
companion object {
@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()
protected fun VertexConsumer.colorEnd(color: Int = this@ColoredElement.color) = colorEnd(color, opacity)
protected fun defaultRectFunc() = RenderingUtil.drawRectangle(x, y, height, width, color, opacity)
}
fun hextoRGB(hex: Int): MutableList<Float> {

View File

@ -6,12 +6,11 @@ import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.Snowy.Companion.MissingComponent
import fr.username404.snowygui.config.Configuration
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.feature.ButtonImpl
import fr.username404.snowygui.gui.feature.Category
import fr.username404.snowygui.gui.feature.Colors
import fr.username404.snowygui.utils.RenderingUtil.buffer
import fr.username404.snowygui.utils.RenderingUtil.tessellator
import io.github.config4k.extract
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.launch

View File

@ -6,8 +6,7 @@ import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.Snowy
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 fr.username404.snowygui.utils.FontUtil
import fr.username404.snowygui.gui.feature.ButtonInfo.Companion.Type
import kotlin.reflect.full.findAnnotation
@ -55,7 +54,7 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) {
}
@JvmField
val info = this::class.findAnnotation<ButtonInfo>() ?: throw Exception("Missing @ButtonInfo annotaton")
override var color = info.parent.categoryColor; get() {
final override var color = info.parent.categoryColor; get() {
return info.parent.box.color.let {
if (field == it) field else it
}
@ -64,8 +63,8 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) {
protected open fun execAction() = Unit
private var wasWithinBounds: Boolean = false
var toggled: Boolean = false; private set(value) {
if (value) lightUp() else if (field) lightDown()
if (field xor value) {
if (value) lightUp() else lightDown()
field = value
execAction()
} else field = value

View File

@ -0,0 +1,17 @@
package fr.username404.snowygui.gui.feature
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.EventSnowy.Companion.useKey
import fr.username404.snowygui.argsLambda
import fr.username404.snowygui.gui.Renderable
sealed class ButtonImplWithHud: ButtonImpl() {
protected abstract val hudRenderLambda: Renderable
private val generatedLambda: argsLambda = { hudRenderLambda.render(it.first() as PoseStack?) }
final override fun execAction() {
useKey("HudRender").run {
if (toggled) add(generatedLambda)
else remove(generatedLambda)
}
}
}

View File

@ -16,13 +16,14 @@ annotation class ButtonInfo(
val ignored: Boolean = false /** Excludes a class from button collection */
) {
companion object {
internal var lightningFactor: Float = 0.33F
const val lightningFactor: Float = 0.33F
enum class Type {
TOGGLE,
CLICK
}
enum class Category(val translationKey: String, val categoryColor: Int, val shouldHide: Boolean = false) {
MISC("snowy.clickbox.misc", Colors.BLUE),
HUD("snowy.clickbox.hud", Colors.DARK_PURPLE),
RISKY("snowy.clickbox.risky", Colors.RED, !riskyCheats),
MACROS("snowy.clickbox.macros", Colors.GREEN);
companion object {

View File

@ -1,26 +1,24 @@
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 fr.username404.snowygui.gui.Renderable
import fr.username404.snowygui.utils.FontUtil
import java.time.LocalDateTime
@ButtonInfo(Category.MISC)
object Clock: ButtonImpl() {
@ButtonInfo(Category.HUD)
object Clock: ButtonImplWithHud() {
override fun toString(): String = LocalDateTime.now().run {
arrayOf(hour, minute, second).map {
(it.takeIf { (it >= 10) } ?: "0$it").toString()
}.reduce { previous, current -> "$previous:$current"}
}
init {
onEvent("HudRender") {
if (toggled) {
FontUtil.drawScaled(it.first() as PoseStack,
this@Clock.toString(),
5.0, 5.0,
0.85F, color = Colors.GOLD
)
}
override val hudRenderLambda = Renderable { poseStack ->
poseStack?.let {
FontUtil.drawScaled(
it, this@Clock.toString(),
5.0, 5.0,
0.85F, color = Colors.GOLD
)
}
}
}

View File

@ -12,6 +12,8 @@ enum class Colors(val hexValue: Int) {
RED(0x660000),
GREEN(0x00FF7F),
ORANGE(0xf18e33),
PURPLE(0xA97BFF);
PURPLE(0xA97BFF),
DARK_PURPLE(0x3C204C);
operator fun invoke() = hexValue
fun asTextColor(): TextColor = TextColor.fromRgb(hexValue)
}

View File

@ -0,0 +1,59 @@
package fr.username404.snowygui.gui.feature
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.gui.ColoredElement
import fr.username404.snowygui.gui.Renderable
import fr.username404.snowygui.utils.FontUtil
import fr.username404.snowygui.utils.RenderingUtil
import net.minecraft.client.Minecraft
@ButtonInfo(Category.HUD)
object Keystrokes: ButtonImplWithHud() {
private val hud = object : ColoredElement(x = 135.0, y = 325.0, height = 36, width = 36, color = Colors.BLACK(), opacity = 0.5F) {
private val keysArray by lazy {
Minecraft.getInstance().options.run {
arrayOf(keyLeft, keyDown, keyRight, keyUp) to arrayOf(0.5, 0.5, 0.5, 0.5)
}
}
private val minimumOpacity = opacity.toDouble()
private fun getDynamicOpacity(i: Int): Double {
val dynamicOpacity = keysArray.second
if (keysArray.first[i].isDown) {
if (dynamicOpacity[i] < 1) {
dynamicOpacity[i] += 0.05
}
} else {
if (dynamicOpacity[i] > minimumOpacity) {
dynamicOpacity[i] -= 0.05
}
}
return dynamicOpacity[i]
}
private fun drawKey(x: Double, y: Double, key: Int, poseStack: PoseStack? = null) {
RenderingUtil.prepareDraw()
RenderingUtil.drawRectangle(
x, y, height / 2, height / 2,
color = color, opacity = getDynamicOpacity(key).toFloat()
)
RenderingUtil.endDraw()
poseStack?.let {
FontUtil.drawScaled(
it, keysArray.first[key].translatedKeyMessage.string.uppercase(),
x + height.toDouble() / 5.5, y + height.toDouble() / 6.5,
scaleFactor = 1F, color = Colors.WHITE
)
}
}
private fun getNewPos(i: Int) = x + (i * (2 + (height / 2)))
override fun render(poseStack: PoseStack?) = with(RenderingUtil) {
for (i in 0 until 3) {
drawKey(getNewPos(i), y, i, poseStack)
}
drawKey(getNewPos(1), y - (height / 2) - 2, 3, poseStack)
}
}
override val hudRenderLambda = Renderable {
hud.display(it)
}
}

View File

@ -1,4 +1,4 @@
package fr.username404.snowygui.gui
package fr.username404.snowygui.utils
import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.Minecraft

View File

@ -0,0 +1,41 @@
package fr.username404.snowygui.utils
import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.BufferBuilder
import com.mojang.blaze3d.vertex.DefaultVertexFormat
import com.mojang.blaze3d.vertex.Tesselator
import com.mojang.blaze3d.vertex.VertexConsumer
import fr.username404.snowygui.gui.feature.Colors
import fr.username404.snowygui.gui.hextoRGB
import org.lwjgl.opengl.GL20
object RenderingUtil {
@JvmField val tessellator: Tesselator = Tesselator.getInstance()
@JvmField val buffer: BufferBuilder = tessellator.builder
@JvmStatic
fun VertexConsumer.colorIt(color: Int, opacity: Float = 1F): VertexConsumer = hextoRGB(color).run {
color(get(0), get(1), get(2), opacity)
}
fun VertexConsumer.colorEnd(color: Int, opacity: Float = 1F) = colorIt(color, opacity).endVertex()
fun prepareDraw() {
RenderSystem.disableTexture()
RenderSystem.enableBlend()
RenderSystem.defaultBlendFunc()
}
fun endDraw() {
RenderSystem.enableTexture()
RenderSystem.disableBlend()
}
fun drawRectangle(
x: Double, y: Double, height: Int, width: Int,
color: Int = Colors.TRANSPARENT(), opacity: Float = 1F
): Unit = buffer.run {
fun VertexConsumer.colorEnd() = colorEnd(color, opacity)
begin(GL20.GL_QUADS, DefaultVertexFormat.POSITION_COLOR)
vertex(x, y + height, 0.0).colorEnd()
vertex(x + width, y + height, 0.0).colorEnd()
vertex(x + width, y, 0.0).colorEnd()
vertex(x, y, 0.0).colorEnd()
tessellator.end()
}
}

View File

@ -17,6 +17,7 @@
"key.snowy.opengui": "Open the snowy gui",
"key.snowy.configkey": "Open the snowy configuration screen",
"snowy.clickbox.misc": "Miscellaneous",
"snowy.clickbox.hud": "HUD",
"snowy.clickbox.risky": "Risky",
"snowy.clickbox.macros": "Macros"
}

View File

@ -17,6 +17,7 @@
"key.snowy.opengui": "Ouvrir l'interface de snowy",
"key.snowy.configkey": "Ouvrir l'écran de configuration de snowy",
"snowy.clickbox.misc": "Divers",
"snowy.clickbox.hud": "ATH",
"snowy.clickbox.risky": "Risqué",
"snowy.clickbox.macros": "Macros"
}