client: Make overlays aware of mouse focus

This is helpful for plugins that wish to show overlays that perform
actions when they have mouse focus, such as offering left click actions.

While it's possible to track this within the overlay itself it is difficult
to do correctly when considering overlapping overlays and overlays being
removed while they have focus.
This commit is contained in:
swazrgb
2021-06-09 21:12:33 +02:00
parent 6eb876b6f6
commit abf9bec7d2
2 changed files with 34 additions and 0 deletions

View File

@@ -97,6 +97,14 @@ public abstract class Overlay implements LayoutableRenderableEntity
{
}
public void onMouseEnter()
{
}
public void onMouseExit()
{
}
/**
* Called when an overlay is dragged onto this, if dragTargetable is true.
* Return true to consume the mouse event and prevent the other

View File

@@ -107,6 +107,10 @@ public class OverlayRenderer extends MouseAdapter implements KeyListener
private boolean isResizeable;
private OverlayBounds emptySnapCorners, snapCorners;
// focused overlay
private Overlay focusedOverlay;
private Overlay prevFocusedOverlay;
@Inject
private OverlayRenderer(
final Client client,
@@ -174,6 +178,14 @@ public class OverlayRenderer extends MouseAdapter implements KeyListener
{
menuEntries = null;
if (focusedOverlay == null && prevFocusedOverlay != null)
{
prevFocusedOverlay.onMouseExit();
}
prevFocusedOverlay = focusedOverlay;
focusedOverlay = null;
if (client.getGameState() == GameState.LOGGED_IN)
{
@@ -352,6 +364,20 @@ public class OverlayRenderer extends MouseAdapter implements KeyListener
menuEntries = createRightClickMenuEntries(overlay);
}
if (focusedOverlay == null)
{
focusedOverlay = overlay;
if (focusedOverlay != prevFocusedOverlay)
{
if (prevFocusedOverlay != null)
{
prevFocusedOverlay.onMouseExit();
}
overlay.onMouseEnter();
}
}
overlay.onMouseOver();
}
}