DebugUI.Containers.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. namespace UnityEngine.Rendering
  2. {
  3. public partial class DebugUI
  4. {
  5. /// <summary>
  6. /// Base class for "container" type widgets, although it can be used on its own (if a display name is set then it'll behave as a group with a header)
  7. /// </summary>
  8. public class Container : Widget, IContainer
  9. {
  10. /// <summary>
  11. /// List of children.
  12. /// </summary>
  13. public ObservableList<Widget> children { get; private set; }
  14. /// <summary>
  15. /// Panel the container is attached to.
  16. /// </summary>
  17. public override Panel panel
  18. {
  19. get { return m_Panel; }
  20. internal set
  21. {
  22. m_Panel = value;
  23. // Bubble down
  24. foreach (var child in children)
  25. child.panel = value;
  26. }
  27. }
  28. /// <summary>
  29. /// Constructor
  30. /// </summary>
  31. public Container()
  32. {
  33. displayName = "";
  34. children = new ObservableList<Widget>();
  35. children.ItemAdded += OnItemAdded;
  36. children.ItemRemoved += OnItemRemoved;
  37. }
  38. /// <summary>
  39. /// Constructor.
  40. /// </summary>
  41. /// <param name="displayName">Display name of the container.</param>
  42. /// <param name="children">List of attached children.</param>
  43. public Container(string displayName, ObservableList<Widget> children)
  44. {
  45. this.displayName = displayName;
  46. this.children = children;
  47. children.ItemAdded += OnItemAdded;
  48. children.ItemRemoved += OnItemRemoved;
  49. }
  50. internal override void GenerateQueryPath()
  51. {
  52. base.GenerateQueryPath();
  53. foreach (var child in children)
  54. child.GenerateQueryPath();
  55. }
  56. /// <summary>
  57. /// Method called when a children is added.
  58. /// </summary>
  59. /// <param name="sender">Sender widget.</param>
  60. /// <param name="e">List of added children.</param>
  61. protected virtual void OnItemAdded(ObservableList<Widget> sender, ListChangedEventArgs<Widget> e)
  62. {
  63. if (e.item != null)
  64. {
  65. e.item.panel = m_Panel;
  66. e.item.parent = this;
  67. }
  68. if (m_Panel != null)
  69. m_Panel.SetDirty();
  70. }
  71. /// <summary>
  72. /// Method called when a children is removed.
  73. /// </summary>
  74. /// <param name="sender">Sender widget.</param>
  75. /// <param name="e">List of removed children.</param>
  76. protected virtual void OnItemRemoved(ObservableList<Widget> sender, ListChangedEventArgs<Widget> e)
  77. {
  78. if (e.item != null)
  79. {
  80. e.item.panel = null;
  81. e.item.parent = null;
  82. }
  83. if (m_Panel != null)
  84. m_Panel.SetDirty();
  85. }
  86. /// <summary>
  87. /// Returns the hash code of the widget.
  88. /// </summary>
  89. /// <returns>Hash code of the widget.</returns>
  90. public override int GetHashCode()
  91. {
  92. int hash = 17;
  93. hash = hash * 23 + queryPath.GetHashCode();
  94. foreach (var child in children)
  95. hash = hash * 23 + child.GetHashCode();
  96. return hash;
  97. }
  98. }
  99. /// <summary>
  100. /// Unity-like foldout that can be collapsed.
  101. /// </summary>
  102. public class Foldout : Container, IValueField
  103. {
  104. /// <summary>
  105. /// Always false.
  106. /// </summary>
  107. public bool isReadOnly { get { return false; } }
  108. /// <summary>
  109. /// Opened state of the foldout.
  110. /// </summary>
  111. public bool opened;
  112. /// <summary>
  113. /// List of columns labels.
  114. /// </summary>
  115. public string[] columnLabels { get; set; } = null;
  116. /// <summary>
  117. /// Constructor.
  118. /// </summary>
  119. public Foldout() : base() { }
  120. /// <summary>
  121. /// Constructor.
  122. /// </summary>
  123. /// <param name="displayName">Display name of the foldout.</param>
  124. /// <param name="children">List of attached children.</param>
  125. /// <param name="columnLabels">Optional list of column names.</param>
  126. public Foldout(string displayName, ObservableList<Widget> children, string[] columnLabels = null)
  127. : base(displayName, children)
  128. {
  129. this.columnLabels = columnLabels;
  130. }
  131. /// <summary>
  132. /// Get the opened state of the foldout.
  133. /// </summary>
  134. /// <returns>True if the foldout is opened.</returns>
  135. public bool GetValue() => opened;
  136. /// <summary>
  137. /// Get the opened state of the foldout.
  138. /// </summary>
  139. /// <returns>True if the foldout is opened.</returns>
  140. object IValueField.GetValue() => GetValue();
  141. /// <summary>
  142. /// Set the opened state of the foldout.
  143. /// </summary>
  144. /// <param name="value">True to open the foldout, false to close it.</param>
  145. public void SetValue(object value) => SetValue((bool)value);
  146. /// <summary>
  147. /// Validates the value of the widget before setting it.
  148. /// </summary>
  149. /// <param name="value">Input value.</param>
  150. /// <returns>The validated value.</returns>
  151. public object ValidateValue(object value) => value;
  152. /// <summary>
  153. /// Set the value of the widget.
  154. /// </summary>
  155. /// <param name="value">Input value.</param>
  156. public void SetValue(bool value) => opened = value;
  157. }
  158. /// <summary>
  159. /// Horizontal Layout Container.
  160. /// </summary>
  161. public class HBox : Container
  162. {
  163. /// <summary>
  164. /// Constructor.
  165. /// </summary>
  166. public HBox()
  167. {
  168. displayName = "HBox";
  169. }
  170. }
  171. /// <summary>
  172. /// Vertical Layout Container.
  173. /// </summary>
  174. public class VBox : Container
  175. {
  176. /// <summary>
  177. /// Constructor.
  178. /// </summary>
  179. public VBox()
  180. {
  181. displayName = "VBox";
  182. }
  183. }
  184. }
  185. }