Add support for preferred location to Panel
- Add support for storing preferredLocation in PanelComponent and translate children based on it. - Offset children in PanelComponent by metrics.getHeight by default - Add null check for background in PanelComponent to support panels without background Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
@@ -27,6 +27,7 @@ package net.runelite.client.plugins.screenmarkers;
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.Stroke;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
@@ -36,6 +37,10 @@ import net.runelite.client.ui.overlay.components.LayoutableRenderableEntity;
|
||||
public class ScreenMarkerRenderable implements LayoutableRenderableEntity
|
||||
{
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
@Setter
|
||||
private Point preferredLocation;
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
@Setter
|
||||
private Dimension preferredSize;
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
@Setter(AccessLevel.PACKAGE)
|
||||
@@ -69,10 +74,4 @@ public class ScreenMarkerRenderable implements LayoutableRenderableEntity
|
||||
graphics.drawRect(offset, offset, width - thickness, height - thickness);
|
||||
return preferredSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPreferredSize(Dimension preferredSize)
|
||||
{
|
||||
this.preferredSize = preferredSize;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,7 +231,7 @@ public class XpGlobesOverlay extends Overlay
|
||||
String skillCurrentXp = decimalFormat.format(mouseOverSkill.getCurrentXp());
|
||||
|
||||
xpTooltip.getChildren().clear();
|
||||
graphics.translate(x, y);
|
||||
xpTooltip.setPreferredLocation(new java.awt.Point(x, y));
|
||||
xpTooltip.setPreferredSize(new Dimension(TOOLTIP_RECT_SIZE_X, 0));
|
||||
|
||||
xpTooltip.getChildren().add(LineComponent.builder()
|
||||
@@ -286,6 +286,5 @@ public class XpGlobesOverlay extends Overlay
|
||||
}
|
||||
|
||||
xpTooltip.render(graphics);
|
||||
graphics.translate(-x, -y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,15 +26,17 @@ package net.runelite.client.ui.overlay.components;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.image.BufferedImage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
@Setter
|
||||
public class ImageComponent implements LayoutableRenderableEntity
|
||||
{
|
||||
private BufferedImage image;
|
||||
private final BufferedImage image;
|
||||
private Point preferredLocation = new Point();
|
||||
|
||||
@Override
|
||||
public Dimension render(Graphics2D graphics)
|
||||
@@ -44,7 +46,7 @@ public class ImageComponent implements LayoutableRenderableEntity
|
||||
return null;
|
||||
}
|
||||
|
||||
graphics.drawImage(image, 0, -graphics.getFontMetrics().getHeight(), null);
|
||||
graphics.drawImage(image, preferredLocation.x, preferredLocation.y, null);
|
||||
return new Dimension(image.getWidth(), image.getHeight());
|
||||
}
|
||||
|
||||
|
||||
@@ -25,9 +25,11 @@
|
||||
package net.runelite.client.ui.overlay.components;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Point;
|
||||
import net.runelite.client.ui.overlay.RenderableEntity;
|
||||
|
||||
public interface LayoutableRenderableEntity extends RenderableEntity
|
||||
{
|
||||
void setPreferredLocation(Point position);
|
||||
void setPreferredSize(Dimension dimension);
|
||||
}
|
||||
|
||||
@@ -47,19 +47,23 @@ public class LineComponent implements LayoutableRenderableEntity
|
||||
@Builder.Default
|
||||
private Color rightColor = Color.WHITE;
|
||||
|
||||
@Builder.Default
|
||||
private Point preferredLocation = new Point();
|
||||
|
||||
@Builder.Default
|
||||
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
|
||||
|
||||
@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 = 0;
|
||||
int y = metrics.getHeight();
|
||||
final int leftFullWidth = getLineWidth(left, metrics);
|
||||
final int rightFullWidth = getLineWidth(right, metrics);
|
||||
|
||||
@@ -108,7 +112,8 @@ public class LineComponent implements LayoutableRenderableEntity
|
||||
y += metrics.getHeight();
|
||||
}
|
||||
|
||||
return new Dimension(preferredSize.width, y);
|
||||
graphics.translate(-preferredLocation.x, -preferredLocation.y);
|
||||
return new Dimension(preferredSize.width, y - metrics.getHeight());
|
||||
}
|
||||
|
||||
final TextComponent leftLineComponent = new TextComponent();
|
||||
@@ -124,7 +129,8 @@ public class LineComponent implements LayoutableRenderableEntity
|
||||
rightLineComponent.render(graphics);
|
||||
y += metrics.getHeight();
|
||||
|
||||
return new Dimension(preferredSize.width, y);
|
||||
graphics.translate(-preferredLocation.x, -preferredLocation.y);
|
||||
return new Dimension(preferredSize.width, y - metrics.getHeight());
|
||||
}
|
||||
|
||||
private static int getLineWidth(final String line, final FontMetrics metrics)
|
||||
|
||||
@@ -26,12 +26,12 @@ package net.runelite.client.ui.overlay.components;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.annotation.Nullable;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@@ -44,8 +44,12 @@ public class PanelComponent implements LayoutableRenderableEntity
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Nullable
|
||||
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
|
||||
|
||||
@Setter
|
||||
private Point preferredLocation = new Point();
|
||||
|
||||
@Setter
|
||||
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
|
||||
|
||||
@@ -78,21 +82,25 @@ public class PanelComponent implements LayoutableRenderableEntity
|
||||
return null;
|
||||
}
|
||||
|
||||
final FontMetrics metrics = graphics.getFontMetrics();
|
||||
graphics.translate(preferredLocation.x, preferredLocation.y);
|
||||
|
||||
// Render background
|
||||
// Calculate panel dimension
|
||||
final Dimension dimension = new Dimension(
|
||||
border.x + childDimensions.width + border.width,
|
||||
border.y + childDimensions.height + border.height);
|
||||
|
||||
final BackgroundComponent backgroundComponent = new BackgroundComponent();
|
||||
backgroundComponent.setRectangle(new Rectangle(dimension));
|
||||
backgroundComponent.setBackgroundColor(backgroundColor);
|
||||
backgroundComponent.render(graphics);
|
||||
// Render background
|
||||
if (backgroundColor != null)
|
||||
{
|
||||
final BackgroundComponent backgroundComponent = new BackgroundComponent();
|
||||
backgroundComponent.setRectangle(new Rectangle(dimension));
|
||||
backgroundComponent.setBackgroundColor(backgroundColor);
|
||||
backgroundComponent.render(graphics);
|
||||
}
|
||||
|
||||
// Offset children
|
||||
final int baseX = border.x;
|
||||
final int baseY = border.y + metrics.getHeight();
|
||||
final int baseY = border.y;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int x = baseX;
|
||||
@@ -111,10 +119,9 @@ public class PanelComponent implements LayoutableRenderableEntity
|
||||
for (int i = 0; i < children.size(); i ++)
|
||||
{
|
||||
final LayoutableRenderableEntity child = children.get(i);
|
||||
child.setPreferredLocation(new Point(x, y));
|
||||
child.setPreferredSize(childPreferredSize);
|
||||
graphics.translate(x, y);
|
||||
final Dimension childDimension = child.render(graphics);
|
||||
graphics.translate(-x, -y);
|
||||
|
||||
switch (orientation)
|
||||
{
|
||||
@@ -167,6 +174,7 @@ public class PanelComponent implements LayoutableRenderableEntity
|
||||
// Cache children bounds
|
||||
childDimensions.setSize(totalWidth, totalHeight);
|
||||
|
||||
graphics.translate(-preferredLocation.x, -preferredLocation.y);
|
||||
return dimension;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,15 +50,17 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
|
||||
private Color foregroundColor = new Color(82, 161, 82);
|
||||
private Color backgroundColor = new Color(255, 255, 255, 127);
|
||||
private Color fontColor = Color.WHITE;
|
||||
private Point preferredLocation = new Point();
|
||||
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 16);
|
||||
|
||||
@Override
|
||||
public Dimension render(Graphics2D graphics)
|
||||
{
|
||||
graphics.translate(preferredLocation.x, preferredLocation.y);
|
||||
final FontMetrics metrics = graphics.getFontMetrics();
|
||||
|
||||
final int barX = 0;
|
||||
final int barY = -metrics.getHeight();
|
||||
final int barY = 0;
|
||||
|
||||
final long span = maximum - minimum;
|
||||
final double currentValue = value - minimum;
|
||||
@@ -92,6 +94,7 @@ public class ProgressBarComponent implements LayoutableRenderableEntity
|
||||
textComponent.setText(textToWrite);
|
||||
textComponent.render(graphics);
|
||||
|
||||
graphics.translate(-preferredLocation.x, -preferredLocation.y);
|
||||
return new Dimension(width, height);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,18 +41,23 @@ public class TitleComponent implements LayoutableRenderableEntity
|
||||
@Builder.Default
|
||||
private Color color = Color.WHITE;
|
||||
|
||||
@Builder.Default
|
||||
private Point preferredLocation = new Point();
|
||||
|
||||
@Builder.Default
|
||||
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
|
||||
|
||||
@Override
|
||||
public Dimension render(Graphics2D graphics)
|
||||
{
|
||||
graphics.translate(preferredLocation.x, 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, 0));
|
||||
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(Math.min(preferredSize.width, dimension.width), dimension.height);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user