Game Control Plus  1.2.2
 All Classes Namespaces Functions Variables Pages
org.gamecontrolplus.gui.MStyledString Class Reference
Inheritance diagram for org.gamecontrolplus.gui.MStyledString:

Classes

class  AttributeRun
 
class  TextLayoutHitInfo
 
class  TextLayoutInfo
 

Public Member Functions

 MStyledString (String startText)
 
 MStyledString (String startText, int wrapWidth)
 
String getPlainText ()
 
String[] getPlainTextAsArray ()
 
int length ()
 
void setJustify (boolean justify)
 
void setJustifyRatio (float jRatio)
 
void addAttribute (Attribute type, Object value)
 
void addAttribute (Attribute type, Object value, int lineNo, int charStart, int charEnd)
 
void addAttribute (Attribute type, Object value, int charStart, int charEnd)
 
void clearAttributes (int lineNo, int charStart, int charEnd)
 
void clearAttributes (int charStart, int charEnd)
 
void clearAttributes ()
 
int insertCharacters (int insertPos, String chars)
 
int insertCharacters (int insertPos, String chars, boolean startNewLine)
 
boolean insertEOL (int insertPos)
 
boolean deleteCharacters (int fromPos, int nbrToRemove)
 
void setFont (Font a_font)
 
LinkedList< TextLayoutInfogetLines (Graphics2D g2d)
 
int getNbrLines ()
 
float getTextAreaHeight ()
 
float getMaxLineLength ()
 
float getMaxLineHeight ()
 
int getWrapWidth ()
 
void setWrapWidth (int wrapWidth)
 

Static Public Member Functions

static void save (PApplet papp, MStyledString ss, String fname)
 
static MStyledString load (PApplet papp, String fname)
 

Package Functions

MStyledString convertToSingleLineText ()
 
TextLayoutHitInfo calculateFromXY (Graphics2D g2d, float px, float py)
 
TextLayoutInfo getTLIforLineNo (int ln)
 
TextLayoutInfo getTLIforYpos (float y)
 
TextLayoutInfo getTLIforCharNo (int charNo)
 
TextLayoutHitInfo getTLHIforCharPosition (int lineNo, int charNo)
 

Package Attributes

int startIdx = -1
 
int endIdx = -1
 

Private Member Functions

AttributedString insertParagraphMarkers (String ptext, AttributedString as)
 
Object validateTextAttributeColor (TextAttribute ta, Object value)
 
void applyAttributes ()
 
int insertCharactersImpl (int insertPos, String chars, boolean startNewLine)
 
String makeStringSafeForInsert (String chars)
 
float getHeight (TextLayout layout)
 
String removeDoubleSpacingFromPlainText (String chars)
 
String removeSingleSpacingFromPlainText (String chars)
 
ImageGraphicAttribute getParagraghSpacer (int ww)
 
void readObject (ObjectInputStream ois) throws ClassNotFoundException, IOException
 

Private Attributes

transient AttributedString styledText = null
 
transient ImageGraphicAttribute spacer = null
 
transient LineBreakMeasurer lineMeasurer = null
 
transient LinkedList
< TextLayoutInfo
linesInfo = new LinkedList<TextLayoutInfo>()
 
transient Font font = null
 
String plainText = ""
 
LinkedList< AttributeRunbaseStyle = new LinkedList<AttributeRun>()
 
LinkedList< AttributeRunatrun = new LinkedList<AttributeRun>()
 
int wrapWidth = Integer.MAX_VALUE
 
boolean invalidLayout = true
 
boolean invalidText = true
 
boolean justify = false
 
float justifyRatio = 0.7f
 
float textHeight = 0
 
float maxLineLength = 0
 
float maxLineHeight = 0
 
int nbrLines
 

Static Private Attributes

static final long serialVersionUID = 8380395122026579368L
 

Additional Inherited Members

- Public Attributes inherited from org.gamecontrolplus.gui.MConstantsInternal
String SLIDER_STYLES = "|grey_blue|blue18px|green_red20px|purple18px|red_yellow18px|"
 
String DEFAULT_SLIDER_STYLE = "grey_blue"
 
int DRAW_METHOD = 0x00000001
 
int MOUSE_METHOD = 0x00000002
 
int PRE_METHOD = 0x00000004
 
int KEY_METHOD = 0x00000008
 
int POST_METHOD = 0x00000010
 
int ALL_METHOD = 0x0000001f
 
int RUNTIME_ERROR = 0xf0000000
 
int MISSING = 0x01000001
 
int NONEXISTANT = 0x01000002
 
int EXCP_IN_HANDLER = 0x81000003
 
int OFF_CONTROL = 0
 
int OVER_CONTROL = 1
 
int PRESS_CONTROL = 2
 
int DRAG_CONTROL = 3
 
int TINT_FOR_ALPHA = 255
 
int I_NONE = 0
 
int I_TL = 1
 
int I_TR = 2
 
int I_CL = 4
 
int I_CR = 8
 
int I_INSIDE = 16
 
int I_COVERED = 32
 
int I_MODES = 63
 
int MERGE_RUNS = 256
 
int CLIP_RUN = 512
 
int COMBI_MODES = 768
 
int[][] grid
 
BasicStroke pen_1_0 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)
 
BasicStroke pen_2_0 = new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)
 
BasicStroke pen_3_0 = new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)
 
BasicStroke pen_4_0 = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)
 

Detailed Description

This class is used to represent text with attributes.
It means that you don't have to have the same style of font or even the same font face over the whole length of the text.

Most font features can be modified all except the text background which is transparent. There is a feature to highlight part of the string by having a different background colour but this is used for highlighting selected text in GTextField and GTextArea components.

It is also used for all controls that use text.

Author
Peter Lager

Constructor & Destructor Documentation

org.gamecontrolplus.gui.MStyledString.MStyledString ( String  startText)

This is assumed to be a single line of text (i.e. no wrap). EOL characters will be stripped from the text before use.

Parameters
startText
org.gamecontrolplus.gui.MStyledString.MStyledString ( String  startText,
int  wrapWidth 
)

Supports multiple lines of text wrapped on word boundaries.

Parameters
startText
wrapWidth

Member Function Documentation

void org.gamecontrolplus.gui.MStyledString.addAttribute ( Attribute  type,
Object  value 
)

Add an attribute that affects the whole length of the string.

Parameters
typeattribute type
valueattribute value
void org.gamecontrolplus.gui.MStyledString.addAttribute ( Attribute  type,
Object  value,
int  lineNo,
int  charStart,
int  charEnd 
)

Add a text attribute (style) to the specifies range of characters

Parameters
typeattribute type
valueattribute value
lineNothe line of test affected
charStartthe first character affected
charEndthe character position after the last character affected.
void org.gamecontrolplus.gui.MStyledString.addAttribute ( Attribute  type,
Object  value,
int  charStart,
int  charEnd 
)

Set the attribute to be applied to a range of characters starting at charStart and ending with charEnd-1.

Parameters
typeattribute type
valueattribute value
charStartthe first character affected
charEndthe character position after the last character affected.
void org.gamecontrolplus.gui.MStyledString.applyAttributes ( )
private

Do not use this method use clearAttributes() instead.
It has been left in to avoid having to release a new GUI Builder version just for this change. Be warned - it will be removed on next release of GUI Builder.

Deprecated:
will be removed on next GUI Builder update

Must call this method to apply

void org.gamecontrolplus.gui.MStyledString.clearAttributes ( int  lineNo,
int  charStart,
int  charEnd 
)

Remove text attributes (style) to the specified line and range of characters

Parameters
lineNothe line of test affected
charStartthe first character affected
charEndthe character position after the last character affected.
void org.gamecontrolplus.gui.MStyledString.clearAttributes ( int  charStart,
int  charEnd 
)

Remove text attributes (style) to the specified range of characters

Parameters
charStartthe first character affected
charEndthe character position after the last character affected.
void org.gamecontrolplus.gui.MStyledString.clearAttributes ( )

Removes all styling from the string.

MStyledString org.gamecontrolplus.gui.MStyledString.convertToSingleLineText ( )
package

Converts this StyledString from multi-line to single-line by replacing all EOL characters with the space character for paragraphs

Parameters
ptext
as
Returns
the converted string
boolean org.gamecontrolplus.gui.MStyledString.deleteCharacters ( int  fromPos,
int  nbrToRemove 
)

Remove a number of characters from the string

Parameters
nbrToRemovenumber of characters to remove
fromPosstart location for removal
Returns
true if the deletion was successful else false
float org.gamecontrolplus.gui.MStyledString.getHeight ( TextLayout  layout)
private

Get the height of the given TextLayout

Parameters
layout
Returns
the height of a given text layout
LinkedList<TextLayoutInfo> org.gamecontrolplus.gui.MStyledString.getLines ( Graphics2D  g2d)

Get the text layouts for display if the string has changed since last call to this method regenerate them.

Parameters
g2dGraphics2D display context
Returns
a list of text layouts for rendering
float org.gamecontrolplus.gui.MStyledString.getMaxLineHeight ( )

Get the height of the tallest line

float org.gamecontrolplus.gui.MStyledString.getMaxLineLength ( )

Return the length of the longest line.

int org.gamecontrolplus.gui.MStyledString.getNbrLines ( )

Get the number of lines in the layout

ImageGraphicAttribute org.gamecontrolplus.gui.MStyledString.getParagraghSpacer ( int  ww)
private

Create a graphic image character to simulate paragraph breaks

Parameters
ww
Returns
a blank image to manage paragraph ends.
String org.gamecontrolplus.gui.MStyledString.getPlainText ( )

Get the plain text as a String. Any line breaks will kept and will be represented by the character 'backslash n'

Returns
the associated plain text
String [] org.gamecontrolplus.gui.MStyledString.getPlainTextAsArray ( )

Get the plain text as a String array. (splitting on line breaks)

Returns
the associated plain text as a String array split on line breaks
float org.gamecontrolplus.gui.MStyledString.getTextAreaHeight ( )

Return the height of the text line(s)

TextLayoutHitInfo org.gamecontrolplus.gui.MStyledString.getTLHIforCharPosition ( int  lineNo,
int  charNo 
)
package
Parameters
lineNo
charNo
Returns
TextLayoutInfo org.gamecontrolplus.gui.MStyledString.getTLIforCharNo ( int  charNo)
package

This will always return a layout provided charNo >= 0.

If charNo > than the index of the last character in the plain text then this should be corrected to the last character in the layout by the caller.

Parameters
charNothe character position in text (must be >= 0)
Returns
the first layout where c is greater that the layout's start char index.
TextLayoutInfo org.gamecontrolplus.gui.MStyledString.getTLIforLineNo ( int  ln)
package

Get a layout based on line number

Parameters
lnline number
Returns
text layout info for the line ln
TextLayoutInfo org.gamecontrolplus.gui.MStyledString.getTLIforYpos ( float  y)
package

This will always return a layout provide there is some text.

Parameters
yMust be >= 0
Returns
the first layout where y is above the upper layout bounds
int org.gamecontrolplus.gui.MStyledString.getWrapWidth ( )

Get the break width used to create the lines.

int org.gamecontrolplus.gui.MStyledString.insertCharacters ( int  insertPos,
String  chars 
)

Insert 1 or more characters into the string. The inserted text will first be made safe by removing any inappropriate EOL characters.
Do not use this method to insert EOL characters, use the

insertEOL(int)

method instead.

Parameters
insertPosposition in string to insert characters
charsthe characters to insert
Returns
the number of characters inserted
int org.gamecontrolplus.gui.MStyledString.insertCharacters ( int  insertPos,
String  chars,
boolean  startNewLine 
)

Insert 1 or more characters into the string. The inserted text will first be made safe by removing any inappropriate EOL characters.
Do not use this method to insert EOL characters, use the

insertEOL(int)

method instead.

Parameters
insertPosposition in string to insert characters
charsthe characters to insert
startNewLineif true insert onto a new line
Returns
the number of characters inserted
boolean org.gamecontrolplus.gui.MStyledString.insertEOL ( int  insertPos)

Use this method to insert an EOL character.

Parameters
insertPosindex position to insert EOL
Returns
true if an EOL was inserted into the string
AttributedString org.gamecontrolplus.gui.MStyledString.insertParagraphMarkers ( String  ptext,
AttributedString  as 
)
private

This class uses transparent images to simulate end/starting positions for paragraphs

Parameters
ptext
as
Returns
the styled string with paragraph marker images embedded
int org.gamecontrolplus.gui.MStyledString.length ( )

Get the number of characters in this styled string

static MStyledString org.gamecontrolplus.gui.MStyledString.load ( PApplet  papp,
String  fname 
)
static

Load and return a StyledString object from the given file.

Parameters
papp
fnamethe filename of the StyledString
String org.gamecontrolplus.gui.MStyledString.makeStringSafeForInsert ( String  chars)
private

This is ONLY used when multiple characters are to be inserted.
If it is single line text i.e. no wrapping then it removes all EOLs If it is multiple line spacing it will reduce all double EOLs to single EOLs and remove any EOLs at the start or end of the string.

Parameters
chars
Returns
a string that is safe for inserting
String org.gamecontrolplus.gui.MStyledString.removeDoubleSpacingFromPlainText ( String  chars)
private

Ensure we do not have blank lines by replacing double EOL characters by single EOL until there are only single EOLs.
Using replaceAll on its own will not work because EOL/EOL/EOL would become EOL/EOL not the single EOL required.

String org.gamecontrolplus.gui.MStyledString.removeSingleSpacingFromPlainText ( String  chars)
private

Remove all EOL characters from the string. This is necessary if the string is for a single line component.

Parameters
charsthe string to use
Returns
the string with all EOLs removed
static void org.gamecontrolplus.gui.MStyledString.save ( PApplet  papp,
MStyledString  ss,
String  fname 
)
static

Save the named StyleString in the named file.

Parameters
papp
ssthe styled string
fname
void org.gamecontrolplus.gui.MStyledString.setJustify ( boolean  justify)

Text can be either left or fully justified.

Parameters
justifytrue for full justification
void org.gamecontrolplus.gui.MStyledString.setJustifyRatio ( float  jRatio)

Justify only if the line has sufficient text to do so.

Parameters
jRatioratio of text length to visibleWidth
void org.gamecontrolplus.gui.MStyledString.setWrapWidth ( int  wrapWidth)

Set the maximum width of a line.

Parameters
wrapWidth
Object org.gamecontrolplus.gui.MStyledString.validateTextAttributeColor ( TextAttribute  ta,
Object  value 
)
private

If the text attribute is BACKGROUND or FOREGROUND then if we pass an integer (e.g. from Processing sketch) then convert it.

Parameters
tathe text attribute
valuethe value to use with this text attribute