Package com.github.tommyettinger.textra
Class TextraLabel
java.lang.Object
com.badlogic.gdx.scenes.scene2d.Actor
com.badlogic.gdx.scenes.scene2d.ui.Widget
com.github.tommyettinger.textra.TextraLabel
- All Implemented Interfaces:
com.badlogic.gdx.scenes.scene2d.utils.Layout
- Direct Known Subclasses:
TypingLabel
public class TextraLabel
extends com.badlogic.gdx.scenes.scene2d.ui.Widget
A scene2d.ui Widget that displays text using a
This is meant to work with
Font
rather than a libGDX BitmapFont. This supports being
laid out in a Table just like the typical Label (when wrap
is false, which is the default). This
permits square-bracket tag markup from Font, such as [light blue]
to change the font color, or [_]
to
underline text. It does not support the curly-brace token markup that its subclass TypingLabel
does, nor does
this handle input in the way TypingLabel can. It also, naturally, doesn't have the typing effect TypingLabel does,
which makes this more suitable for some kinds of text. TypingLabel can be told to immediately
TypingLabel.skipToTheEnd()
, which does make it look like a TextraLabel, but permits effects.
This is meant to work with
FWSkin
or one of its subclasses, such as FreeTypistSkin
, and isn't
guaranteed to work with a regular Skin
. FWSkin can load the same JSON files Skin uses, and it extends Skin.-
Field Summary
Modifier and TypeFieldDescriptionint
protected Font
boolean
If true; allows text to wrap when it would go past the layout'stargetWidth
and continue on the next line; if false, uses a very long target width and only adds newlines when they are in the label's text. -
Constructor Summary
ConstructorDescriptionCreates a TextraLabel that uses the default libGDX font (lsans-15 in the current version) with white color.TextraLabel
(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin) Creates a TextraLabel with the given text (which may be multi-line) and using the specified Skin's LabelStyle.TextraLabel
(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, boolean makeGridGlyphs) Creates a TextraLabel with the given text (which may be multi-line) and using the specified Skin's LabelStyle.TextraLabel
(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, Font replacementFont) Creates a TextraLabel with the given text (which may be multi-line) and using the default style from a Skin, replacing any font that would be drawn from the style withreplacementFont
.TextraLabel
(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName) Creates a TextraLabel with the given text (which may be multi-line) and using the specified style from the given Skin.TextraLabel
(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, boolean makeGridGlyphs) Creates a TextraLabel with the given text (which may be multi-line) and using the specified style from the given Skin.TextraLabel
(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, com.badlogic.gdx.graphics.Color color) Creates a TextraLabel with the given text (which may be multi-line), using the specified style from the given Skin, with the default Color overridden by the given one.TextraLabel
(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, Font replacementFont) Creates a TextraLabel with the given text (which may be multi-line) and using the given style taken by name from a Skin, replacing any font that would be drawn from the style withreplacementFont
.TextraLabel
(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, Font replacementFont, com.badlogic.gdx.graphics.Color color) Creates a TextraLabel with the given text (which may be multi-line) and using the given style taken by name from a Skin, replacing any font that would be drawn from the style withreplacementFont
.TextraLabel
(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, String colorName) Creates a TextraLabel with the given text (which may be multi-line), using the specified style from the given Skin, with the default Color overridden by the color with the given name in the skin.TextraLabel
(String text, Font font) Creates a TextraLabel with the given text (which may be multi-line) and using the given Font.TextraLabel
(String text, Font font, com.badlogic.gdx.graphics.Color color) Creates a TextraLabel with the given text (which may be multi-line), using the given Font, and using the given default color.TextraLabel
(String text, Styles.LabelStyle style) Creates a TextraLabel with the given text (which may be multi-line) and using the given style.TextraLabel
(String text, Styles.LabelStyle style, boolean makeGridGlyphs) Creates a TextraLabel with the given text (which may be multi-line) and using the given style.TextraLabel
(String text, Styles.LabelStyle style, Font replacementFont) Creates a TextraLabel with the given text (which may be multi-line) and using the given style. -
Method Summary
Modifier and TypeMethodDescriptionvoid
draw
(com.badlogic.gdx.graphics.g2d.Batch batch, float parentAlpha) int
Gets the alignment for the text in this TextraLabel.Gets the ellipsis, which may be null, or may be a String that can be placed at the end of the text if its max lines are exceeded.getFont()
long
getGlyph
(int index) float
getLineHeight
(int index) Gets the height of the Line containing the glyph at the given index.int
The maximum number ofLine
s this label can display.float
float
boolean
isWrap()
Gets the current wrapping mode.void
layout()
boolean
remove()
void
setAlignment
(int alignment) Sets the alignment for the text in this TextraLabel.void
setEllipsis
(String ellipsis) Sets the ellipsis text, which replaces the last few glyphs if non-null and the text added would exceed thegetMaxLines()
of this label's layout.void
Sets the font to the specified Font and then regenerates the layout usingFont.regenerateLayout(Layout)
.void
Just likesetFont(Font)
, except this only regenerates the layout ifregenerate
is true.void
setMaxLines
(int maxLines) Sets the maximum number ofLine
s this Layout can display; this is always at least 1.protected void
setParent
(com.badlogic.gdx.scenes.scene2d.Group parent) Called by the framework when an actor is added to or removed from a group.void
setSize
(float width, float height) void
setStage
(com.badlogic.gdx.scenes.scene2d.Stage stage) Called by the framework when this actor or any ascendant is added to a group that is in the stage.void
Changes the text in this TextraLabel to the given String, parsing any markup in it.setWrap
(boolean wrap) Sets the wrapping mode; if this changes the mode, then this invalidates the hierarchy.By default, does nothing; this is overridden in TypingLabel to skip its text progression ahead.substring
(int start, int end) Gets a String from the layout of this label, made of only the char portions of the glyphs from start (inclusive) to end (exclusive).toString()
useIntegerPositions
(boolean integer) A no-op unlessfont
is a subclass that overridesFont.handleIntegerPosition(float)
.Methods inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, invalidate, invalidateHierarchy, needsLayout, pack, setFillParent, setLayoutEnabled, sizeChanged, validate
Methods inherited from class com.badlogic.gdx.scenes.scene2d.Actor
act, addAction, addCaptureListener, addListener, ancestorsVisible, ascendantsVisible, clear, clearActions, clearListeners, clipBegin, clipBegin, clipEnd, debug, drawDebug, drawDebugBounds, fire, firstAscendant, getActions, getCaptureListeners, getColor, getDebug, getHeight, getListeners, getName, getOriginX, getOriginY, getParent, getRight, getRotation, getScaleX, getScaleY, getStage, getTop, getTouchable, getUserObject, getWidth, getX, getX, getY, getY, getZIndex, hasActions, hasKeyboardFocus, hasParent, hasScrollFocus, hit, isAscendantOf, isDescendantOf, isTouchable, isTouchFocusListener, isTouchFocusTarget, isVisible, localToActorCoordinates, localToAscendantCoordinates, localToParentCoordinates, localToScreenCoordinates, localToStageCoordinates, moveBy, notify, parentToLocalCoordinates, positionChanged, removeAction, removeCaptureListener, removeListener, rotateBy, rotationChanged, scaleBy, scaleBy, scaleChanged, screenToLocalCoordinates, setBounds, setColor, setColor, setDebug, setHeight, setName, setOrigin, setOrigin, setOriginX, setOriginY, setPosition, setPosition, setRotation, setScale, setScale, setScaleX, setScaleY, setTouchable, setUserObject, setVisible, setWidth, setX, setX, setY, setY, setZIndex, sizeBy, sizeBy, stageToLocalCoordinates, toBack, toFront
-
Field Details
-
layout
-
font
-
align
public int align -
wrap
public boolean wrapIf true; allows text to wrap when it would go past the layout'stargetWidth
and continue on the next line; if false, uses a very long target width and only adds newlines when they are in the label's text. This should typically be false for widgets that use scene2d.ui layout, but should be true for any widget that dynamically adjusts to fill an area with wrapped text. -
storedText
-
style
-
-
Constructor Details
-
TextraLabel
public TextraLabel()Creates a TextraLabel that uses the default libGDX font (lsans-15 in the current version) with white color. -
TextraLabel
Creates a TextraLabel with the given text (which may be multi-line) and using the specified Skin's LabelStyle. The skin should almost certainly be anFWSkin
or one of its subclasses.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingskin
- almost always anFWSkin
or one of its subclasses; must have aStyles.LabelStyle
orLabel.LabelStyle
registered as "default"
-
TextraLabel
public TextraLabel(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, boolean makeGridGlyphs) Creates a TextraLabel with the given text (which may be multi-line) and using the specified Skin's LabelStyle. The skin should almost certainly be anFWSkin
or one of its subclasses.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingskin
- almost always anFWSkin
or one of its subclasses; must have aStyles.LabelStyle
orLabel.LabelStyle
registered as "default"makeGridGlyphs
- currently ignored
-
TextraLabel
Creates a TextraLabel with the given text (which may be multi-line) and using the specified style from the given Skin. The skin should almost certainly be anFWSkin
or one of its subclasses.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingskin
- almost always anFWSkin
or one of its subclasses; must have aStyles.LabelStyle
orLabel.LabelStyle
registered with the given styleNamestyleName
- the name of a Styles.LabelStyle to use from the Skin
-
TextraLabel
public TextraLabel(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, boolean makeGridGlyphs) Creates a TextraLabel with the given text (which may be multi-line) and using the specified style from the given Skin. The skin should almost certainly be anFWSkin
or one of its subclasses.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingskin
- almost always anFWSkin
or one of its subclasses; must have aStyles.LabelStyle
orLabel.LabelStyle
registered with the given styleNamestyleName
- the name of a Styles.LabelStyle to use from the SkinmakeGridGlyphs
- currently ignored
-
TextraLabel
public TextraLabel(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, com.badlogic.gdx.graphics.Color color) Creates a TextraLabel with the given text (which may be multi-line), using the specified style from the given Skin, with the default Color overridden by the given one. The skin should almost certainly be anFWSkin
or one of its subclasses.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingskin
- almost always anFWSkin
or one of its subclasses; must have aStyles.LabelStyle
orLabel.LabelStyle
registered with the given styleNamestyleName
- the name of a Styles.LabelStyle to use from the Skincolor
- the color to use for the font when unspecified (at the start and when reset)
-
TextraLabel
public TextraLabel(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, String colorName) Creates a TextraLabel with the given text (which may be multi-line), using the specified style from the given Skin, with the default Color overridden by the color with the given name in the skin. The skin should almost certainly be anFWSkin
or one of its subclasses.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingskin
- almost always anFWSkin
or one of its subclasses; must have aStyles.LabelStyle
orLabel.LabelStyle
registered with the given styleNamestyleName
- the name of a Styles.LabelStyle to use from the SkincolorName
- the name in the skin of the color to use for the font when unspecified (at the start and when reset)
-
TextraLabel
Creates a TextraLabel with the given text (which may be multi-line) and using the given style. This does not require a Skin to be available.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingstyle
- the Styles.LabelStyle to use
-
TextraLabel
Creates a TextraLabel with the given text (which may be multi-line) and using the given style. This does not require a Skin to be available.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingstyle
- the Styles.LabelStyle to usemakeGridGlyphs
- currently ignored
-
TextraLabel
Creates a TextraLabel with the given text (which may be multi-line) and using the default style from a Skin, replacing any font that would be drawn from the style withreplacementFont
. The skin should almost certainly be anFWSkin
or one of its subclasses.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingskin
- the default Styles.LabelStyle will be obtained from this and usedreplacementFont
- a Font that will be used in place of the one in style
-
TextraLabel
public TextraLabel(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, Font replacementFont) Creates a TextraLabel with the given text (which may be multi-line) and using the given style taken by name from a Skin, replacing any font that would be drawn from the style withreplacementFont
. The skin should almost certainly be anFWSkin
or one of its subclasses.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingskin
- almost always anFWSkin
or one of its subclasses; must have aStyles.LabelStyle
orLabel.LabelStyle
registered with the given styleNamestyleName
- the name of a Styles.LabelStyle to use from the SkinreplacementFont
- a Font that will be used in place of the one in style
-
TextraLabel
public TextraLabel(String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, String styleName, Font replacementFont, com.badlogic.gdx.graphics.Color color) Creates a TextraLabel with the given text (which may be multi-line) and using the given style taken by name from a Skin, replacing any font that would be drawn from the style withreplacementFont
. The skin should almost certainly be anFWSkin
or one of its subclasses.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingskin
- almost always anFWSkin
or one of its subclasses; must have aStyles.LabelStyle
orLabel.LabelStyle
registered with the given styleNamestyleName
- the name of a Styles.LabelStyle to use from the SkinreplacementFont
- a Font that will be used in place of the one in stylecolor
- the base color to use for the label, used when reset
-
TextraLabel
Creates a TextraLabel with the given text (which may be multi-line) and using the given style. This does not require a Skin to be available.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingstyle
- the Styles.LabelStyle to use, except for its fontreplacementFont
- a Font that will be used in place of the one in style
-
TextraLabel
Creates a TextraLabel with the given text (which may be multi-line) and using the given Font. This does not require a Skin to be available.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingfont
- a Font from this library, such as one obtained fromKnownFonts
-
TextraLabel
Creates a TextraLabel with the given text (which may be multi-line), using the given Font, and using the given default color. This does not require a Skin to be available.- Parameters:
text
- the text to use; may be multi-line, but will default to not wrappingfont
- a Font from this library, such as one obtained fromKnownFonts
color
- the color to use for the font when unspecified (at the start and when reset)
-
-
Method Details
-
draw
public void draw(com.badlogic.gdx.graphics.g2d.Batch batch, float parentAlpha) - Overrides:
draw
in classcom.badlogic.gdx.scenes.scene2d.ui.Widget
-
getPrefWidth
public float getPrefWidth()- Specified by:
getPrefWidth
in interfacecom.badlogic.gdx.scenes.scene2d.utils.Layout
- Overrides:
getPrefWidth
in classcom.badlogic.gdx.scenes.scene2d.ui.Widget
-
getPrefHeight
public float getPrefHeight()- Specified by:
getPrefHeight
in interfacecom.badlogic.gdx.scenes.scene2d.utils.Layout
- Overrides:
getPrefHeight
in classcom.badlogic.gdx.scenes.scene2d.ui.Widget
-
useIntegerPositions
A no-op unlessfont
is a subclass that overridesFont.handleIntegerPosition(float)
.- Parameters:
integer
- usually ignored- Returns:
- this for chaining
-
isWrap
public boolean isWrap()Gets the current wrapping mode. When wrap is enabled, the preferred and/or min/max sizes must be used so this knows where to wrap. If wrap is disabled, lines that are too wide will just widen the size of the widget.- Returns:
- whether this is currently wrapping
-
setWrap
Sets the wrapping mode; if this changes the mode, then this invalidates the hierarchy. When wrap is enabled, the preferred and/or min/max sizes must be used so this knows where to wrap. If wrap is disabled, lines that are too wide will just widen the size of the widget.- Parameters:
wrap
- whether to wrap or not
-
setSize
public void setSize(float width, float height) - Overrides:
setSize
in classcom.badlogic.gdx.scenes.scene2d.Actor
-
layout
public void layout()- Specified by:
layout
in interfacecom.badlogic.gdx.scenes.scene2d.utils.Layout
- Overrides:
layout
in classcom.badlogic.gdx.scenes.scene2d.ui.Widget
-
getAlignment
public int getAlignment()Gets the alignment for the text in this TextraLabel. This is a constant inAlign
.- Returns:
- the alignment used by this TextraLabel, as a constant from
Align
- See Also:
-
Align
-
setAlignment
public void setAlignment(int alignment) Sets the alignment for the text in this TextraLabel.- Parameters:
alignment
- a constant fromAlign
- See Also:
-
Align
-
getFont
-
setFont
Sets the font to the specified Font and then regenerates the layout usingFont.regenerateLayout(Layout)
. This is equivalent to callingsetFont(Font, boolean)
with true for regenerate. This won't regenerate the layout if the given font is equal to the current font for this TextraLabel.- Parameters:
font
- the non-null font to use for this TextraLabel
-
setFont
Just likesetFont(Font)
, except this only regenerates the layout ifregenerate
is true. (To contrast,setFont(Font)
always regenerates the layout.) This won't regenerate the layout if the given font is equal to the current font for this TextraLabel.- Parameters:
font
- the non-null font to use for this TextraLabelregenerate
- if true, the layout will be re-wrapped and its size re-calculated for the new font
-
setText
Changes the text in this TextraLabel to the given String, parsing any markup in it.- Parameters:
markupText
- a String that can contain Font markup
-
skipToTheEnd
By default, does nothing; this is overridden in TypingLabel to skip its text progression ahead. -
remove
public boolean remove()- Overrides:
remove
in classcom.badlogic.gdx.scenes.scene2d.Actor
-
setStage
public void setStage(com.badlogic.gdx.scenes.scene2d.Stage stage) Called by the framework when this actor or any ascendant is added to a group that is in the stage. This is overridden as public instead of protected because most of its usage in scene2d.ui code is not actually in inheriting classes, but in other classes in the same package. That's a problem here, so we make it public.- Overrides:
setStage
in classcom.badlogic.gdx.scenes.scene2d.Actor
- Parameters:
stage
- May be null if the actor or any ascendant is no longer in a stage.
-
setParent
protected void setParent(com.badlogic.gdx.scenes.scene2d.Group parent) Called by the framework when an actor is added to or removed from a group. This is overridden as public instead of protected because most of its usage in scene2d.ui code is not actually in inheriting classes, but in other classes in the same package. That's a problem here, so we make it public.- Overrides:
setParent
in classcom.badlogic.gdx.scenes.scene2d.Actor
- Parameters:
parent
- May be null if the actor has been removed from the parent.
-
toString
- Overrides:
toString
in classcom.badlogic.gdx.scenes.scene2d.Actor
-
getGlyph
public long getGlyph(int index) Gets a glyph from this label'slayout
, where a glyph is along
encoded howFont
uses it. In a TextraLabel, this is effectively equivalent toTypingLabel.getInWorkingLayout(int)
, but it may be different from that method in a TypingLabel, depending on word wrap.- Parameters:
index
- the 0-based index of the glyph to retrieve- Returns:
- the glyph, if it was found, or 16777215 (0xFFFFFF in hexadecimal) if the index was out of bounds
-
getMaxLines
public int getMaxLines()The maximum number ofLine
s this label can display.- Returns:
- the maximum number of
Line
objects this label can display
-
setMaxLines
public void setMaxLines(int maxLines) Sets the maximum number ofLine
s this Layout can display; this is always at least 1. For effectively unlimited lines, passInteger.MAX_VALUE
to this.- Parameters:
maxLines
- the limit for how many Line objects this Layout can display; always 1 or more
-
getEllipsis
Gets the ellipsis, which may be null, or may be a String that can be placed at the end of the text if its max lines are exceeded.- Returns:
- an ellipsis String or null
-
setEllipsis
Sets the ellipsis text, which replaces the last few glyphs if non-null and the text added would exceed thegetMaxLines()
of this label's layout. For the ellipsis to appear, this has to be called with a non-null String (often"..."
, or"…"
if the font supports it), andsetMaxLines(int)
needs to have been called with a small enough number, such as 1.- Parameters:
ellipsis
- a String for a Layout to end with if its max lines are exceeded, or null to avoid such truncation
-
substring
Gets a String from the layout of this label, made of only the char portions of the glyphs from start (inclusive) to end (exclusive). This can retrieve text from across multiple lines.- Parameters:
start
- inclusive start indexend
- exclusive end index- Returns:
- a String made of only the char portions of the glyphs from start to end
-
getLineHeight
public float getLineHeight(int index) Gets the height of the Line containing the glyph at the given index. If the index is out of bounds, this just returnsFont.cellHeight
.- Parameters:
index
- the 0-based index of the glyph to measure- Returns:
- the height of the Line containing the specified glyph
-