Package com.github.tommyettinger.textra
Class TypingLabel
java.lang.Object
com.badlogic.gdx.scenes.scene2d.Actor
com.badlogic.gdx.scenes.scene2d.ui.Widget
com.github.tommyettinger.textra.TextraLabel
com.github.tommyettinger.textra.TypingLabel
- All Implemented Interfaces:
com.badlogic.gdx.scenes.scene2d.utils.Layout
public class TypingLabel extends TextraLabel
An extension of
Label that progressively shows the text as if it was being typed in real time, and allows the
use of tokens in the following format: {TOKEN=PARAMETER}.-
Field Summary
Fields Modifier and Type Field Description protected com.badlogic.gdx.utils.Array<Effect>activeEffectsprotected booleandraggingintlastTouchedIndexThe global glyph index (as used bysetInWorkingLayout(int, long)) of the last glyph touched by the user.com.badlogic.gdx.utils.FloatArrayoffsetsContains two floats per glyph; even items are x offsets, odd items are y offsets.intoverIndexThe global glyph index (as used bysetInWorkingLayout(int, long)) of the last glyph hovered or dragged over by the user (including a click and mouse movement without a click).com.badlogic.gdx.utils.FloatArrayrotationsContains one float per glyph; each is a rotation in degrees to apply to that glyph (around its center).booleanselectableIf true, this label will allow clicking and dragging to select a range of text, iftrackingInputis also true.intselectionEndThe exclusive end index for the selected text, if there is a selection.intselectionStartThe inclusive start index for the selected text, if there is a selection.com.badlogic.gdx.utils.FloatArraysizingContains two floats per glyph, as size multipliers; even items apply to x, odd items apply to y.protected com.badlogic.gdx.utils.Array<com.github.tommyettinger.textra.TokenEntry>tokenEntriesbooleantrackingInputIf true, this will attempt to track which glyph the user's mouse or other pointer is over (seeoverIndexandlastTouchedIndex).protected LayoutworkingLayoutFields inherited from class com.github.tommyettinger.textra.TextraLabel
align, font, layout, storedText, style, wrap -
Constructor Summary
Constructors Constructor Description TypingLabel()TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle style)TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle style, Font replacementFont)TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin)TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, Font replacementFont)TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, java.lang.String styleName)TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, java.lang.String styleName, Font replacementFont)TypingLabel(java.lang.String text, Font font)TypingLabel(java.lang.String text, Font font, com.badlogic.gdx.graphics.Color color) -
Method Summary
Modifier and Type Method Description voidact(float delta)voidcancelSkipping()Cancels calls toskipToTheEnd().voidclearVariables()Removes all variables from this label.booleancopySelectedText()If this label isselectableand there is a selected range of text, this copies that range of text to the clipboard and returns true; otherwise, it returns false.voiddraw(com.badlogic.gdx.graphics.g2d.Batch batch, float parentAlpha)If your font usesFont.DistanceFieldType.SDForFont.DistanceFieldType.MSDF, then this has to do some extra work to use the appropriate shader.com.badlogic.gdx.graphics.ColorgetClearColor()Returns aColorinstance with the color to be used onCLEARCOLORtokens.java.lang.StringgetDefaultToken()Returns the default token being used in this label.java.lang.StringgetEllipsis()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.longgetFromIntermediate(int index)longgetInLayout(Layout layout, int index)java.lang.StringBuildergetIntermediateText()longgetInWorkingLayout(int index)floatgetLineHeight(int index)Gets the height of the Line containing the glyph at the given index, in the working layout.LinegetLineInLayout(Layout layout, int index)intgetMaxLines()The maximum number ofLines this label can display.java.lang.StringBuildergetOriginalText()Similar toLayout.toString(), but returns the original text with all the tokens unchanged.floatgetPrefHeight()floatgetPrefWidth()java.lang.StringgetSelectedText()If this label isselectableand there is a selected range of text, this returns that range of text; otherwise, it returns the empty string.TypingListenergetTypingListener()Returns theTypingListenerassociated with this label.com.badlogic.gdx.utils.ObjectMap<java.lang.String,java.lang.String>getVariables()Returns anObjectMapwith all the variable names and their respective replacement values.LayoutgetWorkingLayout()Returns the meant-for-internal-use-only Layout that is frequently changed as this label is displayed.booleanhasEnded()Returns whether this label's char progression has ended.voidinsertInLayout(Layout layout, int index, long newGlyph)voidinsertInLayout(Layout layout, int index, java.lang.CharSequence text)booleanisPaused()Returns whether this label is paused.booleanisSelectable()Returns true if and only ifselectableis true andtrackingInputis true; otherwise false.booleanisSkipping()Returns whether this label is currently skipping its typing progression all the way to the end.voidlayout()intlength()Gets the length in glyphs of the working layout (what is displayed).voidparseTokens()Parses all tokens of this label.voidpause()Pauses this label's character progression.booleanremove()voidrestart()Restarts this label with the original text and starts the char progression right away.voidrestart(java.lang.String newText)Restarts this label with the given text and starts the char progression right away.protected voidrestoreOriginalText()Restores the original text with all tokens unchanged to this label.voidresume()Resumes this label's character progression.protected voidsaveOriginalText(java.lang.CharSequence text)Copies the content ofgetOriginalText()to theStringBuildercontaining the original text with all tokens unchanged.voidsetDefaultToken(java.lang.String defaultToken)Sets the default token being used in this label.voidsetEllipsis(java.lang.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 working layout.voidsetInLayout(Layout layout, int index, long newGlyph)voidsetIntermediateText(java.lang.CharSequence text, boolean modifyOriginalText, boolean restart)voidsetInWorkingLayout(int index, long newGlyph)voidsetMaxLines(int maxLines)Sets the maximum number ofLines this Layout can display; this is always at least 1.TypingLabelsetSelectable(boolean selectable)voidsetSize(float width, float height)voidsetText(java.lang.String newText)Modifies the text of this label.protected voidsetText(java.lang.String newText, boolean modifyOriginalText)Sets the text of this label.protected voidsetText(java.lang.String newText, boolean modifyOriginalText, boolean restart)Sets the text of this label.voidsetTypingListener(TypingListener listener)Sets theTypingListenerassociated with this label, ornullto remove the current one.voidsetVariable(java.lang.String var, java.lang.String value)Registers a variable and its respective replacement value to this label.voidsetVariables(com.badlogic.gdx.utils.ObjectMap<java.lang.String,java.lang.String> variableMap)Registers a set of variables and their respective replacement values to this label.voidsetVariables(java.util.Map<java.lang.String,java.lang.String> variableMap)Registers a set of variables and their respective replacement values to this label.voidskipToTheEnd()Skips the char progression to the end, showing the entire label.voidskipToTheEnd(boolean ignoreEvents)Skips the char progression to the end, showing the entire label.voidskipToTheEnd(boolean ignoreEvents, boolean ignoreEffects)Skips the char progression to the end, showing the entire label.java.lang.Stringsubstring(int start, int end)Gets a String from the working layout of this label, made of only the char portions of the glyphs from start (inclusive) to end (exclusive).java.lang.StringtoString()voidtriggerEvent(java.lang.String event, boolean always)Triggers an event with the given String name.Methods inherited from class com.github.tommyettinger.textra.TextraLabel
getAlignment, getFont, getGlyph, isWrap, setAlignment, setFont, setFont, setParent, setStage, setWrap, useIntegerPositionsMethods inherited from class com.badlogic.gdx.scenes.scene2d.ui.Widget
getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, invalidate, invalidateHierarchy, needsLayout, pack, setFillParent, setLayoutEnabled, sizeChanged, validateMethods inherited from class com.badlogic.gdx.scenes.scene2d.Actor
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, toFrontMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Field Details
-
tokenEntries
protected final com.badlogic.gdx.utils.Array<com.github.tommyettinger.textra.TokenEntry> tokenEntries -
workingLayout
-
offsets
public final com.badlogic.gdx.utils.FloatArray offsetsContains two floats per glyph; even items are x offsets, odd items are y offsets. -
sizing
public final com.badlogic.gdx.utils.FloatArray sizingContains two floats per glyph, as size multipliers; even items apply to x, odd items apply to y. -
rotations
public final com.badlogic.gdx.utils.FloatArray rotationsContains one float per glyph; each is a rotation in degrees to apply to that glyph (around its center). -
trackingInput
public boolean trackingInputIf true, this will attempt to track which glyph the user's mouse or other pointer is over (seeoverIndexandlastTouchedIndex). -
selectable
public boolean selectableIf true, this label will allow clicking and dragging to select a range of text, iftrackingInputis also true. This does not allow the text to be edited unless so implemented by another class. If text can be selected, then you can usegetSelectedText()to get the selected String, orcopySelectedText()to copy that text directly. To copy automatically, use a listener that checksTypingListener.event(String), and when the event String is"*SELECTED", that means a click-and-drag selected a range of text in this label, and you can do what you want with the selected text (such as callcopySelectedText()). -
lastTouchedIndex
public int lastTouchedIndexThe global glyph index (as used bysetInWorkingLayout(int, long)) of the last glyph touched by the user. If nothing in this TypingLabel was touched during the last call todraw(Batch, float), then this will be either -1 (if the last touch was, roughly, before the first glyph) or -2 (if the last touch was after the last glyph). This only changes when a click, tap, or other touch was just issued. -
overIndex
public int overIndexThe global glyph index (as used bysetInWorkingLayout(int, long)) of the last glyph hovered or dragged over by the user (including a click and mouse movement without a click). If nothing in this TypingLabel was moved over during the last call todraw(Batch, float), then this will be -1 . This changes whenever the mouse or a pointer is over a glyph in this. -
selectionStart
public int selectionStartThe inclusive start index for the selected text, if there is a selection. This should be -1 if there is no selection, or sometimes -2 if the selection went past the end of the text. This is essentially interchangeable withselectionEnd; as long as they are different, it doesn't matter which is higher or lower. -
selectionEnd
public int selectionEndThe exclusive end index for the selected text, if there is a selection. This should be -1 if there is no selection, or sometimes -2 if the selection went past the end of the text. This is essentially interchangeable withselectionStart; as long as they are different, it doesn't matter which is higher or lower. -
dragging
protected boolean dragging -
activeEffects
-
-
Constructor Details
-
TypingLabel
public TypingLabel() -
TypingLabel
public TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin) -
TypingLabel
public TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, Font replacementFont) -
TypingLabel
public TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, java.lang.String styleName) -
TypingLabel
public TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Skin skin, java.lang.String styleName, Font replacementFont) -
TypingLabel
public TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle style) -
TypingLabel
public TypingLabel(java.lang.String text, com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle style, Font replacementFont) -
TypingLabel
-
TypingLabel
-
-
Method Details
-
setText
public void setText(java.lang.String newText)Modifies the text of this label. If the char progression is already running, it's highly recommended to userestart(String)instead.- Overrides:
setTextin classTextraLabel- Parameters:
newText- a String that can contain Font markup
-
setText
protected void setText(java.lang.String newText, boolean modifyOriginalText)Sets the text of this label.- Parameters:
modifyOriginalText- Flag determining if the original text should be modified as well. Iffalse, only the display text is changed while the original text is untouched. Iftrue, then this runsParser.preprocess(CharSequence)on the text, which should only generally be run once per original text.- See Also:
restart(String)
-
setText
protected void setText(java.lang.String newText, boolean modifyOriginalText, boolean restart)Sets the text of this label.- Parameters:
modifyOriginalText- Flag determining if the original text should be modified as well. Iffalse, only the display text is changed while the original text is untouched.restart- Whether this label should restart. Defaults to true.- See Also:
restart(String)
-
getOriginalText
public java.lang.StringBuilder getOriginalText()Similar toLayout.toString(), but returns the original text with all the tokens unchanged. -
saveOriginalText
protected void saveOriginalText(java.lang.CharSequence text)Copies the content ofgetOriginalText()to theStringBuildercontaining the original text with all tokens unchanged. -
restoreOriginalText
protected void restoreOriginalText()Restores the original text with all tokens unchanged to this label. Make sure to callparseTokens()to parse the tokens again. -
getTypingListener
Returns theTypingListenerassociated with this label. May benull. -
setTypingListener
Sets theTypingListenerassociated with this label, ornullto remove the current one. -
getClearColor
public com.badlogic.gdx.graphics.Color getClearColor()Returns aColorinstance with the color to be used onCLEARCOLORtokens. Modify this instance to change the token color. Default value is specified byTypingConfig.- See Also:
TypingConfig.DEFAULT_CLEAR_COLOR
-
getDefaultToken
public java.lang.String getDefaultToken()Returns the default token being used in this label. Defaults to empty string. -
setDefaultToken
public void setDefaultToken(java.lang.String defaultToken)Sets the default token being used in this label. This token will be used before the label's text, and after each {RESET} call. Useful if you want a certain token to be active at all times without having to type it all the time. -
parseTokens
public void parseTokens()Parses all tokens of this label. Use this after setting the text and any variables that should be replaced. -
skipToTheEnd
public void skipToTheEnd()Skips the char progression to the end, showing the entire label. Useful for when users don't want to wait for too long. Ignores all subsequent events by default. This callsact(float)with a delta ofFloat.MIN_VALUE, which allows the text to be skipped ahead without noticeably changing anything time-based.- Overrides:
skipToTheEndin classTextraLabel
-
skipToTheEnd
public void skipToTheEnd(boolean ignoreEvents)Skips the char progression to the end, showing the entire label. Useful for when users don't want to wait for too long. This callsact(float)with a delta ofFloat.MIN_VALUE, which allows the text to be skipped ahead without noticeably changing anything time-based.- Parameters:
ignoreEvents- Iftrue, skipped events won't be reported to the listener.
-
skipToTheEnd
public void skipToTheEnd(boolean ignoreEvents, boolean ignoreEffects)Skips the char progression to the end, showing the entire label. Useful for when users don't want to wait for too long. This callsact(float)with a delta ofFloat.MIN_VALUE, which allows the text to be skipped ahead without noticeably changing anything time-based.- Parameters:
ignoreEvents- Iftrue, skipped events won't be reported to the listener.ignoreEffects- Iftrue, all text effects will be instantly cancelled.
-
cancelSkipping
public void cancelSkipping()Cancels calls toskipToTheEnd(). Useful if you need to restore the label's normal behavior at some event after skipping. -
isSkipping
public boolean isSkipping()Returns whether this label is currently skipping its typing progression all the way to the end. This is only true if skipToTheEnd is called. -
isPaused
public boolean isPaused()Returns whether this label is paused. -
pause
public void pause()Pauses this label's character progression. -
resume
public void resume()Resumes this label's character progression. -
hasEnded
public boolean hasEnded()Returns whether this label's char progression has ended. -
restart
public void restart()Restarts this label with the original text and starts the char progression right away. All tokens are automatically parsed. -
restart
public void restart(java.lang.String newText)Restarts this label with the given text and starts the char progression right away. All tokens are automatically parsed. -
getVariables
public com.badlogic.gdx.utils.ObjectMap<java.lang.String,java.lang.String> getVariables()Returns anObjectMapwith all the variable names and their respective replacement values. -
setVariable
public void setVariable(java.lang.String var, java.lang.String value)Registers a variable and its respective replacement value to this label. -
setVariables
public void setVariables(com.badlogic.gdx.utils.ObjectMap<java.lang.String,java.lang.String> variableMap)Registers a set of variables and their respective replacement values to this label. -
setVariables
public void setVariables(java.util.Map<java.lang.String,java.lang.String> variableMap)Registers a set of variables and their respective replacement values to this label. -
clearVariables
public void clearVariables()Removes all variables from this label. -
act
public void act(float delta)- Overrides:
actin classcom.badlogic.gdx.scenes.scene2d.Actor
-
remove
public boolean remove()- Overrides:
removein classTextraLabel
-
setSize
public void setSize(float width, float height)- Overrides:
setSizein classTextraLabel
-
getPrefWidth
public float getPrefWidth()- Specified by:
getPrefWidthin interfacecom.badlogic.gdx.scenes.scene2d.utils.Layout- Overrides:
getPrefWidthin classTextraLabel
-
getPrefHeight
public float getPrefHeight()- Specified by:
getPrefHeightin interfacecom.badlogic.gdx.scenes.scene2d.utils.Layout- Overrides:
getPrefHeightin classTextraLabel
-
layout
public void layout()- Specified by:
layoutin interfacecom.badlogic.gdx.scenes.scene2d.utils.Layout- Overrides:
layoutin classTextraLabel
-
draw
public void draw(com.badlogic.gdx.graphics.g2d.Batch batch, float parentAlpha)If your font usesFont.DistanceFieldType.SDForFont.DistanceFieldType.MSDF, then this has to do some extra work to use the appropriate shader. IfFont.enableShader(Batch)was called before rendering a group of TypingLabels, then they will try to share one Batch; otherwise this will change the shader to render SDF or MSDF, then change it back at the end of each draw() call.- Overrides:
drawin classTextraLabel- Parameters:
batch- probably should be a SpriteBatchparentAlpha- the alpha of the parent container, or 1.0f if there is none
-
toString
public java.lang.String toString()- Overrides:
toStringin classTextraLabel
-
getSelectedText
public java.lang.String getSelectedText()If this label isselectableand there is a selected range of text, this returns that range of text; otherwise, it returns the empty string.- Returns:
- the currently selected text, or the empty string if none is or can be selected
-
copySelectedText
public boolean copySelectedText()If this label isselectableand there is a selected range of text, this copies that range of text to the clipboard and returns true; otherwise, it returns false.- Returns:
- true if text was copied, or false if the clipboard hasn't received any text
-
setIntermediateText
public void setIntermediateText(java.lang.CharSequence text, boolean modifyOriginalText, boolean restart) -
getIntermediateText
public java.lang.StringBuilder getIntermediateText() -
getInLayout
-
getInWorkingLayout
public long getInWorkingLayout(int index) -
getWorkingLayout
Returns the meant-for-internal-use-only Layout that is frequently changed as this label is displayed. The working layout may be useful to have, even if treated as read-only, so it is exposed here. Still, be very careful with this method and the Layout it returns. The working layout is the one that gets shown, wherelayout()is used as the ideal text before wrapping or other requirements edit it.- Returns:
- the mostly-internal working layout, which is the layout that gets displayed.
-
getMaxLines
public int getMaxLines()The maximum number ofLines this label can display.- Overrides:
getMaxLinesin classTextraLabel- Returns:
- the maximum number of
Lineobjects this label can display
-
setMaxLines
public void setMaxLines(int maxLines)Sets the maximum number ofLines this Layout can display; this is always at least 1. For effectively unlimited lines, passInteger.MAX_VALUEto this.- Overrides:
setMaxLinesin classTextraLabel- Parameters:
maxLines- the limit for how many Line objects this Layout can display; always 1 or more
-
getEllipsis
public java.lang.String 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.- Overrides:
getEllipsisin classTextraLabel- Returns:
- an ellipsis String or null
-
setEllipsis
public void setEllipsis(java.lang.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 working 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.- Overrides:
setEllipsisin classTextraLabel- Parameters:
ellipsis- a String for a Layout to end with if its max lines are exceeded, or null to avoid such truncation
-
substring
public java.lang.String substring(int start, int end)Gets a String from the working 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.- Overrides:
substringin classTextraLabel- Parameters:
start- inclusive start indexend- exclusive end index- Returns:
- a String made of only the char portions of the glyphs from start to end
-
getLineInLayout
-
getLineHeight
public float getLineHeight(int index)Gets the height of the Line containing the glyph at the given index, in the working layout. If the index is out of bounds, this just returnsFont.cellHeight.- Overrides:
getLineHeightin classTextraLabel- Parameters:
index- the 0-based index of the glyph to measure- Returns:
- the height of the Line containing the specified glyph
-
getFromIntermediate
public long getFromIntermediate(int index) -
setInLayout
-
insertInLayout
-
insertInLayout
-
setInWorkingLayout
public void setInWorkingLayout(int index, long newGlyph) -
length
public int length()Gets the length in glyphs of the working layout (what is displayed).- Returns:
- the length in glyphs of the working layout (what is displayed)
-
triggerEvent
public void triggerEvent(java.lang.String event, boolean always)Triggers an event with the given String name. Ifalwaysis true, this will trigger the event even if the typing animation has already ended. This requires aTypingListenerto be set.- Parameters:
event- the event name to triggeralways- if true, the event will be triggered even if the animation has finished.
-
isSelectable
public boolean isSelectable()Returns true if and only ifselectableis true andtrackingInputis true; otherwise false.- Returns:
- whether the text of this label is selectable
-
setSelectable
If giventrue, this makes the text of this labelselectableand ensurestrackingInputis true. Otherwise, this makes the label not-selectable and doesn't changetrackingInput. The application should usually be set to copy the selected text usingcopySelectedText()when the user expects it to be copied. Often, aTypingListenerthat checks for the event"*SELECTED"works.- Parameters:
selectable- true if the text of this label should be selectable- Returns:
- this, for chaining
-