Brak opisu
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.

UIElementsExtensions.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine.UIElements;
  4. using PlasticGui;
  5. using Unity.PlasticSCM.Editor.AssetUtils;
  6. namespace Unity.PlasticSCM.Editor.UI.UIElements
  7. {
  8. internal static class UIElementsExtensions
  9. {
  10. internal static void LoadLayout(
  11. this VisualElement element,
  12. string className)
  13. {
  14. string uxmlRelativePath = Path.Combine(
  15. AssetsPath.GetLayoutsFolderRelativePath(),
  16. string.Format("{0}.uxml", className));
  17. VisualTreeAsset visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
  18. uxmlRelativePath);
  19. if (visualTree == null)
  20. {
  21. UnityEngine.Debug.LogErrorFormat(
  22. "Layout {0} not found at path {1}",
  23. className,
  24. uxmlRelativePath);
  25. return;
  26. }
  27. visualTree.CloneTree(element);
  28. }
  29. internal static void LoadStyle(
  30. this VisualElement element,
  31. string className)
  32. {
  33. string ussRelativePath = Path.Combine(
  34. AssetsPath.GetStylesFolderRelativePath(),
  35. string.Format("{0}.uss", className));
  36. StyleSheet sheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(
  37. ussRelativePath);
  38. if (sheet != null)
  39. {
  40. element.styleSheets.Add(sheet);
  41. }
  42. string ussSkinRelativePath = Path.Combine(
  43. AssetsPath.GetStylesFolderRelativePath(),
  44. string.Format("{0}.{1}.uss",
  45. className, EditorGUIUtility.isProSkin ? "dark" : "light"));
  46. StyleSheet skinSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(
  47. ussSkinRelativePath);
  48. if (skinSheet == null)
  49. return;
  50. element.styleSheets.Add(skinSheet);
  51. }
  52. internal static void FocusOnceLoaded(
  53. this TextField elem)
  54. {
  55. // Really weird behavior from UIElements, apperently in order
  56. // to focus a text field you have to wait for it to attach to
  57. // the panel and then focus it's TextInputBaseField child
  58. // control rather than the TextField itself. For more see:
  59. // https://forum.unity.com/threads/focus-doesnt-seem-to-work.901130/
  60. elem.RegisterCallback<AttachToPanelEvent>(
  61. _ => elem.Q(TextInputBaseField<string>.textInputUssName).Focus());
  62. }
  63. internal static void FocusWorkaround(this TextField textField)
  64. {
  65. // https://issuetracker.unity3d.com/issues/uielements-textfield-is-not-focused-and-you-are-not-able-to-type-in-characters-when-using-focus-method
  66. textField.Q("unity-text-input").Focus();
  67. }
  68. internal static void SetControlImage(
  69. this VisualElement element,
  70. string name,
  71. Images.Name imageName)
  72. {
  73. Image imageElem = element.Query<Image>(name).First();
  74. imageElem.image = Images.GetImage(imageName);
  75. }
  76. internal static void SetControlImage(
  77. this VisualElement element,
  78. string name,
  79. PlasticGui.Help.HelpImage imageName)
  80. {
  81. Image imageElem = element.Query<Image>(name).First();
  82. imageElem.image = Images.GetHelpImage(imageName);
  83. imageElem.image.wrapMode = UnityEngine.TextureWrapMode.Clamp;
  84. }
  85. internal static void SetControlText<T>(
  86. this VisualElement element,
  87. string name, PlasticLocalization.Name fieldName,
  88. params string[] format) where T : VisualElement
  89. {
  90. dynamic control = element.Query<T>(name).First();
  91. string str = PlasticLocalization.GetString(fieldName);
  92. control.text = format.Length > 0 ? string.Format(str, format) : str;
  93. }
  94. internal static void SetControlLabel<T>(
  95. this VisualElement element,
  96. string name, PlasticLocalization.Name fieldName) where T : VisualElement
  97. {
  98. dynamic control = element.Query<T>(name).First();
  99. control.label = PlasticLocalization.GetString(fieldName);
  100. }
  101. }
  102. }