FontDataDrawer.cs 23.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
using UnityEngine;
using UnityEngine.UI;

namespace UnityEditor.UI
{
    [CustomPropertyDrawer(typeof(FontData), true)]
    /// <summary>
    /// This is a PropertyDrawer for FontData. It is implemented using the standard Unity PropertyDrawer framework
    /// </summary>
    public class FontDataDrawer : PropertyDrawer
    {
        static private class Styles
        {
            public static GUIStyle alignmentButtonLeft = new GUIStyle(EditorStyles.miniButtonLeft);
            public static GUIStyle alignmentButtonMid = new GUIStyle(EditorStyles.miniButtonMid);
            public static GUIStyle alignmentButtonRight = new GUIStyle(EditorStyles.miniButtonRight);

            public static GUIContent m_EncodingContent;

            public static GUIContent m_LeftAlignText;
            public static GUIContent m_CenterAlignText;
            public static GUIContent m_RightAlignText;
            public static GUIContent m_TopAlignText;
            public static GUIContent m_MiddleAlignText;
            public static GUIContent m_BottomAlignText;

            public static GUIContent m_LeftAlignTextActive;
            public static GUIContent m_CenterAlignTextActive;
            public static GUIContent m_RightAlignTextActive;
            public static GUIContent m_TopAlignTextActive;
            public static GUIContent m_MiddleAlignTextActive;
            public static GUIContent m_BottomAlignTextActive;

            static Styles()
            {
                m_EncodingContent = EditorGUIUtility.TrTextContent("Rich Text", "Use emoticons and colors");

                // Horizontal Alignment Icons
                m_LeftAlignText = EditorGUIUtility.IconContent(@"GUISystem/align_horizontally_left", "Left Align");
                m_CenterAlignText = EditorGUIUtility.IconContent(@"GUISystem/align_horizontally_center", "Center Align");
                m_RightAlignText = EditorGUIUtility.IconContent(@"GUISystem/align_horizontally_right", "Right Align");
                m_LeftAlignTextActive = EditorGUIUtility.IconContent(@"GUISystem/align_horizontally_left_active", "Left Align");
                m_CenterAlignTextActive = EditorGUIUtility.IconContent(@"GUISystem/align_horizontally_center_active", "Center Align");
                m_RightAlignTextActive = EditorGUIUtility.IconContent(@"GUISystem/align_horizontally_right_active", "Right Align");

                // Vertical Alignment Icons
                m_TopAlignText = EditorGUIUtility.IconContent(@"GUISystem/align_vertically_top", "Top Align");
                m_MiddleAlignText = EditorGUIUtility.IconContent(@"GUISystem/align_vertically_center", "Middle Align");
                m_BottomAlignText = EditorGUIUtility.IconContent(@"GUISystem/align_vertically_bottom", "Bottom Align");
                m_TopAlignTextActive = EditorGUIUtility.IconContent(@"GUISystem/align_vertically_top_active", "Top Align");
                m_MiddleAlignTextActive = EditorGUIUtility.IconContent(@"GUISystem/align_vertically_center_active", "Middle Align");
                m_BottomAlignTextActive = EditorGUIUtility.IconContent(@"GUISystem/align_vertically_bottom_active", "Bottom Align");

                FixAlignmentButtonStyles(alignmentButtonLeft, alignmentButtonMid, alignmentButtonRight);
            }

            static void FixAlignmentButtonStyles(params GUIStyle[] styles)
            {
                foreach (GUIStyle style in styles)
                {
                    style.padding.left = 2;
                    style.padding.right = 2;
                }
            }
        }

        private enum VerticalTextAligment
        {
            Top,
            Middle,
            Bottom
        }

        private enum HorizontalTextAligment
        {
            Left,
            Center,
            Right
        }

        private const int kAlignmentButtonWidth = 20;

        static int s_TextAlignmentHash = "DoTextAligmentControl".GetHashCode();

        private SerializedProperty m_SupportEncoding;
        private SerializedProperty m_Font;
        private SerializedProperty m_FontSize;
        private SerializedProperty m_LineSpacing;
        private SerializedProperty m_FontStyle;
        private SerializedProperty m_ResizeTextForBestFit;
        private SerializedProperty m_ResizeTextMinSize;
        private SerializedProperty m_ResizeTextMaxSize;
        private SerializedProperty m_HorizontalOverflow;
        private SerializedProperty m_VerticalOverflow;
        private SerializedProperty m_Alignment;
        private SerializedProperty m_AlignByGeometry;

        private float m_FontFieldfHeight = 0f;
        private float m_FontStyleHeight = 0f;
        private float m_FontSizeHeight = 0f;
        private float m_LineSpacingHeight = 0f;
        private float m_EncodingHeight = 0f;
        private float m_ResizeTextForBestFitHeight = 0f;
        private float m_ResizeTextMinSizeHeight = 0f;
        private float m_ResizeTextMaxSizeHeight = 0f;
        private float m_HorizontalOverflowHeight = 0f;
        private float m_VerticalOverflowHeight = 0f;
        private float m_AlignByGeometryHeight = 0f;

        protected void Init(SerializedProperty property)
        {
            m_SupportEncoding = property.FindPropertyRelative("m_RichText");
            m_Font = property.FindPropertyRelative("m_Font");
            m_FontSize = property.FindPropertyRelative("m_FontSize");
            m_LineSpacing = property.FindPropertyRelative("m_LineSpacing");
            m_FontStyle = property.FindPropertyRelative("m_FontStyle");
            m_ResizeTextForBestFit = property.FindPropertyRelative("m_BestFit");
            m_ResizeTextMinSize = property.FindPropertyRelative("m_MinSize");
            m_ResizeTextMaxSize = property.FindPropertyRelative("m_MaxSize");
            m_HorizontalOverflow = property.FindPropertyRelative("m_HorizontalOverflow");
            m_VerticalOverflow = property.FindPropertyRelative("m_VerticalOverflow");
            m_Alignment = property.FindPropertyRelative("m_Alignment");
            m_AlignByGeometry = property.FindPropertyRelative("m_AlignByGeometry");
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            Init(property);
            m_FontFieldfHeight = EditorGUI.GetPropertyHeight(m_Font);
            m_FontStyleHeight   = EditorGUI.GetPropertyHeight(m_FontStyle);
            m_FontSizeHeight  = EditorGUI.GetPropertyHeight(m_FontSize);
            m_LineSpacingHeight  = EditorGUI.GetPropertyHeight(m_LineSpacing);
            m_EncodingHeight   = EditorGUI.GetPropertyHeight(m_SupportEncoding);
            m_ResizeTextForBestFitHeight = EditorGUI.GetPropertyHeight(m_ResizeTextForBestFit);
            m_ResizeTextMinSizeHeight = EditorGUI.GetPropertyHeight(m_ResizeTextMinSize);
            m_ResizeTextMaxSizeHeight = EditorGUI.GetPropertyHeight(m_ResizeTextMaxSize);
            m_HorizontalOverflowHeight = EditorGUI.GetPropertyHeight(m_HorizontalOverflow);
            m_VerticalOverflowHeight = EditorGUI.GetPropertyHeight(m_VerticalOverflow);
            m_AlignByGeometryHeight = EditorGUI.GetPropertyHeight(m_AlignByGeometry);

            var height = m_FontFieldfHeight
                + m_FontStyleHeight
                + m_FontSizeHeight
                + m_LineSpacingHeight
                + m_EncodingHeight
                + m_ResizeTextForBestFitHeight
                + m_HorizontalOverflowHeight
                + m_VerticalOverflowHeight
                + EditorGUIUtility.singleLineHeight * 3
                + EditorGUIUtility.standardVerticalSpacing * 10
                + m_AlignByGeometryHeight;

            if (m_ResizeTextForBestFit.boolValue)
            {
                height += m_ResizeTextMinSizeHeight
                    + m_ResizeTextMaxSizeHeight
                    + EditorGUIUtility.standardVerticalSpacing * 2;
            }
            return height;
        }

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            Init(property);

            Rect rect = position;
            rect.height = EditorGUIUtility.singleLineHeight;

            EditorGUI.LabelField(rect, "Character", EditorStyles.boldLabel);
            rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
            ++EditorGUI.indentLevel;
            {
                Font font = m_Font.objectReferenceValue as Font;
                rect.height = m_FontFieldfHeight;
                EditorGUI.BeginChangeCheck();
                EditorGUI.PropertyField(rect, m_Font);
                if (EditorGUI.EndChangeCheck())
                {
                    font = m_Font.objectReferenceValue as Font;
                    if (font != null && !font.dynamic)
                        m_FontSize.intValue = font.fontSize;
                }

                rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                rect.height = m_FontStyleHeight;
                using (new EditorGUI.DisabledScope(!m_Font.hasMultipleDifferentValues && font != null && !font.dynamic))
                {
                    EditorGUI.PropertyField(rect, m_FontStyle);
                }

                rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                rect.height = m_FontSizeHeight;
                EditorGUI.PropertyField(rect, m_FontSize);

                rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                rect.height = m_LineSpacingHeight;
                EditorGUI.PropertyField(rect, m_LineSpacing);

                rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                rect.height = m_EncodingHeight;
                EditorGUI.PropertyField(rect, m_SupportEncoding, Styles.m_EncodingContent);
            }
            --EditorGUI.indentLevel;

            rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
            rect.height = EditorGUIUtility.singleLineHeight;
            EditorGUI.LabelField(rect, "Paragraph", EditorStyles.boldLabel);
            rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
            ++EditorGUI.indentLevel;
            {
                rect.height = EditorGUIUtility.singleLineHeight;
                DoTextAligmentControl(rect, m_Alignment);

                rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                rect.height = m_HorizontalOverflowHeight;
                EditorGUI.PropertyField(rect, m_AlignByGeometry);

                rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                rect.height = m_HorizontalOverflowHeight;
                EditorGUI.PropertyField(rect, m_HorizontalOverflow);

                rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                rect.height = m_VerticalOverflowHeight;
                EditorGUI.PropertyField(rect, m_VerticalOverflow);

                rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                rect.height = m_ResizeTextMaxSizeHeight;
                EditorGUI.PropertyField(rect, m_ResizeTextForBestFit);

                if (m_ResizeTextForBestFit.boolValue)
                {
                    rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                    rect.height = m_ResizeTextMinSizeHeight;
                    EditorGUI.PropertyField(rect, m_ResizeTextMinSize);

                    rect.y += rect.height + EditorGUIUtility.standardVerticalSpacing;
                    rect.height = m_ResizeTextMaxSizeHeight;
                    EditorGUI.PropertyField(rect, m_ResizeTextMaxSize);
                }
            }
            --EditorGUI.indentLevel;
        }

        private void DoTextAligmentControl(Rect position, SerializedProperty alignment)
        {
            GUIContent alingmentContent = EditorGUIUtility.TrTextContent("Alignment");

            int id = EditorGUIUtility.GetControlID(s_TextAlignmentHash, FocusType.Keyboard, position);

            EditorGUIUtility.SetIconSize(new Vector2(15, 15));
            EditorGUI.BeginProperty(position, alingmentContent, alignment);
            {
                Rect controlArea = EditorGUI.PrefixLabel(position, id, alingmentContent);

                float width = kAlignmentButtonWidth * 3;
                float spacing = Mathf.Clamp(controlArea.width - width * 2, 2, 10);

                Rect horizontalAligment = new Rect(controlArea.x, controlArea.y, width, controlArea.height);
                Rect verticalAligment = new Rect(horizontalAligment.xMax + spacing, controlArea.y, width, controlArea.height);

                DoHorizontalAligmentControl(horizontalAligment, alignment);
                DoVerticalAligmentControl(verticalAligment, alignment);
            }
            EditorGUI.EndProperty();
            EditorGUIUtility.SetIconSize(Vector2.zero);
        }

        private static void DoHorizontalAligmentControl(Rect position, SerializedProperty alignment)
        {
            TextAnchor ta = (TextAnchor)alignment.intValue;
            HorizontalTextAligment horizontalAlignment = GetHorizontalAlignment(ta);

            bool leftAlign = (horizontalAlignment == HorizontalTextAligment.Left);
            bool centerAlign = (horizontalAlignment == HorizontalTextAligment.Center);
            bool rightAlign = (horizontalAlignment == HorizontalTextAligment.Right);

            if (alignment.hasMultipleDifferentValues)
            {
                foreach (var obj in alignment.serializedObject.targetObjects)
                {
                    Text text = obj as Text;
                    horizontalAlignment = GetHorizontalAlignment(text.alignment);
                    leftAlign = leftAlign || (horizontalAlignment == HorizontalTextAligment.Left);
                    centerAlign = centerAlign || (horizontalAlignment == HorizontalTextAligment.Center);
                    rightAlign = rightAlign || (horizontalAlignment == HorizontalTextAligment.Right);
                }
            }

            position.width = kAlignmentButtonWidth;

            EditorGUI.BeginChangeCheck();
            EditorToggle(position, leftAlign, leftAlign ? Styles.m_LeftAlignTextActive : Styles.m_LeftAlignText, Styles.alignmentButtonLeft);
            if (EditorGUI.EndChangeCheck())
            {
                SetHorizontalAlignment(alignment, HorizontalTextAligment.Left);
            }

            position.x += position.width;
            EditorGUI.BeginChangeCheck();
            EditorToggle(position, centerAlign, centerAlign ? Styles.m_CenterAlignTextActive : Styles.m_CenterAlignText, Styles.alignmentButtonMid);
            if (EditorGUI.EndChangeCheck())
            {
                SetHorizontalAlignment(alignment, HorizontalTextAligment.Center);
            }

            position.x += position.width;
            EditorGUI.BeginChangeCheck();
            EditorToggle(position, rightAlign, rightAlign ? Styles.m_RightAlignTextActive : Styles.m_RightAlignText, Styles.alignmentButtonRight);
            if (EditorGUI.EndChangeCheck())
            {
                SetHorizontalAlignment(alignment, HorizontalTextAligment.Right);
            }
        }

        private static void DoVerticalAligmentControl(Rect position, SerializedProperty alignment)
        {
            TextAnchor ta = (TextAnchor)alignment.intValue;
            VerticalTextAligment verticalTextAligment = GetVerticalAlignment(ta);

            bool topAlign = (verticalTextAligment == VerticalTextAligment.Top);
            bool middleAlign = (verticalTextAligment == VerticalTextAligment.Middle);
            bool bottomAlign = (verticalTextAligment == VerticalTextAligment.Bottom);

            if (alignment.hasMultipleDifferentValues)
            {
                foreach (var obj in alignment.serializedObject.targetObjects)
                {
                    Text text = obj as Text;
                    TextAnchor textAlignment = text.alignment;
                    verticalTextAligment = GetVerticalAlignment(textAlignment);
                    topAlign = topAlign || (verticalTextAligment == VerticalTextAligment.Top);
                    middleAlign = middleAlign || (verticalTextAligment == VerticalTextAligment.Middle);
                    bottomAlign = bottomAlign || (verticalTextAligment == VerticalTextAligment.Bottom);
                }
            }


            position.width = kAlignmentButtonWidth;

            // position.x += position.width;
            EditorGUI.BeginChangeCheck();
            EditorToggle(position, topAlign, topAlign ? Styles.m_TopAlignTextActive : Styles.m_TopAlignText, Styles.alignmentButtonLeft);
            if (EditorGUI.EndChangeCheck())
            {
                SetVerticalAlignment(alignment, VerticalTextAligment.Top);
            }

            position.x += position.width;
            EditorGUI.BeginChangeCheck();
            EditorToggle(position, middleAlign, middleAlign ? Styles.m_MiddleAlignTextActive : Styles.m_MiddleAlignText, Styles.alignmentButtonMid);
            if (EditorGUI.EndChangeCheck())
            {
                SetVerticalAlignment(alignment, VerticalTextAligment.Middle);
            }

            position.x += position.width;
            EditorGUI.BeginChangeCheck();
            EditorToggle(position, bottomAlign, bottomAlign ? Styles.m_BottomAlignTextActive : Styles.m_BottomAlignText, Styles.alignmentButtonRight);
            if (EditorGUI.EndChangeCheck())
            {
                SetVerticalAlignment(alignment, VerticalTextAligment.Bottom);
            }
        }

        private static bool EditorToggle(Rect position, bool value, GUIContent content, GUIStyle style)
        {
            int hashCode = "AlignToggle".GetHashCode();
            int id = EditorGUIUtility.GetControlID(hashCode, FocusType.Keyboard, position);
            Event evt = Event.current;

            // Toggle selected toggle on space or return key
            if (EditorGUIUtility.keyboardControl == id && evt.type == EventType.KeyDown && (evt.keyCode == KeyCode.Space || evt.keyCode == KeyCode.Return || evt.keyCode == KeyCode.KeypadEnter))
            {
                value = !value;
                evt.Use();
                GUI.changed = true;
            }

            if (evt.type == EventType.KeyDown && Event.current.button == 0 && position.Contains(Event.current.mousePosition))
            {
                GUIUtility.keyboardControl = id;
                EditorGUIUtility.editingTextField = false;
                HandleUtility.Repaint();
            }

            bool returnValue = GUI.Toggle(position, id, value, content, style);

            return returnValue;
        }

        private static HorizontalTextAligment GetHorizontalAlignment(TextAnchor ta)
        {
            switch (ta)
            {
                case TextAnchor.MiddleCenter:
                case TextAnchor.UpperCenter:
                case TextAnchor.LowerCenter:
                    return HorizontalTextAligment.Center;

                case TextAnchor.UpperRight:
                case TextAnchor.MiddleRight:
                case TextAnchor.LowerRight:
                    return HorizontalTextAligment.Right;

                case TextAnchor.UpperLeft:
                case TextAnchor.MiddleLeft:
                case TextAnchor.LowerLeft:
                    return HorizontalTextAligment.Left;
            }

            return HorizontalTextAligment.Left;
        }

        private static VerticalTextAligment GetVerticalAlignment(TextAnchor ta)
        {
            switch (ta)
            {
                case TextAnchor.UpperLeft:
                case TextAnchor.UpperCenter:
                case TextAnchor.UpperRight:
                    return VerticalTextAligment.Top;

                case TextAnchor.MiddleLeft:
                case TextAnchor.MiddleCenter:
                case TextAnchor.MiddleRight:
                    return VerticalTextAligment.Middle;

                case TextAnchor.LowerLeft:
                case TextAnchor.LowerCenter:
                case TextAnchor.LowerRight:
                    return VerticalTextAligment.Bottom;
            }

            return VerticalTextAligment.Top;
        }

        // We can't go through serialized properties here since we're showing two controls for a single SerializzedProperty.
        private static void SetHorizontalAlignment(SerializedProperty alignment, HorizontalTextAligment horizontalAlignment)
        {
            foreach (var obj in alignment.serializedObject.targetObjects)
            {
                Text text = obj as Text;
                VerticalTextAligment currentVerticalAligment = GetVerticalAlignment(text.alignment);
                Undo.RecordObject(text, "Horizontal Alignment");
                text.alignment = GetAnchor(currentVerticalAligment, horizontalAlignment);
                EditorUtility.SetDirty(obj);
            }
        }

        private static void SetVerticalAlignment(SerializedProperty alignment, VerticalTextAligment verticalAlignment)
        {
            foreach (var obj in alignment.serializedObject.targetObjects)
            {
                Text text = obj as Text;
                HorizontalTextAligment currentHorizontalAligment = GetHorizontalAlignment(text.alignment);
                Undo.RecordObject(text, "Vertical Alignment");
                text.alignment = GetAnchor(verticalAlignment, currentHorizontalAligment);
                EditorUtility.SetDirty(obj);
            }
        }

        private static TextAnchor GetAnchor(VerticalTextAligment verticalTextAligment, HorizontalTextAligment horizontalTextAligment)
        {
            TextAnchor ac = TextAnchor.UpperLeft;

            switch (horizontalTextAligment)
            {
                case HorizontalTextAligment.Left:
                    switch (verticalTextAligment)
                    {
                        case VerticalTextAligment.Bottom:
                            ac = TextAnchor.LowerLeft;
                            break;
                        case VerticalTextAligment.Middle:
                            ac = TextAnchor.MiddleLeft;
                            break;
                        default:
                            ac = TextAnchor.UpperLeft;
                            break;
                    }
                    break;
                case HorizontalTextAligment.Center:
                    switch (verticalTextAligment)
                    {
                        case VerticalTextAligment.Bottom:
                            ac = TextAnchor.LowerCenter;
                            break;
                        case VerticalTextAligment.Middle:
                            ac = TextAnchor.MiddleCenter;
                            break;
                        default:
                            ac = TextAnchor.UpperCenter;
                            break;
                    }
                    break;
                default:
                    switch (verticalTextAligment)
                    {
                        case VerticalTextAligment.Bottom:
                            ac = TextAnchor.LowerRight;
                            break;
                        case VerticalTextAligment.Middle:
                            ac = TextAnchor.MiddleRight;
                            break;
                        default:
                            ac = TextAnchor.UpperRight;
                            break;
                    }
                    break;
            }
            return ac;
        }
    }
}