Skip to content

Commit

Permalink
number setting in gui
Browse files Browse the repository at this point in the history
  • Loading branch information
Stivais committed Apr 29, 2024
1 parent 90ff6f5 commit b58a64f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import me.odinmain.features.Module
import me.odinmain.features.ModuleManager.modules
import me.odinmain.features.impl.render.ClickGUIModule.color
import me.odinmain.features.settings.impl.BooleanSetting
import me.odinmain.features.settings.impl.NumberSetting
import me.odinmain.utils.capitalizeFirst
import kotlin.math.roundToInt

@JvmField
val mainColor = Color { color.rgba }

// note: currently settings that have a dependency don't hide
fun clickGUI() = UI {
for (panel in Category.entries) {
column(at(x = panel.x.px, y = panel.y.px)) {
Expand Down Expand Up @@ -80,28 +83,50 @@ private fun Element.module(module: Module) {
if (setting.hidden) continue
// temp
when (setting) {
is BooleanSetting -> {
group(size(w = 240.px, h = 40.px)) {
text(
text = setting.name,
at = at(6.px, Center),
size = 12.px
)
button(
constraints = constrain(x = -10.px, y = Center, w = 20.px, h = 20.px),
offColor = Color.RGB(38, 38, 38),
onColor = mainColor,
on = setting.enabled,
radii = radii(all = 5)
) {
onClick(0) {
setting.enabled = !setting.enabled
true
}
outline(color = mainColor)
is BooleanSetting -> group(size(w = 240.px, h = 40.px)) {
text(
text = setting.name,
at = at(6.px, Center),
size = 12.px
)
button(
constraints = constrain(x = -10.px, y = Center, w = 20.px, h = 20.px),
offColor = Color.RGB(38, 38, 38),
onColor = mainColor,
on = setting.enabled,
radii = radii(all = 5)
) {
onClick(0) {
setting.enabled = !setting.enabled
true
}
outline(color = mainColor)
}
}

is NumberSetting -> group(size(240.px, 40.px)) {
text(
text = setting.name,
at(x = 6.px, y = Center - 3.px),
size = 16.px
)
val display = text(
text = "${(setting.valueDouble * 100.0).roundToInt() / 100.0}",
at(x = -6.px, y = Center - 3.px),
size = 16.px
)
val slider = slider(
constraints = constrain(6.px, -5.px, 228.px, 7.px),
value = setting.valueDouble,
min = setting.min,
max = setting.max,
onChange = { percent ->
setting.valueDouble = percent * (setting.max - setting.min) + setting.min
display.text = "${(setting.valueDouble * 100.0).roundToInt() / 100.0}"
}
)
takeEvents(from = slider)
}
}
}
background(color = Color.RGB(38, 38, 38, 0.7f))
Expand Down
25 changes: 10 additions & 15 deletions odinmain/src/main/kotlin/com/github/stivais/ui/UI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ 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 com.github.stivais.ui.utils.forLoop
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
class UI(
//val renderer: Renderer2D,
settings: UISettings? = null
Expand All @@ -36,8 +35,12 @@ class UI(

val my get() = eventManager!!.mouseY

var afterInit: ArrayList<() -> Unit>? = null

fun initialize() {
main.position()
afterInit?.forLoop { it() }
afterInit = null
}

// frametime metrics
Expand All @@ -48,25 +51,21 @@ 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, 12f, align = TextAlign.Right, verticalAlign = TextPos.Bottom)
}
// renderer.endFrame()

if (settings.frameMetrics) {
frames++
frameTime += System.nanoTime() - start
if (frames > 100) {
performance =
"elements: ${getElementAmount(main, false)}, " +
"elements rendering: ${getElementAmount(main, true)}," +
"elements: ${getStats(main, false)}, " +
"elements rendering: ${getStats(main, true)}," +
"frametime avg: ${(frameTime / frames) / 1_000_000.0}ms"
frames = 0
frameTime = 0
Expand All @@ -77,15 +76,11 @@ class UI(
GlStateManager.popMatrix()
}

fun getElementAmount(element: Element, onlyRender: Boolean): Int {
private fun getStats(element: Element, onlyRender: Boolean): Int {
var amount = 0
if (!(onlyRender && !element.renders)) {
amount++
element.elements?.let {
for (i in it) {
amount += getElementAmount(i, onlyRender)
}
}
element.elements?.forLoop { amount += getStats(it, onlyRender) }
}
return amount
}
Expand Down
6 changes: 6 additions & 0 deletions odinmain/src/main/kotlin/com/github/stivais/ui/UIScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.stivais.ui

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

class UIScreen(val ui: UI) : GuiScreen() {
Expand Down Expand Up @@ -34,7 +35,12 @@ class UIScreen(val ui: UI) : GuiScreen() {
previousWidth = w
previousHeight = h
}

GlStateManager.pushMatrix()
GlStateManager.scale(0.5f, 0.5f, 0.5f)
GlStateManager.translate(0f, 0f, 0f)
ui.render()
GlStateManager.popMatrix()
}

override fun mouseClicked(mouseX: Int, mouseY: Int, button: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ abstract class Element(constraints: Constraints?) {
return { target.accept(this) }
}

fun afterInitialization(block: () -> Unit) {
if (ui.afterInit == null) ui.afterInit = arrayListOf()
ui.afterInit!!.add(block)
}

fun takeEvents(from: Element) {
if (from.events == null) return logger.warning("Tried to take event from an element that doesn't have events")
if (events != null) {
Expand Down
55 changes: 50 additions & 5 deletions odinmain/src/main/kotlin/com/github/stivais/ui/elements/dsl.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.github.stivais.ui.elements

import com.github.stivais.ui.animation.Animations
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.constraints.*
import com.github.stivais.ui.constraints.measurements.Animatable
import com.github.stivais.ui.constraints.sizes.Copying
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.events.onMouseMove
import com.github.stivais.ui.events.onRelease
import com.github.stivais.ui.utils.animate
import com.github.stivais.ui.utils.radii
import com.github.stivais.ui.utils.seconds

fun Element.column(constraints: Constraints? = null, block: Column.() -> Unit = {}): Column {
Expand Down Expand Up @@ -79,4 +82,46 @@ fun Element.button(
}
dsl()
}
}
}

fun Element.slider(
constraints: Constraints?,
value: Double,
min: Double,
max: Double,
onChange: (percent: Float) -> Unit
): Block {
var dragging = false
return block(constraints, Color.RGB(-0xefeff0), radii(3)) {
val sliderAnim = Animatable.Raw(0f)
// color is temp i cba to put in params yet
val color = Color.Animated(Color.RGB(50, 150, 220), Color.RGB(75, 175, 245))
afterInitialization {
sliderAnim.to(((value - min) / (max - min) * width).toFloat())
}
block(constrain(0.px, 0.px, sliderAnim, Copying), color = color, radii(all = 3f))

onClick(0) {
val pos = (ui.eventManager!!.mouseX - x).coerceIn(0f, width)
sliderAnim.animate(to = pos, 0.75.seconds, Animations.EaseOutQuint)
onChange(pos / width)
dragging = true
true
}
onMouseMove {
if (dragging) {
val pos = (ui.eventManager!!.mouseX - x).coerceIn(0f, width)
sliderAnim.to(pos)
onChange(pos / width)
}
true
}
onRelease(0) {
dragging = false
}
onMouseEnterExit {
color.animate(0.25.seconds)
true
}
}
}
4 changes: 1 addition & 3 deletions odinmain/src/main/kotlin/me/odinmain/OdinMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import me.odinmain.features.impl.render.DevPlayers
import me.odinmain.features.impl.render.WaypointManager
import me.odinmain.features.impl.skyblock.PartyNote
import me.odinmain.font.OdinFont
import me.odinmain.lwjgl.LWJGLTest
import me.odinmain.ui.clickgui.ClickGUI
import me.odinmain.ui.util.shader.RoundedRect
import me.odinmain.utils.ServerUtils
Expand Down Expand Up @@ -74,7 +73,6 @@ object OdinMain {
WaypointManager,
DevPlayers,
PartyNote,
LWJGLTest::class,
//HighlightRenderer,
//OdinUpdater,
this
Expand Down Expand Up @@ -120,7 +118,7 @@ object OdinMain {
}

fun onTick() {
this.display = LWJGLTest()
// this.display = LWJGLTest()
if (display != null) {
mc.displayGuiScreen(display)
display = null
Expand Down

0 comments on commit b58a64f

Please sign in to comment.