internal override void DoRepaint(IStylePainter painter) { if (this.isPasswordField) { string text = "".PadRight(this.text.Length, base.maskChar); if (!base.hasFocus) { painter.DrawBackground(this); painter.DrawBorder(this); if (!string.IsNullOrEmpty(text) && base.contentRect.width > 0f && base.contentRect.height > 0f) { TextStylePainterParameters defaultTextParameters = painter.GetDefaultTextParameters(this); defaultTextParameters.text = text; painter.DrawText(defaultTextParameters); } } else { base.DrawWithTextSelectionAndCursor(painter, text); } } else { base.DoRepaint(painter); } }
protected 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, 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); } }
internal static Vector2 MeasureVisualElementTextSize(VisualElement ve, string textToMeasure, float width, MeasureMode widthMode, float height, MeasureMode heightMode) { float measuredWidth = float.NaN; float measuredHeight = float.NaN; // TODO: This scaling parameter should depend on the real scaling of the text (dpi scaling * world scaling) // because depending of its value, the glyphs may align on different pixels which can change the // measure. The resulting measure should then be divided by this scaling to obtain the local measure. float scaling = 1; Font usedFont = ve.style.font; if (textToMeasure == null || usedFont == null) { return(new Vector2(measuredWidth, measuredHeight)); } 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; measuredWidth = 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 = TextNative.ComputeTextHeight(textParams.GetTextNativeSettings(scaling)); if (heightMode == MeasureMode.AtMost) { measuredHeight = Mathf.Min(measuredHeight, height); } } return(new Vector2(measuredWidth, measuredHeight)); }
public void DrawText(string text) { if (!string.IsNullOrEmpty(text) && currentElement.contentRect.width > 0.0f && currentElement.contentRect.height > 0.0f) { DrawText(TextStylePainterParameters.GetDefault(currentElement, text)); } }
protected internal override Vector2 DoMeasure(float width, VisualElement.MeasureMode widthMode, float height, VisualElement.MeasureMode heightMode) { float num = float.NaN; float num2 = float.NaN; Font font = base.style.font; Vector2 result; if (this.text == null || font == null) { result = new Vector2(num, num2); } else { IStylePainter stylePainter = base.elementPanel.stylePainter; if (widthMode == VisualElement.MeasureMode.Exactly) { num = width; } else { TextStylePainterParameters defaultTextParameters = stylePainter.GetDefaultTextParameters(this); defaultTextParameters.text = this.text; defaultTextParameters.font = font; defaultTextParameters.wordWrapWidth = 0f; defaultTextParameters.wordWrap = false; defaultTextParameters.richText = true; num = stylePainter.ComputeTextWidth(defaultTextParameters); if (widthMode == VisualElement.MeasureMode.AtMost) { num = Mathf.Min(num, width); } } if (heightMode == VisualElement.MeasureMode.Exactly) { num2 = height; } else { TextStylePainterParameters defaultTextParameters2 = stylePainter.GetDefaultTextParameters(this); defaultTextParameters2.text = this.text; defaultTextParameters2.font = font; defaultTextParameters2.wordWrapWidth = num; defaultTextParameters2.richText = true; num2 = stylePainter.ComputeTextHeight(defaultTextParameters2); if (heightMode == VisualElement.MeasureMode.AtMost) { num2 = Mathf.Min(num2, height); } } result = new Vector2(num, num2); } return(result); }
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); }
public static TextStylePainterParameters GetDefault(VisualElement ve, string text) { IStyle style = ve.style; var painterParams = new TextStylePainterParameters { rect = ve.contentRect, text = text, font = style.font, fontSize = style.fontSize, fontStyle = style.fontStyleAndWeight, fontColor = style.color.GetSpecifiedValueOrDefault(Color.black), anchor = style.unityTextAlign, wordWrap = style.wordWrap, wordWrapWidth = style.wordWrap ? ve.contentRect.width : 0.0f, richText = false, clipping = style.textClipping }; return(painterParams); }
internal static TextStylePainterParameters GetDefaultTextParameters(this IStylePainter painter, BaseTextElement te) { IStyle style = te.style; var painterParams = new TextStylePainterParameters { rect = te.contentRect, text = te.text, font = style.font, fontSize = style.fontSize, fontStyle = style.fontStyle, fontColor = style.textColor.GetSpecifiedValueOrDefault(Color.black), anchor = style.textAlignment, wordWrap = style.wordWrap, wordWrapWidth = style.wordWrap ? te.contentRect.width : 0.0f, richText = false, clipping = style.textClipping }; return(painterParams); }
internal void DrawWithTextSelectionAndCursor(IStylePainter painter, string newText) { KeyboardTextEditorEventHandler keyboardTextEditorEventHandler = this.editorEventHandler as KeyboardTextEditorEventHandler; if (keyboardTextEditorEventHandler != null) { keyboardTextEditorEventHandler.PreDrawCursor(newText); int cursorIndex = this.editorEngine.cursorIndex; int selectIndex = this.editorEngine.selectIndex; Rect localPosition = this.editorEngine.localPosition; Vector2 scrollOffset = this.editorEngine.scrollOffset; IStyle style = base.style; TextStylePainterParameters defaultTextParameters = painter.GetDefaultTextParameters(this); defaultTextParameters.text = " "; defaultTextParameters.wordWrapWidth = 0f; defaultTextParameters.wordWrap = false; float num = painter.ComputeTextHeight(defaultTextParameters); float num2 = (!style.wordWrap) ? 0f : base.contentRect.width; Input.compositionCursorPos = this.editorEngine.graphicalCursorPos - scrollOffset + new Vector2(localPosition.x, localPosition.y + num); Color specifiedValueOrDefault = this.m_CursorColor.GetSpecifiedValueOrDefault(Color.grey); int num3 = (!string.IsNullOrEmpty(Input.compositionString)) ? (cursorIndex + Input.compositionString.Length) : selectIndex; painter.DrawBackground(this); if (cursorIndex != num3) { RectStylePainterParameters defaultRectParameters = painter.GetDefaultRectParameters(this); defaultRectParameters.color = this.selectionColor; defaultRectParameters.border.SetWidth(0f); defaultRectParameters.border.SetRadius(0f); int cursorIndex2 = (cursorIndex >= num3) ? num3 : cursorIndex; int cursorIndex3 = (cursorIndex <= num3) ? num3 : cursorIndex; CursorPositionStylePainterParameters defaultCursorPositionParameters = painter.GetDefaultCursorPositionParameters(this); defaultCursorPositionParameters.text = this.editorEngine.text; defaultCursorPositionParameters.wordWrapWidth = num2; defaultCursorPositionParameters.cursorIndex = cursorIndex2; Vector2 a = painter.GetCursorPosition(defaultCursorPositionParameters); defaultCursorPositionParameters.cursorIndex = cursorIndex3; Vector2 a2 = painter.GetCursorPosition(defaultCursorPositionParameters); a -= scrollOffset; a2 -= scrollOffset; if (Mathf.Approximately(a.y, a2.y)) { defaultRectParameters.rect = new Rect(a.x, a.y, a2.x - a.x, num); painter.DrawRect(defaultRectParameters); } else { defaultRectParameters.rect = new Rect(a.x, a.y, num2 - a.x, num); painter.DrawRect(defaultRectParameters); float num4 = a2.y - a.y - num; if (num4 > 0f) { defaultRectParameters.rect = new Rect(0f, a.y + num, num2, num4); painter.DrawRect(defaultRectParameters); } defaultRectParameters.rect = new Rect(0f, a2.y, a2.x, num); painter.DrawRect(defaultRectParameters); } } painter.DrawBorder(this); if (!string.IsNullOrEmpty(this.editorEngine.text) && base.contentRect.width > 0f && base.contentRect.height > 0f) { defaultTextParameters = painter.GetDefaultTextParameters(this); defaultTextParameters.rect = new Rect(base.contentRect.x - scrollOffset.x, base.contentRect.y - scrollOffset.y, base.contentRect.width, base.contentRect.height); defaultTextParameters.text = this.editorEngine.text; painter.DrawText(defaultTextParameters); } if (cursorIndex == num3 && style.font != null) { CursorPositionStylePainterParameters defaultCursorPositionParameters = painter.GetDefaultCursorPositionParameters(this); defaultCursorPositionParameters.text = this.editorEngine.text; defaultCursorPositionParameters.wordWrapWidth = num2; defaultCursorPositionParameters.cursorIndex = cursorIndex; Vector2 a3 = painter.GetCursorPosition(defaultCursorPositionParameters); a3 -= scrollOffset; RectStylePainterParameters painterParams = new RectStylePainterParameters { rect = new Rect(a3.x, a3.y, 1f, num), color = specifiedValueOrDefault }; painter.DrawRect(painterParams); } if (this.editorEngine.altCursorPosition != -1) { CursorPositionStylePainterParameters defaultCursorPositionParameters = painter.GetDefaultCursorPositionParameters(this); defaultCursorPositionParameters.text = this.editorEngine.text.Substring(0, this.editorEngine.altCursorPosition); defaultCursorPositionParameters.wordWrapWidth = num2; defaultCursorPositionParameters.cursorIndex = this.editorEngine.altCursorPosition; Vector2 a4 = painter.GetCursorPosition(defaultCursorPositionParameters); a4 -= scrollOffset; RectStylePainterParameters painterParams2 = new RectStylePainterParameters { rect = new Rect(a4.x, a4.y, 1f, num), color = specifiedValueOrDefault }; painter.DrawRect(painterParams2); } keyboardTextEditorEventHandler.PostDrawCursor(); } }
private void DrawWithTextSelectionAndCursor(IStylePainter painter, string newText) { KeyboardTextEditor keyboardTextEditor = this.editor as KeyboardTextEditor; if (keyboardTextEditor != null) { keyboardTextEditor.PreDrawCursor(newText); int cursorIndex = keyboardTextEditor.cursorIndex; int selectIndex = keyboardTextEditor.selectIndex; Rect localPosition = keyboardTextEditor.localPosition; Vector2 scrollOffset = keyboardTextEditor.scrollOffset; IStyle style = base.style; TextStylePainterParameters defaultTextParameters = painter.GetDefaultTextParameters(this); defaultTextParameters.text = " "; defaultTextParameters.wordWrapWidth = 0f; defaultTextParameters.wordWrap = false; float num = painter.ComputeTextHeight(defaultTextParameters); float num2 = (!style.wordWrap) ? 0f : base.contentRect.width; Input.compositionCursorPos = keyboardTextEditor.graphicalCursorPos - scrollOffset + new Vector2(localPosition.x, localPosition.y + num); Color color = (!(this.cursorColor != Color.clear)) ? GUI.skin.settings.cursorColor : this.cursorColor; int num3 = (!string.IsNullOrEmpty(Input.compositionString)) ? (cursorIndex + Input.compositionString.Length) : selectIndex; painter.DrawBackground(this); if (cursorIndex != num3) { RectStylePainterParameters defaultRectParameters = painter.GetDefaultRectParameters(this); defaultRectParameters.color = this.selectionColor; defaultRectParameters.borderLeftWidth = 0f; defaultRectParameters.borderTopWidth = 0f; defaultRectParameters.borderRightWidth = 0f; defaultRectParameters.borderBottomWidth = 0f; defaultRectParameters.borderTopLeftRadius = 0f; defaultRectParameters.borderTopRightRadius = 0f; defaultRectParameters.borderBottomRightRadius = 0f; defaultRectParameters.borderBottomLeftRadius = 0f; int cursorIndex2 = (cursorIndex >= num3) ? num3 : cursorIndex; int cursorIndex3 = (cursorIndex <= num3) ? num3 : cursorIndex; CursorPositionStylePainterParameters defaultCursorPositionParameters = painter.GetDefaultCursorPositionParameters(this); defaultCursorPositionParameters.text = keyboardTextEditor.text; defaultCursorPositionParameters.wordWrapWidth = num2; defaultCursorPositionParameters.cursorIndex = cursorIndex2; Vector2 a = painter.GetCursorPosition(defaultCursorPositionParameters); defaultCursorPositionParameters.cursorIndex = cursorIndex3; Vector2 a2 = painter.GetCursorPosition(defaultCursorPositionParameters); a -= scrollOffset; a2 -= scrollOffset; if (Mathf.Approximately(a.y, a2.y)) { defaultRectParameters.layout = new Rect(a.x, a.y, a2.x - a.x, num); painter.DrawRect(defaultRectParameters); } else { defaultRectParameters.layout = new Rect(a.x, a.y, num2 - a.x, num); painter.DrawRect(defaultRectParameters); float num4 = a2.y - a.y - num; if (num4 > 0f) { defaultRectParameters.layout = new Rect(0f, a.y + num, num2, num4); painter.DrawRect(defaultRectParameters); } defaultRectParameters.layout = new Rect(0f, a2.y, a2.x, num); painter.DrawRect(defaultRectParameters); } } painter.DrawBorder(this); if (!string.IsNullOrEmpty(keyboardTextEditor.text) && base.contentRect.width > 0f && base.contentRect.height > 0f) { defaultTextParameters = painter.GetDefaultTextParameters(this); defaultTextParameters.layout = new Rect(base.contentRect.x - scrollOffset.x, base.contentRect.y - scrollOffset.y, base.contentRect.width, base.contentRect.height); defaultTextParameters.text = keyboardTextEditor.text; painter.DrawText(defaultTextParameters); } if (cursorIndex == num3 && style.font != null) { CursorPositionStylePainterParameters defaultCursorPositionParameters = painter.GetDefaultCursorPositionParameters(this); defaultCursorPositionParameters.text = keyboardTextEditor.text; defaultCursorPositionParameters.wordWrapWidth = num2; defaultCursorPositionParameters.cursorIndex = cursorIndex; Vector2 a3 = painter.GetCursorPosition(defaultCursorPositionParameters); a3 -= scrollOffset; RectStylePainterParameters painterParams = new RectStylePainterParameters { layout = new Rect(a3.x, a3.y, 1f, num), color = color }; painter.DrawRect(painterParams); } if (keyboardTextEditor.altCursorPosition != -1) { CursorPositionStylePainterParameters defaultCursorPositionParameters = painter.GetDefaultCursorPositionParameters(this); defaultCursorPositionParameters.text = keyboardTextEditor.text.Substring(0, keyboardTextEditor.altCursorPosition); defaultCursorPositionParameters.wordWrapWidth = num2; defaultCursorPositionParameters.cursorIndex = keyboardTextEditor.altCursorPosition; Vector2 a4 = painter.GetCursorPosition(defaultCursorPositionParameters); a4 -= scrollOffset; RectStylePainterParameters painterParams2 = new RectStylePainterParameters { layout = new Rect(a4.x, a4.y, 1f, num), color = color }; painter.DrawRect(painterParams2); } keyboardTextEditor.PostDrawCursor(); } }
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(); }