Simplify component system

- Properly use/not use paddings
- Remove title property and extract it to separate component
- Remove position property, let user handle the positioning
- Extract common constants to ComponentConstants
- Use lombok.Setter on classes instead of fields

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-05-07 13:58:33 +02:00
parent dcfbfd8441
commit f8fb6b3543
10 changed files with 134 additions and 121 deletions

View File

@@ -35,10 +35,9 @@ import net.runelite.client.ui.overlay.RenderableEntity;
@NoArgsConstructor
@AllArgsConstructor
@Setter
public class BackgroundComponent implements RenderableEntity
{
public static final Color DEFAULT_BACKGROUND_COLOR = new Color(70, 61, 50, 156);
private static final int BORDER_OFFSET = 2;
private static final int OUTSIDE_STROKE_RED_OFFSET = 14;
@@ -51,13 +50,8 @@ public class BackgroundComponent implements RenderableEntity
private static final int INSIDE_STROKE_BLUE_OFFSET = 19;
private static final int INSIDE_STROKE_ALPHA = 255;
@Setter
private Color backgroundColor = DEFAULT_BACKGROUND_COLOR;
@Setter
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
private Rectangle rectangle = new Rectangle();
@Setter
private boolean fill = true;
@Override

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2018, Tomas Slusny <slusnucky@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.ui.overlay.components;
import java.awt.Color;
public class ComponentConstants
{
public static final int STANDARD_PADDING = 4;
public static final int STANDARD_WIDTH = 129;
public static final Color STANDARD_BACKGROUND_COLOR = new Color(70, 61, 50, 156);
}

View File

@@ -32,29 +32,19 @@ import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Objects;
import javax.annotation.Nullable;
import lombok.Setter;
import net.runelite.client.ui.overlay.RenderableEntity;
@Setter
public class InfoBoxComponent implements RenderableEntity
{
private static final int BOX_SIZE = 35;
private static final int SEPARATOR = 2;
@Setter
private String text;
@Setter
private Color color = Color.WHITE;
@Setter
private Color backgroundColor = BackgroundComponent.DEFAULT_BACKGROUND_COLOR;
@Setter
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
private Point position = new Point();
@Setter
@Nullable
private BufferedImage image;
@Override

View File

@@ -38,8 +38,6 @@ import lombok.Setter;
@Builder
public class LineComponent implements LayoutableRenderableEntity
{
private static final int SEPARATOR = 1;
private String left;
private String right;
@@ -50,7 +48,7 @@ public class LineComponent implements LayoutableRenderableEntity
private Color rightColor = Color.WHITE;
@Builder.Default
private Dimension preferredSize = new Dimension();
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
@Override
public Dimension render(Graphics2D graphics)
@@ -107,7 +105,7 @@ public class LineComponent implements LayoutableRenderableEntity
rightLineComponent.setText(rightText);
rightLineComponent.setColor(rightColor);
rightLineComponent.render(graphics);
y += metrics.getHeight() + SEPARATOR;
y += metrics.getHeight();
}
return new Dimension(preferredSize.width, y);
@@ -124,7 +122,7 @@ public class LineComponent implements LayoutableRenderableEntity
rightLineComponent.setText(right);
rightLineComponent.setColor(rightColor);
rightLineComponent.render(graphics);
y += metrics.getHeight() + SEPARATOR;
y += metrics.getHeight();
return new Dimension(preferredSize.width, y);
}

View File

@@ -24,12 +24,10 @@
*/
package net.runelite.client.ui.overlay.components;
import com.google.common.base.Strings;
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;
@@ -44,26 +42,16 @@ public class PanelComponent implements LayoutableRenderableEntity
VERTICAL;
}
private static final int TOP_BORDER = 4;
private static final int LEFT_BORDER = 4;
private static final int RIGHT_BORDER = 4;
private static final int BOTTOM_BORDER = 4;
private static final int SEPARATOR = 1;
private static final int TOP_BORDER = ComponentConstants.STANDARD_PADDING;
private static final int LEFT_BORDER = ComponentConstants.STANDARD_PADDING;
private static final int RIGHT_BORDER = ComponentConstants.STANDARD_PADDING;
private static final int BOTTOM_BORDER = ComponentConstants.STANDARD_PADDING;
@Setter
private String title;
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
@Setter
private Color titleColor = Color.WHITE;
@Setter
private Color backgroundColor = BackgroundComponent.DEFAULT_BACKGROUND_COLOR;
@Setter
private Point position = new Point();
@Setter
private Dimension preferredSize = new Dimension(129, 0);
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
@Getter
private List<LayoutableRenderableEntity> children = new ArrayList<>();
@@ -71,55 +59,42 @@ public class PanelComponent implements LayoutableRenderableEntity
@Setter
private Orientation orientation = Orientation.VERTICAL;
private final Dimension savedChildrenSize = new Dimension();
private final Dimension childDimensions = new Dimension();
@Override
public Dimension render(Graphics2D graphics)
{
if (Strings.isNullOrEmpty(title) && children.isEmpty())
if (children.isEmpty())
{
return null;
}
final FontMetrics metrics = graphics.getFontMetrics();
// Calculate panel dimensions
int width = preferredSize.width;
int height = preferredSize.height;
int x = LEFT_BORDER;
int y = TOP_BORDER + metrics.getHeight();
// Set graphics offset at correct position
graphics.translate(position.x, position.y);
// Render background
final Dimension dimension = new Dimension(
savedChildrenSize.width + RIGHT_BORDER,
savedChildrenSize.height + BOTTOM_BORDER);
LEFT_BORDER + childDimensions.width + RIGHT_BORDER,
TOP_BORDER + childDimensions.height + BOTTOM_BORDER);
final BackgroundComponent backgroundComponent = new BackgroundComponent();
backgroundComponent.setRectangle(new Rectangle(dimension));
backgroundComponent.setBackgroundColor(backgroundColor);
backgroundComponent.render(graphics);
if (!Strings.isNullOrEmpty(title))
{
// Render title
final TextComponent titleComponent = new TextComponent();
titleComponent.setText(title);
titleComponent.setColor(titleColor);
titleComponent.setPosition(new Point((dimension.width - metrics.stringWidth(title)) / 2, y));
titleComponent.render(graphics);
// Offset children
final int baseX = LEFT_BORDER;
final int baseY = TOP_BORDER + metrics.getHeight();
int width = 0;
int height = 0;
int x = baseX;
int y = baseY;
// Move children a bit
height = y += metrics.getHeight() + SEPARATOR;
}
// Create child preferred size
final Dimension childPreferredSize = new Dimension(
preferredSize.width - LEFT_BORDER - RIGHT_BORDER,
preferredSize.height - TOP_BORDER - BOTTOM_BORDER);
// Render all children
final Dimension childPreferredSize = new Dimension(
preferredSize.width - RIGHT_BORDER,
preferredSize.height - BOTTOM_BORDER);
for (final LayoutableRenderableEntity child : children)
{
child.setPreferredSize(childPreferredSize);
@@ -130,24 +105,21 @@ public class PanelComponent implements LayoutableRenderableEntity
switch (orientation)
{
case VERTICAL:
height = y += childDimension.height + SEPARATOR;
width = Math.max(width, x + childDimension.width);
height += childDimension.height;
y = baseY + height;
width = Math.max(width, childDimension.width);
break;
case HORIZONTAL:
width = x += childDimension.width + SEPARATOR;
height = Math.max(height, y + childDimension.height);
width += childDimension.width;
x = baseX + width;
height = Math.max(height, childDimension.height);
break;
}
}
// Reset the padding
height -= metrics.getHeight();
// Cache children bounds
childDimensions.setSize(width, height);
// Save children size
savedChildrenSize.setSize(width, height);
// Reset graphics position
graphics.translate(-position.x, -position.y);
return dimension;
}
}

View File

@@ -33,28 +33,16 @@ import java.awt.Point;
import java.text.DecimalFormat;
import lombok.Setter;
@Setter
public class ProgressBarComponent implements LayoutableRenderableEntity
{
@Setter
private String text;
@Setter
private double progress;
@Setter
private Point position = new Point();
@Setter
private Color foregroundColor = new Color(82, 161, 82);
@Setter
private Color backgroundColor = new Color(255, 255, 255, 127);
@Setter
private Color fontColor = Color.WHITE;
@Setter
private Dimension preferredSize = new Dimension(129, 16);
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 16);
@Override
public Dimension render(Graphics2D graphics)

View File

@@ -34,25 +34,14 @@ import lombok.Setter;
import net.runelite.api.Point;
import net.runelite.client.ui.overlay.RenderableEntity;
@Setter
public class ProgressPieComponent implements RenderableEntity
{
@Setter
private int diameter = 25;
@Setter
private Color borderColor = Color.WHITE;
@Setter
private Color fill = Color.WHITE;
@Setter
private Stroke stroke = new BasicStroke(1);
@Setter
private double progress;
@Setter
private Point position;
@Override

View File

@@ -33,19 +33,15 @@ import java.util.regex.Pattern;
import lombok.Setter;
import net.runelite.client.ui.overlay.RenderableEntity;
@Setter
public class TextComponent implements RenderableEntity
{
private static final String COL_TAG_REGEX = "(<col=([0-9a-fA-F]){2,6}>)";
private static final Pattern COL_TAG_PATTERN_W_LOOKAHEAD = Pattern.compile("(?=" + COL_TAG_REGEX + ")");
private static final Pattern COL_TAG_PATTERN = Pattern.compile(COL_TAG_REGEX);
@Setter
private String text;
@Setter
private Point position = new Point();
@Setter
private Color color = Color.WHITE;
public static String textWithoutColTags(String text)

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2018, Tomas Slusny <slusnucky@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
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 lombok.Builder;
import lombok.Setter;
@Setter
@Builder
public class TitleComponent implements LayoutableRenderableEntity
{
private String text;
@Builder.Default
private Color color = Color.WHITE;
@Builder.Default
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
@Override
public Dimension render(Graphics2D graphics)
{
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));
final Dimension dimension = titleComponent.render(graphics);
return new Dimension(Math.max(preferredSize.width, dimension.width), Math.max(preferredSize.height, dimension.height));
}
}

View File

@@ -35,22 +35,16 @@ import lombok.Setter;
import net.runelite.api.IndexedSprite;
import net.runelite.client.ui.overlay.RenderableEntity;
@Setter
public class TooltipComponent implements RenderableEntity
{
private static final Pattern BR = Pattern.compile("</br>");
private static final int OFFSET = 4;
private static final int MOD_ICON_WIDTH = 13; // they are generally 13px wide
@Setter
private String text;
@Setter
private Color backgroundColor = BackgroundComponent.DEFAULT_BACKGROUND_COLOR;
@Setter
private Color backgroundColor = ComponentConstants.STANDARD_BACKGROUND_COLOR;
private Point position = new Point();
@Setter
private IndexedSprite[] modIcons;
@Override