diff --git a/runelite-client/src/main/java/net/runelite/client/ui/overlay/WidgetOverlay.java b/runelite-client/src/main/java/net/runelite/client/ui/overlay/WidgetOverlay.java index f5fb94e8b9..9386120bf1 100644 --- a/runelite-client/src/main/java/net/runelite/client/ui/overlay/WidgetOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/ui/overlay/WidgetOverlay.java @@ -60,6 +60,7 @@ public class WidgetOverlay extends Overlay private final Client client; private final WidgetInfo widgetInfo; + private final Rectangle parentBounds = new Rectangle(); private WidgetOverlay(final Client client, final WidgetInfo widgetInfo, final OverlayPosition overlayPosition) { @@ -76,17 +77,39 @@ public class WidgetOverlay extends Overlay return Objects.toString(widgetInfo); } + @Override + public Rectangle getBounds() + { + final Rectangle bounds = super.getBounds(); + final Rectangle parent = getParentBounds(client.getWidget(widgetInfo)); + + if (parent.isEmpty()) + { + return bounds; + } + + int x = bounds.x; + int y = bounds.y; + x = Math.max(parent.x, x); + y = Math.max(parent.y, y); + x = Math.min((int)parent.getMaxX() - bounds.width, x); + y = Math.min((int)parent.getMaxY() - bounds.height, y); + bounds.setLocation(x, y); + return bounds; + } + @Override public Dimension render(Graphics2D graphics) { final Widget widget = client.getWidget(widgetInfo); - if (widget == null || widget.isHidden()) + final Rectangle bounds = super.getBounds(); + final Rectangle parent = getParentBounds(widget); + + if (parent.isEmpty()) { return null; } - final Rectangle bounds = getBounds(); - final Rectangle parent = getParentBounds(widget); int x = bounds.x; int y = bounds.y; x = Math.max(parent.x, x); @@ -103,12 +126,30 @@ public class WidgetOverlay extends Overlay private Rectangle getParentBounds(final Widget widget) { - final Widget parent = widget.getParent(); - if (parent == null) + if (!client.isClientThread()) { - return new Rectangle(client.getRealDimensions()); + return parentBounds; } - return new Rectangle(parent.getCanvasLocation().getX(), parent.getCanvasLocation().getY(), parent.getWidth(), parent.getHeight()); + if (widget == null || widget.isHidden()) + { + parentBounds.setBounds(new Rectangle()); + return parentBounds; + } + + final Widget parent = widget.getParent(); + final Rectangle bounds; + + if (parent == null) + { + bounds = new Rectangle(client.getRealDimensions()); + } + else + { + bounds = new Rectangle(parent.getCanvasLocation().getX(), parent.getCanvasLocation().getY(), parent.getWidth(), parent.getHeight()); + } + + parentBounds.setBounds(bounds); + return bounds; } }