Skip to content

Commit

Permalink
brought stuff over finally for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Stivais committed Apr 27, 2024
1 parent c91e45c commit 0738366
Show file tree
Hide file tree
Showing 31 changed files with 1,704 additions and 1 deletion.
106 changes: 106 additions & 0 deletions odinmain/src/main/kotlin/com/github/stivais/ui/UI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.github.stivais.ui

import com.github.stivais.ui.constraints.Constraints
import com.github.stivais.ui.constraints.px
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.Color as OdinColor
import me.odinmain.utils.render.text
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
class UI(
//val renderer: Renderer2D,
settings: UISettings? = null
) {

val settings: UISettings = settings ?: UISettings()

val main: Group = Group(Constraints(0.px, 0.px, 1920.px, 1080.px)).also {
it.initialize(this)
it.position()
}

constructor(block: Group.() -> Unit) : this() {
main.block()
}

var eventManager: EventManager? = EventManager(this)

val mx get() = eventManager!!.mouseX

val my get() = eventManager!!.mouseY

fun initialize() {
main.position()
}

// frametime metrics
private var frames: Int = 0
private var frameTime: Long = 0
private var performance: String = ""

fun render() {
val start = System.nanoTime()
// renderer.beginFrame()
main.position()
main.render()
if (settings.frameMetrics) {
text(performance, main.width, main.height, OdinColor.WHITE, 16f, align = TextAlign.Right)
}
// renderer.endFrame()
if (settings.frameMetrics) {
frames++
frameTime += System.nanoTime() - start
if (frames > 100) {
performance =
"elements: ${getElementAmount(main, false)}, " +
"elements rendering: ${getElementAmount(main, true)}," +
"frametime avg: ${(frameTime / frames) / 1_000_000.0}ms"
frames = 0
frameTime = 0
}
}
frames++
frameTime += System.nanoTime() - start
}

fun getElementAmount(element: Element, onlyRender: Boolean): Int {
var amount = 0
if (!(onlyRender && !element.renders)) {
amount++
element.elements?.let {
for (i in it) {
amount += getElementAmount(i, onlyRender)
}
}
}
return amount
}

fun resize(width: Int, height: Int) {
main.constraints.width = width.px
main.constraints.height = height.px
}

fun focus(element: Element) {
if (eventManager == null) return logger.warning("Event Manager isn't setup, but called focus")
eventManager!!.focus(element)
}

fun unfocus() {
if (eventManager == null) return logger.warning("Event Manager isn't setup, but called unfocus")
eventManager!!.unfocus()
}

inline fun settings(block: UISettings.() -> Unit): UI {
settings.apply(block)
return this
}

companion object {
val logger: Logger = Logger.getLogger("UI")
}
}
76 changes: 76 additions & 0 deletions odinmain/src/main/kotlin/com/github/stivais/ui/UIScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.github.stivais.ui

import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiScreen
import org.lwjgl.input.Mouse

class UIScreen(val ui: UI) : GuiScreen() {

private var previousWidth: Int = 0
private var previousHeight: Int = 0

override fun initGui() {
ui.initialize()
}

override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) {
val w = mc.framebuffer.framebufferWidth
val h = mc.framebuffer.framebufferHeight
if (w != previousWidth || h != previousHeight) {
ui.resize(w, h)
previousWidth = w
previousHeight = h
}
ui.render()
}

override fun mouseClicked(mouseX: Int, mouseY: Int, button: Int) {
ui.eventManager?.onMouseClick(button)
}

override fun mouseReleased(mouseX: Int, mouseY: Int, button: Int) {
ui.eventManager?.onMouseRelease(button)
}

fun mouseMoved(mx: Float, my: Float) {
ui.eventManager?.onMouseMove(mx, my)
}

override fun handleMouseInput() {
ui.eventManager?.apply {
val mx = Mouse.getX().toFloat()
val my = Mouse.getY().toFloat()

if (mouseX != mx || mouseY != my) {
onMouseMove(mx, my)
}

val scroll = Mouse.getEventDWheel()
if (scroll != 0) {
onMouseScroll(scroll.toFloat())
}
}
super.handleMouseInput()
}

override fun keyTyped(typedChar: Char, keyCode: Int) {
if (ui.eventManager?.onKeyType(typedChar) == true) return
if (ui.eventManager?.onKeycodePressed(keyCode) == true) return
super.keyTyped(typedChar, keyCode)
}

// no key released because 1.8.9 doesn't have it and I don't want to manually recreate it
// override fun keyReleased(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
// if (ui.eventManager?.onKeyReleased(keyCode) == true) {
// return true
// }
// return super.keyPressed(keyCode, scanCode, modifiers)
// }

override fun onResize(mcIn: Minecraft?, w: Int, h: Int) {
ui.resize(mc.framebuffer.framebufferWidth, mc.framebuffer.framebufferHeight)
super.onResize(mcIn, w, h)
}

override fun doesGuiPauseGame(): Boolean = false
}
12 changes: 12 additions & 0 deletions odinmain/src/main/kotlin/com/github/stivais/ui/UISettings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@file:Suppress("UNUSED")

package com.github.stivais.ui

// add more customizablity
class UISettings {

var positionOnAdd: Boolean = false

var frameMetrics: Boolean = true

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.stivais.ui.animation

class Animation(private var duration: Float, val type: Animations, var from: Float = 0f, var to: Float = 1f) {

private var time: Long = System.nanoTime()

var finished: Boolean = false

fun get(): Float {
val percent = ((System.nanoTime() - time) / duration)
finished = percent >= 1f
return (if (finished) to else from + (to - from) * type.getValue(percent))
}

override fun toString(): String = "Animation(duration=$duration)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.stivais.ui.animation

import kotlin.math.pow

private interface Strategy {
fun getValue(percent: Float): Float
}

// todo: add more
enum class Animations : Strategy {
Linear {
override fun getValue(percent: Float): Float = percent
},
EaseInQuint {
override fun getValue(percent: Float): Float = percent * percent * percent * percent * percent
},
EaseOutQuint {
override fun getValue(percent: Float): Float = 1 - (1 - percent).pow(5f)
},
EaseInOutQuint {
override fun getValue(percent: Float): Float {
return if (percent < 0.5f) 16f * percent * percent * percent * percent * percent
else 1 - (-2 * percent + 2).pow(5f) / 2f
}
};
}
Loading

0 comments on commit 0738366

Please sign in to comment.