Package com.github.tommyettinger.textra
Class Parser
java.lang.Object
com.github.tommyettinger.textra.Parser
Utility class to parse tokens from a
TypingLabel
; not intended for external use in most situations.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
Replaces any square-bracket markup of the form[-SOMETHING]
with the curly-brace tag form{SOMETHING}
.static void
parseTokens
(TypingLabel label) Parses all tokens from the givenTypingLabel
.static String
preprocess
(String text) Replaces any style markup using square brackets, such as[_]
for underline, with the syntax{STYLE=_}
(changing_
based on what was in the square brackets).static boolean
stringToBoolean
(String str) Returns a boolean value parsed from the given String, or the default value if the string couldn't be parsed.static int
stringToColor
(TypingLabel label, String str) Parses a color from the given string.static String
Encloses the given string in brackets to work as a regular color markup tag.static float
stringToFloat
(String str, float defaultValue) Returns a float value parsed from the given String, or the default value if the string couldn't be parsed.static String
Matches style names to syntax and encloses the given string in brackets to work as a style markup tag.
-
Constructor Details
-
Parser
public Parser()
-
-
Method Details
-
handleBracketMinusMarkup
Replaces any square-bracket markup of the form[-SOMETHING]
with the curly-brace tag form{SOMETHING}
. This allows you to produce curly-brace tags even when curly braces have some meaning that is reserved by another tool, such as I18N bundles. As long as the other tool somehow consumes curly braces from the source text, this can bring back curly braces beforeFont.markup(String, Layout)
gets called. From then on, something like[-RAINBOW]
will effectively be{RAINBOW}
.
If this finds no occurrence of"[-"
intext
, this simply returnstext
without allocating.- Parameters:
text
- text that could have square-bracket-minus markup- Returns:
text
with any square-bracket-minus markup changed to curly-brace tags
-
preprocess
Replaces any style markup using square brackets, such as[_]
for underline, with the syntax{STYLE=_}
(changing_
based on what was in the square brackets). This also changes[ ]
to{RESET}
and[]
to{UNDO}
. This won't change escaped brackets or the inline image syntax that uses[+name of an image in an atlas]
.- Parameters:
text
- text that could have square-bracket style markup- Returns:
text
with square bracket style markup changed to curly-brace style tags
-
parseTokens
Parses all tokens from the givenTypingLabel
. -
stringToFloat
Returns a float value parsed from the given String, or the default value if the string couldn't be parsed. This can be useful in Effects. -
stringToBoolean
Returns a boolean value parsed from the given String, or the default value if the string couldn't be parsed. This can be useful in Effects. -
stringToColor
Parses a color from the given string. Returns null if the color couldn't be parsed. This can be useful in Effects. This uses theColorLookup
of the given label's default Font to try to parsestr
(unless the ColorLookup is null), and if that finds a named color, this returns its value. Otherwise, this parses the color as a hex RGBA8888, hex RGB888, or hex RGB444 color. The RGB modes assume alpha is fully opaque. This will attempt to parse the most chars it can and determines which RGB(A) format to use based on the length ofstr
.- Parameters:
label
- a TypingLabel; only used to get its default Font, which is only used for its ColorLookupstr
- a String containing either the name of a color as a ColorLookup can understand, or an RGB(A) hex int- Returns:
- an RGBA8888 int representing the color that str represents, or
256
if no color could be looked up
-
stringToColorMarkup
Encloses the given string in brackets to work as a regular color markup tag. -
stringToStyleMarkup
Matches style names to syntax and encloses the given string in brackets to work as a style markup tag.
-