Skip to content

Commit

Permalink
Merge pull request runelite#184 from tylerthardy/itemSprites
Browse files Browse the repository at this point in the history
Add method to draw item sprites on screen
  • Loading branch information
Adam- authored Oct 19, 2017
2 parents ed10a9d + 3afa41b commit 750e25b
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 44 deletions.
2 changes: 2 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ public interface Actor extends Renderable

Point getCanvasImageLocation(Graphics2D graphics, BufferedImage image, int zOffset);

Point getCanvasSpriteLocation(Graphics2D graphics, SpritePixels sprite, int zOffset);

Point getMinimapLocation();
}
2 changes: 2 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public interface Client

ItemComposition getItemDefinition(int id);

SpritePixels createItemSprite(int itemId, int quantity, int border, int shadowColor, int stackable, boolean noted);

int getBaseX();

int getBaseY();
Expand Down
28 changes: 28 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/Perspective.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,32 @@ public static Point getCanvasImageLocation(Client client, Graphics2D graphics, P
return new Point(xOffset, yOffset);
}

/**
* Calculates sprite position and centers depending on sprite size.
*
* @param client
* @param graphics
* @param localLocation local location of the tile
* @param sprite SpritePixel for size measurement
* @param zOffset offset from ground plane
* @return a {@link Point} on screen corresponding to the given
* localLocation.
*/
public static Point getCanvasSpriteLocation(Client client, Graphics2D graphics, Point localLocation, SpritePixels sprite, int zOffset)
{
int plane = client.getPlane();

Point p = Perspective.worldToCanvas(client, localLocation.getX(), localLocation.getY(), plane, zOffset);

if (p == null)
{
return null;
}

int xOffset = p.getX() - sprite.getWidth() / 2;
int yOffset = p.getY() - sprite.getHeight() / 2;

return new Point(xOffset, yOffset);
}

}
36 changes: 36 additions & 0 deletions runelite-api/src/main/java/net/runelite/api/SpritePixels.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.api;

public interface SpritePixels
{
int DEFAULT_SHADOW_COLOR = 3153952;

void drawAt(int x, int y);

int getWidth();

int getHeight();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package net.runelite.client.plugins.fishing;

import net.runelite.api.ItemID;

import java.util.HashMap;
import java.util.Map;
import static net.runelite.api.NpcID.FISHING_SPOT_1506;
Expand Down Expand Up @@ -58,40 +60,40 @@

public enum FishingSpot
{
SHRIMP("Shrimp, Anchovies", "shrimp",
SHRIMP("Shrimp, Anchovies", ItemID.RAW_SHRIMPS,
FISHING_SPOT_1518, FISHING_SPOT_1525, FISHING_SPOT_1528,
FISHING_SPOT_1530, FISHING_SPOT_1544, FISHING_SPOT_7155,
FISHING_SPOT_7469
),
LOBSTER("Lobster, Swordfish, Tuna", "lobster",
LOBSTER("Lobster, Swordfish, Tuna", ItemID.RAW_LOBSTER,
FISHING_SPOT_1510, FISHING_SPOT_1519, FISHING_SPOT_1521,
FISHING_SPOT_1522, FISHING_SPOT_7199, FISHING_SPOT_7470
),
SHARK("Shark, Bass", "shark",
SHARK("Shark, Bass", ItemID.RAW_SHARK,
FISHING_SPOT_1511, FISHING_SPOT_1520, FISHING_SPOT_7200
),
MONKFISH("Monkfish", "monkfish",
MONKFISH("Monkfish", ItemID.RAW_MONKFISH,
FISHING_SPOT_4316
),
SALMON("Salmon, Trout", "salmon",
SALMON("Salmon, Trout", ItemID.RAW_SALMON,
FISHING_SPOT_1506, FISHING_SPOT_1508, FISHING_SPOT_1515,
FISHING_SPOT_1526, FISHING_SPOT_1527
),
BARB_FISH("Sturgeon, Salmon, Trout", "barb",
BARB_FISH("Sturgeon, Salmon, Trout", ItemID.LEAPING_STURGEON,
FISHING_SPOT_1542
),
ANGLERFISH("Anglerfish", "anglerfish",
ANGLERFISH("Anglerfish", ItemID.RAW_ANGLERFISH,
FISHING_SPOT_6825
),
MINNOW("Minnow", "minnow",
MINNOW("Minnow", ItemID.MINNOW,
FISHING_SPOT_7730, FISHING_SPOT_7731, FISHING_SPOT_7732, FISHING_SPOT_7733, FISHING_SPOT_7734
);

public static final int FLYING_FISH = FISHING_SPOT_7734;
private static final Map<Integer, FishingSpot> fishingSpots = new HashMap<>();

private final String name;
private final String image;
private final int itemSpriteId;
private final int[] spots;

static
Expand All @@ -107,10 +109,10 @@ public enum FishingSpot
}
}

FishingSpot(String spot, String image, int... spots)
FishingSpot(String spot, int itemSpriteId, int... spots)
{
this.name = spot;
this.image = image;
this.itemSpriteId = itemSpriteId;
this.spots = spots;
}

Expand All @@ -119,9 +121,9 @@ public String getName()
return name;
}

public String getImage()
public int getItemSpriteId()
{
return image;
return itemSpriteId;
}

public int[] getIds()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.NPC;
import net.runelite.api.SpritePixels;
import net.runelite.api.queries.NPCQuery;
import net.runelite.client.RuneLite;
import net.runelite.client.ui.overlay.Overlay;
Expand All @@ -49,8 +46,6 @@ class FishingSpotOverlay extends Overlay
{
private static final Logger logger = LoggerFactory.getLogger(FishingSpotOverlay.class);

private final BufferedImage[] imgCache = new BufferedImage[FishingSpot.values().length];

private final List<Integer> ids = new ArrayList<>();

private final RuneLite runelite = RuneLite.getRunelite();
Expand Down Expand Up @@ -86,10 +81,10 @@ public Dimension render(Graphics2D graphics)
Color color = npc.getId() == FishingSpot.FLYING_FISH ? Color.RED : Color.CYAN;
if (config.showIcons())
{
BufferedImage fishImage = getFishImage(spot);
if (fishImage != null)
SpritePixels fishSprite = getFishSprite(spot);
if (fishSprite != null)
{
OverlayUtil.renderActorOverlayImage(graphics, npc, fishImage, color.darker());
OverlayUtil.renderActorOverlaySprite(graphics, npc, fishSprite, color.darker());
}
}
else
Expand All @@ -102,28 +97,9 @@ public Dimension render(Graphics2D graphics)
return null;
}

private BufferedImage getFishImage(FishingSpot spot)
private SpritePixels getFishSprite(FishingSpot spot)
{
int fishIdx = spot.ordinal();
BufferedImage fishImage = null;

if (imgCache[fishIdx] != null)
{
return imgCache[fishIdx];
}

try
{
InputStream in = FishingSpotOverlay.class.getResourceAsStream(spot.getImage() + ".png");
fishImage = ImageIO.read(in);
imgCache[fishIdx] = fishImage;
}
catch (IOException e)
{
logger.warn("Error Loading fish icon", e);
}

return fishImage;
return client.createItemSprite(spot.getItemSpriteId(), 5, 1, SpritePixels.DEFAULT_SHADOW_COLOR, 0, false);
}

public void updateConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import net.runelite.api.Actor;
import net.runelite.api.Point;
import net.runelite.api.SpritePixels;
import net.runelite.api.TileObject;


Expand Down Expand Up @@ -78,6 +79,14 @@ public static void renderImageLocation(Graphics2D graphics, Point imgLoc, Buffer
graphics.drawImage(image, x, y, null);
}

public static void renderSpriteLocation(Graphics2D graphics, Point imgLoc, SpritePixels sprite)
{
int x = imgLoc.getX();
int y = imgLoc.getY();

sprite.drawAt(x, y);
}

public static void renderActorOverlay(Graphics2D graphics, Actor actor, String text, Color color)
{
Polygon poly = actor.getCanvasTilePoly();
Expand Down Expand Up @@ -120,6 +129,27 @@ public static void renderActorOverlayImage(Graphics2D graphics, Actor actor, Buf
}
}

public static void renderActorOverlaySprite(Graphics2D graphics, Actor actor, SpritePixels sprite, Color color)
{
Polygon poly = actor.getCanvasTilePoly();
if (poly != null)
{
renderPolygon(graphics, poly, color);
}

Point minimapLocation = actor.getMinimapLocation();
if (minimapLocation != null)
{
renderMinimapLocation(graphics, minimapLocation, color);
}

Point imageLocation = actor.getCanvasSpriteLocation(graphics, sprite, actor.getModelHeight());
if (imageLocation != null)
{
renderSpriteLocation(graphics, imageLocation, sprite);
}
}

public static void renderTileOverlay(Graphics2D graphics, TileObject tileObject, String text, Color color)
{
Polygon poly = tileObject.getCanvasTilePoly();
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.runelite.api.Perspective;
import net.runelite.api.Player;
import net.runelite.api.Point;
import net.runelite.api.SpritePixels;
import net.runelite.api.mixins.Inject;
import net.runelite.api.mixins.Mixin;
import net.runelite.api.mixins.Shadow;
Expand Down Expand Up @@ -143,6 +144,13 @@ public Point getCanvasImageLocation(Graphics2D graphics, BufferedImage image, in
return Perspective.getCanvasImageLocation(client, graphics, getLocalLocation(), image, zOffset);
}

@Inject
@Override
public Point getCanvasSpriteLocation(Graphics2D graphics, SpritePixels sprite, int zOffset)
{
return Perspective.getCanvasSpriteLocation(client, graphics, getLocalLocation(), sprite, zOffset);
}

@Inject
@Override
public Point getMinimapLocation()
Expand Down
4 changes: 4 additions & 0 deletions runescape-api/src/main/java/net/runelite/rs/api/RSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ public interface RSClient extends RSGameEngine, Client
@Override
RSItemComposition getItemDefinition(int itemId);

@Import("createSprite")
@Override
RSSpritePixels createItemSprite(int itemId, int quantity, int thickness, int borderColor, int stackable, boolean noted);

@Import("componentTable")
@Override
RSHashTable getComponentTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@
*/
package net.runelite.rs.api;

public interface RSSpritePixels
import net.runelite.api.SpritePixels;
import net.runelite.mapping.Import;

public interface RSSpritePixels extends SpritePixels
{
@Import("drawAt")
@Override
void drawAt(int x, int y);

@Import("height")
@Override
int getHeight();

@Import("width")
@Override
int getWidth();
}

0 comments on commit 750e25b

Please sign in to comment.