暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TMP_BaseEditorPanel.cs 64KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine.TextCore.LowLevel;
  6. namespace TMPro.EditorUtilities
  7. {
  8. public abstract class TMP_BaseEditorPanel : Editor
  9. {
  10. //Labels and Tooltips
  11. static readonly GUIContent k_RtlToggleLabel = new GUIContent("Enable RTL Editor", "Reverses text direction and allows right to left editing.");
  12. //static readonly GUIContent k_MainSettingsLabel = new GUIContent("Main Settings");
  13. static readonly GUIContent k_FontAssetLabel = new GUIContent("Font Asset", "The Font Asset containing the glyphs that can be rendered for this text.");
  14. static readonly GUIContent k_MaterialPresetLabel = new GUIContent("Material Preset", "The material used for rendering. Only materials created from the Font Asset can be used.");
  15. static readonly GUIContent k_StyleLabel = new GUIContent("Text Style", "The style from a style sheet to be applied to the text.");
  16. static readonly GUIContent k_AutoSizeLabel = new GUIContent("Auto Size", "Auto sizes the text to fit the available space.");
  17. static readonly GUIContent k_FontSizeLabel = new GUIContent("Font Size", "The size the text will be rendered at in points.");
  18. static readonly GUIContent k_AutoSizeOptionsLabel = new GUIContent("Auto Size Options");
  19. static readonly GUIContent k_MinLabel = new GUIContent("Min", "The minimum font size.");
  20. static readonly GUIContent k_MaxLabel = new GUIContent("Max", "The maximum font size.");
  21. static readonly GUIContent k_WdLabel = new GUIContent("WD%", "Compresses character width up to this value before reducing font size.");
  22. static readonly GUIContent k_LineLabel = new GUIContent("Line", "Negative value only. Compresses line height down to this value before reducing font size.");
  23. static readonly GUIContent k_FontStyleLabel = new GUIContent("Font Style", "Styles to apply to the text such as Bold or Italic.");
  24. static readonly GUIContent k_BoldLabel = new GUIContent("B", "Bold");
  25. static readonly GUIContent k_ItalicLabel = new GUIContent("I", "Italic");
  26. static readonly GUIContent k_UnderlineLabel = new GUIContent("U", "Underline");
  27. static readonly GUIContent k_StrikethroughLabel = new GUIContent("S", "Strikethrough");
  28. static readonly GUIContent k_LowercaseLabel = new GUIContent("ab", "Lowercase");
  29. static readonly GUIContent k_UppercaseLabel = new GUIContent("AB", "Uppercase");
  30. static readonly GUIContent k_SmallcapsLabel = new GUIContent("SC", "Smallcaps");
  31. static readonly GUIContent k_ColorModeLabel = new GUIContent("Color Mode", "The type of gradient to use.");
  32. static readonly GUIContent k_BaseColorLabel = new GUIContent("Vertex Color", "The base color of the text vertices.");
  33. static readonly GUIContent k_ColorPresetLabel = new GUIContent("Color Preset", "A Color Preset which override the local color settings.");
  34. static readonly GUIContent k_ColorGradientLabel = new GUIContent("Color Gradient", "The gradient color applied over the Vertex Color. Can be locally set or driven by a Gradient Asset.");
  35. static readonly GUIContent k_CorenerColorsLabel = new GUIContent("Colors", "The color composition of the gradient.");
  36. static readonly GUIContent k_OverrideTagsLabel = new GUIContent("Override Tags", "Whether the color settings override the <color> tag.");
  37. static readonly GUIContent k_SpacingOptionsLabel = new GUIContent("Spacing Options (em)", "Spacing adjustments between different elements of the text. Values are in font units where a value of 1 equals 1/100em.");
  38. static readonly GUIContent k_CharacterSpacingLabel = new GUIContent("Character");
  39. static readonly GUIContent k_WordSpacingLabel = new GUIContent("Word");
  40. static readonly GUIContent k_LineSpacingLabel = new GUIContent("Line");
  41. static readonly GUIContent k_ParagraphSpacingLabel = new GUIContent("Paragraph");
  42. static readonly GUIContent k_AlignmentLabel = new GUIContent("Alignment", "Horizontal and vertical alignment of the text within its container.");
  43. static readonly GUIContent k_WrapMixLabel = new GUIContent("Wrap Mix (W <-> C)", "How much to favor words versus characters when distributing the text.");
  44. static readonly GUIContent k_WrappingLabel = new GUIContent("Wrapping", "Wraps text to the next line when reaching the edge of the container.");
  45. static readonly GUIContent[] k_WrappingOptions = { new GUIContent("Disabled"), new GUIContent("Enabled") };
  46. static readonly GUIContent k_OverflowLabel = new GUIContent("Overflow", "How to display text which goes past the edge of the container.");
  47. static readonly GUIContent k_MarginsLabel = new GUIContent("Margins", "The space between the text and the edge of its container.");
  48. static readonly GUIContent k_GeometrySortingLabel = new GUIContent("Geometry Sorting", "The order in which text geometry is sorted. Used to adjust the way overlapping characters are displayed.");
  49. static readonly GUIContent k_IsTextObjectScaleStatic = new GUIContent("Is Scale Static", "Controls whether a text object will be excluded from the InteralUpdate callback to handle scale changes of the text object or its parent(s).");
  50. static readonly GUIContent k_RichTextLabel = new GUIContent("Rich Text", "Enables the use of rich text tags such as <color> and <font>.");
  51. static readonly GUIContent k_EscapeCharactersLabel = new GUIContent("Parse Escape Characters", "Whether to display strings such as \"\\n\" as is or replace them by the character they represent.");
  52. static readonly GUIContent k_VisibleDescenderLabel = new GUIContent("Visible Descender", "Compute descender values from visible characters only. Used to adjust layout behavior when hiding and revealing characters dynamically.");
  53. static readonly GUIContent k_EmojiFallbackSupportLabel = new GUIContent("Emoji Fallback Support", "When text contains Emojis, try using and displaying those from the potential Text Assets assigned in the TMP Settings Emoji Fallback Text Assets.");
  54. static readonly GUIContent k_SpriteAssetLabel = new GUIContent("Sprite Asset", "The Sprite Asset used when NOT specifically referencing one using <sprite=\"Sprite Asset Name\">.");
  55. static readonly GUIContent k_StyleSheetAssetLabel = new GUIContent("Style Sheet Asset", "The Style Sheet Asset used by this text object.");
  56. static readonly GUIContent k_HorizontalMappingLabel = new GUIContent("Horizontal Mapping", "Horizontal UV mapping when using a shader with a texture face option.");
  57. static readonly GUIContent k_VerticalMappingLabel = new GUIContent("Vertical Mapping", "Vertical UV mapping when using a shader with a texture face option.");
  58. static readonly GUIContent k_LineOffsetLabel = new GUIContent("Line Offset", "Adds an horizontal offset to each successive line. Used for slanted texturing.");
  59. static readonly GUIContent k_FontFeaturesLabel = new GUIContent("Font Features", "Font features available for the primary font asset assigned to the text component.");
  60. static readonly GUIContent k_PaddingLabel = new GUIContent("Extra Padding", "Adds some padding between the characters and the edge of the text mesh. Can reduce graphical errors when displaying small text.");
  61. static readonly GUIContent k_LeftLabel = new GUIContent("Left");
  62. static readonly GUIContent k_TopLabel = new GUIContent("Top");
  63. static readonly GUIContent k_RightLabel = new GUIContent("Right");
  64. static readonly GUIContent k_BottomLabel = new GUIContent("Bottom");
  65. protected static readonly GUIContent k_ExtraSettingsLabel = new GUIContent("Extra Settings");
  66. protected static string[] k_UiStateLabel = new string[] { "<i>(Click to collapse)</i> ", "<i>(Click to expand)</i> " };
  67. static Dictionary<int, TMP_Style> k_AvailableStyles = new Dictionary<int, TMP_Style>();
  68. protected Dictionary<int, int> m_TextStyleIndexLookup = new Dictionary<int, int>();
  69. protected struct Foldout
  70. {
  71. // Track Inspector foldout panel states, globally.
  72. public static bool extraSettings = false;
  73. public static bool materialInspector = true;
  74. }
  75. protected static int s_EventId;
  76. public int selAlignGridA;
  77. public int selAlignGridB;
  78. protected SerializedProperty m_TextProp;
  79. protected SerializedProperty m_IsRightToLeftProp;
  80. protected string m_RtlText;
  81. protected SerializedProperty m_FontAssetProp;
  82. protected SerializedProperty m_FontSharedMaterialProp;
  83. protected Material[] m_MaterialPresets;
  84. protected GUIContent[] m_MaterialPresetNames;
  85. protected Dictionary<int, int> m_MaterialPresetIndexLookup = new Dictionary<int, int>();
  86. protected int m_MaterialPresetSelectionIndex;
  87. protected bool m_IsPresetListDirty;
  88. protected List<TMP_Style> m_Styles = new List<TMP_Style>();
  89. protected GUIContent[] m_StyleNames;
  90. protected int m_StyleSelectionIndex;
  91. protected SerializedProperty m_FontStyleProp;
  92. protected SerializedProperty m_FontColorProp;
  93. protected SerializedProperty m_EnableVertexGradientProp;
  94. protected SerializedProperty m_FontColorGradientProp;
  95. protected SerializedProperty m_FontColorGradientPresetProp;
  96. protected SerializedProperty m_OverrideHtmlColorProp;
  97. protected SerializedProperty m_FontSizeProp;
  98. protected SerializedProperty m_FontSizeBaseProp;
  99. protected SerializedProperty m_AutoSizingProp;
  100. protected SerializedProperty m_FontSizeMinProp;
  101. protected SerializedProperty m_FontSizeMaxProp;
  102. protected SerializedProperty m_LineSpacingMaxProp;
  103. protected SerializedProperty m_CharWidthMaxAdjProp;
  104. protected SerializedProperty m_CharacterSpacingProp;
  105. protected SerializedProperty m_WordSpacingProp;
  106. protected SerializedProperty m_LineSpacingProp;
  107. protected SerializedProperty m_ParagraphSpacingProp;
  108. protected SerializedProperty m_TextAlignmentProp;
  109. protected SerializedProperty m_HorizontalAlignmentProp;
  110. protected SerializedProperty m_VerticalAlignmentProp;
  111. protected SerializedProperty m_HorizontalMappingProp;
  112. protected SerializedProperty m_VerticalMappingProp;
  113. protected SerializedProperty m_UvLineOffsetProp;
  114. protected SerializedProperty m_TextWrappingModeProp;
  115. protected SerializedProperty m_WordWrappingRatiosProp;
  116. protected SerializedProperty m_TextOverflowModeProp;
  117. protected SerializedProperty m_PageToDisplayProp;
  118. protected SerializedProperty m_LinkedTextComponentProp;
  119. protected SerializedProperty m_ParentLinkedTextComponentProp;
  120. protected SerializedProperty m_FontFeaturesActiveProp;
  121. protected SerializedProperty m_IsRichTextProp;
  122. protected SerializedProperty m_HasFontAssetChangedProp;
  123. protected SerializedProperty m_EnableExtraPaddingProp;
  124. protected SerializedProperty m_CheckPaddingRequiredProp;
  125. protected SerializedProperty m_EnableEscapeCharacterParsingProp;
  126. protected SerializedProperty m_UseMaxVisibleDescenderProp;
  127. protected SerializedProperty m_GeometrySortingOrderProp;
  128. protected SerializedProperty m_IsTextObjectScaleStaticProp;
  129. protected SerializedProperty m_EmojiFallbackSupportProp;
  130. protected SerializedProperty m_SpriteAssetProp;
  131. protected SerializedProperty m_StyleSheetAssetProp;
  132. protected SerializedProperty m_TextStyleHashCodeProp;
  133. protected SerializedProperty m_MarginProp;
  134. protected SerializedProperty m_ColorModeProp;
  135. protected bool m_HavePropertiesChanged;
  136. protected TMP_Text m_TextComponent;
  137. protected TMP_Text m_PreviousLinkedTextComponent;
  138. protected RectTransform m_RectTransform;
  139. protected Material m_TargetMaterial;
  140. protected Vector3[] m_RectCorners = new Vector3[4];
  141. protected Vector3[] m_HandlePoints = new Vector3[4];
  142. private static readonly string[] k_FontFeatures = new string[] { "kern", "liga", "mark", "mkmk" };
  143. protected virtual void OnEnable()
  144. {
  145. m_TextProp = serializedObject.FindProperty("m_text");
  146. m_IsRightToLeftProp = serializedObject.FindProperty("m_isRightToLeft");
  147. m_FontAssetProp = serializedObject.FindProperty("m_fontAsset");
  148. m_FontSharedMaterialProp = serializedObject.FindProperty("m_sharedMaterial");
  149. m_FontStyleProp = serializedObject.FindProperty("m_fontStyle");
  150. m_FontSizeProp = serializedObject.FindProperty("m_fontSize");
  151. m_FontSizeBaseProp = serializedObject.FindProperty("m_fontSizeBase");
  152. m_AutoSizingProp = serializedObject.FindProperty("m_enableAutoSizing");
  153. m_FontSizeMinProp = serializedObject.FindProperty("m_fontSizeMin");
  154. m_FontSizeMaxProp = serializedObject.FindProperty("m_fontSizeMax");
  155. m_LineSpacingMaxProp = serializedObject.FindProperty("m_lineSpacingMax");
  156. m_CharWidthMaxAdjProp = serializedObject.FindProperty("m_charWidthMaxAdj");
  157. // Colors & Gradient
  158. m_FontColorProp = serializedObject.FindProperty("m_fontColor");
  159. m_EnableVertexGradientProp = serializedObject.FindProperty("m_enableVertexGradient");
  160. m_FontColorGradientProp = serializedObject.FindProperty("m_fontColorGradient");
  161. m_FontColorGradientPresetProp = serializedObject.FindProperty("m_fontColorGradientPreset");
  162. m_OverrideHtmlColorProp = serializedObject.FindProperty("m_overrideHtmlColors");
  163. m_CharacterSpacingProp = serializedObject.FindProperty("m_characterSpacing");
  164. m_WordSpacingProp = serializedObject.FindProperty("m_wordSpacing");
  165. m_LineSpacingProp = serializedObject.FindProperty("m_lineSpacing");
  166. m_ParagraphSpacingProp = serializedObject.FindProperty("m_paragraphSpacing");
  167. m_TextAlignmentProp = serializedObject.FindProperty("m_textAlignment");
  168. m_HorizontalAlignmentProp = serializedObject.FindProperty("m_HorizontalAlignment");
  169. m_VerticalAlignmentProp = serializedObject.FindProperty("m_VerticalAlignment");
  170. m_HorizontalMappingProp = serializedObject.FindProperty("m_horizontalMapping");
  171. m_VerticalMappingProp = serializedObject.FindProperty("m_verticalMapping");
  172. m_UvLineOffsetProp = serializedObject.FindProperty("m_uvLineOffset");
  173. m_TextWrappingModeProp = serializedObject.FindProperty("m_TextWrappingMode");
  174. m_WordWrappingRatiosProp = serializedObject.FindProperty("m_wordWrappingRatios");
  175. m_TextOverflowModeProp = serializedObject.FindProperty("m_overflowMode");
  176. m_PageToDisplayProp = serializedObject.FindProperty("m_pageToDisplay");
  177. m_LinkedTextComponentProp = serializedObject.FindProperty("m_linkedTextComponent");
  178. m_ParentLinkedTextComponentProp = serializedObject.FindProperty("parentLinkedComponent");
  179. m_FontFeaturesActiveProp = serializedObject.FindProperty("m_ActiveFontFeatures");
  180. m_EnableExtraPaddingProp = serializedObject.FindProperty("m_enableExtraPadding");
  181. m_IsRichTextProp = serializedObject.FindProperty("m_isRichText");
  182. m_CheckPaddingRequiredProp = serializedObject.FindProperty("checkPaddingRequired");
  183. m_EnableEscapeCharacterParsingProp = serializedObject.FindProperty("m_parseCtrlCharacters");
  184. m_UseMaxVisibleDescenderProp = serializedObject.FindProperty("m_useMaxVisibleDescender");
  185. m_GeometrySortingOrderProp = serializedObject.FindProperty("m_geometrySortingOrder");
  186. m_IsTextObjectScaleStaticProp = serializedObject.FindProperty("m_IsTextObjectScaleStatic");
  187. m_EmojiFallbackSupportProp = serializedObject.FindProperty("m_EmojiFallbackSupport");
  188. m_SpriteAssetProp = serializedObject.FindProperty("m_spriteAsset");
  189. m_StyleSheetAssetProp = serializedObject.FindProperty("m_StyleSheet");
  190. m_TextStyleHashCodeProp = serializedObject.FindProperty("m_TextStyleHashCode");
  191. m_MarginProp = serializedObject.FindProperty("m_margin");
  192. m_HasFontAssetChangedProp = serializedObject.FindProperty("m_hasFontAssetChanged");
  193. m_ColorModeProp = serializedObject.FindProperty("m_colorMode");
  194. m_TextComponent = (TMP_Text)target;
  195. m_RectTransform = m_TextComponent.rectTransform;
  196. // Restore Previous Linked Text Component
  197. m_PreviousLinkedTextComponent = m_TextComponent.linkedTextComponent;
  198. // Create new Material Editor if one does not exists
  199. m_TargetMaterial = m_TextComponent.fontSharedMaterial;
  200. // Set material inspector visibility
  201. if (m_TargetMaterial != null)
  202. UnityEditorInternal.InternalEditorUtility.SetIsInspectorExpanded(m_TargetMaterial, Foldout.materialInspector);
  203. // Find all Material Presets matching the current Font Asset Material
  204. m_MaterialPresetNames = GetMaterialPresets();
  205. // Get Styles from Style Sheet
  206. if (TMP_Settings.instance != null)
  207. m_StyleNames = GetStyleNames();
  208. // Get list of font features for the primary font asset assigned to the text component
  209. // FontEngine.LoadFontFace(m_TextComponent.font.SourceFont_EditorRef);
  210. // OTL_Table gposTable = UnityEngine.TextCore.LowLevel.FontEngine.GetOpenTypeLayoutTable(OTL_TableType.GPOS);
  211. // OTL_Table gsubTable = UnityEngine.TextCore.LowLevel.FontEngine.GetOpenTypeLayoutTable(OTL_TableType.GSUB);
  212. //
  213. // HashSet<string> fontFeatures = new HashSet<string>();
  214. //
  215. // foreach (OTL_Feature feature in gposTable.features)
  216. // {
  217. // fontFeatures.Add(feature.tag);
  218. // }
  219. //
  220. // foreach (OTL_Feature feature in gsubTable.features)
  221. // {
  222. // fontFeatures.Add(feature.tag);
  223. // }
  224. //
  225. // m_FontFeatures = fontFeatures.OrderBy(item => item).ToArray();
  226. // Register to receive events when style sheets are modified.
  227. TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Add(ON_TEXT_STYLE_CHANGED);
  228. // Initialize the Event Listener for Undo Events.
  229. Undo.undoRedoPerformed += OnUndoRedo;
  230. }
  231. protected virtual void OnDisable()
  232. {
  233. // Set material inspector visibility
  234. if (m_TargetMaterial != null)
  235. Foldout.materialInspector = UnityEditorInternal.InternalEditorUtility.GetIsInspectorExpanded(m_TargetMaterial);
  236. if (Undo.undoRedoPerformed != null)
  237. Undo.undoRedoPerformed -= OnUndoRedo;
  238. // Unregister from style sheet related events.
  239. TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Remove(ON_TEXT_STYLE_CHANGED);
  240. }
  241. // Event received when Text Styles are changed.
  242. void ON_TEXT_STYLE_CHANGED(bool isChanged)
  243. {
  244. m_StyleNames = GetStyleNames();
  245. }
  246. public override void OnInspectorGUI()
  247. {
  248. // Make sure Multi selection only includes TMP Text objects.
  249. if (IsMixSelectionTypes()) return;
  250. serializedObject.Update();
  251. DrawTextInput();
  252. DrawMainSettings();
  253. DrawExtraSettings();
  254. EditorGUILayout.Space();
  255. if (serializedObject.ApplyModifiedProperties() || m_HavePropertiesChanged)
  256. {
  257. m_TextComponent.havePropertiesChanged = true;
  258. m_HavePropertiesChanged = false;
  259. }
  260. }
  261. public void OnSceneGUI()
  262. {
  263. if (IsMixSelectionTypes()) return;
  264. // Margin Frame & Handles
  265. m_RectTransform.GetWorldCorners(m_RectCorners);
  266. Vector4 marginOffset = m_TextComponent.margin;
  267. Vector3 lossyScale = m_RectTransform.lossyScale;
  268. m_HandlePoints[0] = m_RectCorners[0] + m_RectTransform.TransformDirection(new Vector3(marginOffset.x * lossyScale.x, marginOffset.w * lossyScale.y, 0));
  269. m_HandlePoints[1] = m_RectCorners[1] + m_RectTransform.TransformDirection(new Vector3(marginOffset.x * lossyScale.x, -marginOffset.y * lossyScale.y, 0));
  270. m_HandlePoints[2] = m_RectCorners[2] + m_RectTransform.TransformDirection(new Vector3(-marginOffset.z * lossyScale.x, -marginOffset.y * lossyScale.y, 0));
  271. m_HandlePoints[3] = m_RectCorners[3] + m_RectTransform.TransformDirection(new Vector3(-marginOffset.z * lossyScale.x, marginOffset.w * lossyScale.y, 0));
  272. Handles.DrawSolidRectangleWithOutline(m_HandlePoints, new Color32(255, 255, 255, 0), new Color32(255, 255, 0, 255));
  273. Matrix4x4 matrix = m_RectTransform.worldToLocalMatrix;
  274. // Draw & process FreeMoveHandles
  275. // LEFT HANDLE
  276. Vector3 oldLeft = (m_HandlePoints[0] + m_HandlePoints[1]) * 0.5f;
  277. #if UNITY_2022_1_OR_NEWER
  278. Vector3 newLeft = Handles.FreeMoveHandle(oldLeft, HandleUtility.GetHandleSize(m_RectTransform.position) * 0.05f, Vector3.zero, Handles.DotHandleCap);
  279. #else
  280. Vector3 newLeft = Handles.FreeMoveHandle(oldLeft, Quaternion.identity, HandleUtility.GetHandleSize(m_RectTransform.position) * 0.05f, Vector3.zero, Handles.DotHandleCap);
  281. #endif
  282. bool hasChanged = false;
  283. if (oldLeft != newLeft)
  284. {
  285. oldLeft = matrix.MultiplyPoint(oldLeft);
  286. newLeft = matrix.MultiplyPoint(newLeft);
  287. float delta = (oldLeft.x - newLeft.x) * lossyScale.x;
  288. marginOffset.x += -delta / lossyScale.x;
  289. //Debug.Log("Left Margin H0:" + handlePoints[0] + " H1:" + handlePoints[1]);
  290. hasChanged = true;
  291. }
  292. // TOP HANDLE
  293. Vector3 oldTop = (m_HandlePoints[1] + m_HandlePoints[2]) * 0.5f;
  294. #if UNITY_2022_1_OR_NEWER
  295. Vector3 newTop = Handles.FreeMoveHandle(oldTop, HandleUtility.GetHandleSize(m_RectTransform.position) * 0.05f, Vector3.zero, Handles.DotHandleCap);
  296. #else
  297. Vector3 newTop = Handles.FreeMoveHandle(oldTop, Quaternion.identity, HandleUtility.GetHandleSize(m_RectTransform.position) * 0.05f, Vector3.zero, Handles.DotHandleCap);
  298. #endif
  299. if (oldTop != newTop)
  300. {
  301. oldTop = matrix.MultiplyPoint(oldTop);
  302. newTop = matrix.MultiplyPoint(newTop);
  303. float delta = (oldTop.y - newTop.y) * lossyScale.y;
  304. marginOffset.y += delta / lossyScale.y;
  305. //Debug.Log("Top Margin H1:" + handlePoints[1] + " H2:" + handlePoints[2]);
  306. hasChanged = true;
  307. }
  308. // RIGHT HANDLE
  309. Vector3 oldRight = (m_HandlePoints[2] + m_HandlePoints[3]) * 0.5f;
  310. #if UNITY_2022_1_OR_NEWER
  311. Vector3 newRight = Handles.FreeMoveHandle(oldRight, HandleUtility.GetHandleSize(m_RectTransform.position) * 0.05f, Vector3.zero, Handles.DotHandleCap);
  312. #else
  313. Vector3 newRight = Handles.FreeMoveHandle(oldRight, Quaternion.identity, HandleUtility.GetHandleSize(m_RectTransform.position) * 0.05f, Vector3.zero, Handles.DotHandleCap);
  314. #endif
  315. if (oldRight != newRight)
  316. {
  317. oldRight = matrix.MultiplyPoint(oldRight);
  318. newRight = matrix.MultiplyPoint(newRight);
  319. float delta = (oldRight.x - newRight.x) * lossyScale.x;
  320. marginOffset.z += delta / lossyScale.x;
  321. hasChanged = true;
  322. //Debug.Log("Right Margin H2:" + handlePoints[2] + " H3:" + handlePoints[3]);
  323. }
  324. // BOTTOM HANDLE
  325. Vector3 oldBottom = (m_HandlePoints[3] + m_HandlePoints[0]) * 0.5f;
  326. #if UNITY_2022_1_OR_NEWER
  327. Vector3 newBottom = Handles.FreeMoveHandle(oldBottom, HandleUtility.GetHandleSize(m_RectTransform.position) * 0.05f, Vector3.zero, Handles.DotHandleCap);
  328. #else
  329. Vector3 newBottom = Handles.FreeMoveHandle(oldBottom, Quaternion.identity, HandleUtility.GetHandleSize(m_RectTransform.position) * 0.05f, Vector3.zero, Handles.DotHandleCap);
  330. #endif
  331. if (oldBottom != newBottom)
  332. {
  333. oldBottom = matrix.MultiplyPoint(oldBottom);
  334. newBottom = matrix.MultiplyPoint(newBottom);
  335. float delta = (oldBottom.y - newBottom.y) * lossyScale.y;
  336. marginOffset.w += -delta / lossyScale.y;
  337. hasChanged = true;
  338. //Debug.Log("Bottom Margin H0:" + handlePoints[0] + " H3:" + handlePoints[3]);
  339. }
  340. if (hasChanged)
  341. {
  342. Undo.RecordObjects(new Object[] {m_RectTransform, m_TextComponent }, "Margin Changes");
  343. m_TextComponent.margin = marginOffset;
  344. EditorUtility.SetDirty(target);
  345. }
  346. }
  347. protected void DrawTextInput()
  348. {
  349. EditorGUILayout.Space();
  350. Rect rect = EditorGUILayout.GetControlRect(false, 22);
  351. GUI.Label(rect, new GUIContent("<b>Text Input</b>"), TMP_UIStyleManager.sectionHeader);
  352. EditorGUI.indentLevel = 0;
  353. // If the text component is linked, disable the text input box.
  354. if (m_ParentLinkedTextComponentProp.objectReferenceValue != null)
  355. {
  356. EditorGUILayout.HelpBox("The Text Input Box is disabled due to this text component being linked to another.", MessageType.Info);
  357. }
  358. else
  359. {
  360. // Display RTL Toggle
  361. float labelWidth = EditorGUIUtility.labelWidth;
  362. EditorGUIUtility.labelWidth = 110f;
  363. m_IsRightToLeftProp.boolValue = EditorGUI.Toggle(new Rect(rect.width - 120, rect.y + 3, 130, 20), k_RtlToggleLabel, m_IsRightToLeftProp.boolValue);
  364. EditorGUIUtility.labelWidth = labelWidth;
  365. EditorGUI.BeginChangeCheck();
  366. EditorGUILayout.PropertyField(m_TextProp, GUIContent.none);
  367. // Need to also compare string content due to issue related to scroll bar drag handle
  368. if (EditorGUI.EndChangeCheck() && m_TextProp.stringValue != m_TextComponent.text)
  369. {
  370. m_TextComponent.m_inputSource = TMP_Text.TextInputSources.TextInputBox;
  371. m_HavePropertiesChanged = true;
  372. }
  373. if (m_IsRightToLeftProp.boolValue)
  374. {
  375. // Copy source text to RTL string
  376. m_RtlText = string.Empty;
  377. string sourceText = m_TextProp.stringValue;
  378. // Reverse Text displayed in Text Input Box
  379. for (int i = 0; i < sourceText.Length; i++)
  380. m_RtlText += sourceText[sourceText.Length - i - 1];
  381. GUILayout.Label("RTL Text Input");
  382. EditorGUI.BeginChangeCheck();
  383. m_RtlText = EditorGUILayout.TextArea(m_RtlText, TMP_UIStyleManager.wrappingTextArea, GUILayout.Height(EditorGUI.GetPropertyHeight(m_TextProp) - EditorGUIUtility.singleLineHeight), GUILayout.ExpandWidth(true));
  384. if (EditorGUI.EndChangeCheck())
  385. {
  386. // Convert RTL input
  387. sourceText = string.Empty;
  388. // Reverse Text displayed in Text Input Box
  389. for (int i = 0; i < m_RtlText.Length; i++)
  390. sourceText += m_RtlText[m_RtlText.Length - i - 1];
  391. m_TextProp.stringValue = sourceText;
  392. }
  393. }
  394. // TEXT STYLE
  395. if (m_StyleNames != null)
  396. {
  397. rect = EditorGUILayout.GetControlRect(false, 17);
  398. EditorGUI.BeginProperty(rect, k_StyleLabel, m_TextStyleHashCodeProp);
  399. m_TextStyleIndexLookup.TryGetValue(m_TextStyleHashCodeProp.intValue, out m_StyleSelectionIndex);
  400. EditorGUI.BeginChangeCheck();
  401. m_StyleSelectionIndex = EditorGUI.Popup(rect, k_StyleLabel, m_StyleSelectionIndex, m_StyleNames);
  402. if (EditorGUI.EndChangeCheck())
  403. {
  404. m_TextStyleHashCodeProp.intValue = m_Styles[m_StyleSelectionIndex].hashCode;
  405. m_TextComponent.m_TextStyle = m_Styles[m_StyleSelectionIndex];
  406. m_HavePropertiesChanged = true;
  407. }
  408. EditorGUI.EndProperty();
  409. }
  410. }
  411. }
  412. protected void DrawMainSettings()
  413. {
  414. // MAIN SETTINGS SECTION
  415. GUILayout.Label(new GUIContent("<b>Main Settings</b>"), TMP_UIStyleManager.sectionHeader);
  416. //EditorGUI.indentLevel += 1;
  417. DrawFont();
  418. DrawColor();
  419. DrawSpacing();
  420. DrawAlignment();
  421. DrawWrappingOverflow();
  422. DrawTextureMapping();
  423. //EditorGUI.indentLevel -= 1;
  424. }
  425. void DrawFont()
  426. {
  427. bool isFontAssetDirty = false;
  428. // FONT ASSET
  429. EditorGUI.BeginChangeCheck();
  430. EditorGUILayout.PropertyField(m_FontAssetProp, k_FontAssetLabel);
  431. if (EditorGUI.EndChangeCheck())
  432. {
  433. m_HavePropertiesChanged = true;
  434. m_HasFontAssetChangedProp.boolValue = true;
  435. // Get new Material Presets for the new font asset
  436. m_MaterialPresetNames = GetMaterialPresets();
  437. m_MaterialPresetSelectionIndex = 0;
  438. isFontAssetDirty = true;
  439. }
  440. Rect rect;
  441. // MATERIAL PRESET
  442. if (m_MaterialPresetNames != null && !isFontAssetDirty )
  443. {
  444. EditorGUI.BeginChangeCheck();
  445. rect = EditorGUILayout.GetControlRect(false, 17);
  446. EditorGUI.BeginProperty(rect, k_MaterialPresetLabel, m_FontSharedMaterialProp);
  447. float oldHeight = EditorStyles.popup.fixedHeight;
  448. EditorStyles.popup.fixedHeight = rect.height;
  449. int oldSize = EditorStyles.popup.fontSize;
  450. EditorStyles.popup.fontSize = 11;
  451. if (m_FontSharedMaterialProp.objectReferenceValue != null)
  452. m_MaterialPresetIndexLookup.TryGetValue(m_FontSharedMaterialProp.objectReferenceValue.GetInstanceID(), out m_MaterialPresetSelectionIndex);
  453. m_MaterialPresetSelectionIndex = EditorGUI.Popup(rect, k_MaterialPresetLabel, m_MaterialPresetSelectionIndex, m_MaterialPresetNames);
  454. EditorGUI.EndProperty();
  455. if (EditorGUI.EndChangeCheck())
  456. {
  457. m_FontSharedMaterialProp.objectReferenceValue = m_MaterialPresets[m_MaterialPresetSelectionIndex];
  458. m_HavePropertiesChanged = true;
  459. }
  460. EditorStyles.popup.fixedHeight = oldHeight;
  461. EditorStyles.popup.fontSize = oldSize;
  462. }
  463. // FONT STYLE
  464. EditorGUI.BeginChangeCheck();
  465. int v1, v2, v3, v4, v5, v6, v7;
  466. if (EditorGUIUtility.wideMode)
  467. {
  468. rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight + 2f);
  469. EditorGUI.BeginProperty(rect, k_FontStyleLabel, m_FontStyleProp);
  470. EditorGUI.PrefixLabel(rect, k_FontStyleLabel);
  471. int styleValue = m_FontStyleProp.intValue;
  472. rect.x += EditorGUIUtility.labelWidth;
  473. rect.width -= EditorGUIUtility.labelWidth;
  474. rect.width = Mathf.Max(25f, rect.width / 7f);
  475. v1 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 1) == 1, k_BoldLabel, TMP_UIStyleManager.alignmentButtonLeft) ? 1 : 0; // Bold
  476. rect.x += rect.width;
  477. v2 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 2) == 2, k_ItalicLabel, TMP_UIStyleManager.alignmentButtonMid) ? 2 : 0; // Italics
  478. rect.x += rect.width;
  479. v3 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 4) == 4, k_UnderlineLabel, TMP_UIStyleManager.alignmentButtonMid) ? 4 : 0; // Underline
  480. rect.x += rect.width;
  481. v7 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 64) == 64, k_StrikethroughLabel, TMP_UIStyleManager.alignmentButtonRight) ? 64 : 0; // Strikethrough
  482. rect.x += rect.width;
  483. int selected = 0;
  484. EditorGUI.BeginChangeCheck();
  485. v4 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 8) == 8, k_LowercaseLabel, TMP_UIStyleManager.alignmentButtonLeft) ? 8 : 0; // Lowercase
  486. if (EditorGUI.EndChangeCheck() && v4 > 0)
  487. {
  488. selected = v4;
  489. }
  490. rect.x += rect.width;
  491. EditorGUI.BeginChangeCheck();
  492. v5 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 16) == 16, k_UppercaseLabel, TMP_UIStyleManager.alignmentButtonMid) ? 16 : 0; // Uppercase
  493. if (EditorGUI.EndChangeCheck() && v5 > 0)
  494. {
  495. selected = v5;
  496. }
  497. rect.x += rect.width;
  498. EditorGUI.BeginChangeCheck();
  499. v6 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 32) == 32, k_SmallcapsLabel, TMP_UIStyleManager.alignmentButtonRight) ? 32 : 0; // Smallcaps
  500. if (EditorGUI.EndChangeCheck() && v6 > 0)
  501. {
  502. selected = v6;
  503. }
  504. if (selected > 0)
  505. {
  506. v4 = selected == 8 ? 8 : 0;
  507. v5 = selected == 16 ? 16 : 0;
  508. v6 = selected == 32 ? 32 : 0;
  509. }
  510. EditorGUI.EndProperty();
  511. }
  512. else
  513. {
  514. rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight + 2f);
  515. EditorGUI.BeginProperty(rect, k_FontStyleLabel, m_FontStyleProp);
  516. EditorGUI.PrefixLabel(rect, k_FontStyleLabel);
  517. int styleValue = m_FontStyleProp.intValue;
  518. rect.x += EditorGUIUtility.labelWidth;
  519. rect.width -= EditorGUIUtility.labelWidth;
  520. rect.width = Mathf.Max(25f, rect.width / 4f);
  521. v1 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 1) == 1, k_BoldLabel, TMP_UIStyleManager.alignmentButtonLeft) ? 1 : 0; // Bold
  522. rect.x += rect.width;
  523. v2 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 2) == 2, k_ItalicLabel, TMP_UIStyleManager.alignmentButtonMid) ? 2 : 0; // Italics
  524. rect.x += rect.width;
  525. v3 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 4) == 4, k_UnderlineLabel, TMP_UIStyleManager.alignmentButtonMid) ? 4 : 0; // Underline
  526. rect.x += rect.width;
  527. v7 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 64) == 64, k_StrikethroughLabel, TMP_UIStyleManager.alignmentButtonRight) ? 64 : 0; // Strikethrough
  528. rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight + 2f);
  529. rect.x += EditorGUIUtility.labelWidth;
  530. rect.width -= EditorGUIUtility.labelWidth;
  531. rect.width = Mathf.Max(25f, rect.width / 4f);
  532. int selected = 0;
  533. EditorGUI.BeginChangeCheck();
  534. v4 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 8) == 8, k_LowercaseLabel, TMP_UIStyleManager.alignmentButtonLeft) ? 8 : 0; // Lowercase
  535. if (EditorGUI.EndChangeCheck() && v4 > 0)
  536. {
  537. selected = v4;
  538. }
  539. rect.x += rect.width;
  540. EditorGUI.BeginChangeCheck();
  541. v5 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 16) == 16, k_UppercaseLabel, TMP_UIStyleManager.alignmentButtonMid) ? 16 : 0; // Uppercase
  542. if (EditorGUI.EndChangeCheck() && v5 > 0)
  543. {
  544. selected = v5;
  545. }
  546. rect.x += rect.width;
  547. EditorGUI.BeginChangeCheck();
  548. v6 = TMP_EditorUtility.EditorToggle(rect, (styleValue & 32) == 32, k_SmallcapsLabel, TMP_UIStyleManager.alignmentButtonRight) ? 32 : 0; // Smallcaps
  549. if (EditorGUI.EndChangeCheck() && v6 > 0)
  550. {
  551. selected = v6;
  552. }
  553. if (selected > 0)
  554. {
  555. v4 = selected == 8 ? 8 : 0;
  556. v5 = selected == 16 ? 16 : 0;
  557. v6 = selected == 32 ? 32 : 0;
  558. }
  559. EditorGUI.EndProperty();
  560. }
  561. if (EditorGUI.EndChangeCheck())
  562. {
  563. m_FontStyleProp.intValue = v1 + v2 + v3 + v4 + v5 + v6 + v7;
  564. m_HavePropertiesChanged = true;
  565. }
  566. // FONT SIZE
  567. EditorGUI.BeginChangeCheck();
  568. EditorGUI.BeginDisabledGroup(m_AutoSizingProp.boolValue);
  569. EditorGUILayout.PropertyField(m_FontSizeProp, k_FontSizeLabel, GUILayout.MaxWidth(EditorGUIUtility.labelWidth + 50f));
  570. EditorGUI.EndDisabledGroup();
  571. if (EditorGUI.EndChangeCheck())
  572. {
  573. float fontSize = Mathf.Clamp(m_FontSizeProp.floatValue, 0, 32767);
  574. m_FontSizeProp.floatValue = fontSize;
  575. m_FontSizeBaseProp.floatValue = fontSize;
  576. m_HavePropertiesChanged = true;
  577. }
  578. EditorGUI.indentLevel += 1;
  579. EditorGUI.BeginChangeCheck();
  580. EditorGUILayout.PropertyField(m_AutoSizingProp, k_AutoSizeLabel);
  581. if (EditorGUI.EndChangeCheck())
  582. {
  583. if (m_AutoSizingProp.boolValue == false)
  584. m_FontSizeProp.floatValue = m_FontSizeBaseProp.floatValue;
  585. m_HavePropertiesChanged = true;
  586. }
  587. // Show auto sizing options
  588. if (m_AutoSizingProp.boolValue)
  589. {
  590. rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
  591. EditorGUI.PrefixLabel(rect, k_AutoSizeOptionsLabel);
  592. int previousIndent = EditorGUI.indentLevel;
  593. EditorGUI.indentLevel = 0;
  594. rect.width = (rect.width - EditorGUIUtility.labelWidth) / 4f;
  595. rect.x += EditorGUIUtility.labelWidth;
  596. EditorGUIUtility.labelWidth = 24;
  597. EditorGUI.BeginChangeCheck();
  598. EditorGUI.PropertyField(rect, m_FontSizeMinProp, k_MinLabel);
  599. if (EditorGUI.EndChangeCheck())
  600. {
  601. float minSize = m_FontSizeMinProp.floatValue;
  602. minSize = Mathf.Max(0, minSize);
  603. m_FontSizeMinProp.floatValue = Mathf.Min(minSize, m_FontSizeMaxProp.floatValue);
  604. m_HavePropertiesChanged = true;
  605. }
  606. rect.x += rect.width;
  607. EditorGUIUtility.labelWidth = 27;
  608. EditorGUI.BeginChangeCheck();
  609. EditorGUI.PropertyField(rect, m_FontSizeMaxProp, k_MaxLabel);
  610. if (EditorGUI.EndChangeCheck())
  611. {
  612. float maxSize = Mathf.Clamp(m_FontSizeMaxProp.floatValue, 0, 32767);
  613. m_FontSizeMaxProp.floatValue = Mathf.Max(m_FontSizeMinProp.floatValue, maxSize);
  614. m_HavePropertiesChanged = true;
  615. }
  616. rect.x += rect.width;
  617. EditorGUI.BeginChangeCheck();
  618. EditorGUIUtility.labelWidth = 36;
  619. EditorGUI.PropertyField(rect, m_CharWidthMaxAdjProp, k_WdLabel);
  620. rect.x += rect.width;
  621. EditorGUIUtility.labelWidth = 28;
  622. EditorGUI.PropertyField(rect, m_LineSpacingMaxProp, k_LineLabel);
  623. EditorGUIUtility.labelWidth = 0;
  624. if (EditorGUI.EndChangeCheck())
  625. {
  626. m_CharWidthMaxAdjProp.floatValue = Mathf.Clamp(m_CharWidthMaxAdjProp.floatValue, 0, 50);
  627. m_LineSpacingMaxProp.floatValue = Mathf.Min(0, m_LineSpacingMaxProp.floatValue);
  628. m_HavePropertiesChanged = true;
  629. }
  630. EditorGUI.indentLevel = previousIndent;
  631. }
  632. EditorGUI.indentLevel -= 1;
  633. EditorGUILayout.Space();
  634. }
  635. void DrawColor()
  636. {
  637. // FACE VERTEX COLOR
  638. EditorGUI.BeginChangeCheck();
  639. EditorGUILayout.PropertyField(m_FontColorProp, k_BaseColorLabel);
  640. if (EditorGUI.EndChangeCheck())
  641. {
  642. m_HavePropertiesChanged = true;
  643. }
  644. EditorGUI.BeginChangeCheck();
  645. EditorGUILayout.PropertyField(m_EnableVertexGradientProp, k_ColorGradientLabel);
  646. if (EditorGUI.EndChangeCheck())
  647. {
  648. m_HavePropertiesChanged = true;
  649. }
  650. EditorGUIUtility.fieldWidth = 0;
  651. if (m_EnableVertexGradientProp.boolValue)
  652. {
  653. EditorGUI.indentLevel += 1;
  654. EditorGUI.BeginChangeCheck();
  655. EditorGUILayout.PropertyField(m_FontColorGradientPresetProp, k_ColorPresetLabel);
  656. SerializedObject obj = null;
  657. SerializedProperty colorMode;
  658. SerializedProperty topLeft;
  659. SerializedProperty topRight;
  660. SerializedProperty bottomLeft;
  661. SerializedProperty bottomRight;
  662. if (m_FontColorGradientPresetProp.objectReferenceValue == null)
  663. {
  664. colorMode = m_ColorModeProp;
  665. topLeft = m_FontColorGradientProp.FindPropertyRelative("topLeft");
  666. topRight = m_FontColorGradientProp.FindPropertyRelative("topRight");
  667. bottomLeft = m_FontColorGradientProp.FindPropertyRelative("bottomLeft");
  668. bottomRight = m_FontColorGradientProp.FindPropertyRelative("bottomRight");
  669. }
  670. else
  671. {
  672. obj = new SerializedObject(m_FontColorGradientPresetProp.objectReferenceValue);
  673. colorMode = obj.FindProperty("colorMode");
  674. topLeft = obj.FindProperty("topLeft");
  675. topRight = obj.FindProperty("topRight");
  676. bottomLeft = obj.FindProperty("bottomLeft");
  677. bottomRight = obj.FindProperty("bottomRight");
  678. }
  679. EditorGUILayout.PropertyField(colorMode, k_ColorModeLabel);
  680. Rect rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 1 : 2));
  681. EditorGUI.PrefixLabel(rect, k_CorenerColorsLabel);
  682. rect.x += EditorGUIUtility.labelWidth;
  683. rect.width = rect.width - EditorGUIUtility.labelWidth;
  684. switch ((ColorMode)colorMode.enumValueIndex)
  685. {
  686. case ColorMode.Single:
  687. TMP_EditorUtility.DrawColorProperty(rect, topLeft);
  688. topRight.colorValue = topLeft.colorValue;
  689. bottomLeft.colorValue = topLeft.colorValue;
  690. bottomRight.colorValue = topLeft.colorValue;
  691. break;
  692. case ColorMode.HorizontalGradient:
  693. rect.width /= 2f;
  694. TMP_EditorUtility.DrawColorProperty(rect, topLeft);
  695. rect.x += rect.width;
  696. TMP_EditorUtility.DrawColorProperty(rect, topRight);
  697. bottomLeft.colorValue = topLeft.colorValue;
  698. bottomRight.colorValue = topRight.colorValue;
  699. break;
  700. case ColorMode.VerticalGradient:
  701. TMP_EditorUtility.DrawColorProperty(rect, topLeft);
  702. rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 1 : 2));
  703. rect.x += EditorGUIUtility.labelWidth;
  704. TMP_EditorUtility.DrawColorProperty(rect, bottomLeft);
  705. topRight.colorValue = topLeft.colorValue;
  706. bottomRight.colorValue = bottomLeft.colorValue;
  707. break;
  708. case ColorMode.FourCornersGradient:
  709. rect.width /= 2f;
  710. TMP_EditorUtility.DrawColorProperty(rect, topLeft);
  711. rect.x += rect.width;
  712. TMP_EditorUtility.DrawColorProperty(rect, topRight);
  713. rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 1 : 2));
  714. rect.x += EditorGUIUtility.labelWidth;
  715. rect.width = (rect.width - EditorGUIUtility.labelWidth) / 2f;
  716. TMP_EditorUtility.DrawColorProperty(rect, bottomLeft);
  717. rect.x += rect.width;
  718. TMP_EditorUtility.DrawColorProperty(rect, bottomRight);
  719. break;
  720. }
  721. if (EditorGUI.EndChangeCheck())
  722. {
  723. m_HavePropertiesChanged = true;
  724. if (obj != null)
  725. {
  726. obj.ApplyModifiedProperties();
  727. TMPro_EventManager.ON_COLOR_GRADIENT_PROPERTY_CHANGED(m_FontColorGradientPresetProp.objectReferenceValue as TMP_ColorGradient);
  728. }
  729. }
  730. EditorGUI.indentLevel -= 1;
  731. }
  732. EditorGUILayout.PropertyField(m_OverrideHtmlColorProp, k_OverrideTagsLabel);
  733. EditorGUILayout.Space();
  734. }
  735. void DrawSpacing()
  736. {
  737. // CHARACTER, LINE & PARAGRAPH SPACING
  738. EditorGUI.BeginChangeCheck();
  739. Rect rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
  740. EditorGUI.PrefixLabel(rect, k_SpacingOptionsLabel);
  741. int oldIndent = EditorGUI.indentLevel;
  742. EditorGUI.indentLevel = 0;
  743. float currentLabelWidth = EditorGUIUtility.labelWidth;
  744. rect.x += currentLabelWidth;
  745. rect.width = (rect.width - currentLabelWidth - 3f) / 2f;
  746. EditorGUIUtility.labelWidth = Mathf.Min(rect.width * 0.55f, 80f);
  747. EditorGUI.PropertyField(rect, m_CharacterSpacingProp, k_CharacterSpacingLabel);
  748. rect.x += rect.width + 3f;
  749. EditorGUI.PropertyField(rect, m_WordSpacingProp, k_WordSpacingLabel);
  750. rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
  751. rect.x += currentLabelWidth;
  752. rect.width = (rect.width - currentLabelWidth -3f) / 2f;
  753. EditorGUIUtility.labelWidth = Mathf.Min(rect.width * 0.55f, 80f);
  754. EditorGUI.PropertyField(rect, m_LineSpacingProp, k_LineSpacingLabel);
  755. rect.x += rect.width + 3f;
  756. EditorGUI.PropertyField(rect, m_ParagraphSpacingProp, k_ParagraphSpacingLabel);
  757. EditorGUIUtility.labelWidth = currentLabelWidth;
  758. EditorGUI.indentLevel = oldIndent;
  759. if (EditorGUI.EndChangeCheck())
  760. {
  761. m_HavePropertiesChanged = true;
  762. }
  763. EditorGUILayout.Space();
  764. }
  765. void DrawAlignment()
  766. {
  767. // TEXT ALIGNMENT
  768. EditorGUI.BeginChangeCheck();
  769. Rect rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.currentViewWidth > 504 ? 20 : 40 + 3);
  770. EditorGUI.BeginProperty(rect, k_AlignmentLabel, m_HorizontalAlignmentProp);
  771. EditorGUI.BeginProperty(rect, k_AlignmentLabel, m_VerticalAlignmentProp);
  772. EditorGUI.PrefixLabel(rect, k_AlignmentLabel);
  773. rect.x += EditorGUIUtility.labelWidth;
  774. EditorGUI.PropertyField(rect, m_HorizontalAlignmentProp, GUIContent.none);
  775. EditorGUI.PropertyField(rect, m_VerticalAlignmentProp, GUIContent.none);
  776. // WRAPPING RATIOS shown if Justified mode is selected.
  777. if (((HorizontalAlignmentOptions)m_HorizontalAlignmentProp.intValue & HorizontalAlignmentOptions.Justified) == HorizontalAlignmentOptions.Justified || ((HorizontalAlignmentOptions)m_HorizontalAlignmentProp.intValue & HorizontalAlignmentOptions.Flush) == HorizontalAlignmentOptions.Flush)
  778. DrawPropertySlider(k_WrapMixLabel, m_WordWrappingRatiosProp);
  779. if (EditorGUI.EndChangeCheck())
  780. m_HavePropertiesChanged = true;
  781. EditorGUI.EndProperty();
  782. EditorGUI.EndProperty();
  783. EditorGUILayout.Space();
  784. }
  785. void DrawWrappingOverflow()
  786. {
  787. // TEXT WRAPPING
  788. Rect rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
  789. EditorGUI.BeginProperty(rect, k_WrappingLabel, m_TextWrappingModeProp);
  790. EditorGUI.BeginChangeCheck();
  791. EditorGUI.PropertyField(rect, m_TextWrappingModeProp);
  792. if (EditorGUI.EndChangeCheck())
  793. {
  794. m_HavePropertiesChanged = true;
  795. }
  796. EditorGUI.EndProperty();
  797. // TEXT OVERFLOW
  798. EditorGUI.BeginChangeCheck();
  799. if ((TextOverflowModes)m_TextOverflowModeProp.enumValueIndex == TextOverflowModes.Linked)
  800. {
  801. EditorGUILayout.BeginHorizontal();
  802. float fieldWidth = EditorGUIUtility.fieldWidth;
  803. EditorGUIUtility.fieldWidth = 65;
  804. EditorGUILayout.PropertyField(m_TextOverflowModeProp, k_OverflowLabel);
  805. EditorGUIUtility.fieldWidth = fieldWidth;
  806. EditorGUILayout.PropertyField(m_LinkedTextComponentProp, GUIContent.none);
  807. EditorGUILayout.EndHorizontal();
  808. if (GUI.changed)
  809. {
  810. TMP_Text linkedComponent = m_LinkedTextComponentProp.objectReferenceValue as TMP_Text;
  811. if (linkedComponent == null)
  812. {
  813. m_LinkedTextComponentProp.objectReferenceValue = null;
  814. if (m_PreviousLinkedTextComponent != null)
  815. m_TextComponent.ReleaseLinkedTextComponent(m_PreviousLinkedTextComponent);
  816. }
  817. else if (m_TextComponent.IsSelfOrLinkedAncestor(linkedComponent))
  818. {
  819. m_LinkedTextComponentProp.objectReferenceValue = m_PreviousLinkedTextComponent;
  820. }
  821. else
  822. {
  823. if (m_PreviousLinkedTextComponent != null)
  824. m_TextComponent.ReleaseLinkedTextComponent(m_PreviousLinkedTextComponent);
  825. m_LinkedTextComponentProp.objectReferenceValue = linkedComponent;
  826. linkedComponent.parentLinkedComponent = m_TextComponent;
  827. m_PreviousLinkedTextComponent = linkedComponent;
  828. }
  829. }
  830. }
  831. else if ((TextOverflowModes)m_TextOverflowModeProp.enumValueIndex == TextOverflowModes.Page)
  832. {
  833. EditorGUILayout.BeginHorizontal();
  834. EditorGUILayout.PropertyField(m_TextOverflowModeProp, k_OverflowLabel);
  835. EditorGUILayout.PropertyField(m_PageToDisplayProp, GUIContent.none);
  836. EditorGUILayout.EndHorizontal();
  837. if (m_PreviousLinkedTextComponent)
  838. {
  839. m_TextComponent.ReleaseLinkedTextComponent(m_PreviousLinkedTextComponent);
  840. m_TextComponent.linkedTextComponent = null;
  841. }
  842. }
  843. else
  844. {
  845. EditorGUILayout.PropertyField(m_TextOverflowModeProp, k_OverflowLabel);
  846. if (m_PreviousLinkedTextComponent)
  847. {
  848. m_TextComponent.ReleaseLinkedTextComponent(m_PreviousLinkedTextComponent);
  849. m_TextComponent.linkedTextComponent = null;
  850. }
  851. }
  852. if (EditorGUI.EndChangeCheck())
  853. {
  854. m_HavePropertiesChanged = true;
  855. }
  856. EditorGUILayout.Space();
  857. }
  858. protected abstract void DrawExtraSettings();
  859. protected void DrawMargins()
  860. {
  861. EditorGUI.BeginChangeCheck();
  862. DrawMarginProperty(m_MarginProp, k_MarginsLabel);
  863. if (EditorGUI.EndChangeCheck())
  864. {
  865. // Value range check on margins to make sure they are not excessive.
  866. Vector4 margins = m_MarginProp.vector4Value;
  867. Rect textContainerSize = m_RectTransform.rect;
  868. margins.x = Mathf.Clamp(margins.x, -textContainerSize.width, textContainerSize.width);
  869. margins.z = Mathf.Clamp(margins.z, -textContainerSize.width, textContainerSize.width);
  870. margins.y = Mathf.Clamp(margins.y, -textContainerSize.height, textContainerSize.height);
  871. margins.w = Mathf.Clamp(margins.w, -textContainerSize.height, textContainerSize.height);
  872. m_MarginProp.vector4Value = margins;
  873. m_HavePropertiesChanged = true;
  874. }
  875. EditorGUILayout.Space();
  876. }
  877. protected void DrawGeometrySorting()
  878. {
  879. EditorGUI.BeginChangeCheck();
  880. EditorGUILayout.PropertyField(m_GeometrySortingOrderProp, k_GeometrySortingLabel);
  881. if (EditorGUI.EndChangeCheck())
  882. m_HavePropertiesChanged = true;
  883. EditorGUILayout.Space();
  884. }
  885. protected void DrawIsTextObjectScaleStatic()
  886. {
  887. EditorGUI.BeginChangeCheck();
  888. EditorGUILayout.PropertyField(m_IsTextObjectScaleStaticProp, k_IsTextObjectScaleStatic);
  889. if (EditorGUI.EndChangeCheck())
  890. {
  891. m_TextComponent.isTextObjectScaleStatic = m_IsTextObjectScaleStaticProp.boolValue;
  892. m_HavePropertiesChanged = true;
  893. }
  894. EditorGUILayout.Space();
  895. }
  896. protected void DrawRichText()
  897. {
  898. EditorGUI.BeginChangeCheck();
  899. EditorGUILayout.PropertyField(m_IsRichTextProp, k_RichTextLabel);
  900. if (EditorGUI.EndChangeCheck())
  901. m_HavePropertiesChanged = true;
  902. }
  903. protected void DrawParsing()
  904. {
  905. EditorGUI.BeginChangeCheck();
  906. EditorGUILayout.PropertyField(m_EnableEscapeCharacterParsingProp, k_EscapeCharactersLabel);
  907. EditorGUILayout.PropertyField(m_UseMaxVisibleDescenderProp, k_VisibleDescenderLabel);
  908. if (EditorGUI.EndChangeCheck())
  909. m_HavePropertiesChanged = true;
  910. EditorGUILayout.Space();
  911. }
  912. protected void DrawEmojiFallbackSupport()
  913. {
  914. EditorGUI.BeginChangeCheck();
  915. EditorGUILayout.PropertyField(m_EmojiFallbackSupportProp, k_EmojiFallbackSupportLabel);
  916. if (EditorGUI.EndChangeCheck())
  917. m_HavePropertiesChanged = true;
  918. }
  919. protected void DrawSpriteAsset()
  920. {
  921. EditorGUI.BeginChangeCheck();
  922. EditorGUILayout.PropertyField(m_SpriteAssetProp, k_SpriteAssetLabel, true);
  923. if (EditorGUI.EndChangeCheck())
  924. m_HavePropertiesChanged = true;
  925. EditorGUILayout.Space();
  926. }
  927. protected void DrawStyleSheet()
  928. {
  929. EditorGUI.BeginChangeCheck();
  930. EditorGUILayout.PropertyField(m_StyleSheetAssetProp, k_StyleSheetAssetLabel, true);
  931. if (EditorGUI.EndChangeCheck())
  932. {
  933. m_StyleNames = GetStyleNames();
  934. m_HavePropertiesChanged = true;
  935. }
  936. EditorGUILayout.Space();
  937. }
  938. protected void DrawTextureMapping()
  939. {
  940. // TEXTURE MAPPING OPTIONS
  941. EditorGUI.BeginChangeCheck();
  942. EditorGUILayout.PropertyField(m_HorizontalMappingProp, k_HorizontalMappingLabel);
  943. EditorGUILayout.PropertyField(m_VerticalMappingProp, k_VerticalMappingLabel);
  944. if (EditorGUI.EndChangeCheck())
  945. {
  946. m_HavePropertiesChanged = true;
  947. }
  948. // UV OPTIONS
  949. if (m_HorizontalMappingProp.enumValueIndex > 0)
  950. {
  951. EditorGUI.BeginChangeCheck();
  952. EditorGUILayout.PropertyField(m_UvLineOffsetProp, k_LineOffsetLabel, GUILayout.MinWidth(70f));
  953. if (EditorGUI.EndChangeCheck())
  954. {
  955. m_HavePropertiesChanged = true;
  956. }
  957. }
  958. EditorGUILayout.Space();
  959. }
  960. protected void DrawFontFeatures()
  961. {
  962. int srcMask = 0;
  963. int featureCount = m_FontFeaturesActiveProp.arraySize;
  964. for (int i = 0; i < featureCount; i++)
  965. {
  966. SerializedProperty activeFeatureProperty = m_FontFeaturesActiveProp.GetArrayElementAtIndex(i);
  967. for (int j = 0; j < k_FontFeatures.Length; j++)
  968. {
  969. if (activeFeatureProperty.intValue == k_FontFeatures[j].TagToInt())
  970. {
  971. srcMask |= 0x1 << j;
  972. break;
  973. }
  974. }
  975. }
  976. EditorGUI.BeginChangeCheck();
  977. int mask = EditorGUILayout.MaskField(k_FontFeaturesLabel, srcMask, k_FontFeatures);
  978. if (EditorGUI.EndChangeCheck())
  979. {
  980. m_FontFeaturesActiveProp.ClearArray();
  981. int writeIndex = 0;
  982. for (int i = 0; i < k_FontFeatures.Length; i++)
  983. {
  984. int bit = 0x1 << i;
  985. if ((mask & bit) == bit)
  986. {
  987. m_FontFeaturesActiveProp.InsertArrayElementAtIndex(writeIndex);
  988. SerializedProperty newFeature = m_FontFeaturesActiveProp.GetArrayElementAtIndex(writeIndex);
  989. newFeature.intValue = k_FontFeatures[i].TagToInt();
  990. writeIndex += 1;
  991. }
  992. }
  993. m_HavePropertiesChanged = true;
  994. }
  995. }
  996. protected void DrawPadding()
  997. {
  998. // EXTRA PADDING
  999. EditorGUI.BeginChangeCheck();
  1000. EditorGUILayout.PropertyField(m_EnableExtraPaddingProp, k_PaddingLabel);
  1001. if (EditorGUI.EndChangeCheck())
  1002. {
  1003. m_HavePropertiesChanged = true;
  1004. m_CheckPaddingRequiredProp.boolValue = true;
  1005. }
  1006. }
  1007. /// <summary>
  1008. /// Method to retrieve the material presets that match the currently selected font asset.
  1009. /// </summary>
  1010. protected GUIContent[] GetMaterialPresets()
  1011. {
  1012. TMP_FontAsset fontAsset = m_FontAssetProp.objectReferenceValue as TMP_FontAsset;
  1013. if (fontAsset == null) return null;
  1014. m_MaterialPresets = TMP_EditorUtility.FindMaterialReferences(fontAsset);
  1015. m_MaterialPresetNames = new GUIContent[m_MaterialPresets.Length];
  1016. m_MaterialPresetIndexLookup.Clear();
  1017. for (int i = 0; i < m_MaterialPresetNames.Length; i++)
  1018. {
  1019. m_MaterialPresetNames[i] = new GUIContent(m_MaterialPresets[i].name);
  1020. m_MaterialPresetIndexLookup.Add(m_MaterialPresets[i].GetInstanceID(), i);
  1021. //if (m_TargetMaterial.GetInstanceID() == m_MaterialPresets[i].GetInstanceID())
  1022. // m_MaterialPresetSelectionIndex = i;
  1023. }
  1024. m_IsPresetListDirty = false;
  1025. return m_MaterialPresetNames;
  1026. }
  1027. protected GUIContent[] GetStyleNames()
  1028. {
  1029. k_AvailableStyles.Clear();
  1030. m_TextStyleIndexLookup.Clear();
  1031. m_Styles.Clear();
  1032. // First style on the list is always the Normal default style.
  1033. TMP_Style styleNormal = TMP_Style.NormalStyle;
  1034. m_Styles.Add(styleNormal);
  1035. m_TextStyleIndexLookup.Add(styleNormal.hashCode, 0);
  1036. k_AvailableStyles.Add(styleNormal.hashCode, styleNormal);
  1037. // Get styles from Style Sheet potentially assigned to the text object.
  1038. TMP_StyleSheet localStyleSheet = (TMP_StyleSheet)m_StyleSheetAssetProp.objectReferenceValue;
  1039. if (localStyleSheet != null)
  1040. {
  1041. int styleCount = localStyleSheet.styles.Count;
  1042. for (int i = 0; i < styleCount; i++)
  1043. {
  1044. TMP_Style style = localStyleSheet.styles[i];
  1045. if (k_AvailableStyles.ContainsKey(style.hashCode) == false)
  1046. {
  1047. k_AvailableStyles.Add(style.hashCode, style);
  1048. m_Styles.Add(style);
  1049. m_TextStyleIndexLookup.Add(style.hashCode, m_TextStyleIndexLookup.Count);
  1050. }
  1051. }
  1052. }
  1053. // Get styles from TMP Settings' default style sheet.
  1054. TMP_StyleSheet globalStyleSheet = TMP_Settings.defaultStyleSheet;
  1055. if (globalStyleSheet != null)
  1056. {
  1057. int styleCount = globalStyleSheet.styles.Count;
  1058. for (int i = 0; i < styleCount; i++)
  1059. {
  1060. TMP_Style style = globalStyleSheet.styles[i];
  1061. if (k_AvailableStyles.ContainsKey(style.hashCode) == false)
  1062. {
  1063. k_AvailableStyles.Add(style.hashCode, style);
  1064. m_Styles.Add(style);
  1065. m_TextStyleIndexLookup.Add(style.hashCode, m_TextStyleIndexLookup.Count);
  1066. }
  1067. }
  1068. }
  1069. // Create array that will contain the list of available styles.
  1070. GUIContent[] styleNames = k_AvailableStyles.Values.Select(item => new GUIContent(item.name)).ToArray();
  1071. // Set text style index
  1072. m_TextStyleIndexLookup.TryGetValue(m_TextStyleHashCodeProp.intValue, out m_StyleSelectionIndex);
  1073. return styleNames;
  1074. }
  1075. // DRAW MARGIN PROPERTY
  1076. protected void DrawMarginProperty(SerializedProperty property, GUIContent label)
  1077. {
  1078. Rect rect = EditorGUILayout.GetControlRect(false, 2 * 18);
  1079. EditorGUI.BeginProperty(rect, label, property);
  1080. Rect pos0 = new Rect(rect.x, rect.y + 2, rect.width - 15, 18);
  1081. float width = rect.width + 3;
  1082. pos0.width = EditorGUIUtility.labelWidth;
  1083. EditorGUI.PrefixLabel(pos0, label);
  1084. Vector4 margins = property.vector4Value;
  1085. float widthB = width - EditorGUIUtility.labelWidth;
  1086. float fieldWidth = widthB / 4;
  1087. pos0.width = Mathf.Max(fieldWidth - 5, 45f);
  1088. // Labels
  1089. pos0.x = EditorGUIUtility.labelWidth + 15;
  1090. margins.x = DrawMarginField(pos0, "Left", margins.x);
  1091. pos0.x += fieldWidth;
  1092. margins.y = DrawMarginField(pos0, "Top", margins.y);
  1093. pos0.x += fieldWidth;
  1094. margins.z = DrawMarginField(pos0, "Right", margins.z);
  1095. pos0.x += fieldWidth;
  1096. margins.w = DrawMarginField(pos0, "Bottom", margins.w);
  1097. property.vector4Value = margins;
  1098. EditorGUI.EndProperty();
  1099. }
  1100. float DrawMarginField(Rect position, string label, float value)
  1101. {
  1102. int controlId = GUIUtility.GetControlID(FocusType.Keyboard, position);
  1103. EditorGUI.PrefixLabel(position, controlId, new GUIContent(label));
  1104. Rect dragZone = new Rect(position.x, position.y, position.width, position.height);
  1105. position.y += EditorGUIUtility.singleLineHeight;
  1106. return EditorGUI.DoFloatField(EditorGUI.s_RecycledEditor, position, dragZone, controlId, value, EditorGUI.kFloatFieldFormatString, EditorStyles.numberField, true);
  1107. }
  1108. protected void DrawPropertySlider(GUIContent label, SerializedProperty property)
  1109. {
  1110. Rect rect = EditorGUILayout.GetControlRect(false, 17);
  1111. GUIContent content = label ?? GUIContent.none;
  1112. EditorGUI.Slider(new Rect(rect.x, rect.y, rect.width, rect.height), property, 0.0f, 1.0f, content);
  1113. }
  1114. protected abstract bool IsMixSelectionTypes();
  1115. // Special Handling of Undo / Redo Events.
  1116. protected abstract void OnUndoRedo();
  1117. }
  1118. }