diff options
author | Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> | 2024-02-19 13:42:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-19 13:42:03 +0100 |
commit | ab769839034888bc10ddfb2ef815be2400d64641 (patch) | |
tree | 4ad0b31063374402960ba3dd82e19517fcdb4b6a /src/main/java/at/hannibal2/skyhanni/utils | |
parent | 7157cd389dd7d5d22de3a713876649fe18a3918f (diff) | |
download | skyhanni-ab769839034888bc10ddfb2ef815be2400d64641.tar.gz skyhanni-ab769839034888bc10ddfb2ef815be2400d64641.tar.bz2 skyhanni-ab769839034888bc10ddfb2ef815be2400d64641.zip |
Added support for Renderable progressBar. #887
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
3 files changed, 39 insertions, 1 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt index 8eab43636..cf851d65f 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ColorUtils.kt @@ -13,4 +13,10 @@ object ColorUtils { fun getBlue(colour: Int) = colour and 0xFF fun getAlpha(colour: Int) = colour shr 24 and 0xFF + + fun blendRGB(start: Color, end: Color, percent: Double) = Color( + (start.red * (1 - percent) + end.red * percent).toInt(), + (start.green * (1 - percent) + end.green * percent).toInt(), + (start.blue * (1 - percent) + end.blue * percent).toInt() + ) } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt index 639d57bd0..608bd2e11 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt @@ -177,7 +177,7 @@ object NumberUtil { } fun percentageColor(have: Long, max: Long): LorenzColor { - val percentage = have.toDouble() / max.toDouble() + val percentage = have.fractionOf(max) return when { percentage > 0.9 -> LorenzColor.DARK_GREEN percentage > 0.75 -> LorenzColor.GREEN @@ -207,4 +207,9 @@ object NumberUtil { val Int.milion get() = this * 1_000_000.0 private val Int.bilion get() = this * 1_000_000_000.0 val Double.milion get() = (this * 1_000_000.0).toLong() + + /** @return clamped to [0.0, 1.0]**/ + fun Number.fractionOf(maxValue: Number) = maxValue.toDouble().takeIf { it != 0.0 }?.let { max -> + this.toDouble() / max + }?.coerceIn(0.0, 1.0) ?: 1.0 } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt index 933b61d38..5224cef0f 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -2,6 +2,7 @@ package at.hannibal2.skyhanni.utils.renderables import at.hannibal2.skyhanni.config.core.config.gui.GuiPositionEditor import at.hannibal2.skyhanni.data.ToolTipData +import at.hannibal2.skyhanni.utils.ColorUtils import at.hannibal2.skyhanni.utils.LorenzLogger import at.hannibal2.skyhanni.utils.NEUItems.renderOnScreen import at.hannibal2.skyhanni.utils.RenderUtils.HorizontalAlignment @@ -14,6 +15,7 @@ import net.minecraft.client.gui.inventory.GuiEditSign import net.minecraft.client.renderer.GlStateManager import net.minecraft.item.ItemStack import org.lwjgl.input.Mouse +import java.awt.Color import java.util.Collections import kotlin.math.max @@ -293,5 +295,30 @@ interface Renderable { override fun render(posX: Int, posY: Int) { } } + + fun progressBar( + percent: Double, + startColor: Color = Color(255, 0, 0), + endColor: Color = Color(0, 255, 0), + width: Int = 30, + height: Int = 4, + horizontalAlign: HorizontalAlignment = HorizontalAlignment.LEFT, + verticalAlign: VerticalAlignment = VerticalAlignment.TOP, + ) = object : Renderable { + override val width = width + override val height = height + override val horizontalAlign = horizontalAlign + override val verticalAlign = verticalAlign + + val progress = (1.0 + percent * (width - 2.0)).toInt() + val color = ColorUtils.blendRGB(startColor, endColor, percent) + + override fun render(posX: Int, posY: Int) { + Gui.drawRect(0, 0, width, height, 0xFF43464B.toInt()) + Gui.drawRect(1, 1, width - 1, height - 1, color.darker().rgb) + Gui.drawRect(1, 1, progress, height - 1, color.rgb) + } + + } } } |