From b616ef9eabc705ac1e4deafa115b26799a64df0e Mon Sep 17 00:00:00 2001 From: seandewar <6256228+seandewar@users.noreply.github.com> Date: Sat, 27 Apr 2019 01:49:35 +0100 Subject: [PATCH] opponentinfo: add option to show both hp value and percent --- .../opponentinfo/HitpointsDisplayStyle.java | 32 +++++++++++++++++++ .../opponentinfo/OpponentInfoConfig.java | 10 +++--- .../opponentinfo/OpponentInfoOverlay.java | 13 ++++++-- .../components/ProgressBarComponent.java | 20 ++++++++++-- 4 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/HitpointsDisplayStyle.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/HitpointsDisplayStyle.java b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/HitpointsDisplayStyle.java new file mode 100644 index 0000000000..0ed6649f16 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/HitpointsDisplayStyle.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019, Sean Dewar + * 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.plugins.opponentinfo; + +public enum HitpointsDisplayStyle +{ + HITPOINTS, + PERCENTAGE, + BOTH; +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoConfig.java index 82a7c5f214..07a84629c8 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoConfig.java @@ -43,14 +43,14 @@ public interface OpponentInfoConfig extends Config } @ConfigItem( - keyName = "showPercent", - name = "Show percent", - description = "Shows hitpoints as a percentage even if hitpoints are known", + keyName = "hitpointsDisplayStyle", + name = "Hitpoints display style", + description = "Show opponent's hitpoints as a value (if known), percentage, or both", position = 1 ) - default boolean showPercent() + default HitpointsDisplayStyle hitpointsDisplayStyle() { - return false; + return HitpointsDisplayStyle.HITPOINTS; } @ConfigItem( diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoOverlay.java index 63432efa20..f7945a5646 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/opponentinfo/OpponentInfoOverlay.java @@ -164,7 +164,10 @@ class OpponentInfoOverlay extends Overlay progressBarComponent.setBackgroundColor(HP_RED); progressBarComponent.setForegroundColor(HP_GREEN); - if (lastMaxHealth != null && !opponentInfoConfig.showPercent()) + final HitpointsDisplayStyle displayStyle = opponentInfoConfig.hitpointsDisplayStyle(); + + if ((displayStyle == HitpointsDisplayStyle.HITPOINTS || displayStyle == HitpointsDisplayStyle.BOTH) + && lastMaxHealth != null) { // This is the reverse of the calculation of healthRatio done by the server // which is: healthRatio = 1 + (healthScale - 1) * health / maxHealth (if health > 0, 0 otherwise) @@ -194,11 +197,15 @@ class OpponentInfoOverlay extends Overlay // so we know nothing about the upper limit except that it can't be higher than maxHealth maxHealth = lastMaxHealth; } - // Take the average of min and max possible healts + // Take the average of min and max possible healths health = (minHealth + maxHealth + 1) / 2; } - progressBarComponent.setLabelDisplayMode(ProgressBarComponent.LabelDisplayMode.FULL); + // Show both the hitpoint and percentage values if enabled in the config + final ProgressBarComponent.LabelDisplayMode progressBarDisplayMode = displayStyle == HitpointsDisplayStyle.BOTH ? + ProgressBarComponent.LabelDisplayMode.BOTH : ProgressBarComponent.LabelDisplayMode.FULL; + + progressBarComponent.setLabelDisplayMode(progressBarDisplayMode); progressBarComponent.setMaximum(lastMaxHealth); progressBarComponent.setValue(health); } 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 index cadced4e96..a891eaba41 100644 --- 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 @@ -40,7 +40,8 @@ public class ProgressBarComponent implements LayoutableRenderableEntity public enum LabelDisplayMode { PERCENTAGE, - FULL + FULL, + BOTH } private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.0"); @@ -79,10 +80,13 @@ public class ProgressBarComponent implements LayoutableRenderableEntity switch (labelDisplayMode) { case PERCENTAGE: - textToWrite = DECIMAL_FORMAT.format(pc * 100d) + "%"; + textToWrite = formatPercentageProgress(pc); + break; + case BOTH: + textToWrite = formatFullProgress(currentValue, maximum) + " (" + formatPercentageProgress(pc) + ")"; break; default: - textToWrite = DECIMAL_FORMAT_ABS.format(Math.floor(currentValue)) + "/" + maximum; + textToWrite = formatFullProgress(currentValue, maximum); } final int width = preferredSize.width; @@ -126,4 +130,14 @@ public class ProgressBarComponent implements LayoutableRenderableEntity bounds.setSize(dimension); return dimension; } + + private static String formatFullProgress(double current, long maximum) + { + return DECIMAL_FORMAT_ABS.format(Math.floor(current)) + "/" + maximum; + } + + private static String formatPercentageProgress(double ratio) + { + return DECIMAL_FORMAT.format(ratio * 100d) + "%"; + } }