From 0135b49f15c81f78c03808ce8cd72cc7ec0d39d1 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 7 Mar 2021 21:39:00 +0100 Subject: [PATCH] rendering: ProgressBar --- .../minosoft/gui/rendering/RenderWindow.kt | 2 +- .../hud/atlas/ProgressBarAtlasElement.kt | 19 ++++ .../gui/rendering/hud/elements/ProgressBar.kt | 52 ----------- .../hud/elements/other/HotbarHUDElement.kt | 19 ++-- .../hud/elements/primitive/ProgressBar.kt | 89 +++++++++++++++++++ 5 files changed, 119 insertions(+), 62 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/hud/atlas/ProgressBarAtlasElement.kt delete mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/ProgressBar.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/ProgressBar.kt diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt index 8d0d2019f..8f300dc7a 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt @@ -224,7 +224,7 @@ class RenderWindow( // Make the OpenGL context current glfwMakeContextCurrent(windowId) // Enable v-sync - glfwSwapInterval(0) + glfwSwapInterval(1) // Make the window visible diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/atlas/ProgressBarAtlasElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/atlas/ProgressBarAtlasElement.kt new file mode 100644 index 000000000..6eeb3cbdc --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/atlas/ProgressBarAtlasElement.kt @@ -0,0 +1,19 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program.If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.hud.atlas + +data class ProgressBarAtlasElement( + val emptyAtlasElement: HUDAtlasElement, + val fullAtlasElement: HUDAtlasElement, +) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/ProgressBar.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/ProgressBar.kt deleted file mode 100644 index 2812657f9..000000000 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/ProgressBar.kt +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2021 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program.If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.gui.rendering.hud.elements - -import de.bixilon.minosoft.gui.rendering.hud.atlas.HUDAtlasElement -import glm_.vec2.Vec2 - -class ProgressBar( - private val emptyAtlasElement: HUDAtlasElement, - private val fullAtlasElement: HUDAtlasElement, - private val hudElement: HUDElement, -) { - val size: Vec2 = emptyAtlasElement.binding.size - -// fun draw(elementMesh: ElementMesh, start: Vec2, end: Vec2, progress: Float, z: Int = 1) { -//// elementMesh.addElement(start, end, emptyAtlasElement, z) - -// if (progress == 0.0f) { -// return -// } - -// val xDiff = abs(end.x - start.x) - -// val ourXDiff = xDiff * progress - -// val textureStart = Vec2((fullAtlasElement.binding.start.x * fullAtlasElement.texture.widthFactor) / fullAtlasElement.texture.width.toFloat(), (fullAtlasElement.binding.start.y * fullAtlasElement.texture.heightFactor) / fullAtlasElement.texture.height.toFloat()) -// var textureEnd = Vec2(((fullAtlasElement.binding.end.x + 1) * fullAtlasElement.texture.widthFactor) / (fullAtlasElement.texture.width + 1.0f), ((fullAtlasElement.binding.end.y + 1) * fullAtlasElement.texture.heightFactor) / (fullAtlasElement.texture.height + 1.0f)) - -// textureEnd = Vec2((textureEnd.x - textureStart.x) * progress, textureEnd.y) - -// elementMesh.addElement(start, Vec2(start.x + ourXDiff, end.y), object : TextureLike { -// override val texture: Texture -// get() = fullAtlasElement.texture -// override val uvStart: Vec2 -// get() = textureStart -// override val uvEnd: Vec2 -// get() = textureEnd - -// }, z + 1) - // } -} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/other/HotbarHUDElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/other/HotbarHUDElement.kt index 17e274dce..1d071a948 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/other/HotbarHUDElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/other/HotbarHUDElement.kt @@ -19,10 +19,11 @@ import de.bixilon.minosoft.data.mappings.ResourceLocation import de.bixilon.minosoft.gui.rendering.hud.HUDElementProperties import de.bixilon.minosoft.gui.rendering.hud.HUDRenderer import de.bixilon.minosoft.gui.rendering.hud.atlas.HUDAtlasElement +import de.bixilon.minosoft.gui.rendering.hud.atlas.ProgressBarAtlasElement import de.bixilon.minosoft.gui.rendering.hud.elements.HUDElement -import de.bixilon.minosoft.gui.rendering.hud.elements.ProgressBar import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.ElementListElement import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.ImageElement +import de.bixilon.minosoft.gui.rendering.hud.elements.primitive.ProgressBar import glm_.vec2.Vec2 class HotbarHUDElement( @@ -40,17 +41,16 @@ class HotbarHUDElement( private lateinit var hotbarBaseAtlasElement: HUDAtlasElement private lateinit var hotbarSelectedSlotFrameAtlasElement: HUDAtlasElement - private lateinit var experienceBar: ProgressBar + private lateinit var experienceBarAtlasElement: ProgressBarAtlasElement override fun init() { hotbarBaseAtlasElement = hudRenderer.hudAtlasElements[ResourceLocation("minecraft:hotbar_base")]!! hotbarSelectedSlotFrameAtlasElement = hudRenderer.hudAtlasElements[ResourceLocation("minecraft:hotbar_selected_slot_frame")]!! - experienceBar = ProgressBar( + experienceBarAtlasElement = ProgressBarAtlasElement( hudRenderer.hudAtlasElements[ResourceLocation("minecraft:experience_bar_empty")]!!, hudRenderer.hudAtlasElements[ResourceLocation("minecraft:experience_bar_full")]!!, - this ) for ((slotIndex, resourceLocation) in KeyBindingsNames.SELECT_HOTBAR_SLOTS.withIndex()) { @@ -70,12 +70,13 @@ class HotbarHUDElement( elementList.forceX = hotbarBaseAtlasElement.binding.size.x.toInt() - // if (hudRenderer.connection.player.gamemode != Gamemodes.CREATIVE) { - // // experience bar - // val experienceBarEnd = Vec2(hotbarBaseAtlasElement.binding.size.x, experienceBar.size.y) + if (hudRenderer.connection.player.gamemode != Gamemodes.CREATIVE) { + // experience bar + val experienceBarEnd = Vec2(hotbarBaseAtlasElement.binding.size.x, experienceBarAtlasElement.emptyAtlasElement.binding.size.y) - // experienceBar.draw(elementList, Vec2(), experienceBarEnd, hudRenderer.connection.player.experienceBarProgress, 3) - // } + val experienceBar = ProgressBar(Vec2(), experienceBarEnd, experienceBarAtlasElement, hudRenderer.connection.player.experienceBarProgress, 1) + elementList.addChild(experienceBar) + } val hotbarStart = Vec2(0, elementList.size.y + ELEMENT_PADDING) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/ProgressBar.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/ProgressBar.kt new file mode 100644 index 000000000..62cd8e6ea --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/elements/primitive/ProgressBar.kt @@ -0,0 +1,89 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program.If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.hud.elements.primitive + +import de.bixilon.minosoft.gui.rendering.hud.HUDMesh +import de.bixilon.minosoft.gui.rendering.hud.atlas.ProgressBarAtlasElement +import de.bixilon.minosoft.gui.rendering.hud.atlas.TextureLike +import de.bixilon.minosoft.gui.rendering.textures.Texture +import glm_.mat4x4.Mat4 +import glm_.vec2.Vec2 + +class ProgressBar( + start: Vec2, + val end: Vec2, + private val atlasElement: ProgressBarAtlasElement, + val progress: Float = 0.0f, + val z: Int = 1, +) : Element(start) { + + init { + recalculateSize() + } + + override fun recalculateSize() { + size = atlasElement.emptyAtlasElement.binding.size + } + + // fun draw(elementMesh: ElementMesh, start: Vec2, end: Vec2, progress: Float, z: Int = 1) { + // elementMesh.addElement(start, end, emptyAtlasElement, z) + + // if (progress == 0.0f) { + // return + // } + + // val xDiff = abs(end.x - start.x) + + // val ourXDiff = xDiff * progress + + // val textureStart = Vec2((fullAtlasElement.binding.start.x * fullAtlasElement.texture.widthFactor) / fullAtlasElement.texture.width.toFloat(), (fullAtlasElement.binding.start.y * fullAtlasElement.texture.heightFactor) / fullAtlasElement.texture.height.toFloat()) + // var textureEnd = Vec2(((fullAtlasElement.binding.end.x + 1) * fullAtlasElement.texture.widthFactor) / (fullAtlasElement.texture.width + 1.0f), ((fullAtlasElement.binding.end.y + 1) * fullAtlasElement.texture.heightFactor) / (fullAtlasElement.texture.height + 1.0f)) + + // textureEnd = Vec2((textureEnd.x - textureStart.x) * progress, textureEnd.y) + + // elementMesh.addElement(start, Vec2(start.x + ourXDiff, end.y), object : TextureLike { + // override val texture: Texture + // get() = fullAtlasElement.texture + // override val uvStart: Vec2 + // get() = textureStart + // override val uvEnd: Vec2 + // get() = textureEnd + + // }, z + 1) +// } + + override fun prepareVertices(start: Vec2, scaleFactor: Float, hudMesh: HUDMesh, matrix: Mat4, z: Int) { + val emptyImageElement = ImageElement(this.start, end, atlasElement.emptyAtlasElement, z) + + emptyImageElement.prepareVertices(start, scaleFactor, hudMesh, matrix, z) + + if (progress == 0.0f) { + return + } + + + val fullImageElement = ImageElement(this.start, Vec2(end.x * progress, end.y), object : TextureLike { + override val texture: Texture + get() = atlasElement.fullAtlasElement.texture + override val uvStart: Vec2 + get() = atlasElement.fullAtlasElement.uvStart + override val uvEnd: Vec2 + get() = Vec2((atlasElement.fullAtlasElement.uvEnd.x - atlasElement.fullAtlasElement.uvStart.x) * progress + atlasElement.fullAtlasElement.uvStart.x, atlasElement.fullAtlasElement.uvEnd.y) + + }, z + 1) + + fullImageElement.prepareVertices(start, scaleFactor, hudMesh, matrix, z) + + } +}