TMP_FontAssetEditor.cs 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditorInternal;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine.TextCore;
  7. using UnityEngine.TextCore.LowLevel;
  8. namespace TMPro.EditorUtilities
  9. {
  10. [CustomPropertyDrawer(typeof(TMP_FontWeightPair))]
  11. public class FontWeightDrawer : PropertyDrawer
  12. {
  13. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  14. {
  15. SerializedProperty prop_regular = property.FindPropertyRelative("regularTypeface");
  16. SerializedProperty prop_italic = property.FindPropertyRelative("italicTypeface");
  17. float width = position.width;
  18. position.width = EditorGUIUtility.labelWidth;
  19. EditorGUI.LabelField(position, label);
  20. int oldIndent = EditorGUI.indentLevel;
  21. EditorGUI.indentLevel = 0;
  22. // NORMAL TYPEFACE
  23. if (label.text[0] == '4') GUI.enabled = false;
  24. position.x += position.width; position.width = (width - position.width) / 2;
  25. EditorGUI.PropertyField(position, prop_regular, GUIContent.none);
  26. // ITALIC TYPEFACE
  27. GUI.enabled = true;
  28. position.x += position.width;
  29. EditorGUI.PropertyField(position, prop_italic, GUIContent.none);
  30. EditorGUI.indentLevel = oldIndent;
  31. }
  32. }
  33. [CustomEditor(typeof(TMP_FontAsset))]
  34. public class TMP_FontAssetEditor : Editor
  35. {
  36. private struct UI_PanelState
  37. {
  38. public static bool faceInfoPanel = true;
  39. public static bool generationSettingsPanel = true;
  40. public static bool fontAtlasInfoPanel = true;
  41. public static bool fontWeightPanel = true;
  42. public static bool fallbackFontAssetPanel = true;
  43. public static bool glyphTablePanel = false;
  44. public static bool characterTablePanel = false;
  45. public static bool fontFeatureTablePanel = false;
  46. }
  47. private struct AtlasSettings
  48. {
  49. public GlyphRenderMode glyphRenderMode;
  50. public int pointSize;
  51. public int padding;
  52. public int atlasWidth;
  53. public int atlasHeight;
  54. }
  55. /// <summary>
  56. /// Material used to display SDF glyphs in the Character and Glyph tables.
  57. /// </summary>
  58. internal static Material internalSDFMaterial
  59. {
  60. get
  61. {
  62. if (s_InternalSDFMaterial == null)
  63. {
  64. Shader shader = Shader.Find("Hidden/TextMeshPro/Internal/Distance Field SSD");
  65. if (shader != null)
  66. s_InternalSDFMaterial = new Material(shader);
  67. }
  68. return s_InternalSDFMaterial;
  69. }
  70. }
  71. static Material s_InternalSDFMaterial;
  72. /// <summary>
  73. /// Material used to display Bitmap glyphs in the Character and Glyph tables.
  74. /// </summary>
  75. internal static Material internalBitmapMaterial
  76. {
  77. get
  78. {
  79. if (s_InternalBitmapMaterial == null)
  80. {
  81. Shader shader = Shader.Find("Hidden/Internal-GUITextureClipText");
  82. if (shader != null)
  83. s_InternalBitmapMaterial = new Material(shader);
  84. }
  85. return s_InternalBitmapMaterial;
  86. }
  87. }
  88. static Material s_InternalBitmapMaterial;
  89. private static string[] s_UiStateLabel = new string[] { "<i>(Click to collapse)</i> ", "<i>(Click to expand)</i> " };
  90. private GUIContent[] m_AtlasResolutionLabels = { new GUIContent("8"), new GUIContent("16"), new GUIContent("32"), new GUIContent("64"), new GUIContent("128"), new GUIContent("256"), new GUIContent("512"), new GUIContent("1024"), new GUIContent("2048"), new GUIContent("4096"), new GUIContent("8192") };
  91. private int[] m_AtlasResolutions = { 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
  92. private struct Warning
  93. {
  94. public bool isEnabled;
  95. public double expirationTime;
  96. }
  97. private int m_CurrentGlyphPage = 0;
  98. private int m_CurrentCharacterPage = 0;
  99. private int m_CurrentKerningPage = 0;
  100. private int m_SelectedGlyphRecord = -1;
  101. private int m_SelectedCharacterRecord = -1;
  102. private int m_SelectedAdjustmentRecord = -1;
  103. private string m_dstGlyphID;
  104. private string m_dstUnicode;
  105. private const string k_placeholderUnicodeHex = "<i>New Unicode (Hex)</i>";
  106. private string m_unicodeHexLabel = k_placeholderUnicodeHex;
  107. private const string k_placeholderGlyphID = "<i>New Glyph ID</i>";
  108. private string m_GlyphIDLabel = k_placeholderGlyphID;
  109. private Warning m_AddGlyphWarning;
  110. private Warning m_AddCharacterWarning;
  111. private bool m_DisplayDestructiveChangeWarning;
  112. private AtlasSettings m_AtlasSettings;
  113. private bool m_MaterialPresetsRequireUpdate;
  114. private string m_GlyphSearchPattern;
  115. private List<int> m_GlyphSearchList;
  116. private string m_CharacterSearchPattern;
  117. private List<int> m_CharacterSearchList;
  118. private string m_KerningTableSearchPattern;
  119. private List<int> m_KerningTableSearchList;
  120. private bool m_isSearchDirty;
  121. private const string k_UndoRedo = "UndoRedoPerformed";
  122. private SerializedProperty m_AtlasPopulationMode_prop;
  123. private SerializedProperty font_atlas_prop;
  124. private SerializedProperty font_material_prop;
  125. private SerializedProperty m_AtlasRenderMode_prop;
  126. private SerializedProperty m_SamplingPointSize_prop;
  127. private SerializedProperty m_AtlasPadding_prop;
  128. private SerializedProperty m_AtlasWidth_prop;
  129. private SerializedProperty m_AtlasHeight_prop;
  130. private SerializedProperty fontWeights_prop;
  131. //private SerializedProperty fallbackFontAssets_prop;
  132. private ReorderableList m_list;
  133. private SerializedProperty font_normalStyle_prop;
  134. private SerializedProperty font_normalSpacing_prop;
  135. private SerializedProperty font_boldStyle_prop;
  136. private SerializedProperty font_boldSpacing_prop;
  137. private SerializedProperty font_italicStyle_prop;
  138. private SerializedProperty font_tabSize_prop;
  139. private SerializedProperty m_FaceInfo_prop;
  140. private SerializedProperty m_GlyphTable_prop;
  141. private SerializedProperty m_CharacterTable_prop;
  142. private TMP_FontFeatureTable m_FontFeatureTable;
  143. private SerializedProperty m_FontFeatureTable_prop;
  144. private SerializedProperty m_GlyphPairAdjustmentRecords_prop;
  145. private TMP_SerializedPropertyHolder m_SerializedPropertyHolder;
  146. private SerializedProperty m_EmptyGlyphPairAdjustmentRecord_prop;
  147. private TMP_FontAsset m_fontAsset;
  148. private Material[] m_materialPresets;
  149. private bool isAssetDirty = false;
  150. private int errorCode;
  151. private System.DateTime timeStamp;
  152. public void OnEnable()
  153. {
  154. m_FaceInfo_prop = serializedObject.FindProperty("m_FaceInfo");
  155. font_atlas_prop = serializedObject.FindProperty("m_AtlasTextures").GetArrayElementAtIndex(0);
  156. font_material_prop = serializedObject.FindProperty("material");
  157. m_AtlasPopulationMode_prop = serializedObject.FindProperty("m_AtlasPopulationMode");
  158. m_AtlasRenderMode_prop = serializedObject.FindProperty("m_AtlasRenderMode");
  159. m_SamplingPointSize_prop = m_FaceInfo_prop.FindPropertyRelative("m_PointSize");
  160. m_AtlasPadding_prop = serializedObject.FindProperty("m_AtlasPadding");
  161. m_AtlasWidth_prop = serializedObject.FindProperty("m_AtlasWidth");
  162. m_AtlasHeight_prop = serializedObject.FindProperty("m_AtlasHeight");
  163. fontWeights_prop = serializedObject.FindProperty("m_FontWeightTable");
  164. m_list = new ReorderableList(serializedObject, serializedObject.FindProperty("m_FallbackFontAssetTable"), true, true, true, true);
  165. m_list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
  166. {
  167. var element = m_list.serializedProperty.GetArrayElementAtIndex(index);
  168. rect.y += 2;
  169. EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, GUIContent.none);
  170. };
  171. m_list.drawHeaderCallback = rect =>
  172. {
  173. EditorGUI.LabelField(rect, "Fallback List");
  174. };
  175. // Clean up fallback list in the event if contains null elements.
  176. CleanFallbackFontAssetTable();
  177. font_normalStyle_prop = serializedObject.FindProperty("normalStyle");
  178. font_normalSpacing_prop = serializedObject.FindProperty("normalSpacingOffset");
  179. font_boldStyle_prop = serializedObject.FindProperty("boldStyle");
  180. font_boldSpacing_prop = serializedObject.FindProperty("boldSpacing");
  181. font_italicStyle_prop = serializedObject.FindProperty("italicStyle");
  182. font_tabSize_prop = serializedObject.FindProperty("tabSize");
  183. m_CharacterTable_prop = serializedObject.FindProperty("m_CharacterTable");
  184. m_GlyphTable_prop = serializedObject.FindProperty("m_GlyphTable");
  185. m_FontFeatureTable_prop = serializedObject.FindProperty("m_FontFeatureTable");
  186. m_GlyphPairAdjustmentRecords_prop = m_FontFeatureTable_prop.FindPropertyRelative("m_GlyphPairAdjustmentRecords");
  187. m_fontAsset = target as TMP_FontAsset;
  188. m_FontFeatureTable = m_fontAsset.fontFeatureTable;
  189. // Upgrade Font Feature Table if necessary
  190. if (m_fontAsset.m_KerningTable != null && m_fontAsset.m_KerningTable.kerningPairs != null && m_fontAsset.m_KerningTable.kerningPairs.Count > 0)
  191. m_fontAsset.ReadFontAssetDefinition();
  192. // Create serialized object to allow us to use a serialized property of an empty kerning pair.
  193. m_SerializedPropertyHolder = CreateInstance<TMP_SerializedPropertyHolder>();
  194. m_SerializedPropertyHolder.fontAsset = m_fontAsset;
  195. SerializedObject internalSerializedObject = new SerializedObject(m_SerializedPropertyHolder);
  196. m_EmptyGlyphPairAdjustmentRecord_prop = internalSerializedObject.FindProperty("glyphPairAdjustmentRecord");
  197. m_materialPresets = TMP_EditorUtility.FindMaterialReferences(m_fontAsset);
  198. m_GlyphSearchList = new List<int>();
  199. m_KerningTableSearchList = new List<int>();
  200. }
  201. public void OnDisable()
  202. {
  203. // Revert changes if user closes or changes selection without having made a choice.
  204. if (m_DisplayDestructiveChangeWarning)
  205. {
  206. m_DisplayDestructiveChangeWarning = false;
  207. RestoreAtlasGenerationSettings();
  208. GUIUtility.keyboardControl = 0;
  209. serializedObject.ApplyModifiedProperties();
  210. }
  211. }
  212. public override void OnInspectorGUI()
  213. {
  214. //Debug.Log("OnInspectorGUI Called.");
  215. Event currentEvent = Event.current;
  216. serializedObject.Update();
  217. Rect rect = EditorGUILayout.GetControlRect(false, 24);
  218. float labelWidth = EditorGUIUtility.labelWidth;
  219. float fieldWidth = EditorGUIUtility.fieldWidth;
  220. // FACE INFO PANEL
  221. #region Face info
  222. GUI.Label(rect, new GUIContent("<b>Face Info</b> - v" + m_fontAsset.version), TMP_UIStyleManager.sectionHeader);
  223. rect.x += rect.width - 132f;
  224. rect.y += 2;
  225. rect.width = 130f;
  226. rect.height = 18f;
  227. if (GUI.Button(rect, new GUIContent("Update Atlas Texture")))
  228. {
  229. TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(target as TMP_FontAsset);
  230. }
  231. EditorGUI.indentLevel = 1;
  232. GUI.enabled = false; // Lock UI
  233. // TODO : Consider creating a property drawer for these.
  234. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_FamilyName"));
  235. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_StyleName"));
  236. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_PointSize"));
  237. GUI.enabled = true;
  238. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_Scale"));
  239. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_LineHeight"));
  240. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_AscentLine"));
  241. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_CapLine"));
  242. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_MeanLine"));
  243. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_Baseline"));
  244. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_DescentLine"));
  245. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_UnderlineOffset"));
  246. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_UnderlineThickness"));
  247. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_StrikethroughOffset"));
  248. //EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("strikethroughThickness"));
  249. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_SuperscriptOffset"));
  250. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_SuperscriptSize"));
  251. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_SubscriptOffset"));
  252. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_SubscriptSize"));
  253. EditorGUILayout.PropertyField(m_FaceInfo_prop.FindPropertyRelative("m_TabWidth"));
  254. // TODO : Add clamping for some of these values.
  255. //subSize_prop.floatValue = Mathf.Clamp(subSize_prop.floatValue, 0.25f, 1f);
  256. EditorGUILayout.Space();
  257. #endregion
  258. // GENERATION SETTINGS
  259. #region Generation Settings
  260. rect = EditorGUILayout.GetControlRect(false, 24);
  261. if (GUI.Button(rect, new GUIContent("<b>Generation Settings</b>"), TMP_UIStyleManager.sectionHeader))
  262. UI_PanelState.generationSettingsPanel = !UI_PanelState.generationSettingsPanel;
  263. GUI.Label(rect, (UI_PanelState.generationSettingsPanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  264. if (UI_PanelState.generationSettingsPanel)
  265. {
  266. EditorGUI.indentLevel = 1;
  267. EditorGUI.BeginChangeCheck();
  268. Font sourceFont = (Font)EditorGUILayout.ObjectField("Source Font File", m_fontAsset.m_SourceFontFile_EditorRef, typeof(Font), false);
  269. if (EditorGUI.EndChangeCheck())
  270. {
  271. string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(sourceFont));
  272. m_fontAsset.m_SourceFontFileGUID = guid;
  273. m_fontAsset.m_SourceFontFile_EditorRef = sourceFont;
  274. }
  275. EditorGUI.BeginChangeCheck();
  276. EditorGUILayout.PropertyField(m_AtlasPopulationMode_prop, new GUIContent("Atlas Population Mode"));
  277. if (EditorGUI.EndChangeCheck())
  278. {
  279. serializedObject.ApplyModifiedProperties();
  280. bool isDatabaseRefreshRequired = false;
  281. if (m_AtlasPopulationMode_prop.intValue == 0)
  282. {
  283. m_fontAsset.sourceFontFile = null;
  284. // Set atlas textures to non readable.
  285. //for (int i = 0; i < m_fontAsset.atlasTextures.Length; i++)
  286. //{
  287. // Texture2D tex = m_fontAsset.atlasTextures[i];
  288. // if (tex != null && tex.isReadable)
  289. // {
  290. // string texPath = AssetDatabase.GetAssetPath(tex);
  291. // var texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter;
  292. // if (texImporter != null)
  293. // {
  294. // texImporter.isReadable = false;
  295. // AssetDatabase.ImportAsset(texPath);
  296. // isDatabaseRefreshRequired = true;
  297. // }
  298. // }
  299. //}
  300. Debug.Log("Atlas Population mode set to [Static].");
  301. }
  302. else if (m_AtlasPopulationMode_prop.intValue == 1)
  303. {
  304. if (m_fontAsset.m_SourceFontFile_EditorRef.dynamic == false)
  305. {
  306. Debug.LogWarning("Please set the [" + m_fontAsset.name + "] font to dynamic mode as this is required for Dynamic SDF support.", m_fontAsset.m_SourceFontFile_EditorRef);
  307. m_AtlasPopulationMode_prop.intValue = 0;
  308. serializedObject.ApplyModifiedProperties();
  309. }
  310. else
  311. {
  312. m_fontAsset.sourceFontFile = m_fontAsset.m_SourceFontFile_EditorRef;
  313. /*
  314. // Set atlas textures to non readable.
  315. for (int i = 0; i < m_fontAsset.atlasTextures.Length; i++)
  316. {
  317. Texture2D tex = m_fontAsset.atlasTextures[i];
  318. if (tex != null && tex.isReadable == false)
  319. {
  320. string texPath = AssetDatabase.GetAssetPath(tex.GetInstanceID());
  321. Object[] paths = AssetDatabase.LoadAllAssetsAtPath(texPath);
  322. var texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter;
  323. if (texImporter != null)
  324. {
  325. texImporter.isReadable = true;
  326. AssetDatabase.ImportAsset(texPath);
  327. isDatabaseRefreshRequired = true;
  328. }
  329. }
  330. }
  331. */
  332. Debug.Log("Atlas Population mode set to [Dynamic].");
  333. }
  334. }
  335. if (isDatabaseRefreshRequired)
  336. AssetDatabase.Refresh();
  337. serializedObject.Update();
  338. isAssetDirty = true;
  339. }
  340. GUI.enabled = true;
  341. // Save state of atlas settings
  342. if (m_DisplayDestructiveChangeWarning == false)
  343. {
  344. SavedAtlasGenerationSettings();
  345. //Undo.RegisterCompleteObjectUndo(m_fontAsset, "Font Asset Changes");
  346. }
  347. EditorGUI.BeginChangeCheck();
  348. // TODO: Switch shaders depending on GlyphRenderMode.
  349. EditorGUILayout.PropertyField(m_AtlasRenderMode_prop);
  350. EditorGUILayout.PropertyField(m_SamplingPointSize_prop, new GUIContent("Sampling Point Size"));
  351. if (EditorGUI.EndChangeCheck())
  352. {
  353. m_DisplayDestructiveChangeWarning = true;
  354. }
  355. // Changes to these properties require updating Material Presets for this font asset.
  356. EditorGUI.BeginChangeCheck();
  357. EditorGUILayout.PropertyField(m_AtlasPadding_prop, new GUIContent("Padding"));
  358. EditorGUILayout.IntPopup(m_AtlasWidth_prop, m_AtlasResolutionLabels, m_AtlasResolutions, new GUIContent("Atlas Width"));
  359. EditorGUILayout.IntPopup(m_AtlasHeight_prop, m_AtlasResolutionLabels, m_AtlasResolutions, new GUIContent("Atlas Height"));
  360. if (EditorGUI.EndChangeCheck())
  361. {
  362. m_MaterialPresetsRequireUpdate = true;
  363. m_DisplayDestructiveChangeWarning = true;
  364. }
  365. if (m_DisplayDestructiveChangeWarning)
  366. {
  367. // These changes are destructive on the font asset
  368. rect = EditorGUILayout.GetControlRect(false, 60);
  369. rect.x += 15;
  370. rect.width -= 15;
  371. EditorGUI.HelpBox(rect, "Changing these settings will clear the font asset's character, glyph and texture data.", MessageType.Warning);
  372. if (GUI.Button(new Rect(rect.width - 140, rect.y + 36, 80, 18), new GUIContent("Apply")))
  373. {
  374. m_DisplayDestructiveChangeWarning = false;
  375. // Update face info is sampling point size was changed.
  376. if (m_AtlasSettings.pointSize != m_SamplingPointSize_prop.intValue)
  377. {
  378. FontEngine.LoadFontFace(m_fontAsset.sourceFontFile, m_SamplingPointSize_prop.intValue);
  379. m_fontAsset.faceInfo = FontEngine.GetFaceInfo();
  380. }
  381. // Update material
  382. m_fontAsset.material.SetFloat(ShaderUtilities.ID_TextureWidth, m_AtlasWidth_prop.intValue);
  383. m_fontAsset.material.SetFloat(ShaderUtilities.ID_TextureHeight, m_AtlasHeight_prop.intValue);
  384. m_fontAsset.material.SetFloat(ShaderUtilities.ID_GradientScale, m_AtlasPadding_prop.intValue + 1);
  385. // Update material presets if any of the relevant properties have been changed.
  386. if (m_MaterialPresetsRequireUpdate)
  387. {
  388. m_MaterialPresetsRequireUpdate = false;
  389. Material[] materialPresets = TMP_EditorUtility.FindMaterialReferences(m_fontAsset);
  390. for (int i = 0; i < materialPresets.Length; i++)
  391. {
  392. Material mat = materialPresets[i];
  393. mat.SetFloat(ShaderUtilities.ID_TextureWidth, m_AtlasWidth_prop.intValue);
  394. mat.SetFloat(ShaderUtilities.ID_TextureHeight, m_AtlasHeight_prop.intValue);
  395. mat.SetFloat(ShaderUtilities.ID_GradientScale, m_AtlasPadding_prop.intValue + 1);
  396. }
  397. }
  398. m_fontAsset.ClearFontAssetData();
  399. GUIUtility.keyboardControl = 0;
  400. isAssetDirty = true;
  401. // Update Font Asset Creation Settings to reflect new changes.
  402. UpdateFontAssetCreationSettings();
  403. // TODO: Clear undo buffers.
  404. //Undo.ClearUndo(m_fontAsset);
  405. }
  406. if (GUI.Button(new Rect(rect.width - 56, rect.y + 36, 80, 18), new GUIContent("Revert")))
  407. {
  408. m_DisplayDestructiveChangeWarning = false;
  409. RestoreAtlasGenerationSettings();
  410. GUIUtility.keyboardControl = 0;
  411. // TODO: Clear undo buffers.
  412. //Undo.ClearUndo(m_fontAsset);
  413. }
  414. }
  415. EditorGUILayout.Space();
  416. }
  417. #endregion
  418. // ATLAS & MATERIAL PANEL
  419. #region Atlas & Material
  420. rect = EditorGUILayout.GetControlRect(false, 24);
  421. if (GUI.Button(rect, new GUIContent("<b>Atlas & Material</b>"), TMP_UIStyleManager.sectionHeader))
  422. UI_PanelState.fontAtlasInfoPanel = !UI_PanelState.fontAtlasInfoPanel;
  423. GUI.Label(rect, (UI_PanelState.fontAtlasInfoPanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  424. if (UI_PanelState.fontAtlasInfoPanel)
  425. {
  426. EditorGUI.indentLevel = 1;
  427. GUI.enabled = false;
  428. EditorGUILayout.PropertyField(font_atlas_prop, new GUIContent("Font Atlas"));
  429. EditorGUILayout.PropertyField(font_material_prop, new GUIContent("Font Material"));
  430. GUI.enabled = true;
  431. EditorGUILayout.Space();
  432. }
  433. #endregion
  434. string evt_cmd = Event.current.commandName; // Get Current Event CommandName to check for Undo Events
  435. // FONT WEIGHT PANEL
  436. #region Font Weights
  437. rect = EditorGUILayout.GetControlRect(false, 24);
  438. if (GUI.Button(rect, new GUIContent("<b>Font Weights</b>", "The Font Assets that will be used for different font weights and the settings used to simulate a typeface when no asset is available."), TMP_UIStyleManager.sectionHeader))
  439. UI_PanelState.fontWeightPanel = !UI_PanelState.fontWeightPanel;
  440. GUI.Label(rect, (UI_PanelState.fontWeightPanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  441. if (UI_PanelState.fontWeightPanel)
  442. {
  443. EditorGUIUtility.labelWidth *= 0.75f;
  444. EditorGUIUtility.fieldWidth *= 0.25f;
  445. EditorGUILayout.BeginVertical();
  446. EditorGUI.indentLevel = 1;
  447. rect = EditorGUILayout.GetControlRect(true);
  448. rect.x += EditorGUIUtility.labelWidth;
  449. rect.width = (rect.width - EditorGUIUtility.labelWidth) / 2f;
  450. GUI.Label(rect, "Regular Tyepface", EditorStyles.label);
  451. rect.x += rect.width;
  452. GUI.Label(rect, "Italic Typeface", EditorStyles.label);
  453. EditorGUI.indentLevel = 1;
  454. EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(1), new GUIContent("100 - Thin"));
  455. EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(2), new GUIContent("200 - Extra-Light"));
  456. EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(3), new GUIContent("300 - Light"));
  457. EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(4), new GUIContent("400 - Regular"));
  458. EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(5), new GUIContent("500 - Medium"));
  459. EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(6), new GUIContent("600 - Semi-Bold"));
  460. EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(7), new GUIContent("700 - Bold"));
  461. EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(8), new GUIContent("800 - Heavy"));
  462. EditorGUILayout.PropertyField(fontWeights_prop.GetArrayElementAtIndex(9), new GUIContent("900 - Black"));
  463. EditorGUILayout.EndVertical();
  464. EditorGUILayout.Space();
  465. EditorGUILayout.BeginVertical();
  466. EditorGUILayout.BeginHorizontal();
  467. EditorGUILayout.PropertyField(font_normalStyle_prop, new GUIContent("Normal Weight"));
  468. font_normalStyle_prop.floatValue = Mathf.Clamp(font_normalStyle_prop.floatValue, -3.0f, 3.0f);
  469. if (GUI.changed || evt_cmd == k_UndoRedo)
  470. {
  471. GUI.changed = false;
  472. // Modify the material property on matching material presets.
  473. for (int i = 0; i < m_materialPresets.Length; i++)
  474. m_materialPresets[i].SetFloat("_WeightNormal", font_normalStyle_prop.floatValue);
  475. }
  476. EditorGUILayout.PropertyField(font_boldStyle_prop, new GUIContent("Bold Weight"));
  477. font_boldStyle_prop.floatValue = Mathf.Clamp(font_boldStyle_prop.floatValue, -3.0f, 3.0f);
  478. if (GUI.changed || evt_cmd == k_UndoRedo)
  479. {
  480. GUI.changed = false;
  481. // Modify the material property on matching material presets.
  482. for (int i = 0; i < m_materialPresets.Length; i++)
  483. m_materialPresets[i].SetFloat("_WeightBold", font_boldStyle_prop.floatValue);
  484. }
  485. EditorGUILayout.EndHorizontal();
  486. EditorGUILayout.BeginHorizontal();
  487. EditorGUILayout.PropertyField(font_normalSpacing_prop, new GUIContent("Spacing Offset"));
  488. font_normalSpacing_prop.floatValue = Mathf.Clamp(font_normalSpacing_prop.floatValue, -100, 100);
  489. if (GUI.changed || evt_cmd == k_UndoRedo)
  490. {
  491. GUI.changed = false;
  492. }
  493. EditorGUILayout.PropertyField(font_boldSpacing_prop, new GUIContent("Bold Spacing"));
  494. font_boldSpacing_prop.floatValue = Mathf.Clamp(font_boldSpacing_prop.floatValue, 0, 100);
  495. if (GUI.changed || evt_cmd == k_UndoRedo)
  496. {
  497. GUI.changed = false;
  498. }
  499. EditorGUILayout.EndHorizontal();
  500. EditorGUILayout.BeginHorizontal();
  501. EditorGUILayout.PropertyField(font_italicStyle_prop, new GUIContent("Italic Style"));
  502. font_italicStyle_prop.intValue = Mathf.Clamp(font_italicStyle_prop.intValue, 15, 60);
  503. EditorGUILayout.PropertyField(font_tabSize_prop, new GUIContent("Tab Multiple"));
  504. EditorGUILayout.EndHorizontal();
  505. EditorGUILayout.EndVertical();
  506. EditorGUILayout.Space();
  507. }
  508. EditorGUIUtility.labelWidth = 0;
  509. EditorGUIUtility.fieldWidth = 0;
  510. #endregion
  511. // FALLBACK FONT ASSETS
  512. #region Fallback Font Asset
  513. rect = EditorGUILayout.GetControlRect(false, 24);
  514. EditorGUI.indentLevel = 0;
  515. if (GUI.Button(rect, new GUIContent("<b>Fallback Font Assets</b>", "Select the Font Assets that will be searched and used as fallback when characters are missing from this font asset."), TMP_UIStyleManager.sectionHeader))
  516. UI_PanelState.fallbackFontAssetPanel = !UI_PanelState.fallbackFontAssetPanel;
  517. GUI.Label(rect, (UI_PanelState.fallbackFontAssetPanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  518. if (UI_PanelState.fallbackFontAssetPanel)
  519. {
  520. EditorGUIUtility.labelWidth = 120;
  521. EditorGUI.indentLevel = 0;
  522. m_list.DoLayoutList();
  523. EditorGUILayout.Space();
  524. }
  525. #endregion
  526. // CHARACTER TABLE TABLE
  527. #region Character Table
  528. EditorGUIUtility.labelWidth = labelWidth;
  529. EditorGUIUtility.fieldWidth = fieldWidth;
  530. EditorGUI.indentLevel = 0;
  531. rect = EditorGUILayout.GetControlRect(false, 24);
  532. if (GUI.Button(rect, new GUIContent("<b>Character Table</b>", "List of characters contained in this font asset."), TMP_UIStyleManager.sectionHeader))
  533. UI_PanelState.characterTablePanel = !UI_PanelState.characterTablePanel;
  534. GUI.Label(rect, (UI_PanelState.characterTablePanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  535. if (UI_PanelState.characterTablePanel)
  536. {
  537. int arraySize = m_CharacterTable_prop.arraySize;
  538. int itemsPerPage = 15;
  539. // Display Glyph Management Tools
  540. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  541. {
  542. // Search Bar implementation
  543. #region DISPLAY SEARCH BAR
  544. EditorGUILayout.BeginHorizontal();
  545. {
  546. EditorGUIUtility.labelWidth = 130f;
  547. EditorGUI.BeginChangeCheck();
  548. string searchPattern = EditorGUILayout.TextField("Character Search", m_CharacterSearchPattern, "SearchTextField");
  549. if (EditorGUI.EndChangeCheck() || m_isSearchDirty)
  550. {
  551. if (string.IsNullOrEmpty(searchPattern) == false)
  552. {
  553. m_CharacterSearchPattern = searchPattern;
  554. // Search Character Table for potential matches
  555. SearchCharacterTable (m_CharacterSearchPattern, ref m_CharacterSearchList);
  556. }
  557. else
  558. m_CharacterSearchPattern = null;
  559. m_isSearchDirty = false;
  560. }
  561. string styleName = string.IsNullOrEmpty(m_CharacterSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton";
  562. if (GUILayout.Button(GUIContent.none, styleName))
  563. {
  564. GUIUtility.keyboardControl = 0;
  565. m_CharacterSearchPattern = string.Empty;
  566. }
  567. }
  568. EditorGUILayout.EndHorizontal();
  569. #endregion
  570. // Display Page Navigation
  571. if (!string.IsNullOrEmpty(m_CharacterSearchPattern))
  572. arraySize = m_CharacterSearchList.Count;
  573. DisplayPageNavigation(ref m_CurrentCharacterPage, arraySize, itemsPerPage);
  574. }
  575. EditorGUILayout.EndVertical();
  576. // Display Character Table Elements
  577. if (arraySize > 0)
  578. {
  579. // Display each character entry using the CharacterPropertyDrawer.
  580. for (int i = itemsPerPage * m_CurrentCharacterPage; i < arraySize && i < itemsPerPage * (m_CurrentCharacterPage + 1); i++)
  581. {
  582. // Define the start of the selection region of the element.
  583. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  584. int elementIndex = i;
  585. if (!string.IsNullOrEmpty(m_CharacterSearchPattern))
  586. elementIndex = m_CharacterSearchList[i];
  587. SerializedProperty characterProperty = m_CharacterTable_prop.GetArrayElementAtIndex(elementIndex);
  588. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  589. EditorGUI.BeginDisabledGroup(i != m_SelectedCharacterRecord);
  590. {
  591. EditorGUILayout.PropertyField(characterProperty);
  592. }
  593. EditorGUI.EndDisabledGroup();
  594. EditorGUILayout.EndVertical();
  595. // Define the end of the selection region of the element.
  596. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  597. // Check for Item selection
  598. Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y);
  599. if (DoSelectionCheck(selectionArea))
  600. {
  601. if (m_SelectedCharacterRecord == i)
  602. m_SelectedCharacterRecord = -1;
  603. else
  604. {
  605. m_SelectedCharacterRecord = i;
  606. m_AddCharacterWarning.isEnabled = false;
  607. m_unicodeHexLabel = k_placeholderUnicodeHex;
  608. GUIUtility.keyboardControl = 0;
  609. }
  610. }
  611. // Draw Selection Highlight and Glyph Options
  612. if (m_SelectedCharacterRecord == i)
  613. {
  614. TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255));
  615. // Draw Glyph management options
  616. Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f);
  617. float optionAreaWidth = controlRect.width * 0.6f;
  618. float btnWidth = optionAreaWidth / 3;
  619. Rect position = new Rect(controlRect.x + controlRect.width * .4f, controlRect.y, btnWidth, controlRect.height);
  620. // Copy Selected Glyph to Target Glyph ID
  621. GUI.enabled = !string.IsNullOrEmpty(m_dstUnicode);
  622. if (GUI.Button(position, new GUIContent("Copy to")))
  623. {
  624. GUIUtility.keyboardControl = 0;
  625. // Convert Hex Value to Decimal
  626. int dstGlyphID = TMP_TextUtilities.StringHexToInt(m_dstUnicode);
  627. //Add new glyph at target Unicode hex id.
  628. if (!AddNewCharacter(elementIndex, dstGlyphID))
  629. {
  630. m_AddCharacterWarning.isEnabled = true;
  631. m_AddCharacterWarning.expirationTime = EditorApplication.timeSinceStartup + 1;
  632. }
  633. m_dstUnicode = string.Empty;
  634. m_isSearchDirty = true;
  635. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset);
  636. }
  637. // Target Glyph ID
  638. GUI.enabled = true;
  639. position.x += btnWidth;
  640. GUI.SetNextControlName("CharacterID_Input");
  641. m_dstUnicode = EditorGUI.TextField(position, m_dstUnicode);
  642. // Placeholder text
  643. EditorGUI.LabelField(position, new GUIContent(m_unicodeHexLabel, "The Unicode (Hex) ID of the duplicated Character"), TMP_UIStyleManager.label);
  644. // Only filter the input when the destination glyph ID text field has focus.
  645. if (GUI.GetNameOfFocusedControl() == "CharacterID_Input")
  646. {
  647. m_unicodeHexLabel = string.Empty;
  648. //Filter out unwanted characters.
  649. char chr = Event.current.character;
  650. if ((chr < '0' || chr > '9') && (chr < 'a' || chr > 'f') && (chr < 'A' || chr > 'F'))
  651. {
  652. Event.current.character = '\0';
  653. }
  654. }
  655. else
  656. {
  657. m_unicodeHexLabel = k_placeholderUnicodeHex;
  658. //m_dstUnicode = string.Empty;
  659. }
  660. // Remove Glyph
  661. position.x += btnWidth;
  662. if (GUI.Button(position, "Remove"))
  663. {
  664. GUIUtility.keyboardControl = 0;
  665. RemoveCharacterFromList(elementIndex);
  666. isAssetDirty = true;
  667. m_SelectedCharacterRecord = -1;
  668. m_isSearchDirty = true;
  669. break;
  670. }
  671. if (m_AddCharacterWarning.isEnabled && EditorApplication.timeSinceStartup < m_AddCharacterWarning.expirationTime)
  672. {
  673. EditorGUILayout.HelpBox("The Destination Character ID already exists", MessageType.Warning);
  674. }
  675. }
  676. }
  677. }
  678. DisplayPageNavigation(ref m_CurrentCharacterPage, arraySize, itemsPerPage);
  679. EditorGUILayout.Space();
  680. }
  681. #endregion
  682. // GLYPH TABLE
  683. #region Glyph Table
  684. EditorGUIUtility.labelWidth = labelWidth;
  685. EditorGUIUtility.fieldWidth = fieldWidth;
  686. EditorGUI.indentLevel = 0;
  687. rect = EditorGUILayout.GetControlRect(false, 24);
  688. GUIStyle glyphPanelStyle = new GUIStyle(EditorStyles.helpBox);
  689. if (GUI.Button(rect, new GUIContent("<b>Glyph Table</b>", "List of glyphs contained in this font asset."), TMP_UIStyleManager.sectionHeader))
  690. UI_PanelState.glyphTablePanel = !UI_PanelState.glyphTablePanel;
  691. GUI.Label(rect, (UI_PanelState.glyphTablePanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  692. if (UI_PanelState.glyphTablePanel)
  693. {
  694. int arraySize = m_GlyphTable_prop.arraySize;
  695. int itemsPerPage = 15;
  696. // Display Glyph Management Tools
  697. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  698. {
  699. // Search Bar implementation
  700. #region DISPLAY SEARCH BAR
  701. EditorGUILayout.BeginHorizontal();
  702. {
  703. EditorGUIUtility.labelWidth = 130f;
  704. EditorGUI.BeginChangeCheck();
  705. string searchPattern = EditorGUILayout.TextField("Glyph Search", m_GlyphSearchPattern, "SearchTextField");
  706. if (EditorGUI.EndChangeCheck() || m_isSearchDirty)
  707. {
  708. if (string.IsNullOrEmpty(searchPattern) == false)
  709. {
  710. m_GlyphSearchPattern = searchPattern;
  711. // Search Glyph Table for potential matches
  712. SearchGlyphTable(m_GlyphSearchPattern, ref m_GlyphSearchList);
  713. }
  714. else
  715. m_GlyphSearchPattern = null;
  716. m_isSearchDirty = false;
  717. }
  718. string styleName = string.IsNullOrEmpty(m_GlyphSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton";
  719. if (GUILayout.Button(GUIContent.none, styleName))
  720. {
  721. GUIUtility.keyboardControl = 0;
  722. m_GlyphSearchPattern = string.Empty;
  723. }
  724. }
  725. EditorGUILayout.EndHorizontal();
  726. #endregion
  727. // Display Page Navigation
  728. if (!string.IsNullOrEmpty(m_GlyphSearchPattern))
  729. arraySize = m_GlyphSearchList.Count;
  730. DisplayPageNavigation(ref m_CurrentGlyphPage, arraySize, itemsPerPage);
  731. }
  732. EditorGUILayout.EndVertical();
  733. // Display Glyph Table Elements
  734. if (arraySize > 0)
  735. {
  736. // Display each GlyphInfo entry using the GlyphInfo property drawer.
  737. for (int i = itemsPerPage * m_CurrentGlyphPage; i < arraySize && i < itemsPerPage * (m_CurrentGlyphPage + 1); i++)
  738. {
  739. // Define the start of the selection region of the element.
  740. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  741. int elementIndex = i;
  742. if (!string.IsNullOrEmpty(m_GlyphSearchPattern))
  743. elementIndex = m_GlyphSearchList[i];
  744. SerializedProperty glyphProperty = m_GlyphTable_prop.GetArrayElementAtIndex(elementIndex);
  745. EditorGUILayout.BeginVertical(glyphPanelStyle);
  746. using (new EditorGUI.DisabledScope(i != m_SelectedGlyphRecord))
  747. {
  748. EditorGUILayout.PropertyField(glyphProperty);
  749. }
  750. EditorGUILayout.EndVertical();
  751. // Define the end of the selection region of the element.
  752. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  753. // Check for Item selection
  754. Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y);
  755. if (DoSelectionCheck(selectionArea))
  756. {
  757. if (m_SelectedGlyphRecord == i)
  758. m_SelectedGlyphRecord = -1;
  759. else
  760. {
  761. m_SelectedGlyphRecord = i;
  762. m_AddGlyphWarning.isEnabled = false;
  763. m_unicodeHexLabel = k_placeholderUnicodeHex;
  764. GUIUtility.keyboardControl = 0;
  765. }
  766. }
  767. // Draw Selection Highlight and Glyph Options
  768. if (m_SelectedGlyphRecord == i)
  769. {
  770. TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255));
  771. // Draw Glyph management options
  772. Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f);
  773. float optionAreaWidth = controlRect.width * 0.6f;
  774. float btnWidth = optionAreaWidth / 3;
  775. Rect position = new Rect(controlRect.x + controlRect.width * .4f, controlRect.y, btnWidth, controlRect.height);
  776. // Copy Selected Glyph to Target Glyph ID
  777. GUI.enabled = !string.IsNullOrEmpty(m_dstGlyphID);
  778. if (GUI.Button(position, new GUIContent("Copy to")))
  779. {
  780. GUIUtility.keyboardControl = 0;
  781. int dstGlyphID;
  782. // Convert Hex Value to Decimal
  783. int.TryParse(m_dstGlyphID, out dstGlyphID);
  784. //Add new glyph at target Unicode hex id.
  785. if (!AddNewGlyph(elementIndex, dstGlyphID))
  786. {
  787. m_AddGlyphWarning.isEnabled = true;
  788. m_AddGlyphWarning.expirationTime = EditorApplication.timeSinceStartup + 1;
  789. }
  790. m_dstGlyphID = string.Empty;
  791. m_isSearchDirty = true;
  792. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset);
  793. }
  794. // Target Glyph ID
  795. GUI.enabled = true;
  796. position.x += btnWidth;
  797. GUI.SetNextControlName("GlyphID_Input");
  798. m_dstGlyphID = EditorGUI.TextField(position, m_dstGlyphID);
  799. // Placeholder text
  800. EditorGUI.LabelField(position, new GUIContent(m_GlyphIDLabel, "The Glyph ID of the duplicated Glyph"), TMP_UIStyleManager.label);
  801. // Only filter the input when the destination glyph ID text field has focus.
  802. if (GUI.GetNameOfFocusedControl() == "GlyphID_Input")
  803. {
  804. m_GlyphIDLabel = string.Empty;
  805. //Filter out unwanted characters.
  806. char chr = Event.current.character;
  807. if ((chr < '0' || chr > '9'))
  808. {
  809. Event.current.character = '\0';
  810. }
  811. }
  812. else
  813. {
  814. m_GlyphIDLabel = k_placeholderGlyphID;
  815. //m_dstGlyphID = string.Empty;
  816. }
  817. // Remove Glyph
  818. position.x += btnWidth;
  819. if (GUI.Button(position, "Remove"))
  820. {
  821. GUIUtility.keyboardControl = 0;
  822. RemoveGlyphFromList(elementIndex);
  823. isAssetDirty = true;
  824. m_SelectedGlyphRecord = -1;
  825. m_isSearchDirty = true;
  826. break;
  827. }
  828. if (m_AddGlyphWarning.isEnabled && EditorApplication.timeSinceStartup < m_AddGlyphWarning.expirationTime)
  829. {
  830. EditorGUILayout.HelpBox("The Destination Glyph ID already exists", MessageType.Warning);
  831. }
  832. }
  833. }
  834. }
  835. DisplayPageNavigation(ref m_CurrentGlyphPage, arraySize, itemsPerPage);
  836. EditorGUILayout.Space();
  837. }
  838. #endregion
  839. // FONT FEATURE TABLE
  840. #region Font Feature Table
  841. EditorGUIUtility.labelWidth = labelWidth;
  842. EditorGUIUtility.fieldWidth = fieldWidth;
  843. EditorGUI.indentLevel = 0;
  844. rect = EditorGUILayout.GetControlRect(false, 24);
  845. if (GUI.Button(rect, new GUIContent("<b>Glyph Adjustment Table</b>", "List of glyph adjustment / advanced kerning pairs."), TMP_UIStyleManager.sectionHeader))
  846. UI_PanelState.fontFeatureTablePanel = !UI_PanelState.fontFeatureTablePanel;
  847. GUI.Label(rect, (UI_PanelState.fontFeatureTablePanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  848. if (UI_PanelState.fontFeatureTablePanel)
  849. {
  850. int arraySize = m_GlyphPairAdjustmentRecords_prop.arraySize;
  851. int itemsPerPage = 20;
  852. // Display Kerning Pair Management Tools
  853. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  854. {
  855. // Search Bar implementation
  856. #region DISPLAY SEARCH BAR
  857. EditorGUILayout.BeginHorizontal();
  858. {
  859. EditorGUIUtility.labelWidth = 150f;
  860. EditorGUI.BeginChangeCheck();
  861. string searchPattern = EditorGUILayout.TextField("Adjustment Pair Search", m_KerningTableSearchPattern, "SearchTextField");
  862. if (EditorGUI.EndChangeCheck() || m_isSearchDirty)
  863. {
  864. if (string.IsNullOrEmpty(searchPattern) == false)
  865. {
  866. m_KerningTableSearchPattern = searchPattern;
  867. // Search Glyph Table for potential matches
  868. SearchKerningTable(m_KerningTableSearchPattern, ref m_KerningTableSearchList);
  869. }
  870. else
  871. m_KerningTableSearchPattern = null;
  872. m_isSearchDirty = false;
  873. }
  874. string styleName = string.IsNullOrEmpty(m_KerningTableSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton";
  875. if (GUILayout.Button(GUIContent.none, styleName))
  876. {
  877. GUIUtility.keyboardControl = 0;
  878. m_KerningTableSearchPattern = string.Empty;
  879. }
  880. }
  881. EditorGUILayout.EndHorizontal();
  882. #endregion
  883. // Display Page Navigation
  884. if (!string.IsNullOrEmpty(m_KerningTableSearchPattern))
  885. arraySize = m_KerningTableSearchList.Count;
  886. DisplayPageNavigation(ref m_CurrentKerningPage, arraySize, itemsPerPage);
  887. }
  888. EditorGUILayout.EndVertical();
  889. if (arraySize > 0)
  890. {
  891. // Display each GlyphInfo entry using the GlyphInfo property drawer.
  892. for (int i = itemsPerPage * m_CurrentKerningPage; i < arraySize && i < itemsPerPage * (m_CurrentKerningPage + 1); i++)
  893. {
  894. // Define the start of the selection region of the element.
  895. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  896. int elementIndex = i;
  897. if (!string.IsNullOrEmpty(m_KerningTableSearchPattern))
  898. elementIndex = m_KerningTableSearchList[i];
  899. SerializedProperty pairAdjustmentRecordProperty = m_GlyphPairAdjustmentRecords_prop.GetArrayElementAtIndex(elementIndex);
  900. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  901. using (new EditorGUI.DisabledScope(i != m_SelectedAdjustmentRecord))
  902. {
  903. EditorGUILayout.PropertyField(pairAdjustmentRecordProperty, new GUIContent("Selectable"));
  904. }
  905. EditorGUILayout.EndVertical();
  906. // Define the end of the selection region of the element.
  907. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  908. // Check for Item selection
  909. Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y);
  910. if (DoSelectionCheck(selectionArea))
  911. {
  912. if (m_SelectedAdjustmentRecord == i)
  913. {
  914. m_SelectedAdjustmentRecord = -1;
  915. }
  916. else
  917. {
  918. m_SelectedAdjustmentRecord = i;
  919. GUIUtility.keyboardControl = 0;
  920. }
  921. }
  922. // Draw Selection Highlight and Kerning Pair Options
  923. if (m_SelectedAdjustmentRecord == i)
  924. {
  925. TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255));
  926. // Draw Glyph management options
  927. Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f);
  928. float optionAreaWidth = controlRect.width;
  929. float btnWidth = optionAreaWidth / 4;
  930. Rect position = new Rect(controlRect.x + controlRect.width - btnWidth, controlRect.y, btnWidth, controlRect.height);
  931. // Remove Kerning pair
  932. GUI.enabled = true;
  933. if (GUI.Button(position, "Remove"))
  934. {
  935. GUIUtility.keyboardControl = 0;
  936. RemoveAdjustmentPairFromList(i);
  937. isAssetDirty = true;
  938. m_SelectedAdjustmentRecord = -1;
  939. m_isSearchDirty = true;
  940. break;
  941. }
  942. }
  943. }
  944. }
  945. DisplayPageNavigation(ref m_CurrentKerningPage, arraySize, itemsPerPage);
  946. GUILayout.Space(5);
  947. // Add new kerning pair
  948. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  949. {
  950. EditorGUILayout.PropertyField(m_EmptyGlyphPairAdjustmentRecord_prop);
  951. }
  952. EditorGUILayout.EndVertical();
  953. if (GUILayout.Button("Add New Glyph Adjustment Record"))
  954. {
  955. SerializedProperty firstAdjustmentRecordProperty = m_EmptyGlyphPairAdjustmentRecord_prop.FindPropertyRelative("m_FirstAdjustmentRecord");
  956. SerializedProperty secondAdjustmentRecordProperty = m_EmptyGlyphPairAdjustmentRecord_prop.FindPropertyRelative("m_SecondAdjustmentRecord");
  957. uint firstGlyphIndex = (uint)firstAdjustmentRecordProperty.FindPropertyRelative("m_GlyphIndex").intValue;
  958. uint secondGlyphIndex = (uint)secondAdjustmentRecordProperty.FindPropertyRelative("m_GlyphIndex").intValue;
  959. TMP_GlyphValueRecord firstValueRecord = GetValueRecord(firstAdjustmentRecordProperty.FindPropertyRelative("m_GlyphValueRecord"));
  960. TMP_GlyphValueRecord secondValueRecord = GetValueRecord(secondAdjustmentRecordProperty.FindPropertyRelative("m_GlyphValueRecord"));
  961. errorCode = -1;
  962. long pairKey = (long)secondGlyphIndex << 32 | firstGlyphIndex;
  963. if (m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.ContainsKey(pairKey) == false)
  964. {
  965. TMP_GlyphPairAdjustmentRecord adjustmentRecord = new TMP_GlyphPairAdjustmentRecord(new TMP_GlyphAdjustmentRecord(firstGlyphIndex, firstValueRecord), new TMP_GlyphAdjustmentRecord(secondGlyphIndex, secondValueRecord));
  966. m_FontFeatureTable.m_GlyphPairAdjustmentRecords.Add(adjustmentRecord);
  967. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(pairKey, adjustmentRecord);
  968. errorCode = 0;
  969. }
  970. TMP_Character character;
  971. // Add glyphs and characters
  972. uint firstCharacter = m_SerializedPropertyHolder.firstCharacter;
  973. if (!m_fontAsset.characterLookupTable.ContainsKey(firstCharacter))
  974. m_fontAsset.TryAddCharacterInternal(firstCharacter, out character);
  975. uint secondCharacter = m_SerializedPropertyHolder.secondCharacter;
  976. if (!m_fontAsset.characterLookupTable.ContainsKey(secondCharacter))
  977. m_fontAsset.TryAddCharacterInternal(secondCharacter, out character);
  978. // Sort Kerning Pairs & Reload Font Asset if new kerning pair was added.
  979. if (errorCode != -1)
  980. {
  981. m_FontFeatureTable.SortGlyphPairAdjustmentRecords();
  982. serializedObject.ApplyModifiedProperties();
  983. isAssetDirty = true;
  984. m_isSearchDirty = true;
  985. }
  986. else
  987. {
  988. timeStamp = System.DateTime.Now.AddSeconds(5);
  989. }
  990. // Clear Add Kerning Pair Panel
  991. // TODO
  992. }
  993. if (errorCode == -1)
  994. {
  995. GUILayout.BeginHorizontal();
  996. GUILayout.FlexibleSpace();
  997. GUILayout.Label("Kerning Pair already <color=#ffff00>exists!</color>", TMP_UIStyleManager.label);
  998. GUILayout.FlexibleSpace();
  999. GUILayout.EndHorizontal();
  1000. if (System.DateTime.Now > timeStamp)
  1001. errorCode = 0;
  1002. }
  1003. }
  1004. #endregion
  1005. if (serializedObject.ApplyModifiedProperties() || evt_cmd == k_UndoRedo || isAssetDirty)
  1006. {
  1007. // Delay callback until user has decided to Apply or Revert the changes.
  1008. if (m_DisplayDestructiveChangeWarning == false)
  1009. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset);
  1010. if (m_fontAsset.m_IsFontAssetLookupTablesDirty || evt_cmd == k_UndoRedo)
  1011. m_fontAsset.ReadFontAssetDefinition();
  1012. isAssetDirty = false;
  1013. EditorUtility.SetDirty(target);
  1014. }
  1015. // Clear selection if mouse event was not consumed.
  1016. GUI.enabled = true;
  1017. if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
  1018. m_SelectedAdjustmentRecord = -1;
  1019. }
  1020. void CleanFallbackFontAssetTable()
  1021. {
  1022. SerializedProperty m_FallbackFontAsseTable = serializedObject.FindProperty("m_FallbackFontAssetTable");
  1023. bool isListDirty = false;
  1024. int elementCount = m_FallbackFontAsseTable.arraySize;
  1025. for (int i = 0; i < elementCount; i++)
  1026. {
  1027. SerializedProperty element = m_FallbackFontAsseTable.GetArrayElementAtIndex(i);
  1028. if (element.objectReferenceValue == null)
  1029. {
  1030. m_FallbackFontAsseTable.DeleteArrayElementAtIndex(i);
  1031. elementCount -= 1;
  1032. i -= 1;
  1033. isListDirty = true;
  1034. }
  1035. }
  1036. if (isListDirty)
  1037. {
  1038. serializedObject.ApplyModifiedProperties();
  1039. serializedObject.Update();
  1040. }
  1041. }
  1042. void SavedAtlasGenerationSettings()
  1043. {
  1044. m_AtlasSettings.glyphRenderMode = (GlyphRenderMode)m_AtlasRenderMode_prop.intValue;
  1045. m_AtlasSettings.pointSize = m_SamplingPointSize_prop.intValue;
  1046. m_AtlasSettings.padding = m_AtlasPadding_prop.intValue;
  1047. m_AtlasSettings.atlasWidth = m_AtlasWidth_prop.intValue;
  1048. m_AtlasSettings.atlasHeight = m_AtlasHeight_prop.intValue;
  1049. }
  1050. void RestoreAtlasGenerationSettings()
  1051. {
  1052. m_AtlasRenderMode_prop.intValue = (int)m_AtlasSettings.glyphRenderMode;
  1053. m_SamplingPointSize_prop.intValue = m_AtlasSettings.pointSize;
  1054. m_AtlasPadding_prop.intValue = m_AtlasSettings.padding;
  1055. m_AtlasWidth_prop.intValue = m_AtlasSettings.atlasWidth;
  1056. m_AtlasHeight_prop.intValue = m_AtlasSettings.atlasHeight;
  1057. }
  1058. void UpdateFontAssetCreationSettings()
  1059. {
  1060. m_fontAsset.m_CreationSettings.pointSize = m_SamplingPointSize_prop.intValue;
  1061. m_fontAsset.m_CreationSettings.renderMode = m_AtlasRenderMode_prop.intValue;
  1062. m_fontAsset.m_CreationSettings.padding = m_AtlasPadding_prop.intValue;
  1063. m_fontAsset.m_CreationSettings.atlasWidth = m_AtlasWidth_prop.intValue;
  1064. m_fontAsset.m_CreationSettings.atlasHeight = m_AtlasHeight_prop.intValue;
  1065. }
  1066. void UpdateCharacterData(SerializedProperty property, int index)
  1067. {
  1068. TMP_Character character = m_fontAsset.characterTable[index];
  1069. character.unicode = (uint)property.FindPropertyRelative("m_Unicode").intValue;
  1070. character.scale = property.FindPropertyRelative("m_Scale").floatValue;
  1071. SerializedProperty glyphProperty = property.FindPropertyRelative("m_Glyph");
  1072. character.glyph.index = (uint)glyphProperty.FindPropertyRelative("m_Index").intValue;
  1073. SerializedProperty glyphRectProperty = glyphProperty.FindPropertyRelative("m_GlyphRect");
  1074. character.glyph.glyphRect = new GlyphRect(glyphRectProperty.FindPropertyRelative("m_X").intValue, glyphRectProperty.FindPropertyRelative("m_Y").intValue, glyphRectProperty.FindPropertyRelative("m_Width").intValue, glyphRectProperty.FindPropertyRelative("m_Height").intValue);
  1075. SerializedProperty glyphMetricsProperty = glyphProperty.FindPropertyRelative("m_Metrics");
  1076. character.glyph.metrics = new GlyphMetrics(glyphMetricsProperty.FindPropertyRelative("m_Width").floatValue, glyphMetricsProperty.FindPropertyRelative("m_Height").floatValue, glyphMetricsProperty.FindPropertyRelative("m_HorizontalBearingX").floatValue, glyphMetricsProperty.FindPropertyRelative("m_HorizontalBearingY").floatValue, glyphMetricsProperty.FindPropertyRelative("m_HorizontalAdvance").floatValue);
  1077. character.glyph.scale = glyphProperty.FindPropertyRelative("m_Scale").floatValue;
  1078. character.glyph.atlasIndex = glyphProperty.FindPropertyRelative("m_AtlasIndex").intValue;
  1079. }
  1080. void UpdateGlyphData(SerializedProperty property, int index)
  1081. {
  1082. Glyph glyph = m_fontAsset.glyphTable[index];
  1083. glyph.index = (uint)property.FindPropertyRelative("m_Index").intValue;
  1084. SerializedProperty glyphRect = property.FindPropertyRelative("m_GlyphRect");
  1085. glyph.glyphRect = new GlyphRect(glyphRect.FindPropertyRelative("m_X").intValue, glyphRect.FindPropertyRelative("m_Y").intValue, glyphRect.FindPropertyRelative("m_Width").intValue, glyphRect.FindPropertyRelative("m_Height").intValue);
  1086. SerializedProperty glyphMetrics = property.FindPropertyRelative("m_Metrics");
  1087. glyph.metrics = new GlyphMetrics(glyphMetrics.FindPropertyRelative("m_Width").floatValue, glyphMetrics.FindPropertyRelative("m_Height").floatValue, glyphMetrics.FindPropertyRelative("m_HorizontalBearingX").floatValue, glyphMetrics.FindPropertyRelative("m_HorizontalBearingY").floatValue, glyphMetrics.FindPropertyRelative("m_HorizontalAdvance").floatValue);
  1088. glyph.scale = property.FindPropertyRelative("m_Scale").floatValue;
  1089. }
  1090. void DisplayPageNavigation(ref int currentPage, int arraySize, int itemsPerPage)
  1091. {
  1092. Rect pagePos = EditorGUILayout.GetControlRect(false, 20);
  1093. pagePos.width /= 3;
  1094. int shiftMultiplier = Event.current.shift ? 10 : 1; // Page + Shift goes 10 page forward
  1095. // Previous Page
  1096. GUI.enabled = currentPage > 0;
  1097. if (GUI.Button(pagePos, "Previous Page"))
  1098. currentPage -= 1 * shiftMultiplier;
  1099. // Page Counter
  1100. GUI.enabled = true;
  1101. pagePos.x += pagePos.width;
  1102. int totalPages = (int)(arraySize / (float)itemsPerPage + 0.999f);
  1103. GUI.Label(pagePos, "Page " + (currentPage + 1) + " / " + totalPages, TMP_UIStyleManager.centeredLabel);
  1104. // Next Page
  1105. pagePos.x += pagePos.width;
  1106. GUI.enabled = itemsPerPage * (currentPage + 1) < arraySize;
  1107. if (GUI.Button(pagePos, "Next Page"))
  1108. currentPage += 1 * shiftMultiplier;
  1109. // Clamp page range
  1110. currentPage = Mathf.Clamp(currentPage, 0, arraySize / itemsPerPage);
  1111. GUI.enabled = true;
  1112. }
  1113. /// <summary>
  1114. ///
  1115. /// </summary>
  1116. /// <param name="srcGlyphID"></param>
  1117. /// <param name="dstGlyphID"></param>
  1118. bool AddNewGlyph(int srcIndex, int dstGlyphID)
  1119. {
  1120. // Make sure Destination Glyph ID doesn't already contain a Glyph
  1121. if (m_fontAsset.glyphLookupTable.ContainsKey((uint)dstGlyphID))
  1122. return false;
  1123. // Add new element to glyph list.
  1124. m_GlyphTable_prop.arraySize += 1;
  1125. // Get a reference to the source glyph.
  1126. SerializedProperty sourceGlyph = m_GlyphTable_prop.GetArrayElementAtIndex(srcIndex);
  1127. int dstIndex = m_GlyphTable_prop.arraySize - 1;
  1128. // Get a reference to the target / destination glyph.
  1129. SerializedProperty targetGlyph = m_GlyphTable_prop.GetArrayElementAtIndex(dstIndex);
  1130. CopyGlyphSerializedProperty(sourceGlyph, ref targetGlyph);
  1131. // Update the ID of the glyph
  1132. targetGlyph.FindPropertyRelative("m_Index").intValue = dstGlyphID;
  1133. serializedObject.ApplyModifiedProperties();
  1134. m_fontAsset.SortGlyphTable();
  1135. m_fontAsset.ReadFontAssetDefinition();
  1136. return true;
  1137. }
  1138. /// <summary>
  1139. ///
  1140. /// </summary>
  1141. /// <param name="glyphID"></param>
  1142. void RemoveGlyphFromList(int index)
  1143. {
  1144. if (index > m_GlyphTable_prop.arraySize)
  1145. return;
  1146. int targetGlyphIndex = m_GlyphTable_prop.GetArrayElementAtIndex(index).FindPropertyRelative("m_Index").intValue;
  1147. m_GlyphTable_prop.DeleteArrayElementAtIndex(index);
  1148. // Remove all characters referencing this glyph.
  1149. for (int i = 0; i < m_CharacterTable_prop.arraySize; i++)
  1150. {
  1151. int glyphIndex = m_CharacterTable_prop.GetArrayElementAtIndex(i).FindPropertyRelative("m_GlyphIndex").intValue;
  1152. if (glyphIndex == targetGlyphIndex)
  1153. {
  1154. // Remove character
  1155. m_CharacterTable_prop.DeleteArrayElementAtIndex(i);
  1156. }
  1157. }
  1158. serializedObject.ApplyModifiedProperties();
  1159. m_fontAsset.ReadFontAssetDefinition();
  1160. }
  1161. bool AddNewCharacter(int srcIndex, int dstGlyphID)
  1162. {
  1163. // Make sure Destination Glyph ID doesn't already contain a Glyph
  1164. if (m_fontAsset.characterLookupTable.ContainsKey((uint)dstGlyphID))
  1165. return false;
  1166. // Add new element to glyph list.
  1167. m_CharacterTable_prop.arraySize += 1;
  1168. // Get a reference to the source glyph.
  1169. SerializedProperty sourceCharacter = m_CharacterTable_prop.GetArrayElementAtIndex(srcIndex);
  1170. int dstIndex = m_CharacterTable_prop.arraySize - 1;
  1171. // Get a reference to the target / destination glyph.
  1172. SerializedProperty targetCharacter = m_CharacterTable_prop.GetArrayElementAtIndex(dstIndex);
  1173. CopyCharacterSerializedProperty(sourceCharacter, ref targetCharacter);
  1174. // Update the ID of the glyph
  1175. targetCharacter.FindPropertyRelative("m_Unicode").intValue = dstGlyphID;
  1176. serializedObject.ApplyModifiedProperties();
  1177. m_fontAsset.SortCharacterTable();
  1178. m_fontAsset.ReadFontAssetDefinition();
  1179. return true;
  1180. }
  1181. void RemoveCharacterFromList(int index)
  1182. {
  1183. if (index > m_CharacterTable_prop.arraySize)
  1184. return;
  1185. m_CharacterTable_prop.DeleteArrayElementAtIndex(index);
  1186. serializedObject.ApplyModifiedProperties();
  1187. m_fontAsset.ReadFontAssetDefinition();
  1188. }
  1189. // Check if any of the Style elements were clicked on.
  1190. private bool DoSelectionCheck(Rect selectionArea)
  1191. {
  1192. Event currentEvent = Event.current;
  1193. switch (currentEvent.type)
  1194. {
  1195. case EventType.MouseDown:
  1196. if (selectionArea.Contains(currentEvent.mousePosition) && currentEvent.button == 0)
  1197. {
  1198. currentEvent.Use();
  1199. return true;
  1200. }
  1201. break;
  1202. }
  1203. return false;
  1204. }
  1205. TMP_GlyphValueRecord GetValueRecord(SerializedProperty property)
  1206. {
  1207. TMP_GlyphValueRecord record = new TMP_GlyphValueRecord();
  1208. record.xPlacement = property.FindPropertyRelative("m_XPlacement").floatValue;
  1209. record.yPlacement = property.FindPropertyRelative("m_YPlacement").floatValue;
  1210. record.xAdvance = property.FindPropertyRelative("m_XAdvance").floatValue;
  1211. record.yAdvance = property.FindPropertyRelative("m_YAdvance").floatValue;
  1212. return record;
  1213. }
  1214. void RemoveAdjustmentPairFromList(int index)
  1215. {
  1216. if (index > m_GlyphPairAdjustmentRecords_prop.arraySize)
  1217. return;
  1218. m_GlyphPairAdjustmentRecords_prop.DeleteArrayElementAtIndex(index);
  1219. serializedObject.ApplyModifiedProperties();
  1220. m_fontAsset.ReadFontAssetDefinition();
  1221. }
  1222. /// <summary>
  1223. ///
  1224. /// </summary>
  1225. /// <param name="srcGlyph"></param>
  1226. /// <param name="dstGlyph"></param>
  1227. void CopyGlyphSerializedProperty(SerializedProperty srcGlyph, ref SerializedProperty dstGlyph)
  1228. {
  1229. // TODO : Should make a generic function which copies each of the properties.
  1230. dstGlyph.FindPropertyRelative("m_Index").intValue = srcGlyph.FindPropertyRelative("m_Index").intValue;
  1231. // Glyph -> GlyphMetrics
  1232. SerializedProperty srcGlyphMetrics = srcGlyph.FindPropertyRelative("m_Metrics");
  1233. SerializedProperty dstGlyphMetrics = dstGlyph.FindPropertyRelative("m_Metrics");
  1234. dstGlyphMetrics.FindPropertyRelative("m_Width").floatValue = srcGlyphMetrics.FindPropertyRelative("m_Width").floatValue;
  1235. dstGlyphMetrics.FindPropertyRelative("m_Height").floatValue = srcGlyphMetrics.FindPropertyRelative("m_Height").floatValue;
  1236. dstGlyphMetrics.FindPropertyRelative("m_HorizontalBearingX").floatValue = srcGlyphMetrics.FindPropertyRelative("m_HorizontalBearingX").floatValue;
  1237. dstGlyphMetrics.FindPropertyRelative("m_HorizontalBearingY").floatValue = srcGlyphMetrics.FindPropertyRelative("m_HorizontalBearingY").floatValue;
  1238. dstGlyphMetrics.FindPropertyRelative("m_HorizontalAdvance").floatValue = srcGlyphMetrics.FindPropertyRelative("m_HorizontalAdvance").floatValue;
  1239. // Glyph -> GlyphRect
  1240. SerializedProperty srcGlyphRect = srcGlyph.FindPropertyRelative("m_GlyphRect");
  1241. SerializedProperty dstGlyphRect = dstGlyph.FindPropertyRelative("m_GlyphRect");
  1242. dstGlyphRect.FindPropertyRelative("m_X").intValue = srcGlyphRect.FindPropertyRelative("m_X").intValue;
  1243. dstGlyphRect.FindPropertyRelative("m_Y").intValue = srcGlyphRect.FindPropertyRelative("m_Y").intValue;
  1244. dstGlyphRect.FindPropertyRelative("m_Width").intValue = srcGlyphRect.FindPropertyRelative("m_Width").intValue;
  1245. dstGlyphRect.FindPropertyRelative("m_Height").intValue = srcGlyphRect.FindPropertyRelative("m_Height").intValue;
  1246. dstGlyph.FindPropertyRelative("m_Scale").floatValue = srcGlyph.FindPropertyRelative("m_Scale").floatValue;
  1247. dstGlyph.FindPropertyRelative("m_AtlasIndex").intValue = srcGlyph.FindPropertyRelative("m_AtlasIndex").intValue;
  1248. }
  1249. void CopyCharacterSerializedProperty(SerializedProperty source, ref SerializedProperty target)
  1250. {
  1251. // TODO : Should make a generic function which copies each of the properties.
  1252. int unicode = source.FindPropertyRelative("m_Unicode").intValue;
  1253. target.FindPropertyRelative("m_Unicode").intValue = unicode;
  1254. int srcGlyphIndex = source.FindPropertyRelative("m_GlyphIndex").intValue;
  1255. target.FindPropertyRelative("m_GlyphIndex").intValue = srcGlyphIndex;
  1256. target.FindPropertyRelative("m_Scale").floatValue = source.FindPropertyRelative("m_Scale").floatValue;
  1257. }
  1258. /// <summary>
  1259. ///
  1260. /// </summary>
  1261. /// <param name="searchPattern"></param>
  1262. /// <returns></returns>
  1263. void SearchGlyphTable (string searchPattern, ref List<int> searchResults)
  1264. {
  1265. if (searchResults == null) searchResults = new List<int>();
  1266. searchResults.Clear();
  1267. int arraySize = m_GlyphTable_prop.arraySize;
  1268. for (int i = 0; i < arraySize; i++)
  1269. {
  1270. SerializedProperty sourceGlyph = m_GlyphTable_prop.GetArrayElementAtIndex(i);
  1271. int id = sourceGlyph.FindPropertyRelative("m_Index").intValue;
  1272. // Check for potential match against a character.
  1273. //if (searchPattern.Length == 1 && id == searchPattern[0])
  1274. // searchResults.Add(i);
  1275. // Check for potential match against decimal id
  1276. if (id.ToString().Contains(searchPattern))
  1277. searchResults.Add(i);
  1278. //if (id.ToString("x").Contains(searchPattern))
  1279. // searchResults.Add(i);
  1280. //if (id.ToString("X").Contains(searchPattern))
  1281. // searchResults.Add(i);
  1282. }
  1283. }
  1284. void SearchCharacterTable(string searchPattern, ref List<int> searchResults)
  1285. {
  1286. if (searchResults == null) searchResults = new List<int>();
  1287. searchResults.Clear();
  1288. int arraySize = m_CharacterTable_prop.arraySize;
  1289. for (int i = 0; i < arraySize; i++)
  1290. {
  1291. SerializedProperty sourceCharacter = m_CharacterTable_prop.GetArrayElementAtIndex(i);
  1292. int id = sourceCharacter.FindPropertyRelative("m_Unicode").intValue;
  1293. // Check for potential match against a character.
  1294. if (searchPattern.Length == 1 && id == searchPattern[0])
  1295. searchResults.Add(i);
  1296. else if (id.ToString("x").Contains(searchPattern))
  1297. searchResults.Add(i);
  1298. else if (id.ToString("X").Contains(searchPattern))
  1299. searchResults.Add(i);
  1300. // Check for potential match against decimal id
  1301. //if (id.ToString().Contains(searchPattern))
  1302. // searchResults.Add(i);
  1303. }
  1304. }
  1305. void SearchKerningTable(string searchPattern, ref List<int> searchResults)
  1306. {
  1307. if (searchResults == null) searchResults = new List<int>();
  1308. searchResults.Clear();
  1309. // Lookup glyph index of potential characters contained in the search pattern.
  1310. uint firstGlyphIndex = 0;
  1311. TMP_Character firstCharacterSearch;
  1312. if (searchPattern.Length > 0 && m_fontAsset.characterLookupTable.TryGetValue(searchPattern[0], out firstCharacterSearch))
  1313. firstGlyphIndex = firstCharacterSearch.glyphIndex;
  1314. uint secondGlyphIndex = 0;
  1315. TMP_Character secondCharacterSearch;
  1316. if (searchPattern.Length > 1 && m_fontAsset.characterLookupTable.TryGetValue(searchPattern[1], out secondCharacterSearch))
  1317. secondGlyphIndex = secondCharacterSearch.glyphIndex;
  1318. int arraySize = m_GlyphPairAdjustmentRecords_prop.arraySize;
  1319. for (int i = 0; i < arraySize; i++)
  1320. {
  1321. SerializedProperty record = m_GlyphPairAdjustmentRecords_prop.GetArrayElementAtIndex(i);
  1322. SerializedProperty firstAdjustmentRecord = record.FindPropertyRelative("m_FirstAdjustmentRecord");
  1323. SerializedProperty secondAdjustmentRecord = record.FindPropertyRelative("m_SecondAdjustmentRecord");
  1324. int firstGlyph = firstAdjustmentRecord.FindPropertyRelative("m_GlyphIndex").intValue;
  1325. int secondGlyph = secondAdjustmentRecord.FindPropertyRelative("m_GlyphIndex").intValue;
  1326. if (firstGlyphIndex == firstGlyph && secondGlyphIndex == secondGlyph)
  1327. searchResults.Add(i);
  1328. else if (searchPattern.Length == 1 && (firstGlyphIndex == firstGlyph || firstGlyphIndex == secondGlyph))
  1329. searchResults.Add(i);
  1330. else if (firstGlyph.ToString().Contains(searchPattern))
  1331. searchResults.Add(i);
  1332. else if (secondGlyph.ToString().Contains(searchPattern))
  1333. searchResults.Add(i);
  1334. }
  1335. }
  1336. }
  1337. }