TMP_StyleSheet.cs 3.51 KB
Newer Older
BlackAngle233's avatar
BlackAngle233 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using UnityEngine;
using System;
using System.Collections.Generic;


namespace TMPro
{

    [Serializable]
    public class TMP_StyleSheet : ScriptableObject
    {
        private static TMP_StyleSheet s_Instance;

        [SerializeField]
        private List<TMP_Style> m_StyleList = new List<TMP_Style>(1);
        private Dictionary<int, TMP_Style> m_StyleDictionary = new Dictionary<int, TMP_Style>();


        /// <summary>
        /// Get a singleton instance of the TMP_StyleSheet
        /// </summary>
        public static TMP_StyleSheet instance
        {
            get
            {
                if (s_Instance == null)
                {
                    s_Instance = TMP_Settings.defaultStyleSheet;

                    if (s_Instance == null)
                        s_Instance = Resources.Load<TMP_StyleSheet>("Style Sheets/Default Style Sheet");

                    if (s_Instance == null) return null;

                    // Load the style dictionary.
                    s_Instance.LoadStyleDictionaryInternal();
                }

                return s_Instance;
            }
        }


        /// <summary>
        /// Static Function to load the Default Style Sheet.
        /// </summary>
        /// <returns></returns>
        public static TMP_StyleSheet LoadDefaultStyleSheet()
        {
            return instance;
        }


        /// <summary>
        /// Function to retrieve the Style matching the HashCode.
        /// </summary>
        /// <param name="hashCode"></param>
        /// <returns></returns>
        public static TMP_Style GetStyle(int hashCode)
        {
            return instance.GetStyleInternal(hashCode);
        }


        /// <summary>
        /// Internal method to retrieve the Style matching the Hashcode.
        /// </summary>
        /// <param name="hashCode"></param>
        /// <returns></returns>
        private TMP_Style GetStyleInternal(int hashCode)
        {
            if (m_StyleDictionary.TryGetValue(hashCode, out TMP_Style style))
            {
                return style;
            }

            return null;
        }


        public void UpdateStyleDictionaryKey(int old_key, int new_key)
        {
            if (m_StyleDictionary.ContainsKey(old_key))
            {
                TMP_Style style = m_StyleDictionary[old_key];
                m_StyleDictionary.Add(new_key, style);
                m_StyleDictionary.Remove(old_key);
            }
        }


        /// <summary>
        /// Function to update the internal reference to a newly assigned style sheet in the TMP Settings.
        /// </summary>
        public static void UpdateStyleSheet()
        {
            // Reset instance
            s_Instance = null;

            RefreshStyles();
        }


        /// <summary>
        /// Function to refresh the Style Dictionary.
        /// </summary>
        public static void RefreshStyles()
        {
            instance.LoadStyleDictionaryInternal();
        }


        /// <summary>
        /// 
        /// </summary>
        private void LoadStyleDictionaryInternal()
        {
            m_StyleDictionary.Clear();
            
            // Read Styles from style list and store them into dictionary for faster access.
            for (int i = 0; i < m_StyleList.Count; i++)
            {
                m_StyleList[i].RefreshStyle();
              
                if (!m_StyleDictionary.ContainsKey(m_StyleList[i].hashCode))
                    m_StyleDictionary.Add(m_StyleList[i].hashCode, m_StyleList[i]);
            }
        }
    }

}