VisualElementExtensions.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Linq;
  2. using System.Reflection;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. using UnityEditor;
  6. namespace UnityEditor.VFX.UI
  7. {
  8. static class VisualElementExtensions
  9. {
  10. static MethodInfo m_ValidateLayoutMethod;
  11. public static void InternalValidateLayout(this IPanel panel)
  12. {
  13. if (m_ValidateLayoutMethod == null)
  14. m_ValidateLayoutMethod = panel.GetType().GetMethod("ValidateLayout", BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public);
  15. m_ValidateLayoutMethod.Invoke(panel, new object[] { });
  16. }
  17. static PropertyInfo m_OwnerPropertyInfo;
  18. public static GUIView InternalGetGUIView(this IPanel panel)
  19. {
  20. if (m_OwnerPropertyInfo == null)
  21. m_OwnerPropertyInfo = panel.GetType().GetProperty("ownerObject", BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public);
  22. return (GUIView)m_OwnerPropertyInfo.GetValue(panel, new object[] { });
  23. }
  24. public static bool HasFocus(this VisualElement visualElement)
  25. {
  26. if (visualElement.panel == null) return false;
  27. return visualElement.panel.focusController.focusedElement == visualElement;
  28. }
  29. public static void AddStyleSheetPath(this VisualElement visualElement, string path)
  30. {
  31. var sheet = VFXView.LoadStyleSheet(path);
  32. if (sheet != null)
  33. visualElement.styleSheets.Add(sheet);
  34. }
  35. public static void AddStyleSheetPathWithSkinVariant(this VisualElement visualElement, string path)
  36. {
  37. visualElement.AddStyleSheetPath(path);
  38. //if (true)
  39. {
  40. visualElement.AddStyleSheetPath(path + "Dark");
  41. }
  42. /*else
  43. {
  44. visualElement.AddStyleSheetPath(path + "Light");
  45. }*/
  46. }
  47. public static void ResetPositionProperties(this VisualElement visualElement)
  48. {
  49. var style = visualElement.style;
  50. style.position = StyleKeyword.Null;
  51. style.marginLeft = StyleKeyword.Null;
  52. style.marginRight = StyleKeyword.Null;
  53. style.marginBottom = StyleKeyword.Null;
  54. style.marginTop = StyleKeyword.Null;
  55. style.left = StyleKeyword.Null;
  56. style.top = StyleKeyword.Null;
  57. style.right = StyleKeyword.Null;
  58. style.bottom = StyleKeyword.Null;
  59. style.width = StyleKeyword.Null;
  60. style.height = StyleKeyword.Null;
  61. }
  62. public static Vector2 GlobalToBound(this VisualElement visualElement, Vector2 position)
  63. {
  64. return visualElement.worldTransform.inverse.MultiplyPoint3x4(position);
  65. }
  66. public static Vector2 BoundToGlobal(this VisualElement visualElement, Vector2 position)
  67. {
  68. position = visualElement.worldTransform.MultiplyPoint3x4(position);
  69. return position;
  70. }
  71. }
  72. }