示例#1
0
        internal void DrawWithTextSelectionAndCursor(IStylePainter painter, string newText)
        {
            var keyboardTextEditor = editorEventHandler as KeyboardTextEditorEventHandler;

            if (keyboardTextEditor == null)
            {
                return;
            }

            keyboardTextEditor.PreDrawCursor(newText);

            int     cursorIndex   = editorEngine.cursorIndex;
            int     selectIndex   = editorEngine.selectIndex;
            Rect    localPosition = editorEngine.localPosition;
            Vector2 scrollOffset  = editorEngine.scrollOffset;

            IStyle style = this.style;

            var textParams = painter.GetDefaultTextParameters(this);

            textParams.text          = " ";
            textParams.wordWrapWidth = 0.0f;
            textParams.wordWrap      = false;

            float lineHeight   = painter.ComputeTextHeight(textParams);
            float contentWidth = contentRect.width;

            Input.compositionCursorPos = editorEngine.graphicalCursorPos - scrollOffset +
                                         new Vector2(localPosition.x, localPosition.y + lineHeight);

            Color drawCursorColor = m_CursorColor.GetSpecifiedValueOrDefault(Color.grey);

            int selectionEndIndex = string.IsNullOrEmpty(Input.compositionString)
                ? selectIndex
                : cursorIndex + Input.compositionString.Length;

            // Draw the background as in VisualElement
            painter.DrawBackground(this);

            CursorPositionStylePainterParameters cursorParams;

            // Draw highlighted section, if any
            if (cursorIndex != selectionEndIndex)
            {
                var painterParams = painter.GetDefaultRectParameters(this);
                painterParams.color = selectionColor;
                painterParams.border.SetWidth(0.0f);
                painterParams.border.SetRadius(0.0f);

                int min = cursorIndex < selectionEndIndex ? cursorIndex : selectionEndIndex;
                int max = cursorIndex > selectionEndIndex ? cursorIndex : selectionEndIndex;

                cursorParams               = painter.GetDefaultCursorPositionParameters(this);
                cursorParams.text          = editorEngine.text;
                cursorParams.wordWrapWidth = contentWidth;
                cursorParams.cursorIndex   = min;

                Vector2 minPos = painter.GetCursorPosition(cursorParams);
                cursorParams.cursorIndex = max;
                Vector2 maxPos = painter.GetCursorPosition(cursorParams);

                minPos -= scrollOffset;
                maxPos -= scrollOffset;

                if (Mathf.Approximately(minPos.y, maxPos.y))
                {
                    painterParams.rect = new Rect(minPos.x, minPos.y, maxPos.x - minPos.x, lineHeight);
                    painter.DrawRect(painterParams);
                }
                else
                {
                    // Draw first line
                    painterParams.rect = new Rect(minPos.x, minPos.y, contentRect.xMax - minPos.x, lineHeight);
                    painter.DrawRect(painterParams);

                    var inbetweenHeight = (maxPos.y - minPos.y) - lineHeight;
                    if (inbetweenHeight > 0f)
                    {
                        // Draw all lines in-between
                        painterParams.rect = new Rect(contentRect.x, minPos.y + lineHeight, contentWidth, inbetweenHeight);
                        painter.DrawRect(painterParams);
                    }

                    // Draw last line if not empty
                    if (maxPos.x != contentRect.x)
                    {
                        painterParams.rect = new Rect(contentRect.x, maxPos.y, maxPos.x, lineHeight);
                        painter.DrawRect(painterParams);
                    }
                }
            }

            // Draw the border as in VisualElement
            painter.DrawBorder(this);

            // Draw the text with the scroll offset
            if (!string.IsNullOrEmpty(editorEngine.text) && contentRect.width > 0.0f && contentRect.height > 0.0f)
            {
                textParams      = painter.GetDefaultTextParameters(this);
                textParams.rect = new Rect(contentRect.x - scrollOffset.x, contentRect.y - scrollOffset.y, contentRect.width, contentRect.height);
                textParams.text = editorEngine.text;
                painter.DrawText(textParams);
            }

            // Draw the cursor
            if (cursorIndex == selectionEndIndex && (Font)style.font != null)
            {
                cursorParams               = painter.GetDefaultCursorPositionParameters(this);
                cursorParams.text          = editorEngine.text;
                cursorParams.wordWrapWidth = contentWidth;
                cursorParams.cursorIndex   = cursorIndex;

                Vector2 cursorPosition = painter.GetCursorPosition(cursorParams);
                cursorPosition -= scrollOffset;
                var painterParams = new RectStylePainterParameters
                {
                    rect  = new Rect(cursorPosition.x, cursorPosition.y, 1f, lineHeight),
                    color = drawCursorColor
                };
                painter.DrawRect(painterParams);
            }

            // Draw alternate cursor, if any
            if (editorEngine.altCursorPosition != -1)
            {
                cursorParams               = painter.GetDefaultCursorPositionParameters(this);
                cursorParams.text          = editorEngine.text.Substring(0, editorEngine.altCursorPosition);
                cursorParams.wordWrapWidth = contentWidth;
                cursorParams.cursorIndex   = editorEngine.altCursorPosition;

                Vector2 altCursorPosition = painter.GetCursorPosition(cursorParams);
                altCursorPosition -= scrollOffset;

                var painterParams = new RectStylePainterParameters
                {
                    rect  = new Rect(altCursorPosition.x, altCursorPosition.y, 1f, lineHeight),
                    color = drawCursorColor
                };
                painter.DrawRect(painterParams);
            }

            keyboardTextEditor.PostDrawCursor();
        }