Change InfoBoxOverlay to use PanelComponent
Instead of doing layouting by itself, use PanelComponent and newly added support for component wrapping and disabling of background. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
@@ -32,26 +32,34 @@ import java.awt.Point;
|
|||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import net.runelite.client.ui.overlay.RenderableEntity;
|
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
public class InfoBoxComponent implements RenderableEntity
|
public class InfoBoxComponent implements LayoutableRenderableEntity
|
||||||
{
|
{
|
||||||
private static final int BOX_SIZE = 35;
|
private static final int BOX_SIZE = 35;
|
||||||
private static final int SEPARATOR = 2;
|
private static final int SEPARATOR = 2;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private String tooltip;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private Point preferredLocation = new Point();
|
||||||
|
|
||||||
private String text;
|
private String text;
|
||||||
private Color color = Color.WHITE;
|
private Color color = Color.WHITE;
|
||||||
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
|
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
|
||||||
private Point position = new Point();
|
|
||||||
private BufferedImage image;
|
private BufferedImage image;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dimension render(Graphics2D graphics)
|
public Dimension render(Graphics2D graphics)
|
||||||
{
|
{
|
||||||
|
graphics.translate(preferredLocation.x, preferredLocation.y);
|
||||||
final FontMetrics metrics = graphics.getFontMetrics();
|
final FontMetrics metrics = graphics.getFontMetrics();
|
||||||
final Rectangle bounds = new Rectangle(position.x, position.y, BOX_SIZE, BOX_SIZE);
|
final int w = BOX_SIZE;
|
||||||
|
final int h = BOX_SIZE;
|
||||||
|
final Rectangle bounds = new Rectangle(w, h);
|
||||||
final BackgroundComponent backgroundComponent = new BackgroundComponent();
|
final BackgroundComponent backgroundComponent = new BackgroundComponent();
|
||||||
backgroundComponent.setBackgroundColor(backgroundColor);
|
backgroundComponent.setBackgroundColor(backgroundColor);
|
||||||
backgroundComponent.setRectangle(bounds);
|
backgroundComponent.setRectangle(bounds);
|
||||||
@@ -59,18 +67,30 @@ public class InfoBoxComponent implements RenderableEntity
|
|||||||
|
|
||||||
if (Objects.nonNull(image))
|
if (Objects.nonNull(image))
|
||||||
{
|
{
|
||||||
graphics.drawImage(image,
|
graphics.drawImage(
|
||||||
position.x + (BOX_SIZE - image.getWidth()) / 2,
|
image,
|
||||||
position.y + (BOX_SIZE - image.getHeight()) / 2, null);
|
(w - image.getWidth()) / 2,
|
||||||
|
(h - image.getHeight()) / 2,
|
||||||
|
null);
|
||||||
}
|
}
|
||||||
|
|
||||||
final TextComponent textComponent = new TextComponent();
|
final TextComponent textComponent = new TextComponent();
|
||||||
textComponent.setColor(color);
|
textComponent.setColor(color);
|
||||||
textComponent.setText(text);
|
textComponent.setText(text);
|
||||||
textComponent.setPosition(new Point(
|
textComponent.setPosition(new Point(((w - metrics.stringWidth(text)) / 2), h - SEPARATOR));
|
||||||
position.x + ((BOX_SIZE - metrics.stringWidth(text)) / 2),
|
|
||||||
position.y + BOX_SIZE - SEPARATOR));
|
|
||||||
textComponent.render(graphics);
|
textComponent.render(graphics);
|
||||||
|
graphics.translate(-preferredLocation.x, -preferredLocation.y);
|
||||||
|
return bounds.getSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dimension getPreferredSize()
|
||||||
|
{
|
||||||
return new Dimension(BOX_SIZE, BOX_SIZE);
|
return new Dimension(BOX_SIZE, BOX_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setPreferredSize(Dimension dimension)
|
||||||
|
{
|
||||||
|
// Just use infobox dimensions for now
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,15 +39,14 @@ import net.runelite.client.ui.overlay.Overlay;
|
|||||||
import net.runelite.client.ui.overlay.OverlayPosition;
|
import net.runelite.client.ui.overlay.OverlayPosition;
|
||||||
import net.runelite.client.ui.overlay.OverlayUtil;
|
import net.runelite.client.ui.overlay.OverlayUtil;
|
||||||
import net.runelite.client.ui.overlay.components.InfoBoxComponent;
|
import net.runelite.client.ui.overlay.components.InfoBoxComponent;
|
||||||
|
import net.runelite.client.ui.overlay.components.LayoutableRenderableEntity;
|
||||||
|
import net.runelite.client.ui.overlay.components.PanelComponent;
|
||||||
import net.runelite.client.ui.overlay.tooltip.Tooltip;
|
import net.runelite.client.ui.overlay.tooltip.Tooltip;
|
||||||
import net.runelite.client.ui.overlay.tooltip.TooltipManager;
|
import net.runelite.client.ui.overlay.tooltip.TooltipManager;
|
||||||
|
|
||||||
public class InfoBoxOverlay extends Overlay
|
public class InfoBoxOverlay extends Overlay
|
||||||
{
|
{
|
||||||
private static final int BOXSIZE = 35;
|
private final PanelComponent panelComponent = new PanelComponent();
|
||||||
private static final int SEPARATOR = 2;
|
|
||||||
private static final int TOTAL_BOXSIZE = BOXSIZE + SEPARATOR;
|
|
||||||
|
|
||||||
private final InfoBoxManager infoboxManager;
|
private final InfoBoxManager infoboxManager;
|
||||||
private final TooltipManager tooltipManager;
|
private final TooltipManager tooltipManager;
|
||||||
private final Provider<Client> clientProvider;
|
private final Provider<Client> clientProvider;
|
||||||
@@ -65,108 +64,72 @@ public class InfoBoxOverlay extends Overlay
|
|||||||
this.clientProvider = clientProvider;
|
this.clientProvider = clientProvider;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
setPosition(OverlayPosition.TOP_LEFT);
|
setPosition(OverlayPosition.TOP_LEFT);
|
||||||
|
|
||||||
|
panelComponent.setBackgroundColor(null);
|
||||||
|
panelComponent.setBorder(new Rectangle());
|
||||||
|
panelComponent.setGap(new Point(2, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dimension render(Graphics2D graphics)
|
public Dimension render(Graphics2D graphics)
|
||||||
{
|
{
|
||||||
List<InfoBox> infoBoxes = infoboxManager.getInfoBoxes();
|
final List<InfoBox> infoBoxes = infoboxManager.getInfoBoxes();
|
||||||
|
|
||||||
if (infoBoxes.isEmpty())
|
if (infoBoxes.isEmpty())
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wrap = config.infoBoxWrap();
|
panelComponent.getChildren().clear();
|
||||||
int infoBoxCount = infoBoxes.size();
|
panelComponent.setWrapping(config.infoBoxWrap());
|
||||||
boolean vertical = config.infoBoxVertical();
|
panelComponent.setOrientation(config.infoBoxVertical()
|
||||||
|
? PanelComponent.Orientation.VERTICAL
|
||||||
|
: PanelComponent.Orientation.HORIZONTAL);
|
||||||
|
|
||||||
int width, height;
|
infoBoxes.forEach(box ->
|
||||||
if (!vertical)
|
|
||||||
{
|
{
|
||||||
width = getWidth(infoBoxCount, wrap);
|
|
||||||
height = getHeight(infoBoxCount, wrap);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
width = getHeight(infoBoxCount, wrap);
|
|
||||||
height = getWidth(infoBoxCount, wrap);
|
|
||||||
}
|
|
||||||
|
|
||||||
int x = 0;
|
|
||||||
int y = 0;
|
|
||||||
|
|
||||||
for (InfoBox box : infoBoxes)
|
|
||||||
{
|
|
||||||
if (!box.render())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
final InfoBoxComponent infoBoxComponent = new InfoBoxComponent();
|
final InfoBoxComponent infoBoxComponent = new InfoBoxComponent();
|
||||||
infoBoxComponent.setColor(box.getTextColor());
|
infoBoxComponent.setColor(box.getTextColor());
|
||||||
infoBoxComponent.setImage(box.getImage());
|
infoBoxComponent.setImage(box.getImage());
|
||||||
infoBoxComponent.setText(box.getText());
|
infoBoxComponent.setText(box.getText());
|
||||||
infoBoxComponent.setPosition(new Point(x, y));
|
infoBoxComponent.setTooltip(box.getTooltip());
|
||||||
final Dimension infoBoxBounds = infoBoxComponent.render(graphics);
|
panelComponent.getChildren().add(infoBoxComponent);
|
||||||
|
});
|
||||||
|
|
||||||
if (!Strings.isNullOrEmpty(box.getTooltip()))
|
final Dimension dimension = panelComponent.render(graphics);
|
||||||
|
final Client client = clientProvider.get();
|
||||||
|
|
||||||
|
// Handle tooltips
|
||||||
|
if (client != null)
|
||||||
|
{
|
||||||
|
final Point mouse = new Point(client.getMouseCanvasPosition().getX(), client.getMouseCanvasPosition().getY());
|
||||||
|
|
||||||
|
for (final LayoutableRenderableEntity child : panelComponent.getChildren())
|
||||||
{
|
{
|
||||||
final Rectangle intersectionRectangle = new Rectangle(infoBoxBounds);
|
if (child instanceof InfoBoxComponent)
|
||||||
intersectionRectangle.setLocation(getBounds().getLocation());
|
{
|
||||||
intersectionRectangle.translate(x, y);
|
final InfoBoxComponent component = (InfoBoxComponent) child;
|
||||||
final Point transformed = OverlayUtil.transformPosition(getPosition(), intersectionRectangle.getSize());
|
|
||||||
intersectionRectangle.translate(transformed.x, transformed.y);
|
|
||||||
|
|
||||||
final Client client = clientProvider.get();
|
if (!Strings.isNullOrEmpty(component.getTooltip()))
|
||||||
|
{
|
||||||
|
final Rectangle intersectionRectangle = new Rectangle(component.getPreferredLocation(), component.getPreferredSize());
|
||||||
|
|
||||||
if (client != null && intersectionRectangle.contains(new Point(client.getMouseCanvasPosition().getX(),
|
// Move the intersection based on overlay position
|
||||||
client.getMouseCanvasPosition().getY())))
|
intersectionRectangle.translate(getBounds().x, getBounds().y);
|
||||||
{
|
|
||||||
tooltipManager.add(new Tooltip(box.getTooltip()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine which axis to reset/increase
|
// Move the intersection based on overlay "orientation"
|
||||||
if (vertical)
|
final Point transformed = OverlayUtil.transformPosition(getPosition(), intersectionRectangle.getSize());
|
||||||
{
|
intersectionRectangle.translate(transformed.x, transformed.y);
|
||||||
// Reset y if newbox reaches height limit
|
|
||||||
if (y + TOTAL_BOXSIZE < height)
|
if (intersectionRectangle.contains(mouse))
|
||||||
{
|
{
|
||||||
y += TOTAL_BOXSIZE;
|
tooltipManager.add(new Tooltip(component.getTooltip()));
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
|
||||||
y = 0;
|
|
||||||
x += TOTAL_BOXSIZE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Reset x if newbox reaches width limit
|
|
||||||
if (x + TOTAL_BOXSIZE < width)
|
|
||||||
{
|
|
||||||
x += TOTAL_BOXSIZE;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
x = 0;
|
|
||||||
y += TOTAL_BOXSIZE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Dimension(width, height);
|
return dimension;
|
||||||
}
|
|
||||||
|
|
||||||
private static int getHeight(int infoBoxCount, int maxRow)
|
|
||||||
{
|
|
||||||
return maxRow == 0 ? TOTAL_BOXSIZE : (int) Math.ceil((double)infoBoxCount / maxRow) * TOTAL_BOXSIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int getWidth(int infoBoxCount, int maxRow)
|
|
||||||
{
|
|
||||||
return maxRow == 0 ? infoBoxCount * TOTAL_BOXSIZE : (maxRow > infoBoxCount ? infoBoxCount : maxRow) * TOTAL_BOXSIZE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user