Add GUI utils.

This commit is contained in:
Username404-59 2021-04-10 14:41:42 +02:00
parent 7b100967d6
commit 03f2963486
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
8 changed files with 95 additions and 14 deletions

View File

@ -0,0 +1,10 @@
package fr.username404.snowygui
import fr.username404.snowygui.gui.Element
import fr.username404.snowygui.gui.SnowyScreen
class ClickGui: SnowyScreen() {
override val components: MutableSet<Element> = mutableSetOf(
// TODO Add components to the ClickGui
)
}

View File

@ -1,8 +0,0 @@
package fr.username404.snowygui
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.TranslatableComponent
internal class SnowyScreen: Screen(TranslatableComponent("screen.snowy.gui")) {
}

View File

@ -1,7 +1,7 @@
package fr.username404.snowygui.config
import com.mojang.blaze3d.platform.InputConstants
import fr.username404.snowygui.SnowyScreen
import fr.username404.snowygui.ClickGui
import net.minecraft.client.KeyMapping
import net.minecraft.client.Minecraft
import org.lwjgl.glfw.GLFW.GLFW_KEY_U
@ -17,7 +17,7 @@ object AddKeyMaps {
) to lambda
}
val list: MutableMap<KeyMapping, (() -> Unit)?> = mutableMapOf(
mkMap("opengui", GLFW_KEY_Y) { Minecraft.getInstance().setScreen(SnowyScreen()) },
mkMap("opengui", GLFW_KEY_Y) { Minecraft.getInstance().setScreen(ClickGui()) },
mkMap("configkey", GLFW_KEY_U) { Minecraft.getInstance().setScreen(SnowyConfigScreen()) }
)
}

View File

@ -1,8 +1,10 @@
package fr.username404.snowygui.config
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.TranslatableComponent
open class SnowyConfigScreen: Screen(TranslatableComponent("screen.snowygui.config")) { // TODO Actual config screen
import fr.username404.snowygui.gui.Element
import fr.username404.snowygui.gui.SnowyScreen
open class SnowyConfigScreen: SnowyScreen("screen.snowygui.config") { // TODO Actual config screen
override val components: MutableSet<Element>? = mutableSetOf(
// TODO Add components to the config gui
)
}

View File

@ -0,0 +1,9 @@
package fr.username404.snowygui.gui
import fr.username404.snowygui.rendering.Util
class Box(x: Int, y: Int, scale: Int): Element(x, y, 20 * scale, 10 * scale) {
override fun render() {
Util.UIRect(x, y, this.width, this.height, opacity = 1F)
}
}

View File

@ -0,0 +1,10 @@
package fr.username404.snowygui.gui
fun interface Renderable { fun render() }
abstract class Element(
var x: Int, var y: Int,
val width: Int, val height: Int
): Renderable {
fun display() { if (!hidden) render() }
var hidden: Boolean = false
}

View File

@ -0,0 +1,17 @@
package fr.username404.snowygui.gui
import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.TranslatableComponent
abstract class SnowyScreen(translatableString: String = "screen.snowy.gui", private val willPauseScreen: Boolean = false): Screen(TranslatableComponent(translatableString)) {
open val components: MutableSet<Element>? = null
override fun render(poseStack: PoseStack?, i: Int, j: Int, f: Float) {
if (poseStack != null) {
components?.forEach {
it.display()
}
}
}
override fun isPauseScreen(): Boolean = willPauseScreen
}

View File

@ -0,0 +1,41 @@
package fr.username404.snowygui.rendering
import com.mojang.blaze3d.platform.GlStateManager
import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.BufferBuilder
import com.mojang.blaze3d.vertex.DefaultVertexFormat
import com.mojang.blaze3d.vertex.PoseStack
import com.mojang.blaze3d.vertex.Tesselator
import net.minecraft.client.Minecraft
import org.lwjgl.opengl.GL11
internal object Util {
private val tessellator: Tesselator = Tesselator.getInstance()
private val buffer: BufferBuilder = tessellator.builder
fun UIRect(x: Int, y: Int, width: Int, height: Int, color: Int = -0x1, opacity: Float) {
val r = (color shr 16 and 255) / 255.0f
val g = (color shr 8 and 255) / 255.0f
val b = (color and 255) / 255.0f
RenderSystem.enableBlend()
RenderSystem.disableTexture()
RenderSystem.blendFuncSeparate(
GlStateManager.SourceFactor.SRC_ALPHA,
GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA,
GlStateManager.SourceFactor.ONE,
GlStateManager.DestFactor.ZERO
)
with(buffer) {
begin(GL11.GL_QUADS, DefaultVertexFormat.POSITION_COLOR)
vertex(x.toDouble(), y.toDouble() + height, 0.0).color(r, g, b, opacity).endVertex()
vertex(x.toDouble() + width, y.toDouble() + height, 0.0).color(r, g, b, opacity).endVertex()
vertex(x.toDouble() + width, y.toDouble(), 0.0).color(r, g, b, opacity).endVertex()
vertex(x.toDouble(), y.toDouble(), 0.0).color(r, g, b, opacity).endVertex()
tessellator.end()
RenderSystem.enableTexture()
RenderSystem.disableBlend()
}
}
fun UIString(poseStack: PoseStack, string: String, i: Float, j: Float) {
Minecraft.getInstance().font.draw(poseStack, string, i, j, 10)
}
}