TMP_InputFieldEditor.cs 13 KB

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