You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.9 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. namespace TMPro.EditorUtilities
  5. {
  6. [CustomPropertyDrawer(typeof(KerningPair))]
  7. public class KerningPairDrawer : PropertyDrawer
  8. {
  9. private bool isEditingEnabled = false;
  10. private bool isSelectable = false;
  11. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  12. {
  13. SerializedProperty prop_FirstGlyph = property.FindPropertyRelative("m_FirstGlyph");
  14. SerializedProperty prop_FirstGlyphAdjustment = property.FindPropertyRelative("m_FirstGlyphAdjustments");
  15. SerializedProperty prop_SecondGlyph = property.FindPropertyRelative("m_SecondGlyph");
  16. SerializedProperty prop_SecondGlyphAdjustment = property.FindPropertyRelative("m_SecondGlyphAdjustments");
  17. position.yMin += 2;
  18. // We get Rect since a valid position may not be provided by the caller.
  19. GUILayoutUtility.GetRect(position.width, 45);
  20. float width = position.width / 2;
  21. float padding = 5.0f;
  22. Rect rect;
  23. isEditingEnabled = GUI.enabled;
  24. isSelectable = label.text == "Selectable" ? true : false;
  25. // First Glyph
  26. GUI.enabled = isEditingEnabled;
  27. if (isSelectable)
  28. {
  29. bool prevGuiState = GUI.enabled;
  30. GUI.enabled = true;
  31. rect = new Rect(position.x, position.y, 40, 18);
  32. EditorGUI.LabelField(rect, "Char:", TMP_UIStyleManager.label);
  33. rect = new Rect(position.x + 35f, position.y, 30, 18);
  34. EditorGUI.LabelField(rect, "<color=#FFFF80>" + (char)prop_FirstGlyph.intValue + "</color>", TMP_UIStyleManager.label);
  35. // Display ASCII decimal value
  36. rect = new Rect(position.x + 60f, position.y, 30, 18);
  37. EditorGUI.LabelField(rect, "ID:", TMP_UIStyleManager.label);
  38. rect = new Rect(position.x + 80f, position.y, 40, 18);
  39. EditorGUI.LabelField(rect, "<color=#FFFF80>" + prop_FirstGlyph.intValue + "</color>", TMP_UIStyleManager.label);
  40. GUI.enabled = prevGuiState;
  41. }
  42. else
  43. {
  44. rect = new Rect(position.x, position.y, width / 2 * 0.8f - padding, 18);
  45. string glyph = EditorGUI.TextArea(rect, "" + (char)prop_FirstGlyph.intValue);
  46. if (GUI.changed && glyph != "")
  47. {
  48. GUI.changed = false;
  49. prop_FirstGlyph.intValue = glyph[0];
  50. }
  51. rect.x += width / 2 * 0.8f;
  52. EditorGUI.PropertyField(rect, prop_FirstGlyph, GUIContent.none);
  53. }
  54. GUI.enabled = isEditingEnabled;
  55. EditorGUIUtility.labelWidth = 25f;
  56. rect = new Rect(position.x, position.y + 20, width * 0.5f - padding, 18);
  57. EditorGUI.PropertyField(rect, prop_FirstGlyphAdjustment.FindPropertyRelative("xPlacement"), new GUIContent("OX"));
  58. rect.x += width * 0.5f;
  59. EditorGUI.PropertyField(rect, prop_FirstGlyphAdjustment.FindPropertyRelative("yPlacement"), new GUIContent("OY"));
  60. rect.x = position.x;
  61. rect.y += 20;
  62. EditorGUI.PropertyField(rect, prop_FirstGlyphAdjustment.FindPropertyRelative("xAdvance"), new GUIContent("AX"));
  63. //rect.x += width * 0.5f;
  64. //EditorGUI.PropertyField(rect, prop_FirstGlyphAdjustment.FindPropertyRelative("yAdvance"), new GUIContent("AY"));
  65. // Second Glyph
  66. GUI.enabled = isEditingEnabled;
  67. if (isSelectable)
  68. {
  69. bool prevGuiState = GUI.enabled;
  70. GUI.enabled = true;
  71. rect = new Rect(position.width / 2 + 20, position.y, 40f, 18);
  72. EditorGUI.LabelField(rect, "Char:", TMP_UIStyleManager.label);
  73. rect = new Rect(rect.x + 35f, position.y, 30, 18);
  74. EditorGUI.LabelField(rect, "<color=#FFFF80>" + (char)prop_SecondGlyph.intValue + "</color>", TMP_UIStyleManager.label);
  75. // Display ASCII decimal value
  76. rect = new Rect(rect.x + 25f, position.y, 30, 18);
  77. EditorGUI.LabelField(rect, "ID:", TMP_UIStyleManager.label);
  78. rect = new Rect(rect.x + 20f, position.y, 40, 18);
  79. EditorGUI.LabelField(rect, "<color=#FFFF80>" + prop_SecondGlyph.intValue + "</color>", TMP_UIStyleManager.label);
  80. GUI.enabled = prevGuiState;
  81. }
  82. else
  83. {
  84. rect = new Rect(position.width / 2 + 20, position.y, width / 2 * 0.8f - padding, 18);
  85. string glyph = EditorGUI.TextArea(rect, "" + (char)prop_SecondGlyph.intValue);
  86. if (GUI.changed && glyph != "")
  87. {
  88. GUI.changed = false;
  89. prop_SecondGlyph.intValue = glyph[0];
  90. }
  91. rect.x += width / 2 * 0.8f;
  92. EditorGUI.PropertyField(rect, prop_SecondGlyph, GUIContent.none);
  93. }
  94. GUI.enabled = isEditingEnabled;
  95. EditorGUIUtility.labelWidth = 25f;
  96. rect = new Rect(position.width / 2 + 20, position.y + 20, width * 0.5f - padding, 18);
  97. EditorGUI.PropertyField(rect, prop_SecondGlyphAdjustment.FindPropertyRelative("xPlacement"), new GUIContent("OX"));
  98. rect.x += width * 0.5f;
  99. EditorGUI.PropertyField(rect, prop_SecondGlyphAdjustment.FindPropertyRelative("yPlacement"), new GUIContent("OY"));
  100. rect.x = position.width / 2 + 20;
  101. rect.y += 20;
  102. EditorGUI.PropertyField(rect, prop_SecondGlyphAdjustment.FindPropertyRelative("xAdvance"), new GUIContent("AX"));
  103. //rect.x += width * 0.5f;
  104. //EditorGUI.PropertyField(rect, prop_SecondGlyphAdjustment.FindPropertyRelative("yAdvance"), new GUIContent("AY"));
  105. }
  106. }
  107. }