Class Text

java.lang.Object
org.openpatch.scratch.Text

public class Text extends Object
A text on the stage: plain, in a box, or in a speech or thought bubble. Every sprite has one for say and think; texts of your own are added to a stage with stage.add(text).
Text score = new Text("Score: 0", -200, 160, 0);
score.setLayer(Layer.UI);
stage.add(score);

Texts are laid out from the font file at their nominal size, in stage units, so where lines break and how large boxes and bubbles are never depends on the window. They are drawn at the window's resolution, in front of the foreground layer (or in the UI layer), and stay sharp.

  • Constructor Details

    • Text

      public Text()
      An empty plain text at (0, 0).
    • Text

      public Text(String text, double x, double y, double width)
      A plain text.
      Parameters:
      text - what it says
      x - x of its position
      y - y of its position (the first line's baseline)
      width - where lines wrap; 0 for no wrapping
    • Text

      public Text(String text, double x, double y, double width, TextStyle style)
      A text in a style.
      Parameters:
      text - what it says
      x - x of its position
      y - y of its position: the first baseline for plain text, the bottom edge of a box
      width - where lines wrap; 0 for no wrapping
      style - plain, box, speech or thought bubble
  • Method Details

    • useFont

      public static void useFont(String path)
      The font every text uses unless it sets its own, from now on. Without it, texts use Ubuntu Mono, or Jersey 10 in pixel-art mode.
      Parameters:
      path - a TTF or OTF file
    • useFont

      public static void useFont(String path, double pixelSize)
      A pixel font as default font, with the size at which one of its pixels is one stage pixel: for a pixel font whose outlines do not show its grid (most do: then useFont(String) is enough). In pixel-art mode sizes are rounded to whole pixels of it.
      Parameters:
      path - a TTF or OTF file
      pixelSize - the size at which one font pixel is one stage pixel
    • getDefaultFont

      public static String getDefaultFont()
      Returns:
      the default font file
    • getDefaultFontSize

      public static double getDefaultFontSize()
      Returns:
      the default font size
    • whenAddedToStage

      public void whenAddedToStage()
      Called when the text was added to a stage.
    • whenRemovedFromStage

      public void whenRemovedFromStage()
      Called when the text was removed from its stage.
    • getStage

      public Stage getStage()
      Returns:
      the stage the text is on (a sprite's text: the sprite's stage), or null
    • remove

      public void remove()
      Removes the text from its stage.
    • showText

      public void showText(String text)
      Shows the text with new words until it is changed or hidden.
      Parameters:
      text - what it says; an empty text hides a bubble
    • showText

      public void showText(String text, double seconds)
      Shows the text for a number of seconds of game time, then hides it.
      Parameters:
      text - what it says
      seconds - how long
    • getText

      public String getText()
      Returns:
      what the text says
    • show

      public void show()
      Shows the text.
    • hide

      public void hide()
      Hides the text.
    • isVisible

      public boolean isVisible()
      Returns:
      whether the text is shown
    • setPosition

      public void setPosition(double x, double y)
      Parameters:
      x - x of the position
      y - y of the position
    • setPosition

      public void setPosition(Vector2 position)
      Parameters:
      position - the position
    • getPosition

      public Vector2 getPosition()
      Returns:
      the position
    • setX

      public void setX(double x)
      Parameters:
      x - x of the position
    • getX

      public double getX()
      Returns:
      x of the position
    • setY

      public void setY(double y)
      Parameters:
      y - y of the position
    • getY

      public double getY()
      Returns:
      y of the position
    • setWidth

      public void setWidth(double width)
      Parameters:
      width - where lines wrap, in stage units; 0 for no wrapping
    • getWidth

      public double getWidth()
      Returns:
      where lines wrap; 0 for no wrapping
    • setStyle

      public void setStyle(TextStyle style)
      Parameters:
      style - plain, box, speech or thought bubble
    • getStyle

      public TextStyle getStyle()
      Returns:
      the style
    • setAlign

      public void setAlign(TextAlign align)
      Parameters:
      align - how lines sit relative to the position
    • getAlign

      public TextAlign getAlign()
      Returns:
      the alignment
    • setTextColor

      public void setTextColor(Color color)
      Parameters:
      color - the colour of the letters
    • setTextColor

      public void setTextColor(double red, double green, double blue)
      Parameters:
      red - red from 0 to 255
      green - green from 0 to 255
      blue - blue from 0 to 255
    • setBackgroundColor

      public void setBackgroundColor(Color color)
      Parameters:
      color - the fill of a box or bubble
    • setBackgroundColor

      public void setBackgroundColor(double red, double green, double blue)
      Parameters:
      red - red from 0 to 255
      green - green from 0 to 255
      blue - blue from 0 to 255
    • setStrokeColor

      public void setStrokeColor(Color color)
      Parameters:
      color - the outline of a box or bubble
    • setStrokeColor

      public void setStrokeColor(double red, double green, double blue)
      Parameters:
      red - red from 0 to 255
      green - green from 0 to 255
      blue - blue from 0 to 255
    • addFont

      public void addFont(String name, String path)
      Adds a font this text can switch to; the first one added is used.
      Parameters:
      name - the font's name
      path - the TTF or OTF file
    • switchFont

      public void switchFont(String name)
      Parameters:
      name - the name of a font added with addFont(String, String)
    • nextFont

      public void nextFont()
      Switches to the next font added, after the last to the first.
    • getCurrentFontName

      public String getCurrentFontName()
      Returns:
      the name of the font in use, or null for the default font
    • getCurrentFontIndex

      public int getCurrentFontIndex()
      Returns:
      the number of the font in use, counting from 0; -1 for the default font
    • goToFrontLayer

      public void goToFrontLayer()
      Draws this text in front of the other texts.
      Scratch Block:
      go to [front v] layer
    • goToBackLayer

      public void goToBackLayer()
      Draws this text behind the other texts.
      Scratch Block:
      go to [back v] layer
    • goLayersForwards

      public void goLayersForwards(int number)
      Parameters:
      number - how many texts to move in front of
      Scratch Block:
      go [forward v] (number) layers
    • goLayersBackwards

      public void goLayersBackwards(int number)
      Parameters:
      number - how many texts to move behind
      Scratch Block:
      go [backward v] (number) layers
    • setFont

      public void setFont(String path)
      Parameters:
      path - the font file for this text, or null for the default font
    • getFont

      public String getFont()
      Returns:
      the font file, or null for the default font
    • setTextSize

      public void setTextSize(double size)
      Parameters:
      size - the font size in stage units (in pixel-art mode rounded to whole font pixels when drawn)
    • getTextSize

      public double getTextSize()
      Returns:
      the font size, or the default
    • setLayer

      public void setLayer(Layer layer)
      Moves the text to another layer: Layer.UI keeps it in place when the camera moves; any other layer puts it into the world, in front of the foreground.
      Parameters:
      layer - the layer
    • getLayer

      public Layer getLayer()
      Returns:
      the layer