aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/gui/WBar.kt
blob: 04c5e3d5f31f1abf2e9e4e24a30b6751528b0437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package moe.nea.firmament.gui

import com.mojang.blaze3d.systems.RenderSystem
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

open class WBar(
    var progress: Double,
    val total: Double,
    val fillColor: Color,
    val emptyColor: Color,
) : WWidget() {
    companion object {
        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, 8 / 64F, 8 / 64F)
        val right = Texture(resource, 8 / 64F, 0 / 64F, 12 / 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)
        RenderSystem.setShaderColor(1F, 1F, 1F, 1F)
    }
}