From 60a5bc45bff339d7ec16b9eba85495bd6713d812 Mon Sep 17 00:00:00 2001 From: twenty48 Date: Thu, 5 Dec 2024 17:18:39 +0530 Subject: [PATCH] more stuff, sliders and toggles --- .../blend/module/impl/client/ThemeModule.kt | 4 +- .../ui/dropdown/components/ModuleComponent.kt | 16 +++++++- .../values/BooleanValueComponent.kt | 9 ++--- .../components/values/NumberValueComponent.kt | 40 ++++++++++++++++++- .../kotlin/dev/blend/util/render/ColorUtil.kt | 6 +-- .../kotlin/dev/blend/util/render/DrawUtil.kt | 7 ++-- 6 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/dev/blend/module/impl/client/ThemeModule.kt b/src/main/kotlin/dev/blend/module/impl/client/ThemeModule.kt index 4c225c8..4f600fd 100644 --- a/src/main/kotlin/dev/blend/module/impl/client/ThemeModule.kt +++ b/src/main/kotlin/dev/blend/module/impl/client/ThemeModule.kt @@ -5,6 +5,7 @@ import dev.blend.module.api.Category import dev.blend.module.api.ModuleInfo import dev.blend.value.impl.ColorValue import dev.blend.value.impl.ModeValue +import dev.blend.value.impl.NumberValue import java.awt.Color @ModuleInfo( @@ -16,7 +17,8 @@ object ThemeModule: Module() { val accent = ColorValue("Accent", this, Color(0, 160, 255)) val secondary = ColorValue("Secondary", this, Color(160, 0, 255)) -// val theme = ModeValue("Theme", this, arrayOf("Dark", "Light")) + val theme = ModeValue("Theme", this, arrayOf("Dark", "Light")) + val gradientSpeed = NumberValue("Speed", this, 500.0, 200.0, 2000.0, 100.0) override fun onEnable() { this.set(false) diff --git a/src/main/kotlin/dev/blend/ui/dropdown/components/ModuleComponent.kt b/src/main/kotlin/dev/blend/ui/dropdown/components/ModuleComponent.kt index 29afba0..41321cd 100644 --- a/src/main/kotlin/dev/blend/ui/dropdown/components/ModuleComponent.kt +++ b/src/main/kotlin/dev/blend/ui/dropdown/components/ModuleComponent.kt @@ -67,8 +67,10 @@ class ModuleComponent( it.x = x it.y = y + veryRealHeight it.width = width - it.render(mouseX, mouseY) - veryRealHeight += it.height + if (it.value.visibility()) { + it.render(mouseX, mouseY) + veryRealHeight += it.height + } } expandAnimation.animate( if (expanded) veryRealHeight else initialHeight @@ -103,10 +105,20 @@ class ModuleComponent( } override fun release(mouseX: Double, mouseY: Double, mouseButton: Int): Boolean { + components.forEach { + if (it.release(mouseX, mouseY, mouseButton)) { + return true + } + } return false } override fun key(key: Int, scancode: Int, modifiers: Int): Boolean { + components.forEach { + if (it.key(key, scancode, modifiers)) { + return true + } + } return false } diff --git a/src/main/kotlin/dev/blend/ui/dropdown/components/values/BooleanValueComponent.kt b/src/main/kotlin/dev/blend/ui/dropdown/components/values/BooleanValueComponent.kt index 3b7c7db..58c02b2 100644 --- a/src/main/kotlin/dev/blend/ui/dropdown/components/values/BooleanValueComponent.kt +++ b/src/main/kotlin/dev/blend/ui/dropdown/components/values/BooleanValueComponent.kt @@ -17,7 +17,7 @@ class BooleanValueComponent( ) { private val toggleAnimation = SineOutAnimation() - private val clickAnimation = SineOutAnimation() + private val toggleDependentAnimation = SineOutAnimation() override fun init() { @@ -30,17 +30,16 @@ class BooleanValueComponent( val pillY = y + (height / 2.0) val indicatorRadius = 6.0 val indicatorOffset = 1.0 - val indicatorX = (pillX - ((indicatorRadius / 2) + indicatorOffset)) - ((pillWidth - (indicatorRadius + (indicatorOffset * 2.0))) * toggleAnimation.get()) + val indicatorX = (pillX - (pillWidth - indicatorRadius + (indicatorOffset * 2.0))) + (pillWidth - (indicatorRadius + (indicatorOffset * 2.0))) * toggleAnimation.get() val pillColor = ColorUtil.mixColors(ThemeHandler.gray, ThemeHandler.getPrimary(), toggleAnimation.get()) - val outline = ColorUtil.applyOpacity(ThemeHandler.getContrast(), toggleAnimation.get()) with(DrawUtil) { drawString(value.name, x + padding, y + (height / 2.0), 8, ThemeHandler.getTextColor(), Alignment.CENTER_LEFT) roundedRect(pillX, pillY, pillWidth, pillHeight, pillHeight / 2.0, pillColor, Alignment.CENTER_RIGHT) - roundedRect(indicatorX, pillY, indicatorRadius + ((indicatorRadius / 3.0) * clickAnimation.get()), indicatorRadius, indicatorRadius / 2.0, ThemeHandler.getContrast(), Alignment.CENTER) + roundedRect(indicatorX, pillY, indicatorRadius + ((indicatorRadius / 3.0) * toggleDependentAnimation.get()), indicatorRadius, indicatorRadius / 2.0, ThemeHandler.getContrast(), Alignment.CENTER) } toggleAnimation.animate(if (value.get()) 1.0 else 0.0) - clickAnimation.animate(if (toggleAnimation.finished) 0.0 else 1.0) + toggleDependentAnimation.animate(if (toggleAnimation.finished) 0.0 else 1.0) } override fun click(mouseX: Double, mouseY: Double, mouseButton: Int): Boolean { diff --git a/src/main/kotlin/dev/blend/ui/dropdown/components/values/NumberValueComponent.kt b/src/main/kotlin/dev/blend/ui/dropdown/components/values/NumberValueComponent.kt index 4c9ff75..441e8bd 100644 --- a/src/main/kotlin/dev/blend/ui/dropdown/components/values/NumberValueComponent.kt +++ b/src/main/kotlin/dev/blend/ui/dropdown/components/values/NumberValueComponent.kt @@ -3,7 +3,10 @@ package dev.blend.ui.dropdown.components.values import dev.blend.handler.impl.ThemeHandler import dev.blend.ui.dropdown.components.AbstractValueComponent import dev.blend.ui.dropdown.components.ModuleComponent +import dev.blend.util.animations.CubicOutAnimation +import dev.blend.util.animations.SineOutAnimation import dev.blend.util.render.Alignment +import dev.blend.util.render.ColorUtil import dev.blend.util.render.DrawUtil import dev.blend.value.impl.NumberValue @@ -11,22 +14,55 @@ class NumberValueComponent( parent: ModuleComponent, override val value: NumberValue ): AbstractValueComponent( - parent, value, height = 20.0 + parent, value, height = 30.0 ) { + private val dragAnimation = CubicOutAnimation() + private val dragDependentAnimation = SineOutAnimation() + private val selectAnimation = SineOutAnimation() + private var held = false + override fun init() { } override fun render(mouseX: Int, mouseY: Int) { - DrawUtil.drawString(value.name, x + 5.0, y + (height / 2.0), 8, ThemeHandler.getTextColor(), Alignment.CENTER_LEFT) + val sliderW = width - (padding * 2.0) + val sliderH = 2.0 + val sliderX = x + padding + val sliderY = (y + height) - (padding * 2.0) + val dragIndicator = 6.0 + (selectAnimation.get() * 2.0) + val holdIndicator = 4.0 + // (value - min) / (max - min) + // (15 - 10) / (20 - 10) = 0.5 + val relativeValue = (value.get().toDouble() - value.min.toDouble()) / (value.max.toDouble() - value.min.toDouble()) + val relativeMouseX = (mouseX - sliderX) / ((sliderX + sliderW) - sliderX) + if (held) { + value.set(value.min.toDouble() + relativeMouseX * (value.max.toDouble() - value.min.toDouble())) + } + + val heldColor = ColorUtil.mixColors(ThemeHandler.getTextColor(), ThemeHandler.getPrimary(), selectAnimation.get()) + with(DrawUtil) { + drawString(value.name, x + padding, y + padding, 8, ThemeHandler.getTextColor(), Alignment.CENTER_LEFT) + drawString(value.toString(), (x + width) - padding, y + padding, 8, heldColor, Alignment.CENTER_RIGHT) + roundedRect(sliderX, sliderY, sliderW, sliderH, sliderH / 2.0, ThemeHandler.getContrast(), Alignment.CENTER_LEFT) + roundedRect(sliderX, sliderY, dragAnimation.get(), sliderH, sliderH / 2.0, ThemeHandler.getPrimary(), Alignment.CENTER_LEFT) + roundedRect(sliderX + dragAnimation.get(), sliderY, dragIndicator + (dragDependentAnimation.get() * 3.0), dragIndicator, dragIndicator / 2.0, ThemeHandler.getContrast(), Alignment.CENTER) + roundedRect(sliderX + dragAnimation.get() , sliderY, holdIndicator + (dragDependentAnimation.get() * 3.0), holdIndicator, holdIndicator / 2.0, heldColor, Alignment.CENTER) + } + dragAnimation.duration = 100.0 + dragAnimation.animate(relativeValue * sliderW) + dragDependentAnimation.animate(if (dragAnimation.finished) 0.0 else 1.0) + selectAnimation.animate(if (held) 1.0 else 0.0) } override fun click(mouseX: Double, mouseY: Double, mouseButton: Int): Boolean { + held = true return false } override fun release(mouseX: Double, mouseY: Double, mouseButton: Int): Boolean { + held = false return false } diff --git a/src/main/kotlin/dev/blend/util/render/ColorUtil.kt b/src/main/kotlin/dev/blend/util/render/ColorUtil.kt index 5d2ebe9..ab0ceac 100644 --- a/src/main/kotlin/dev/blend/util/render/ColorUtil.kt +++ b/src/main/kotlin/dev/blend/util/render/ColorUtil.kt @@ -7,9 +7,9 @@ object ColorUtil { @JvmStatic fun mixColors(primary: Color, secondary: Color, factor: Double): Color { val otherFactor = 1.0 - factor - val redFactor = (primary.red * factor + secondary.red * otherFactor).toInt() - val greenFactor = (primary.green * factor + secondary.green * otherFactor).toInt() - val blueFactor = (primary.blue * factor + secondary.blue * otherFactor).toInt() + val redFactor = (primary.red * otherFactor + secondary.red * factor).toInt() + val greenFactor = (primary.green * otherFactor + secondary.green * factor).toInt() + val blueFactor = (primary.blue * otherFactor + secondary.blue * factor).toInt() return Color(redFactor, greenFactor, blueFactor) } @JvmStatic diff --git a/src/main/kotlin/dev/blend/util/render/DrawUtil.kt b/src/main/kotlin/dev/blend/util/render/DrawUtil.kt index 2becaac..0a70c43 100644 --- a/src/main/kotlin/dev/blend/util/render/DrawUtil.kt +++ b/src/main/kotlin/dev/blend/util/render/DrawUtil.kt @@ -184,9 +184,10 @@ object DrawUtil: IAccessor { private fun postRender() { RenderSystem.disableCull() RenderSystem.disableDepthTest() - RenderSystem.defaultBlendFunc() -// RenderSystem.enableBlend() -// RenderSystem.blendFuncSeparate(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SrcFactor.ZERO, GlStateManager.DstFactor.ONE) + RenderSystem.enableBlend() +// RenderSystem.defaultBlendFunc() +// RenderSystem.blendFunc(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA) + RenderSystem.blendFuncSeparate(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SrcFactor.ZERO, GlStateManager.DstFactor.ONE) } private fun alignX(x: Number, width: Number, alignment: Alignment): Float { return when (alignment) {