runepouch plugin: use itemmanager to render runepouch item icons

This commit is contained in:
Tyler Hardy
2018-02-08 20:40:54 -06:00
committed by Adam
parent 2e8013b078
commit e2d5d8957b
24 changed files with 158 additions and 114 deletions

View File

@@ -1,103 +0,0 @@
/*
* Copyright (c) 2017, Tyler <https://github.com/tylerthardy>
* 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.runepouch;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;
import javax.imageio.ImageIO;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RuneImageCache
{
// ids from the varbit
private static final String[] RUNE_NAMES =
{
"None",
"Air", //1
"Water", //2
"Earth", //3
"Fire", //4
"Mind", //5
"Chaos", //6
"Death", //7
"Blood", //8
"Cosmic", //9
"Nature", //10
"Law", //11
"Body", //12
"Soul", //13
"Astral", //14
"Mist", //15
"Mud", //16
"Dust", //17
"Lava", //18
"Steam", //19
"Smoke", //20
"Wrath" //21
};
private final LoadingCache<Integer, BufferedImage> cache;
public RuneImageCache()
{
cache = CacheBuilder.newBuilder()
.maximumSize(RUNE_NAMES.length)
.build(
new CacheLoader<Integer, BufferedImage>()
{
@Override
public BufferedImage load(Integer runeId) throws Exception
{
InputStream in = RunepouchOverlay.class.getResourceAsStream(RUNE_NAMES[runeId] + ".png");
return ImageIO.read(in);
}
}
);
}
public BufferedImage getImage(int runeId)
{
try
{
return cache.get(runeId);
}
catch (ExecutionException e)
{
log.warn("unable to load rune image", e);
return null;
}
}
public String getName(int runeId)
{
return RUNE_NAMES[runeId];
}
}

View File

@@ -36,6 +36,7 @@ import net.runelite.api.Query;
import net.runelite.api.Varbits;
import net.runelite.api.queries.InventoryWidgetItemQuery;
import net.runelite.api.widgets.WidgetItem;
import net.runelite.client.game.ItemManager;
import net.runelite.client.ui.FontManager;
import net.runelite.client.ui.overlay.Overlay;
import net.runelite.client.ui.overlay.OverlayLayer;
@@ -55,14 +56,17 @@ public class RunepouchOverlay extends Overlay
{
Varbits.RUNE_POUCH_RUNE1, Varbits.RUNE_POUCH_RUNE2, Varbits.RUNE_POUCH_RUNE3
};
private static final Dimension IMAGE_SIZE = new Dimension(11, 11);
private final RuneImageCache runeImageCache = new RuneImageCache();
private final QueryRunner queryRunner;
private final Client client;
private final RunepouchConfig config;
private final TooltipManager tooltipManager;
@Inject
private ItemManager itemManager;
@Inject
RunepouchOverlay(QueryRunner queryRunner, Client client, RunepouchConfig config, TooltipManager tooltipManager)
{
@@ -114,11 +118,16 @@ public class RunepouchOverlay extends Overlay
Varbits runeVarbit = RUNE_VARBITS[i];
int runeId = client.getSetting(runeVarbit);
Runes rune = Runes.getRune(runeId);
if (rune == null)
{
continue;
}
tooltipBuilder
.append(amount)
.append(" <col=ffff00>")
.append(runeImageCache.getName(runeId))
.append(rune.getName())
.append("</col></br>");
if (config.showOnlyOnHover())
@@ -127,27 +136,27 @@ public class RunepouchOverlay extends Overlay
}
graphics.setColor(Color.black);
graphics.drawString("" + formatNumber(amount), location.getX() + (config.showIcons() ? 13 : 1),
location.getY() + 14 + graphics.getFontMetrics().getHeight() * i);
graphics.drawString("" + formatNumber(amount), location.getX() + (config.showIcons() ? 13 : 6),
location.getY() + 14 + (graphics.getFontMetrics().getHeight() - 1) * i);
graphics.setColor(config.fontColor());
graphics.drawString("" + formatNumber(amount), location.getX() + (config.showIcons() ? 12 : 0),
location.getY() + 13 + graphics.getFontMetrics().getHeight() * i);
graphics.drawString("" + formatNumber(amount), location.getX() + (config.showIcons() ? 12 : 5),
location.getY() + 13 + (graphics.getFontMetrics().getHeight() - 1) * i);
if (!config.showIcons())
{
continue;
}
BufferedImage runeImg = runeImageCache.getImage(runeId);
if (runeImg != null)
BufferedImage image = getRuneImage(rune);
if (image != null)
{
OverlayUtil.renderImageLocation(graphics,
new Point(location.getX(), location.getY() + 2 + (graphics.getFontMetrics().getHeight()) * i),
runeImg);
new Point(location.getX(), location.getY() + graphics.getFontMetrics().getHeight() * i),
getRuneImage(rune));
}
}
String tooltip = tooltipBuilder.toString();
if (!tooltip.isEmpty() && runePouch.getCanvasBounds().contains(client.getMouseCanvasPosition().getX(), client.getMouseCanvasPosition().getY()))
@@ -157,6 +166,29 @@ public class RunepouchOverlay extends Overlay
return null;
}
private BufferedImage getRuneImage(Runes rune)
{
BufferedImage runeImg = rune.getImage();
if (runeImg != null)
{
return runeImg;
}
runeImg = itemManager.getImage(rune.getItemId());
if (runeImg == null)
{
return null;
}
BufferedImage resizedImg = new BufferedImage(IMAGE_SIZE.width, IMAGE_SIZE.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImg.createGraphics();
g.drawImage(runeImg, 0, 0, IMAGE_SIZE.width, IMAGE_SIZE.height, null);
g.dispose();
rune.setImage(resizedImg);
return resizedImg;
}
private static String formatNumber(int amount)
{
return amount < 1000 ? String.valueOf(amount) : amount / 1000 + "K";

View File

@@ -0,0 +1,115 @@
/*
* Copyright (c) 2017, Tyler <https://github.com/tylerthardy>
* 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.runepouch;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import static net.runelite.api.ItemID.AIR_RUNE;
import static net.runelite.api.ItemID.ASTRAL_RUNE;
import static net.runelite.api.ItemID.BLOOD_RUNE;
import static net.runelite.api.ItemID.BODY_RUNE;
import static net.runelite.api.ItemID.CHAOS_RUNE;
import static net.runelite.api.ItemID.COSMIC_RUNE;
import static net.runelite.api.ItemID.DEATH_RUNE;
import static net.runelite.api.ItemID.DUST_RUNE;
import static net.runelite.api.ItemID.EARTH_RUNE;
import static net.runelite.api.ItemID.FIRE_RUNE;
import static net.runelite.api.ItemID.LAVA_RUNE;
import static net.runelite.api.ItemID.LAW_RUNE;
import static net.runelite.api.ItemID.MIND_RUNE;
import static net.runelite.api.ItemID.MIST_RUNE;
import static net.runelite.api.ItemID.MUD_RUNE;
import static net.runelite.api.ItemID.NATURE_RUNE;
import static net.runelite.api.ItemID.SMOKE_RUNE;
import static net.runelite.api.ItemID.SOUL_RUNE;
import static net.runelite.api.ItemID.STEAM_RUNE;
import static net.runelite.api.ItemID.WATER_RUNE;
import static net.runelite.api.ItemID.WRATH_RUNE;
public enum Runes
{
AIR(1, AIR_RUNE),
WATER(2, WATER_RUNE),
EARTH(3, EARTH_RUNE),
FIRE(4, FIRE_RUNE),
MIND(5, MIND_RUNE),
CHAOS(6, CHAOS_RUNE),
DEATH(7, DEATH_RUNE),
BLOOD(8, BLOOD_RUNE),
COSMIC(9, COSMIC_RUNE),
NATURE(10, NATURE_RUNE),
LAW(11, LAW_RUNE),
BODY(12, BODY_RUNE),
SOUL(13, SOUL_RUNE),
ASTRAL(14, ASTRAL_RUNE),
MIST(15, MIST_RUNE),
MUD(16, MUD_RUNE),
DUST(17, DUST_RUNE),
LAVA(18, LAVA_RUNE),
STEAM(19, STEAM_RUNE),
SMOKE(20, SMOKE_RUNE),
WRATH(21, WRATH_RUNE);
@Getter
private final int id;
@Getter
private final int itemId;
@Getter
@Setter
private BufferedImage image;
private static final Map<Integer, Runes> runes = new HashMap<>();
static
{
for (Runes rune : values())
{
runes.put(rune.getId(), rune);
}
}
Runes(int id, int itemId)
{
this.id = id;
this.itemId = itemId;
}
public static Runes getRune(int varbit)
{
return runes.get(varbit);
}
public String getName()
{
String name = this.name();
name = name.substring(0, 1) + name.substring(1, name.length()).toLowerCase();
return name;
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB