Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TMP_StyleSheetEditor.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. float totalWidth = rect.width;
  144. rect.width = totalWidth * 0.175f;
  145. // Move Style up.
  146. bool guiEnabled = GUI.enabled;
  147. if (m_SelectedElement == -1 || m_SelectedElement == 0) { GUI.enabled = false; }
  148. if (GUI.Button(rect, "Up"))
  149. {
  150. SwapStyleElements(m_SelectedElement, m_SelectedElement - 1);
  151. }
  152. GUI.enabled = guiEnabled;
  153. // Move Style down.
  154. rect.x += rect.width;
  155. if (m_SelectedElement == elementCount - 1) { GUI.enabled = false; }
  156. if (GUI.Button(rect, "Down"))
  157. {
  158. SwapStyleElements(m_SelectedElement, m_SelectedElement + 1);
  159. }
  160. GUI.enabled = guiEnabled;
  161. // Add Style
  162. rect.x += rect.width + totalWidth * 0.3f;
  163. if (GUI.Button(rect, "+"))
  164. {
  165. int index = m_SelectedElement == -1 ? elementCount : m_SelectedElement;
  166. if (index > elementCount)
  167. index = elementCount;
  168. // Copy selected element
  169. m_StyleListProp.InsertArrayElementAtIndex(index);
  170. // Select newly inserted element
  171. m_SelectedElement = index + 1;
  172. serializedObject.ApplyModifiedProperties();
  173. m_StyleSheet.RefreshStyles();
  174. }
  175. // Delete style
  176. rect.x += rect.width;
  177. if (m_SelectedElement == -1 || m_SelectedElement >= elementCount) GUI.enabled = false;
  178. if (GUI.Button(rect, "-"))
  179. {
  180. int index = m_SelectedElement == -1 ? 0 : m_SelectedElement;
  181. m_StyleListProp.DeleteArrayElementAtIndex(index);
  182. m_SelectedElement = -1;
  183. serializedObject.ApplyModifiedProperties();
  184. m_StyleSheet.RefreshStyles();
  185. return;
  186. }
  187. // Return if we can't display any items.
  188. if (itemsPerPage == 0) return;
  189. // DISPLAY PAGE CONTROLS
  190. int shiftMultiplier = currentEvent.shift ? 10 : 1; // Page + Shift goes 10 page forward
  191. Rect pagePos = EditorGUILayout.GetControlRect(false, 20);
  192. pagePos.width = totalWidth * 0.35f;
  193. // Previous Page
  194. if (m_Page > 0) GUI.enabled = true;
  195. else GUI.enabled = false;
  196. if (GUI.Button(pagePos, "Previous"))
  197. m_Page -= 1 * shiftMultiplier;
  198. // PAGE COUNTER
  199. GUI.enabled = true;
  200. pagePos.x += pagePos.width;
  201. pagePos.width = totalWidth * 0.30f;
  202. int totalPages = (int)(elementCount / (float)itemsPerPage + 0.999f);
  203. GUI.Label(pagePos, "Page " + (m_Page + 1) + " / " + totalPages, TMP_UIStyleManager.centeredLabel);
  204. // Next Page
  205. pagePos.x += pagePos.width;
  206. pagePos.width = totalWidth * 0.35f;
  207. if (itemsPerPage * (m_Page + 1) < elementCount) GUI.enabled = true;
  208. else GUI.enabled = false;
  209. if (GUI.Button(pagePos, "Next"))
  210. m_Page += 1 * shiftMultiplier;
  211. // Clamp page range
  212. m_Page = Mathf.Clamp(m_Page, 0, elementCount / itemsPerPage);
  213. if (serializedObject.ApplyModifiedProperties())
  214. {
  215. TMPro_EventManager.ON_TEXT_STYLE_PROPERTY_CHANGED(true);
  216. if (m_IsStyleSheetDirty)
  217. {
  218. m_IsStyleSheetDirty = false;
  219. m_StyleSheet.RefreshStyles();
  220. }
  221. }
  222. // Clear selection if mouse event was not consumed.
  223. GUI.enabled = true;
  224. if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
  225. m_SelectedElement = -1;
  226. }
  227. // Check if any of the Style elements were clicked on.
  228. static bool DoSelectionCheck(Rect selectionArea)
  229. {
  230. Event currentEvent = Event.current;
  231. switch (currentEvent.type)
  232. {
  233. case EventType.MouseDown:
  234. if (selectionArea.Contains(currentEvent.mousePosition) && currentEvent.button == 0)
  235. {
  236. currentEvent.Use();
  237. return true;
  238. }
  239. break;
  240. }
  241. return false;
  242. }
  243. void SwapStyleElements(int selectedIndex, int newIndex)
  244. {
  245. m_StyleListProp.MoveArrayElement(selectedIndex, newIndex);
  246. m_SelectedElement = newIndex;
  247. m_IsStyleSheetDirty = true;
  248. }
  249. }
  250. }