Improve ProgressBarComponent to support %/FULL

- Add minimum, maximum and value properties to ProgressBarComponent
- Add label display mode that will differentiate between percentage and
current/maximum display modes
- Properly center the text in progress bar

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-05-08 15:59:01 +02:00
parent 6f66b6790d
commit 19afa3f933

View File

@@ -24,7 +24,6 @@
*/ */
package net.runelite.client.ui.overlay.components; package net.runelite.client.ui.overlay.components;
import com.google.common.base.Strings;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.FontMetrics; import java.awt.FontMetrics;
@@ -36,9 +35,18 @@ import lombok.Setter;
@Setter @Setter
public class ProgressBarComponent implements LayoutableRenderableEntity public class ProgressBarComponent implements LayoutableRenderableEntity
{ {
private String text; public enum LabelDisplayMode
private double progress; {
private Point position = new Point(); PERCENTAGE,
FULL
}
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.0");
private static final DecimalFormat DECIMAL_FORMAT2 = new DecimalFormat("#0");
private long minimum;
private long maximum = 100;
private double value;
private LabelDisplayMode labelDisplayMode = LabelDisplayMode.PERCENTAGE;
private Color foregroundColor = new Color(82, 161, 82); private Color foregroundColor = new Color(82, 161, 82);
private Color backgroundColor = new Color(255, 255, 255, 127); private Color backgroundColor = new Color(255, 255, 255, 127);
private Color fontColor = Color.WHITE; private Color fontColor = Color.WHITE;
@@ -47,29 +55,30 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
@Override @Override
public Dimension render(Graphics2D graphics) public Dimension render(Graphics2D graphics)
{ {
FontMetrics metrics = graphics.getFontMetrics(); final FontMetrics metrics = graphics.getFontMetrics();
int barX = position.x; final int barX = 0;
int barY = position.y - metrics.getHeight(); final int barY = -metrics.getHeight();
String textToWrite;
if (Strings.isNullOrEmpty(text)) final long span = maximum - minimum;
final double currentValue = value - minimum;
final double pc = currentValue / span;
final String textToWrite;
switch (labelDisplayMode)
{ {
DecimalFormat df = new DecimalFormat("#0"); case PERCENTAGE:
textToWrite = df.format(Math.floor(progress)) + "%"; textToWrite = DECIMAL_FORMAT.format(Math.floor(pc)) + "%";
} break;
else default:
{ textToWrite = DECIMAL_FORMAT2.format(Math.floor(currentValue)) + "/" + maximum;
textToWrite = text;
} }
int width = preferredSize.width; final int width = preferredSize.width;
int height = Math.max(preferredSize.height, 16); final int height = Math.max(preferredSize.height, 16);
final int progressTextX = barX + (width - metrics.stringWidth(textToWrite)) / 2;
int progressTextX = barX + (width - metrics.stringWidth(textToWrite)) / 2; final int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getHeight();
int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getAscent(); final int progressFill = (int) (width * pc);
int progressFill = (int) ((width / 100F) * progress);
//Draw bar //Draw bar
graphics.setColor(backgroundColor); graphics.setColor(backgroundColor);
@@ -77,7 +86,7 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
graphics.setColor(foregroundColor); graphics.setColor(foregroundColor);
graphics.fillRect(barX, barY, progressFill, height); graphics.fillRect(barX, barY, progressFill, height);
TextComponent textComponent = new TextComponent(); final TextComponent textComponent = new TextComponent();
textComponent.setPosition(new Point(progressTextX, progressTextY)); textComponent.setPosition(new Point(progressTextX, progressTextY));
textComponent.setColor(fontColor); textComponent.setColor(fontColor);
textComponent.setText(textToWrite); textComponent.setText(textToWrite);