From 76fad6daa4b7d022673e94772057330a9ab52c29 Mon Sep 17 00:00:00 2001 From: stivais Date: Sat, 27 Apr 2024 20:40:02 +0300 Subject: [PATCH] basic test --- .../main/kotlin/com/github/stivais/ui/Test.kt | 27 +++++++ .../main/kotlin/com/github/stivais/ui/UI.kt | 12 ++- .../kotlin/com/github/stivais/ui/UIScreen.kt | 2 +- .../com/github/stivais/ui/elements/Element.kt | 2 + .../com/github/stivais/ui/elements/dsl.kt | 76 +++++++++++++++++++ .../github/stivais/ui/elements/impl/Text.kt | 4 +- .../me/odinmain/commands/impl/DevCommand.kt | 10 +++ 7 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 odinmain/src/main/kotlin/com/github/stivais/ui/Test.kt diff --git a/odinmain/src/main/kotlin/com/github/stivais/ui/Test.kt b/odinmain/src/main/kotlin/com/github/stivais/ui/Test.kt new file mode 100644 index 000000000..0995336ff --- /dev/null +++ b/odinmain/src/main/kotlin/com/github/stivais/ui/Test.kt @@ -0,0 +1,27 @@ +package com.github.stivais.ui + +import com.github.stivais.ui.color.Color +import com.github.stivais.ui.constraints.px +import com.github.stivais.ui.constraints.size +import com.github.stivais.ui.elements.block +import com.github.stivais.ui.elements.column +import com.github.stivais.ui.elements.text +import com.github.stivais.ui.events.onClick +import me.odinmain.utils.skyblock.modMessage + +fun basic() = UI { + column { + repeat(5) { + block( + constraints = size(100.px, 100.px), + color = Color.RGB(255, 0, 0) + ) { + text(text = "$it") + onClick(0) { + modMessage("Clicked block $it") + true + } + } + } + } +} \ No newline at end of file diff --git a/odinmain/src/main/kotlin/com/github/stivais/ui/UI.kt b/odinmain/src/main/kotlin/com/github/stivais/ui/UI.kt index 77dde53a7..941e2aeb4 100644 --- a/odinmain/src/main/kotlin/com/github/stivais/ui/UI.kt +++ b/odinmain/src/main/kotlin/com/github/stivais/ui/UI.kt @@ -6,8 +6,11 @@ import com.github.stivais.ui.elements.Element import com.github.stivais.ui.elements.impl.Group import com.github.stivais.ui.events.EventManager import me.odinmain.utils.render.TextAlign +import me.odinmain.utils.render.TextPos import me.odinmain.utils.render.Color as OdinColor import me.odinmain.utils.render.text +import me.odinmain.utils.render.translate +import net.minecraft.client.renderer.GlStateManager import java.util.logging.Logger // TODO: When finished with dsl and inputs, bring to its own window instead of inside of minecraft for benchmarking and reduce all memory usage @@ -44,11 +47,17 @@ class UI( fun render() { val start = System.nanoTime() + + GlStateManager.pushMatrix() + GlStateManager.scale(0.5f, 0.5f, 0.5f) + translate(0f, 0f, 0f) + + // renderer.beginFrame() main.position() main.render() if (settings.frameMetrics) { - text(performance, main.width, main.height, OdinColor.WHITE, 16f, align = TextAlign.Right) + text(performance, main.width, main.height, OdinColor.WHITE, 12f, align = TextAlign.Right, verticalAlign = TextPos.Bottom) } // renderer.endFrame() if (settings.frameMetrics) { @@ -65,6 +74,7 @@ class UI( } frames++ frameTime += System.nanoTime() - start + GlStateManager.popMatrix() } fun getElementAmount(element: Element, onlyRender: Boolean): Int { diff --git a/odinmain/src/main/kotlin/com/github/stivais/ui/UIScreen.kt b/odinmain/src/main/kotlin/com/github/stivais/ui/UIScreen.kt index 8281e2e8a..94817a6a2 100644 --- a/odinmain/src/main/kotlin/com/github/stivais/ui/UIScreen.kt +++ b/odinmain/src/main/kotlin/com/github/stivais/ui/UIScreen.kt @@ -39,7 +39,7 @@ class UIScreen(val ui: UI) : GuiScreen() { override fun handleMouseInput() { ui.eventManager?.apply { val mx = Mouse.getX().toFloat() - val my = Mouse.getY().toFloat() + val my = mc.displayHeight - Mouse.getY() - 1f if (mouseX != mx || mouseY != my) { onMouseMove(mx, my) diff --git a/odinmain/src/main/kotlin/com/github/stivais/ui/elements/Element.kt b/odinmain/src/main/kotlin/com/github/stivais/ui/elements/Element.kt index e56480084..11adb5f8c 100644 --- a/odinmain/src/main/kotlin/com/github/stivais/ui/elements/Element.kt +++ b/odinmain/src/main/kotlin/com/github/stivais/ui/elements/Element.kt @@ -11,6 +11,7 @@ import com.github.stivais.ui.constraints.positions.Center import com.github.stivais.ui.events.Event import com.github.stivais.ui.events.Mouse import com.github.stivais.ui.utils.forLoop +import me.odinmain.utils.render.rectangleOutline // Positioning (X or Y) should be: defined with a measurement or aligned by left/top, center, right/bottom (with some padding) @@ -106,6 +107,7 @@ abstract class Element(constraints: Constraints?) { } fun render() { + rectangleOutline(x, y, width, height, me.odinmain.utils.render.Color.WHITE, 0f, 2f) if (!renders) return draw() elements?.forLoop { element -> diff --git a/odinmain/src/main/kotlin/com/github/stivais/ui/elements/dsl.kt b/odinmain/src/main/kotlin/com/github/stivais/ui/elements/dsl.kt index 215e191d4..05d933f1c 100644 --- a/odinmain/src/main/kotlin/com/github/stivais/ui/elements/dsl.kt +++ b/odinmain/src/main/kotlin/com/github/stivais/ui/elements/dsl.kt @@ -1,2 +1,78 @@ package com.github.stivais.ui.elements +import com.github.stivais.ui.color.Color +import com.github.stivais.ui.constraints.Constraints +import com.github.stivais.ui.constraints.Measurement +import com.github.stivais.ui.constraints.copyParent +import com.github.stivais.ui.constraints.percent +import com.github.stivais.ui.elements.impl.* +import com.github.stivais.ui.events.onClick +import com.github.stivais.ui.events.onMouseEnterExit +import com.github.stivais.ui.utils.animate +import com.github.stivais.ui.utils.seconds + +fun Element.button( + constraints: Constraints? = null, + offColor: Color = Color.RGB(38, 38, 38), + onColor: Color = Color.RGB(50, 150, 220), + on: Boolean = false, + radii: FloatArray? = null, + dsl: Block.() -> Unit +): Block { + val mainColor = Color.Animated(offColor, onColor, on) + val hoverColor = Color.Animated(Color.TRANSPARENT, Color.RGB(255, 255, 255, 0.05f)) + + return block(constraints, mainColor, radii) { + block(copyParent(), color = hoverColor, radius = radii) { + onMouseEnterExit { + hoverColor.animate(0.25.seconds) + true + } + } + onClick(0) { + mainColor.animate(0.15.seconds) + false + } + dsl() + } +} + +fun Element.column(constraints: Constraints? = null, block: Column.() -> Unit = {}): Column { + val column = Column(constraints) + addElement(column) + column.block() + return column +} + +// todo: improve outline color +fun Element.block( + constraints: Constraints? = null, + color: Color, + radius: FloatArray? = null, + block: Block.() -> Unit = {} +): Block { + val block = if (radius != null) RoundedBlock(constraints, color, radius) else Block(constraints, color) + addElement(block) + block.block() + return block +} + +fun Element.text( + text: String, + at: Constraints? = null, + size: Measurement = 50.percent, + color: Color = Color.WHITE, + block: Text.() -> Unit = {} +): Text { + val text = Text(text, color, at, size) + addElement(text) + text.block() + return text +} + +fun Element.group(constraints: Constraints? = null, block: Group.() -> Unit = {}): Group { + val column = Group(constraints) + addElement(column) + column.block() + return column +} \ No newline at end of file diff --git a/odinmain/src/main/kotlin/com/github/stivais/ui/elements/impl/Text.kt b/odinmain/src/main/kotlin/com/github/stivais/ui/elements/impl/Text.kt index 9a9c5e61a..d3d6e9e45 100644 --- a/odinmain/src/main/kotlin/com/github/stivais/ui/elements/impl/Text.kt +++ b/odinmain/src/main/kotlin/com/github/stivais/ui/elements/impl/Text.kt @@ -8,6 +8,8 @@ import com.github.stivais.ui.constraints.px import com.github.stivais.ui.elements.Element import com.github.stivais.ui.utils.replaceUndefined import me.odinmain.font.OdinFont +import me.odinmain.utils.render.TextAlign +import me.odinmain.utils.render.TextPos import kotlin.reflect.KProperty class Text( @@ -39,7 +41,7 @@ class Text( } override fun draw() { - OdinFont.text(text, x, y, me.odinmain.utils.render.Color(color!!.rgba), height) + OdinFont.text(text, x, y, me.odinmain.utils.render.Color(color!!.rgba), height, align = TextAlign.Left, verticalAlign = TextPos.Top) // renderer.text(text, x, y, color!!.rgba, height) } diff --git a/odinmain/src/main/kotlin/me/odinmain/commands/impl/DevCommand.kt b/odinmain/src/main/kotlin/me/odinmain/commands/impl/DevCommand.kt index 419384db4..447cf300b 100644 --- a/odinmain/src/main/kotlin/me/odinmain/commands/impl/DevCommand.kt +++ b/odinmain/src/main/kotlin/me/odinmain/commands/impl/DevCommand.kt @@ -1,7 +1,10 @@ package me.odinmain.commands.impl import com.github.stivais.commodore.utils.GreedyString +import com.github.stivais.ui.UIScreen +import com.github.stivais.ui.basic import kotlinx.coroutines.launch +import me.odinmain.OdinMain.display import me.odinmain.OdinMain.mc import me.odinmain.OdinMain.scope import me.odinmain.commands.commodore @@ -25,6 +28,13 @@ import java.awt.datatransfer.StringSelection val devCommand = commodore("oddev") { //if (!isDev) return@commodore // i forgot to add require to recode lol + literal("ui") { + literal("basic").runs { display = UIScreen(basic()) } + } + + + + literal("getdata") { literal("entity").runs { copyEntityData() } literal("block").runs { copyBlockData() }