diff --git a/runelite-api/src/main/java/net/runelite/api/Client.java b/runelite-api/src/main/java/net/runelite/api/Client.java index 9218958dd1..90ae2e24ed 100644 --- a/runelite-api/src/main/java/net/runelite/api/Client.java +++ b/runelite-api/src/main/java/net/runelite/api/Client.java @@ -26,6 +26,7 @@ package net.runelite.api; import java.awt.Canvas; import java.util.Arrays; +import net.runelite.api.widgets.Widget; public class Client { @@ -174,4 +175,32 @@ public class Client { return client.getBaseY(); } + + public Widget getWidget(int groupId, int childId) + { + net.runelite.rs.api.Widget[][] widgets = client.getWidgets(); + + if (widgets == null || widgets.length <= groupId) + { + return null; + } + + net.runelite.rs.api.Widget[] childWidgets = widgets[groupId]; + if (childWidgets == null || childWidgets.length <= childId) + { + return null; + } + + return new Widget(this, childWidgets[childId]); + } + + public int[] getWidgetPositionsX() + { + return client.getWidgetPositionsX(); + } + + public int[] getWidgetPositionsY() + { + return client.getWidgetPositionsY(); + } } diff --git a/runelite-api/src/main/java/net/runelite/api/widgets/Widget.java b/runelite-api/src/main/java/net/runelite/api/widgets/Widget.java new file mode 100644 index 0000000000..ee345822a9 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/widgets/Widget.java @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2017, Adam + * 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.widgets; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import net.runelite.api.Client; +import net.runelite.api.Point; + +public class Widget +{ + private static final int WIDGET_ITEM_WIDTH = 42; + private static final int WIDGET_ITEM_HEIGHT = 36; + + private final Client client; + private final net.runelite.rs.api.Widget widget; + + public Widget(Client client, net.runelite.rs.api.Widget widget) + { + this.client = client; + this.widget = widget; + } + + public Widget getParent() + { + net.runelite.rs.api.Widget parent = widget.getParent(); + + if (parent == null) + { + return null; + } + + return new Widget(client, parent); + } + + private int getRelativeX() + { + return widget.getRelativeX(); + } + + private int getRelativeY() + { + return widget.getRelativeY(); + } + + public Point getCanvasLocation() + { + int x = 0; + int y = 0; + + int boundsIndex = widget.getBoundsIndex(); + if (boundsIndex != -1) + { + int[] widgetBoundsWidth = client.getWidgetPositionsX(); + int[] widgetBoundsHeight = client.getWidgetPositionsY(); + + x += widgetBoundsWidth[boundsIndex]; + y += widgetBoundsHeight[boundsIndex]; + } + + for (Widget cur = this; cur != null; cur = cur.getParent()) + { + x += cur.getRelativeX(); + y += cur.getRelativeY(); + } + + return new Point(x, y); + } + + public int getWidth() + { + return widget.getWidth(); + } + + public int getHeight() + { + return widget.getHeight(); + } + + public Collection getWidgetItems() + { + int[] itemIds = widget.getItemIds(); + int[] itemQuantities = widget.getItemQuantities(); + + if (itemIds == null || itemQuantities == null) + { + return null; + } + + List items = new ArrayList<>(itemIds.length); + + assert itemIds.length == itemQuantities.length; + + int itemsX = getWidth(); // this appears to be the number of items that fit in the width + Point widgetCanvasLocation = getCanvasLocation(); + + for (int i = 0; i < itemIds.length; ++i) + { + int itemId = itemIds[i]; + int itemQuantity = itemQuantities[i]; + + if (itemId <= 0 || itemQuantity <= 0) + { + continue; + } + + Rectangle bounds = null; + + if (itemsX > 0) + { + int itemX = widgetCanvasLocation.getX() + (i % itemsX) * WIDGET_ITEM_WIDTH; + int itemY = widgetCanvasLocation.getY() + (i / itemsX) * WIDGET_ITEM_HEIGHT; + + bounds = new Rectangle(itemX + 1, itemY - 1, WIDGET_ITEM_WIDTH - 2, WIDGET_ITEM_HEIGHT - 2); + } + + WidgetItem item = new WidgetItem(itemId - 1, itemQuantity, i, bounds); + items.add(item); + } + + return items; + } +} diff --git a/runelite-api/src/main/java/net/runelite/api/widgets/WidgetID.java b/runelite-api/src/main/java/net/runelite/api/widgets/WidgetID.java new file mode 100644 index 0000000000..f40d76a9f7 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/widgets/WidgetID.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017, Adam + * 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.widgets; + +public class WidgetID +{ + public static final int INVENTORY_GROUP_ID = 149; + public static final int INVENTORY_CHILD_ID = 0; +} diff --git a/runelite-api/src/main/java/net/runelite/api/widgets/WidgetItem.java b/runelite-api/src/main/java/net/runelite/api/widgets/WidgetItem.java new file mode 100644 index 0000000000..7576b9e423 --- /dev/null +++ b/runelite-api/src/main/java/net/runelite/api/widgets/WidgetItem.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2017, Adam + * 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.widgets; + +import java.awt.Rectangle; + +public class WidgetItem +{ + private final int id; + private final int quantity; + private final int index; + private final Rectangle canvasBounds; + + public WidgetItem(int id, int quantity, int index, Rectangle canvasBounds) + { + this.id = id; + this.quantity = quantity; + this.index = index; + this.canvasBounds = canvasBounds; + } + + @Override + public String toString() + { + return "WidgetItem{" + "id=" + id + ", quantity=" + quantity + ", index=" + index + ", canvasBounds=" + canvasBounds + '}'; + } + + public int getId() + { + return id; + } + + public int getQuantity() + { + return quantity; + } + + public int getIndex() + { + return index; + } + + public Rectangle getCanvasBounds() + { + return canvasBounds; + } + +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/debug/DebugOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/debug/DebugOverlay.java index 481983426c..bc4a827054 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/debug/DebugOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/debug/DebugOverlay.java @@ -27,6 +27,7 @@ package net.runelite.client.plugins.debug; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; +import java.awt.Rectangle; import net.runelite.api.Client; import net.runelite.api.DecorativeObject; import net.runelite.api.GameObject; @@ -37,6 +38,9 @@ import net.runelite.api.Region; import net.runelite.api.Tile; import net.runelite.api.TileObject; import net.runelite.api.WallObject; +import net.runelite.api.widgets.Widget; +import net.runelite.api.widgets.WidgetID; +import net.runelite.api.widgets.WidgetItem; import net.runelite.client.RuneLite; import net.runelite.client.ui.overlay.Overlay; import net.runelite.client.ui.overlay.OverlayPosition; @@ -60,6 +64,14 @@ public class DebugOverlay extends Overlay return null; } + renderTiles(graphics); + renderInventory(graphics); + + return null; + } + + private void renderTiles(Graphics2D graphics) + { Region region = client.getRegion(); Tile[][][] tiles = region.getTiles(); @@ -79,8 +91,30 @@ public class DebugOverlay extends Overlay render(graphics, tile); } } + } - return null; + private void renderInventory(Graphics2D graphics) + { + Widget inventoryWidget = client.getWidget(WidgetID.INVENTORY_GROUP_ID, WidgetID.INVENTORY_CHILD_ID); + if (inventoryWidget == null) + { + return; + } + + for (WidgetItem item : inventoryWidget.getWidgetItems()) + { + Rectangle bounds = item.getCanvasBounds(); + + Color[] colors = new Color[] + { + Color.RED, Color.GREEN, Color.BLUE + }; + graphics.setColor(colors[item.getIndex() % 3]); + if (bounds != null) + { + graphics.draw(bounds); + } + } } private void render(Graphics2D graphics, Tile tile) diff --git a/runescape-api/src/main/java/net/runelite/rs/api/Client.java b/runescape-api/src/main/java/net/runelite/rs/api/Client.java index 3fee74b932..1219c8837b 100644 --- a/runescape-api/src/main/java/net/runelite/rs/api/Client.java +++ b/runescape-api/src/main/java/net/runelite/rs/api/Client.java @@ -25,10 +25,9 @@ package net.runelite.rs.api; +import java.awt.Canvas; import net.runelite.mapping.Import; -import java.awt.*; - public interface Client extends GameEngine { @Import("cameraX") diff --git a/runescape-api/src/main/java/net/runelite/rs/api/Widget.java b/runescape-api/src/main/java/net/runelite/rs/api/Widget.java index 3e340bce39..abf4862eb7 100644 --- a/runescape-api/src/main/java/net/runelite/rs/api/Widget.java +++ b/runescape-api/src/main/java/net/runelite/rs/api/Widget.java @@ -122,9 +122,9 @@ public interface Widget @Import("itemQuantity") int getItemQuantity(); - @Import("x") - int getX(); + @Import("originalX") + int getOriginalX(); - @Import("y") - int getY(); + @Import("originalY") + int getOriginalY(); }