AnimCollapseSection.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #define AVPROVIDEO_SUPPORT_LIVEEDITMODE
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. //-----------------------------------------------------------------------------
  6. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. namespace RenderHeads.Media.AVProVideo.Editor
  9. {
  10. /// A collapsable GUI section that animates during open and close
  11. internal class AnimCollapseSection
  12. {
  13. internal const string SettingsPrefix = "AVProVideo-MediaPlayerEditor-";
  14. private const float CollapseSpeed = 2f;
  15. private static GUIStyle _styleCollapsableSection = null;
  16. private static GUIStyle _styleButtonFoldout = null;
  17. private static GUIStyle _styleHelpBoxNoPad = null;
  18. public AnimCollapseSection(string label, bool showOnlyInEditMode, bool isDefaultExpanded, System.Action action, UnityEditor.Editor editor, Color backgroundColor, List<AnimCollapseSection> groupItems = null)
  19. : this(new GUIContent(label), showOnlyInEditMode, isDefaultExpanded, action, editor, backgroundColor, groupItems)
  20. {
  21. }
  22. public AnimCollapseSection(GUIContent label, bool showOnlyInEditMode, bool isDefaultExpanded, System.Action action, UnityEditor.Editor editor, Color backgroundColor, List<AnimCollapseSection> groupItems = null)
  23. {
  24. Label = label;
  25. _name = Label.text;
  26. Label.text = " " + Label.text; // Add a space for aesthetics
  27. ShowOnlyInEditMode = showOnlyInEditMode;
  28. _action = action;
  29. isDefaultExpanded = EditorPrefs.GetBool(PrefName, isDefaultExpanded);
  30. BackgroundColor = backgroundColor;
  31. _groupItems = groupItems;
  32. _anim = new UnityEditor.AnimatedValues.AnimBool(isDefaultExpanded);
  33. _anim.speed = CollapseSpeed;
  34. _anim.valueChanged.AddListener(editor.Repaint);
  35. }
  36. ~AnimCollapseSection()
  37. {
  38. _anim.valueChanged.RemoveAllListeners();
  39. }
  40. private string _name;
  41. private UnityEditor.AnimatedValues.AnimBool _anim;
  42. private System.Action _action;
  43. private List<AnimCollapseSection> _groupItems;
  44. public void Invoke()
  45. {
  46. _action.Invoke();
  47. }
  48. public bool IsExpanded { get { return _anim.target; } set { if (_anim.target != value) { _anim.target = value; if (value) CollapseSiblings(); } } }
  49. public float Faded { get { return _anim.faded; } }
  50. public GUIContent Label { get; private set; }
  51. public bool ShowOnlyInEditMode { get; private set; }
  52. public Color BackgroundColor { get; private set; }
  53. private string PrefName { get { return GetPrefName(_name); } }
  54. public void Save()
  55. {
  56. EditorPrefs.SetBool(PrefName, IsExpanded);
  57. }
  58. private void CollapseSiblings()
  59. {
  60. // Ensure only a single item is in an expanded state
  61. if (_groupItems != null)
  62. {
  63. foreach (AnimCollapseSection section in _groupItems)
  64. {
  65. if (section != this && section.IsExpanded)
  66. {
  67. section.IsExpanded = false;
  68. }
  69. }
  70. }
  71. }
  72. internal static string GetPrefName(string label)
  73. {
  74. return SettingsPrefix + "Expand-" + label;
  75. }
  76. internal static void CreateStyles()
  77. {
  78. if (_styleCollapsableSection == null)
  79. {
  80. _styleCollapsableSection = new GUIStyle(GUI.skin.box);
  81. _styleCollapsableSection.padding.top = 0;
  82. _styleCollapsableSection.padding.bottom = 0;
  83. }
  84. if (_styleButtonFoldout == null)
  85. {
  86. _styleButtonFoldout = new GUIStyle(EditorStyles.foldout);
  87. _styleButtonFoldout.margin = new RectOffset();
  88. _styleButtonFoldout.fontStyle = FontStyle.Bold;
  89. _styleButtonFoldout.alignment = TextAnchor.MiddleLeft;
  90. }
  91. if (_styleHelpBoxNoPad == null)
  92. {
  93. _styleHelpBoxNoPad = new GUIStyle(EditorStyles.helpBox);
  94. _styleHelpBoxNoPad.padding = new RectOffset();
  95. //_styleHelpBoxNoPad.border = new RectOffset();
  96. _styleHelpBoxNoPad.overflow = new RectOffset();
  97. _styleHelpBoxNoPad.margin = new RectOffset();
  98. _styleHelpBoxNoPad.margin = new RectOffset(8, 0, 0, 0);
  99. _styleHelpBoxNoPad.stretchWidth = false;
  100. _styleHelpBoxNoPad.stretchHeight = false;
  101. //_styleHelpBoxNoPad.normal.background = Texture2D.whiteTexture;
  102. }
  103. }
  104. internal static void Show(AnimCollapseSection section, int indentLevel = 0)
  105. {
  106. if (section.ShowOnlyInEditMode && Application.isPlaying) return;
  107. float headerGlow = Mathf.Lerp(0.5f, 0.85f, section.Faded);
  108. //float headerGlow = Mathf.Lerp(0.85f, 1f, section.Faded);
  109. if (EditorGUIUtility.isProSkin)
  110. {
  111. GUI.backgroundColor = section.BackgroundColor * new Color(headerGlow, headerGlow, headerGlow, 1f);
  112. }
  113. else
  114. {
  115. headerGlow = Mathf.Lerp(0.75f, 1f, section.Faded);
  116. GUI.backgroundColor = section.BackgroundColor * new Color(headerGlow, headerGlow, headerGlow, 1f);
  117. }
  118. GUILayout.BeginVertical(_styleHelpBoxNoPad);
  119. GUILayout.Box(GUIContent.none, EditorStyles.miniButton, GUILayout.ExpandWidth(true));
  120. GUI.backgroundColor = Color.white;
  121. Rect buttonRect = GUILayoutUtility.GetLastRect();
  122. if (Event.current.type != EventType.Layout)
  123. {
  124. buttonRect.xMin += indentLevel * EditorGUIUtility.fieldWidth / 3f;
  125. EditorGUI.indentLevel++;
  126. EditorGUIUtility.SetIconSize(new Vector2(16f, 16f));
  127. section.IsExpanded = EditorGUI.Foldout(buttonRect, section.IsExpanded, section.Label, true, _styleButtonFoldout);
  128. EditorGUIUtility.SetIconSize(Vector2.zero);
  129. EditorGUI.indentLevel--;
  130. }
  131. if (EditorGUILayout.BeginFadeGroup(section.Faded))
  132. {
  133. section.Invoke();
  134. }
  135. EditorGUILayout.EndFadeGroup();
  136. GUILayout.EndVertical();
  137. }
  138. internal static void Show(string label, ref bool isExpanded, System.Action action, bool showOnlyInEditMode)
  139. {
  140. if (showOnlyInEditMode && Application.isPlaying) return;
  141. if (BeginShow(label, ref isExpanded, Color.white))
  142. {
  143. action.Invoke();
  144. }
  145. EndShow();
  146. }
  147. internal static bool BeginShow(string label, ref bool isExpanded, Color tintColor)
  148. {
  149. GUI.color = Color.white;
  150. GUI.backgroundColor = Color.clear;
  151. if (isExpanded)
  152. {
  153. GUI.color = Color.white;
  154. GUI.backgroundColor = new Color(0.8f, 0.8f, 0.8f, 0.1f);
  155. if (EditorGUIUtility.isProSkin)
  156. {
  157. GUI.backgroundColor = Color.black;
  158. }
  159. }
  160. GUILayout.BeginVertical(_styleCollapsableSection);
  161. GUI.color = tintColor;
  162. GUI.backgroundColor = Color.white;
  163. if (GUILayout.Button(label, EditorStyles.toolbarButton))
  164. {
  165. isExpanded = !isExpanded;
  166. }
  167. GUI.color = Color.white;
  168. return isExpanded;
  169. }
  170. internal static void EndShow()
  171. {
  172. GUILayout.EndVertical();
  173. }
  174. }
  175. }