From 3636c7485119e8dca5651dce1360dc621e4f2441 Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Sat, 10 Apr 2021 19:17:46 +0200 Subject: [PATCH] Rename Box to ClickBox, make a ColoredElement class, draw strings and add more translations --- .../fr/username404/snowygui/ClickGui.kt | 3 ++ .../kotlin/fr/username404/snowygui/gui/Box.kt | 9 ------ .../fr/username404/snowygui/gui/ClickBox.kt | 31 +++++++++++++++++++ .../fr/username404/snowygui/gui/Element.kt | 25 +++++++++++++-- .../resources/assets/snowygui/lang/en_us.json | 3 +- .../resources/assets/snowygui/lang/fr_fr.json | 3 +- 6 files changed, 61 insertions(+), 13 deletions(-) delete mode 100644 common/src/main/kotlin/fr/username404/snowygui/gui/Box.kt create mode 100644 common/src/main/kotlin/fr/username404/snowygui/gui/ClickBox.kt diff --git a/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt b/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt index 6f03a47..9ec40c8 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/ClickGui.kt @@ -1,10 +1,13 @@ package fr.username404.snowygui +import fr.username404.snowygui.gui.ClickBox import fr.username404.snowygui.gui.Element import fr.username404.snowygui.gui.SnowyScreen +import net.minecraft.network.chat.TranslatableComponent class ClickGui: SnowyScreen() { override val components: MutableSet = mutableSetOf( + ClickBox(4.0, 4.0, TranslatableComponent("snowy.clickbox.misc")) // TODO Add components to the ClickGui ) } \ No newline at end of file diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/Box.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/Box.kt deleted file mode 100644 index ac9ce27..0000000 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/Box.kt +++ /dev/null @@ -1,9 +0,0 @@ -package fr.username404.snowygui.gui - -import com.mojang.blaze3d.vertex.PoseStack - -class Box(x: Int, y: Int, scale: Int): Element(x, y, 20 * scale, 10 * scale) { - override fun render(poseStack: PoseStack?) { - - } -} \ No newline at end of file diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/ClickBox.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/ClickBox.kt new file mode 100644 index 0000000..2806b98 --- /dev/null +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/ClickBox.kt @@ -0,0 +1,31 @@ +package fr.username404.snowygui.gui + +import com.mojang.blaze3d.systems.RenderSystem +import com.mojang.blaze3d.vertex.DefaultVertexFormat +import com.mojang.blaze3d.vertex.PoseStack +import net.minecraft.client.Minecraft +import net.minecraft.network.chat.TranslatableComponent +import org.lwjgl.opengl.GL20 + +class ClickBox(x: Double, y: Double, private val name: TranslatableComponent? = null): ColoredElement(x + 10, y + 10, 80, 10, 0x6C9E9D) { + var opacity = 0.5F + override fun render(poseStack: PoseStack?) { + RenderSystem.disableTexture() + RenderSystem.enableBlend() + RenderSystem.defaultBlendFunc() + with(buffer) { + begin(GL20.GL_POLYGON, DefaultVertexFormat.POSITION_COLOR) + vertex(x, y + height, 0.0).colorIt(color, opacity).endVertex() + vertex(x + width, y + height, 0.0).colorIt(color, opacity).endVertex() + vertex(x + width, y, 0.0).colorIt(color, opacity).endVertex() + vertex(x, y, 0.0).colorIt(color, opacity).endVertex() + tessellator.end() + } + RenderSystem.enableTexture() + RenderSystem.disableBlend() + + if (name != null) { + Minecraft.getInstance().font.draw(poseStack, name.string, x.toFloat() + 2, y.toFloat() + 1, TransparentColor) + } + } +} \ No newline at end of file diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/Element.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/Element.kt index 5d04334..4b89363 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/Element.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/Element.kt @@ -3,17 +3,38 @@ package fr.username404.snowygui.gui import com.mojang.blaze3d.vertex.BufferBuilder import com.mojang.blaze3d.vertex.PoseStack import com.mojang.blaze3d.vertex.Tesselator +import com.mojang.blaze3d.vertex.VertexConsumer fun interface Renderable { fun render(poseStack: PoseStack?) } abstract class Element( - var x: Int, var y: Int, + var x: Double, var y: Double, val width: Int, val height: Int ): Renderable { companion object Rendering { @JvmStatic protected val tessellator: Tesselator = Tesselator.getInstance() @JvmStatic protected val buffer: BufferBuilder = tessellator.builder - const val TransparentColor: Int = -0x1 } fun display(stack: PoseStack? = null) { if (!hidden) render(stack) } var hidden: Boolean = false +} + +abstract class ColoredElement(x: Double, y: Double, width: Int, height: Int, val color: Int) : Element(x, y, width, height) { + 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) + } + } + } +} + +fun hextoRGB(hex: Int): MutableList { + val returnedColorList = mutableListOf() + for (i in 2 downTo 0) { + returnedColorList.add( + (hex shr (i * 8) and 255) / 255.0F + ) + } + return returnedColorList } \ No newline at end of file diff --git a/common/src/main/resources/assets/snowygui/lang/en_us.json b/common/src/main/resources/assets/snowygui/lang/en_us.json index 531debe..abc5a6d 100644 --- a/common/src/main/resources/assets/snowygui/lang/en_us.json +++ b/common/src/main/resources/assets/snowygui/lang/en_us.json @@ -3,5 +3,6 @@ "screen.snowy.config": "Snowy configuration screen", "category.snowy.keycategory": "SnowyGUI", "key.snowy.opengui": "Open the snowy gui", - "key.snowy.configkey": "Open the snowy configuration screen" + "key.snowy.configkey": "Open the snowy configuration screen", + "snowy.clickbox.misc": "Misc" } \ No newline at end of file diff --git a/common/src/main/resources/assets/snowygui/lang/fr_fr.json b/common/src/main/resources/assets/snowygui/lang/fr_fr.json index e52e999..b6a15a9 100644 --- a/common/src/main/resources/assets/snowygui/lang/fr_fr.json +++ b/common/src/main/resources/assets/snowygui/lang/fr_fr.json @@ -3,5 +3,6 @@ "screen.snowy.config": "Configuration de snowy", "category.snowy.keycategory": "SnowyGUI", "key.snowy.opengui": "Ouvrir l'interface de snowy", - "key.snowy.configkey": "Ouvrir l'écran de configuration de snowy" + "key.snowy.configkey": "Ouvrir l'écran de configuration de snowy", + "snowy.clickbox.misc": "Divers" } \ No newline at end of file