Merge pull request #7705 from deathbeam/add-layoutable-bounds

Add .getBounds() to LayoutableRenderableEntity
This commit is contained in:
Tomas Slusny
2019-02-17 13:28:18 +00:00
committed by GitHub
9 changed files with 89 additions and 51 deletions

View File

@@ -28,6 +28,7 @@ import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import lombok.AccessLevel;
import lombok.Getter;
@@ -55,6 +56,9 @@ public class ScreenMarkerRenderable implements LayoutableRenderableEntity
@Setter(AccessLevel.PACKAGE)
private Stroke stroke;
@Getter
private final Rectangle bounds = new Rectangle();
@Override
public Dimension render(Graphics2D graphics)
{
@@ -72,6 +76,7 @@ public class ScreenMarkerRenderable implements LayoutableRenderableEntity
graphics.setColor(color);
graphics.setStroke(stroke);
graphics.drawRect(offset, offset, width - thickness, height - thickness);
bounds.setSize(preferredSize);
return preferredSize;
}
}

View File

@@ -27,7 +27,9 @@ package net.runelite.client.ui.overlay.components;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@@ -36,6 +38,10 @@ import lombok.Setter;
public class ImageComponent implements LayoutableRenderableEntity
{
private final BufferedImage image;
@Getter
private final Rectangle bounds = new Rectangle();
private Point preferredLocation = new Point();
@Override
@@ -47,7 +53,10 @@ public class ImageComponent implements LayoutableRenderableEntity
}
graphics.drawImage(image, preferredLocation.x, preferredLocation.y, null);
return new Dimension(image.getWidth(), image.getHeight());
final Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
bounds.setLocation(preferredLocation);
bounds.setSize(dimension);
return dimension;
}
@Override

View File

@@ -45,11 +45,10 @@ public class InfoBoxComponent implements LayoutableRenderableEntity
private String tooltip;
@Getter
private final Rectangle bounds = new Rectangle();
private Point preferredLocation = new Point();
@Setter
private Dimension preferredSize = new Dimension(DEFAULT_SIZE, DEFAULT_SIZE);
private String text;
private Color color = Color.WHITE;
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
@@ -64,12 +63,14 @@ public class InfoBoxComponent implements LayoutableRenderableEntity
}
graphics.setFont(getSize() < DEFAULT_SIZE ? FontManager.getRunescapeSmallFont() : FontManager.getRunescapeFont());
graphics.translate(preferredLocation.x, preferredLocation.y);
final int baseX = preferredLocation.x;
final int baseY = preferredLocation.y;
// Calculate dimensions
final FontMetrics metrics = graphics.getFontMetrics();
final int size = getSize();
final Rectangle bounds = new Rectangle(size, size);
final Rectangle bounds = new Rectangle(baseX, baseY, size, size);
// Render background
final BackgroundComponent backgroundComponent = new BackgroundComponent();
@@ -80,26 +81,21 @@ public class InfoBoxComponent implements LayoutableRenderableEntity
// Render image
graphics.drawImage(
image,
(size - image.getWidth(null)) / 2,
(size - image.getHeight(null)) / 2,
baseX + (size - image.getWidth(null)) / 2,
baseY + (size - image.getHeight(null)) / 2,
null);
// Render caption
final TextComponent textComponent = new TextComponent();
textComponent.setColor(color);
textComponent.setText(text);
textComponent.setPosition(new Point(((size - metrics.stringWidth(text)) / 2), size - SEPARATOR));
textComponent.setPosition(new Point(baseX + ((size - metrics.stringWidth(text)) / 2), baseY + size - SEPARATOR));
textComponent.render(graphics);
graphics.translate(-preferredLocation.x, -preferredLocation.y);
this.bounds.setBounds(bounds);
return bounds.getSize();
}
public Dimension getPreferredSize()
{
return new Dimension(getSize(), getSize());
}
private int getSize()
{
return Math.max(preferredSize.width, preferredSize.height);

View File

@@ -26,10 +26,12 @@ package net.runelite.client.ui.overlay.components;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import net.runelite.client.ui.overlay.RenderableEntity;
public interface LayoutableRenderableEntity extends RenderableEntity
{
Rectangle getBounds();
void setPreferredLocation(Point position);
void setPreferredSize(Dimension dimension);
}

View File

@@ -31,7 +31,9 @@ import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Setter
@@ -53,17 +55,22 @@ public class LineComponent implements LayoutableRenderableEntity
@Builder.Default
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
@Builder.Default
@Getter
private final Rectangle bounds = new Rectangle();
@Override
public Dimension render(Graphics2D graphics)
{
graphics.translate(preferredLocation.x, preferredLocation.y);
// Prevent NPEs
final String left = MoreObjects.firstNonNull(this.left, "");
final String right = MoreObjects.firstNonNull(this.right, "");
final FontMetrics metrics = graphics.getFontMetrics();
int x = 0;
int y = metrics.getHeight();
final int baseX = preferredLocation.x;
final int baseY = preferredLocation.y + metrics.getHeight();
int x = baseX;
int y = baseY;
final int leftFullWidth = getLineWidth(left, metrics);
final int rightFullWidth = getLineWidth(right, metrics);
@@ -112,8 +119,10 @@ public class LineComponent implements LayoutableRenderableEntity
y += metrics.getHeight();
}
graphics.translate(-preferredLocation.x, -preferredLocation.y);
return new Dimension(preferredSize.width, y - metrics.getHeight());
final Dimension dimension = new Dimension(preferredSize.width, y - baseY);
bounds.setLocation(preferredLocation);
bounds.setSize(dimension);
return dimension;
}
final TextComponent leftLineComponent = new TextComponent();
@@ -129,8 +138,10 @@ public class LineComponent implements LayoutableRenderableEntity
rightLineComponent.render(graphics);
y += metrics.getHeight();
graphics.translate(-preferredLocation.x, -preferredLocation.y);
return new Dimension(preferredSize.width, y - metrics.getHeight());
final Dimension dimension = new Dimension(preferredSize.width, y - baseY);
bounds.setLocation(preferredLocation);
bounds.setSize(dimension);
return dimension;
}
private static int getLineWidth(final String line, final FontMetrics metrics)

View File

@@ -43,9 +43,8 @@ public class PanelComponent implements LayoutableRenderableEntity
VERTICAL;
}
@Setter
@Nullable
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
@Getter
private final Rectangle bounds = new Rectangle();
@Setter
private Point preferredLocation = new Point();
@@ -53,8 +52,12 @@ public class PanelComponent implements LayoutableRenderableEntity
@Setter
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
@Setter
@Nullable
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
@Getter
private List<LayoutableRenderableEntity> children = new ArrayList<>();
private final List<LayoutableRenderableEntity> children = new ArrayList<>();
@Setter
private Orientation orientation = Orientation.VERTICAL;
@@ -82,8 +85,6 @@ public class PanelComponent implements LayoutableRenderableEntity
return null;
}
graphics.translate(preferredLocation.x, preferredLocation.y);
// Calculate panel dimension
final Dimension dimension = new Dimension(
border.x + childDimensions.width + border.width,
@@ -93,14 +94,14 @@ public class PanelComponent implements LayoutableRenderableEntity
if (backgroundColor != null)
{
final BackgroundComponent backgroundComponent = new BackgroundComponent();
backgroundComponent.setRectangle(new Rectangle(dimension));
backgroundComponent.setRectangle(new Rectangle(preferredLocation, dimension));
backgroundComponent.setBackgroundColor(backgroundColor);
backgroundComponent.render(graphics);
}
// Offset children
final int baseX = border.x;
final int baseY = border.y;
final int baseX = preferredLocation.x + border.x;
final int baseY = preferredLocation.y + border.y;
int width = 0;
int height = 0;
int x = baseX;
@@ -174,7 +175,9 @@ public class PanelComponent implements LayoutableRenderableEntity
// Cache children bounds
childDimensions.setSize(totalWidth, totalHeight);
graphics.translate(-preferredLocation.x, -preferredLocation.y);
// Cache bounds
bounds.setLocation(preferredLocation);
bounds.setSize(dimension);
return dimension;
}
}

View File

@@ -29,7 +29,9 @@ import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.text.DecimalFormat;
import lombok.Getter;
import lombok.Setter;
@Setter
@@ -43,6 +45,7 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.0");
private static final DecimalFormat DECIMAL_FORMAT_ABS = new DecimalFormat("#0");
private long minimum;
private long maximum = 100;
private double value;
@@ -53,14 +56,16 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
private Point preferredLocation = new Point();
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 16);
@Getter
private final Rectangle bounds = new Rectangle();
@Override
public Dimension render(Graphics2D graphics)
{
graphics.translate(preferredLocation.x, preferredLocation.y);
final FontMetrics metrics = graphics.getFontMetrics();
final int barX = 0;
final int barY = 0;
final int barX = preferredLocation.x;
final int barY = preferredLocation.y;
final long span = maximum - minimum;
final double currentValue = value - minimum;
@@ -82,7 +87,7 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
final int progressTextY = barY + ((height - metrics.getHeight()) / 2) + metrics.getHeight();
final int progressFill = (int) (width * Math.min(1, pc));
//Draw bar
// Draw bar
graphics.setColor(backgroundColor);
graphics.fillRect(barX, barY, width, height);
graphics.setColor(foregroundColor);
@@ -94,7 +99,9 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
textComponent.setText(textToWrite);
textComponent.render(graphics);
graphics.translate(-preferredLocation.x, -preferredLocation.y);
return new Dimension(width, height);
final Dimension dimension = new Dimension(width, height);
bounds.setLocation(preferredLocation);
bounds.setSize(dimension);
return dimension;
}
}

View File

@@ -29,7 +29,9 @@ import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Setter
@@ -47,17 +49,26 @@ public class TitleComponent implements LayoutableRenderableEntity
@Builder.Default
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
@Builder.Default
@Getter
private final Rectangle bounds = new Rectangle();
@Override
public Dimension render(Graphics2D graphics)
{
graphics.translate(preferredLocation.x, preferredLocation.y);
final int baseX = preferredLocation.x;
final int baseY = preferredLocation.y;
final FontMetrics metrics = graphics.getFontMetrics();
final TextComponent titleComponent = new TextComponent();
titleComponent.setText(text);
titleComponent.setColor(color);
titleComponent.setPosition(new Point((preferredSize.width - metrics.stringWidth(text)) / 2, metrics.getHeight()));
final Dimension dimension = titleComponent.render(graphics);
graphics.translate(-preferredLocation.x, -preferredLocation.y);
return new Dimension(preferredSize.width, dimension.height);
titleComponent.setPosition(new Point(
baseX + ((preferredSize.width - metrics.stringWidth(text)) / 2),
baseY + metrics.getHeight()));
final Dimension rendered = titleComponent.render(graphics);
final Dimension dimension = new Dimension(preferredSize.width, rendered.height);
bounds.setLocation(preferredLocation);
bounds.setSize(dimension);
return dimension;
}
}

View File

@@ -37,7 +37,6 @@ import net.runelite.api.Client;
import net.runelite.client.config.RuneLiteConfig;
import net.runelite.client.ui.overlay.Overlay;
import net.runelite.client.ui.overlay.OverlayPosition;
import net.runelite.client.ui.overlay.OverlayUtil;
import net.runelite.client.ui.overlay.components.InfoBoxComponent;
import net.runelite.client.ui.overlay.components.LayoutableRenderableEntity;
import net.runelite.client.ui.overlay.components.PanelComponent;
@@ -116,15 +115,10 @@ public class InfoBoxOverlay extends Overlay
if (!Strings.isNullOrEmpty(component.getTooltip()))
{
final Rectangle intersectionRectangle = new Rectangle(component.getPreferredLocation(), component.getPreferredSize());
// Move the intersection based on overlay position
// Create intersection rectangle
final Rectangle intersectionRectangle = new Rectangle(component.getBounds());
intersectionRectangle.translate(getBounds().x, getBounds().y);
// Move the intersection based on overlay "orientation"
final Point transformed = OverlayUtil.transformPosition(getPosition(), intersectionRectangle.getSize());
intersectionRectangle.translate(transformed.x, transformed.y);
if (intersectionRectangle.contains(mouse))
{
tooltipManager.add(new Tooltip(component.getTooltip()));