Merge pull request #315 from Noremac201/fixXpGlobes

Changed XpGlobes to use new Overlay components
This commit is contained in:
Adam
2018-01-04 21:19:55 -05:00
committed by GitHub
3 changed files with 143 additions and 52 deletions

View File

@@ -27,8 +27,8 @@ package net.runelite.client.plugins.xpglobes;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
@@ -45,6 +45,8 @@ import net.runelite.api.Point;
import net.runelite.api.Skill;
import net.runelite.client.ui.overlay.Overlay;
import net.runelite.client.ui.overlay.OverlayPosition;
import net.runelite.client.ui.overlay.components.PanelComponent;
import net.runelite.client.ui.overlay.components.ProgressBarComponent;
@Slf4j
public class XpGlobesOverlay extends Overlay
@@ -69,8 +71,6 @@ public class XpGlobesOverlay extends Overlay
private final BufferedImage[] imgCache = new BufferedImage[Skill.values().length - 1];
private static final int TOOLTIP_RECT_SIZE_X = 140;
private static final int TOOLTIP_TEXT_RECT_SIZE_X = TOOLTIP_RECT_SIZE_X - 10;
private static final int TOOLTIP_RECT_SIZE_Y = 80;
@Inject
public XpGlobesOverlay(@Nullable Client client, XpGlobesPlugin plugin, XpGlobesConfig config)
@@ -107,7 +107,7 @@ public class XpGlobesOverlay extends Overlay
for (XpGlobe xpGlobe : xpChangedQueue)
{
renderProgressCircle(graphics, xpGlobe, startDrawX, DEFAULT_START_Y);
renderProgressCircle(graphics, point, xpGlobe, startDrawX, DEFAULT_START_Y);
startDrawX += MINIMUM_STEP_WIDTH;
}
plugin.removeExpiredXpGlobes();
@@ -116,7 +116,7 @@ public class XpGlobesOverlay extends Overlay
return null;
}
private void renderProgressCircle(Graphics2D graphics, XpGlobe skillToDraw, int x, int y)
private void renderProgressCircle(Graphics2D graphics, java.awt.Point parent, XpGlobe skillToDraw, int x, int y)
{
double radiusCurrentXp = skillToDraw.getSkillProgressRadius();
double radiusToGoalXp = 360; //draw a circle
@@ -141,12 +141,13 @@ public class XpGlobesOverlay extends Overlay
if (config.enableTooltips())
{
drawTooltipIfMouseover(graphics, skillToDraw, backgroundCircle);
drawTooltipIfMouseover(graphics, parent, skillToDraw, backgroundCircle);
}
}
private void drawProgressArc(Graphics2D graphics, int x, int y, int w, int h, double radiusStart, double radiusEnd, int strokeWidth, Color color)
{
Stroke stroke = graphics.getStroke();
graphics.setStroke(new BasicStroke(strokeWidth));
graphics.setColor(color);
graphics.draw(new Arc2D.Double(
@@ -154,6 +155,7 @@ public class XpGlobesOverlay extends Overlay
w, h,
radiusStart, radiusEnd,
Arc2D.OPEN));
graphics.setStroke(stroke);
}
private Ellipse2D drawEllipse(Graphics2D graphics, int x, int y)
@@ -207,7 +209,7 @@ public class XpGlobesOverlay extends Overlay
return skillImage;
}
private void drawTooltipIfMouseover(Graphics2D graphics, XpGlobe mouseOverSkill, Ellipse2D drawnGlobe)
private void drawTooltipIfMouseover(Graphics2D graphics, java.awt.Point parent, XpGlobe mouseOverSkill, Ellipse2D drawnGlobe)
{
Point mouse = client.getMouseCanvasPosition();
int mouseX = mouse.getX();
@@ -221,55 +223,29 @@ public class XpGlobesOverlay extends Overlay
//draw tooltip under the globe of the mouse location
int x = (int) drawnGlobe.getX() - (TOOLTIP_RECT_SIZE_X / 2) + (DEFAULT_CIRCLE_WIDTH / 2);
int y = (int) drawnGlobe.getY() + DEFAULT_CIRCLE_HEIGHT + 10;
int padding = (TOOLTIP_RECT_SIZE_X - TOOLTIP_TEXT_RECT_SIZE_X) / 2;
int stringX = x + padding;
String skillName = mouseOverSkill.getSkillName();
String skillLevel = Integer.toString(mouseOverSkill.getCurrentLevel());
String skillCurrentXp = Integer.toString(mouseOverSkill.getCurrentXp());
String skillXpToLvl = Integer.toString((mouseOverSkill.getGoalXp() - mouseOverSkill.getCurrentXp()));
FontMetrics fm = graphics.getFontMetrics();
int skillLevelX = x + padding + (TOOLTIP_TEXT_RECT_SIZE_X - fm.stringWidth(skillLevel));
int skillCurrentXpX = x + padding + (TOOLTIP_TEXT_RECT_SIZE_X - fm.stringWidth(skillCurrentXp));
int skillXpToLvlX = x + padding + (TOOLTIP_TEXT_RECT_SIZE_X - fm.stringWidth(skillXpToLvl));
int stringHeight = fm.getHeight();
DecimalFormat decimalFormat = new DecimalFormat("###,###,###");
String skillCurrentXp = decimalFormat.format(mouseOverSkill.getCurrentXp());
String skillXpToLvl = decimalFormat.format(mouseOverSkill.getGoalXp() - mouseOverSkill.getCurrentXp());
//draw tooltip container
graphics.setPaint(DEFAULT_XPGLOBE_BACKGROUND_COLOR);
graphics.fillRect(x, y, TOOLTIP_RECT_SIZE_X, TOOLTIP_RECT_SIZE_Y);
graphics.setPaint(DEFAULT_PROGRESS_REMAINDER_ARC_COLOR);
graphics.setStroke(new BasicStroke(2));
graphics.drawRect(x, y, TOOLTIP_RECT_SIZE_X, TOOLTIP_RECT_SIZE_Y);
PanelComponent xpTooltip = new PanelComponent();
xpTooltip.setPosition(new java.awt.Point(x, y));
//draw the text
graphics.setPaint(Color.WHITE);
graphics.drawString(mouseOverSkill.getSkillName(), stringX, y + stringHeight);
graphics.drawString(skillLevel, skillLevelX, y + stringHeight);
graphics.drawString("Current exp:", stringX, y + (stringHeight * 2));
graphics.drawString(skillCurrentXp, skillCurrentXpX, y + (stringHeight * 2));
graphics.drawString("Exp to level:", stringX, y + (stringHeight * 3));
graphics.drawString(skillXpToLvl, skillXpToLvlX, y + (stringHeight * 3));
List<PanelComponent.Line> lines = xpTooltip.getLines();
lines.add(new PanelComponent.Line(skillName, Color.WHITE, skillLevel, Color.WHITE));
lines.add(new PanelComponent.Line("Current xp:", Color.ORANGE, skillCurrentXp, Color.WHITE));
lines.add(new PanelComponent.Line("Xp to level: ", Color.ORANGE, skillXpToLvl, Color.WHITE));
//draw the progress bar
double progress = mouseOverSkill.getSkillProgress(Experience.getXpForLevel(mouseOverSkill.getCurrentLevel()), mouseOverSkill.getCurrentXp(), mouseOverSkill.getGoalXp());
int barWidth = TOOLTIP_TEXT_RECT_SIZE_X;
int barHeight = 16;
int barX = x + padding;
int barY = y + (stringHeight * 3) + 10;
//Create progress bar for skill.
ProgressBarComponent progressBar = new ProgressBarComponent();
double progress = mouseOverSkill.getSkillProgress(Experience.getXpForLevel(mouseOverSkill.getCurrentLevel()),
mouseOverSkill.getCurrentXp(), mouseOverSkill.getGoalXp());
progressBar.setProgress(progress);
DecimalFormat df = new DecimalFormat("#.00");
String progressText = df.format(progress) + "%";
int progressTextLength = fm.stringWidth(progressText);
int progressTextX = barX + (barWidth / 2) - (progressTextLength / 2);
int progressTextY = barY + 12;
int progressFill = (int) ((barWidth / 100F) * progress);
graphics.setColor(Color.WHITE);
graphics.fillRect(barX, barY, barWidth, barHeight);
graphics.setColor(Color.GREEN);
graphics.fillRect(barX, barY, progressFill, barHeight);
graphics.setPaint(Color.BLACK);
graphics.drawString(progressText, progressTextX, progressTextY);
xpTooltip.setProgressBar(progressBar);
xpTooltip.render(graphics, parent);
}
}

View File

@@ -33,6 +33,7 @@ import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
@@ -74,6 +75,9 @@ public class PanelComponent implements RenderableEntity
@Getter
private List<Line> lines = new ArrayList<>();
@Setter
private ProgressBarComponent progressBar;
@Setter
private int width = 140;
@@ -81,10 +85,11 @@ public class PanelComponent implements RenderableEntity
public Dimension render(Graphics2D graphics, Point parent)
{
final Dimension dimension = new Dimension();
final int elementNumber = (Strings.isNullOrEmpty(title) ? 0 : 1) + lines.size();
final int elementNumber = (Strings.isNullOrEmpty(title) ? 0 : 1) + lines.size() + (Objects.isNull(progressBar) ? 0 : 1);
int height = elementNumber == 0 ? 0 :
TOP_BORDER + (graphics.getFontMetrics().getHeight() * elementNumber)
+ SEPARATOR * elementNumber + BOTTOM_BORDER;
+ SEPARATOR * elementNumber + (Objects.isNull(progressBar) ? 0 : progressBar.getHeight() / 2)
+ BOTTOM_BORDER;
dimension.setSize(width, height);
if (dimension.height == 0)
@@ -131,6 +136,14 @@ public class PanelComponent implements RenderableEntity
y += metrics.getHeight() + SEPARATOR;
}
//Render progress bar
if (!Objects.isNull(progressBar))
{
progressBar.setWidth(width - LEFT_BORDER - RIGHT_BORDER);
progressBar.setPosition(new Point(position.x + LEFT_BORDER, y - (metrics.getHeight() / 2)));
progressBar.render(graphics, parent);
}
return dimension;
}
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright (c) 2018, Cameron <moberg@tuta.io>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
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;
import java.awt.Graphics2D;
import java.awt.Point;
import java.text.DecimalFormat;
import lombok.Getter;
import lombok.Setter;
public class ProgressBarComponent
{
@Setter
private String text;
@Setter
private double progress;
@Setter
private Point position = new Point();
@Setter
private Color foregroundColor = new Color(82, 161, 82);
@Setter
private Color backgroundColor = new Color(255, 255, 255, 127);
@Setter
private Color fontColor = Color.WHITE;
@Getter
@Setter
private int width = 140;
@Getter
@Setter
private int height = 16;
public Dimension render(Graphics2D graphics, Point parent)
{
FontMetrics metrics = graphics.getFontMetrics();
int barX = position.x;
int barY = position.y;
String textToWrite;
if (Strings.isNullOrEmpty(text))
{
DecimalFormat df = new DecimalFormat("#.00");
textToWrite = df.format(progress) + "%";
}
else
{
textToWrite = text;
}
int progressTextX = barX + (width - metrics.stringWidth(textToWrite)) / 2;
int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getAscent();
int progressFill = (int) ((width / 100F) * progress);
//Draw bar
graphics.setColor(backgroundColor);
graphics.fillRect(barX, barY, width, height);
graphics.setColor(foregroundColor);
graphics.fillRect(barX, barY, progressFill, height);
TextComponent textComponent = new TextComponent();
textComponent.setPosition(new Point(progressTextX, progressTextY));
textComponent.setColor(fontColor);
textComponent.setText(textToWrite);
textComponent.render(graphics, parent);
return new Dimension(width, height);
}
}