Package com.github.tommyettinger.textra
Class Layout
java.lang.Object
com.github.tommyettinger.textra.Layout
- All Implemented Interfaces:
com.badlogic.gdx.utils.Pool.Poolable
public class Layout
extends java.lang.Object
implements com.badlogic.gdx.utils.Pool.Poolable
A replacement for libGDX's GlyphLayout, more or less; stores one or more (possibly empty)
Lines of text,
which can use color and style markup from Font, and can be drawn with
Font.drawGlyphs(Batch, Layout, float, float, int). This is a Poolable class, and you can obtain a Layout with
Layout.POOL.obtain() followed by setting the font, or just using a constructor.-
Field Summary
Fields Modifier and Type Field Description protected booleanatLimitprotected floatbaseColorprotected java.lang.Stringellipsisprotected Fontfontprotected com.badlogic.gdx.utils.Array<Line>linesprotected intmaxLinesstatic com.badlogic.gdx.utils.Pool<Layout>POOLprotected floattargetWidth -
Constructor Summary
-
Method Summary
Modifier and Type Method Description Layoutadd(long glyph)Adds alongglyph as processed byFontto store color and style info with the char.java.lang.StringBuilderappendInto(java.lang.StringBuilder sb)Primarily used bytoString(), but can be useful if you want to append many Layouts into a StringBuilder.java.lang.StringBuilderappendIntoDirect(java.lang.StringBuilder sb)Can be useful if you want to append many Layouts into a StringBuilder.Layoutclear()Layoutfont(Font font)One of the ways to set the font on a Layout; this one returns this Layout for chaining.floatgetBaseColor()Gets the base color of the Layout, as the float bits of a Color.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.FontgetFont()floatgetHeight()LinegetLine(int i)Gets a Line from this by its index.intgetMaxLines()The maximum number ofLines this Layout can contain.floatgetTargetWidth()floatgetWidth()LineinsertLine(int index)intlines()LinepeekLine()LinepushLine()voidreset()Resets the object for reuse.voidsetBaseColor(float baseColor)Sets the base color of the Layout; this is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted.voidsetBaseColor(com.badlogic.gdx.graphics.Color baseColor)Sets the base color of the Layout; this is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted.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 Layout.voidsetFont(Font font)One of the ways to set the font on a Layout; this is a traditional setter.voidsetMaxLines(int maxLines)Sets the maximum number ofLines this Layout can contain; this is always at least 1.LayoutsetTargetWidth(float targetWidth)java.lang.StringtoString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Field Details
-
POOL
-
font
-
lines
-
maxLines
protected int maxLines -
atLimit
protected boolean atLimit -
ellipsis
protected java.lang.String ellipsis -
targetWidth
protected float targetWidth -
baseColor
protected float baseColor
-
-
Constructor Details
-
Layout
public Layout() -
Layout
-
Layout
-
-
Method Details
-
font
One of the ways to set the font on a Layout; this one returns this Layout for chaining.- Parameters:
font- the font to use- Returns:
- this Layout, for chaining
-
getFont
-
setFont
One of the ways to set the font on a Layout; this is a traditional setter.- Parameters:
font- the font to use
-
add
Adds alongglyph as processed byFontto store color and style info with the char.- Parameters:
glyph- usually produced byFontto store color and style info with the char- Returns:
- this Layout, for chaining
-
clear
-
getWidth
public float getWidth() -
getHeight
public float getHeight() -
lines
public int lines() -
getLine
Gets a Line from this by its index.- Parameters:
i- index for the Line to fetch; must be at least 0 and less thanlines().- Returns:
- the Line at the given index
-
peekLine
-
pushLine
-
insertLine
-
getTargetWidth
public float getTargetWidth() -
setTargetWidth
-
getBaseColor
public float getBaseColor()Gets the base color of the Layout, as the float bits of a Color. The base color is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted. You can fill a Color object with this value usingColor.abgr8888ToColor(Color, float)(it modifies the Color you give it).- Returns:
- the base color of the Layout, as float bits
-
setBaseColor
public void setBaseColor(float baseColor)Sets the base color of the Layout; this is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted. This takes the color as a primitive float, which you can get from a Color object withColor.toFloatBits(), or in some cases from existing data produced byFont.- Parameters:
baseColor- the float bits of a Color, as obtainable viaColor.toFloatBits()
-
setBaseColor
public void setBaseColor(com.badlogic.gdx.graphics.Color baseColor)Sets the base color of the Layout; this is what font color will be used immediately after resetting formatting with[], as well as the initial color used by text that hasn't been formatted. If the given Color is null, this treats it as white.- Parameters:
baseColor- a Color to use for text that hasn't been formatted; if null, will be treated as white
-
getMaxLines
public int getMaxLines()The maximum number ofLines this Layout can contain.- Returns:
- the maximum number of
Lineobjects this Layout can contain
-
setMaxLines
public void setMaxLines(int maxLines)Sets the maximum number ofLines this Layout can contain; this is always at least 1.- Parameters:
maxLines- the limit for how many Line objects this Layout can contain; 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.- 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 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
-
reset
public void reset()Resets the object for reuse. The font is nulled, but the lines are freed, cleared, and then one blank line is re-added to lines so it can be used normally later.- Specified by:
resetin interfacecom.badlogic.gdx.utils.Pool.Poolable
-
appendIntoDirect
public java.lang.StringBuilder appendIntoDirect(java.lang.StringBuilder sb)Can be useful if you want to append many Layouts into a StringBuilder. This does not treat character u0002 any differently from other characters, which is where it differs fromappendInto(StringBuilder). This does not add or remove newlines from the Layout's contents, and can produce line breaks if they appear.- Parameters:
sb- a non-null StringBuilder from the JDK- Returns:
- sb, for chaining
-
appendInto
public java.lang.StringBuilder appendInto(java.lang.StringBuilder sb)Primarily used bytoString(), but can be useful if you want to append many Layouts into a StringBuilder. This treats instances of the character u0002 as'[', as the library does internally, instead of potentially printing a gibberish character. This does not add or remove newlines from the Layout's contents, and can produce line breaks if they appear.- Parameters:
sb- a non-null StringBuilder from the JDK- Returns:
- sb, for chaining
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-