Package squidpony.squidgrid.gui.gdx
Class GDXMarkup
java.lang.Object
squidpony.squidgrid.gui.gdx.GDXMarkup
- All Implemented Interfaces:
IMarkup<com.badlogic.gdx.graphics.Color>
public class GDXMarkup extends Object implements IMarkup<com.badlogic.gdx.graphics.Color>
GDXMarkup implements IMarkup for libGDX Color objects, and can start blocks of markup that libGDX understands that
display text in a given Color. Typically, the singleton
The notation for colors is the same as in the rest of libGDX, but if you make an IColoredString with colorString(), it doesn't need any flag to be changed on your BitmapFont (like it does for GDX markup normally). This notation looks like
Created by Tommy Ettinger on 1/23/2016.
instance
would be passed to a class that uses
IMarkup of Color, and then anything else will be handled internally as Colors are given to the using class. This does
extend GDX's markup to handle bold and italic options for text; this only works if you are using a TextFamily
as your TextCellFactory
, such as DefaultResources.getSlabFamily()
, and only if you use
colorString(CharSequence)
or styleString(CharSequence)
to generate a value that can be drawn later.
You can use colorStringOnly(CharSequence)
if you aren't using a TextFamily; it ignores bold and italic style
tags and removes them from the IColoredString it returns, but otherwise colors and case-adjusts text as tags say.
The notation for colors is the same as in the rest of libGDX, but if you make an IColoredString with colorString(), it doesn't need any flag to be changed on your BitmapFont (like it does for GDX markup normally). This notation looks like
[#FF00FF]Magenta text here[]
, which starts a tag for a hex color, uses the hex value for bright magenta,
then contains the text "Magenta text here" which will be shown in bright magenta, followed by "[]" to change the
color (and style, see next) back to the default white (and normal style). You can also use the names of colors, as
defined in the documentation for every SColor
, and some libGDX Color values as well; this looks like
[Inside Of A Bottle]Gray text[]
to produce the words "Gray text" with the color
SColor.INSIDE_OF_A_BOTTLE
(note that the docs for that SColor say what the precise name is, and case needs to
match; you can also look up the SColor.name
field). Another option for colors is to use hue, saturation,
value, and optionally opacity as 3 or 4 floats, in that order; this looks like [@ 0 0.7 0.96]
(hue,
saturation, and value, with opacity implicitly 1) or [@ 0 0.7 0.96 0.8]
(0.8 opacity is a bit translucent).
The HSV markup option is an addition to libGDX's syntax; any number of spaces/tabs can be used between HSV components
and the space between @ and the first component is optional. You can use [[
to escape an opening bracket, and
[]
to reset formatting. As an addition to GDX color markup, you can change case with [!]
to make text
ALL CAPS or [=]
to make it lower-case (encountering a tag toggles it, so [!]yelling[!] are we?
would
become YELLING are we?
), using []
to reset all markup or [,]
to reset just case and font
style (see next) markup. If using a TextFamily such as DefaultResources.getLeanFamily()
, you can toggle the
font style as bold with [*]
and as italic with [/]
. If bold is on when this encounters another bold
tag, it will turn bold off; the same is true for italic. These formatting styles can overlap and do not need to be
nested as in HTML; this notation is valid: [*]bold, [/]bold and italic, [*] just italic,[] plain
. While
[]
resets both color, case, and style to white color, normal case, and regular style, there is also
[,]
to reset only style/case, or [WHITE]
to reset only color (to white).
Created by Tommy Ettinger on 1/23/2016.
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
Constructors Constructor Description GDXMarkup()
-
Method Summary
Modifier and Type Method Description String
closeMarkup()
IColoredString<com.badlogic.gdx.graphics.Color>
colorString(CharSequence markupString)
Takes a CharSequence (such as a String or StringBuilder) that contains the markup this class understands, and produces an IColoredString (of Color) with the color markup tags used to mark text in colors in the resulting IColoredString, and any style markup tags used to mark sections of text as bold or italic for a TextFamily to render (normal TextCellFactory rendering may show bold/italic text as gibberish).StringBuilder
colorStringMarkup(CharSequence markupString)
Takes a CharSequence (such as a String or StringBuilder) that contains the markup this class understands, and produces a StringBuilder with the color markup tags inmarkupString
all changed to libGDX-usable color markup, and any style markup tags used to mark sections of text as bold or italic for a TextFamily to render (normal TextCellFactory rendering may show bold/italic text as gibberish).IColoredString<com.badlogic.gdx.graphics.Color>
colorStringOnly(CharSequence markupString)
Takes a CharSequence (such as a String or StringBuilder) that contains the markup this class understands, and produces an IColoredString (of Color) with the color markup tags used to mark text in colors in the resulting IColoredString, but with both bold and italic style markup tags ignored (so normal TextCellFactory objects can render the text correctly).StringBuilder
colorStringOnlyMarkup(CharSequence markupString)
Takes a CharSequence (such as a String or StringBuilder) that contains the markup this class understands, and produces a StringBuilder with the color markup tags inmarkupString
all changed to libGDX-usable color markup, but with both bold and italic style markup tags ignored (so normal TextCellFactory objects can render the text correctly).String
getMarkup(com.badlogic.gdx.graphics.Color value)
StringBuilder
removeMarkup(CharSequence markupString)
Removes all SquidLib and libGDX markup from the givenmarkupString
except for[[
to escape a left bracket, returning the result as a new StringBuilder.char
styleChar(char basis, boolean bold, boolean italic)
Directly styles one char value, toggling its bold state ifbold
is true, and toggling its italic state ifitalic
is true.StringBuilder
styleString(CharSequence markupString)
Similar tocolorString(CharSequence)
, but leaves color tags as they are and only uses case and style tags, like[*]
for bold,[/]
for italic,[!]
for ALL CAPS and[=]
for lower-case.char
unstyleChar(char styled)
If given a char that has added style information for bold/italic modes (colors aren't stored in char data), this removes the bold/italic data and makes the char what it will be rendered as in a normal font (not a special TextFamily, which renders later sections of Unicode as bold and/or italic).
-
Field Details
-
Constructor Details
-
Method Details
-
getMarkup
-
closeMarkup
- Specified by:
closeMarkup
in interfaceIMarkup<com.badlogic.gdx.graphics.Color>
-
removeMarkup
Removes all SquidLib and libGDX markup from the givenmarkupString
except for[[
to escape a left bracket, returning the result as a new StringBuilder.- Parameters:
markupString
- a String or other CharSequence containing color and/or style markup tags- Returns:
- markupString without color, case, or style markup tags, only keeping text and escapes for left brackets.
-
colorString
Takes a CharSequence (such as a String or StringBuilder) that contains the markup this class understands, and produces an IColoredString (of Color) with the color markup tags used to mark text in colors in the resulting IColoredString, and any style markup tags used to mark sections of text as bold or italic for a TextFamily to render (normal TextCellFactory rendering may show bold/italic text as gibberish).- Parameters:
markupString
- a String or other CharSequence containing color and/or style markup tags- Returns:
- an IColoredString (of Color) with all the markup applied and removed from the text after applying
-
colorStringOnly
Takes a CharSequence (such as a String or StringBuilder) that contains the markup this class understands, and produces an IColoredString (of Color) with the color markup tags used to mark text in colors in the resulting IColoredString, but with both bold and italic style markup tags ignored (so normal TextCellFactory objects can render the text correctly). Case markup is still used as in other methods here, and all markup tags, including those for bold and italic styles, will be removed (so you don't need to manually remove styles that can't be shown by a TextCellFactory).- Parameters:
markupString
- a String or other CharSequence containing color and/or case markup tags- Returns:
- an IColoredString (of Color) with all the markup applied and removed from the text after applying
-
styleString
Similar tocolorString(CharSequence)
, but leaves color tags as they are and only uses case and style tags, like[*]
for bold,[/]
for italic,[!]
for ALL CAPS and[=]
for lower-case. The StringBuilder this returns can be converted to a String or used directly for further modification, but styles will probably only render correctly if using a TextFamily. You should be aware that if[]
is used to reset both color and style in the given markupString, then only the style will be reset here but the[]
will be removed, which may affect colors if the result is later given to something that expects color markup to also have been closed. A solution for this is to use[,]
to reset only styles, and to avoid using[]
to change color by explicitly using[WHITE]
to set the text color back to pure white. This way is only necessary if you have color markup in the markupString you pass to this method.- Parameters:
markupString
- a String containing color markup (which is left as-is) and/or case/style markup (which is used)- Returns:
- a StringBuilder based on markupString that has the case/style markup applied and other markup left there
-
styleChar
Directly styles one char value, toggling its bold state ifbold
is true, and toggling its italic state ifitalic
is true. When either or both of bold or italic are enabled in a char, that char will be at some unrelated section of Unicode, and the char will be hard to identify unless you callunstyleChar(char)
on it or render it with a TextFamily (which will render bold and italic chars as the correct glyph and style).- Parameters:
basis
- the char to potentially make bold or italic; can be bold or italic alreadybold
- if true, the bold-ness of basis will be toggled; if false, does not change bold dataitalic
- if true, the italic-ness of basis will be toggled; if false, does not change italic data- Returns:
- a char that will look like basis when rendered appropriately but with bold and italic settings applied
-
unstyleChar
If given a char that has added style information for bold/italic modes (colors aren't stored in char data), this removes the bold/italic data and makes the char what it will be rendered as in a normal font (not a special TextFamily, which renders later sections of Unicode as bold and/or italic).- Parameters:
styled
- a char that should have any bold or italic data set to normal (non-bold, non-italic)- Returns:
- a char that will not be bold or italic
-
colorStringMarkup
Takes a CharSequence (such as a String or StringBuilder) that contains the markup this class understands, and produces a StringBuilder with the color markup tags inmarkupString
all changed to libGDX-usable color markup, and any style markup tags used to mark sections of text as bold or italic for a TextFamily to render (normal TextCellFactory rendering may show bold/italic text as gibberish). This needs markup to be enabled on any BitmapFont that renders it, by settingBitmapFont.BitmapFontData.markupEnabled
to true.- Parameters:
markupString
- a String or other CharSequence containing color and/or style markup tags- Returns:
- a new StringBuilder with all the markup applied; only libGDX-usable markup will remain
-
colorStringOnlyMarkup
Takes a CharSequence (such as a String or StringBuilder) that contains the markup this class understands, and produces a StringBuilder with the color markup tags inmarkupString
all changed to libGDX-usable color markup, but with both bold and italic style markup tags ignored (so normal TextCellFactory objects can render the text correctly). Case markup is still used as in other methods here, and all markup tags that libGDX can't use, including those for bold and italic styles, will be removed (so you don't need to manually remove styles that can't be shown by a TextCellFactory). This needs markup to be enabled on any BitmapFont that renders it, by settingBitmapFont.BitmapFontData.markupEnabled
to true.- Parameters:
markupString
- a String or other CharSequence containing color and/or case markup tags (style markup will be removed)- Returns:
- a new StringBuilder with all the color and case markup applied; only libGDX-usable markup will remain
-