Created
December 7, 2016 18:36
-
-
Save jackmott/7df96e8ca8519cff65dd8c1ba11bb6a6 to your computer and use it in GitHub Desktop.
Turning a simple XML style format into a List of objects that MonoGame can render
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<GalaxyText> Parse(string text, | |
SpriteFont font, | |
SpriteFont fontBold, | |
SpriteFont fontItalic, | |
SpriteFont fontBoldItalic) | |
{ | |
var result = new List<GalaxyText>(); | |
var state = new Stack<ParseState>(); | |
state.Push(new ParseState(font, Color.White)); | |
char[] cArr = text.ToCharArray(); | |
StringBuilder currText = new StringBuilder(); | |
int i = 0; | |
while (i < cArr.Length) | |
{ | |
while (i < cArr.Length && cArr[i] != '<') | |
{ | |
currText.Append(cArr[i]); | |
i++; | |
} | |
result.Add(new GalaxyText(currText.ToString(), state.Peek().Font, state.Peek().Color)); | |
currText.Clear(); | |
string tag = string.Empty; | |
i++; //go past the opening bracket | |
while (i < cArr.Length && cArr[i] != '>') | |
{ | |
tag += cArr[i]; | |
i++; | |
} | |
i++; // go past the close backet | |
tag = tag.Trim().ToLower(); | |
if (tag.Length == 0) break; //eof | |
if (tag == "b") | |
{ | |
SpriteFont currentFont = state.Peek().Font; | |
if (currentFont == fontItalic || currentFont == fontBoldItalic) | |
state.Push(new ParseState(fontBoldItalic, state.Peek().Color)); | |
else | |
state.Push(new ParseState(fontBold, state.Peek().Color)); | |
} | |
else if (tag == "i") | |
{ | |
SpriteFont currentFont = state.Peek().Font; | |
if (currentFont == fontBold || currentFont == fontBoldItalic) | |
state.Push(new ParseState(fontBoldItalic, state.Peek().Color)); | |
else | |
state.Push(new ParseState(fontItalic, state.Peek().Color)); | |
} | |
else if (GalaxyColor.StrIsColor(tag)) | |
{ | |
state.Push(new ParseState(state.Peek().Font, GalaxyColor.StrToColor(tag))); | |
} | |
else if (tag[0] == '/') //closing tag | |
{ | |
state.Pop(); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment