From f9eae3e7c11ce589fc0e28b442068b1e6b6da21f Mon Sep 17 00:00:00 2001 From: noremac201 Date: Wed, 3 Jan 2018 15:39:49 -0600 Subject: [PATCH 1/4] Added ProgressBar Component --- .../components/ProgressBarComponent.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 runelite-client/src/main/java/net/runelite/client/ui/overlay/components/ProgressBarComponent.java diff --git a/runelite-client/src/main/java/net/runelite/client/ui/overlay/components/ProgressBarComponent.java b/runelite-client/src/main/java/net/runelite/client/ui/overlay/components/ProgressBarComponent.java new file mode 100644 index 0000000000..51a7a8e2da --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/ui/overlay/components/ProgressBarComponent.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2018, Cameron + * 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); + } +} From 4a2ba1c6fe2d7602bff50ebc966f0c708e7878ff Mon Sep 17 00:00:00 2001 From: noremac201 Date: Wed, 3 Jan 2018 15:41:03 -0600 Subject: [PATCH 2/4] Modified PanelComponent to hold a ProgressBarComponent --- .../ui/overlay/components/PanelComponent.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/ui/overlay/components/PanelComponent.java b/runelite-client/src/main/java/net/runelite/client/ui/overlay/components/PanelComponent.java index 42d446fc82..d2a8cb1047 100644 --- a/runelite-client/src/main/java/net/runelite/client/ui/overlay/components/PanelComponent.java +++ b/runelite-client/src/main/java/net/runelite/client/ui/overlay/components/PanelComponent.java @@ -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 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; } } From 13de888d6cdf8fc7cefa5a31f5a334fe0c04b4ca Mon Sep 17 00:00:00 2001 From: noremac201 Date: Wed, 3 Jan 2018 15:42:54 -0600 Subject: [PATCH 3/4] Updated XpGlobes to use new Components --- .../plugins/xpglobes/XpGlobesOverlay.java | 73 ++++++------------- 1 file changed, 23 insertions(+), 50 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/xpglobes/XpGlobesOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/xpglobes/XpGlobesOverlay.java index 000b25c254..fc118ec382 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/xpglobes/XpGlobesOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/xpglobes/XpGlobesOverlay.java @@ -27,7 +27,6 @@ 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.geom.Arc2D; import java.awt.geom.Ellipse2D; @@ -45,6 +44,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 +70,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 +106,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 +115,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,7 +140,7 @@ public class XpGlobesOverlay extends Overlay if (config.enableTooltips()) { - drawTooltipIfMouseover(graphics, skillToDraw, backgroundCircle); + drawTooltipIfMouseover(graphics, parent, skillToDraw, backgroundCircle); } } @@ -207,7 +206,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 +220,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 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); } } From 0d0c1a2059db255892c2855634d9471eb7b62c69 Mon Sep 17 00:00:00 2001 From: noremac201 Date: Wed, 3 Jan 2018 15:43:28 -0600 Subject: [PATCH 4/4] Fixed setStroke not being reset in Graphics object --- .../net/runelite/client/plugins/xpglobes/XpGlobesOverlay.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/xpglobes/XpGlobesOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/xpglobes/XpGlobesOverlay.java index fc118ec382..3d565feda9 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/xpglobes/XpGlobesOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/xpglobes/XpGlobesOverlay.java @@ -28,6 +28,7 @@ import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; +import java.awt.Stroke; import java.awt.geom.Arc2D; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; @@ -146,6 +147,7 @@ public class XpGlobesOverlay extends Overlay 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( @@ -153,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)