Skip to content

Commit

Permalink
Effect names should be styles, not colors.
Browse files Browse the repository at this point in the history
Colors should also not be styles! Thanks to Thibweg on Discord for finding this and suggesting an initial fix -- parts of that fix related to VAR might still make it in here, since I didn't want to mess with VAR parsing in the middle of another change.
  • Loading branch information
tommyettinger committed Jan 17, 2024
1 parent 7c434b2 commit 63558a6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
23 changes: 16 additions & 7 deletions src/main/java/com/github/tommyettinger/textra/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*/
public class Parser {
private static final Pattern PATTERN_MARKUP_STRIP = Pattern.compile("((?<!\\[)\\[[^\\[\\]]*(\\]|$))");
private static final Replacer COLOR_MARKUP_TO_TAG = new Replacer(Pattern.compile("(?<!\\[)\\[(?:(?:#({=m}[A-Fa-f0-9]{3,8}))|(?:\\|?)({=m}\\p{Js}[^\\[\\]]*))(\\]|$)"), "{COLOR=${\\m}}");
private static final Replacer MARKUP_TO_TAG = new Replacer(Pattern.compile("(?<!\\[)\\[([^\\[\\]\\+][^\\[\\]]*)(\\]|$)"), "{STYLE=$1}");
private static final Pattern PATTERN_COLOR_HEX_NO_HASH = Pattern.compile("[A-Fa-f0-9]{3,8}");

Expand All @@ -52,7 +53,10 @@ public class Parser {
* @return {@code text} with square bracket style markup changed to curly-brace style markup
*/
public static String preprocess(String text) {
return MARKUP_TO_TAG.replace(text.replace("[ ]", "{RESET}")).replace("[]", "{UNDO}");
text = text.replace("[ ]", "{RESET}");
text = text.replace("[]", "{UNDO}");
text = COLOR_MARKUP_TO_TAG.replace(text);
return MARKUP_TO_TAG.replace(text);
}

/**
Expand Down Expand Up @@ -482,14 +486,17 @@ public static int stringToColor(TypingLabel label, String str) {
*/
public static String stringToColorMarkup(String str) {
if (str != null) {
if(Palette.NAMED.containsKey(str)) {
return "[" + str + "]";
}
// If color isn't registered by name, try to parse it as a hex code.
if (str.length() >= 3 && !Palette.NAMED.containsKey(str) && PATTERN_COLOR_HEX_NO_HASH.matches(str)) {
if (str.length() >= 3 && PATTERN_COLOR_HEX_NO_HASH.matches(str)) {
return "[#" + str + "]";
}
}

// Return color code
return "[" + str + "]";
// Return no change
return "";
}

/**
Expand Down Expand Up @@ -550,11 +557,13 @@ public static String stringToStyleMarkup(String str) {
return "[%" + str.substring(0, str.length() - 1) + "]";
if (str.startsWith("%"))
return "[%" + str.substring(1) + "]";
if (str.length() >= 3 && !Colors.getColors().containsKey(str) && PATTERN_COLOR_HEX_NO_HASH.matches(str))
if(Palette.NAMED.containsKey(str))
return "[" + str + "]";
if (str.length() >= 3 && PATTERN_COLOR_HEX_NO_HASH.matches(str))
return "[#" + str + "]";
}
// Return unaltered code, enclosed
return "[" + str + "]";
// Return no change
return "";
}

/**
Expand Down
36 changes: 19 additions & 17 deletions src/test/java/com/github/tommyettinger/textra/TypingLabelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public TypingLabel createTypingLabel() {
text.append("{SLOWER}{GRADIENT=FF70F1;light exciting pink orange with ignored words;-0.5;5}{EASE=-8;2;1}{SHRINK=2;5}[@Medieval]Welcome,{ENDSHRINK}[%] [@]{WAIT}");
text.append("{SPIRAL=2;0.5;-2.5}{STYLE=/}{STYLE=;}[%^SHADOW]{VAR=title}[%]{STYLE=;}{STYLE=/}{ENDSPIRAL}![ ] {TRIGGER=lightest violet}[lightest violet]{VAR=MUTATE}[+🤔][ ]{WAIT=0.8}");
text.append("{FAST}\n\n");
text.append("{RESET}[@Sans]{ATTENTION}This is a [*][MAROON][%?SHINY]simple[WHITE][*] [%?blacken]test[%][@]{ENDATTENTION} to {SPIN}show you{ENDSPIN}");
text.append("{RESET}[@Sans]{ATTENTION}This is a [*][#b03060ff][%?SHINY]simple[WHITE][*][%] [%?blacken]test[%][@]{ENDATTENTION} [blacken]to[%] {SPIN}show you{ENDSPIN}");
text.append("{GRADIENT=27C1F5;2776E7;-0.5;5} {CROWD=20;1;forever}how to make dialogues{ENDCROWD} {JUMP}{SLOW}[*][/]fun[/][*] again! {ENDGRADIENT}[+🥳]{ENDJUMP}{WAIT}\n");
text.append("{NORMAL}{CLEARCOLOR}{JOLT=1;0.8;inf;0.25;dddddd;fff0cc}With this library{ENDJOLT} [LIGHTER RICH gold]you[WHITE] can {SQUASH}{SIZE=150%}[_]control[_]{ENDSQUASH} {SIZE=%75}the{SIZE=150%} flow[^][SKY] [[citation needed][ ] of the text with");
text.append(" {BLINK=FF6BF3;FF0582;3}tokens{ENDBLINK},{WAIT=0.7}");
Expand Down Expand Up @@ -251,22 +251,24 @@ public TypingLabel createTypingLabel() {
}
// Font condensed = font.family.connected[font.family.fontAliases.get("Condensed", 0)];
// condensed.scaleTo(font.cellWidth, font.cellHeight);
final TypingLabel label = new TypingLabel(text.toString(), font){
@Override
public void act(float delta) {
super.act(delta);
long time = System.currentTimeMillis();
if(time % 5000 >= 1000)
// setVariable("MUTATE", (time % 1000)+"ms.");
setVariable("MUTATE", "[BLUE]"+(time % 1000)+"ms.");
// setVariable("MUTATE", "{OCEAN}"+(time % 1000)+"ms.");
else
// setVariable("MUTATE", (time % 1000)+"MS!!!");
setVariable("MUTATE", "[RED]"+(time % 1000)+"MS!!!");
// setVariable("MUTATE", "{RAINBOW}"+(time % 1000)+"MS!!!");
parseTokens();
}
};
final TypingLabel label = new TypingLabel(text.toString(), font)
// {
// @Override
// public void act(float delta) {
// super.act(delta);
// long time = System.currentTimeMillis();
// if(time % 5000 >= 1000)
//// setVariable("MUTATE", (time % 1000)+"ms.");
// setVariable("MUTATE", "[BLUE]"+(time % 1000)+"ms.");
//// setVariable("MUTATE", "{OCEAN}"+(time % 1000)+"ms.");
// else
//// setVariable("MUTATE", (time % 1000)+"MS!!!");
// setVariable("MUTATE", "[RED]"+(time % 1000)+"MS!!!");
//// setVariable("MUTATE", "{RAINBOW}"+(time % 1000)+"MS!!!");
// parseTokens();
// }
// }
;
label.setAlignment(Align.left);
label.setDefaultToken("{EASE}{FADE=0;1;0.33}");
label.setVariable("MUTATE", "[GREEN]Oh yeah!");
Expand Down
2 changes: 1 addition & 1 deletion versions/latest/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ regExodusVersion=0.1.15
gwtFrameworkVersion=2.10.0
gwtPluginVersion=1.1.19
androidPluginVersion=8.1.4
gdxTeaVMVersion=1.0.0-b8
gdxVersion=1.12.1
gdxTeaVMVersion=1.0.0-b8
teaVMVersion=0.10.0-dev-5

0 comments on commit 63558a6

Please sign in to comment.