示例#1
0
        public override void HandleEvent(EventBase evt)
        {
            if (evt.eventTypeId == AttachToPanelEvent.TypeId() && evt is AttachToPanelEvent attachEvent)
            {
                textHandle = attachEvent.destinationPanel.contextType == ContextType.Editor
                    ? TextHandleFactory.GetEditorHandle()
                    : TextHandleFactory.GetRuntimeHandle();
#if UNITY_EDITOR
                (attachEvent.destinationPanel as BaseVisualElementPanel)?.OnTextElementAdded(this);
#endif
            }
            else if (evt.eventTypeId == DetachFromPanelEvent.TypeId() && evt is DetachFromPanelEvent detachEvent)
            {
#if UNITY_EDITOR
                (detachEvent.originPanel as BaseVisualElementPanel)?.OnTextElementRemoved(this);
#endif
            }
            base.HandleEvent(evt);
        }
示例#2
0
        internal static Vector2 MeasureVisualElementTextSize(VisualElement ve, string textToMeasure, float width, VisualElement.MeasureMode widthMode, float height, VisualElement.MeasureMode heightMode, ITextHandle textHandle)
        {
            float measuredWidth  = float.NaN;
            float measuredHeight = float.NaN;

            if (textToMeasure == null || (ve.computedStyle.unityFont == null && ve.computedStyle.unityFontDefinition.IsEmpty()))
            {
                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 == VisualElement.MeasureMode.Exactly)
            {
                measuredWidth = width;
            }
            else
            {
                var textParams = MeshGenerationContextUtils.TextParams.MakeStyleBased(ve, textToMeasure);
                textParams.wordWrap = 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 == VisualElement.MeasureMode.AtMost)
                {
                    measuredWidth = Mathf.Min(measuredWidth, width);
                }
            }

            if (heightMode == VisualElement.MeasureMode.Exactly)
            {
                measuredHeight = height;
            }
            else
            {
                var textParams = MeshGenerationContextUtils.TextParams.MakeStyleBased(ve, textToMeasure);
                textParams.wordWrapWidth = measuredWidth;

                measuredHeight = textHandle.ComputeTextHeight(textParams, pixelsPerPoint);
                measuredHeight = measuredHeight < pointOffset ? 0 : AlignmentUtils.CeilToPixelGrid(measuredHeight, pixelsPerPoint, pixelOffset);

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

            return(new Vector2(measuredWidth, measuredHeight));
        }
 private void OnAttachToPanel(AttachToPanelEvent e)
 {
     m_TextHandle = e.destinationPanel.contextType == ContextType.Editor
         ? TextHandleFactory.GetEditorHandle()
         : TextHandleFactory.GetRuntimeHandle();
 }
 public static void Text(this MeshGenerationContext mgc, TextParams textParams, ITextHandle handle, float pixelsPerPoint)
 {
     if (textParams.font != null || !textParams.fontDefinition.IsEmpty())
     {
         mgc.painter.DrawText(textParams, handle, pixelsPerPoint);
     }
 }