示例#1
0
            internal override void DoRepaint(IStylePainter painter)
            {
                var stylePainter = (IStylePainterInternal)painter;

                if (isPasswordField)
                {
                    // if we use system keyboard we will have normal text returned (hiding symbols is done inside os)
                    // so before drawing make sure we hide them ourselves
                    string drawText = "".PadRight(text.Length, parentTextField.maskChar);
                    if (!hasFocus)
                    {
                        // We don't have the focus, don't draw the selection and cursor
                        if (!string.IsNullOrEmpty(drawText) && contentRect.width > 0.0f && contentRect.height > 0.0f)
                        {
                            var textParams = TextStylePainterParameters.GetDefault(this, text);
                            textParams.text = drawText;
                            stylePainter.DrawText(textParams);
                        }
                    }
                    else
                    {
                        DrawWithTextSelectionAndCursor(stylePainter, drawText);
                    }
                }
                else
                {
                    base.DoRepaint(painter);
                }
            }
 public void DrawText(string text)
 {
     if (!string.IsNullOrEmpty(text) && currentElement.contentRect.width > 0.0f && currentElement.contentRect.height > 0.0f)
     {
         DrawText(TextStylePainterParameters.GetDefault(currentElement, text));
     }
 }
        public void DrawText(TextStylePainterParameters painterParams)
        {
            Rect         screenRect    = painterParams.rect;
            string       text          = painterParams.text;
            Font         font          = painterParams.font;
            int          fontSize      = painterParams.fontSize;
            FontStyle    fontStyle     = painterParams.fontStyle;
            Color        fontColor     = painterParams.fontColor * UIElementsUtility.editorPlayModeTintColor;
            TextAnchor   anchor        = painterParams.anchor;
            bool         wordWrap      = painterParams.wordWrap;
            float        wordWrapWidth = painterParams.wordWrapWidth;
            bool         richText      = painterParams.richText;
            TextClipping clipping      = painterParams.clipping;

            DrawText(screenRect, text, font, fontSize, fontStyle, fontColor * m_OpacityColor, anchor, wordWrap, wordWrapWidth, richText, clipping);
        }
示例#4
0
        public static TextStylePainterParameters GetDefault(VisualElement ve, string text)
        {
            ComputedStyle style         = ve.computedStyle;
            var           painterParams = new TextStylePainterParameters
            {
                rect          = ve.contentRect,
                text          = text,
                font          = style.unityFont.value,
                fontSize      = (int)style.fontSize.value.value,
                fontStyle     = style.unityFontStyleAndWeight.value,
                fontColor     = style.color.value,
                anchor        = style.unityTextAlign.value,
                wordWrap      = style.whiteSpace.value == WhiteSpace.Normal,
                wordWrapWidth = style.whiteSpace.value == WhiteSpace.Normal ? ve.contentRect.width : 0.0f,
                richText      = false,
                clipping      = (TextClipping)style.overflow.value
            };

            return(painterParams);
        }
示例#5
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();
            }
示例#6
0
        internal static Vector2 MeasureVisualElementTextSize(VisualElement ve, string textToMeasure, float width, MeasureMode widthMode, float height, MeasureMode heightMode)
        {
            float measuredWidth  = float.NaN;
            float measuredHeight = float.NaN;

            Font usedFont = ve.computedStyle.unityFont.value;

            if (textToMeasure == null || usedFont == null)
            {
                return(new Vector2(measuredWidth, measuredHeight));
            }

            var elementScaling = ve.ComputeGlobalScale();

            float pixelsPerPoint = (ve.elementPanel != null) ? ve.elementPanel.currentPixelsPerPoint : GUIUtility.pixelsPerPoint;
            float scaling        = (elementScaling.x + elementScaling.y) * 0.5f * pixelsPerPoint;

            if (widthMode == MeasureMode.Exactly)
            {
                measuredWidth = width;
            }
            else
            {
                var textParams = TextStylePainterParameters.GetDefault(ve, textToMeasure);
                textParams.text          = textToMeasure;
                textParams.font          = usedFont;
                textParams.wordWrapWidth = 0.0f;
                textParams.wordWrap      = false;
                textParams.richText      = true;

                //we make sure to round up as yoga could decide to round down and text would start wrapping
                measuredWidth = Mathf.Ceil(TextNative.ComputeTextWidth(textParams.GetTextNativeSettings(scaling)));

                if (widthMode == MeasureMode.AtMost)
                {
                    measuredWidth = Mathf.Min(measuredWidth, width);
                }
            }

            if (heightMode == MeasureMode.Exactly)
            {
                measuredHeight = height;
            }
            else
            {
                var textParams = TextStylePainterParameters.GetDefault(ve, textToMeasure);
                textParams.text          = textToMeasure;
                textParams.font          = usedFont;
                textParams.wordWrapWidth = measuredWidth;
                textParams.richText      = true;

                measuredHeight = Mathf.Ceil(TextNative.ComputeTextHeight(textParams.GetTextNativeSettings(scaling)));

                if (heightMode == MeasureMode.AtMost)
                {
                    measuredHeight = Mathf.Min(measuredHeight, height);
                }
            }

            return(new Vector2(measuredWidth, measuredHeight));
        }