org.jrabbit.base.graphics.skins.text
Class TextSkin

java.lang.Object
  extended by org.jrabbit.base.graphics.skins.text.TextSkin
All Implemented Interfaces:
Renderable, Skin, Dimensioned
Direct Known Subclasses:
RenderedTextSkin

public class TextSkin
extends java.lang.Object
implements Skin

A TextSkin is a Skin that uses one of the currently loaded Fonts to render a dynamic string of text. The text displayed can be formatted into paragraphs (by default this is turned on). It should be noted that unlike other skins, TextSkin is rendered from its top left corner, not from its center. NOTE: Rendering text is expensive, and very little is done to accelerate font rendering in this class. If the Font used to render text is accelerated, this Skin will benefit, but beyond that this class is relatively slow for large amounts of text. On the plus side, there is not much additional overhead for rendering changing text.

Author:
Chris Molini

Field Summary
protected  FontPointer fontPointer
          The FontPointer used to obtain and set the Font used to render text.
protected  boolean format
          Whether or not to push the text into paragraph format.
protected  java.lang.String[] formattedText
          The text that has been passed through the Font for formatting.
protected  int formatWidth
          If formatting is enabled, this indicated the maximum width of the paragraph.
protected  float height
          The base height of the text being rendered.
protected  boolean indent
          If formatting is enabled, this marks whether or not to indent the lines of text that are retrieved.
protected  boolean reformat
          Whether or not the TextSkin needs to reformat its text.
protected  java.lang.String text
          The base text to render.
protected  float width
          The base width of the text being rendered.
 
Constructor Summary
TextSkin(java.lang.String text)
          Creates a TextSkin that uses the default font to render the supplied text.
TextSkin(java.lang.String text, java.lang.String font)
          Creates a TextSkin that uses the indicated font to render the supplied text.
 
Method Summary
 void checkFormat()
          Makes sure that the text to render is properly formatted.
protected  void defaultFormattting()
          This method is used to obtain the initial formatting the TextSkin will use.
 void enableFormatting(boolean format)
          Sets whether or not to format the base text into paragraphs.
 void enableIndent(boolean indent)
          Sets whether or not to indent formatted text.
 void flagReformat()
          Flags the TextSprite that it needs to reformat on the next render.
 Font font()
          Obtains a reference to the active Font.
protected  void format(Font font)
          This calculates the list of Strings to use for rendering.
 java.lang.String[] formattedText()
          Accesses the processed, formatted Strings of text that are actually rendered.
 boolean formatting()
          Learns if formatting is enabled.
 int formatWidth()
          Learns the current formatting width.
 float height()
          Learns about the dimensions of the TextSkin.
 boolean indenting()
          Learns if indenting is enabled.
 void reformat()
          This method forces a reformat of the text to render.
 void render()
          First, checks to see if the TextSkin needs to update its formatting; then renders the formatted text.
 void setFont(java.lang.String font)
          Changes the font currently being used.
 void setFormatWidth(int width)
          Redefines the maximum paragraph width for formatted text.
 void setText(java.lang.String text)
          Changes the active text.
 void setToDefaultFont()
          Resets the current font to use the default font at all times.
 java.lang.String text()
          Accesses the current String of text.
 float textHeight()
          Learns the height of the formatted text.
 float textWidth()
          Learns the maximum width of all formatted lines of text.
 float width()
          Learns about the dimensions of the TextSkin.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

text

protected java.lang.String text
The base text to render.


formattedText

protected java.lang.String[] formattedText
The text that has been passed through the Font for formatting. This is the text that is actually drawn.


format

protected boolean format
Whether or not to push the text into paragraph format. Even if this is set to false, the base text will be broken by line breaks.


indent

protected boolean indent
If formatting is enabled, this marks whether or not to indent the lines of text that are retrieved.


formatWidth

protected int formatWidth
If formatting is enabled, this indicated the maximum width of the paragraph.


width

protected float width
The base width of the text being rendered.


height

protected float height
The base height of the text being rendered.


reformat

protected boolean reformat
Whether or not the TextSkin needs to reformat its text. This may happen for a variety of reasons - the text to render may change; the font may change, the maximum width of the formatted paragraph may change, etc.


fontPointer

protected FontPointer fontPointer
The FontPointer used to obtain and set the Font used to render text.

Constructor Detail

TextSkin

public TextSkin(java.lang.String text)
Creates a TextSkin that uses the default font to render the supplied text.

Parameters:
text - The text to render.

TextSkin

public TextSkin(java.lang.String text,
                java.lang.String font)
Creates a TextSkin that uses the indicated font to render the supplied text. NOTE: Supplying a null reference for the font will result in the default font being used.

Parameters:
text - The text to render.
font - The identifier of the Font to render.
Method Detail

defaultFormattting

protected void defaultFormattting()
This method is used to obtain the initial formatting the TextSkin will use. Placing this into a method allows extensions of TextSkin to have a different default formatting.


font

public Font font()
Obtains a reference to the active Font.

Returns:
The Font currently being used to render text.

setFont

public void setFont(java.lang.String font)
Changes the font currently being used.

Parameters:
font - The identifier of the new Font to use.

setToDefaultFont

public void setToDefaultFont()
Resets the current font to use the default font at all times.


text

public java.lang.String text()
Accesses the current String of text.

Returns:
The base text being rendered.

setText

public void setText(java.lang.String text)
Changes the active text.

Parameters:
text - The new text to render.

formattedText

public java.lang.String[] formattedText()
Accesses the processed, formatted Strings of text that are actually rendered.

Returns:
The formatted text in array form. Each element in the array is a separate line of text. Formatting is managed this way to simplify rendering.

setFormatWidth

public void setFormatWidth(int width)
Redefines the maximum paragraph width for formatted text.

Parameters:
width - The new width for formatting.

formatWidth

public int formatWidth()
Learns the current formatting width.

Returns:
The maximum width of formatted paragraphs.

enableFormatting

public void enableFormatting(boolean format)
Sets whether or not to format the base text into paragraphs. NOTE: Even if this is turned off, the base text will still be split by line breaks ('\n' or '\r').

Parameters:
format - Whether or not formatting should be enabled.

formatting

public boolean formatting()
Learns if formatting is enabled.

Returns:
True if text should be formatted into paragraphs, false if formatting occurs simply by line breaks.

enableIndent

public void enableIndent(boolean indent)
Sets whether or not to indent formatted text.

Parameters:
indent - Whether or not indenting should be enabled.

indenting

public boolean indenting()
Learns if indenting is enabled.

Returns:
True if text should be indented when formatted into paragraphs.

checkFormat

public void checkFormat()
Makes sure that the text to render is properly formatted. If the skin has already been formatted and no changes have been made, this does nothing. However, if the formatted text is not up-to-date, then this recalculates the text to render.


reformat

public void reformat()
This method forces a reformat of the text to render. NOTE: The reformat happens immediately, it does not wait for


flagReformat

public void flagReformat()
Flags the TextSprite that it needs to reformat on the next render.


format

protected void format(Font font)
This calculates the list of Strings to use for rendering. Additionally, this calculates the rendered dimensions of the final lines of text.

Parameters:
font - The Font that should do the formatting calculations.

textWidth

public float textWidth()
Learns the maximum width of all formatted lines of text.

Returns:
The width of the longest line of text, in pixels.

textHeight

public float textHeight()
Learns the height of the formatted text.

Returns:
The total height of all rendered lines of text, in pixels.

width

public float width()
Learns about the dimensions of the TextSkin. Since this is rendered from its top left corner, and we need to take rotation into account, we need to return 2x the width of the text.

Specified by:
width in interface Dimensioned
Returns:
The width the TextSkin will fit within when rotated in any direction.

height

public float height()
Learns about the dimensions of the TextSkin. Since this is rendered from its top left corner, and we need to take rotation into account, we need to return 2x the height of the text.

Specified by:
height in interface Dimensioned
Returns:
The height the TextSkin will fit within when rotated in any direction.

render

public void render()
First, checks to see if the TextSkin needs to update its formatting; then renders the formatted text.

Specified by:
render in interface Renderable