public class NinePatchWidget extends AbstractWidget<NinePatchWidget>
A "widgetized" version of libgdx's NinePatch (NinePatch
). NinePatches are a convenient way to seamlessly scalable textures that won't make the corners stretch poorly. A nine patch is the jargon term for a texture which has nine regions: top left, top middle, top right, center left, center, center right, bottom left, bottom center, and bottom right. All four corners in this nine patch can be considered "fixed" and won't grow - the inner pieces will either grow horizontally (in terms on the top and bottom edges) or vertically (the left or right edge) or both (the very center piece). This lets you have dynamically sized textures where the corners don't get stretched awkwardly or get super pixelated, as long as you've set up your initial ninepatch texture correctly.
This library will typically use 128px by 128px textures (with the corners 32px in size) to make its default NinePatches - and so there are a few convenience constructors that use these sizes. However, you are not limited to just these dimensions and can specify the full dimensions of your nine patch if you need to alter it at all.
hasInteractivity, hb, isHovered, leftClickStarted, onLeftClick, onMouseEnter, onMouseLeave, onRightClick, rightClickStarted
Constructor and Description |
---|
NinePatchWidget(float width,
float height,
int patchLeft,
int patchRight,
int patchTop,
int patchBottom,
com.badlogic.gdx.graphics.Texture texture)
Constructs a new NinePatch widget using a texture and specifying all corner sizes for the patch.
|
NinePatchWidget(float width,
float height,
int patchLeft,
int patchRight,
int patchTop,
int patchBottom,
com.badlogic.gdx.graphics.g2d.TextureRegion texRegion)
Constructs a new NinePatch widget using a texture region and specifying all corner sizes for the patch.
|
NinePatchWidget(float width,
float height,
int patchOffset,
com.badlogic.gdx.graphics.Texture texture)
Constructs a new NinePatch widget using a texture.
|
NinePatchWidget(float width,
float height,
int patchOffset,
com.badlogic.gdx.graphics.g2d.TextureRegion texRegion)
Constructs a new NinePatch widget using a texture region.
|
NinePatchWidget(float width,
float height,
com.badlogic.gdx.graphics.Texture texture)
Constructs a new NinePatch widget using a full texture.
|
NinePatchWidget(float width,
float height,
com.badlogic.gdx.graphics.g2d.TextureRegion texRegion)
Constructs a new NinePatch widget using a texture region.
|
Modifier and Type | Method and Description |
---|---|
NinePatchWidget |
anchoredAt(float x,
float y,
AnchorPosition anchorPosition,
InterpolationSpeed movementSpeed)
Moves the widget towards a specific spot.
|
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.
|
NinePatchWidget |
scaleToFullWidget(AbstractWidget widget)
Resizes this widget to adjust to the full size of the provided widget (convenience).
|
NinePatchWidget |
withColor(com.badlogic.gdx.graphics.Color renderColor)
Sets the color used to render this nine patch.
|
NinePatchWidget |
withDimensions(float width,
float height)
Resizes this widget (will likely require another anchoring, e.g.
|
NinePatchWidget |
withHeight(float height) |
NinePatchWidget |
withWidth(float width) |
anchoredAt, anchoredAtClamped, anchoredAtClamped, anchoredCenteredOnMouse, anchoredCenteredOnMouse, anchoredCenteredOnMouseClamped, anchoredCenteredOnMouseClamped, anchoredCenteredOnScreen, anchoredCenteredOnScreen, cancelMovementQueue, cancelMovementQueueForAllChildren, 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, setChildrenDelayedMovement, show, toString, translate, update, updateInteractivity, updateWidget, withMargins, withMargins, withMargins
public NinePatchWidget(float width, float height, com.badlogic.gdx.graphics.g2d.TextureRegion texRegion)
width
- the desired width of the final widget (what we render)height
- the desired height of the final widget (what we render)texRegion
- the texture region containing the entire nine patchNinePatchWidget(float, float, Texture)
,
NinePatchWidget(float, float, int, int, int, int, TextureRegion)
public NinePatchWidget(float width, float height, int patchOffset, com.badlogic.gdx.graphics.g2d.TextureRegion texRegion)
patchOffset
in size.width
- the desired width of the final widget (what we render)height
- the desired height of the final widget (what we render)patchOffset
- the number of pixels from each edge defining the patch edge (assumes square)texRegion
- the texture region containing the entire nine patchpublic NinePatchWidget(float width, float height, int patchOffset, com.badlogic.gdx.graphics.Texture texture)
patchOffset
in size.width
- the desired width of the final widget (what we render)height
- the desired height of the final widget (what we render)patchOffset
- the number of pixels from each edge defining the patch edge (assumes square)texture
- the texture containing the entire nine patchpublic NinePatchWidget(float width, float height, com.badlogic.gdx.graphics.Texture texture)
width
- the desired width of the final widget (what we render)height
- the desired height of the final widget (what we render)texture
- the texture containing the entire nine patchNinePatchWidget(float, float, TextureRegion)
,
NinePatchWidget(float, float, int, int, int, int, Texture)
public NinePatchWidget(float width, float height, int patchLeft, int patchRight, int patchTop, int patchBottom, com.badlogic.gdx.graphics.g2d.TextureRegion texRegion)
NinePatch
from the libgdx library for more details about what NinePatches are or what the corner values mean.width
- the desired width of the final widget (what we render)height
- the desired height of the final widget (what we render)patchLeft
- the number of pixels from the left which define the left patch edgepatchRight
- the number of pixels from the right which define the right patch edgepatchTop
- the number of pixels from the top which define the top patch edgepatchBottom
- the number of pixels from the bottom which define the bottom patch edgetexRegion
- the texture region containing the entire nine patchNinePatchWidget(float, float, int, int, int, int, Texture)
,
NinePatchWidget(float, float, TextureRegion)
public NinePatchWidget(float width, float height, int patchLeft, int patchRight, int patchTop, int patchBottom, com.badlogic.gdx.graphics.Texture texture)
NinePatch
from the libgdx library for more details about what NinePatches are or what the corner values mean.width
- the desired width of the final widget (what we render)height
- the desired height of the final widget (what we render)patchLeft
- the number of pixels from the left which define the left patch edgepatchRight
- the number of pixels from the right which define the right patch edgepatchTop
- the number of pixels from the top which define the top patch edgepatchBottom
- the number of pixels from the bottom which define the bottom patch edgetexture
- the texture containing the entire nine patchNinePatchWidget(float, float, int, int, int, int, TextureRegion)
,
NinePatchWidget(float, float, Texture)
public NinePatchWidget withColor(com.badlogic.gdx.graphics.Color renderColor)
renderColor
- the color to render withpublic NinePatchWidget withDimensions(float width, float height)
AbstractWidget.anchoredAt(float, float, AnchorPosition)
). The final rendered output will depend on the width/height provided here (or from the constructor, if this does not exist). This function will override the constructor's width/height and will also rebuild hitboxes if required.width
- the new width of the widgetheight
- the new height of the widgetscaleToFullWidget(AbstractWidget)
public NinePatchWidget withWidth(float width)
public NinePatchWidget withHeight(float height)
public NinePatchWidget scaleToFullWidget(AbstractWidget widget)
Resizes this widget to adjust to the full size of the provided widget (convenience). E.g. the new width of this widget when rendered is the value of AbstractWidget.getWidth()
at the time of this function call (it will not update automatically if the source widget changes in size in the future). Note that this takes the full size of the given widget (including margins), and not the inner content sizes (e.g. it does NOT look at AbstractWidget.getContentWidth()
). This is because you often want to scale your nine patch to be a bit bigger than the widget(s) it is supposed to be surrounding, and margins (AbstractWidget.withMargins(float)
) let you do that quite easily. You can use withDimensions(float, float)
to use precisely specified width/height should you need more control.
Like withDimensions(float, float)
, you'll probably want to call some sort of AbstractWidget.anchoredAt(float, float, AnchorPosition)
to put the widget in the proper place afterwards.
widget
- the widget whose total width/height dimensions will be copiedwithDimensions(float, float)
public float getContentWidth()
AbstractWidget
getContentWidth
in class AbstractWidget<NinePatchWidget>
AbstractWidget.getWidth()
,
AbstractWidget.getContentHeight()
public float getContentHeight()
AbstractWidget
getContentHeight
in class AbstractWidget<NinePatchWidget>
AbstractWidget.getHeight()
,
AbstractWidget.getContentWidth()
public NinePatchWidget 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<NinePatchWidget>
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 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<NinePatchWidget>
sb
- the SpriteBatch the widget should be rendered onAbstractWidget.renderTopLevel(SpriteBatch)