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.

270 lines
12 KiB

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEditor;
  4. using UnityEditor.UI;
  5. using UnityEditor.AnimatedValues;
  6. namespace TMPro.EditorUtilities
  7. {
  8. [CanEditMultipleObjects]
  9. [CustomEditor(typeof(TMP_InputField), true)]
  10. public class TMP_InputFieldEditor : SelectableEditor
  11. {
  12. private struct m_foldout
  13. { // Track Inspector foldout panel states, globally.
  14. public static bool textInput = true;
  15. public static bool fontSettings = true;
  16. public static bool extraSettings = true;
  17. //public static bool shadowSetting = false;
  18. //public static bool materialEditor = true;
  19. }
  20. SerializedProperty m_TextViewport;
  21. SerializedProperty m_TextComponent;
  22. SerializedProperty m_Text;
  23. SerializedProperty m_ContentType;
  24. SerializedProperty m_LineType;
  25. SerializedProperty m_InputType;
  26. SerializedProperty m_CharacterValidation;
  27. SerializedProperty m_InputValidator;
  28. SerializedProperty m_RegexValue;
  29. SerializedProperty m_KeyboardType;
  30. SerializedProperty m_CharacterLimit;
  31. SerializedProperty m_CaretBlinkRate;
  32. SerializedProperty m_CaretWidth;
  33. SerializedProperty m_CaretColor;
  34. SerializedProperty m_CustomCaretColor;
  35. SerializedProperty m_SelectionColor;
  36. SerializedProperty m_HideMobileInput;
  37. SerializedProperty m_Placeholder;
  38. SerializedProperty m_VerticalScrollbar;
  39. SerializedProperty m_ScrollbarScrollSensitivity;
  40. SerializedProperty m_OnValueChanged;
  41. SerializedProperty m_OnEndEdit;
  42. SerializedProperty m_OnSelect;
  43. SerializedProperty m_OnDeselect;
  44. SerializedProperty m_ReadOnly;
  45. SerializedProperty m_RichText;
  46. SerializedProperty m_RichTextEditingAllowed;
  47. SerializedProperty m_ResetOnDeActivation;
  48. SerializedProperty m_RestoreOriginalTextOnEscape;
  49. SerializedProperty m_OnFocusSelectAll;
  50. SerializedProperty m_GlobalPointSize;
  51. SerializedProperty m_GlobalFontAsset;
  52. AnimBool m_CustomColor;
  53. //TMP_InputValidator m_ValidationScript;
  54. protected override void OnEnable()
  55. {
  56. base.OnEnable();
  57. m_TextViewport = serializedObject.FindProperty("m_TextViewport");
  58. m_TextComponent = serializedObject.FindProperty("m_TextComponent");
  59. m_Text = serializedObject.FindProperty("m_Text");
  60. m_ContentType = serializedObject.FindProperty("m_ContentType");
  61. m_LineType = serializedObject.FindProperty("m_LineType");
  62. m_InputType = serializedObject.FindProperty("m_InputType");
  63. m_CharacterValidation = serializedObject.FindProperty("m_CharacterValidation");
  64. m_InputValidator = serializedObject.FindProperty("m_InputValidator");
  65. m_RegexValue = serializedObject.FindProperty("m_RegexValue");
  66. m_KeyboardType = serializedObject.FindProperty("m_KeyboardType");
  67. m_CharacterLimit = serializedObject.FindProperty("m_CharacterLimit");
  68. m_CaretBlinkRate = serializedObject.FindProperty("m_CaretBlinkRate");
  69. m_CaretWidth = serializedObject.FindProperty("m_CaretWidth");
  70. m_CaretColor = serializedObject.FindProperty("m_CaretColor");
  71. m_CustomCaretColor = serializedObject.FindProperty("m_CustomCaretColor");
  72. m_SelectionColor = serializedObject.FindProperty("m_SelectionColor");
  73. m_HideMobileInput = serializedObject.FindProperty("m_HideMobileInput");
  74. m_Placeholder = serializedObject.FindProperty("m_Placeholder");
  75. m_VerticalScrollbar = serializedObject.FindProperty("m_VerticalScrollbar");
  76. m_ScrollbarScrollSensitivity = serializedObject.FindProperty("m_ScrollSensitivity");
  77. m_OnValueChanged = serializedObject.FindProperty("m_OnValueChanged");
  78. m_OnEndEdit = serializedObject.FindProperty("m_OnEndEdit");
  79. m_OnSelect = serializedObject.FindProperty("m_OnSelect");
  80. m_OnDeselect = serializedObject.FindProperty("m_OnDeselect");
  81. m_ReadOnly = serializedObject.FindProperty("m_ReadOnly");
  82. m_RichText = serializedObject.FindProperty("m_RichText");
  83. m_RichTextEditingAllowed = serializedObject.FindProperty("m_isRichTextEditingAllowed");
  84. m_ResetOnDeActivation = serializedObject.FindProperty("m_ResetOnDeActivation");
  85. m_RestoreOriginalTextOnEscape = serializedObject.FindProperty("m_RestoreOriginalTextOnEscape");
  86. m_OnFocusSelectAll = serializedObject.FindProperty("m_OnFocusSelectAll");
  87. m_GlobalPointSize = serializedObject.FindProperty("m_GlobalPointSize");
  88. m_GlobalFontAsset = serializedObject.FindProperty("m_GlobalFontAsset");
  89. m_CustomColor = new AnimBool(m_CustomCaretColor.boolValue);
  90. m_CustomColor.valueChanged.AddListener(Repaint);
  91. }
  92. protected override void OnDisable()
  93. {
  94. base.OnDisable();
  95. m_CustomColor.valueChanged.RemoveListener(Repaint);
  96. }
  97. public override void OnInspectorGUI()
  98. {
  99. serializedObject.Update();
  100. base.OnInspectorGUI();
  101. EditorGUILayout.Space();
  102. EditorGUILayout.PropertyField(m_TextViewport);
  103. EditorGUILayout.PropertyField(m_TextComponent);
  104. TextMeshProUGUI text = null;
  105. if (m_TextComponent != null && m_TextComponent.objectReferenceValue != null)
  106. {
  107. text = m_TextComponent.objectReferenceValue as TextMeshProUGUI;
  108. //if (text.supportRichText)
  109. //{
  110. // EditorGUILayout.HelpBox("Using Rich Text with input is unsupported.", MessageType.Warning);
  111. //}
  112. }
  113. EditorGUI.BeginDisabledGroup(m_TextComponent == null || m_TextComponent.objectReferenceValue == null);
  114. // TEXT INPUT BOX
  115. EditorGUILayout.PropertyField(m_Text);
  116. // INPUT FIELD SETTINGS
  117. #region INPUT FIELD SETTINGS
  118. m_foldout.fontSettings = EditorGUILayout.Foldout(m_foldout.fontSettings, "Input Field Settings", true, TMP_UIStyleManager.boldFoldout);
  119. if (m_foldout.fontSettings)
  120. {
  121. EditorGUI.indentLevel++;
  122. EditorGUI.BeginChangeCheck();
  123. EditorGUILayout.PropertyField(m_GlobalFontAsset, new GUIContent("Font Asset", "Set the Font Asset for both Placeholder and Input Field text object."));
  124. if (EditorGUI.EndChangeCheck())
  125. {
  126. TMP_InputField inputField = target as TMP_InputField;
  127. inputField.SetGlobalFontAsset(m_GlobalFontAsset.objectReferenceValue as TMP_FontAsset);
  128. }
  129. EditorGUI.BeginChangeCheck();
  130. EditorGUILayout.PropertyField(m_GlobalPointSize, new GUIContent("Point Size", "Set the point size of both Placeholder and Input Field text object."));
  131. if (EditorGUI.EndChangeCheck())
  132. {
  133. TMP_InputField inputField = target as TMP_InputField;
  134. inputField.SetGlobalPointSize(m_GlobalPointSize.floatValue);
  135. }
  136. EditorGUI.BeginChangeCheck();
  137. EditorGUILayout.PropertyField(m_CharacterLimit);
  138. EditorGUILayout.Space();
  139. EditorGUILayout.PropertyField(m_ContentType);
  140. if (!m_ContentType.hasMultipleDifferentValues)
  141. {
  142. EditorGUI.indentLevel++;
  143. if (m_ContentType.enumValueIndex == (int)TMP_InputField.ContentType.Standard ||
  144. m_ContentType.enumValueIndex == (int)TMP_InputField.ContentType.Autocorrected ||
  145. m_ContentType.enumValueIndex == (int)TMP_InputField.ContentType.Custom)
  146. {
  147. EditorGUI.BeginChangeCheck();
  148. EditorGUILayout.PropertyField(m_LineType);
  149. if (EditorGUI.EndChangeCheck())
  150. {
  151. if (text != null)
  152. {
  153. if (m_LineType.enumValueIndex == (int)TMP_InputField.LineType.SingleLine)
  154. text.enableWordWrapping = false;
  155. else
  156. text.enableWordWrapping = true;
  157. }
  158. }
  159. }
  160. if (m_ContentType.enumValueIndex == (int)TMP_InputField.ContentType.Custom)
  161. {
  162. EditorGUILayout.PropertyField(m_InputType);
  163. EditorGUILayout.PropertyField(m_KeyboardType);
  164. EditorGUILayout.PropertyField(m_CharacterValidation);
  165. if (m_CharacterValidation.enumValueIndex == (int)TMP_InputField.CharacterValidation.Regex)
  166. {
  167. EditorGUILayout.PropertyField(m_RegexValue);
  168. }
  169. else if (m_CharacterValidation.enumValueIndex == (int)TMP_InputField.CharacterValidation.CustomValidator)
  170. {
  171. EditorGUILayout.PropertyField(m_InputValidator);
  172. }
  173. }
  174. EditorGUI.indentLevel--;
  175. }
  176. EditorGUILayout.Space();
  177. EditorGUILayout.PropertyField(m_Placeholder);
  178. EditorGUILayout.PropertyField(m_VerticalScrollbar);
  179. if (m_VerticalScrollbar.objectReferenceValue != null)
  180. EditorGUILayout.PropertyField(m_ScrollbarScrollSensitivity);
  181. EditorGUILayout.PropertyField(m_CaretBlinkRate);
  182. EditorGUILayout.PropertyField(m_CaretWidth);
  183. EditorGUILayout.PropertyField(m_CustomCaretColor);
  184. m_CustomColor.target = m_CustomCaretColor.boolValue;
  185. if (EditorGUILayout.BeginFadeGroup(m_CustomColor.faded))
  186. {
  187. EditorGUILayout.PropertyField(m_CaretColor);
  188. }
  189. EditorGUILayout.EndFadeGroup();
  190. EditorGUILayout.PropertyField(m_SelectionColor);
  191. EditorGUI.indentLevel--;
  192. }
  193. #endregion
  194. // CONTROL SETTINGS
  195. #region CONTROL SETTINGS
  196. m_foldout.extraSettings = EditorGUILayout.Foldout(m_foldout.extraSettings, "Control Settings", true, TMP_UIStyleManager.boldFoldout);
  197. if (m_foldout.extraSettings)
  198. {
  199. EditorGUI.indentLevel++;
  200. EditorGUILayout.PropertyField(m_OnFocusSelectAll, new GUIContent("OnFocus - Select All", "Should all the text be selected when the Input Field is selected."));
  201. EditorGUILayout.PropertyField(m_ResetOnDeActivation, new GUIContent("Reset On DeActivation", "Should the Text and Caret position be reset when Input Field is DeActivated."));
  202. EditorGUILayout.PropertyField(m_RestoreOriginalTextOnEscape, new GUIContent("Restore On ESC Key", "Should the original text be restored when pressing ESC."));
  203. EditorGUILayout.PropertyField(m_HideMobileInput);
  204. EditorGUILayout.PropertyField(m_ReadOnly);
  205. EditorGUILayout.PropertyField(m_RichText);
  206. EditorGUILayout.PropertyField(m_RichTextEditingAllowed, new GUIContent("Allow Rich Text Editing"));
  207. EditorGUI.indentLevel--;
  208. }
  209. #endregion
  210. EditorGUILayout.Space();
  211. EditorGUILayout.PropertyField(m_OnValueChanged);
  212. EditorGUILayout.PropertyField(m_OnEndEdit);
  213. EditorGUILayout.PropertyField(m_OnSelect);
  214. EditorGUILayout.PropertyField(m_OnDeselect);
  215. EditorGUI.EndDisabledGroup();
  216. serializedObject.ApplyModifiedProperties();
  217. }
  218. }
  219. }