public float ComputeTextHeight(string textToMeasure, float width, float height) { ConvertUssToTextGenerationSettings(s_LayoutSettings); s_LayoutSettings.text = textToMeasure; s_LayoutSettings.screenRect = new Rect(0, 0, width, height); return(textHandle.ComputeTextHeight(s_LayoutSettings)); }
internal static Vector2 MeasureVisualElementTextSize(VisualElement ve, string textToMeasure, float width, MeasureMode widthMode, float height, MeasureMode heightMode, TextHandle textHandle) { 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 scaling = (elementScaling.x + elementScaling.y) * 0.5f * ve.scaledPixelsPerPoint; if (scaling <= 0) { return(Vector2.zero); } if (widthMode == MeasureMode.Exactly) { measuredWidth = width; } else { var textParams = GetTextSettings(ve, textToMeasure); 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(textHandle.ComputeTextWidth(textParams, scaling)); if (widthMode == MeasureMode.AtMost) { measuredWidth = Mathf.Min(measuredWidth, width); } } if (heightMode == MeasureMode.Exactly) { measuredHeight = height; } else { var textParams = GetTextSettings(ve, textToMeasure); textParams.wordWrapWidth = measuredWidth; measuredHeight = Mathf.Ceil(textHandle.ComputeTextHeight(textParams, scaling)); if (heightMode == MeasureMode.AtMost) { measuredHeight = Mathf.Min(measuredHeight, height); } } return(new Vector2(measuredWidth, measuredHeight)); }
internal void DrawWithTextSelectionAndCursor(MeshGenerationContext mgc, string newText, float pixelsPerPoint) { var playmodeTintColor = panel.contextType == ContextType.Editor ? UIElementsUtility.editorPlayModeTintColor : Color.white; 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 = TextHandle.ComputeTextScaling(worldTransform, pixelsPerPoint); var textParams = MeshGenerationContextUtils.TextParams.MakeStyleBased(this, text); textParams.text = " "; textParams.wordWrapWidth = 0.0f; textParams.wordWrap = false; float lineHeight = m_TextHandle.ComputeTextHeight(textParams, textScaling); 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; } Vector2 pos = editorEngine.graphicalCursorPos - scrollOffset; pos.y += lineHeight; GUIUtility.compositionCursorPos = this.LocalToWorld(pos); 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) { 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; Vector2 minPos = m_TextHandle.GetCursorPosition(cursorParams, textScaling); cursorParams.cursorIndex = max; Vector2 maxPos = m_TextHandle.GetCursorPosition(cursorParams, textScaling); minPos -= scrollOffset; maxPos -= scrollOffset; if (Mathf.Approximately(minPos.y, maxPos.y)) { mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams() { rect = new Rect(minPos.x, minPos.y, maxPos.x - minPos.x, lineHeight), color = selectionColor, playmodeTintColor = playmodeTintColor }); } else { // Draw first line mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams() { rect = new Rect(minPos.x, minPos.y, contentRect.xMax - minPos.x, lineHeight), color = selectionColor, playmodeTintColor = playmodeTintColor }); var inbetweenHeight = (maxPos.y - minPos.y) - lineHeight; if (inbetweenHeight > 0f) { // Draw all lines in-between mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams() { rect = new Rect(contentRect.xMin, minPos.y + lineHeight, contentRect.width, inbetweenHeight), color = selectionColor, playmodeTintColor = playmodeTintColor }); } // Draw last line if not empty if (maxPos.x != contentRect.x) { mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams() { rect = new Rect(contentRect.xMin, maxPos.y, maxPos.x, lineHeight), color = selectionColor, playmodeTintColor = playmodeTintColor }); } } } // Draw the text with the scroll offset if (!string.IsNullOrEmpty(editorEngine.text) && contentRect.width > 0.0f && contentRect.height > 0.0f) { textParams = MeshGenerationContextUtils.TextParams.MakeStyleBased(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; mgc.Text(textParams, m_TextHandle, scaledPixelsPerPoint); } // 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; Vector2 cursorPosition = m_TextHandle.GetCursorPosition(cursorParams, textScaling); cursorPosition -= scrollOffset; mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams { rect = new Rect(cursorPosition.x, cursorPosition.y, 1f, lineHeight), color = drawCursorColor, playmodeTintColor = playmodeTintColor }); } // 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; Vector2 altCursorPosition = m_TextHandle.GetCursorPosition(cursorParams, textScaling); altCursorPosition -= scrollOffset; mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams { rect = new Rect(altCursorPosition.x, altCursorPosition.y, 1f, lineHeight), color = drawCursorColor, playmodeTintColor = playmodeTintColor }); } } keyboardTextEditor.PostDrawCursor(); }
internal static Vector2 MeasureVisualElementTextSize(VisualElement ve, string textToMeasure, float width, VisualElement.MeasureMode widthMode, float height, VisualElement.MeasureMode heightMode, TextHandle textHandle) { float num = float.NaN; float num2 = float.NaN; Font value = ve.computedStyle.unityFont.value; bool flag = textToMeasure == null || value == null; Vector2 result; if (flag) { result = new Vector2(num, num2); } else { Vector3 vector = ve.ComputeGlobalScale(); bool flag2 = vector.x + vector.y <= 0f || ve.scaledPixelsPerPoint <= 0f; if (flag2) { result = Vector2.zero; } else { float scaledPixelsPerPoint = ve.scaledPixelsPerPoint; float num3 = 0.02f; float num4 = num3 / scaledPixelsPerPoint; bool flag3 = widthMode == VisualElement.MeasureMode.Exactly; if (flag3) { num = width; } else { MeshGenerationContextUtils.TextParams textSettings = TextElement.GetTextSettings(ve, textToMeasure); textSettings.wordWrap = false; textSettings.richText = false; num = textHandle.ComputeTextWidth(textSettings, scaledPixelsPerPoint); num = ((num < num4) ? 0f : AlignmentUtils.CeilToPixelGrid(num, scaledPixelsPerPoint, num3)); bool flag4 = widthMode == VisualElement.MeasureMode.AtMost; if (flag4) { num = Mathf.Min(num, width); } } bool flag5 = heightMode == VisualElement.MeasureMode.Exactly; if (flag5) { num2 = height; } else { MeshGenerationContextUtils.TextParams textSettings2 = TextElement.GetTextSettings(ve, textToMeasure); textSettings2.wordWrapWidth = num; textSettings2.richText = false; num2 = textHandle.ComputeTextHeight(textSettings2, scaledPixelsPerPoint); num2 = ((num2 < num4) ? 0f : AlignmentUtils.CeilToPixelGrid(num2, scaledPixelsPerPoint, num3)); bool flag6 = heightMode == VisualElement.MeasureMode.AtMost; if (flag6) { num2 = Mathf.Min(num2, height); } } result = new Vector2(num, num2); } } return(result); }
internal static Vector2 MeasureVisualElementTextSize(VisualElement ve, string textToMeasure, float width, MeasureMode widthMode, float height, MeasureMode heightMode, TextHandle textHandle) { 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(); if (elementScaling.x + elementScaling.y <= 0 || ve.scaledPixelsPerPoint <= 0) { return(Vector2.zero); } float pixelsPerPoint = ve.scaledPixelsPerPoint; float pixelOffset = 0.02f; float pointOffset = pixelOffset / pixelsPerPoint; if (widthMode == MeasureMode.Exactly) { measuredWidth = width; } else { var textParams = GetTextSettings(ve, textToMeasure); textParams.wordWrap = false; textParams.richText = false; // Case 1215962: round up as yoga could decide to round down and text would start wrapping measuredWidth = textHandle.ComputeTextWidth(textParams, pixelsPerPoint); measuredWidth = measuredWidth < pointOffset ? 0 : AlignmentUtils.CeilToPixelGrid(measuredWidth, pixelsPerPoint, pixelOffset); if (widthMode == MeasureMode.AtMost) { measuredWidth = Mathf.Min(measuredWidth, width); } } if (heightMode == MeasureMode.Exactly) { measuredHeight = height; } else { var textParams = GetTextSettings(ve, textToMeasure); textParams.wordWrapWidth = measuredWidth; textParams.richText = false; measuredHeight = textHandle.ComputeTextHeight(textParams, pixelsPerPoint); measuredHeight = measuredHeight < pointOffset ? 0 : AlignmentUtils.CeilToPixelGrid(measuredHeight, pixelsPerPoint, pixelOffset); if (heightMode == MeasureMode.AtMost) { measuredHeight = Mathf.Min(measuredHeight, height); } } return(new Vector2(measuredWidth, measuredHeight)); }