You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.5 KiB

  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace TMPro
  5. {
  6. [Serializable]
  7. public class TMP_StyleSheet : ScriptableObject
  8. {
  9. private static TMP_StyleSheet s_Instance;
  10. [SerializeField]
  11. private List<TMP_Style> m_StyleList = new List<TMP_Style>(1);
  12. private Dictionary<int, TMP_Style> m_StyleDictionary = new Dictionary<int, TMP_Style>();
  13. /// <summary>
  14. /// Get a singleton instance of the TMP_StyleSheet
  15. /// </summary>
  16. public static TMP_StyleSheet instance
  17. {
  18. get
  19. {
  20. if (s_Instance == null)
  21. {
  22. s_Instance = TMP_Settings.defaultStyleSheet;
  23. if (s_Instance == null)
  24. s_Instance = Resources.Load<TMP_StyleSheet>("Style Sheets/TMP Default Style Sheet");
  25. if (s_Instance == null) return null;
  26. // Load the style dictionary.
  27. s_Instance.LoadStyleDictionaryInternal();
  28. }
  29. return s_Instance;
  30. }
  31. }
  32. /// <summary>
  33. /// Static Function to load the Default Style Sheet.
  34. /// </summary>
  35. /// <returns></returns>
  36. public static TMP_StyleSheet LoadDefaultStyleSheet()
  37. {
  38. return instance;
  39. }
  40. /// <summary>
  41. /// Function to retrieve the Style matching the HashCode.
  42. /// </summary>
  43. /// <param name="hashCode"></param>
  44. /// <returns></returns>
  45. public static TMP_Style GetStyle(int hashCode)
  46. {
  47. return instance.GetStyleInternal(hashCode);
  48. }
  49. /// <summary>
  50. /// Internal method to retrieve the Style matching the Hashcode.
  51. /// </summary>
  52. /// <param name="hashCode"></param>
  53. /// <returns></returns>
  54. private TMP_Style GetStyleInternal(int hashCode)
  55. {
  56. TMP_Style style;
  57. if (m_StyleDictionary.TryGetValue(hashCode, out style))
  58. {
  59. return style;
  60. }
  61. return null;
  62. }
  63. public void UpdateStyleDictionaryKey(int old_key, int new_key)
  64. {
  65. if (m_StyleDictionary.ContainsKey(old_key))
  66. {
  67. TMP_Style style = m_StyleDictionary[old_key];
  68. m_StyleDictionary.Add(new_key, style);
  69. m_StyleDictionary.Remove(old_key);
  70. }
  71. }
  72. /// <summary>
  73. /// Function to update the internal reference to a newly assigned style sheet in the TMP Settings.
  74. /// </summary>
  75. public static void UpdateStyleSheet()
  76. {
  77. // Reset instance
  78. s_Instance = null;
  79. RefreshStyles();
  80. }
  81. /// <summary>
  82. /// Function to refresh the Style Dictionary.
  83. /// </summary>
  84. public static void RefreshStyles()
  85. {
  86. instance.LoadStyleDictionaryInternal();
  87. }
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. private void LoadStyleDictionaryInternal()
  92. {
  93. m_StyleDictionary.Clear();
  94. // Read Styles from style list and store them into dictionary for faster access.
  95. for (int i = 0; i < m_StyleList.Count; i++)
  96. {
  97. m_StyleList[i].RefreshStyle();
  98. if (!m_StyleDictionary.ContainsKey(m_StyleList[i].hashCode))
  99. m_StyleDictionary.Add(m_StyleList[i].hashCode, m_StyleList[i]);
  100. }
  101. }
  102. }
  103. }