Class TextPanel

java.lang.Object
squidpony.squidgrid.gui.gdx.TextPanel

public class TextPanel
extends Object
A panel to display some text using libgdx directly (i.e. without using 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 Details

  • Constructor Details

  • Method Details

    • setFont

      public void setFont​(com.badlogic.gdx.graphics.g2d.BitmapFont font)
      Sets the font to use. This method should be called once before init(float, float, Collection) if the font wasn't given at creation-time.
      Parameters:
      font - The font to use as a BitmapFont.
    • setFont

      public void setFont​(TextCellFactory font)
      Sets the font to use. This method should be called once before init(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 of scrollPane and textActor. 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

      public void initShared​(float width, float maxHeight, ArrayList<CharSequence> text)
      This method sets the sizes of scrollPane and textActor, and shares a direct reference to text 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

      public void init​(float width, float maxHeight, com.badlogic.gdx.graphics.Color color, String... text)
    • drawBorder

      public void drawBorder​(com.badlogic.gdx.graphics.g2d.Batch batch)
      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

      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 to initShared(float, float, ArrayList), then resizes the getTextActor() to fit the current text and lays out the getScrollPane() to match. Called in the text actor's draw() method.
    • scroll

      public 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). 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

      public void scrollToEdge​(boolean goToTop)
      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

      public com.badlogic.gdx.scenes.scene2d.ui.ScrollPane getScrollPane()
      Returns:
      The ScrollPane containing getTextActor().
    • getTextActor

      public com.badlogic.gdx.scenes.scene2d.Actor getTextActor()
      Returns:
      The Actor where the text is drawn. It may be bigger than getScrollPane().
    • getFont

      Returns:
      The font used, if set, as a TextCellFactory (one is always created even if only given a BitmapFont).
    • dispose

      public void dispose()
    • yScrollingCallback

      protected void yScrollingCallback​(boolean required)
      Callback done to do stuff according to whether y-scrolling is required
      Parameters:
      required - Whether y scrolling is required.
    • buildRenderer

      protected com.badlogic.gdx.graphics.glutils.ShapeRenderer buildRenderer()
      Returns:
      A fresh renderer.
    • getRenderer

      protected com.badlogic.gdx.graphics.glutils.ShapeRenderer getRenderer()
      Returns:
      The renderer to use.