TMP_GlyphPropertyDrawer.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1.  using UnityEngine;
  2. using UnityEngine.TextCore;
  3. using UnityEngine.TextCore.LowLevel;
  4. using UnityEditor;
  5. using System.Collections;
  6. namespace TMPro.EditorUtilities
  7. {
  8. [CustomPropertyDrawer(typeof(Glyph))]
  9. public class TMP_GlyphPropertyDrawer : PropertyDrawer
  10. {
  11. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  12. {
  13. SerializedProperty prop_GlyphIndex = property.FindPropertyRelative("m_Index");
  14. SerializedProperty prop_GlyphMetrics = property.FindPropertyRelative("m_Metrics");
  15. SerializedProperty prop_GlyphRect = property.FindPropertyRelative("m_GlyphRect");
  16. SerializedProperty prop_Scale = property.FindPropertyRelative("m_Scale");
  17. SerializedProperty prop_AtlasIndex = property.FindPropertyRelative("m_AtlasIndex");
  18. GUIStyle style = new GUIStyle(EditorStyles.label);
  19. style.richText = true;
  20. Rect rect = new Rect(position.x + 70, position.y, position.width, 49);
  21. float labelWidth = GUI.skin.label.CalcSize(new GUIContent("ID: " + prop_GlyphIndex.intValue)).x;
  22. EditorGUI.LabelField(new Rect(position.x + (64 - labelWidth) / 2, position.y + 85, 64f, 18f), new GUIContent("ID: <color=#FFFF80>" + prop_GlyphIndex.intValue + "</color>"), style);
  23. //EditorGUIUtility.labelWidth = 22f;
  24. //EditorGUI.DelayedIntField(new Rect(position.x + (64 - labelWidth) / 2, position.y + 89, 58f, 18f), prop_GlyphIndex, new GUIContent("ID:"));
  25. // We get Rect since a valid position may not be provided by the caller.
  26. EditorGUI.PropertyField(new Rect(rect.x, rect.y, position.width, 49), prop_GlyphRect);
  27. rect.y += 45;
  28. EditorGUI.PropertyField(rect, prop_GlyphMetrics);
  29. EditorGUIUtility.labelWidth = 40f;
  30. EditorGUI.PropertyField(new Rect(rect.x, rect.y + 65, 75, 18), prop_Scale, new GUIContent("Scale:")); // new GUIContent("Scale: <color=#FFFF80>" + prop_Scale.floatValue + "</color>"), style);
  31. EditorGUIUtility.labelWidth = 74f;
  32. EditorGUI.PropertyField(new Rect(rect.x + 85, rect.y + 65, 95, 18), prop_AtlasIndex, new GUIContent("Atlas Index:")); // new GUIContent("Atlas Index: <color=#FFFF80>" + prop_AtlasIndex.intValue + "</color>"), style);
  33. DrawGlyph(position, property);
  34. }
  35. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  36. {
  37. return 130f;
  38. }
  39. void DrawGlyph(Rect position, SerializedProperty property)
  40. {
  41. // Get a reference to the sprite texture
  42. TMP_FontAsset fontAsset = property.serializedObject.targetObject as TMP_FontAsset;
  43. if (fontAsset == null)
  44. return;
  45. // Get reference to atlas texture.
  46. int atlasIndex = property.FindPropertyRelative("m_AtlasIndex").intValue;
  47. Texture2D atlasTexture = fontAsset.atlasTextures.Length > atlasIndex ? fontAsset.atlasTextures[atlasIndex] : null;
  48. if (atlasTexture == null)
  49. return;
  50. Material mat;
  51. if (((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
  52. {
  53. mat = TMP_FontAssetEditor.internalBitmapMaterial;
  54. if (mat == null)
  55. return;
  56. mat.mainTexture = atlasTexture;
  57. mat.SetColor("_Color", Color.white);
  58. }
  59. else
  60. {
  61. mat = TMP_FontAssetEditor.internalSDFMaterial;
  62. if (mat == null)
  63. return;
  64. mat.mainTexture = atlasTexture;
  65. mat.SetFloat(ShaderUtilities.ID_GradientScale, fontAsset.atlasPadding + 1);
  66. }
  67. // Draw glyph from atlas texture.
  68. Rect glyphDrawPosition = new Rect(position.x, position.y + 2, 64, 80);
  69. SerializedProperty prop_GlyphRect = property.FindPropertyRelative("m_GlyphRect");
  70. int glyphOriginX = prop_GlyphRect.FindPropertyRelative("m_X").intValue;
  71. int glyphOriginY = prop_GlyphRect.FindPropertyRelative("m_Y").intValue;
  72. int glyphWidth = prop_GlyphRect.FindPropertyRelative("m_Width").intValue;
  73. int glyphHeight = prop_GlyphRect.FindPropertyRelative("m_Height").intValue;
  74. float normalizedHeight = fontAsset.faceInfo.ascentLine - fontAsset.faceInfo.descentLine;
  75. float scale = glyphDrawPosition.width / normalizedHeight;
  76. // Compute the normalized texture coordinates
  77. Rect texCoords = new Rect((float)glyphOriginX / atlasTexture.width, (float)glyphOriginY / atlasTexture.height, (float)glyphWidth / atlasTexture.width, (float)glyphHeight / atlasTexture.height);
  78. if (Event.current.type == EventType.Repaint)
  79. {
  80. glyphDrawPosition.x += (glyphDrawPosition.width - glyphWidth * scale) / 2;
  81. glyphDrawPosition.y += (glyphDrawPosition.height - glyphHeight * scale) / 2;
  82. glyphDrawPosition.width = glyphWidth * scale;
  83. glyphDrawPosition.height = glyphHeight * scale;
  84. // Could switch to using the default material of the font asset which would require passing scale to the shader.
  85. Graphics.DrawTexture(glyphDrawPosition, atlasTexture, texCoords, 0, 0, 0, 0, new Color(1f, 1f, 1f), mat);
  86. }
  87. }
  88. }
  89. }