/// <summary> /// Value converter for Font type. /// </summary> public override ConversionResult Convert(object value, ValueConverterContext context) { if (value == null) { return(base.Convert(value, context)); } Type valueType = value.GetType(); if (valueType == _type) { return(base.Convert(value, context)); } else if (valueType == _stringType) { var stringValue = (string)value; try { // format: "colorTopLeft;colorTopRight;colorBottomLeft;colorBottomRight" string[] colors = stringValue.Split(';'); if (colors.Length == 4) { var result1 = _colorValueConverter.Convert(colors[0], context); var result2 = _colorValueConverter.Convert(colors[1], context); var result3 = _colorValueConverter.Convert(colors[2], context); var result4 = _colorValueConverter.Convert(colors[3], context); return(result1.Success && result2.Success && result3.Success && result4.Success ? new ConversionResult(new VertexGradient((Color)result1.ConvertedValue, (Color)result2.ConvertedValue, (Color)result3.ConvertedValue, (Color)result4.ConvertedValue)) : StringConversionFailed(value)); } else if (colors.Length == 1) { var result1 = _colorValueConverter.Convert(colors[0], context); return(result1.Success ? new ConversionResult(new VertexGradient((Color)result1.ConvertedValue)) : StringConversionFailed(value)); } else { return(StringConversionFailed(value)); } } catch (Exception e) { return(ConversionFailed(value, e)); } } return(ConversionFailed(value)); }
/// <summary> /// Replaces BBCode style tags with unity rich text syntax and parses embedded views. /// </summary> public string ParseText(string text) { if (text == null) { return(String.Empty); } if (!TextComponent.supportRichText) { return(text); } string formattedText = string.Empty; string separatorString = "&sp;"; // search for tokens and apply formatting and embedded views List <TextToken> tokens = new List <TextToken>(); formattedText = _tagRegex.Replace(text, x => { string tag = x.Groups["tag"].Value.Trim(); string tagNoWs = tag.RemoveWhitespace(); // check if tag matches default tokens if (String.Equals("B", tagNoWs, StringComparison.OrdinalIgnoreCase)) { // bold tokens.Add(new TextToken { TextTokenType = TextTokenType.BoldStart }); return(separatorString); } else if (String.Equals("/B", tagNoWs, StringComparison.OrdinalIgnoreCase)) { // bold end tokens.Add(new TextToken { TextTokenType = TextTokenType.BoldEnd }); return(separatorString); } else if (String.Equals("I", tagNoWs, StringComparison.OrdinalIgnoreCase)) { // italic tokens.Add(new TextToken { TextTokenType = TextTokenType.ItalicStart }); return(separatorString); } else if (String.Equals("/I", tagNoWs, StringComparison.OrdinalIgnoreCase)) { // italic end tokens.Add(new TextToken { TextTokenType = TextTokenType.ItalicEnd }); return(separatorString); } else if (tagNoWs.StartsWith("SIZE=", StringComparison.OrdinalIgnoreCase)) { // parse size value var vc = new IntValueConverter(); var convertResult = vc.Convert(tagNoWs.Substring(5), ValueConverterContext.Empty); if (!convertResult.Success) { // unable to parse token Utils.LogError("[MarkLight] {0}: Unable to parse text embedded size tag \"[{1}]\". {2}", GameObjectName, tag, convertResult.ErrorMessage); return(String.Format("[{0}]", tag)); } tokens.Add(new TextToken { TextTokenType = TextTokenType.SizeStart, FontSize = (int)convertResult.ConvertedValue }); return(separatorString); } else if (String.Equals("/SIZE", tagNoWs, StringComparison.OrdinalIgnoreCase)) { // size end tokens.Add(new TextToken { TextTokenType = TextTokenType.SizeEnd }); return(separatorString); } else if (tagNoWs.StartsWith("COLOR=", StringComparison.OrdinalIgnoreCase)) { // parse color value var vc = new ColorValueConverter(); var convertResult = vc.Convert(tagNoWs.Substring(6), ValueConverterContext.Empty); if (!convertResult.Success) { // unable to parse token Utils.LogError("[MarkLight] {0}: Unable to parse text embedded color tag \"[{1}]\". {2}", GameObjectName, tag, convertResult.ErrorMessage); return(String.Format("[{0}]", tag)); } Color color = (Color)convertResult.ConvertedValue; tokens.Add(new TextToken { TextTokenType = TextTokenType.ColorStart, FontColor = color }); return(separatorString); } else if (String.Equals("/COLOR", tagNoWs, StringComparison.OrdinalIgnoreCase)) { // color end tokens.Add(new TextToken { TextTokenType = TextTokenType.ColorEnd }); return(separatorString); } return(String.Format("[{0}]", tag)); }); // replace newline in string formattedText = formattedText.Replace("\\n", Environment.NewLine); // split the string up on each line StringBuilder result = new StringBuilder(); var splitString = formattedText.Split(new string[] { separatorString }, StringSplitOptions.None); int splitIndex = 0; int fontBoldCount = 0; int fontItalicCount = 0; Stack <int> fontSizeStack = new Stack <int>(); // loop through each split string and apply tokens (embedded views & font styles) foreach (var str in splitString) { int tokenIndex = splitIndex - 1; var token = tokenIndex >= 0 && tokenIndex < tokens.Count ? tokens[tokenIndex] : null; ++splitIndex; // do we have a token? if (token != null) { // yes. parse token type switch (token.TextTokenType) { case TextTokenType.BoldStart: result.Append("<b>"); ++fontBoldCount; break; case TextTokenType.BoldEnd: result.Append("</b>"); --fontBoldCount; break; case TextTokenType.ItalicStart: result.Append("<i>"); ++fontItalicCount; break; case TextTokenType.ItalicEnd: result.Append("</i>"); --fontItalicCount; break; case TextTokenType.SizeStart: result.Append(String.Format("<size={0}>", token.FontSize)); fontSizeStack.Push(token.FontSize); break; case TextTokenType.SizeEnd: result.Append("</size>"); fontSizeStack.Pop(); break; case TextTokenType.ColorStart: int r = (int)(token.FontColor.r * 255f); int g = (int)(token.FontColor.g * 255f); int b = (int)(token.FontColor.b * 255f); int a = (int)(token.FontColor.a * 255f); result.Append(String.Format("<color=#{0}{1}{2}{3}>", r.ToString("X2"), g.ToString("X2"), b.ToString("X2"), a.ToString("X2"))); break; case TextTokenType.ColorEnd: result.Append("</color>"); break; case TextTokenType.Unknown: default: break; } } result.Append(str); } return(result.ToString()); }