From 3a24eb80f328ec55cb4587e712a6f0c02dfa5c89 Mon Sep 17 00:00:00 2001 From: nea Date: Thu, 1 Jun 2023 22:38:09 +0200 Subject: [PATCH] Add WBar --- .../kotlin/moe/nea/firmament/Firmament.kt | 3 + src/main/kotlin/moe/nea/firmament/gui/WBar.kt | 69 ++++++++++++++++++ .../moe/nea/firmament/util/colorconversion.kt | 6 ++ .../assets/firmament/textures/gui/bar.png | Bin 0 -> 679 bytes 4 files changed, 78 insertions(+) create mode 100644 src/main/kotlin/moe/nea/firmament/gui/WBar.kt create mode 100644 src/main/kotlin/moe/nea/firmament/util/colorconversion.kt create mode 100644 src/main/resources/assets/firmament/textures/gui/bar.png diff --git a/src/main/kotlin/moe/nea/firmament/Firmament.kt b/src/main/kotlin/moe/nea/firmament/Firmament.kt index b3e2115..6132d3b 100644 --- a/src/main/kotlin/moe/nea/firmament/Firmament.kt +++ b/src/main/kotlin/moe/nea/firmament/Firmament.kt @@ -49,6 +49,7 @@ import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json import kotlin.coroutines.EmptyCoroutineContext import net.minecraft.command.CommandRegistryAccess +import net.minecraft.util.Identifier import moe.nea.firmament.commands.registerFirmamentCommand import moe.nea.firmament.dbus.FirmamentDbusObject import moe.nea.firmament.features.FeatureManager @@ -129,4 +130,6 @@ object Firmament : ModInitializer, ClientModInitializer { }) } + + fun identifier(path: String) = Identifier(MOD_ID, path) } diff --git a/src/main/kotlin/moe/nea/firmament/gui/WBar.kt b/src/main/kotlin/moe/nea/firmament/gui/WBar.kt new file mode 100644 index 0000000..879805d --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/gui/WBar.kt @@ -0,0 +1,69 @@ +package moe.nea.firmament.gui + +import io.github.cottonmc.cotton.gui.client.ScreenDrawing +import io.github.cottonmc.cotton.gui.widget.WWidget +import io.github.cottonmc.cotton.gui.widget.data.Texture +import me.shedaniel.math.Color +import net.minecraft.client.util.math.MatrixStack +import moe.nea.firmament.Firmament + +data class WBar( + var progress: Double, + val total: Double, + val fillColor: Color, + val emptyColor: Color, +) : WWidget() { + val resource = Firmament.identifier("textures/gui/bar.png") + val left = Texture(resource, 0 / 64F, 0 / 64F, 4 / 64F, 8 / 64F) + val middle = Texture(resource, 4 / 64F, 0 / 64F, 4 / 64F, 8 / 64F) + val right = Texture(resource, 8 / 64F, 0 / 64F, 4 / 64F, 8 / 64F) + val segmentOverlay = Texture(resource, 12 / 64F, 0 / 64F, 15 / 64F, 8 / 64F) + + override fun canResize(): Boolean { + return true + } + + private fun drawSection( + matrices: MatrixStack, + texture: Texture, + x: Int, + y: Int, + width: Int, + sectionStart: Double, + sectionEnd: Double + ) { + if (sectionEnd < progress && width == 4) { + ScreenDrawing.texturedRect(matrices, x, y, 4, 8, texture, fillColor.color) + return + } + if (sectionStart > progress && width == 4) { + ScreenDrawing.texturedRect(matrices, x, y, 4, 8, texture, emptyColor.color) + return + } + val increasePerPixel = (sectionEnd - sectionStart / 4) + var valueAtPixel = sectionStart + for (i in (0 until width)) { + ScreenDrawing.texturedRect( + matrices, x + i, y, 1, 8, + texture.image, texture.u1 + i / 64F, texture.v1, texture.u1 + (i + 1) / 64F, texture.v2, + if (valueAtPixel < progress) fillColor.color else emptyColor.color + ) + valueAtPixel += increasePerPixel + } + } + + override fun paint(matrices: MatrixStack, x: Int, y: Int, mouseX: Int, mouseY: Int) { + var i = 0 + while (i < width - 4) { + drawSection( + matrices, + if (i == 0) left else middle, + x + i, y, + (width - (i + 4)).coerceAtMost(4), + i * total / width, (i + 4) * total / width + ) + i += 4 + } + drawSection(matrices, right, x + width - 4, y, 4, (width - 4) * total / width, total) + } +} diff --git a/src/main/kotlin/moe/nea/firmament/util/colorconversion.kt b/src/main/kotlin/moe/nea/firmament/util/colorconversion.kt new file mode 100644 index 0000000..c19eefa --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/colorconversion.kt @@ -0,0 +1,6 @@ +package moe.nea.firmament.util + +import net.minecraft.util.DyeColor + +fun DyeColor.toShedaniel(): me.shedaniel.math.Color = + me.shedaniel.math.Color.ofOpaque(this.signColor) diff --git a/src/main/resources/assets/firmament/textures/gui/bar.png b/src/main/resources/assets/firmament/textures/gui/bar.png new file mode 100644 index 0000000000000000000000000000000000000000..97a3ccc77d2c9855e24b122371893e0e5060ca02 GIT binary patch literal 679 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7T#lEU~J8Fb`J1#c2+1T%1_J8No8Qr zm{>c}*5h!1NUMMFRwYqK>kf;Q6}xV@y2@mWb_FP2;aY1oW6u}&j7t-i?6BZk$I4zW zwwh1x+%BsfTul%7J63m3UerCMB0eZX@Nw($-xlxZ9j^Y)=(P8Y$!cZ3j9t0QCUY9j zjuv!Y|H6skuYiG?@#z^;mx$E7em$>h<+~)C)BE3_e--*!jG0NPaE|0M-#vaddo!iy zeD73Vqq6$Q^COSdVki8V_`2(O{+w<-ZX4sIv`fWD1d}%2IOZk2dD2Qje?j+9-{m!m zuQ#5m?A{!