package game.gui.element; import game.renderer.Drawing; import game.util.ExtMath; public class Bar extends Fill { private int color = 0x00ff00; private int progress = 0; private int total = 0; private String desc = ""; private String unit = ""; public Bar(int x, int y, int w, int h, boolean top, boolean left) { super(x, y, w, h, "", top, left); } public Bar(int x, int y, int w, int h, boolean left) { super(x, y, w, h, "", left); } public Bar(int x, int y, int w, int h) { super(x, y, w, h, ""); } protected void drawBackground() { Drawing.drawGradient2Border(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.fill_btm, this.gm.style.fill_top, this.gm.style.brdr_top, this.gm.style.brdr_btm); if(this.progress > 0) Drawing.drawGradient(this.pos_x + 2, this.pos_y + 2, this.progress, this.size_y - 4, this.color | 0xff000000, Drawing.mixColor(this.color | 0xff000000, 0xff000000)); } protected void drawForeground(int x1, int y1, int x2, int y2) { Drawing.drawText(this.text, x1 + this.text_x, y1 + this.text_y, this.gm.style.text_label); } protected int getMargin() { return 0; } public Bar setDescription(String desc, String unit, int total) { this.desc = desc == null ? "" : (desc + ": "); this.unit = unit == null ? "" : (" " + unit); this.total = total; return this; } public Bar setDescription(String desc, int total) { return this.setDescription(desc, null, total); } public Bar setDescription(String desc) { this.desc = desc == null ? "" : (desc + ": "); this.unit = ""; this.total = 0; return this; } public Bar resetProgress() { this.setText(""); this.progress = 0; this.setDescription(null); return this; } public Bar setProgressFill(int progress) { this.progress = ExtMath.clampi(progress, 0, this.size_x - 4); return this; } public Bar setProgressFill(float progress) { return this.setProgressFill((int)(progress * (float)(this.size_x - 4))); } public Bar setProgress(float progress) { if(this.total == 0) this.setText(String.format("%s%.1f %%", this.desc, progress * 100.0f)); else this.setText(String.format("%s%d/%d%s (%.1f %%)", this.desc, (int)progress, this.total, this.unit, (progress / (float)this.total) * 100.0f)); return this.setProgressFill(this.total == 0 ? progress : (progress / (float)this.total)); } public Bar setColor(int color) { this.color = color; return this; } }