package speiger.src.coreengine.rendering.gui.components; import java.util.function.IntFunction; import java.util.function.IntSupplier; import speiger.src.coreengine.rendering.gui.GuiComponent; import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox; import speiger.src.coreengine.rendering.gui.helper.box.ParentBox; import speiger.src.coreengine.rendering.gui.helper.constrains.Constraints; import speiger.src.coreengine.rendering.gui.renderer.UIRenderer; public class ProgressBarComponent extends GuiComponent { IGuiBox inner = new ParentBox(1F); TextComponent text = new TextComponent(); int color; int value; int max; IntFunction name; IntSupplier valueGenerator; public ProgressBarComponent(int color, int value, int max) { this(color, value, max, null); } public ProgressBarComponent(int color, int value, int max, IntFunction name) { this(0F, 0F, 0F, 0F, color, value, max, name); } public ProgressBarComponent(float x, float y, float width, float height, int color, int value, int max) { this(x, y, width, height, color, value, max, null); } public ProgressBarComponent(float x, float y, float width, float height, int color, int value, int max, IntFunction name) { super(x, y, width, height); this.name = name; setColor(color); setMax(max); setValue(value); } @Override public void init() { addBox(inner); text.setZOffset(0.1F); text.setText(stringify()); addChild(text, Constraints.getParentConstrains()); } public TextComponent getText() { return text; } public ProgressBarComponent setColor(int color) { this.color = color; return this; } public ProgressBarComponent setValueGenerator(IntSupplier valueGenerator) { this.valueGenerator = valueGenerator; return this; } public ProgressBarComponent setValue(int newValue) { if(value != newValue) { value = newValue; text.setText(stringify()); } return this; } public ProgressBarComponent setMax(int newMax) { if(max != newMax) { max = newMax; if(value > max) setValue(max); } return this; } protected String stringify() { if(name != null) return name.apply(value); else if(max == 0) return "0%"; return Integer.toString((int)(((float)value / (float)max) * 100F))+"%"; } protected float getProgress() { return max == 0 ? 0F : (float)value / (float)max; } @Override protected boolean fixedUpdateSelf() { if(valueGenerator != null) setValue(valueGenerator.getAsInt()); return true; } @Override protected boolean renderSelf(int mouseX, int mouseY, float particalTicks) { float extra = (inner.getMaxX() - inner.getMinX()) * getProgress(); UIRenderer render = getRenderer(); render.setBrightness(getActiveBrightness() * 0.75F).drawQuad(getBox(), color); render.setBrightness(getActiveBrightness()).drawQuad(inner, 0.01F, color); render.setBrightness(getActiveBrightness() * 1.25F).drawQuad(inner.getMinX(), inner.getMinY(), inner.getMinX() + extra, inner.getMaxY(), 0.02F, color); text.setBrightness(getActiveBrightness()); return true; } }