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;
import com.google.common.base.Strings;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
@@ -36,9 +35,18 @@ import lombok.Setter;
@Setter
public class ProgressBarComponent implements LayoutableRenderableEntity
{
private String text;
private double progress;
private Point position = new Point();
public enum LabelDisplayMode
{
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 backgroundColor = new Color(255, 255, 255, 127);
private Color fontColor = Color.WHITE;
@@ -47,29 +55,30 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
@Override
public Dimension render(Graphics2D graphics)
{
FontMetrics metrics = graphics.getFontMetrics();
final FontMetrics metrics = graphics.getFontMetrics();
int barX = position.x;
int barY = position.y - metrics.getHeight();
String textToWrite;
final int barX = 0;
final int barY = -metrics.getHeight();
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");
textToWrite = df.format(Math.floor(progress)) + "%";
}
else
{
textToWrite = text;
case PERCENTAGE:
textToWrite = DECIMAL_FORMAT.format(Math.floor(pc)) + "%";
break;
default:
textToWrite = DECIMAL_FORMAT2.format(Math.floor(currentValue)) + "/" + maximum;
}
int width = preferredSize.width;
int height = Math.max(preferredSize.height, 16);
int progressTextX = barX + (width - metrics.stringWidth(textToWrite)) / 2;
int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getAscent();
int progressFill = (int) ((width / 100F) * progress);
final int width = preferredSize.width;
final int height = Math.max(preferredSize.height, 16);
final int progressTextX = barX + (width - metrics.stringWidth(textToWrite)) / 2;
final int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getHeight();
final int progressFill = (int) (width * pc);
//Draw bar
graphics.setColor(backgroundColor);
@@ -77,7 +86,7 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
graphics.setColor(foregroundColor);
graphics.fillRect(barX, barY, progressFill, height);
TextComponent textComponent = new TextComponent();
final TextComponent textComponent = new TextComponent();
textComponent.setPosition(new Point(progressTextX, progressTextY));
textComponent.setColor(fontColor);
textComponent.setText(textToWrite);