HorizontalProgressBar.java now can be filled left to right or the other way

This commit is contained in:
Antoine 2021-06-12 03:09:43 +02:00
parent 8b9d698acc
commit f93f801e0b
4 changed files with 51 additions and 34 deletions

View File

@ -0,0 +1,31 @@
#version 410
in vec4 color;
in vec2 fragCoord;
out vec4 FragColor;
uniform float time;
uniform float fill;
uniform bool leftToRight;
void main()
{
if (leftToRight){
if (fill > fragCoord.x){
FragColor = color;
}
else {
FragColor = vec4(color.xyz, 0f);
}
} else {
if (fill < fragCoord.x){
FragColor = color;
}
else {
FragColor = vec4(color.xyz, 0f);
}
}
}

View File

@ -1,21 +0,0 @@
#version 410
in vec4 color;
in vec2 fragCoord;
out vec4 FragColor;
uniform float time;
uniform float fill;
void main()
{
if (fill > fragCoord.x){
FragColor = color;
}
else {
FragColor = vec4(color.xyz, 0f);
}
}

View File

@ -4,11 +4,9 @@ import engine.gui.UIElement;
import engine.gui.UIElementText;
import engine.input.*;
import engine.math.Vector3f;
import engine.object.Hitbox;
import engine.object.ObjectGl;
import engine.object.ProgressBar;
import engine.object.HorizontalProgressBar;
import engine.object.Sprite;
import engine.sound.*;
import java.util.ArrayList;
import java.util.List;
@ -83,7 +81,7 @@ public class TestEngine {
UIElementText uiTextCoordP1 = new UIElementText("Boulevard Combattant", 7.0f, 0.0f,0.05f, 25.0f, engine);
engine.add_uiElement(uiTextCoordP1);
ProgressBar healthP1 = new ProgressBar(50f, 10, 1, 100, 100, 100);
HorizontalProgressBar healthP1 = new HorizontalProgressBar(50f, 10, 1, 100, 100, 100, false);
UIElement healthP1UI = new UIElement(healthP1, 0f, 1f, engine);
engine.add_uiElement(healthP1UI);
float hpCurrent = 100;
@ -162,11 +160,11 @@ public class TestEngine {
frame = 0;
}
while (!nextFrame) {
nextFrame = System.currentTimeMillis() - lastFrame >= 16.66f;
}
// while (!nextFrame) {
// nextFrame = System.currentTimeMillis() - lastFrame >= 16.66f;
// }
hpCurrent -= 1;
hpCurrent -= 0.01;
healthP1.setCurrent(hpCurrent);
nextFrame = false;

View File

@ -4,16 +4,18 @@ import engine.math.Vector3f;
import static org.lwjgl.glfw.GLFW.glfwGetTime;
public class ProgressBar extends ObjectGl{
public class HorizontalProgressBar extends ObjectGl{
private float max;
private float current;
private int leftToRight;
public ProgressBar(float z, float w, float h, float size, float current, float max) {
public HorizontalProgressBar(float z, float w, float h, float size, float current, float max, boolean leftToRight) {
super(z, w, h, size, null, new Vector3f(0f, 1f, 0f));
this.current = current;
this.max = max;
this.setShader("shaders/StylishShaders/BasicNoTexVert.glsl", "shaders/StylishShaders/ProgressBarFrag.glsl");
this.current = current;
this.leftToRight = leftToRight ? 1 : 0;
this.setShader("shaders/StylishShaders/BasicNoTexVert.glsl", "shaders/StylishShaders/HorizontalProgressBar.glsl");
}
public void setCurrent(float newCurrent){
@ -32,7 +34,14 @@ public class ProgressBar extends ObjectGl{
this.shader.enable();
if (this.useTime) this.shader.setUniform1f("time", (float) glfwGetTime());
this.shader.setUniform1f("fill", (this.current / this.max) * this.getWidth());
if (this.leftToRight == 1){
this.shader.setUniform1f("fill", (this.current / this.max) * this.getWidth());
} else {
this.shader.setUniform1f("fill", Math.abs(((this.current / this.max) * this.getWidth()) - this.getWidth()));
}
this.shader.setUniform1i("leftToRight", this.leftToRight);
this.shader.setUniformMat4f("projection", projection);
this.shader.setUniformMat4f("view", view);