TMP_FontAssetEditor.cs 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  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. // Convert Hex Value to Decimal
  782. int.TryParse(m_dstGlyphID, out int dstGlyphID);
  783. //Add new glyph at target Unicode hex id.
  784. if (!AddNewGlyph(elementIndex, dstGlyphID))
  785. {
  786. m_AddGlyphWarning.isEnabled = true;
  787. m_AddGlyphWarning.expirationTime = EditorApplication.timeSinceStartup + 1;
  788. }
  789. m_dstGlyphID = string.Empty;
  790. m_isSearchDirty = true;
  791. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset);
  792. }
  793. // Target Glyph ID
  794. GUI.enabled = true;
  795. position.x += btnWidth;
  796. GUI.SetNextControlName("GlyphID_Input");
  797. m_dstGlyphID = EditorGUI.TextField(position, m_dstGlyphID);
  798. // Placeholder text
  799. EditorGUI.LabelField(position, new GUIContent(m_GlyphIDLabel, "The Glyph ID of the duplicated Glyph"), TMP_UIStyleManager.label);
  800. // Only filter the input when the destination glyph ID text field has focus.
  801. if (GUI.GetNameOfFocusedControl() == "GlyphID_Input")
  802. {
  803. m_GlyphIDLabel = string.Empty;
  804. //Filter out unwanted characters.
  805. char chr = Event.current.character;
  806. if ((chr < '0' || chr > '9'))
  807. {
  808. Event.current.character = '\0';
  809. }
  810. }
  811. else
  812. {
  813. m_GlyphIDLabel = k_placeholderGlyphID;
  814. //m_dstGlyphID = string.Empty;
  815. }
  816. // Remove Glyph
  817. position.x += btnWidth;
  818. if (GUI.Button(position, "Remove"))
  819. {
  820. GUIUtility.keyboardControl = 0;
  821. RemoveGlyphFromList(elementIndex);
  822. isAssetDirty = true;
  823. m_SelectedGlyphRecord = -1;
  824. m_isSearchDirty = true;
  825. break;
  826. }
  827. if (m_AddGlyphWarning.isEnabled && EditorApplication.timeSinceStartup < m_AddGlyphWarning.expirationTime)
  828. {
  829. EditorGUILayout.HelpBox("The Destination Glyph ID already exists", MessageType.Warning);
  830. }
  831. }
  832. }
  833. }
  834. DisplayPageNavigation(ref m_CurrentGlyphPage, arraySize, itemsPerPage);
  835. EditorGUILayout.Space();
  836. }
  837. #endregion
  838. // FONT FEATURE TABLE
  839. #region Font Feature Table
  840. EditorGUIUtility.labelWidth = labelWidth;
  841. EditorGUIUtility.fieldWidth = fieldWidth;
  842. EditorGUI.indentLevel = 0;
  843. rect = EditorGUILayout.GetControlRect(false, 24);
  844. if (GUI.Button(rect, new GUIContent("<b>Glyph Adjustment Table</b>", "List of glyph adjustment / advanced kerning pairs."), TMP_UIStyleManager.sectionHeader))
  845. UI_PanelState.fontFeatureTablePanel = !UI_PanelState.fontFeatureTablePanel;
  846. GUI.Label(rect, (UI_PanelState.fontFeatureTablePanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  847. if (UI_PanelState.fontFeatureTablePanel)
  848. {
  849. int arraySize = m_GlyphPairAdjustmentRecords_prop.arraySize;
  850. int itemsPerPage = 20;
  851. // Display Kerning Pair Management Tools
  852. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  853. {
  854. // Search Bar implementation
  855. #region DISPLAY SEARCH BAR
  856. EditorGUILayout.BeginHorizontal();
  857. {
  858. EditorGUIUtility.labelWidth = 150f;
  859. EditorGUI.BeginChangeCheck();
  860. string searchPattern = EditorGUILayout.TextField("Adjustment Pair Search", m_KerningTableSearchPattern, "SearchTextField");
  861. if (EditorGUI.EndChangeCheck() || m_isSearchDirty)
  862. {
  863. if (string.IsNullOrEmpty(searchPattern) == false)
  864. {
  865. m_KerningTableSearchPattern = searchPattern;
  866. // Search Glyph Table for potential matches
  867. SearchKerningTable(m_KerningTableSearchPattern, ref m_KerningTableSearchList);
  868. }
  869. else
  870. m_KerningTableSearchPattern = null;
  871. m_isSearchDirty = false;
  872. }
  873. string styleName = string.IsNullOrEmpty(m_KerningTableSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton";
  874. if (GUILayout.Button(GUIContent.none, styleName))
  875. {
  876. GUIUtility.keyboardControl = 0;
  877. m_KerningTableSearchPattern = string.Empty;
  878. }
  879. }
  880. EditorGUILayout.EndHorizontal();
  881. #endregion
  882. // Display Page Navigation
  883. if (!string.IsNullOrEmpty(m_KerningTableSearchPattern))
  884. arraySize = m_KerningTableSearchList.Count;
  885. DisplayPageNavigation(ref m_CurrentKerningPage, arraySize, itemsPerPage);
  886. }
  887. EditorGUILayout.EndVertical();
  888. if (arraySize > 0)
  889. {
  890. // Display each GlyphInfo entry using the GlyphInfo property drawer.
  891. for (int i = itemsPerPage * m_CurrentKerningPage; i < arraySize && i < itemsPerPage * (m_CurrentKerningPage + 1); i++)
  892. {
  893. // Define the start of the selection region of the element.
  894. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  895. int elementIndex = i;
  896. if (!string.IsNullOrEmpty(m_KerningTableSearchPattern))
  897. elementIndex = m_KerningTableSearchList[i];
  898. SerializedProperty pairAdjustmentRecordProperty = m_GlyphPairAdjustmentRecords_prop.GetArrayElementAtIndex(elementIndex);
  899. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  900. using (new EditorGUI.DisabledScope(i != m_SelectedAdjustmentRecord))
  901. {
  902. EditorGUILayout.PropertyField(pairAdjustmentRecordProperty, new GUIContent("Selectable"));
  903. }
  904. EditorGUILayout.EndVertical();
  905. // Define the end of the selection region of the element.
  906. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  907. // Check for Item selection
  908. Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y);
  909. if (DoSelectionCheck(selectionArea))
  910. {
  911. if (m_SelectedAdjustmentRecord == i)
  912. {
  913. m_SelectedAdjustmentRecord = -1;
  914. }
  915. else
  916. {
  917. m_SelectedAdjustmentRecord = i;
  918. GUIUtility.keyboardControl = 0;
  919. }
  920. }
  921. // Draw Selection Highlight and Kerning Pair Options
  922. if (m_SelectedAdjustmentRecord == i)
  923. {
  924. TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255));
  925. // Draw Glyph management options
  926. Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f);
  927. float optionAreaWidth = controlRect.width;
  928. float btnWidth = optionAreaWidth / 4;
  929. Rect position = new Rect(controlRect.x + controlRect.width - btnWidth, controlRect.y, btnWidth, controlRect.height);
  930. // Remove Kerning pair
  931. GUI.enabled = true;
  932. if (GUI.Button(position, "Remove"))
  933. {
  934. GUIUtility.keyboardControl = 0;
  935. RemoveAdjustmentPairFromList(i);
  936. isAssetDirty = true;
  937. m_SelectedAdjustmentRecord = -1;
  938. m_isSearchDirty = true;
  939. break;
  940. }
  941. }
  942. }
  943. }
  944. DisplayPageNavigation(ref m_CurrentKerningPage, arraySize, itemsPerPage);
  945. GUILayout.Space(5);
  946. // Add new kerning pair
  947. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  948. {
  949. EditorGUILayout.PropertyField(m_EmptyGlyphPairAdjustmentRecord_prop);
  950. }
  951. EditorGUILayout.EndVertical();
  952. if (GUILayout.Button("Add New Glyph Adjustment Record"))
  953. {
  954. SerializedProperty firstAdjustmentRecordProperty = m_EmptyGlyphPairAdjustmentRecord_prop.FindPropertyRelative("m_FirstAdjustmentRecord");
  955. SerializedProperty secondAdjustmentRecordProperty = m_EmptyGlyphPairAdjustmentRecord_prop.FindPropertyRelative("m_SecondAdjustmentRecord");
  956. uint firstGlyphIndex = (uint)firstAdjustmentRecordProperty.FindPropertyRelative("m_GlyphIndex").intValue;
  957. uint secondGlyphIndex = (uint)secondAdjustmentRecordProperty.FindPropertyRelative("m_GlyphIndex").intValue;
  958. TMP_GlyphValueRecord firstValueRecord = GetValueRecord(firstAdjustmentRecordProperty.FindPropertyRelative("m_GlyphValueRecord"));
  959. TMP_GlyphValueRecord secondValueRecord = GetValueRecord(secondAdjustmentRecordProperty.FindPropertyRelative("m_GlyphValueRecord"));
  960. errorCode = -1;
  961. long pairKey = (long)secondGlyphIndex << 32 | firstGlyphIndex;
  962. if (m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.ContainsKey(pairKey) == false)
  963. {
  964. TMP_GlyphPairAdjustmentRecord adjustmentRecord = new TMP_GlyphPairAdjustmentRecord(new TMP_GlyphAdjustmentRecord(firstGlyphIndex, firstValueRecord), new TMP_GlyphAdjustmentRecord(secondGlyphIndex, secondValueRecord));
  965. m_FontFeatureTable.m_GlyphPairAdjustmentRecords.Add(adjustmentRecord);
  966. m_FontFeatureTable.m_GlyphPairAdjustmentRecordLookupDictionary.Add(pairKey, adjustmentRecord);
  967. errorCode = 0;
  968. }
  969. // Add glyphs and characters
  970. uint firstCharacter = m_SerializedPropertyHolder.firstCharacter;
  971. if (!m_fontAsset.characterLookupTable.ContainsKey(firstCharacter))
  972. m_fontAsset.TryAddCharacterInternal(firstCharacter, out TMP_Character character);
  973. uint secondCharacter = m_SerializedPropertyHolder.secondCharacter;
  974. if (!m_fontAsset.characterLookupTable.ContainsKey(secondCharacter))
  975. m_fontAsset.TryAddCharacterInternal(secondCharacter, out TMP_Character character);
  976. // Sort Kerning Pairs & Reload Font Asset if new kerning pair was added.
  977. if (errorCode != -1)
  978. {
  979. m_FontFeatureTable.SortGlyphPairAdjustmentRecords();
  980. serializedObject.ApplyModifiedProperties();
  981. isAssetDirty = true;
  982. m_isSearchDirty = true;
  983. }
  984. else
  985. {
  986. timeStamp = System.DateTime.Now.AddSeconds(5);
  987. }
  988. // Clear Add Kerning Pair Panel
  989. // TODO
  990. }
  991. if (errorCode == -1)
  992. {
  993. GUILayout.BeginHorizontal();
  994. GUILayout.FlexibleSpace();
  995. GUILayout.Label("Kerning Pair already <color=#ffff00>exists!</color>", TMP_UIStyleManager.label);
  996. GUILayout.FlexibleSpace();
  997. GUILayout.EndHorizontal();
  998. if (System.DateTime.Now > timeStamp)
  999. errorCode = 0;
  1000. }
  1001. }
  1002. #endregion
  1003. if (serializedObject.ApplyModifiedProperties() || evt_cmd == k_UndoRedo || isAssetDirty)
  1004. {
  1005. // Delay callback until user has decided to Apply or Revert the changes.
  1006. if (m_DisplayDestructiveChangeWarning == false)
  1007. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, m_fontAsset);
  1008. if (m_fontAsset.m_IsFontAssetLookupTablesDirty || evt_cmd == k_UndoRedo)
  1009. m_fontAsset.ReadFontAssetDefinition();
  1010. isAssetDirty = false;
  1011. EditorUtility.SetDirty(target);
  1012. }
  1013. // Clear selection if mouse event was not consumed.
  1014. GUI.enabled = true;
  1015. if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
  1016. m_SelectedAdjustmentRecord = -1;
  1017. }
  1018. void CleanFallbackFontAssetTable()
  1019. {
  1020. SerializedProperty m_FallbackFontAsseTable = serializedObject.FindProperty("m_FallbackFontAssetTable");
  1021. bool isListDirty = false;
  1022. int elementCount = m_FallbackFontAsseTable.arraySize;
  1023. for (int i = 0; i < elementCount; i++)
  1024. {
  1025. SerializedProperty element = m_FallbackFontAsseTable.GetArrayElementAtIndex(i);
  1026. if (element.objectReferenceValue == null)
  1027. {
  1028. m_FallbackFontAsseTable.DeleteArrayElementAtIndex(i);
  1029. elementCount -= 1;
  1030. i -= 1;
  1031. isListDirty = true;
  1032. }
  1033. }
  1034. if (isListDirty)
  1035. {
  1036. serializedObject.ApplyModifiedProperties();
  1037. serializedObject.Update();
  1038. }
  1039. }
  1040. void SavedAtlasGenerationSettings()
  1041. {
  1042. m_AtlasSettings.glyphRenderMode = (GlyphRenderMode)m_AtlasRenderMode_prop.intValue;
  1043. m_AtlasSettings.pointSize = m_SamplingPointSize_prop.intValue;
  1044. m_AtlasSettings.padding = m_AtlasPadding_prop.intValue;
  1045. m_AtlasSettings.atlasWidth = m_AtlasWidth_prop.intValue;
  1046. m_AtlasSettings.atlasHeight = m_AtlasHeight_prop.intValue;
  1047. }
  1048. void RestoreAtlasGenerationSettings()
  1049. {
  1050. m_AtlasRenderMode_prop.intValue = (int)m_AtlasSettings.glyphRenderMode;
  1051. m_SamplingPointSize_prop.intValue = m_AtlasSettings.pointSize;
  1052. m_AtlasPadding_prop.intValue = m_AtlasSettings.padding;
  1053. m_AtlasWidth_prop.intValue = m_AtlasSettings.atlasWidth;
  1054. m_AtlasHeight_prop.intValue = m_AtlasSettings.atlasHeight;
  1055. }
  1056. void UpdateFontAssetCreationSettings()
  1057. {
  1058. m_fontAsset.m_CreationSettings.pointSize = m_SamplingPointSize_prop.intValue;
  1059. m_fontAsset.m_CreationSettings.renderMode = m_AtlasRenderMode_prop.intValue;
  1060. m_fontAsset.m_CreationSettings.padding = m_AtlasPadding_prop.intValue;
  1061. m_fontAsset.m_CreationSettings.atlasWidth = m_AtlasWidth_prop.intValue;
  1062. m_fontAsset.m_CreationSettings.atlasHeight = m_AtlasHeight_prop.intValue;
  1063. }
  1064. void UpdateCharacterData(SerializedProperty property, int index)
  1065. {
  1066. TMP_Character character = m_fontAsset.characterTable[index];
  1067. character.unicode = (uint)property.FindPropertyRelative("m_Unicode").intValue;
  1068. character.scale = property.FindPropertyRelative("m_Scale").floatValue;
  1069. SerializedProperty glyphProperty = property.FindPropertyRelative("m_Glyph");
  1070. character.glyph.index = (uint)glyphProperty.FindPropertyRelative("m_Index").intValue;
  1071. SerializedProperty glyphRectProperty = glyphProperty.FindPropertyRelative("m_GlyphRect");
  1072. 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);
  1073. SerializedProperty glyphMetricsProperty = glyphProperty.FindPropertyRelative("m_Metrics");
  1074. 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);
  1075. character.glyph.scale = glyphProperty.FindPropertyRelative("m_Scale").floatValue;
  1076. character.glyph.atlasIndex = glyphProperty.FindPropertyRelative("m_AtlasIndex").intValue;
  1077. }
  1078. void UpdateGlyphData(SerializedProperty property, int index)
  1079. {
  1080. Glyph glyph = m_fontAsset.glyphTable[index];
  1081. glyph.index = (uint)property.FindPropertyRelative("m_Index").intValue;
  1082. SerializedProperty glyphRect = property.FindPropertyRelative("m_GlyphRect");
  1083. glyph.glyphRect = new GlyphRect(glyphRect.FindPropertyRelative("m_X").intValue, glyphRect.FindPropertyRelative("m_Y").intValue, glyphRect.FindPropertyRelative("m_Width").intValue, glyphRect.FindPropertyRelative("m_Height").intValue);
  1084. SerializedProperty glyphMetrics = property.FindPropertyRelative("m_Metrics");
  1085. 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);
  1086. glyph.scale = property.FindPropertyRelative("m_Scale").floatValue;
  1087. }
  1088. void DisplayPageNavigation(ref int currentPage, int arraySize, int itemsPerPage)
  1089. {
  1090. Rect pagePos = EditorGUILayout.GetControlRect(false, 20);
  1091. pagePos.width /= 3;
  1092. int shiftMultiplier = Event.current.shift ? 10 : 1; // Page + Shift goes 10 page forward
  1093. // Previous Page
  1094. GUI.enabled = currentPage > 0;
  1095. if (GUI.Button(pagePos, "Previous Page"))
  1096. currentPage -= 1 * shiftMultiplier;
  1097. // Page Counter
  1098. GUI.enabled = true;
  1099. pagePos.x += pagePos.width;
  1100. int totalPages = (int)(arraySize / (float)itemsPerPage + 0.999f);
  1101. GUI.Label(pagePos, "Page " + (currentPage + 1) + " / " + totalPages, TMP_UIStyleManager.centeredLabel);
  1102. // Next Page
  1103. pagePos.x += pagePos.width;
  1104. GUI.enabled = itemsPerPage * (currentPage + 1) < arraySize;
  1105. if (GUI.Button(pagePos, "Next Page"))
  1106. currentPage += 1 * shiftMultiplier;
  1107. // Clamp page range
  1108. currentPage = Mathf.Clamp(currentPage, 0, arraySize / itemsPerPage);
  1109. GUI.enabled = true;
  1110. }
  1111. /// <summary>
  1112. ///
  1113. /// </summary>
  1114. /// <param name="srcGlyphID"></param>
  1115. /// <param name="dstGlyphID"></param>
  1116. bool AddNewGlyph(int srcIndex, int dstGlyphID)
  1117. {
  1118. // Make sure Destination Glyph ID doesn't already contain a Glyph
  1119. if (m_fontAsset.glyphLookupTable.ContainsKey((uint)dstGlyphID))
  1120. return false;
  1121. // Add new element to glyph list.
  1122. m_GlyphTable_prop.arraySize += 1;
  1123. // Get a reference to the source glyph.
  1124. SerializedProperty sourceGlyph = m_GlyphTable_prop.GetArrayElementAtIndex(srcIndex);
  1125. int dstIndex = m_GlyphTable_prop.arraySize - 1;
  1126. // Get a reference to the target / destination glyph.
  1127. SerializedProperty targetGlyph = m_GlyphTable_prop.GetArrayElementAtIndex(dstIndex);
  1128. CopyGlyphSerializedProperty(sourceGlyph, ref targetGlyph);
  1129. // Update the ID of the glyph
  1130. targetGlyph.FindPropertyRelative("m_Index").intValue = dstGlyphID;
  1131. serializedObject.ApplyModifiedProperties();
  1132. m_fontAsset.SortGlyphTable();
  1133. m_fontAsset.ReadFontAssetDefinition();
  1134. return true;
  1135. }
  1136. /// <summary>
  1137. ///
  1138. /// </summary>
  1139. /// <param name="glyphID"></param>
  1140. void RemoveGlyphFromList(int index)
  1141. {
  1142. if (index > m_GlyphTable_prop.arraySize)
  1143. return;
  1144. int targetGlyphIndex = m_GlyphTable_prop.GetArrayElementAtIndex(index).FindPropertyRelative("m_Index").intValue;
  1145. m_GlyphTable_prop.DeleteArrayElementAtIndex(index);
  1146. // Remove all characters referencing this glyph.
  1147. for (int i = 0; i < m_CharacterTable_prop.arraySize; i++)
  1148. {
  1149. int glyphIndex = m_CharacterTable_prop.GetArrayElementAtIndex(i).FindPropertyRelative("m_GlyphIndex").intValue;
  1150. if (glyphIndex == targetGlyphIndex)
  1151. {
  1152. // Remove character
  1153. m_CharacterTable_prop.DeleteArrayElementAtIndex(i);
  1154. }
  1155. }
  1156. serializedObject.ApplyModifiedProperties();
  1157. m_fontAsset.ReadFontAssetDefinition();
  1158. }
  1159. bool AddNewCharacter(int srcIndex, int dstGlyphID)
  1160. {
  1161. // Make sure Destination Glyph ID doesn't already contain a Glyph
  1162. if (m_fontAsset.characterLookupTable.ContainsKey((uint)dstGlyphID))
  1163. return false;
  1164. // Add new element to glyph list.
  1165. m_CharacterTable_prop.arraySize += 1;
  1166. // Get a reference to the source glyph.
  1167. SerializedProperty sourceCharacter = m_CharacterTable_prop.GetArrayElementAtIndex(srcIndex);
  1168. int dstIndex = m_CharacterTable_prop.arraySize - 1;
  1169. // Get a reference to the target / destination glyph.
  1170. SerializedProperty targetCharacter = m_CharacterTable_prop.GetArrayElementAtIndex(dstIndex);
  1171. CopyCharacterSerializedProperty(sourceCharacter, ref targetCharacter);
  1172. // Update the ID of the glyph
  1173. targetCharacter.FindPropertyRelative("m_Unicode").intValue = dstGlyphID;
  1174. serializedObject.ApplyModifiedProperties();
  1175. m_fontAsset.SortCharacterTable();
  1176. m_fontAsset.ReadFontAssetDefinition();
  1177. return true;
  1178. }
  1179. void RemoveCharacterFromList(int index)
  1180. {
  1181. if (index > m_CharacterTable_prop.arraySize)
  1182. return;
  1183. m_CharacterTable_prop.DeleteArrayElementAtIndex(index);
  1184. serializedObject.ApplyModifiedProperties();
  1185. m_fontAsset.ReadFontAssetDefinition();
  1186. }
  1187. // Check if any of the Style elements were clicked on.
  1188. private bool DoSelectionCheck(Rect selectionArea)
  1189. {
  1190. Event currentEvent = Event.current;
  1191. switch (currentEvent.type)
  1192. {
  1193. case EventType.MouseDown:
  1194. if (selectionArea.Contains(currentEvent.mousePosition) && currentEvent.button == 0)
  1195. {
  1196. currentEvent.Use();
  1197. return true;
  1198. }
  1199. break;
  1200. }
  1201. return false;
  1202. }
  1203. TMP_GlyphValueRecord GetValueRecord(SerializedProperty property)
  1204. {
  1205. TMP_GlyphValueRecord record = new TMP_GlyphValueRecord();
  1206. record.xPlacement = property.FindPropertyRelative("m_XPlacement").floatValue;
  1207. record.yPlacement = property.FindPropertyRelative("m_YPlacement").floatValue;
  1208. record.xAdvance = property.FindPropertyRelative("m_XAdvance").floatValue;
  1209. record.yAdvance = property.FindPropertyRelative("m_YAdvance").floatValue;
  1210. return record;
  1211. }
  1212. void RemoveAdjustmentPairFromList(int index)
  1213. {
  1214. if (index > m_GlyphPairAdjustmentRecords_prop.arraySize)
  1215. return;
  1216. m_GlyphPairAdjustmentRecords_prop.DeleteArrayElementAtIndex(index);
  1217. serializedObject.ApplyModifiedProperties();
  1218. m_fontAsset.ReadFontAssetDefinition();
  1219. }
  1220. /// <summary>
  1221. ///
  1222. /// </summary>
  1223. /// <param name="srcGlyph"></param>
  1224. /// <param name="dstGlyph"></param>
  1225. void CopyGlyphSerializedProperty(SerializedProperty srcGlyph, ref SerializedProperty dstGlyph)
  1226. {
  1227. // TODO : Should make a generic function which copies each of the properties.
  1228. dstGlyph.FindPropertyRelative("m_Index").intValue = srcGlyph.FindPropertyRelative("m_Index").intValue;
  1229. // Glyph -> GlyphMetrics
  1230. SerializedProperty srcGlyphMetrics = srcGlyph.FindPropertyRelative("m_Metrics");
  1231. SerializedProperty dstGlyphMetrics = dstGlyph.FindPropertyRelative("m_Metrics");
  1232. dstGlyphMetrics.FindPropertyRelative("m_Width").floatValue = srcGlyphMetrics.FindPropertyRelative("m_Width").floatValue;
  1233. dstGlyphMetrics.FindPropertyRelative("m_Height").floatValue = srcGlyphMetrics.FindPropertyRelative("m_Height").floatValue;
  1234. dstGlyphMetrics.FindPropertyRelative("m_HorizontalBearingX").floatValue = srcGlyphMetrics.FindPropertyRelative("m_HorizontalBearingX").floatValue;
  1235. dstGlyphMetrics.FindPropertyRelative("m_HorizontalBearingY").floatValue = srcGlyphMetrics.FindPropertyRelative("m_HorizontalBearingY").floatValue;
  1236. dstGlyphMetrics.FindPropertyRelative("m_HorizontalAdvance").floatValue = srcGlyphMetrics.FindPropertyRelative("m_HorizontalAdvance").floatValue;
  1237. // Glyph -> GlyphRect
  1238. SerializedProperty srcGlyphRect = srcGlyph.FindPropertyRelative("m_GlyphRect");
  1239. SerializedProperty dstGlyphRect = dstGlyph.FindPropertyRelative("m_GlyphRect");
  1240. dstGlyphRect.FindPropertyRelative("m_X").intValue = srcGlyphRect.FindPropertyRelative("m_X").intValue;
  1241. dstGlyphRect.FindPropertyRelative("m_Y").intValue = srcGlyphRect.FindPropertyRelative("m_Y").intValue;
  1242. dstGlyphRect.FindPropertyRelative("m_Width").intValue = srcGlyphRect.FindPropertyRelative("m_Width").intValue;
  1243. dstGlyphRect.FindPropertyRelative("m_Height").intValue = srcGlyphRect.FindPropertyRelative("m_Height").intValue;
  1244. dstGlyph.FindPropertyRelative("m_Scale").floatValue = srcGlyph.FindPropertyRelative("m_Scale").floatValue;
  1245. dstGlyph.FindPropertyRelative("m_AtlasIndex").intValue = srcGlyph.FindPropertyRelative("m_AtlasIndex").intValue;
  1246. }
  1247. void CopyCharacterSerializedProperty(SerializedProperty source, ref SerializedProperty target)
  1248. {
  1249. // TODO : Should make a generic function which copies each of the properties.
  1250. int unicode = source.FindPropertyRelative("m_Unicode").intValue;
  1251. target.FindPropertyRelative("m_Unicode").intValue = unicode;
  1252. int srcGlyphIndex = source.FindPropertyRelative("m_GlyphIndex").intValue;
  1253. target.FindPropertyRelative("m_GlyphIndex").intValue = srcGlyphIndex;
  1254. target.FindPropertyRelative("m_Scale").floatValue = source.FindPropertyRelative("m_Scale").floatValue;
  1255. }
  1256. /// <summary>
  1257. ///
  1258. /// </summary>
  1259. /// <param name="searchPattern"></param>
  1260. /// <returns></returns>
  1261. void SearchGlyphTable (string searchPattern, ref List<int> searchResults)
  1262. {
  1263. if (searchResults == null) searchResults = new List<int>();
  1264. searchResults.Clear();
  1265. int arraySize = m_GlyphTable_prop.arraySize;
  1266. for (int i = 0; i < arraySize; i++)
  1267. {
  1268. SerializedProperty sourceGlyph = m_GlyphTable_prop.GetArrayElementAtIndex(i);
  1269. int id = sourceGlyph.FindPropertyRelative("m_Index").intValue;
  1270. // Check for potential match against a character.
  1271. //if (searchPattern.Length == 1 && id == searchPattern[0])
  1272. // searchResults.Add(i);
  1273. // Check for potential match against decimal id
  1274. if (id.ToString().Contains(searchPattern))
  1275. searchResults.Add(i);
  1276. //if (id.ToString("x").Contains(searchPattern))
  1277. // searchResults.Add(i);
  1278. //if (id.ToString("X").Contains(searchPattern))
  1279. // searchResults.Add(i);
  1280. }
  1281. }
  1282. void SearchCharacterTable(string searchPattern, ref List<int> searchResults)
  1283. {
  1284. if (searchResults == null) searchResults = new List<int>();
  1285. searchResults.Clear();
  1286. int arraySize = m_CharacterTable_prop.arraySize;
  1287. for (int i = 0; i < arraySize; i++)
  1288. {
  1289. SerializedProperty sourceCharacter = m_CharacterTable_prop.GetArrayElementAtIndex(i);
  1290. int id = sourceCharacter.FindPropertyRelative("m_Unicode").intValue;
  1291. // Check for potential match against a character.
  1292. if (searchPattern.Length == 1 && id == searchPattern[0])
  1293. searchResults.Add(i);
  1294. else if (id.ToString("x").Contains(searchPattern))
  1295. searchResults.Add(i);
  1296. else if (id.ToString("X").Contains(searchPattern))
  1297. searchResults.Add(i);
  1298. // Check for potential match against decimal id
  1299. //if (id.ToString().Contains(searchPattern))
  1300. // searchResults.Add(i);
  1301. }
  1302. }
  1303. void SearchKerningTable(string searchPattern, ref List<int> searchResults)
  1304. {
  1305. if (searchResults == null) searchResults = new List<int>();
  1306. searchResults.Clear();
  1307. // Lookup glyph index of potential characters contained in the search pattern.
  1308. uint firstGlyphIndex = 0;
  1309. if (searchPattern.Length > 0 && m_fontAsset.characterLookupTable.TryGetValue(searchPattern[0], out TMP_Character firstCharacterSearch))
  1310. firstGlyphIndex = firstCharacterSearch.glyphIndex;
  1311. uint secondGlyphIndex = 0;
  1312. if (searchPattern.Length > 1 && m_fontAsset.characterLookupTable.TryGetValue(searchPattern[1], out TMP_Character secondCharacterSearch))
  1313. secondGlyphIndex = secondCharacterSearch.glyphIndex;
  1314. int arraySize = m_GlyphPairAdjustmentRecords_prop.arraySize;
  1315. for (int i = 0; i < arraySize; i++)
  1316. {
  1317. SerializedProperty record = m_GlyphPairAdjustmentRecords_prop.GetArrayElementAtIndex(i);
  1318. SerializedProperty firstAdjustmentRecord = record.FindPropertyRelative("m_FirstAdjustmentRecord");
  1319. SerializedProperty secondAdjustmentRecord = record.FindPropertyRelative("m_SecondAdjustmentRecord");
  1320. int firstGlyph = firstAdjustmentRecord.FindPropertyRelative("m_GlyphIndex").intValue;
  1321. int secondGlyph = secondAdjustmentRecord.FindPropertyRelative("m_GlyphIndex").intValue;
  1322. if (firstGlyphIndex == firstGlyph && secondGlyphIndex == secondGlyph)
  1323. searchResults.Add(i);
  1324. else if (searchPattern.Length == 1 && (firstGlyphIndex == firstGlyph || firstGlyphIndex == secondGlyph))
  1325. searchResults.Add(i);
  1326. else if (firstGlyph.ToString().Contains(searchPattern))
  1327. searchResults.Add(i);
  1328. else if (secondGlyph.ToString().Contains(searchPattern))
  1329. searchResults.Add(i);
  1330. }
  1331. }
  1332. }
  1333. }