public static void CalculateLines(List<UITextEditLine> m_Lines, List<string> newWordsArray, TextStyle TextStyle, float lineWidth, float spaceWidth, Vector2 topLeftIconSpace, float lineHeight)
        {
            var currentLine = new StringBuilder();
            var currentLineWidth = 0.0f;
            var currentLineNum = 0;

            for (var i = 0; i < newWordsArray.Count; i++)
            {
                var allowedWidth = (currentLineNum*lineHeight<topLeftIconSpace.Y)?lineWidth-topLeftIconSpace.X:lineWidth;
                var word = newWordsArray[i];

                if (word == "\r\n")
                {
                    /** Line break **/
                    m_Lines.Add(new UITextEditLine
                    {
                        Text = currentLine.ToString(),
                        LineWidth = currentLineWidth,
                        LineNumber = currentLineNum,
                        WhitespaceSuffix = 2
                    });
                    currentLineNum++;
                    currentLine = new StringBuilder();

                    currentLineWidth = 0;
                }
                else
                {
                    bool wordWritten = false;
                    while (!wordWritten) //repeat until the full word is written (as part of it can be written each pass if it is too long)
                    {
                        var wordSize = TextStyle.MeasureString(word);

                        if (wordSize.X > allowedWidth)
                        {
                            //SPECIAL CASE, word is bigger than line width and cannot fit on its own line
                            if (currentLineWidth > 0)
                            {
                                //if there are words on this line, we'll start this one on the next to get the most space for it
                                m_Lines.Add(new UITextEditLine
                                {
                                    Text = currentLine.ToString(),
                                    LineWidth = currentLineWidth,
                                    LineNumber = currentLineNum
                                });
                                currentLineNum++;
                                currentLine = new StringBuilder();
                                currentLineWidth = 0;
                            }

                            float width = allowedWidth + 1;
                            int j = word.Length;
                            while (width > allowedWidth)
                            {
                                width = TextStyle.MeasureString(word.Substring(0, --j)).X;
                            }
                            currentLine.Append(word.Substring(0, j));
                            currentLineWidth += width;
                            word = word.Substring(j);

                            m_Lines.Add(new UITextEditLine
                            {
                                Text = currentLine.ToString(),
                                LineWidth = currentLineWidth,
                                LineNumber = currentLineNum,
                                WhitespaceSuffix = 1
                            });

                            currentLineNum++;
                            currentLine = new StringBuilder();
                            currentLineWidth = 0;
                        }
                        else if (currentLineWidth + wordSize.X < allowedWidth)
                        {
                            currentLine.Append(word);
                            if (i != newWordsArray.Count - 1) { currentLine.Append(' '); currentLineWidth += spaceWidth; }
                            currentLineWidth += wordSize.X;
                            wordWritten = true;
                        }
                        else
                        {
                            /** New line **/
                            m_Lines.Add(new UITextEditLine
                            {
                                Text = currentLine.ToString(),
                                LineWidth = currentLineWidth,
                                LineNumber = currentLineNum,
                                WhitespaceSuffix = 1
                            });
                            currentLineNum++;
                            currentLine = new StringBuilder();
                            currentLine.Append(word);
                            currentLineWidth = wordSize.X;
                            if (i != newWordsArray.Count - 1) { currentLine.Append(' '); currentLineWidth += spaceWidth; }
                            wordWritten = true;
                        }
                    }
                }
            }

            m_Lines.Add(new UITextEditLine //add even if length is 0, so we can move the cursor down!
            {
                Text = currentLine.ToString(),
                LineWidth = currentLineWidth,
                LineNumber = currentLineNum
            });

            var currentIndex = 0;
            foreach (var line in m_Lines)
            {
                line.StartIndex = currentIndex;
                currentIndex += (line.Text.Length - 1) + line.WhitespaceSuffix;
            }
        }
        public static void CalculateLines(List<UITextEditLine> m_Lines, List<string> newWordsArray, TextStyle TextStyle, float lineWidth, float spaceWidth)
        {
            var currentLine = new StringBuilder();
            var currentLineWidth = 0.0f;
            var currentLineNum = 0;

            for (var i = 0; i < newWordsArray.Count; i++)
            {
                var word = newWordsArray[i];

                if (word == "\r\n")
                {
                    /** Line break **/
                    m_Lines.Add(new UITextEditLine
                    {
                        Text = currentLine.ToString(),
                        LineWidth = currentLineWidth,
                        LineNumber = currentLineNum,
                        WhitespaceSuffix = 2
                    });
                    currentLineNum++;
                    currentLine = new StringBuilder();

                    currentLineWidth = 0;
                }
                else
                {
                    var wordSize = TextStyle.MeasureString(word);

                    if (currentLineWidth + wordSize.X < lineWidth)
                    {
                        currentLine.Append(word);
                        currentLine.Append(' ');
                        currentLineWidth += wordSize.X;
                        currentLineWidth += spaceWidth;
                    }
                    else
                    {
                        /** New line **/
                        m_Lines.Add(new UITextEditLine
                        {
                            Text = currentLine.ToString(),
                            LineWidth = currentLineWidth,
                            LineNumber = currentLineNum
                        });
                        currentLineNum++;
                        currentLine = new StringBuilder();
                        currentLine.Append(word);
                        currentLine.Append(' ');

                        currentLineWidth = wordSize.X + spaceWidth;
                    }
                }
            }

            if (currentLine.Length > 0)
            {
                m_Lines.Add(new UITextEditLine
                {
                    Text = currentLine.ToString(),
                    LineWidth = currentLineWidth,
                    LineNumber = currentLineNum
                });
            }

            var currentIndex = 0;
            foreach (var line in m_Lines)
            {
                line.StartIndex = currentIndex;
                currentIndex += (line.Text.Length - 1) + line.WhitespaceSuffix;
            }
        }
        public static void CalculateLines(List <UITextEditLine> m_Lines, List <string> newWordsArray, TextStyle TextStyle, float lineWidth, float spaceWidth)
        {
            var currentLine      = new StringBuilder();
            var currentLineWidth = 0.0f;
            var currentLineNum   = 0;

            for (var i = 0; i < newWordsArray.Count; i++)
            {
                var word = newWordsArray[i];

                if (word == "\r\n")
                {
                    /** Line break **/
                    m_Lines.Add(new UITextEditLine
                    {
                        Text             = currentLine.ToString(),
                        LineWidth        = currentLineWidth,
                        LineNumber       = currentLineNum,
                        WhitespaceSuffix = 2
                    });
                    currentLineNum++;
                    currentLine = new StringBuilder();

                    currentLineWidth = 0;
                }
                else
                {
                    var wordSize = TextStyle.MeasureString(word);

                    if (currentLineWidth + wordSize.X < lineWidth)
                    {
                        currentLine.Append(word);
                        currentLine.Append(' ');
                        currentLineWidth += wordSize.X;
                        currentLineWidth += spaceWidth;
                    }
                    else
                    {
                        /** New line **/
                        m_Lines.Add(new UITextEditLine
                        {
                            Text       = currentLine.ToString(),
                            LineWidth  = currentLineWidth,
                            LineNumber = currentLineNum
                        });
                        currentLineNum++;
                        currentLine = new StringBuilder();
                        currentLine.Append(word);
                        currentLine.Append(' ');

                        currentLineWidth = wordSize.X + spaceWidth;
                    }
                }
            }

            if (currentLine.Length > 0)
            {
                m_Lines.Add(new UITextEditLine
                {
                    Text       = currentLine.ToString(),
                    LineWidth  = currentLineWidth,
                    LineNumber = currentLineNum
                });
            }

            var currentIndex = 0;

            foreach (var line in m_Lines)
            {
                line.StartIndex = currentIndex;
                currentIndex   += (line.Text.Length - 1) + line.WhitespaceSuffix;
            }
        }
Esempio n. 4
0
        public static void CalculateLines(List <UITextEditLine> m_Lines, List <string> newWordsArray, TextStyle TextStyle, float lineWidth, float spaceWidth)
        {
            var currentLine      = new StringBuilder();
            var currentLineWidth = 0.0f;
            var currentLineNum   = 0;

            for (var i = 0; i < newWordsArray.Count; i++)
            {
                var word = newWordsArray[i];

                if (word == "\r\n")
                {
                    /** Line break **/
                    m_Lines.Add(new UITextEditLine
                    {
                        Text             = currentLine.ToString(),
                        LineWidth        = currentLineWidth,
                        LineNumber       = currentLineNum,
                        WhitespaceSuffix = 2
                    });
                    currentLineNum++;
                    currentLine = new StringBuilder();

                    currentLineWidth = 0;
                }
                else
                {
                    bool wordWritten = false;
                    while (!wordWritten) //repeat until the full word is written (as part of it can be written each pass if it is too long)
                    {
                        var wordSize = TextStyle.MeasureString(word);

                        if (wordSize.X > lineWidth)
                        {
                            //SPECIAL CASE, word is bigger than line width and cannot fit on its own line
                            if (currentLineWidth > 0)
                            {
                                //if there are words on this line, we'll start this one on the next to get the most space for it
                                m_Lines.Add(new UITextEditLine
                                {
                                    Text       = currentLine.ToString(),
                                    LineWidth  = currentLineWidth,
                                    LineNumber = currentLineNum
                                });
                                currentLineNum++;
                                currentLine      = new StringBuilder();
                                currentLineWidth = 0;
                            }

                            float width = lineWidth + 1;
                            int   j     = word.Length;
                            while (width > lineWidth)
                            {
                                width = TextStyle.MeasureString(word.Substring(0, --j)).X;
                            }
                            currentLine.Append(word.Substring(0, j));
                            currentLineWidth += width;
                            word              = word.Substring(j);

                            m_Lines.Add(new UITextEditLine
                            {
                                Text             = currentLine.ToString(),
                                LineWidth        = currentLineWidth,
                                LineNumber       = currentLineNum,
                                WhitespaceSuffix = 1
                            });

                            currentLineNum++;
                            currentLine      = new StringBuilder();
                            currentLineWidth = 0;
                        }
                        else if (currentLineWidth + wordSize.X < lineWidth)
                        {
                            currentLine.Append(word);
                            if (i != newWordsArray.Count - 1)
                            {
                                currentLine.Append(' '); currentLineWidth += spaceWidth;
                            }
                            currentLineWidth += wordSize.X;
                            wordWritten       = true;
                        }
                        else
                        {
                            /** New line **/
                            m_Lines.Add(new UITextEditLine
                            {
                                Text             = currentLine.ToString(),
                                LineWidth        = currentLineWidth,
                                LineNumber       = currentLineNum,
                                WhitespaceSuffix = 1
                            });
                            currentLineNum++;
                            currentLine = new StringBuilder();
                            currentLine.Append(word);
                            currentLineWidth = wordSize.X;
                            if (i != newWordsArray.Count - 1)
                            {
                                currentLine.Append(' '); currentLineWidth += spaceWidth;
                            }
                            wordWritten = true;
                        }
                    }
                }
            }

            m_Lines.Add(new UITextEditLine //add even if length is 0, so we can move the cursor down!
            {
                Text       = currentLine.ToString(),
                LineWidth  = currentLineWidth,
                LineNumber = currentLineNum
            });

            var currentIndex = 0;

            foreach (var line in m_Lines)
            {
                line.StartIndex = currentIndex;
                currentIndex   += (line.Text.Length - 1) + line.WhitespaceSuffix;
            }
        }