BetterTextEditor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEditor.UI;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace TheraBytes.BetterUi.Editor
  10. {
  11. [CustomEditor(typeof(BetterText), true), CanEditMultipleObjects]
  12. public class BetterTextEditor : GraphicEditor
  13. {
  14. SerializedProperty text;
  15. SerializedProperty sizerFallback, sizerCollection, fitting;
  16. SerializedProperty font, style, lineSpace, rich, align, geoAlign, overflowH, overflowV;
  17. SerializedProperty maskable;
  18. [MenuItem("CONTEXT/Text/♠ Make Better")]
  19. public static void MakeBetter(MenuCommand command)
  20. {
  21. Text txt = command.context as Text;
  22. int size = txt.fontSize;
  23. bool bestFit = txt.resizeTextForBestFit;
  24. int min = txt.resizeTextMinSize;
  25. int max = txt.resizeTextMaxSize;
  26. var newTxt = Betterizer.MakeBetter<Text, BetterText>(txt);
  27. var betterTxt = newTxt as BetterText;
  28. if(betterTxt != null)
  29. {
  30. if(bestFit)
  31. {
  32. betterTxt.FontSizer.MinSize = min;
  33. betterTxt.FontSizer.MaxSize = max;
  34. }
  35. betterTxt.FontSizer.SetSize(betterTxt, size);
  36. }
  37. Betterizer.Validate(newTxt);
  38. }
  39. protected override void OnEnable()
  40. {
  41. base.OnEnable();
  42. this.text = base.serializedObject.FindProperty("m_Text");
  43. this.sizerFallback = base.serializedObject.FindProperty("fontSizerFallback");
  44. this.sizerCollection = base.serializedObject.FindProperty("customFontSizers");
  45. this.fitting = base.serializedObject.FindProperty("fitting");
  46. var fontData = base.serializedObject.FindProperty("m_FontData");
  47. this.font = fontData.FindPropertyRelative("m_Font");
  48. this.style = fontData.FindPropertyRelative("m_FontStyle");
  49. this.lineSpace = fontData.FindPropertyRelative("m_LineSpacing");
  50. this.rich = fontData.FindPropertyRelative("m_RichText");
  51. this.align = fontData.FindPropertyRelative("m_Alignment");
  52. this.geoAlign = fontData.FindPropertyRelative("m_AlignByGeometry");
  53. this.overflowH = fontData.FindPropertyRelative("m_HorizontalOverflow");
  54. this.overflowV = fontData.FindPropertyRelative("m_VerticalOverflow");
  55. this.maskable = base.serializedObject.FindProperty("m_Maskable");
  56. }
  57. public override void OnInspectorGUI()
  58. {
  59. var obj = base.target as BetterText;
  60. base.serializedObject.Update();
  61. EditorGUILayout.PropertyField(this.text);
  62. EditorGUILayout.LabelField("Better Sizing", EditorStyles.boldLabel);
  63. ScreenConfigConnectionHelper.DrawSizerGui("Font Size", sizerCollection, ref sizerFallback);
  64. EditorGUI.indentLevel += 1;
  65. EditorGUILayout.PropertyField(fitting);
  66. EditorGUI.indentLevel -= 1;
  67. EditorGUILayout.LabelField("Character", EditorStyles.boldLabel);
  68. EditorGUI.indentLevel += 1;
  69. EditorGUILayout.PropertyField(font);
  70. EditorGUILayout.PropertyField(style);
  71. EditorGUILayout.PropertyField(lineSpace);
  72. EditorGUILayout.PropertyField(rich);
  73. EditorGUI.indentLevel -= 1;
  74. EditorGUILayout.LabelField("Paragraph", EditorStyles.boldLabel);
  75. EditorGUI.indentLevel += 1;
  76. //EditorGUILayout.PropertyField(align);
  77. DrawAnchorIcons(align, obj.alignment);
  78. if(geoAlign != null) // not present in old unity versions
  79. EditorGUILayout.PropertyField(geoAlign);
  80. EditorGUILayout.PropertyField(overflowH);
  81. EditorGUILayout.PropertyField(overflowV);
  82. EditorGUI.indentLevel -= 1;
  83. base.AppearanceControlsGUI();
  84. base.RaycastControlsGUI();
  85. if(maskable != null)
  86. {
  87. EditorGUILayout.PropertyField(maskable);
  88. }
  89. base.serializedObject.ApplyModifiedProperties();
  90. }
  91. void DrawAnchorIcons(SerializedProperty prop, TextAnchor anchor)
  92. {
  93. bool hLeft = anchor == TextAnchor.LowerLeft || anchor == TextAnchor.MiddleLeft || anchor == TextAnchor.UpperLeft;
  94. bool hCenter = anchor == TextAnchor.LowerCenter || anchor == TextAnchor.MiddleCenter|| anchor == TextAnchor.UpperCenter;
  95. bool hRight = anchor == TextAnchor.LowerRight || anchor == TextAnchor.MiddleRight || anchor == TextAnchor.UpperRight;
  96. bool vTop = anchor == TextAnchor.UpperLeft || anchor == TextAnchor.UpperCenter || anchor == TextAnchor.UpperRight;
  97. bool vCenter = anchor == TextAnchor.MiddleLeft || anchor == TextAnchor.MiddleCenter || anchor == TextAnchor.MiddleRight;
  98. bool vBottom = anchor == TextAnchor.LowerLeft || anchor == TextAnchor.LowerCenter || anchor == TextAnchor.LowerRight;
  99. EditorGUILayout.BeginHorizontal();
  100. EditorGUILayout.LabelField("Alignment", GUILayout.Width(EditorGUIUtility.labelWidth - 12), GUILayout.ExpandWidth(false));
  101. bool hLeft2 = DrawAlignIcon(Styles.LeftAlignActive, Styles.LeftAlign, TextAlignment.Left, hLeft);
  102. bool hCenter2 = DrawAlignIcon(Styles.CenterAlignActive, Styles.CenterAlign, TextAlignment.Center, hCenter);
  103. bool hRight2 = DrawAlignIcon(Styles.RightAlignActive, Styles.RightAlign, TextAlignment.Right, hRight);
  104. if(hLeft != hLeft2 || hCenter != hCenter2 || hRight != hRight2)
  105. {
  106. hLeft = (hLeft == hLeft2) ? false : hLeft2;
  107. hCenter = (hCenter == hCenter2) ? false : hCenter2;
  108. hRight = (hRight == hRight2) ? false : hRight2;
  109. }
  110. EditorGUILayout.Separator();
  111. bool vTop2 = DrawAlignIcon(Styles.TopAlignActive, Styles.TopAlign, TextAlignment.Left, vTop);
  112. bool vCenter2 = DrawAlignIcon(Styles.MiddleAlignActive, Styles.MiddleAlign, TextAlignment.Center, vCenter);
  113. bool vBottom2 = DrawAlignIcon(Styles.BottomAlignActive, Styles.BottomAlign, TextAlignment.Right, vBottom);
  114. if(vTop != vTop2 || vCenter != vCenter2 || vBottom != vBottom2)
  115. {
  116. vTop = (vTop == vTop2) ? false : vTop2;
  117. vCenter = (vCenter == vCenter2) ? false : vCenter2;
  118. vBottom = (vBottom == vBottom2) ? false : vBottom2;
  119. }
  120. GUILayout.FlexibleSpace();
  121. EditorGUILayout.EndHorizontal();
  122. TextAnchor prev = anchor;
  123. if(hLeft)
  124. {
  125. anchor = (vTop)
  126. ? TextAnchor.UpperLeft
  127. : ((vCenter)
  128. ? TextAnchor.MiddleLeft
  129. : TextAnchor.LowerLeft);
  130. }
  131. else if(hCenter)
  132. {
  133. anchor = (vTop)
  134. ? TextAnchor.UpperCenter
  135. : ((vCenter)
  136. ? TextAnchor.MiddleCenter
  137. : TextAnchor.LowerCenter);
  138. }
  139. else if(hRight)
  140. {
  141. anchor = (vTop)
  142. ? TextAnchor.UpperRight
  143. : ((vCenter)
  144. ? TextAnchor.MiddleRight
  145. : TextAnchor.LowerRight);
  146. }
  147. if (prev != anchor)
  148. {
  149. prop.intValue = (int)anchor;
  150. prop.serializedObject.ApplyModifiedProperties();
  151. }
  152. }
  153. bool DrawAlignIcon(GUIContent contentActive, GUIContent contentInactive, TextAlignment align, bool value)
  154. {
  155. GUIStyle style = null;
  156. switch (align)
  157. {
  158. case TextAlignment.Left:
  159. style = Styles.alignmentButtonLeft;
  160. break;
  161. case TextAlignment.Center:
  162. style = Styles.alignmentButtonMid;
  163. break;
  164. case TextAlignment.Right:
  165. style = Styles.alignmentButtonRight;
  166. break;
  167. }
  168. if (value)
  169. {
  170. EditorGUI.BeginDisabledGroup(true);
  171. }
  172. bool result = GUILayout.Toggle(value,
  173. (value) ? contentActive : contentInactive,
  174. style, GUILayout.ExpandWidth(false));
  175. if (value)
  176. {
  177. EditorGUI.EndDisabledGroup();
  178. }
  179. return result;
  180. }
  181. private static class Styles
  182. {
  183. public static GUIStyle alignmentButtonLeft;
  184. public static GUIStyle alignmentButtonMid;
  185. public static GUIStyle alignmentButtonRight;
  186. public static GUIContent EncodingContent;
  187. public static GUIContent LeftAlign;
  188. public static GUIContent CenterAlign;
  189. public static GUIContent RightAlign;
  190. public static GUIContent TopAlign;
  191. public static GUIContent MiddleAlign;
  192. public static GUIContent BottomAlign;
  193. public static GUIContent LeftAlignActive;
  194. public static GUIContent CenterAlignActive;
  195. public static GUIContent RightAlignActive;
  196. public static GUIContent TopAlignActive;
  197. public static GUIContent MiddleAlignActive;
  198. public static GUIContent BottomAlignActive;
  199. static Styles()
  200. {
  201. alignmentButtonLeft = new GUIStyle(EditorStyles.miniButtonLeft);
  202. alignmentButtonMid = new GUIStyle(EditorStyles.miniButtonMid);
  203. alignmentButtonRight = new GUIStyle(EditorStyles.miniButtonRight);
  204. EncodingContent = new GUIContent("Rich Text", "Use emoticons and colors");
  205. LeftAlign = EditorGUIUtility.IconContent("GUISystem/align_horizontally_left", "Left Align");
  206. CenterAlign = EditorGUIUtility.IconContent("GUISystem/align_horizontally_center", "Center Align");
  207. RightAlign = EditorGUIUtility.IconContent("GUISystem/align_horizontally_right", "Right Align");
  208. LeftAlignActive = EditorGUIUtility.IconContent("GUISystem/align_horizontally_left_active", "Left Align");
  209. CenterAlignActive = EditorGUIUtility.IconContent("GUISystem/align_horizontally_center_active", "Center Align");
  210. RightAlignActive = EditorGUIUtility.IconContent("GUISystem/align_horizontally_right_active", "Right Align");
  211. TopAlign = EditorGUIUtility.IconContent("GUISystem/align_vertically_top", "Top Align");
  212. MiddleAlign = EditorGUIUtility.IconContent("GUISystem/align_vertically_center", "Middle Align");
  213. BottomAlign = EditorGUIUtility.IconContent("GUISystem/align_vertically_bottom", "Bottom Align");
  214. TopAlignActive = EditorGUIUtility.IconContent("GUISystem/align_vertically_top_active", "Top Align");
  215. MiddleAlignActive = EditorGUIUtility.IconContent("GUISystem/align_vertically_center_active", "Middle Align");
  216. BottomAlignActive = EditorGUIUtility.IconContent("GUISystem/align_vertically_bottom_active", "Bottom Align");
  217. FixAlignmentButtonStyles(new GUIStyle[]{ alignmentButtonLeft, alignmentButtonMid, alignmentButtonRight });
  218. }
  219. private static void FixAlignmentButtonStyles(params GUIStyle[] styles)
  220. {
  221. GUIStyle[] gUIStyleArray = styles;
  222. for (int i = 0; i < (int)gUIStyleArray.Length; i++)
  223. {
  224. GUIStyle gUIStyle = gUIStyleArray[i];
  225. gUIStyle.padding.left = 2;
  226. gUIStyle.padding.right = 2;
  227. }
  228. }
  229. }
  230. private enum VerticalTextAligment
  231. {
  232. Top,
  233. Middle,
  234. Bottom
  235. }
  236. }
  237. }