Skip to content

Commit

Permalink
overlay: use hovered overlay for picking managed overlay
Browse files Browse the repository at this point in the history
Hovered overlay already accounts for render order, with the exception of
the vanilla WidgetOverlays which are drawn prior to any of our overlays
being drawn. Instead we order them in render-order, which works for the
dynamic-layer ones.

The minimap must be top_right so that our overlays layout around it
correctly, which currently causes it to be picked up first over dynamic
overlays due to the way overlay sorting is done.
  • Loading branch information
Adam- committed May 17, 2022
1 parent 573a66a commit 09949d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package net.runelite.client.ui.overlay;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import java.awt.Color;
import java.awt.Composite;
Expand All @@ -41,7 +40,6 @@
import java.awt.geom.AffineTransform;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
Expand Down Expand Up @@ -104,7 +102,8 @@ public class OverlayRenderer extends MouseAdapter
private boolean inOverlayResizingMode;
private boolean inOverlayDraggingMode;
private boolean startedMovingOverlay;
private Overlay hoveredOverlay; // for building menu entries
private Overlay curHoveredOverlay; // for building menu entries
private Overlay lastHoveredOverlay; // for off-thread access

// Overlay state validation
private Rectangle viewportBounds;
Expand Down Expand Up @@ -166,14 +165,16 @@ public void onFocusChanged(FocusChanged event)
resetOverlayManagementMode();
}

hoveredOverlay = null;
curHoveredOverlay = null;
}
}

@Subscribe
protected void onClientTick(ClientTick t)
{
final Overlay overlay = hoveredOverlay;
lastHoveredOverlay = curHoveredOverlay;

final Overlay overlay = curHoveredOverlay;
if (overlay == null || client.isMenuOpen())
{
return;
Expand Down Expand Up @@ -207,7 +208,7 @@ protected void onClientTick(ClientTick t)
@Subscribe
public void onBeforeRender(BeforeRender event)
{
hoveredOverlay = null;
curHoveredOverlay = null;

if (client.getGameState() == GameState.LOGGED_IN)
{
Expand Down Expand Up @@ -273,10 +274,6 @@ private void renderOverlays(final Graphics2D graphics, Collection<Overlay> overl
graphics.setColor(previous);
}

// Get mouse position
final net.runelite.api.Point mouseCanvasPosition = client.getMouseCanvasPosition();
final Point mouse = new Point(mouseCanvasPosition.getX(), mouseCanvasPosition.getY());

// Save graphics2d properties so we can restore them later
final AffineTransform transform = graphics.getTransform();
final Stroke stroke = graphics.getStroke();
Expand Down Expand Up @@ -372,9 +369,9 @@ else if (inOverlayDraggingMode && overlay.isDragTargetable() && currentManagedOv
graphics.setPaint(paint);
}

if (!client.isMenuOpen() && !client.getSpellSelected() && bounds.contains(mouse))
if (!client.isMenuOpen() && !client.getSpellSelected() && bounds.contains(mousePosition))
{
hoveredOverlay = overlay;
curHoveredOverlay = overlay;
overlay.onMouseOver();
}
}
Expand All @@ -384,17 +381,16 @@ else if (inOverlayDraggingMode && overlay.isDragTargetable() && currentManagedOv
@Override
public MouseEvent mousePressed(MouseEvent mouseEvent)
{
final Point mousePoint = mouseEvent.getPoint();
mousePosition.setLocation(mousePoint);

if (!inOverlayManagingMode)
{
return mouseEvent;
}

final Point mousePoint = mouseEvent.getPoint();
mousePosition.setLocation(mousePoint);

// See if we've clicked on an overlay
currentManagedOverlay = findMangedOverlay(mousePoint);

currentManagedOverlay = lastHoveredOverlay;
if (currentManagedOverlay == null)
{
return mouseEvent;
Expand Down Expand Up @@ -430,17 +426,17 @@ else if (SwingUtilities.isLeftMouseButton(mouseEvent))
@Override
public MouseEvent mouseMoved(MouseEvent mouseEvent)
{
final Point mousePoint = mouseEvent.getPoint();
mousePosition.setLocation(mousePoint);

if (!inOverlayManagingMode)
{
return mouseEvent;
}

final Point mousePoint = mouseEvent.getPoint();
mousePosition.setLocation(mousePoint);

if (!inOverlayResizingMode && !inOverlayDraggingMode)
{
currentManagedOverlay = findMangedOverlay(mousePoint);
currentManagedOverlay = lastHoveredOverlay;
}

if (currentManagedOverlay == null || !currentManagedOverlay.isResizable())
Expand Down Expand Up @@ -487,46 +483,17 @@ public MouseEvent mouseMoved(MouseEvent mouseEvent)
return mouseEvent;
}

/**
* Find an overlay to manage which is under the given mouse point
* @param mousePoint
* @return
*/
private Overlay findMangedOverlay(Point mousePoint)
{
synchronized (overlayManager)
{
// render order is roughly: under -> manual -> above -> always on top
final List<OverlayLayer> layerOrder = ImmutableList.of(OverlayLayer.UNDER_WIDGETS, OverlayLayer.MANUAL, OverlayLayer.ABOVE_WIDGETS, OverlayLayer.ALWAYS_ON_TOP);
return overlayManager.getOverlays()
.stream()
// ABOVE_SCENE overlays aren't managed
.filter(c -> layerOrder.contains(c.getLayer()))
// never allow moving dynamic or tooltip overlays
.filter(Overlay::isMovable)
.sorted(
Comparator.<Overlay>comparingInt(c -> layerOrder.indexOf(c.getLayer()))
.thenComparing(OverlayManager.OVERLAY_COMPARATOR)
// pick order is reversed from render order
.reversed()
)
.filter(o -> o.getBounds().contains(mousePoint))
.findFirst()
.orElse(null);
}
}

@Override
public MouseEvent mouseDragged(MouseEvent mouseEvent)
{
final Point p = mouseEvent.getPoint();
mousePosition.setLocation(p);

if (!inOverlayManagingMode)
{
return mouseEvent;
}

final Point p = mouseEvent.getPoint();
mousePosition.setLocation(p);

if (currentManagedOverlay == null)
{
return mouseEvent;
Expand Down Expand Up @@ -661,13 +628,14 @@ else if (inOverlayDraggingMode)
@Override
public MouseEvent mouseReleased(MouseEvent mouseEvent)
{
final Point mousePoint = mouseEvent.getPoint();
mousePosition.setLocation(mousePoint);

if (!inOverlayManagingMode || currentManagedOverlay == null || (!inOverlayDraggingMode && !inOverlayResizingMode))
{
return mouseEvent;
}

mousePosition.setLocation(-1, -1);

if (dragTargetOverlay != null)
{
if (dragTargetOverlay.onDrag(currentManagedOverlay))
Expand All @@ -685,7 +653,7 @@ public MouseEvent mouseReleased(MouseEvent mouseEvent)

for (Rectangle snapCorner : snapCorners.getBounds())
{
if (snapCorner.contains(mouseEvent.getPoint()))
if (snapCorner.contains(mousePoint))
{
OverlayPosition position = snapCorners.fromBounds(snapCorner);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ public class WidgetOverlay extends Overlay
public static Collection<WidgetOverlay> createOverlays(final OverlayManager overlayManager, final Client client)
{
return Arrays.asList(
// classic resizable
new WidgetOverlay(client, WidgetInfo.RESIZABLE_VIEWPORT_CHATBOX_PARENT, OverlayPosition.DYNAMIC, OverlayPriority.HIGH),
new WidgetOverlay(client, WidgetInfo.RESIZABLE_VIEWPORT_BOTTOM_LINE_CHATBOX_PARENT, OverlayPosition.DYNAMIC, OverlayPriority.HIGH),
// classic resizable - these are in render order for managed overlay picking
new WidgetOverlay(client, WidgetInfo.RESIZABLE_VIEWPORT_CHATBOX_PARENT, OverlayPosition.DYNAMIC),
new WidgetOverlay(client, WidgetInfo.RESIZABLE_VIEWPORT_INVENTORY_PARENT, OverlayPosition.DYNAMIC),
new WidgetOverlay(client, WidgetInfo.RESIZABLE_MINIMAP_WIDGET, OverlayPosition.CANVAS_TOP_RIGHT, OverlayPriority.MED),
new WidgetOverlay(client, WidgetInfo.RESIZABLE_MINIMAP_STONES_WIDGET, OverlayPosition.CANVAS_TOP_RIGHT),
// modern resizable
new WidgetOverlay(client, WidgetInfo.RESIZABLE_VIEWPORT_BOTTOM_LINE_CHATBOX_PARENT, OverlayPosition.DYNAMIC),
new WidgetOverlay(client, WidgetInfo.RESIZABLE_MINIMAP_WIDGET, OverlayPosition.CANVAS_TOP_RIGHT),
new WidgetOverlay(client, WidgetInfo.RESIZABLE_VIEWPORT_BOTTOM_LINE_TABS1, OverlayPosition.DYNAMIC),
new WidgetOverlay(client, WidgetInfo.RESIZABLE_VIEWPORT_BOTTOM_LINE_TABS2, OverlayPosition.DYNAMIC),
new WidgetOverlay(client, WidgetInfo.RESIZABLE_VIEWPORT_BOTTOM_LINE_INVENTORY_PARENT, OverlayPosition.DYNAMIC),
new WidgetOverlay(client, WidgetInfo.RESIZABLE_MINIMAP_STONES_WIDGET, OverlayPosition.CANVAS_TOP_RIGHT, OverlayPriority.MED),
// The client forces the oxygen bar below the xp tracker, so set its priority lower
new WidgetOverlay(client, WidgetInfo.FOSSIL_ISLAND_OXYGENBAR, OverlayPosition.TOP_CENTER, OverlayPriority.HIGH),
new XpTrackerWidgetOverlay(overlayManager, client, WidgetInfo.EXPERIENCE_TRACKER_WIDGET, OverlayPosition.TOP_RIGHT),
Expand Down

0 comments on commit 09949d3

Please sign in to comment.