TMP_TextAlignmentDrawer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace TMPro.EditorUtilities
  4. {
  5. [CustomPropertyDrawer(typeof(TextAlignmentOptions))]
  6. public class TMP_TextAlignmentDrawer : PropertyDrawer
  7. {
  8. const int k_AlignmentButtonWidth = 24;
  9. const int k_AlignmentButtonHeight = 20;
  10. const int k_WideViewWidth = 504;
  11. const int k_ControlsSpacing = 6;
  12. const int k_GroupWidth = k_AlignmentButtonWidth * 6;
  13. static readonly int k_TextAlignmentHash = "DoTextAligmentControl".GetHashCode();
  14. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  15. {
  16. return EditorGUIUtility.currentViewWidth > k_WideViewWidth ? k_AlignmentButtonHeight : k_AlignmentButtonHeight * 2 + 3;
  17. }
  18. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  19. {
  20. var id = GUIUtility.GetControlID(k_TextAlignmentHash, FocusType.Keyboard, position);
  21. EditorGUI.BeginProperty(position, label, property);
  22. {
  23. var controlArea = EditorGUI.PrefixLabel(position, id, label);
  24. var horizontalAligment = new Rect(controlArea.x, controlArea.y, k_GroupWidth, k_AlignmentButtonHeight);
  25. var verticalAligment = new Rect(!(EditorGUIUtility.currentViewWidth > k_WideViewWidth) ? controlArea.x : horizontalAligment.xMax + k_ControlsSpacing, !(EditorGUIUtility.currentViewWidth > k_WideViewWidth) ? controlArea.y + k_AlignmentButtonHeight + 3 : controlArea.y, k_GroupWidth, k_AlignmentButtonHeight);
  26. EditorGUI.BeginChangeCheck();
  27. var selectedHorizontal = DoHorizontalAligmentControl(horizontalAligment, property);
  28. var selectedVertical = DoVerticalAligmentControl(verticalAligment, property);
  29. if (EditorGUI.EndChangeCheck())
  30. {
  31. var value = (0x1 << selectedHorizontal) | (0x100 << selectedVertical);
  32. property.intValue = value;
  33. }
  34. }
  35. EditorGUI.EndProperty();
  36. }
  37. static int DoHorizontalAligmentControl(Rect position, SerializedProperty alignment)
  38. {
  39. var selected = TMP_EditorUtility.GetHorizontalAlignmentGridValue(alignment.intValue);
  40. var values = new bool[6];
  41. values[selected] = true;
  42. if (alignment.hasMultipleDifferentValues)
  43. {
  44. foreach (var obj in alignment.serializedObject.targetObjects)
  45. {
  46. var text = obj as TMP_Text;
  47. if (text != null)
  48. {
  49. values[TMP_EditorUtility.GetHorizontalAlignmentGridValue((int)text.alignment)] = true;
  50. }
  51. }
  52. }
  53. position.width = k_AlignmentButtonWidth;
  54. for (var i = 0; i < values.Length; i++)
  55. {
  56. var oldValue = values[i];
  57. var newValue = TMP_EditorUtility.EditorToggle(position, oldValue, TMP_UIStyleManager.alignContentA[i], i == 0 ? TMP_UIStyleManager.alignmentButtonLeft : (i == 5 ? TMP_UIStyleManager.alignmentButtonRight : TMP_UIStyleManager.alignmentButtonMid));
  58. if (newValue != oldValue)
  59. {
  60. selected = i;
  61. }
  62. position.x += position.width;
  63. }
  64. return selected;
  65. }
  66. static int DoVerticalAligmentControl(Rect position, SerializedProperty alignment)
  67. {
  68. var selected = TMP_EditorUtility.GetVerticalAlignmentGridValue(alignment.intValue);
  69. var values = new bool[6];
  70. values[selected] = true;
  71. if (alignment.hasMultipleDifferentValues)
  72. {
  73. foreach (var obj in alignment.serializedObject.targetObjects)
  74. {
  75. var text = obj as TMP_Text;
  76. if (text != null)
  77. {
  78. values[TMP_EditorUtility.GetVerticalAlignmentGridValue((int)text.alignment)] = true;
  79. }
  80. }
  81. }
  82. position.width = k_AlignmentButtonWidth;
  83. for (var i = 0; i < values.Length; i++)
  84. {
  85. var oldValue = values[i];
  86. var newValue = TMP_EditorUtility.EditorToggle(position, oldValue, TMP_UIStyleManager.alignContentB[i], i == 0 ? TMP_UIStyleManager.alignmentButtonLeft : (i == 5 ? TMP_UIStyleManager.alignmentButtonRight : TMP_UIStyleManager.alignmentButtonMid));
  87. if (newValue != oldValue)
  88. {
  89. selected = i;
  90. }
  91. position.x += position.width;
  92. }
  93. return selected;
  94. }
  95. }
  96. }