public BackColor(string _HexColor) { Color c; HexUtil.HexToColor(_HexColor, out c); bgColor = new Texture2D(1, 1); bgColor.SetPixel(0, 0, c); bgColor.wrapMode = TextureWrapMode.Repeat; bgColor.Apply(); }
public void draw() { TextAlignment textAlignment = TextAlignment.Left; GUILayout.BeginVertical(); GUILayout.BeginHorizontal(); GUIStyle gUIStyle = new GUIStyle(); gUIStyle.normal.textColor = GUI.skin.GetStyle("Label").normal.textColor; gUIStyle.font = GUI.skin.font; gUIStyle.border = new RectOffset(0, 0, 0, 0); gUIStyle.contentOffset = new Vector2(0f, 0f); gUIStyle.margin = new RectOffset(0, 0, 0, 0); gUIStyle.padding = new RectOffset(0, 0, 0, 0); _defaultColor = gUIStyle.normal.textColor; _defaultBackgroundColor = GUI.skin.GetStyle("Label").normal.background; int num = mDrawlength; foreach (string line in _lines) { if (num == 0) { break; } int num2 = 0; if (line.Length >= 5 && line.StartsWith("[HA ")) { num2 = 6; switch (line[4]) { case 'L': textAlignment = TextAlignment.Left; break; case 'R': textAlignment = TextAlignment.Right; break; case 'C': textAlignment = TextAlignment.Center; break; } } if (textAlignment == TextAlignment.Right || textAlignment == TextAlignment.Center) { GUILayout.FlexibleSpace(); } while (num2 < line.Length) { int num3 = line.IndexOf('[', num2); if (num3 == num2 && num3 + 1 < line.Length && line[num3 + 1] == '[') { if (num != -1) { num--; } drawText(gUIStyle, line.Substring(num2, 1)); num2 += 2; } else if (num3 == num2) { int num4 = line.IndexOf(']', num3); num2 = num4 + 1; if (num4 < num3) { Debug.Log("<< length: " + line.Length + " command start: " + num3 + " commandEnd: " + num4); Debug.Log("line: " + line); return; } string[] array = line.Substring(num3 + 1, num4 - num3 - 1).Split(' '); switch (array[0]) { case "BC": if (array[1] == "?") { gUIStyle.normal.background = _defaultBackgroundColor; } else { Color color; HexUtil.HexToColor(array[1], out color); _backgroundColor = new Texture2D(1, 1); _backgroundColor.SetPixel(0, 0, color); _backgroundColor.wrapMode = TextureWrapMode.Repeat; _backgroundColor.Apply(); gUIStyle.normal.background = _backgroundColor; } break; case "C": if (array[1] == "?") { gUIStyle.normal.textColor = _defaultColor; } else { Color color2; HexUtil.HexToColor(array[1], out color2); gUIStyle.normal.textColor = color2; } break; case "F": gUIStyle.font = (Font)Resources.Load("Fonts/" + array[1]); break; case "FA": if (array[1] == "U") { _fontUnderline = true; } else if (array[1] == "-U") { _fontUnderline = false; } else if (array[1] == "S") { _fontStrikethrough = true; } else if (array[1] == "-S") { _fontStrikethrough = false; } break; case "FS": gUIStyle.fontSize = int.Parse(array[1]); break; case "H": _createHyperlinkId = "Hyperlink_" + array[1]; break; case "-H": _createHyperlinkId = string.Empty; break; case "LH": _lineHeight = float.Parse(array[1]); break; case "S": GUILayout.Space(float.Parse(array[1])); break; case "VA": switch (array[1]) { case "?": _verticalAlignment = VerticalAlignment.Default; break; case "B": _verticalAlignment = VerticalAlignment.Bottom; break; } break; } } else if (num3 == -1) { string text = line.Substring(num2); if (num != -1) { if (text.Length > num) { text = text.Substring(0, num); num = 0; } else { num -= text.Length; } } drawText(gUIStyle, text); num2 = line.Length; } else { string text2 = line.Substring(num2, num3 - num2); if (num != -1) { if (text2.Length > num) { text2 = text2.Substring(0, num); num = 0; } else { num -= text2.Length; } } drawText(gUIStyle, text2); num2 = num3; } if (num == 0) { break; } } if (textAlignment == TextAlignment.Left || textAlignment == TextAlignment.Center) { GUILayout.FlexibleSpace(); } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); } GUILayout.EndHorizontal(); GUILayout.EndVertical(); handleHyperlink(); }
private void format(string text) { if (text == null) { Logger.traceError("[TextFormatter::format] - text is null"); return; } _guiStyle = new GUIStyle(); _line = new StringBuilder(); addLineHeight(realHeight: false); _lineLength = 0f; _lineHeight = 0f; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < text.Length; i++) { int num; if (text[i] == '\\' && text.Length > i + 1 && text[i + 1] == '\\') { stringBuilder.Append("\\"); i++; } else if (text[i] == '\n') { addWordToLine(stringBuilder.ToString()); createNewLine(); stringBuilder.Length = 0; } else if (text[i] == ' ' && stringBuilder.Length != 0) { addWordToLine(stringBuilder.ToString()); stringBuilder.Length = 0; stringBuilder.Append(' '); } else if (text[i] == '[' && text.Length > i + 1 && text[i + 1] == '[') { stringBuilder.Append("[["); i++; } else if (text[i] == '[' && text.Length > i + 1 && (num = text.IndexOf(']', i)) != -1) { addWordToLine(stringBuilder.ToString()); stringBuilder.Length = 0; string text2 = text.Substring(i + 1, num - i - 1); i += text2.Length + 1; string[] array = text2.Split(' '); for (int j = 0; j < array.Length; j++) { switch (array[j].ToUpper()) { case "BC": case "BACKCOLOR": if (array.Length > j + 1) { j++; Color c; if (array[j] == "?" || HexUtil.HexToColor(array[j], out c)) { addCommandToLine("BC " + array[j]); } else { Debug.LogError("The 'BackColor' command requires a color parameter of RRGGBBAA or '?'."); } } else { Debug.LogError("The 'BackColor' command requires a color parameter of RRGGBBAA or '?'."); } break; case "C": case "COLOR": if (array.Length > j + 1) { j++; Color c; if (array[j] == "?" || HexUtil.HexToColor(array[j], out c)) { addCommandToLine("C " + array[j]); } else { Debug.LogError("The 'color' command requires a color parameter of RRGGBBAA or '?'."); } } else { Debug.LogError("The 'color' command requires a color parameter of RRGGBBAA:\n" + text); } break; case "F": case "FONT": if (array.Length > j + 1) { j++; Font font = (Font)Resources.Load("Fonts/" + array[j]); if (font == null) { Debug.LogError("The font '" + array[j] + "' does not exist within Assets/Resources/Fonts/"); } else { _guiStyle.font = font; addCommandToLine("F " + array[j]); } if (array.Length > j + 1) { j++; int result; if (int.TryParse(array[j], out result)) { addCommandToLine("FS " + array[j]); _guiStyle.fontSize = result; } else { Debug.LogError("The font size '" + array[j] + "' is not a valid integer"); } } } else { Debug.LogError("The 'font' command requires a font name parameter and an optional font size parameter."); } break; case "FA": case "FONTATTRIBUTE": if (array.Length > j + 1) { j++; string text5; switch (array[j].ToUpper()) { case "U": case "UNDERLINE": text5 = "U"; break; case "-U": case "-UNDERLINE": text5 = "-U"; break; case "S": case "STRIKETHROUGH": text5 = "S"; break; case "-S": case "-STRIKETHROUGH": text5 = "-S"; break; default: text5 = string.Empty; Debug.LogError("The 'font attribute' command requires a font parameter of U (underline on), -U (underline off), S (strikethrough on) or -S (strikethrough off)."); break; } if (text5.Length != 0) { addCommandToLine("FA " + text5); } } else { Debug.LogError("The 'font attribute' command requires a font parameter of U (underline on), -U (underline off), S (strikethrough on) or -S (strikethrough off)."); } break; case "FS": case "FONTSIZE": if (array.Length > j + 1) { j++; int result3; if (int.TryParse(array[j], out result3)) { addCommandToLine("FS " + array[j]); _guiStyle.fontSize = result3; } else { Debug.LogError("The font size '" + array[j] + "' is not a valid integer"); } } else { Debug.LogError("The 'font size' command requires a font size parameter."); } break; case "H": case "HYPERLINK": if (array.Length > j + 1) { j++; addCommandToLine("H " + array[j]); } else { Debug.LogError("The 'hyperlink' command requires an hyperlink id parameter."); } break; case "-H": case "-HYPERLINK": addCommandToLine("-H"); break; case "HA": case "HALIGN": if (array.Length > j + 1) { j++; string text4; switch (array[j].ToUpper()) { case "L": case "LEFT": text4 = "L"; break; case "R": case "RIGHT": text4 = "R"; break; case "C": case "CENTER": text4 = "C"; break; default: text4 = string.Empty; Debug.LogError("The 'HAlign' command requires an alignment parameter of L (left), R (right), or C (center)."); break; } if (text4.Length != 0) { addCommandToLine("HA " + text4); } } else { Debug.LogError("The 'HAlign' command requires an alignment parameter of L (left), R (right), or C (center)."); } break; case "S": case "SPACE": if (array.Length > j + 1) { j++; int result2; if (int.TryParse(array[j], out result2)) { addCommandToLine("S " + array[j]); _lineLength += result2; } else { Debug.LogError("The space size '" + array[j] + "' is not a valid integer"); } } else { Debug.LogError("The 'space' command requires a pixel count parameter."); } break; case "VA": case "VALIGN": if (array.Length > j + 1) { j++; string text3; switch (array[j].ToUpper()) { default: { // TODO: Find out what this is int num2 = 1; if (num2 == 1) { text3 = "B"; break; } text3 = string.Empty; Debug.LogError("The 'VAlign' command requires an alignment parameter of ? (default) or B (bottom)."); break; } case "?": text3 = "?"; break; } if (text3.Length != 0) { addCommandToLine("VA " + text3); } } else { Debug.LogError("The 'VAlign' command requires an alignment parameter of ? (default) or B (bottom)."); } break; } } } else { stringBuilder.Append(text[i]); } } addWordToLine(stringBuilder.ToString()); addLineToLines(); }
/// <summary> /// Format the raw text into an easier (aka faster) format to parse. /// * Process \n such that they are removed and 'new lines' created /// * Break down the text into lines that fit the requested width /// </summary> /// <param name="text">The raw text to parse</param> private void format(string text) { //Debug.Log("Formatting: " + text); _guiStyle = new GUIStyle(); int endIndex; _line = new StringBuilder(); _fLine = new FormattedLine(); addLineHeight(false); _lineLength = 0; _lineHeight = 0.0f; StringBuilder word = new StringBuilder(); _sequence = new Sequence(); _nextSequence = new Sequence(); for (int letterIndex = 0; letterIndex < text.Length; letterIndex++) { int currentLetterIndex = letterIndex; if (text[letterIndex] == '\\' && text.Length > letterIndex + 1 && text[letterIndex + 1] == '\\') { // Escaped '\' word.Append("\\"); letterIndex++; // Skip the second '\' } else if (text[letterIndex] == '\n') { // New line addWordToLine(word.ToString()); createNewLine(); word.Length = 0; } else if (text[letterIndex] == ' ' && word.Length != 0) { // Reached a word boundary addWordToLine(word.ToString()); word.Length = 0; word.Append(' '); } else if (text[letterIndex] == '[' && text.Length > letterIndex + 1 && text[letterIndex + 1] == '[') { // Escaped '[' word.Append("[["); letterIndex++; // Skip the second '[' } else if (text[letterIndex] == '[' && text.Length > letterIndex + 1 && (endIndex = text.IndexOf(']', letterIndex)) != -1) { // Command addWordToLine(word.ToString()); word.Length = 0; string command = text.Substring(letterIndex + 1, endIndex - letterIndex - 1); letterIndex += command.Length + 1; string[] commandList = command.Split(' '); for (int commandIndex = 0; commandIndex < commandList.Length; commandIndex++) { command = commandList[commandIndex].ToUpper(); if (command.IsEqual("NL") || command.IsEqual("NEWLINE")) { createNewLine(); } else if (command.IsEqual("BC") || command.IsEqual("BACKCOLOR")) //if (command == "BC" || command == "BACKCOLOR") { // Background Color if (commandList.Length > commandIndex + 1) { commandIndex++; Color color; if (commandList[commandIndex] == "?") { _nextSequence.Add(new BackColor(_guiStyle.normal.background)); addCommandToLine("BC " + commandList[commandIndex]); } else if (HexUtil.HexToColor(commandList[commandIndex], out color)) { _nextSequence.Add(new BackColor(commandList[commandIndex])); addCommandToLine("BC " + commandList[commandIndex]); } else { Debug.LogError("The 'BackColor' command requires a color parameter of RRGGBBAA or '?'."); } } else { Debug.LogError("The 'BackColor' command requires a color parameter of RRGGBBAA or '?'."); } } else if (command.IsEqual("C") || command.IsEqual("COLOR")) { // Color if (commandList.Length > commandIndex + 1) { commandIndex++; Color color; if (commandList[commandIndex] == "?" || HexUtil.HexToColor(commandList[commandIndex], out color)) { _nextSequence.Add(new FontColor(color)); addCommandToLine("C " + commandList[commandIndex]); } else { Debug.LogError("The 'color' command requires a color parameter of RRGGBBAA or '?'."); } } else { Debug.LogError("The 'color' command requires a color parameter of RRGGBBAA:\n" + text); } } else if (command.IsEqual("F") || command.IsEqual("FONT")) { // Font if (commandList.Length > commandIndex + 1) { commandIndex++; Font font = (Font)fontTable[commandList[commandIndex]]; if (font == null) { Debug.LogError("The font '" + commandList[commandIndex] + "' does not exist within Assets/Resources/Fonts/"); } else { _guiStyle.font = font; // Update the font to properly measure text addCommandToLine("F " + commandList[commandIndex]); _nextSequence.Add(new CustomFont(font)); } if (commandList.Length > commandIndex + 1) { commandIndex++; int fontSize; if (System.Int32.TryParse(commandList[commandIndex], out fontSize)) { addCommandToLine("FS " + commandList[commandIndex]); _nextSequence.Add(new FontSize(fontSize)); _guiStyle.fontSize = fontSize; // Update the size to properly measure text } else { Debug.LogError("The font size '" + commandList[commandIndex] + "' is not a valid integer"); } } } else { Debug.LogError("The 'font' command requires a font name parameter and an optional font size parameter."); } } else if (command.IsEqual("FA") || command.IsEqual("FONTATTRIBUTE")) { // Font Attribute if (commandList.Length > commandIndex + 1) { commandIndex++; string attribute = commandList[commandIndex].ToUpper(); switch (attribute) { case "U": case "UNDERLINE": attribute = "U"; _nextSequence.underline = true; break; case "-U": case "-UNDERLINE": attribute = "-U"; _nextSequence.underline = false; break; case "S": case "STRIKETHROUGH": attribute = "S"; Debug.Log("strike ? " + _nextSequence.txt); _nextSequence.strikeThrough = true; break; case "-S": case "-STRIKETHROUGH": attribute = "-S"; _nextSequence.strikeThrough = false; break; default: attribute = ""; Debug.LogError("The 'font attribute' command requires a font parameter of U (underline on), -U (underline off), S (strikethrough on) or -S (strikethrough off)."); break; } if (attribute.Length != 0) { addCommandToLine("FA " + attribute); } } else { Debug.LogError("The 'font attribute' command requires a font parameter of U (underline on), -U (underline off), S (strikethrough on) or -S (strikethrough off)."); } } else if (command.IsEqual("FS") || command.IsEqual("FONTSIZE")) { // Font Size if (commandList.Length > commandIndex + 1) { commandIndex++; int fontSize; if (System.Int32.TryParse(commandList[commandIndex], out fontSize)) { addCommandToLine("FS " + commandList[commandIndex]); _nextSequence.Add(new FontSize(fontSize)); _guiStyle.fontSize = fontSize; // Update the size to properly measure text } else { Debug.LogError("The font size '" + commandList[commandIndex] + "' is not a valid integer"); } } else { Debug.LogError("The 'font size' command requires a font size parameter."); } } else if (command.IsEqual("H") || command.IsEqual("HYPERLINK")) { // Hyperlink on if (commandList.Length > commandIndex + 1) { commandIndex++; addCommandToLine("H " + commandList[commandIndex]); _nextSequence.hyperlinkId = HYPERLINK_TAG + commandList[commandIndex]; } else { Debug.LogError("The 'hyperlink' command requires an hyperlink id parameter."); } } else if (command.IsEqual("-H") || command.IsEqual("-HYPERLINK")) { // Hyperlink off addCommandToLine("-H"); _nextSequence.hyperlinkId = ""; } else if (command.IsEqual("HA") || command.IsEqual("HALIGN")) { // Horizontal line alignment if (commandList.Length > commandIndex + 1) { commandIndex++; string alignment = commandList[commandIndex].ToUpper(); switch (alignment) { case "L": case "LEFT": alignment = "L"; _fLine.alignement = TextAlignment.Left; break; case "R": case "RIGHT": alignment = "R"; _fLine.alignement = TextAlignment.Right; break; case "C": case "CENTER": alignment = "C"; _fLine.alignement = TextAlignment.Center; break; default: alignment = ""; Debug.LogError("The 'HAlign' command requires an alignment parameter of L (left), R (right), or C (center)."); break; } if (alignment.Length != 0) { addCommandToLine("HA " + alignment); } } else { Debug.LogError("The 'HAlign' command requires an alignment parameter of L (left), R (right), or C (center)."); } } else if (command.IsEqual("S") || command.IsEqual("SPACE")) { // Space (pixels) if (commandList.Length > commandIndex + 1) { commandIndex++; int spaceSize; if (System.Int32.TryParse(commandList[commandIndex], out spaceSize)) { addCommandToLine("S " + commandList[commandIndex]); _nextSequence.Add(new AddSpace(spaceSize)); _lineLength += spaceSize; } else { Debug.LogError("The space size '" + commandList[commandIndex] + "' is not a valid integer"); } } else { Debug.LogError("The 'space' command requires a pixel count parameter."); } } else if (command.IsEqual("VA") || command.IsEqual("VALIGN")) { // Vertical alignment if (commandList.Length > commandIndex + 1) { commandIndex++; string alignment = commandList[commandIndex].ToUpper(); switch (alignment) { case "?": alignment = "?"; _nextSequence.alignBottom = false; break; case "B": case "BOTTOM": alignment = "B"; _nextSequence.alignBottom = true; break; default: alignment = ""; Debug.LogError("The 'VAlign' command requires an alignment parameter of ? (default) or B (bottom)."); break; } if (alignment.Length != 0) { addCommandToLine("VA " + alignment); } } else { Debug.LogError("The 'VAlign' command requires an alignment parameter of ? (default) or B (bottom)."); } } else { //Pass through any invalid commands or let words with brackets with out using double bracket //and decide what to do with it later invalidCommand = true; } } if (invalidCommand) { addCommandToLine(string.Format("{0}", text.Substring(currentLetterIndex + 1, endIndex - currentLetterIndex - 1))); //Debug.Log(string.Format("Invalid Command: {0}", commandList[commandIndex])); invalidCommand = false; } addSequenceToLine(); } else { // Add letter to the word word.Append(text[letterIndex]); } } addWordToLine(word.ToString()); addLineToLines(); }