aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/gui
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-06-01 22:38:09 +0200
committernea <nea@nea.moe>2023-06-01 22:38:09 +0200
commit3a24eb80f328ec55cb4587e712a6f0c02dfa5c89 (patch)
treed56ac2c53bb766802091f89fc8a3da5d747740bc /src/main/kotlin/moe/nea/firmament/gui
parentc969074af3ba24f7304cf78ab9fb95eb52743ba9 (diff)
downloadfirmament-3a24eb80f328ec55cb4587e712a6f0c02dfa5c89.tar.gz
firmament-3a24eb80f328ec55cb4587e712a6f0c02dfa5c89.tar.bz2
firmament-3a24eb80f328ec55cb4587e712a6f0c02dfa5c89.zip
Add WBar
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/gui')
-rw-r--r--src/main/kotlin/moe/nea/firmament/gui/WBar.kt69
1 files changed, 69 insertions, 0 deletions
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)
+ }
+}