Beispiel #1
0
            internal void DrawWithTextSelectionAndCursor(IStylePainterInternal 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;
                var  scrollOffset  = editorEngine.scrollOffset;

                float textScaling = TextNative.ComputeTextScaling(worldTransform, GUIUtility.pixelsPerPoint);

                var textParams = TextStylePainterParameters.GetDefault(this, text);

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

                var   textNativeSettings = textParams.GetTextNativeSettings(textScaling);
                float lineHeight         = TextNative.ComputeTextHeight(textNativeSettings);

                float wordWrapWidth = 0.0f;

                // Make sure to take into account the word wrap style...
                if (editorEngine.multiline && (resolvedStyle.whiteSpace == WhiteSpace.Normal))
                {
                    wordWrapWidth = contentRect.width;
                    // Since the wrapping is enabled, there is no need to offset the text... It will always fit the space on screen !
                    scrollOffset = Vector2.zero;
                }

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

                Color drawCursorColor = cursorColor;

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

                CursorPositionStylePainterParameters cursorParams;

                // Draw highlighted section, if any
                if ((cursorIndex != selectionEndIndex) && !isDragging)
                {
                    var painterParams = RectStylePainterParameters.GetDefault(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               = CursorPositionStylePainterParameters.GetDefault(this, text);
                    cursorParams.text          = editorEngine.text;
                    cursorParams.wordWrapWidth = wordWrapWidth;
                    cursorParams.cursorIndex   = min;

                    textNativeSettings = cursorParams.GetTextNativeSettings(textScaling);
                    Vector2 minPos = TextNative.GetCursorPosition(textNativeSettings, cursorParams.rect, min);
                    Vector2 maxPos = TextNative.GetCursorPosition(textNativeSettings, cursorParams.rect, max);

                    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.xMin, minPos.y + lineHeight, contentRect.width, inbetweenHeight);
                            painter.DrawRect(painterParams);
                        }

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

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

                    painter.DrawText(textParams);
                }

                // Draw the cursor
                if (!isReadOnly && !isDragging)
                {
                    if (cursorIndex == selectionEndIndex && computedStyle.unityFont.value != null)
                    {
                        cursorParams               = CursorPositionStylePainterParameters.GetDefault(this, text);
                        cursorParams.text          = editorEngine.text;
                        cursorParams.wordWrapWidth = wordWrapWidth;
                        cursorParams.cursorIndex   = cursorIndex;

                        textNativeSettings = cursorParams.GetTextNativeSettings(textScaling);
                        Vector2 cursorPosition = TextNative.GetCursorPosition(textNativeSettings, cursorParams.rect, cursorParams.cursorIndex);
                        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               = CursorPositionStylePainterParameters.GetDefault(this, text);
                        cursorParams.text          = editorEngine.text.Substring(0, editorEngine.altCursorPosition);
                        cursorParams.wordWrapWidth = wordWrapWidth;
                        cursorParams.cursorIndex   = editorEngine.altCursorPosition;

                        textNativeSettings = cursorParams.GetTextNativeSettings(textScaling);
                        Vector2 altCursorPosition = TextNative.GetCursorPosition(textNativeSettings, cursorParams.rect, cursorParams.cursorIndex);
                        altCursorPosition -= scrollOffset;

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

                keyboardTextEditor.PostDrawCursor();
            }
        bool DrawUsingUIVertices(IStylePainterInternal painter)
        {
            if (edgeWidth <= 0)
            {
                return(true);
            }

            UpdateRenderPoints();
            if (m_RenderPoints.Count == 0)
            {
                return(true); // Don't draw anything
            }
            Color inColor  = this.inputColor;
            Color outColor = this.outputColor;

            inColor  *= UIElementsUtility.editorPlayModeTintColor;
            outColor *= UIElementsUtility.editorPlayModeTintColor;

            uint cpt          = (uint)m_RenderPoints.Count;
            uint wantedLength = (cpt) * 2;
            uint indexCount   = (wantedLength - 2) * 3;
            NativeSlice <Vertex> vertices;
            NativeSlice <UInt16> indices;
            var meshParams = MeshStylePainterParameters.GetDefault(null, wantedLength, indexCount);

            meshParams.uvIsDisplacement = true; // We store displacement data in the UV channel
            painter.DrawMesh(MeshStylePainterParameters.GetDefault(null, wantedLength, indexCount), out vertices, out indices);
            if (vertices.Length == 0)
            {
                return(false);
            }

            float polyLineLength = 0;

            for (int i = 1; i < cpt; ++i)
            {
                polyLineLength += (m_RenderPoints[i - 1] - m_RenderPoints[i]).sqrMagnitude;
            }

            float halfWidth     = edgeWidth * 0.5f;
            float currentLength = 0;
            float flags         = (float)VertexFlags.LastType;

            Vector2 unitPreviousSegment = Vector2.zero;

            for (int i = 0; i < cpt; ++i)
            {
                Vector2 dir;
                Vector2 unitNextSegment = Vector2.zero;
                Vector2 nextSegment     = Vector2.zero;

                if (i < cpt - 1)
                {
                    nextSegment     = (m_RenderPoints[i + 1] - m_RenderPoints[i]);
                    unitNextSegment = nextSegment.normalized;
                }


                if (i > 0 && i < cpt - 1)
                {
                    dir = unitPreviousSegment + unitNextSegment;
                    dir.Normalize();
                }
                else if (i > 0)
                {
                    dir = unitPreviousSegment;
                }
                else
                {
                    dir = unitNextSegment;
                }

                Vector2 pos   = m_RenderPoints[i];
                Vector2 uv    = new Vector2(dir.y * halfWidth, -dir.x * halfWidth); // Normal scaled by half width
                Color32 tint  = Color.LerpUnclamped(outColor, inColor, currentLength / polyLineLength);
                int     index = i * 2;

                vertices[index] = new Vertex()
                {
                    position = new Vector3(pos.x, pos.y, 1), uv = uv, tint = tint, flags = flags
                };
                vertices[index + 1] = new Vertex()
                {
                    position = new Vector3(pos.x, pos.y, -1), uv = uv, tint = tint, flags = flags
                };

                if (i < cpt - 2)
                {
                    currentLength += nextSegment.sqrMagnitude;
                }
                else
                {
                    currentLength = polyLineLength;
                }

                unitPreviousSegment = unitNextSegment;
            }

            // Fill triangle indices as it is a triangle strip
            for (uint i = 0; i < wantedLength - 2; ++i)
            {
                int index = (int)(i * 3);
                if ((i & 0x01) == 0)
                {
                    indices[index]     = (UInt16)i;
                    indices[index + 1] = (UInt16)(i + 1);
                    indices[index + 2] = (UInt16)(i + 2);
                }
                else
                {
                    indices[index]     = (UInt16)(i + 1);
                    indices[index + 1] = (UInt16)i;
                    indices[index + 2] = (UInt16)(i + 2);
                }
            }
            return(true);
        }
Beispiel #3
0
 internal void DrawWithTextSelectionAndCursor(IStylePainterInternal painter, string newText)
 {
     m_TextInputBase.DrawWithTextSelectionAndCursor(painter, newText);
 }
Beispiel #4
0
        internal void DrawWithTextSelectionAndCursor(IStylePainterInternal 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;

            float textScaling = TextNative.ComputeTextScaling(worldTransform);

            var textParams = TextStylePainterParameters.GetDefault(this, text);

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

            var   textNativeSettings = textParams.GetTextNativeSettings(textScaling);
            float lineHeight         = TextNative.ComputeTextHeight(textNativeSettings);
            float wordWrapWidth      = editorEngine.multiline
                ? contentRect.width
                : 0.0f;

            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;

            CursorPositionStylePainterParameters cursorParams;

            // Draw highlighted section, if any
            if (cursorIndex != selectionEndIndex)
            {
                var painterParams = RectStylePainterParameters.GetDefault(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               = CursorPositionStylePainterParameters.GetDefault(this, text);
                cursorParams.text          = editorEngine.text;
                cursorParams.wordWrapWidth = wordWrapWidth;
                cursorParams.cursorIndex   = min;

                textNativeSettings = cursorParams.GetTextNativeSettings(textScaling);
                Vector2 minPos = TextNative.GetCursorPosition(textNativeSettings, cursorParams.rect, min);
                Vector2 maxPos = TextNative.GetCursorPosition(textNativeSettings, cursorParams.rect, max);

                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, wordWrapWidth, 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 text with the scroll offset
            if (!string.IsNullOrEmpty(editorEngine.text) && contentRect.width > 0.0f && contentRect.height > 0.0f)
            {
                textParams      = TextStylePainterParameters.GetDefault(this, text);
                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               = CursorPositionStylePainterParameters.GetDefault(this, text);
                cursorParams.text          = editorEngine.text;
                cursorParams.wordWrapWidth = wordWrapWidth;
                cursorParams.cursorIndex   = cursorIndex;

                textNativeSettings = cursorParams.GetTextNativeSettings(textScaling);
                Vector2 cursorPosition = TextNative.GetCursorPosition(textNativeSettings, cursorParams.rect, cursorParams.cursorIndex);
                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               = CursorPositionStylePainterParameters.GetDefault(this, text);
                cursorParams.text          = editorEngine.text.Substring(0, editorEngine.altCursorPosition);
                cursorParams.wordWrapWidth = wordWrapWidth;
                cursorParams.cursorIndex   = editorEngine.altCursorPosition;

                textNativeSettings = cursorParams.GetTextNativeSettings(textScaling);
                Vector2 altCursorPosition = TextNative.GetCursorPosition(textNativeSettings, cursorParams.rect, cursorParams.cursorIndex);
                altCursorPosition -= scrollOffset;

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

            keyboardTextEditor.PostDrawCursor();
        }