TMP_StyleSheetEditor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. namespace TMPro.EditorUtilities
  5. {
  6. [CustomPropertyDrawer(typeof(TMP_Style))]
  7. public class StyleDrawer : PropertyDrawer
  8. {
  9. public static readonly float height = 95f;
  10. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  11. {
  12. return height;
  13. }
  14. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  15. {
  16. SerializedProperty nameProperty = property.FindPropertyRelative("m_Name");
  17. SerializedProperty hashCodeProperty = property.FindPropertyRelative("m_HashCode");
  18. SerializedProperty openingDefinitionProperty = property.FindPropertyRelative("m_OpeningDefinition");
  19. SerializedProperty closingDefinitionProperty = property.FindPropertyRelative("m_ClosingDefinition");
  20. SerializedProperty openingDefinitionArray = property.FindPropertyRelative("m_OpeningTagArray");
  21. SerializedProperty closingDefinitionArray = property.FindPropertyRelative("m_ClosingTagArray");
  22. EditorGUIUtility.labelWidth = 86;
  23. position.height = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  24. float labelHeight = position.height + 2f;
  25. EditorGUI.BeginChangeCheck();
  26. Rect rect0 = new Rect(position.x, position.y, (position.width) / 2 + 5, position.height);
  27. EditorGUI.PropertyField(rect0, nameProperty);
  28. if (EditorGUI.EndChangeCheck())
  29. {
  30. // Recompute HashCode if name has changed.
  31. hashCodeProperty.intValue = TMP_TextUtilities.GetSimpleHashCode(nameProperty.stringValue);
  32. property.serializedObject.ApplyModifiedProperties();
  33. // Dictionary needs to be updated since HashCode has changed.
  34. TMP_StyleSheet styleSheet = property.serializedObject.targetObject as TMP_StyleSheet;
  35. styleSheet.RefreshStyles();
  36. }
  37. // HashCode
  38. Rect rect1 = new Rect(rect0.x + rect0.width + 5, position.y, 65, position.height);
  39. GUI.Label(rect1, "HashCode");
  40. GUI.enabled = false;
  41. rect1.x += 65;
  42. rect1.width = position.width / 2 - 75;
  43. EditorGUI.PropertyField(rect1, hashCodeProperty, GUIContent.none);
  44. GUI.enabled = true;
  45. // Text Tags
  46. EditorGUI.BeginChangeCheck();
  47. // Opening Tags
  48. position.y += labelHeight;
  49. GUI.Label(position, "Opening Tags");
  50. Rect textRect1 = new Rect(110, position.y, position.width - 86, 35);
  51. openingDefinitionProperty.stringValue = EditorGUI.TextArea(textRect1, openingDefinitionProperty.stringValue);
  52. if (EditorGUI.EndChangeCheck())
  53. {
  54. // If any properties have changed, we need to update the Opening and Closing Arrays.
  55. int size = openingDefinitionProperty.stringValue.Length;
  56. // Adjust array size to match new string length.
  57. if (openingDefinitionArray.arraySize != size) openingDefinitionArray.arraySize = size;
  58. for (int i = 0; i < size; i++)
  59. {
  60. SerializedProperty element = openingDefinitionArray.GetArrayElementAtIndex(i);
  61. element.intValue = openingDefinitionProperty.stringValue[i];
  62. }
  63. }
  64. EditorGUI.BeginChangeCheck();
  65. // Closing Tags
  66. position.y += 38;
  67. GUI.Label(position, "Closing Tags");
  68. Rect textRect2 = new Rect(110, position.y, position.width - 86, 35);
  69. closingDefinitionProperty.stringValue = EditorGUI.TextArea(textRect2, closingDefinitionProperty.stringValue);
  70. if (EditorGUI.EndChangeCheck())
  71. {
  72. // If any properties have changed, we need to update the Opening and Closing Arrays.
  73. int size = closingDefinitionProperty.stringValue.Length;
  74. // Adjust array size to match new string length.
  75. if (closingDefinitionArray.arraySize != size) closingDefinitionArray.arraySize = size;
  76. for (int i = 0; i < size; i++)
  77. {
  78. SerializedProperty element = closingDefinitionArray.GetArrayElementAtIndex(i);
  79. element.intValue = closingDefinitionProperty.stringValue[i];
  80. }
  81. }
  82. }
  83. }
  84. [CustomEditor(typeof(TMP_StyleSheet)), CanEditMultipleObjects]
  85. public class TMP_StyleEditor : Editor
  86. {
  87. TMP_StyleSheet m_StyleSheet;
  88. SerializedProperty m_StyleListProp;
  89. int m_SelectedElement = -1;
  90. int m_Page;
  91. bool m_IsStyleSheetDirty;
  92. void OnEnable()
  93. {
  94. m_StyleSheet = target as TMP_StyleSheet;
  95. m_StyleListProp = serializedObject.FindProperty("m_StyleList");
  96. }
  97. public override void OnInspectorGUI()
  98. {
  99. Event currentEvent = Event.current;
  100. serializedObject.Update();
  101. m_IsStyleSheetDirty = false;
  102. int elementCount = m_StyleListProp.arraySize;
  103. int itemsPerPage = (Screen.height - 100) / 110;
  104. if (elementCount > 0)
  105. {
  106. // Display each Style entry using the StyleDrawer PropertyDrawer.
  107. for (int i = itemsPerPage * m_Page; i < elementCount && i < itemsPerPage * (m_Page + 1); i++)
  108. {
  109. // Define the start of the selection region of the element.
  110. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  111. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  112. SerializedProperty styleProperty = m_StyleListProp.GetArrayElementAtIndex(i);
  113. EditorGUI.BeginChangeCheck();
  114. EditorGUILayout.PropertyField(styleProperty);
  115. EditorGUILayout.EndVertical();
  116. if (EditorGUI.EndChangeCheck())
  117. {
  118. //
  119. }
  120. // Define the end of the selection region of the element.
  121. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  122. // Check for Item selection
  123. Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y);
  124. if (DoSelectionCheck(selectionArea))
  125. {
  126. if (m_SelectedElement == i)
  127. {
  128. m_SelectedElement = -1;
  129. }
  130. else
  131. {
  132. m_SelectedElement = i;
  133. GUIUtility.keyboardControl = 0;
  134. }
  135. }
  136. // Handle Selection Highlighting
  137. if (m_SelectedElement == i)
  138. TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255));
  139. }
  140. }
  141. // STYLE LIST MANAGEMENT
  142. Rect rect = EditorGUILayout.GetControlRect(false, 20);
  143. rect.width /= 6;
  144. // Move Style up.
  145. bool guiEnabled = GUI.enabled;
  146. if (m_SelectedElement == -1 || m_SelectedElement == 0) { GUI.enabled = false; }
  147. if (GUI.Button(rect, "Up"))
  148. {
  149. SwapStyleElements(m_SelectedElement, m_SelectedElement - 1);
  150. }
  151. GUI.enabled = guiEnabled;
  152. // Move Style down.
  153. rect.x += rect.width;
  154. if (m_SelectedElement == elementCount - 1) { GUI.enabled = false; }
  155. if (GUI.Button(rect, "Down"))
  156. {
  157. SwapStyleElements(m_SelectedElement, m_SelectedElement + 1);
  158. }
  159. GUI.enabled = guiEnabled;
  160. // Add Style
  161. rect.x += rect.width * 3;
  162. if (GUI.Button(rect, "+"))
  163. {
  164. int index = m_SelectedElement == -1 ? 0 : m_SelectedElement;
  165. if (index > elementCount)
  166. index = elementCount;
  167. // Copy selected element
  168. m_StyleListProp.InsertArrayElementAtIndex(index);
  169. // Select newly inserted element
  170. m_SelectedElement = index + 1;
  171. serializedObject.ApplyModifiedProperties();
  172. m_StyleSheet.RefreshStyles();
  173. }
  174. // Delete style
  175. rect.x += rect.width;
  176. if (m_SelectedElement == -1 || m_SelectedElement >= elementCount) GUI.enabled = false;
  177. if (GUI.Button(rect, "-"))
  178. {
  179. int index = m_SelectedElement == -1 ? 0 : m_SelectedElement;
  180. m_StyleListProp.DeleteArrayElementAtIndex(index);
  181. m_SelectedElement = -1;
  182. serializedObject.ApplyModifiedProperties();
  183. m_StyleSheet.RefreshStyles();
  184. return;
  185. }
  186. // Return if we can't display any items.
  187. if (itemsPerPage == 0) return;
  188. // DISPLAY PAGE CONTROLS
  189. int shiftMultiplier = currentEvent.shift ? 10 : 1; // Page + Shift goes 10 page forward
  190. Rect pagePos = EditorGUILayout.GetControlRect(false, 20);
  191. pagePos.width /= 3;
  192. // Previous Page
  193. if (m_Page > 0) GUI.enabled = true;
  194. else GUI.enabled = false;
  195. if (GUI.Button(pagePos, "Previous"))
  196. m_Page -= 1 * shiftMultiplier;
  197. // PAGE COUNTER
  198. GUI.enabled = true;
  199. pagePos.x += pagePos.width;
  200. int totalPages = (int)(elementCount / (float)itemsPerPage + 0.999f);
  201. GUI.Label(pagePos, "Page " + (m_Page + 1) + " / " + totalPages, TMP_UIStyleManager.centeredLabel);
  202. // Next Page
  203. pagePos.x += pagePos.width;
  204. if (itemsPerPage * (m_Page + 1) < elementCount) GUI.enabled = true;
  205. else GUI.enabled = false;
  206. if (GUI.Button(pagePos, "Next"))
  207. m_Page += 1 * shiftMultiplier;
  208. // Clamp page range
  209. m_Page = Mathf.Clamp(m_Page, 0, elementCount / itemsPerPage);
  210. if (serializedObject.ApplyModifiedProperties())
  211. {
  212. TMPro_EventManager.ON_TEXT_STYLE_PROPERTY_CHANGED(true);
  213. if (m_IsStyleSheetDirty)
  214. {
  215. m_IsStyleSheetDirty = false;
  216. m_StyleSheet.RefreshStyles();
  217. }
  218. }
  219. // Clear selection if mouse event was not consumed.
  220. GUI.enabled = true;
  221. if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
  222. m_SelectedElement = -1;
  223. }
  224. // Check if any of the Style elements were clicked on.
  225. static bool DoSelectionCheck(Rect selectionArea)
  226. {
  227. Event currentEvent = Event.current;
  228. switch (currentEvent.type)
  229. {
  230. case EventType.MouseDown:
  231. if (selectionArea.Contains(currentEvent.mousePosition) && currentEvent.button == 0)
  232. {
  233. currentEvent.Use();
  234. return true;
  235. }
  236. break;
  237. }
  238. return false;
  239. }
  240. void SwapStyleElements(int selectedIndex, int newIndex)
  241. {
  242. m_StyleListProp.MoveArrayElement(selectedIndex, newIndex);
  243. m_SelectedElement = newIndex;
  244. m_IsStyleSheetDirty = true;
  245. }
  246. }
  247. }