Skip to content

Commit

Permalink
basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stivais committed Apr 27, 2024
1 parent 0738366 commit 76fad6d
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 3 deletions.
27 changes: 27 additions & 0 deletions odinmain/src/main/kotlin/com/github/stivais/ui/Test.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
}
12 changes: 11 additions & 1 deletion odinmain/src/main/kotlin/com/github/stivais/ui/UI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -65,6 +74,7 @@ class UI(
}
frames++
frameTime += System.nanoTime() - start
GlStateManager.popMatrix()
}

fun getElementAmount(element: Element, onlyRender: Boolean): Int {
Expand Down
2 changes: 1 addition & 1 deletion odinmain/src/main/kotlin/com/github/stivais/ui/UIScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 ->
Expand Down
76 changes: 76 additions & 0 deletions odinmain/src/main/kotlin/com/github/stivais/ui/elements/dsl.kt
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions odinmain/src/main/kotlin/me/odinmain/commands/impl/DevCommand.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() }
Expand Down

0 comments on commit 76fad6d

Please sign in to comment.