VisibilityToolBase.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using UnityEditor.IMGUI.Controls;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.U2D.Animation
  6. {
  7. internal interface IVisibilityTool
  8. {
  9. VisualElement view { get; }
  10. string name { get; }
  11. void Activate();
  12. void Deactivate();
  13. bool isAvailable { get; }
  14. void SetAvailabilityChangeCallback(Action callback);
  15. void Setup();
  16. void Dispose();
  17. }
  18. internal class VisibilityToolViewBase : VisualElement
  19. {
  20. IMGUIContainer m_Container;
  21. SearchField m_SearchField;
  22. protected TreeView m_TreeView;
  23. protected TreeViewState m_TreeViewState = new TreeViewState();
  24. public Action<float> SetOpacityValue = null;
  25. public Func<float> GetOpacityValue = null;
  26. protected static class Styles
  27. {
  28. public static readonly GUIStyle preLabel = "preLabel";
  29. public static readonly GUIStyle preSlider = "preSlider";
  30. public static readonly GUIStyle preSliderThumb = "preSliderThumb";
  31. }
  32. public VisibilityToolViewBase()
  33. {
  34. m_Container = new IMGUIContainer(OnGUI);
  35. this.Add(m_Container);
  36. m_TreeViewState.searchString = "";
  37. }
  38. protected void SetupSearchField()
  39. {
  40. m_SearchField = new SearchField();
  41. m_SearchField.downOrUpArrowKeyPressed += m_TreeView.SetFocusAndEnsureSelectedItem;
  42. }
  43. void DoSearchField()
  44. {
  45. m_SearchField.downOrUpArrowKeyPressed += m_TreeView.SetFocusAndEnsureSelectedItem;
  46. GUILayout.BeginHorizontal(EditorStyles.toolbar);
  47. m_TreeView.searchString = m_SearchField.OnToolbarGUI(m_TreeView.searchString);
  48. GUILayout.EndHorizontal();
  49. }
  50. void DoOpacitySlider()
  51. {
  52. if (GetOpacityValue != null && SetOpacityValue != null)
  53. {
  54. GUILayout.BeginHorizontal(EditorStyles.toolbar);
  55. EditorGUI.BeginChangeCheck();
  56. var opacity = GUILayout.HorizontalSlider(GetOpacityValue(), 0, 1, Styles.preSlider, Styles.preSliderThumb);
  57. if (EditorGUI.EndChangeCheck())
  58. SetOpacityValue(opacity);
  59. GUILayout.EndHorizontal();
  60. }
  61. }
  62. void OnGUI()
  63. {
  64. GUILayout.BeginVertical();
  65. DoSearchField();
  66. GUILayout.EndVertical();
  67. Rect rect = GUILayoutUtility.GetRect(0, 100000, 0, 100000);
  68. m_TreeView.OnGUI(rect);
  69. DoOpacitySlider();
  70. }
  71. }
  72. internal class TreeViewItemBase<T> : TreeViewItem
  73. {
  74. public T customData;
  75. public TreeViewItemBase(int id, int depth, string name, T data) : base(id, depth, name)
  76. {
  77. customData = data;
  78. }
  79. }
  80. internal class VisibilityTreeViewBase : TreeView
  81. {
  82. static internal class VisibilityIconStyle
  83. {
  84. public static readonly GUIContent visibilityOnIcon = new GUIContent(IconUtility.LoadIconResource("Visibility_Tool", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr("On"));
  85. public static readonly GUIContent visibilityOffIcon = new GUIContent(IconUtility.LoadIconResource("Visibility_Hidded", IconUtility.k_LightIconResourcePath, IconUtility.k_DarkIconResourcePath), L10n.Tr("Off"));
  86. }
  87. public VisibilityTreeViewBase(TreeViewState treeViewState, MultiColumnHeader multiColumn)
  88. : base(treeViewState, multiColumn)
  89. {
  90. Init();
  91. }
  92. public VisibilityTreeViewBase(TreeViewState treeViewState)
  93. : base(treeViewState)
  94. {
  95. Init();
  96. }
  97. void Init()
  98. {
  99. this.showAlternatingRowBackgrounds = true;
  100. this.useScrollView = true;
  101. }
  102. protected override TreeViewItem BuildRoot()
  103. {
  104. return new TreeViewItem { id = 0, depth = -1 };
  105. }
  106. }
  107. }