Merge pull request #959 from deathbeam/tooltip-right
Move tooltip to the right of the mouse
This commit is contained in:
@@ -83,17 +83,6 @@ public class TooltipComponent implements RenderableEntity
|
|||||||
// Tooltip position
|
// Tooltip position
|
||||||
int x = position.x;
|
int x = position.x;
|
||||||
int y = position.y;
|
int y = position.y;
|
||||||
x = x - tooltipWidth - OFFSET * 2;
|
|
||||||
if (x < 0)
|
|
||||||
{
|
|
||||||
x = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
y = y - tooltipHeight - OFFSET * 2;
|
|
||||||
if (y < 0)
|
|
||||||
{
|
|
||||||
y = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render tooltip - background
|
// Render tooltip - background
|
||||||
final Rectangle tooltipBackground = new Rectangle(x, y,
|
final Rectangle tooltipBackground = new Rectangle(x, y,
|
||||||
|
|||||||
@@ -24,17 +24,12 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.client.ui.overlay.tooltip;
|
package net.runelite.client.ui.overlay.tooltip;
|
||||||
|
|
||||||
import java.awt.Point;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class Tooltip
|
public class Tooltip
|
||||||
{
|
{
|
||||||
private final String text;
|
private final String text;
|
||||||
private boolean followMouse = true;
|
|
||||||
private Point position = new Point();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ import java.awt.Rectangle;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.runelite.api.Client;
|
import net.runelite.api.Client;
|
||||||
import net.runelite.client.ui.overlay.Overlay;
|
import net.runelite.client.ui.overlay.Overlay;
|
||||||
import net.runelite.client.ui.overlay.OverlayLayer;
|
import net.runelite.client.ui.overlay.OverlayLayer;
|
||||||
@@ -38,20 +40,23 @@ import net.runelite.client.ui.overlay.OverlayPosition;
|
|||||||
import net.runelite.client.ui.overlay.OverlayPriority;
|
import net.runelite.client.ui.overlay.OverlayPriority;
|
||||||
import net.runelite.client.ui.overlay.components.TooltipComponent;
|
import net.runelite.client.ui.overlay.components.TooltipComponent;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
@Slf4j
|
||||||
public class TooltipOverlay extends Overlay
|
public class TooltipOverlay extends Overlay
|
||||||
{
|
{
|
||||||
|
private static final int OFFSET = 24;
|
||||||
private static final int PADDING = 2;
|
private static final int PADDING = 2;
|
||||||
private final TooltipManager tooltipManager;
|
private final TooltipManager tooltipManager;
|
||||||
private final Provider<Client> clientProvider;
|
private final Provider<Client> clientProvider;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public TooltipOverlay(TooltipManager tooltipManager, Provider<Client> clientProvider)
|
public TooltipOverlay(Provider<Client> clientProvider, TooltipManager tooltipManager)
|
||||||
{
|
{
|
||||||
|
this.clientProvider = clientProvider;
|
||||||
|
this.tooltipManager = tooltipManager;
|
||||||
setPosition(OverlayPosition.TOOLTIP);
|
setPosition(OverlayPosition.TOOLTIP);
|
||||||
setPriority(OverlayPriority.HIGH);
|
setPriority(OverlayPriority.HIGH);
|
||||||
setLayer(OverlayLayer.ALWAYS_ON_TOP);
|
setLayer(OverlayLayer.ALWAYS_ON_TOP);
|
||||||
this.tooltipManager = tooltipManager;
|
|
||||||
this.clientProvider = clientProvider;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -64,53 +69,55 @@ public class TooltipOverlay extends Overlay
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rectangle lastLocation = null;
|
final Client client = clientProvider.get();
|
||||||
|
final Rectangle clientCanvasBounds = new Rectangle(client.getRealDimensions());
|
||||||
|
final net.runelite.api.Point mouseCanvasPosition = client.getMouseCanvasPosition();
|
||||||
|
final Point mousePosition = new Point(mouseCanvasPosition.getX(), mouseCanvasPosition.getY() + OFFSET);
|
||||||
|
final Rectangle bounds = new Rectangle(getBounds());
|
||||||
|
bounds.setLocation(mousePosition);
|
||||||
|
|
||||||
|
if (!clientCanvasBounds.contains(bounds))
|
||||||
|
{
|
||||||
|
final int clientX = (int) clientCanvasBounds.getMaxX();
|
||||||
|
final int clientY = (int) clientCanvasBounds.getMaxY();
|
||||||
|
final int boundsX = (int) bounds.getMaxX();
|
||||||
|
final int boundsY = (int) bounds.getMaxY();
|
||||||
|
|
||||||
|
if (boundsY > clientY)
|
||||||
|
{
|
||||||
|
graphics.translate(0, -bounds.height - OFFSET);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (boundsX > clientX)
|
||||||
|
{
|
||||||
|
graphics.translate(-bounds.width, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final Rectangle newBounds = new Rectangle(-1, -1, 0, 0);
|
||||||
|
|
||||||
for (Tooltip tooltip : tooltips)
|
for (Tooltip tooltip : tooltips)
|
||||||
{
|
{
|
||||||
final Client client = clientProvider.get();
|
|
||||||
final TooltipComponent tooltipComponent = new TooltipComponent();
|
final TooltipComponent tooltipComponent = new TooltipComponent();
|
||||||
tooltipComponent.setModIcons(client.getModIcons());
|
tooltipComponent.setModIcons(client.getModIcons());
|
||||||
tooltipComponent.setText(tooltip.getText());
|
tooltipComponent.setText(tooltip.getText());
|
||||||
|
|
||||||
final Point position = new Point();
|
if (newBounds.contains(mousePosition))
|
||||||
|
|
||||||
if (tooltip.isFollowMouse())
|
|
||||||
{
|
{
|
||||||
final net.runelite.api.Point mouseCanvasPosition = client != null
|
mousePosition.move(mouseCanvasPosition.getX(), mouseCanvasPosition.getY() + OFFSET + newBounds.height);
|
||||||
? client.getMouseCanvasPosition()
|
|
||||||
: new net.runelite.api.Point(0, 0);
|
|
||||||
|
|
||||||
position.setLocation(mouseCanvasPosition.getX(), mouseCanvasPosition.getY());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
position.setLocation(tooltip.getPosition());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if this tooltip would overlap the last
|
tooltipComponent.setPosition(mousePosition);
|
||||||
if (lastLocation != null && lastLocation.contains(position))
|
final Dimension dimension = tooltipComponent.render(graphics);
|
||||||
{
|
|
||||||
// shift tooltip above previous
|
|
||||||
position.translate(0, -lastLocation.height - PADDING);
|
|
||||||
}
|
|
||||||
|
|
||||||
// render tooltip
|
// Create incremental tooltip newBounds
|
||||||
tooltipComponent.setPosition(position);
|
newBounds.x = newBounds.x != -1 ? Math.min(newBounds.x, mousePosition.x) : mousePosition.x;
|
||||||
final Dimension thisSize = tooltipComponent.render(graphics);
|
newBounds.y = newBounds.y != -1 ? Math.min(newBounds.y, mousePosition.y) : mousePosition.y;
|
||||||
|
newBounds.height += dimension.height + PADDING;
|
||||||
// update tooltip bounding rect
|
newBounds.width = Math.max(newBounds.width, dimension.width);
|
||||||
if (lastLocation == null)
|
|
||||||
{
|
|
||||||
lastLocation = new Rectangle(position, thisSize);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lastLocation.setSize(new Dimension(Math.max(lastLocation.width, thisSize.width), lastLocation.height + thisSize.height + PADDING));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tooltipManager.clear();
|
tooltipManager.clear();
|
||||||
return null;
|
return newBounds.getSize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user