diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/gui')
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/gui/BarComponent.kt | 113 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/gui/WBar.kt | 4 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/gui/hud/MoulConfigHud.kt | 62 |
3 files changed, 177 insertions, 2 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/gui/BarComponent.kt b/src/main/kotlin/moe/nea/firmament/gui/BarComponent.kt new file mode 100644 index 0000000..f59d6b1 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/gui/BarComponent.kt @@ -0,0 +1,113 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +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.data.Texture +import io.github.moulberry.moulconfig.common.MyResourceLocation +import io.github.moulberry.moulconfig.common.RenderContext +import io.github.moulberry.moulconfig.gui.GuiComponent +import io.github.moulberry.moulconfig.gui.GuiImmediateContext +import io.github.moulberry.moulconfig.observer.GetSetter +import io.github.notenoughupdates.moulconfig.platform.ModernRenderContext +import me.shedaniel.math.Color +import net.minecraft.client.gui.DrawContext +import net.minecraft.util.Identifier +import moe.nea.firmament.Firmament + +class BarComponent( + val progress: GetSetter<Double>, val total: GetSetter<Double>, + val fillColor: Color, + val emptyColor: Color, +) : GuiComponent() { + override fun getWidth(): Int { + return 80 + } + + override fun getHeight(): Int { + return 8 + } + + 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) + } + + private fun drawSection( + context: DrawContext, + texture: Texture, + x: Int, + y: Int, + width: Int, + sectionStart: Double, + sectionEnd: Double + ) { + if (sectionEnd < progress.get() && width == 4) { + ScreenDrawing.texturedRect(context, x, y, 4, 8, texture, fillColor.color) + return + } + if (sectionStart > progress.get() && width == 4) { + ScreenDrawing.texturedRect(context, x, y, 4, 8, texture, emptyColor.color) + return + } + val increasePerPixel = (sectionEnd - sectionStart / 4) + var valueAtPixel = sectionStart + for (i in (0 until width)) { + ScreenDrawing.texturedRect( + context, x + i, y, 1, 8, + texture.image, texture.u1 + i / 64F, texture.v1, texture.u1 + (i + 1) / 64F, texture.v2, + if (valueAtPixel < progress.get()) fillColor.color else emptyColor.color + ) + valueAtPixel += increasePerPixel + } + } + + override fun render(context: GuiImmediateContext) { + val renderContext = (context.renderContext as ModernRenderContext).drawContext + var i = 0 + val x = 0 + val y = 0 + while (i < context.width - 4) { + drawSection( + renderContext, + if (i == 0) left else middle, + x + i, y, + (context.width - (i + 4)).coerceAtMost(4), + i * total.get() / context.width, (i + 4) * total.get() / context.width + ) + i += 4 + } + drawSection( + renderContext, + right, + x + context.width - 4, + y, + 4, + (context.width - 4) * total.get() / context.width, + total.get() + ) + RenderSystem.setShaderColor(1F, 1F, 1F, 1F) + + } + +} + +fun Identifier.toMoulConfig(): MyResourceLocation { + return MyResourceLocation(this.namespace, this.path) +} + +fun RenderContext.color(color: Color) { + color(color.red, color.green, color.blue, color.alpha) +} + +fun RenderContext.color(red: Int, green: Int, blue: Int, alpha: Int) { + color(red / 255f, green / 255f, blue / 255f, alpha / 255f) +} diff --git a/src/main/kotlin/moe/nea/firmament/gui/WBar.kt b/src/main/kotlin/moe/nea/firmament/gui/WBar.kt index 2c37d11..5772aac 100644 --- a/src/main/kotlin/moe/nea/firmament/gui/WBar.kt +++ b/src/main/kotlin/moe/nea/firmament/gui/WBar.kt @@ -16,9 +16,9 @@ import moe.nea.firmament.Firmament open class WBar( var progress: Double, - val total: Double, + var total: Double, val fillColor: Color, - val emptyColor: Color, + val emptyColor: Color = fillColor.darker(2.0), ) : WWidget() { companion object { val resource = Firmament.identifier("textures/gui/bar.png") diff --git a/src/main/kotlin/moe/nea/firmament/gui/hud/MoulConfigHud.kt b/src/main/kotlin/moe/nea/firmament/gui/hud/MoulConfigHud.kt new file mode 100644 index 0000000..d56f713 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/gui/hud/MoulConfigHud.kt @@ -0,0 +1,62 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.gui.hud + +import io.github.moulberry.moulconfig.gui.GuiContext +import io.github.moulberry.moulconfig.gui.component.TextComponent +import io.github.notenoughupdates.moulconfig.gui.GuiComponentWrapper +import net.minecraft.resource.ResourceManager +import net.minecraft.resource.SynchronousResourceReloader +import moe.nea.firmament.events.HudRenderEvent +import moe.nea.firmament.gui.config.HudMeta +import moe.nea.firmament.util.MC +import moe.nea.firmament.util.MoulConfigUtils + +abstract class MoulConfigHud( + val name: String, + val hudMeta: HudMeta, +) { + companion object { + private val componentWrapper = object : GuiComponentWrapper(GuiContext(TextComponent("§cERROR"))) { + init { + this.client = MC.instance + } + } + } + + private var fragment: GuiContext? = null + + open fun shouldRender(): Boolean { + return true + } + + init { + HudRenderEvent.subscribe { + if (!shouldRender()) return@subscribe + val renderContext = componentWrapper.createContext(it.context) + if (fragment == null) + loadFragment() + it.context.matrices.push() + hudMeta.applyTransformations(it.context.matrices) + val renderContextTranslated = + renderContext.translated(hudMeta.absoluteX, hudMeta.absoluteY, hudMeta.width, hudMeta.height) + .scaled(hudMeta.scale) + fragment!!.root.render(renderContextTranslated) + it.context.matrices.pop() + } + MC.resourceManager.registerReloader(object : SynchronousResourceReloader { + override fun reload(manager: ResourceManager?) { + fragment = null + } + }) + } + + fun loadFragment() { + fragment = MoulConfigUtils.loadGui(name, this) + } + +} |