Class TextPanel
public class TextPanel extends Object
SquidPanel
) as in these examples (no scrolling first, then with a
scroll bar):
It supports vertical scrolling, i.e. it'll put a vertical scrollbar if there's too much text to display. This class does a lot of stuff, you typically only have to provide the textures for the scrollbars and the scroll knobs (see example below).
A typical usage of this class is as follows:
final TextPanel<Color> tp = new TextPanel<>(new GDXMarkup(), font);
tp.init(screenWidth, screenHeight, text); <- first 2 params: for fullscreen
final ScrollPane sp = tp.getScrollPane();
sp.setScrollPaneStyle(new ScrollPaneStyle(...)); <- set textures
stage.addActor(sp);
stage.setScrollFocus(sp);
stage.draw();
This class shares what ScrollPane
does (knobs, handling of the wheel).
Drawing the result of getScrollPane()
will set the shader of batch
if using a distance field or MSDF
font and the shader is currently not configured for such a font; it does not reset the shader to the default so that
multiple Actors can all use the same shader and so specific extra glyphs or other items can be rendered after calling
draw(). If you need to draw both a distance field font and full-color art, you should set the shader on the Batch to
null when you want to draw full-color art, and end the Batch between drawing this object and the other art.
- Author:
- smelC
- See Also:
A libGDX widget for general scrolling through only the visible part of a large widget
-
Field Summary
Fields Modifier and Type Field Description com.badlogic.gdx.graphics.Color
backgroundColor
The color to use to paint the background (outside buttons) usingShapeRenderer
.com.badlogic.gdx.graphics.Color
borderColor
The color of the border around this panel, if any.float
borderSize
The size of the border, if anyUIUtil.CornerStyle
borderStyle
protected com.badlogic.gdx.graphics.g2d.BitmapFont
font
protected com.badlogic.gdx.scenes.scene2d.ui.ScrollPane
scrollPane
protected TextCellFactory
tcf
ArrayList<CharSequence>
text
The text to displayprotected com.badlogic.gdx.scenes.scene2d.Actor
textActor
The actor whose size is adjusted to the text. -
Constructor Summary
Constructors Constructor Description TextPanel(com.badlogic.gdx.graphics.g2d.BitmapFont font)
The text to display MUST be set later on withinit(float, float, Collection)
.TextPanel(TextCellFactory font)
The text to display MUST be set later on withinit(float, float, Collection)
(which can't be updated) orinitShared(float, float, ArrayList)
(which reflects changes in the given ArrayList). -
Method Summary
Modifier and Type Method Description protected com.badlogic.gdx.graphics.glutils.ShapeRenderer
buildRenderer()
void
dispose()
void
drawBorder(com.badlogic.gdx.graphics.g2d.Batch batch)
Draws the border.TextCellFactory
getFont()
protected com.badlogic.gdx.graphics.glutils.ShapeRenderer
getRenderer()
com.badlogic.gdx.scenes.scene2d.ui.ScrollPane
getScrollPane()
com.badlogic.gdx.scenes.scene2d.Actor
getTextActor()
ArrayList<String>
getTypesetText()
void
init(float width, float maxHeight, com.badlogic.gdx.graphics.Color color, String... text)
void
init(float width, float maxHeight, Collection<? extends IColoredString<com.badlogic.gdx.graphics.Color>> coloredText)
This method sets the sizes ofscrollPane
andtextActor
.void
initShared(float width, float maxHeight, ArrayList<CharSequence> text)
This method sets the sizes ofscrollPane
andtextActor
, and shares a direct reference totext
so changes to that ArrayList will also be picked up here and rendered.protected void
prepareText()
Updates the text this will show based on the current contents of the ArrayList of IColoredString values that may be shared due toinitShared(float, float, ArrayList)
, then resizes thegetTextActor()
to fit the current text and lays out thegetScrollPane()
to match.void
scroll(float downDistance)
Scrolls the scroll pane this holds down by some number of rows (which may be fractional, and may be negative to scroll up).void
scrollToEdge(boolean goToTop)
If the parameter is true, scrolls to the top of this scroll pane; otherwise scrolls to the bottom.void
setFont(com.badlogic.gdx.graphics.g2d.BitmapFont font)
Sets the font to use.void
setFont(TextCellFactory font)
Sets the font to use.protected void
yScrollingCallback(boolean required)
Callback done to do stuff according to whether y-scrolling is required
-
Field Details
-
backgroundColor
The color to use to paint the background (outside buttons) usingShapeRenderer
. Ornull
to disable background coloring. -
borderColor
The color of the border around this panel, if any. If set, it'll be rendered usingShapeRenderer
andborderStyle
. -
borderSize
The size of the border, if any -
borderStyle
-
font
-
tcf
-
text
The text to display -
scrollPane
-
textActor
The actor whose size is adjusted to the text. When scrolling is required, it is bigger thanscrollPane
.
-
-
Constructor Details
-
TextPanel
The text to display MUST be set later on withinit(float, float, Collection)
.- Parameters:
font
- The font to use. It can be set later usingsetFont(BitmapFont)
, but it MUST be set before drawing this panel.
-
TextPanel
The text to display MUST be set later on withinit(float, float, Collection)
(which can't be updated) orinitShared(float, float, ArrayList)
(which reflects changes in the given ArrayList).- Parameters:
font
- A TextCellFactory, typically holding a distance field font ("stretchable" or "crisp" in DefaultResources). This won't force glyphs into same-size cells, despite the name.
-
-
Method Details
-
setFont
Sets the font to use. This method should be called once beforeinit(float, float, Collection)
if the font wasn't given at creation-time.- Parameters:
font
- The font to use as a BitmapFont.
-
setFont
Sets the font to use. This method should be called once beforeinit(float, float, Collection)
if the font wasn't given at creation-time.- Parameters:
font
- The font to use as a TextCellFactory.
-
init
public void init(float width, float maxHeight, Collection<? extends IColoredString<com.badlogic.gdx.graphics.Color>> coloredText)This method sets the sizes ofscrollPane
andtextActor
. This method MUST be called before rendering.- Parameters:
maxHeight
- The maximum height that the scrollpane can take (equal or smaller than the height of the text actor).width
- The width of the scrollpane and the text actor.coloredText
- any Collection of IColoredString that use Color or a subclass as their color type
-
initShared
This method sets the sizes ofscrollPane
andtextActor
, and shares a direct reference totext
so changes to that ArrayList will also be picked up here and rendered. This method MUST be called before rendering.- Parameters:
maxHeight
- The maximum height that the scrollpane can take (equal or smaller than the height of the text actor).width
- The width of the scrollpane and the text actor.text
- an ArrayList of CharSequence that will be used directly by this TextPanel (changes to the ArrayList will show up in the TextPanel)
-
init
-
drawBorder
Draws the border. You have to call this method manually, because the border is outside the actor and hence should be drawn at the very end, otherwise it can get overwritten by UI elements.- Parameters:
batch
-
-
getTypesetText
- Returns:
- The text to draw, without color information present in
text
.
-
prepareText
Updates the text this will show based on the current contents of the ArrayList of IColoredString values that may be shared due toinitShared(float, float, ArrayList)
, then resizes thegetTextActor()
to fit the current text and lays out thegetScrollPane()
to match. Called in the text actor's draw() method. -
scroll
Scrolls the scroll pane this holds down by some number of rows (which may be fractional, and may be negative to scroll up). This is not a smooth scroll, and will not be animated.- Parameters:
downDistance
- The distance in rows to scroll down, which can be negative to scroll up instead
-
scrollToEdge
If the parameter is true, scrolls to the top of this scroll pane; otherwise scrolls to the bottom. This is not a smooth scroll, and will not be animated.- Parameters:
goToTop
- If true, will scroll to the top edge; if false, will scroll to the bottom edge.
-
getScrollPane
- Returns:
- The
ScrollPane
containinggetTextActor()
.
-
getTextActor
- Returns:
- The
Actor
where the text is drawn. It may be bigger thangetScrollPane()
.
-
getFont
- Returns:
- The font used, if set, as a TextCellFactory (one is always created even if only given a BitmapFont).
-
dispose
-
yScrollingCallback
Callback done to do stuff according to whether y-scrolling is required- Parameters:
required
- Whether y scrolling is required.
-
buildRenderer
- Returns:
- A fresh renderer.
-
getRenderer
- Returns:
- The renderer to use.
-