clan manager: Use Sprite IDs for rank badges

This commit adds `ImageUtil::resizeCanvas`, `ImageUtil::outlineImage`,
and `ImageUtil::fillImage`, and applies these functions to the clan
manager to replace the clan rank images with calls to `SpriteManager`.
This commit is contained in:
Jordan Atwood
2018-07-29 19:38:23 -07:00
parent 8fb36d3ea4
commit d6e465877e
14 changed files with 303 additions and 48 deletions

View File

@@ -28,16 +28,17 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.eventbus.Subscribe;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
@@ -46,26 +47,33 @@ import net.runelite.api.ClanMemberRank;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.IndexedSprite;
import net.runelite.api.SpriteID;
import net.runelite.api.events.ClanChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.Text;
@Singleton
@Slf4j
public class ClanManager
{
private static final String[] CLANCHAT_IMAGES =
private static final int[] CLANCHAT_IMAGES =
{
"Friend_clan_rank.png", "Recruit_clan_rank.png",
"Corporal_clan_rank.png", "Sergeant_clan_rank.png",
"Lieutenant_clan_rank.png", "Captain_clan_rank.png",
"General_clan_rank.png", "Owner_clan_rank.png",
"JMod_clan_rank.png"
SpriteID.CLAN_CHAT_RANK_SMILEY_FRIEND,
SpriteID.CLAN_CHAT_RANK_SINGLE_CHEVRON_RECRUIT,
SpriteID.CLAN_CHAT_RANK_DOUBLE_CHEVRON_CORPORAL,
SpriteID.CLAN_CHAT_RANK_TRIPLE_CHEVRON_SERGEANT,
SpriteID.CLAN_CHAT_RANK_BRONZE_STAR_LIEUTENANT,
SpriteID.CLAN_CHAT_RANK_SILVER_STAR_CAPTAIN,
SpriteID.CLAN_CHAT_RANK_GOLD_STAR_GENERAL,
SpriteID.CLAN_CHAT_RANK_KEY_CHANNEL_OWNER,
SpriteID.CLAN_CHAT_RANK_CROWN_JAGEX_MODERATOR,
};
private int modIconsLength;
private static final Dimension CLANCHAT_IMAGE_DIMENSION = new Dimension(11, 11);
private static final Color CLANCHAT_IMAGE_OUTLINE_COLOR = new Color(33, 33, 33);
private final Client client;
private final SpriteManager spriteManager;
private final BufferedImage[] clanChatImages = new BufferedImage[CLANCHAT_IMAGES.length];
private final LoadingCache<String, ClanMemberRank> clanRanksCache = CacheBuilder.newBuilder()
@@ -74,7 +82,7 @@ public class ClanManager
.build(new CacheLoader<String, ClanMemberRank>()
{
@Override
public ClanMemberRank load(String key) throws Exception
public ClanMemberRank load(@Nonnull String key)
{
final ClanMember[] clanMembersArr = client.getClanMembers();
@@ -92,27 +100,13 @@ public class ClanManager
}
});
private int modIconsLength;
@Inject
private ClanManager(Client client)
private ClanManager(Client client, SpriteManager spriteManager)
{
this.client = client;
int i = 0;
for (String resource : CLANCHAT_IMAGES)
{
try
{
final BufferedImage bufferedImage = rgbaToIndexedBufferedImage(ImageIO
.read(ClanManager.class.getResource(resource)));
clanChatImages[i] = bufferedImage;
}
catch (IOException e)
{
log.warn("unable to load clan image", e);
}
++i;
}
this.spriteManager = spriteManager;
}
public ClanMemberRank getRank(String playerName)
@@ -138,10 +132,9 @@ public class ClanManager
@Subscribe
public void onGameStateChanged(GameStateChanged gameStateChanged)
{
if (gameStateChanged.getGameState() == GameState.LOGIN_SCREEN
if (gameStateChanged.getGameState() == GameState.LOGGED_IN
&& modIconsLength == 0)
{
// this is after "Loading sprites" so we can modify modicons now
loadClanChatIcons();
}
}
@@ -154,25 +147,19 @@ public class ClanManager
private void loadClanChatIcons()
{
try
{
final IndexedSprite[] modIcons = client.getModIcons();
final IndexedSprite[] newModIcons = Arrays.copyOf(modIcons, modIcons.length + CLANCHAT_IMAGES.length);
int curPosition = newModIcons.length - CLANCHAT_IMAGES.length;
final IndexedSprite[] modIcons = client.getModIcons();
final IndexedSprite[] newModIcons = Arrays.copyOf(modIcons, modIcons.length + CLANCHAT_IMAGES.length);
int curPosition = newModIcons.length - CLANCHAT_IMAGES.length;
for (BufferedImage image : clanChatImages)
{
IndexedSprite sprite = createIndexedSprite(client, image);
newModIcons[curPosition++] = sprite;
}
client.setModIcons(newModIcons);
modIconsLength = newModIcons.length;
}
catch (IOException e)
for (int i = 0; i < CLANCHAT_IMAGES.length; i++, curPosition++)
{
log.warn("Failed loading of clan chat icons", e);
final int resource = CLANCHAT_IMAGES[i];
clanChatImages[i] = rgbaToIndexedBufferedImage(clanChatImageFromSprite(spriteManager.getSprite(resource, 0)));
newModIcons[curPosition] = createIndexedSprite(client, clanChatImages[i]);
}
client.setModIcons(newModIcons);
modIconsLength = newModIcons.length;
}
private static String sanitize(String lookup)
@@ -181,7 +168,7 @@ public class ClanManager
return cleaned.replace('\u00A0', ' ');
}
private static IndexedSprite createIndexedSprite(final Client client, final BufferedImage bufferedImage) throws IOException
private static IndexedSprite createIndexedSprite(final Client client, final BufferedImage bufferedImage)
{
final IndexColorModel indexedCM = (IndexColorModel) bufferedImage.getColorModel();
@@ -228,4 +215,10 @@ public class ClanManager
resultIndexedImage.getGraphics().drawImage(sourceBufferedImage, 0, 0, null);
return resultIndexedImage;
}
private static BufferedImage clanChatImageFromSprite(final BufferedImage clanSprite)
{
final BufferedImage clanChatCanvas = ImageUtil.resizeCanvas(clanSprite, CLANCHAT_IMAGE_DIMENSION.width, CLANCHAT_IMAGE_DIMENSION.height);
return ImageUtil.outlineImage(clanChatCanvas, CLANCHAT_IMAGE_OUTLINE_COLOR);
}
}

View File

@@ -112,4 +112,14 @@ public class ColorUtil
{
return String.format("%06x", color.getRGB() & 0xFFFFFF);
}
static boolean isFullyTransparent(final Color color)
{
return color.getAlpha() == 0;
}
static boolean isNotFullyTransparent(final Color color)
{
return !isFullyTransparent(color);
}
}

View File

@@ -24,6 +24,7 @@
*/
package net.runelite.client.util;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
@@ -32,6 +33,7 @@ import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.util.Arrays;
import javax.swing.GrayFilter;
import java.util.function.Predicate;
/**
* Various Image/BufferedImage utilities.
@@ -178,6 +180,26 @@ public class ImageUtil
return ImageUtil.bufferedImageFromImage(resized);
}
/**
* Re-size a BufferedImage's canvas to the given dimensions.
*
* @param image The image whose canvas should be re-sized.
* @param newWidth The width to set the BufferedImage to.
* @param newHeight The height to set the BufferedImage to.
* @return The BufferedImage centered within canvas of given dimensions.
*/
public static BufferedImage resizeCanvas(final BufferedImage image, final int newWidth, final int newHeight)
{
final BufferedImage dimg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
final int centeredX = newWidth / 2 - image.getWidth() / 2;
final int centeredY = newHeight / 2 - image.getHeight() / 2;
final Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(image, centeredX, centeredY, null);
g2d.dispose();
return dimg;
}
/**
* Rotates an image around its center by a given number of radians.
*
@@ -229,6 +251,124 @@ public class ImageUtil
return out;
}
/**
* Outlines non-transparent pixels of a BufferedImage with the given color.
*
* @param image The image to be outlined.
* @param color The color to use for the outline.
* @return The BufferedImage with its edges outlined with the given color.
*/
public static BufferedImage outlineImage(final BufferedImage image, final Color color)
{
return outlineImage(image, color, ColorUtil::isNotFullyTransparent, false);
}
/**
* Outlines pixels of a BufferedImage with the given color, using a given predicate to colorize
* the given image for outlining.
*
* @param image The image to be outlined.
* @param color The color to use for the outline.
* @param fillCondition The predicate to be consumed by {@link #fillImage(BufferedImage, Color, Predicate) fillImage(BufferedImage, Color, Predicate)}
* @return The BufferedImage with its edges outlined with the given color.
*/
public static BufferedImage outlineImage(final BufferedImage image, final Color color, final Predicate<Color> fillCondition)
{
return outlineImage(image, color, fillCondition, false);
}
/**
* Outlines non-transparent pixels of a BufferedImage with the given color. Optionally outlines
* corners in addition to edges.
*
* @param image The image to be outlined.
* @param color The color to use for the outline.
* @param outlineCorners Whether to draw an outline around corners, or only around edges.
* @return The BufferedImage with its edges--and optionally, corners--outlined
* with the given color.
*/
public static BufferedImage outlineImage(final BufferedImage image, final Color color, final Boolean outlineCorners)
{
return outlineImage(image, color, ColorUtil::isNotFullyTransparent, outlineCorners);
}
/**
* Outlines pixels of a BufferedImage with the given color, using a given predicate to colorize
* the given image for outlining. Optionally outlines corners in addition to edges.
*
* @param image The image to be outlined.
* @param color The color to use for the outline.
* @param fillCondition The predicate to be consumed by {@link #fillImage(BufferedImage, Color, Predicate) fillImage(BufferedImage, Color, Predicate)}
* @param outlineCorners Whether to draw an outline around corners, or only around edges.
* @return The BufferedImage with its edges--and optionally, corners--outlined
* with the given color.
*/
public static BufferedImage outlineImage(final BufferedImage image, final Color color, final Predicate<Color> fillCondition, final Boolean outlineCorners)
{
final BufferedImage filledImage = fillImage(image, color, fillCondition);
final BufferedImage outlinedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = outlinedImage.createGraphics();
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if ((x == 0 && y == 0)
|| (!outlineCorners && Math.abs(x) + Math.abs(y) != 1))
{
continue;
}
g2d.drawImage(filledImage, x, y, null);
}
}
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return outlinedImage;
}
/**
* Fills all non-transparent pixels of the given image with the given color.
*
* @param image The image which should have its non-transparent pixels filled.
* @param color The color with which to fill pixels.
* @return The given image with all non-transparent pixels set to the given color.
*/
static BufferedImage fillImage(final BufferedImage image, final Color color)
{
return fillImage(image, color, ColorUtil::isNotFullyTransparent);
}
/**
* Fills pixels of the given image with the given color based on a given fill condition
* predicate.
*
* @param image The image which should have its non-transparent pixels filled.
* @param color The color with which to fill pixels.
* @param fillCondition The condition on which to fill pixels with the given color.
* @return The given image with all pixels fulfilling the fill condition predicate
* set to the given color.
*/
static BufferedImage fillImage(final BufferedImage image, final Color color, final Predicate<Color> fillCondition)
{
final BufferedImage filledImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < filledImage.getWidth(); x++)
{
for (int y = 0; y < filledImage.getHeight(); y++)
{
final Color pixelColor = new Color(image.getRGB(x, y), true);
if (!fillCondition.test(pixelColor))
{
continue;
}
filledImage.setRGB(x, y, color.getRGB());
}
}
return filledImage;
}
/**
* Performs a rescale operation on the image's color components.
*