public TextStyles GetTextStyles(string text, TextSettings baseStyle) { TextSettings style2 = baseStyle.Clone(); style2.HasShadow = true; style2.ShadowDepth = 2; style2.ShadowColor = new Microsoft.Xna.Framework.Color(255, 0, 0, 255); TextSettings style3 = baseStyle.Clone(); style3.Monospaced = true; style3.MonospaceSize = 9; TextSettings[] styles = new TextSettings[] { baseStyle, style2, style3, }; List <int> switches = new List <int>() { 0 }; List <int> switchStyles = new List <int>() { 0 }; bool incode = false; bool inshadow = false; for (int i = 0; i < text.Length; i++) { char c = text[i]; if (c == '`') { if (!incode) { incode = true; switches.Add(i); switchStyles.Add(2); } else { incode = false; switches.Add(i + 1); switchStyles.Add(0); } } else if (c == '*') { if (!inshadow) { inshadow = true; switches.Add(i); switchStyles.Add(1); } else { inshadow = false; switches.Add(i + 1); switchStyles.Add(0); } } } return(new TextStyles(styles, switches.ToArray(), switchStyles.ToArray())); }
public TextStyles GetTextStyles(string text, TextSettings baseStyle) { // note: expects a monospaced basestyle int shadowDepth = 1; if (baseStyle.FontSize > 11) { shadowDepth = 2; } void applyStyle(TextSettings style, Color?color, bool isShadow) { if (color == null) { return; } if (isShadow) { style.HasShadow = true; style.ShadowDepth = shadowDepth; style.ShadowColor = color.Value; } else { style.Color = color.Value; } } TextSettings stylevar = baseStyle.Clone(); applyStyle(stylevar, ColorVar, ShadowVar); TextSettings stylefunc = baseStyle.Clone(); applyStyle(stylefunc, ColorFunc, ShadowFunc); TextSettings stylelib = baseStyle.Clone(); applyStyle(stylelib, ColorLib, ShadowLib); TextSettings styledefn = baseStyle.Clone(); applyStyle(styledefn, ColorDefn, ShadowDefn); TextSettings styleobj = baseStyle.Clone(); applyStyle(styleobj, ColorObj, ShadowObj); TextSettings stylekey = baseStyle.Clone(); applyStyle(stylekey, ColorKey, ShadowKey); TextSettings stylenum = baseStyle.Clone(); applyStyle(stylenum, ColorNum, ShadowNum); TextSettings stylestring = baseStyle.Clone(); applyStyle(stylestring, ColorString, ShadowString); TextSettings stylecomment = baseStyle.Clone(); applyStyle(stylecomment, ColorComment, ShadowComment); stylecomment.Monospaced = false; TextSettings stylearg = baseStyle.Clone(); applyStyle(stylearg, ColorArg, ShadowArg); TextSettings stylecast = baseStyle.Clone(); applyStyle(stylecast, ColorCast, ShadowCast); TextSettings[] styles = new TextSettings[] { baseStyle, // 0 stylevar, // 1 stylefunc, // 2 stylelib, // 3 styledefn, // 4 styleobj, // 5 stylekey, // 6 stylenum, // 7 stylestring, // 8 stylecomment, // 9 stylearg, // 10 stylecast, // 10 }; List <int> switches = new List <int>() { 0 }; List <int> switchStyles = new List <int>() { 0 }; eStyleType cur = eStyleType.BASE; bool singlequotestring = false; int keybypass = 0; void change(int index, eStyleType type) { cur = type; switches.Add(index); switchStyles.Add((int)cur); } bool safecheck(int index, string word) { if (index + word.Length > text.Length) { return(false); } for (int i = 0; i < word.Length; i++) { if (text[index + i] != word[i]) { return(false); } } return(true); } bool keycheck(int index, string word) { if (safecheck(index, word)) { keybypass = word.Length; change(index, eStyleType.KEY); return(true); } return(false); } text = text.ToLower(); char lastc = ' '; for (int i = 0; i < text.Length; i++) { if (keybypass > 0) { keybypass--; if (keybypass == 0) { change(i, eStyleType.BASE); } continue; } char c = text[i]; if (cur == eStyleType.NUM && !IsNumber(c)) { change(i, eStyleType.BASE); } if (cur == eStyleType.STRING) { if ((singlequotestring && c == '\'') || (!singlequotestring && c == '"')) { change(i + 1, eStyleType.BASE); singlequotestring = false; } } else { bool lastwasletter = Char.IsLetter(lastc); if (c == '/' && i + 1 < text.Length && text[i + 1] == '/') { change(i, eStyleType.COMMENT); break; // after a comment, no more is accepted } else if (c == '"') { change(i, eStyleType.STRING); } else if (c == '\'') { singlequotestring = true; change(i, eStyleType.STRING); } else if (!lastwasletter && c == 'v' && i + 1 < text.Length && text[i + 1] == ':') { change(i, eStyleType.VAR); } else if (!lastwasletter && c == 'f' && i + 1 < text.Length && text[i + 1] == ':') { change(i, eStyleType.FUNC); } else if (!lastwasletter && c == 'l' && i + 1 < text.Length && text[i + 1] == ':') { change(i, eStyleType.LIB); } else if (!lastwasletter && c == 'd' && i + 1 < text.Length && text[i + 1] == ':') { change(i, eStyleType.DEFN); } else if (!lastwasletter && c == 'o' && i + 1 < text.Length && text[i + 1] == ':') { change(i, eStyleType.OBJ); } else if (!lastwasletter && c == 'a' && i + 1 < text.Length && text[i + 1] == ':') { change(i, eStyleType.ARG); } else if (!lastwasletter && c == 'c' && i + 1 < text.Length && text[i + 1] == ':') { change(i, eStyleType.CAST); } else if (cur != eStyleType.NUM && c == '-' && i + 1 < text.Length && IsNumber(text[i + 1])) { // special case for negative numbers change(i, eStyleType.NUM); } else if (c == ' ' || IsOperator(c)) { change(i, eStyleType.BASE); } else if (cur == eStyleType.BASE && IsNumber(c)) { change(i, eStyleType.NUM); } else if (c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']') { change(i, eStyleType.BASE); } else if (cur == eStyleType.BASE || cur == eStyleType.NUM) { // checks for keywords, only if we're on base or num if (keycheck(i, "if") || keycheck(i, "while") || keycheck(i, "for") || keycheck(i, "else") || keycheck(i, "return") || keycheck(i, "defn") || keycheck(i, "object") || keycheck(i, "import") || keycheck(i, "anon") || keycheck(i, "true") || keycheck(i, "false") || keycheck(i, "free")) { // don't actually need to do anything here // the OR here is just so that this check stops // checking further words once one is matched } } } lastc = c; } return(new TextStyles(styles, switches.ToArray(), switchStyles.ToArray())); }