Açıklama Yok
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.

HeaderFoldout.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace UnityEditor.Rendering
  5. {
  6. /// <summary> UITK component to display header styled foldout</summary>
  7. [UxmlElement]
  8. public partial class HeaderFoldout : Foldout
  9. {
  10. const string k_StylesheetPathFormat = "Packages/com.unity.render-pipelines.core/Editor/StyleSheets/HeaderFoldout{0}.uss";
  11. const string k_Class = "header-foldout";
  12. const string k_IconName = "header-foldout__icon";
  13. private string m_DocumentationURL;
  14. private Texture2D m_Icon;
  15. private Func<GenericMenu> m_ContextMenuGenerator;
  16. private VisualElement m_HelpButton;
  17. private VisualElement m_ContextMenuButton;
  18. private VisualElement m_IconElement;
  19. /// <summary>URL to use on documentation icon. If null, button don't show.</summary>
  20. public string documentationURL
  21. {
  22. get => m_DocumentationURL;
  23. set
  24. {
  25. if (m_DocumentationURL == value)
  26. return;
  27. m_DocumentationURL = value;
  28. m_HelpButton?.SetEnabled(!string.IsNullOrEmpty(m_DocumentationURL));
  29. }
  30. }
  31. /// <summary>Context menu to show on click of the context button. If null, button don't show.</summary>
  32. public Func<GenericMenu> contextMenuGenerator //Use ImGUI for now
  33. {
  34. get => m_ContextMenuGenerator;
  35. set
  36. {
  37. if (m_ContextMenuGenerator == value)
  38. return;
  39. m_ContextMenuGenerator = value;
  40. m_ContextMenuButton?.SetEnabled(m_ContextMenuGenerator != null);
  41. }
  42. }
  43. /// <summary>Optional icon image. If not set, no icon is shown.</summary>
  44. public Texture2D icon
  45. {
  46. get => m_Icon;
  47. set
  48. {
  49. if (m_Icon == value)
  50. return;
  51. m_Icon = value;
  52. m_IconElement.style.backgroundImage = Background.FromTexture2D(m_Icon);
  53. m_IconElement.style.display = m_Icon != null ? DisplayStyle.Flex : DisplayStyle.None;
  54. }
  55. }
  56. /// <summary>Constructor</summary>
  57. public HeaderFoldout() : base()
  58. {
  59. styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>(string.Format(k_StylesheetPathFormat, "")));
  60. styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>(string.Format(k_StylesheetPathFormat, EditorGUIUtility.isProSkin ? "Dark" : "Light")));
  61. AddToClassList(k_Class);
  62. RegisterCallback<AttachToPanelEvent>(DelayedInit);
  63. var line = hierarchy[0][0]; //pass by herarchy to ignore content redirection
  64. m_HelpButton = new Button(Background.FromTexture2D(CoreEditorStyles.iconHelp), () => Help.BrowseURL(m_DocumentationURL));
  65. m_HelpButton.SetEnabled(!string.IsNullOrEmpty(m_DocumentationURL));
  66. line.Add(m_HelpButton);
  67. m_ContextMenuButton = new Button(Background.FromTexture2D(CoreEditorStyles.paneOptionsIcon), () => ShowMenu());
  68. m_ContextMenuButton.SetEnabled(m_ContextMenuGenerator != null);
  69. line.Add(m_ContextMenuButton);
  70. m_IconElement = new Image();
  71. m_IconElement.name = k_IconName;
  72. m_IconElement.style.display = DisplayStyle.None; // Disable by default, will be enabled if icon is set
  73. // Delay insertion of icon to happen after foldout is constructed so we can put it in the right place
  74. RegisterCallbackOnce<AttachToPanelEvent>(evt => line.Insert(1, m_IconElement));
  75. }
  76. void DelayedInit(AttachToPanelEvent evt)
  77. {
  78. //Only show top line if previous item is not a HeaderFoldout to avoid bolder border
  79. bool shouldShowTopLine = true;
  80. var parent = hierarchy.parent;
  81. int posInParent = parent.hierarchy.IndexOf(this);
  82. if (posInParent > 0 && parent[posInParent - 1].ClassListContains(k_Class))
  83. shouldShowTopLine = false;
  84. style.borderTopWidth = shouldShowTopLine ? 1 : 0;
  85. }
  86. void ShowMenu()
  87. {
  88. var menu = m_ContextMenuGenerator.Invoke();
  89. menu.DropDown(new Rect(m_ContextMenuButton.worldBound.position + m_ContextMenuButton.worldBound.size.y * Vector2.up, Vector2.zero));
  90. }
  91. }
  92. /// <summary> UITK component to display header styled foldout. This variant have an enable checkbox.</summary>
  93. public class HeaderToggleFoldout : HeaderFoldout
  94. {
  95. private Toggle m_Toggle;
  96. /// <summary>Property to get the enablement state</summary>
  97. public bool enabled
  98. {
  99. get => m_Toggle.value;
  100. set => m_Toggle.value = value;
  101. }
  102. /// <summary>Quick access to the enable toggle if one need to register events</summary>
  103. public Toggle enableToggle => m_Toggle;
  104. /// <summary>Constructor</summary>
  105. public HeaderToggleFoldout() : base()
  106. {
  107. var line = hierarchy[0][0]; //pass by herarchy to ignore content redirection
  108. m_Toggle = new Toggle()
  109. {
  110. value = true,
  111. name = "enable-checkbox",
  112. };
  113. //Need to delay insertion as foldout will be constructed after and we need to squeeze rigth after
  114. RegisterCallbackOnce<AttachToPanelEvent>(evt => line.Insert(1, m_Toggle));
  115. m_Toggle.RegisterValueChangedCallback(HandleDisabling);
  116. }
  117. void HandleDisabling(ChangeEvent<bool> evt)
  118. => contentContainer.SetEnabled(evt.newValue);
  119. }
  120. }