TMP_SpriteAssetEditor.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. using UnityEngine;
  2. using UnityEngine.TextCore;
  3. using UnityEditor;
  4. using UnityEditorInternal;
  5. using System.Collections.Generic;
  6. namespace TMPro.EditorUtilities
  7. {
  8. [CustomEditor(typeof(TMP_SpriteAsset))]
  9. public class TMP_SpriteAssetEditor : Editor
  10. {
  11. struct UI_PanelState
  12. {
  13. public static bool spriteAssetInfoPanel = true;
  14. public static bool fallbackSpriteAssetPanel = true;
  15. public static bool spriteCharacterTablePanel;
  16. public static bool spriteGlyphTablePanel;
  17. }
  18. private static string[] s_UiStateLabel = new string[] { "<i>(Click to collapse)</i> ", "<i>(Click to expand)</i> " };
  19. int m_moveToIndex;
  20. int m_selectedElement = -1;
  21. int m_CurrentCharacterPage;
  22. int m_CurrentGlyphPage;
  23. const string k_UndoRedo = "UndoRedoPerformed";
  24. string m_CharacterSearchPattern;
  25. List<int> m_CharacterSearchList;
  26. bool m_IsCharacterSearchDirty;
  27. string m_GlyphSearchPattern;
  28. List<int> m_GlyphSearchList;
  29. bool m_IsGlyphSearchDirty;
  30. SerializedProperty m_spriteAtlas_prop;
  31. SerializedProperty m_material_prop;
  32. SerializedProperty m_SpriteCharacterTableProperty;
  33. SerializedProperty m_SpriteGlyphTableProperty;
  34. ReorderableList m_fallbackSpriteAssetList;
  35. TMP_SpriteAsset m_SpriteAsset;
  36. bool isAssetDirty;
  37. float m_xOffset;
  38. float m_yOffset;
  39. float m_xAdvance;
  40. float m_scale;
  41. public void OnEnable()
  42. {
  43. m_SpriteAsset = target as TMP_SpriteAsset;
  44. m_spriteAtlas_prop = serializedObject.FindProperty("spriteSheet");
  45. m_material_prop = serializedObject.FindProperty("material");
  46. m_SpriteCharacterTableProperty = serializedObject.FindProperty("m_SpriteCharacterTable");
  47. m_SpriteGlyphTableProperty = serializedObject.FindProperty("m_SpriteGlyphTable");
  48. // Fallback TMP Sprite Asset list
  49. m_fallbackSpriteAssetList = new ReorderableList(serializedObject, serializedObject.FindProperty("fallbackSpriteAssets"), true, true, true, true);
  50. m_fallbackSpriteAssetList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
  51. {
  52. var element = m_fallbackSpriteAssetList.serializedProperty.GetArrayElementAtIndex(index);
  53. rect.y += 2;
  54. EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, GUIContent.none);
  55. };
  56. m_fallbackSpriteAssetList.drawHeaderCallback = rect =>
  57. {
  58. EditorGUI.LabelField(rect, new GUIContent("Fallback Sprite Asset List", "Select the Sprite Assets that will be searched and used as fallback when a given sprite is missing from this sprite asset."));
  59. };
  60. }
  61. public override void OnInspectorGUI()
  62. {
  63. //Debug.Log("OnInspectorGUI Called.");
  64. Event currentEvent = Event.current;
  65. string evt_cmd = currentEvent.commandName; // Get Current Event CommandName to check for Undo Events
  66. serializedObject.Update();
  67. Rect rect;
  68. // TEXTMESHPRO SPRITE INFO PANEL
  69. GUILayout.Label("Sprite Info", EditorStyles.boldLabel);
  70. EditorGUI.indentLevel = 1;
  71. EditorGUI.BeginChangeCheck();
  72. EditorGUILayout.PropertyField(m_spriteAtlas_prop , new GUIContent("Sprite Atlas"));
  73. if (EditorGUI.EndChangeCheck())
  74. {
  75. // Assign the new sprite atlas texture to the current material
  76. Texture2D tex = m_spriteAtlas_prop.objectReferenceValue as Texture2D;
  77. if (tex != null)
  78. {
  79. Material mat = m_material_prop.objectReferenceValue as Material;
  80. if (mat != null)
  81. mat.mainTexture = tex;
  82. }
  83. }
  84. EditorGUILayout.PropertyField(m_material_prop, new GUIContent("Default Material"));
  85. EditorGUILayout.Space();
  86. // FALLBACK SPRITE ASSETS
  87. EditorGUI.indentLevel = 0;
  88. UI_PanelState.fallbackSpriteAssetPanel = EditorGUILayout.Foldout(UI_PanelState.fallbackSpriteAssetPanel, new GUIContent("Fallback Sprite Assets", "Select the Sprite Assets that will be searched and used as fallback when a given sprite is missing from this sprite asset."), true, TMP_UIStyleManager.boldFoldout);
  89. if (UI_PanelState.fallbackSpriteAssetPanel)
  90. {
  91. m_fallbackSpriteAssetList.DoLayoutList();
  92. }
  93. // SPRITE CHARACTER TABLE
  94. #region Display Sprite Character Table
  95. EditorGUI.indentLevel = 0;
  96. rect = EditorGUILayout.GetControlRect(false, 24);
  97. if (GUI.Button(rect, new GUIContent("<b>Sprite Character Table</b>", "List of sprite characters contained in this sprite asset."), TMP_UIStyleManager.sectionHeader))
  98. UI_PanelState.spriteCharacterTablePanel = !UI_PanelState.spriteCharacterTablePanel;
  99. GUI.Label(rect, (UI_PanelState.spriteCharacterTablePanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  100. if (UI_PanelState.spriteCharacterTablePanel)
  101. {
  102. int arraySize = m_SpriteCharacterTableProperty.arraySize;
  103. int itemsPerPage = 10;
  104. // Display Glyph Management Tools
  105. EditorGUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.ExpandWidth(true));
  106. {
  107. // Search Bar implementation
  108. #region DISPLAY SEARCH BAR
  109. EditorGUILayout.BeginHorizontal();
  110. {
  111. EditorGUIUtility.labelWidth = 110f;
  112. EditorGUI.BeginChangeCheck();
  113. string searchPattern = EditorGUILayout.TextField("Sprite Search", m_CharacterSearchPattern, "SearchTextField");
  114. if (EditorGUI.EndChangeCheck() || m_IsCharacterSearchDirty)
  115. {
  116. if (string.IsNullOrEmpty(searchPattern) == false)
  117. {
  118. //GUIUtility.keyboardControl = 0;
  119. m_CharacterSearchPattern = searchPattern.ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim();
  120. // Search Glyph Table for potential matches
  121. SearchCharacterTable(m_CharacterSearchPattern, ref m_CharacterSearchList);
  122. }
  123. else
  124. m_CharacterSearchPattern = null;
  125. m_IsCharacterSearchDirty = false;
  126. }
  127. string styleName = string.IsNullOrEmpty(m_CharacterSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton";
  128. if (GUILayout.Button(GUIContent.none, styleName))
  129. {
  130. GUIUtility.keyboardControl = 0;
  131. m_CharacterSearchPattern = string.Empty;
  132. }
  133. }
  134. EditorGUILayout.EndHorizontal();
  135. #endregion
  136. // Display Page Navigation
  137. if (!string.IsNullOrEmpty(m_CharacterSearchPattern))
  138. arraySize = m_CharacterSearchList.Count;
  139. // Display Page Navigation
  140. DisplayPageNavigation(ref m_CurrentCharacterPage, arraySize, itemsPerPage);
  141. }
  142. EditorGUILayout.EndVertical();
  143. if (arraySize > 0)
  144. {
  145. // Display each SpriteInfo entry using the SpriteInfo property drawer.
  146. for (int i = itemsPerPage * m_CurrentCharacterPage; i < arraySize && i < itemsPerPage * (m_CurrentCharacterPage + 1); i++)
  147. {
  148. // Define the start of the selection region of the element.
  149. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  150. int elementIndex = i;
  151. if (!string.IsNullOrEmpty(m_CharacterSearchPattern))
  152. elementIndex = m_CharacterSearchList[i];
  153. SerializedProperty spriteCharacterProperty = m_SpriteCharacterTableProperty.GetArrayElementAtIndex(elementIndex);
  154. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  155. {
  156. EditorGUI.BeginDisabledGroup(i != m_selectedElement);
  157. {
  158. EditorGUILayout.PropertyField(spriteCharacterProperty);
  159. }
  160. EditorGUI.EndDisabledGroup();
  161. }
  162. EditorGUILayout.EndVertical();
  163. // Define the end of the selection region of the element.
  164. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  165. // Check for Item selection
  166. Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y);
  167. if (DoSelectionCheck(selectionArea))
  168. {
  169. if (m_selectedElement == i)
  170. {
  171. m_selectedElement = -1;
  172. }
  173. else
  174. {
  175. m_selectedElement = i;
  176. GUIUtility.keyboardControl = 0;
  177. }
  178. }
  179. // Draw & Handle Section Area
  180. if (m_selectedElement == i)
  181. {
  182. // Draw selection highlight
  183. TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255));
  184. // Draw options to MoveUp, MoveDown, Add or Remove Sprites
  185. Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f);
  186. controlRect.width /= 8;
  187. // Move sprite up.
  188. bool guiEnabled = GUI.enabled;
  189. if (i == 0) { GUI.enabled = false; }
  190. if (GUI.Button(controlRect, "Up"))
  191. {
  192. SwapCharacterElements(i, i - 1);
  193. }
  194. GUI.enabled = guiEnabled;
  195. // Move sprite down.
  196. controlRect.x += controlRect.width;
  197. if (i == arraySize - 1) { GUI.enabled = false; }
  198. if (GUI.Button(controlRect, "Down"))
  199. {
  200. SwapCharacterElements(i, i + 1);
  201. }
  202. GUI.enabled = guiEnabled;
  203. // Move sprite to new index
  204. controlRect.x += controlRect.width * 2;
  205. //if (i == arraySize - 1) { GUI.enabled = false; }
  206. m_moveToIndex = EditorGUI.IntField(controlRect, m_moveToIndex);
  207. controlRect.x -= controlRect.width;
  208. if (GUI.Button(controlRect, "Goto"))
  209. {
  210. MoveCharacterToIndex(i, m_moveToIndex);
  211. }
  212. //controlRect.x += controlRect.width;
  213. GUI.enabled = guiEnabled;
  214. // Add new Sprite
  215. controlRect.x += controlRect.width * 4;
  216. if (GUI.Button(controlRect, "+"))
  217. {
  218. m_SpriteCharacterTableProperty.arraySize += 1;
  219. int index = m_SpriteCharacterTableProperty.arraySize - 1;
  220. SerializedProperty spriteInfo_prop = m_SpriteCharacterTableProperty.GetArrayElementAtIndex(index);
  221. // Copy properties of the selected element
  222. CopyCharacterSerializedProperty(m_SpriteCharacterTableProperty.GetArrayElementAtIndex(elementIndex), ref spriteInfo_prop);
  223. //spriteInfo_prop.FindPropertyRelative("m_Index").intValue = index;
  224. serializedObject.ApplyModifiedProperties();
  225. m_IsCharacterSearchDirty = true;
  226. }
  227. // Delete selected Sprite
  228. controlRect.x += controlRect.width;
  229. if (m_selectedElement == -1) GUI.enabled = false;
  230. if (GUI.Button(controlRect, "-"))
  231. {
  232. m_SpriteCharacterTableProperty.DeleteArrayElementAtIndex(elementIndex);
  233. m_selectedElement = -1;
  234. serializedObject.ApplyModifiedProperties();
  235. m_IsCharacterSearchDirty = true;
  236. return;
  237. }
  238. }
  239. }
  240. }
  241. DisplayPageNavigation(ref m_CurrentCharacterPage, arraySize, itemsPerPage);
  242. EditorGUIUtility.labelWidth = 40f;
  243. EditorGUIUtility.fieldWidth = 20f;
  244. GUILayout.Space(5f);
  245. // GLOBAL TOOLS
  246. #region Global Tools
  247. /*
  248. GUI.enabled = true;
  249. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  250. rect = EditorGUILayout.GetControlRect(false, 40);
  251. float width = (rect.width - 75f) / 4;
  252. EditorGUI.LabelField(rect, "Global Offsets & Scale", EditorStyles.boldLabel);
  253. rect.x += 70;
  254. bool old_ChangedState = GUI.changed;
  255. GUI.changed = false;
  256. m_xOffset = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 0, rect.y + 20, width - 5f, 18), new GUIContent("OX:"), m_xOffset);
  257. if (GUI.changed) UpdateGlobalProperty("m_HorizontalBearingX", m_xOffset);
  258. m_yOffset = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 1, rect.y + 20, width - 5f, 18), new GUIContent("OY:"), m_yOffset);
  259. if (GUI.changed) UpdateGlobalProperty("m_HorizontalBearingY", m_yOffset);
  260. m_xAdvance = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 2, rect.y + 20, width - 5f, 18), new GUIContent("ADV."), m_xAdvance);
  261. if (GUI.changed) UpdateGlobalProperty("m_HorizontalAdvance", m_xAdvance);
  262. m_scale = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 3, rect.y + 20, width - 5f, 18), new GUIContent("SF."), m_scale);
  263. if (GUI.changed) UpdateGlobalProperty("m_Scale", m_scale);
  264. EditorGUILayout.EndVertical();
  265. GUI.changed = old_ChangedState;
  266. */
  267. #endregion
  268. }
  269. #endregion
  270. // SPRITE GLYPH TABLE
  271. #region Display Sprite Glyph Table
  272. EditorGUI.indentLevel = 0;
  273. rect = EditorGUILayout.GetControlRect(false, 24);
  274. if (GUI.Button(rect, new GUIContent("<b>Sprite Glyph Table</b>", "A list of the SpriteGlyphs contained in this sprite asset."), TMP_UIStyleManager.sectionHeader))
  275. UI_PanelState.spriteGlyphTablePanel = !UI_PanelState.spriteGlyphTablePanel;
  276. GUI.Label(rect, (UI_PanelState.spriteGlyphTablePanel ? "" : s_UiStateLabel[1]), TMP_UIStyleManager.rightLabel);
  277. if (UI_PanelState.spriteGlyphTablePanel)
  278. {
  279. int arraySize = m_SpriteGlyphTableProperty.arraySize;
  280. int itemsPerPage = 10;
  281. // Display Glyph Management Tools
  282. EditorGUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.ExpandWidth(true));
  283. {
  284. // Search Bar implementation
  285. #region DISPLAY SEARCH BAR
  286. EditorGUILayout.BeginHorizontal();
  287. {
  288. EditorGUIUtility.labelWidth = 110f;
  289. EditorGUI.BeginChangeCheck();
  290. string searchPattern = EditorGUILayout.TextField("Sprite Search", m_GlyphSearchPattern, "SearchTextField");
  291. if (EditorGUI.EndChangeCheck() || m_IsGlyphSearchDirty)
  292. {
  293. if (string.IsNullOrEmpty(searchPattern) == false)
  294. {
  295. //GUIUtility.keyboardControl = 0;
  296. m_GlyphSearchPattern = searchPattern.ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim();
  297. // Search Glyph Table for potential matches
  298. SearchCharacterTable(m_GlyphSearchPattern, ref m_GlyphSearchList);
  299. }
  300. else
  301. m_GlyphSearchPattern = null;
  302. m_IsGlyphSearchDirty = false;
  303. }
  304. string styleName = string.IsNullOrEmpty(m_GlyphSearchPattern) ? "SearchCancelButtonEmpty" : "SearchCancelButton";
  305. if (GUILayout.Button(GUIContent.none, styleName))
  306. {
  307. GUIUtility.keyboardControl = 0;
  308. m_GlyphSearchPattern = string.Empty;
  309. }
  310. }
  311. EditorGUILayout.EndHorizontal();
  312. #endregion
  313. // Display Page Navigation
  314. if (!string.IsNullOrEmpty(m_GlyphSearchPattern))
  315. arraySize = m_GlyphSearchList.Count;
  316. // Display Page Navigation
  317. DisplayPageNavigation(ref m_CurrentGlyphPage, arraySize, itemsPerPage);
  318. }
  319. EditorGUILayout.EndVertical();
  320. if (arraySize > 0)
  321. {
  322. // Display each SpriteInfo entry using the SpriteInfo property drawer.
  323. for (int i = itemsPerPage * m_CurrentGlyphPage; i < arraySize && i < itemsPerPage * (m_CurrentGlyphPage + 1); i++)
  324. {
  325. // Define the start of the selection region of the element.
  326. Rect elementStartRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  327. int elementIndex = i;
  328. if (!string.IsNullOrEmpty(m_GlyphSearchPattern))
  329. elementIndex = m_GlyphSearchList[i];
  330. SerializedProperty spriteGlyphProperty = m_SpriteGlyphTableProperty.GetArrayElementAtIndex(elementIndex);
  331. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  332. {
  333. EditorGUI.BeginDisabledGroup(i != m_selectedElement);
  334. {
  335. EditorGUILayout.PropertyField(spriteGlyphProperty);
  336. }
  337. EditorGUI.EndDisabledGroup();
  338. }
  339. EditorGUILayout.EndVertical();
  340. // Define the end of the selection region of the element.
  341. Rect elementEndRegion = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  342. // Check for Item selection
  343. Rect selectionArea = new Rect(elementStartRegion.x, elementStartRegion.y, elementEndRegion.width, elementEndRegion.y - elementStartRegion.y);
  344. if (DoSelectionCheck(selectionArea))
  345. {
  346. if (m_selectedElement == i)
  347. {
  348. m_selectedElement = -1;
  349. }
  350. else
  351. {
  352. m_selectedElement = i;
  353. GUIUtility.keyboardControl = 0;
  354. }
  355. }
  356. // Draw & Handle Section Area
  357. if (m_selectedElement == i)
  358. {
  359. // Draw selection highlight
  360. TMP_EditorUtility.DrawBox(selectionArea, 2f, new Color32(40, 192, 255, 255));
  361. // Draw options to MoveUp, MoveDown, Add or Remove Sprites
  362. Rect controlRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * 1f);
  363. controlRect.width /= 8;
  364. // Move sprite up.
  365. bool guiEnabled = GUI.enabled;
  366. if (i == 0) { GUI.enabled = false; }
  367. if (GUI.Button(controlRect, "Up"))
  368. {
  369. SwapGlyphElements(i, i - 1);
  370. }
  371. GUI.enabled = guiEnabled;
  372. // Move sprite down.
  373. controlRect.x += controlRect.width;
  374. if (i == arraySize - 1) { GUI.enabled = false; }
  375. if (GUI.Button(controlRect, "Down"))
  376. {
  377. SwapGlyphElements(i, i + 1);
  378. }
  379. GUI.enabled = guiEnabled;
  380. // Move sprite to new index
  381. controlRect.x += controlRect.width * 2;
  382. //if (i == arraySize - 1) { GUI.enabled = false; }
  383. m_moveToIndex = EditorGUI.IntField(controlRect, m_moveToIndex);
  384. controlRect.x -= controlRect.width;
  385. if (GUI.Button(controlRect, "Goto"))
  386. {
  387. MoveGlyphToIndex(i, m_moveToIndex);
  388. }
  389. //controlRect.x += controlRect.width;
  390. GUI.enabled = guiEnabled;
  391. // Add new Sprite
  392. controlRect.x += controlRect.width * 4;
  393. if (GUI.Button(controlRect, "+"))
  394. {
  395. m_SpriteGlyphTableProperty.arraySize += 1;
  396. int index = m_SpriteGlyphTableProperty.arraySize - 1;
  397. SerializedProperty newSpriteGlyphProperty = m_SpriteGlyphTableProperty.GetArrayElementAtIndex(index);
  398. // Copy properties of the selected element
  399. CopyGlyphSerializedProperty(m_SpriteGlyphTableProperty.GetArrayElementAtIndex(elementIndex), ref newSpriteGlyphProperty);
  400. newSpriteGlyphProperty.FindPropertyRelative("m_Index").intValue = index;
  401. serializedObject.ApplyModifiedProperties();
  402. m_IsGlyphSearchDirty = true;
  403. //m_SpriteAsset.UpdateLookupTables();
  404. }
  405. // Delete selected Sprite
  406. controlRect.x += controlRect.width;
  407. if (m_selectedElement == -1) GUI.enabled = false;
  408. if (GUI.Button(controlRect, "-"))
  409. {
  410. SerializedProperty selectedSpriteGlyphProperty = m_SpriteGlyphTableProperty.GetArrayElementAtIndex(elementIndex);
  411. int selectedGlyphIndex = selectedSpriteGlyphProperty.FindPropertyRelative("m_Index").intValue;
  412. m_SpriteGlyphTableProperty.DeleteArrayElementAtIndex(elementIndex);
  413. // Remove all Sprite Characters referencing this glyph.
  414. for (int j = 0; j < m_SpriteCharacterTableProperty.arraySize; j++)
  415. {
  416. int glyphIndex = m_SpriteCharacterTableProperty.GetArrayElementAtIndex(j).FindPropertyRelative("m_GlyphIndex").intValue;
  417. if (glyphIndex == selectedGlyphIndex)
  418. {
  419. // Remove character
  420. m_SpriteCharacterTableProperty.DeleteArrayElementAtIndex(j);
  421. }
  422. }
  423. m_selectedElement = -1;
  424. serializedObject.ApplyModifiedProperties();
  425. m_IsGlyphSearchDirty = true;
  426. //m_SpriteAsset.UpdateLookupTables();
  427. return;
  428. }
  429. }
  430. }
  431. }
  432. DisplayPageNavigation(ref m_CurrentGlyphPage, arraySize, itemsPerPage);
  433. EditorGUIUtility.labelWidth = 40f;
  434. EditorGUIUtility.fieldWidth = 20f;
  435. GUILayout.Space(5f);
  436. // GLOBAL TOOLS
  437. #region Global Tools
  438. GUI.enabled = true;
  439. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  440. rect = EditorGUILayout.GetControlRect(false, 40);
  441. float width = (rect.width - 75f) / 4;
  442. EditorGUI.LabelField(rect, "Global Offsets & Scale", EditorStyles.boldLabel);
  443. rect.x += 70;
  444. bool old_ChangedState = GUI.changed;
  445. GUI.changed = false;
  446. m_xOffset = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 0, rect.y + 20, width - 5f, 18), new GUIContent("OX:"), m_xOffset);
  447. if (GUI.changed) UpdateGlobalProperty("m_HorizontalBearingX", m_xOffset);
  448. m_yOffset = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 1, rect.y + 20, width - 5f, 18), new GUIContent("OY:"), m_yOffset);
  449. if (GUI.changed) UpdateGlobalProperty("m_HorizontalBearingY", m_yOffset);
  450. m_xAdvance = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 2, rect.y + 20, width - 5f, 18), new GUIContent("ADV."), m_xAdvance);
  451. if (GUI.changed) UpdateGlobalProperty("m_HorizontalAdvance", m_xAdvance);
  452. m_scale = EditorGUI.FloatField(new Rect(rect.x + 5f + width * 3, rect.y + 20, width - 5f, 18), new GUIContent("SF."), m_scale);
  453. if (GUI.changed) UpdateGlobalProperty("m_Scale", m_scale);
  454. EditorGUILayout.EndVertical();
  455. #endregion
  456. GUI.changed = old_ChangedState;
  457. }
  458. #endregion
  459. if (serializedObject.ApplyModifiedProperties() || evt_cmd == k_UndoRedo || isAssetDirty)
  460. {
  461. if (m_SpriteAsset.m_IsSpriteAssetLookupTablesDirty || evt_cmd == k_UndoRedo)
  462. m_SpriteAsset.UpdateLookupTables();
  463. TMPro_EventManager.ON_SPRITE_ASSET_PROPERTY_CHANGED(true, m_SpriteAsset);
  464. isAssetDirty = false;
  465. EditorUtility.SetDirty(target);
  466. }
  467. // Clear selection if mouse event was not consumed.
  468. GUI.enabled = true;
  469. if (currentEvent.type == EventType.MouseDown && currentEvent.button == 0)
  470. m_selectedElement = -1;
  471. }
  472. /// <summary>
  473. ///
  474. /// </summary>
  475. /// <param name="arraySize"></param>
  476. /// <param name="itemsPerPage"></param>
  477. void DisplayPageNavigation(ref int currentPage, int arraySize, int itemsPerPage)
  478. {
  479. Rect pagePos = EditorGUILayout.GetControlRect(false, 20);
  480. pagePos.width /= 3;
  481. int shiftMultiplier = Event.current.shift ? 10 : 1; // Page + Shift goes 10 page forward
  482. // Previous Page
  483. GUI.enabled = currentPage > 0;
  484. if (GUI.Button(pagePos, "Previous Page"))
  485. {
  486. currentPage -= 1 * shiftMultiplier;
  487. //m_isNewPage = true;
  488. }
  489. // Page Counter
  490. GUI.enabled = true;
  491. pagePos.x += pagePos.width;
  492. int totalPages = (int)(arraySize / (float)itemsPerPage + 0.999f);
  493. GUI.Label(pagePos, "Page " + (currentPage + 1) + " / " + totalPages, TMP_UIStyleManager.centeredLabel);
  494. // Next Page
  495. pagePos.x += pagePos.width;
  496. GUI.enabled = itemsPerPage * (currentPage + 1) < arraySize;
  497. if (GUI.Button(pagePos, "Next Page"))
  498. {
  499. currentPage += 1 * shiftMultiplier;
  500. //m_isNewPage = true;
  501. }
  502. // Clamp page range
  503. currentPage = Mathf.Clamp(currentPage, 0, arraySize / itemsPerPage);
  504. GUI.enabled = true;
  505. }
  506. /// <summary>
  507. /// Method to update the properties of all sprites
  508. /// </summary>
  509. /// <param name="property"></param>
  510. /// <param name="value"></param>
  511. void UpdateGlobalProperty(string property, float value)
  512. {
  513. int arraySize = m_SpriteGlyphTableProperty.arraySize;
  514. for (int i = 0; i < arraySize; i++)
  515. {
  516. // Get a reference to the sprite glyph.
  517. SerializedProperty spriteGlyphProperty = m_SpriteGlyphTableProperty.GetArrayElementAtIndex(i);
  518. if (property == "m_Scale")
  519. {
  520. spriteGlyphProperty.FindPropertyRelative(property).floatValue = value;
  521. }
  522. else
  523. {
  524. SerializedProperty glyphMetricsProperty = spriteGlyphProperty.FindPropertyRelative("m_Metrics");
  525. glyphMetricsProperty.FindPropertyRelative(property).floatValue = value;
  526. }
  527. }
  528. GUI.changed = false;
  529. }
  530. // Check if any of the Style elements were clicked on.
  531. private bool DoSelectionCheck(Rect selectionArea)
  532. {
  533. Event currentEvent = Event.current;
  534. switch (currentEvent.type)
  535. {
  536. case EventType.MouseDown:
  537. if (selectionArea.Contains(currentEvent.mousePosition) && currentEvent.button == 0)
  538. {
  539. currentEvent.Use();
  540. return true;
  541. }
  542. break;
  543. }
  544. return false;
  545. }
  546. /// <summary>
  547. /// Swap the sprite item at the currently selected array index to another index.
  548. /// </summary>
  549. /// <param name="selectedIndex">Selected index.</param>
  550. /// <param name="newIndex">New index.</param>
  551. void SwapCharacterElements(int selectedIndex, int newIndex)
  552. {
  553. m_SpriteCharacterTableProperty.MoveArrayElement(selectedIndex, newIndex);
  554. m_selectedElement = newIndex;
  555. m_IsCharacterSearchDirty = true;
  556. m_SpriteAsset.m_IsSpriteAssetLookupTablesDirty = true;
  557. }
  558. /// <summary>
  559. /// Move Sprite Element at selected index to another index and reorder sprite list.
  560. /// </summary>
  561. /// <param name="selectedIndex"></param>
  562. /// <param name="newIndex"></param>
  563. void MoveCharacterToIndex(int selectedIndex, int newIndex)
  564. {
  565. int arraySize = m_SpriteCharacterTableProperty.arraySize;
  566. if (newIndex >= arraySize)
  567. newIndex = arraySize - 1;
  568. m_SpriteCharacterTableProperty.MoveArrayElement(selectedIndex, newIndex);
  569. m_selectedElement = newIndex;
  570. m_IsCharacterSearchDirty = true;
  571. m_SpriteAsset.m_IsSpriteAssetLookupTablesDirty = true;
  572. // TODO: Need to handle switching pages if the character or glyph is moved to a different page.
  573. }
  574. /// <summary>
  575. ///
  576. /// </summary>
  577. /// <param name="selectedIndex"></param>
  578. /// <param name="newIndex"></param>
  579. void SwapGlyphElements(int selectedIndex, int newIndex)
  580. {
  581. m_SpriteGlyphTableProperty.MoveArrayElement(selectedIndex, newIndex);
  582. m_selectedElement = newIndex;
  583. m_IsGlyphSearchDirty = true;
  584. m_SpriteAsset.m_IsSpriteAssetLookupTablesDirty = true;
  585. }
  586. /// <summary>
  587. /// Move Sprite Element at selected index to another index and reorder sprite list.
  588. /// </summary>
  589. /// <param name="selectedIndex"></param>
  590. /// <param name="newIndex"></param>
  591. void MoveGlyphToIndex(int selectedIndex, int newIndex)
  592. {
  593. int arraySize = m_SpriteGlyphTableProperty.arraySize;
  594. if (newIndex >= arraySize)
  595. newIndex = arraySize - 1;
  596. m_SpriteGlyphTableProperty.MoveArrayElement(selectedIndex, newIndex);
  597. m_selectedElement = newIndex;
  598. m_IsGlyphSearchDirty = true;
  599. m_SpriteAsset.m_IsSpriteAssetLookupTablesDirty = true;
  600. // TODO: Need to handle switching pages if the character or glyph is moved to a different page.
  601. }
  602. /// <summary>
  603. ///
  604. /// </summary>
  605. /// <param name="source"></param>
  606. /// <param name="target"></param>
  607. void CopyCharacterSerializedProperty(SerializedProperty source, ref SerializedProperty target)
  608. {
  609. target.FindPropertyRelative("m_Name").stringValue = source.FindPropertyRelative("m_Name").stringValue;
  610. target.FindPropertyRelative("m_HashCode").intValue = source.FindPropertyRelative("m_HashCode").intValue;
  611. target.FindPropertyRelative("m_Unicode").intValue = source.FindPropertyRelative("m_Unicode").intValue;
  612. target.FindPropertyRelative("m_GlyphIndex").intValue = source.FindPropertyRelative("m_GlyphIndex").intValue;
  613. target.FindPropertyRelative("m_Scale").floatValue = source.FindPropertyRelative("m_Scale").floatValue;
  614. }
  615. void CopyGlyphSerializedProperty(SerializedProperty srcGlyph, ref SerializedProperty dstGlyph)
  616. {
  617. // TODO : Should make a generic function which copies each of the properties.
  618. // Index
  619. dstGlyph.FindPropertyRelative("m_Index").intValue = srcGlyph.FindPropertyRelative("m_Index").intValue;
  620. // GlyphMetrics
  621. SerializedProperty srcGlyphMetrics = srcGlyph.FindPropertyRelative("m_Metrics");
  622. SerializedProperty dstGlyphMetrics = dstGlyph.FindPropertyRelative("m_Metrics");
  623. dstGlyphMetrics.FindPropertyRelative("m_Width").floatValue = srcGlyphMetrics.FindPropertyRelative("m_Width").floatValue;
  624. dstGlyphMetrics.FindPropertyRelative("m_Height").floatValue = srcGlyphMetrics.FindPropertyRelative("m_Height").floatValue;
  625. dstGlyphMetrics.FindPropertyRelative("m_HorizontalBearingX").floatValue = srcGlyphMetrics.FindPropertyRelative("m_HorizontalBearingX").floatValue;
  626. dstGlyphMetrics.FindPropertyRelative("m_HorizontalBearingY").floatValue = srcGlyphMetrics.FindPropertyRelative("m_HorizontalBearingY").floatValue;
  627. dstGlyphMetrics.FindPropertyRelative("m_HorizontalAdvance").floatValue = srcGlyphMetrics.FindPropertyRelative("m_HorizontalAdvance").floatValue;
  628. // GlyphRect
  629. SerializedProperty srcGlyphRect = srcGlyph.FindPropertyRelative("m_GlyphRect");
  630. SerializedProperty dstGlyphRect = dstGlyph.FindPropertyRelative("m_GlyphRect");
  631. dstGlyphRect.FindPropertyRelative("m_X").intValue = srcGlyphRect.FindPropertyRelative("m_X").intValue;
  632. dstGlyphRect.FindPropertyRelative("m_Y").intValue = srcGlyphRect.FindPropertyRelative("m_Y").intValue;
  633. dstGlyphRect.FindPropertyRelative("m_Width").intValue = srcGlyphRect.FindPropertyRelative("m_Width").intValue;
  634. dstGlyphRect.FindPropertyRelative("m_Height").intValue = srcGlyphRect.FindPropertyRelative("m_Height").intValue;
  635. dstGlyph.FindPropertyRelative("m_Scale").floatValue = srcGlyph.FindPropertyRelative("m_Scale").floatValue;
  636. dstGlyph.FindPropertyRelative("m_AtlasIndex").intValue = srcGlyph.FindPropertyRelative("m_AtlasIndex").intValue;
  637. }
  638. /// <summary>
  639. ///
  640. /// </summary>
  641. /// <param name="searchPattern"></param>
  642. /// <returns></returns>
  643. void SearchCharacterTable(string searchPattern, ref List<int> searchResults)
  644. {
  645. if (searchResults == null) searchResults = new List<int>();
  646. searchResults.Clear();
  647. int arraySize = m_SpriteCharacterTableProperty.arraySize;
  648. for (int i = 0; i < arraySize; i++)
  649. {
  650. SerializedProperty sourceSprite = m_SpriteCharacterTableProperty.GetArrayElementAtIndex(i);
  651. // Check for potential match against array index
  652. if (i.ToString().Contains(searchPattern))
  653. {
  654. searchResults.Add(i);
  655. continue;
  656. }
  657. // Check for potential match against decimal id
  658. int id = sourceSprite.FindPropertyRelative("m_GlyphIndex").intValue;
  659. if (id.ToString().Contains(searchPattern))
  660. {
  661. searchResults.Add(i);
  662. continue;
  663. }
  664. // Check for potential match against name
  665. string name = sourceSprite.FindPropertyRelative("m_Name").stringValue.ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim();
  666. if (name.Contains(searchPattern))
  667. {
  668. searchResults.Add(i);
  669. continue;
  670. }
  671. }
  672. }
  673. void SearchGlyphTable(string searchPattern, ref List<int> searchResults)
  674. {
  675. if (searchResults == null) searchResults = new List<int>();
  676. searchResults.Clear();
  677. int arraySize = m_SpriteGlyphTableProperty.arraySize;
  678. for (int i = 0; i < arraySize; i++)
  679. {
  680. SerializedProperty sourceSprite = m_SpriteGlyphTableProperty.GetArrayElementAtIndex(i);
  681. // Check for potential match against array index
  682. if (i.ToString().Contains(searchPattern))
  683. {
  684. searchResults.Add(i);
  685. continue;
  686. }
  687. // Check for potential match against decimal id
  688. int id = sourceSprite.FindPropertyRelative("m_GlyphIndex").intValue;
  689. if (id.ToString().Contains(searchPattern))
  690. {
  691. searchResults.Add(i);
  692. continue;
  693. }
  694. // Check for potential match against name
  695. string name = sourceSprite.FindPropertyRelative("m_Name").stringValue.ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim();
  696. if (name.Contains(searchPattern))
  697. {
  698. searchResults.Add(i);
  699. continue;
  700. }
  701. }
  702. }
  703. }
  704. }