Beispiel #1
0
 /// <summary>
 /// Instantiates new instance
 /// </summary>
 /// <param name="donkeyScroller">Parent DonkeyScroller</param>
 public Line(DonkeyScroller donkeyScroller)
 {
     _donkeyScroller = donkeyScroller;
 }
Beispiel #2
0
 /// <summary>
 /// Instance
 /// </summary>
 /// <param name="donkeyScroller">The owner</param>
 public LineRenderer(DonkeyScroller donkeyScroller)
 {
     _donkeyScroller = donkeyScroller;
 }
Beispiel #3
0
        /// <summary>
        /// Parses text to a <see cref="Line"/>
        /// </summary>
        /// <param name="text">The text to parse</param>
        /// <param name="donkeyScroller">The owner for the new <see cref="Line"/></param>
        /// <returns>New <see cref="Line"/></returns>
        public static Line ParseLineText(string text, DonkeyScroller donkeyScroller)
        {
            text = TrimLineTextRightSide(text);

            text = UrlRegex.Replace(text, ControlChar.Url + "$0" + ControlChar.Url);

            Line  line          = new Line(donkeyScroller);
            bool  bold          = false;
            bool  underline     = false;
            bool  strikethrough = false;
            bool  italic        = false;
            bool  markingUrl    = false;
            int   urlStartIndex = -1;
            Color foreColor     = Color.Black;
            Color backColor     = Color.Transparent;
            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            CharData currentCharFormat = new CharData(donkeyScroller.Font, foreColor, backColor);

            // ReSharper enable ConditionIsAlwaysTrueOrFalse

            for (int i = 0; i < text.Length; i++)
            {
                char c = text[i];
                switch (c)
                {
                case ControlChar.Bold:
                    bold = !bold;
                    break;

                case ControlChar.StrikeThrough:
                    strikethrough = !strikethrough;
                    break;

                case ControlChar.Italic:
                    italic = !italic;
                    break;

                case ControlChar.Underline:
                case ControlChar.Underline2:
                    underline = !underline;
                    break;

                case ControlChar.HexColor:
                {
                    ParseHexColors(text, ref i, out foreColor, out backColor);
                    break;
                }

                case ControlChar.Color:
                {
                    // Remember current background color
                    Color previousBackColor = backColor;

                    ParseIRCColors(text, ref i, out foreColor, out backColor);

                    // If the parsed color has NO backgroundcolor but the previous did, restore it
                    // This way we can change foreground color but keep the current background color
                    if (backColor == Color.Transparent && previousBackColor != Color.Transparent)
                    {
                        backColor = previousBackColor;
                    }

                    break;
                }

                case ControlChar.Url:
                {
                    if (!markingUrl)
                    {
                        urlStartIndex = line.Count;
                    }
                    else
                    {
                        int    urlEndIndex = line.Count;
                        string url         = line.GetRange(urlStartIndex, urlEndIndex - urlStartIndex).Aggregate("", (current, thisChar) => current + thisChar.C);
                        line.InteractiveUrls.Add(urlStartIndex, urlEndIndex - 1, url);
                    }
                    markingUrl = !markingUrl;
                    break;
                }

                case ControlChar.InteractiveTextEnd:
                {
                    // do nothing, just eat it as it was handled in the case below
                    break;
                }

                case ControlChar.InteractiveTextStart:
                {
                    string interactiveData  = "";
                    int    interactiveStart = line.Count;
                    bool   doContinue       = true;
                    for (++i; i < text.Length && doContinue; i++)
                    {
                        char thisC = text[i];
                        if (thisC == ControlChar.InteractiveTextSplit)
                        {
                            int textStart = i + 1;
                            for (++i; i < text.Length && doContinue; i++)
                            {
                                thisC = text[i];
                                if (thisC == ControlChar.InteractiveTextEnd)
                                {
                                    string interactiveText = text.Substring(textStart, i - textStart);
                                    line.InteractiveTexts.Add(interactiveStart, interactiveStart + interactiveText.Length - 1, interactiveData, interactiveText);
                                    i          = textStart - 3;
                                    doContinue = false;
                                }
                            }
                        }
                        else
                        {
                            interactiveData += thisC;
                        }
                    }
                    break;
                }

                default:
                    FontStyle fontStyle     = Utils.GetFontStyle(bold, underline, italic, strikethrough);
                    CharData  newCharFormat = new CharData(new Font(donkeyScroller.Font, fontStyle), foreColor, backColor);
                    line.Add(new Char
                    {
                        C    = c,
                        Data = !newCharFormat.Equals(currentCharFormat) ? newCharFormat : null
                    });
                    currentCharFormat = newCharFormat;
                    break;
                }
            }
            line.CalculateDimensions();
            return(line);
        }