No Description
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.

TMP_PropertyDrawerUtilities.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.TextCore;
  5. using UnityEngine.TextCore.LowLevel;
  6. namespace TMPro.EditorUtilities
  7. {
  8. internal struct GlyphProxy
  9. {
  10. public uint index;
  11. public GlyphRect glyphRect;
  12. public GlyphMetrics metrics;
  13. public int atlasIndex;
  14. }
  15. internal static class TMP_PropertyDrawerUtilities
  16. {
  17. internal static bool s_RefreshGlyphProxyLookup;
  18. private static Dictionary<SerializedObject, Dictionary<uint, GlyphProxy>> s_GlyphProxyLookups = new Dictionary<SerializedObject, Dictionary<uint, GlyphProxy>>();
  19. internal static void ClearGlyphProxyLookups()
  20. {
  21. s_GlyphProxyLookups.Clear();
  22. }
  23. internal static void RefreshGlyphProxyLookup(SerializedObject so)
  24. {
  25. if (!s_GlyphProxyLookups.ContainsKey(so))
  26. return;
  27. Dictionary<uint, GlyphProxy> glyphProxyLookup = s_GlyphProxyLookups[so];
  28. glyphProxyLookup.Clear();
  29. PopulateGlyphProxyLookupDictionary(so, glyphProxyLookup);
  30. s_RefreshGlyphProxyLookup = false;
  31. }
  32. internal static Dictionary<uint, GlyphProxy> GetGlyphProxyLookupDictionary(SerializedObject so)
  33. {
  34. if (s_GlyphProxyLookups.ContainsKey(so))
  35. return s_GlyphProxyLookups[so];
  36. Dictionary<uint, GlyphProxy> glyphProxyLookup = new Dictionary<uint, GlyphProxy>();
  37. PopulateGlyphProxyLookupDictionary(so, glyphProxyLookup);
  38. s_GlyphProxyLookups.Add(so, glyphProxyLookup);
  39. return glyphProxyLookup;
  40. }
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. /// <param name="so"></param>
  45. /// <param name="lookupDictionary"></param>
  46. static void PopulateGlyphProxyLookupDictionary(SerializedObject so, Dictionary<uint, GlyphProxy> lookupDictionary)
  47. {
  48. if (lookupDictionary == null)
  49. return;
  50. // Get reference to serialized property for the glyph table
  51. SerializedProperty glyphTable = so.FindProperty("m_GlyphTable");
  52. for (int i = 0; i < glyphTable.arraySize; i++)
  53. {
  54. SerializedProperty glyphProperty = glyphTable.GetArrayElementAtIndex(i);
  55. GlyphProxy proxy = GetGlyphProxyFromSerializedProperty(glyphProperty);
  56. lookupDictionary.Add(proxy.index, proxy);
  57. }
  58. }
  59. internal static GlyphRect GetGlyphRectFromGlyphSerializedProperty(SerializedProperty property)
  60. {
  61. SerializedProperty glyphRectProp = property.FindPropertyRelative("m_GlyphRect");
  62. GlyphRect glyphRect = new GlyphRect();
  63. glyphRect.x = glyphRectProp.FindPropertyRelative("m_X").intValue;
  64. glyphRect.y = glyphRectProp.FindPropertyRelative("m_Y").intValue;
  65. glyphRect.width = glyphRectProp.FindPropertyRelative("m_Width").intValue;
  66. glyphRect.height = glyphRectProp.FindPropertyRelative("m_Height").intValue;
  67. return glyphRect;
  68. }
  69. internal static GlyphMetrics GetGlyphMetricsFromGlyphSerializedProperty(SerializedProperty property)
  70. {
  71. SerializedProperty glyphMetricsProperty = property.FindPropertyRelative("m_Metrics");
  72. GlyphMetrics glyphMetrics = new GlyphMetrics();
  73. glyphMetrics.horizontalBearingX = glyphMetricsProperty.FindPropertyRelative("m_HorizontalBearingX").floatValue;
  74. glyphMetrics.horizontalBearingY = glyphMetricsProperty.FindPropertyRelative("m_HorizontalBearingY").floatValue;
  75. glyphMetrics.horizontalAdvance = glyphMetricsProperty.FindPropertyRelative("m_HorizontalAdvance").floatValue;
  76. glyphMetrics.width = glyphMetricsProperty.FindPropertyRelative("m_Width").floatValue;
  77. glyphMetrics.height = glyphMetricsProperty.FindPropertyRelative("m_Height").floatValue;
  78. return glyphMetrics;
  79. }
  80. /// <summary>
  81. ///
  82. /// </summary>
  83. /// <param name="property"></param>
  84. /// <returns></returns>
  85. internal static GlyphProxy GetGlyphProxyFromSerializedProperty(SerializedProperty property)
  86. {
  87. GlyphProxy proxy = new GlyphProxy();
  88. proxy.index = (uint)property.FindPropertyRelative("m_Index").intValue;
  89. proxy.glyphRect = GetGlyphRectFromGlyphSerializedProperty(property);
  90. proxy.metrics = GetGlyphMetricsFromGlyphSerializedProperty(property);
  91. proxy.atlasIndex = property.FindPropertyRelative("m_AtlasIndex").intValue;
  92. return proxy;
  93. }
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. /// <param name="serializedObject"></param>
  98. /// <param name="glyphIndex"></param>
  99. /// <param name="texture"></param>
  100. /// <returns></returns>
  101. internal static bool TryGetAtlasTextureFromSerializedObject(SerializedObject serializedObject, int glyphIndex, out Texture2D texture)
  102. {
  103. SerializedProperty atlasTextureProperty = serializedObject.FindProperty("m_AtlasTextures");
  104. texture = atlasTextureProperty.GetArrayElementAtIndex(glyphIndex).objectReferenceValue as Texture2D;
  105. if (texture == null)
  106. return false;
  107. return true;
  108. }
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. /// <param name="serializedObject"></param>
  113. /// <param name="texture"></param>
  114. /// <param name="mat"></param>
  115. /// <returns></returns>
  116. internal static bool TryGetMaterial(SerializedObject serializedObject, Texture2D texture, out Material mat)
  117. {
  118. GlyphRenderMode atlasRenderMode = (GlyphRenderMode)serializedObject.FindProperty("m_AtlasRenderMode").intValue;
  119. if (((GlyphRasterModes)atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
  120. {
  121. #if TEXTCORE_FONT_ENGINE_1_5_OR_NEWER
  122. if (atlasRenderMode == GlyphRenderMode.COLOR || atlasRenderMode == GlyphRenderMode.COLOR_HINTED)
  123. mat = TMP_FontAssetEditor.internalRGBABitmapMaterial;
  124. else
  125. mat = TMP_FontAssetEditor.internalBitmapMaterial;
  126. #else
  127. mat = TMP_FontAssetEditor.internalBitmapMaterial;
  128. #endif
  129. if (mat == null)
  130. return false;
  131. mat.mainTexture = texture;
  132. mat.color = Color.white;
  133. }
  134. else
  135. {
  136. mat = TMP_FontAssetEditor.internalSDFMaterial;
  137. if (mat == null)
  138. return false;
  139. int padding = serializedObject.FindProperty("m_AtlasPadding").intValue;
  140. mat.mainTexture = texture;
  141. mat.SetFloat(ShaderUtilities.ID_GradientScale, padding + 1);
  142. }
  143. return true;
  144. }
  145. }
  146. }