public class StyledContainer extends AbstractWidget<StyledContainer>
A container with a nice textured background and borders. Contains some helpers for making it have a nice header and for scaling everything to fit the given contents. Like layouts or other containers: anchoring, rendering, updates and the other usual suspects will be passed down the hierarchy, so calling them once on the container itself will be sufficient to handle the content and header widgets managed by this container. StyledContainers work nicely with MoveContainer
s, and are designed as a "super" tooltip to give the base game tooltips a bit more flexibility. They can also be used as larger screens as well.
Example use:
StyledContainer c = new StyledContainer(500, 500)
.withHeader("Title", "Subtitle")
.withHeaderAnchor(AnchorPosition.LEFT_CENTER)
.withHeaderColor(EaselColors.HEADER_PURPLE())
.withContent(
new VerticalLayout(20)
.withChild(new Label("Content One"))
.withChild(new Label("Content Two"))
)
.scaleToContent()
.anchoredCenteredOnScreen();
hasInteractivity, hb, isHovered, leftClickStarted, onLeftClick, onMouseEnter, onMouseLeave, onRightClick, rightClickStarted
Constructor and Description |
---|
StyledContainer(float width,
float height)
Constructs a new styled container with the given dimensions.
|
Modifier and Type | Method and Description |
---|---|
StyledContainer |
anchoredAt(float x,
float y,
AnchorPosition anchorPosition,
InterpolationSpeed movementSpeed)
Moves the widget towards a specific spot.
|
protected void |
cancelMovementQueueForAllChildren(boolean shouldTryAndResolveOneLastTime) |
float |
getContentHeight()
The internal content height of the widget (excludes margins).
|
float |
getContentWidth()
The internal content width of the widget (excludes margins).
|
protected void |
renderWidget(com.badlogic.gdx.graphics.g2d.SpriteBatch sb)
Custom widgets should implement this method for rendering.
|
StyledContainer |
scaleToContent()
Automatically scale the container to fit the main contents.
|
StyledContainer |
scaleToContentHeight()
Automatically scale the container to fit its main content (and header) height.
|
StyledContainer |
scaleToContentWidth()
Automatically scale the container to fit its main content (and header) width.
|
protected void |
setChildrenDelayedMovement(float deltaX,
float deltaY,
InterpolationSpeed movementSpeed,
long startingTimeMillis)
|
static void |
syncContainerHeights(boolean scaleToContentFirst,
java.util.List<StyledContainer> containers)
|
static void |
syncContainerHeights(boolean scaleToContentFirst,
java.util.stream.Stream<StyledContainer> containers)
Makes all containers share the height of the tallest container.
|
static void |
syncContainerHeights(boolean scaleToContentFirst,
StyledContainer... containers)
|
static void |
syncContainerHeights(java.util.List<StyledContainer> containers)
|
static void |
syncContainerHeights(java.util.stream.Stream<StyledContainer> containers)
|
static void |
syncContainerHeights(StyledContainer... containers)
|
static void |
syncContainerWidths(boolean scaleToContentFirst,
java.util.List<StyledContainer> containers)
|
static void |
syncContainerWidths(boolean scaleToContentFirst,
java.util.stream.Stream<StyledContainer> containers)
Makes all StyledContainers in the stream share the width of the widest one.
|
static void |
syncContainerWidths(boolean scaleToContentFirst,
StyledContainer... containers)
|
static void |
syncContainerWidths(java.util.List<StyledContainer> containers)
|
static void |
syncContainerWidths(java.util.stream.Stream<StyledContainer> containers)
|
static void |
syncContainerWidths(StyledContainer... containers)
|
protected void |
updateWidget()
Required for interactive components.
|
StyledContainer |
withBaseColor(com.badlogic.gdx.graphics.Color baseColor)
The background color of the entire container texture.
|
StyledContainer |
withContent(AbstractWidget content,
boolean autoAddMargins)
Set the main content of this container.
|
StyledContainer |
withContentAnchor(AnchorPosition contentAnchor)
Sets the position of the main contents inside the bottom region.
|
StyledContainer |
withDimensions(float newWidth,
float newHeight)
Set new dimensions for the full container.
|
StyledContainer |
withHeader(AbstractWidget customHeader,
boolean autoAddMargins)
Add a custom child widget to serve as this containers header.
|
StyledContainer |
withHeader(java.lang.String title)
Add a single-line title to this container with the given text.
|
StyledContainer |
withHeader(java.lang.String title,
java.lang.String subtitle)
Add a double-line title/subtitle to this container with the given text.
|
StyledContainer |
withHeaderAnchor(AnchorPosition headerAnchor)
Sets the position of the header contents inside the header region.
|
StyledContainer |
withHeaderColor(com.badlogic.gdx.graphics.Color headerColor)
The background color of the header, if it exists (use with one of the header builder functions:
withHeader(String) etc.). |
StyledContainer |
withHeight(float newHeight)
Set the new height of the full container to a new value Requires re-anchoring.
|
StyledContainer |
withShadows(boolean enabled)
Set whether a drop shadow is rendered underneath the container.
|
StyledContainer |
withTrimColors(com.badlogic.gdx.graphics.Color trimColor,
com.badlogic.gdx.graphics.Color trimHighlightColor)
The trim colors of the entire container texture (for the outermost borders).
|
StyledContainer |
withWidth(float newWidth)
Set the new width of the full container to a new value.
|
anchoredAt, anchoredAtClamped, anchoredAtClamped, anchoredCenteredOnMouse, anchoredCenteredOnMouse, anchoredCenteredOnMouseClamped, anchoredCenteredOnMouseClamped, anchoredCenteredOnScreen, anchoredCenteredOnScreen, cancelMovementQueue, delayedTranslate, getBottom, getContentBottom, getContentCenterX, getContentCenterY, getContentLeft, getContentRight, getContentTop, getHeight, getLeft, getRight, getTop, getWidth, hide, initializeInteractivity, isMouseInBounds, isMouseInContentBounds, leftMouseClick, mouseEnter, mouseLeave, onLeftClick, onMouseEnter, onMouseLeave, onRightClick, refreshAnchor, render, renderTopLevel, resolveMovementQueue, rightMouseClick, scaleHitboxToContent, setAllDelayedMovement, show, toString, translate, update, updateInteractivity, withMargins, withMargins, withMargins
public StyledContainer(float width, float height)
scaleToContent()
or resized manually with withDimensions(float, float)
).width
- the entire width of the containerheight
- the entire height of the containerpublic StyledContainer withHeader(java.lang.String title)
withHeaderAnchor(AnchorPosition)
.title
- the text to serve as the headerpublic StyledContainer withHeader(java.lang.String title, java.lang.String subtitle)
withHeaderAnchor(AnchorPosition)
.title
- the text to serve as the header titlesubtitle
- the text to serve as the header subtitlepublic StyledContainer withHeader(AbstractWidget customHeader, boolean autoAddMargins)
withHeaderAnchor(AnchorPosition)
.customHeader
- the widget which will sit in the header region (which automatically scales to surround this custom header)autoAddMargins
- if true, will call AbstractWidget.withMargins(float, float)
on the customHeader
with some suggested defaults (the header needs some sort of margins to not touch the borders of the container)public StyledContainer withHeaderColor(com.badlogic.gdx.graphics.Color headerColor)
withHeader(String)
etc.). The header colors can be picked from the EaselColors.HEADER_
family of colors, such as EaselColors.HEADER_RED()
. These header colors were chosen since they can fit well with the default background and trim colors of the overall container, and will work with the default header text colors. If not set, defaults to EaselColors.HEADER_BLUE()
.headerColor
- the desired color of the headerpublic StyledContainer withBaseColor(com.badlogic.gdx.graphics.Color baseColor)
EaselColors.TOOLTIP_BASE()
if not set.baseColor
- the new background colorpublic StyledContainer withTrimColors(com.badlogic.gdx.graphics.Color trimColor, com.badlogic.gdx.graphics.Color trimHighlightColor)
EaselColors.TOOLTIP_TRIM()
and EaselColors.TOOLTIP_TRIM_HIGHLIGHT()
if not set.trimColor
- the basic border colortrimHighlightColor
- the highlight color for the borderpublic StyledContainer withHeaderAnchor(AnchorPosition headerAnchor)
headerAnchor
- where the header contents are anchored inside the header regionpublic StyledContainer withContentAnchor(AnchorPosition contentAnchor)
contentAnchor
- where the main contents are anchored in the bottom section of the containerpublic StyledContainer withShadows(boolean enabled)
MoveContainer
when moving styled containers around to improve the aesthetics a bit.enabled
- if true, will render shadows beneath the entire widgetpublic StyledContainer withContent(AbstractWidget content, boolean autoAddMargins)
autoAddMargins
flag which can be used to automatically give the new content a reasonable set of default margins. Margins on the content widget itself are required for the content to not touch the sides of the container. Requires re-anchoring.content
- the main widget that is the focus of this containerautoAddMargins
- if true, will call AbstractWidget.withMargins(float)
on the content widget with some suggested valuespublic StyledContainer withWidth(float newWidth)
newWidth
- the new widthpublic StyledContainer withHeight(float newHeight)
newHeight
- the new heightpublic StyledContainer withDimensions(float newWidth, float newHeight)
withWidth(float)
and withHeight(float)
at the same time. Requires re-anchoring.newWidth
- the new widthnewHeight
- the new heightpublic StyledContainer scaleToContentWidth()
public StyledContainer scaleToContentHeight()
public StyledContainer scaleToContent()
public static void syncContainerWidths(StyledContainer... containers)
syncContainerWidths(boolean, Stream)
. Will not automatically scale to content first.containers
- a variadic list of containers to scalesyncContainerWidths(boolean, Stream)
public static void syncContainerWidths(java.util.stream.Stream<StyledContainer> containers)
syncContainerWidths(boolean, Stream)
. Will not automatically scale to content first.containers
- a stream of containers to scalesyncContainerWidths(boolean, Stream)
public static void syncContainerWidths(java.util.List<StyledContainer> containers)
syncContainerWidths(boolean, Stream)
. Will not automatically scale to content first.containers
- a list of containers to scalesyncContainerWidths(boolean, Stream)
public static void syncContainerWidths(boolean scaleToContentFirst, StyledContainer... containers)
scaleToContentFirst
- whether to call scaleToContent()
on each container first before scaling all widths to the maximumcontainers
- a variadic list of containers to scalesyncContainerWidths(boolean, Stream)
public static void syncContainerWidths(boolean scaleToContentFirst, java.util.stream.Stream<StyledContainer> containers)
Makes all StyledContainers in the stream share the width of the widest one. As this is a width altering function, each affected widget will require re-anchoring before being used. The scaleToContentFirst
flag is mostly for convenience and if set, will call scaleToContent()
on all widgets before computing the the final new width.
An example use:
// This is some layout which contains the StyledContainers as children:
layout = ...;
// We want to sync up all the containers to have the same width
// the first argument "true" simply calls .scaleToContent() on each container
// before computing the width to scale everything to
StyledContainer.syncContainerWidths(true, layout.iteratorOfType(StyledContainer.class));
// We've (probably) adjusted the dimensions of multiple styled containers, so be sure
// to reanchor everything affected. Since we've stored the containers in a layout, you
// can just perform your anchoring step on it now
layout.anchoredCenteredOnScreen();
scaleToContentFirst
- whether to call scaleToContent()
on each container first before scaling all widths to the maximumcontainers
- a stream of containers to scalesyncContainerHeights(boolean, Stream)
,
syncContainerWidths(boolean, StyledContainer...)
,
syncContainerWidths(boolean, List)
,
syncContainerWidths(Stream)
,
syncContainerWidths(List)
,
syncContainerWidths(StyledContainer...)
public static void syncContainerWidths(boolean scaleToContentFirst, java.util.List<StyledContainer> containers)
scaleToContentFirst
- whether to call scaleToContent()
on each container first before scaling all widths to the maximumcontainers
- a list of containers to scalesyncContainerWidths(boolean, Stream)
public static void syncContainerHeights(StyledContainer... containers)
syncContainerHeights(boolean, Stream)
. Will not automatically scale to content first.containers
- a variadic list of containers to scalesyncContainerHeights(boolean, Stream)
public static void syncContainerHeights(java.util.stream.Stream<StyledContainer> containers)
syncContainerHeights(boolean, Stream)
. Will not automatically scale to content first.containers
- a stream of containers to scalesyncContainerHeights(boolean, Stream)
public static void syncContainerHeights(java.util.List<StyledContainer> containers)
syncContainerHeights(boolean, Stream)
. Will not automatically scale to content first.containers
- a list of containers to scalesyncContainerHeights(boolean, Stream)
public static void syncContainerHeights(boolean scaleToContentFirst, StyledContainer... containers)
scaleToContentFirst
- whether to call scaleToContent()
on each container first before scaling all heights to the maximumcontainers
- a stream of containers to scalesyncContainerHeights(boolean, Stream)
public static void syncContainerHeights(boolean scaleToContentFirst, java.util.stream.Stream<StyledContainer> containers)
syncContainerWidths(boolean, Stream)
, but can be used in a similar manner.scaleToContentFirst
- whether to call scaleToContent()
on each container first before scaling all heights to the maximumcontainers
- a stream of containers to scale heights ofsyncContainerWidths(boolean, Stream)
,
syncContainerHeights(boolean, StyledContainer...)
,
syncContainerHeights(boolean, List)
,
syncContainerHeights(Stream)
,
syncContainerHeights(StyledContainer...)
,
syncContainerHeights(List)
public static void syncContainerHeights(boolean scaleToContentFirst, java.util.List<StyledContainer> containers)
scaleToContentFirst
- whether to call scaleToContent()
on each container first before scaling all heights to the maximumcontainers
- a list of containers to scalesyncContainerHeights(boolean, Stream)
public float getContentWidth()
AbstractWidget
getContentWidth
in class AbstractWidget<StyledContainer>
AbstractWidget.getWidth()
,
AbstractWidget.getContentHeight()
public float getContentHeight()
AbstractWidget
getContentHeight
in class AbstractWidget<StyledContainer>
AbstractWidget.getHeight()
,
AbstractWidget.getContentWidth()
public StyledContainer anchoredAt(float x, float y, AnchorPosition anchorPosition, InterpolationSpeed movementSpeed)
AbstractWidget
Moves the widget towards a specific spot. The anchorPosition
defines the point on the widget that will be placed at the position (x, y)
.
For example, anchoredAt(100, 200, AnchorPosition.LEFT_BOTTOM, InterpolationSpeed.FAST)
will set the bottom-left corner of the widget to 100 pixels from the left side of the screen and 200 pixels from the bottom of the screen. The widget then renders up and to the right of this point since we anchored at an AnchorPosition.LEFT_BOTTOM
. As a second example, using AnchorPosition.CENTER
ensures that (x, y)
will be the center of the widget.
The interpolation speed movementSpeed
determines how quickly the widget moves to the target location. Using a speed other than InterpolationSpeed.INSTANT
makes the widget move towards the desired position over the next few frames in a smoothly animated manner. For convenience since instant moving is often the desired effect, see AbstractWidget.anchoredAt(float, float, AnchorPosition)
.
Note: you should always anchor at least once before rendering. For more dynamic widgets that move a lot (e.g. a tooltip dependent on the mouse cursor location using InputHelper.mX
and InputHelper.mY
), a general pattern is to call anchoredAt
right before rendering in your main render function, e.g.:
widget.anchoredAt(x, y, AnchorPosition.CENTER, InterpolationSpeed.INSTANT)
.render(sb);
anchoredAt
in class AbstractWidget<StyledContainer>
x
- the x position in pixels from the left edge of the screeny
- the y position in pixels from the bottom edge of the screenanchorPosition
- what piece of the widget will be moved to (x, y)
movementSpeed
- how quickly the widget will move towards the desired positionAbstractWidget.anchoredAt(float, float, AnchorPosition)
,
AbstractWidget.anchoredAtClamped(float, float, AnchorPosition, float)
,
AbstractWidget.anchoredAtClamped(float, float, AnchorPosition, InterpolationSpeed, float)
protected void cancelMovementQueueForAllChildren(boolean shouldTryAndResolveOneLastTime)
cancelMovementQueueForAllChildren
in class AbstractWidget<StyledContainer>
protected void setChildrenDelayedMovement(float deltaX, float deltaY, InterpolationSpeed movementSpeed, long startingTimeMillis)
AbstractWidget
AbstractWidget.delayedTranslate(float, float, InterpolationSpeed, long)
. Make sure any widget with children (e.g. containers, layouts, etc.) overrides this function and calls AbstractWidget.setAllDelayedMovement(float, float, InterpolationSpeed, long)
using the input to this function on all direct descendants. This is required if you want to be able to use a delayed anchoredAt command on custom widgets.setChildrenDelayedMovement
in class AbstractWidget<StyledContainer>
deltaX
- how much movement horizontallydeltaY
- how much movement verticallymovementSpeed
- how fast the widget will move towards the target, once startingTimeMillis is reached (i.e. System.currentTimeMillis()
is greater than or equal to this starting time)startingTimeMillis
- a time generated by an offset of System.currentTimeMillis()
, determined by the original AbstractWidget.delayedTranslate(float, float, InterpolationSpeed, long)
function that starts this chainprotected void updateWidget()
AbstractWidget
update()
on all children. For widgets that require some sort of updates each frame, you can do so here.updateWidget
in class AbstractWidget<StyledContainer>
protected void renderWidget(com.badlogic.gdx.graphics.g2d.SpriteBatch sb)
AbstractWidget
Custom widgets should implement this method for rendering. Use the inner content positions (e.g. AbstractWidget.getContentLeft()
, AbstractWidget.getContentWidth()
, etc.) to determine any position information necessary for rendering at a specific location. If the library is used as intended, these content locations should be accurate to where the widget needs to be rendered, as they reflect the most up to date location set by an anchoredAt call (this automatically will be interpolated if the anchorAt move is set to occur over several frames).
Note: you NEED to revert any changes you make to the SpriteBatch (e.g. setting a shader, changing the perspective matrix, etc.) by the time this function returns, as the SpriteBatch will be reused for rendering other widgets which will not expect those changes. You also don't technically need to render to this particular SpriteBatch (e.g. you can render to your own batch if you know what you're doing), as long as you follow the general intent of this function to render the widget.
renderWidget
in class AbstractWidget<StyledContainer>
sb
- the SpriteBatch the widget should be rendered onAbstractWidget.renderTopLevel(SpriteBatch)