InspectableCategory.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Reflection;
  6. using bs;
  7. namespace bs.Editor
  8. {
  9. /** @addtogroup Inspector
  10. * @{
  11. */
  12. /// <summary>
  13. /// Helper inspectable type that groups another inspectable fields under a common category.
  14. /// </summary>
  15. public class InspectableCategory : InspectableField
  16. {
  17. private const int IndentAmount = 5;
  18. public GUILayoutY ChildLayout { get; private set; }
  19. private List<InspectableField> children = new List<InspectableField>();
  20. private GUILayoutY guiLayout;
  21. private GUIPanel guiContentPanel;
  22. private bool isExpanded;
  23. /// <summary>
  24. /// Creates a new inspectable category. The category is initially empty and children must be added by calling
  25. /// <see cref="AddChild"/>.
  26. /// </summary>
  27. /// <param name="context">Context shared by all inspectable fields created by the same parent.</param>
  28. /// <param name="title">Name of the category.</param>
  29. /// <param name="path">Full path to the category (includes name of the category and all parent properties).</param>
  30. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  31. /// contain other fields, in which case you should increase this value by one.</param>
  32. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  33. public InspectableCategory(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout)
  34. : base(context, title, "", SerializableProperty.FieldType.Object, depth, layout, null)
  35. {
  36. isExpanded = context.Persistent.GetBool(path + "_Expanded");
  37. }
  38. /// <summary>
  39. /// Registers a new child field in the category.
  40. /// </summary>
  41. /// <param name="child">Child field to add to the category. The field must have the category
  42. /// <see cref="ChildLayout"/> as its parent.</param>
  43. public void AddChild(InspectableField child)
  44. {
  45. children.Add(child);
  46. }
  47. /// <inheritdoc/>
  48. public override InspectableState Refresh(int layoutIndex)
  49. {
  50. InspectableState state = InspectableState.NotModified;
  51. int currentIndex = 0;
  52. for (int i = 0; i < children.Count; i++)
  53. {
  54. state |= children[i].Refresh(currentIndex);
  55. currentIndex += children[i].GetNumLayoutElements();
  56. }
  57. return state;
  58. }
  59. /// <inheritdoc />
  60. public override InspectableField FindPath(string path)
  61. {
  62. return FindPath(path, depth, children);
  63. }
  64. /// <inheritdoc/>
  65. protected internal override void Initialize(int index)
  66. {
  67. guiLayout = layout.AddLayoutY(index);
  68. GUILayoutX guiTitleLayout = guiLayout.AddLayoutX();
  69. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  70. guiFoldout.Value = isExpanded;
  71. guiFoldout.AcceptsKeyFocus = false;
  72. guiFoldout.OnToggled += OnFoldoutToggled;
  73. guiTitleLayout.AddElement(guiFoldout);
  74. GUILayoutX categoryContentLayout = guiLayout.AddLayoutX();
  75. categoryContentLayout.AddSpace(IndentAmount);
  76. guiContentPanel = categoryContentLayout.AddPanel();
  77. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  78. guiIndentLayoutX.AddSpace(IndentAmount);
  79. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  80. guiIndentLayoutY.AddSpace(IndentAmount);
  81. ChildLayout = guiIndentLayoutY.AddLayoutY();
  82. guiIndentLayoutY.AddSpace(IndentAmount);
  83. guiIndentLayoutX.AddSpace(IndentAmount);
  84. categoryContentLayout.AddSpace(IndentAmount);
  85. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  86. string bgPanelStyle = depth % 2 == 0
  87. ? EditorStylesInternal.InspectorContentBgAlternate
  88. : EditorStylesInternal.InspectorContentBg;
  89. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  90. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  91. backgroundPanel.AddElement(inspectorContentBg);
  92. guiContentPanel.Active = isExpanded;
  93. }
  94. /// <summary>
  95. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  96. /// </summary>
  97. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  98. private void OnFoldoutToggled(bool expanded)
  99. {
  100. context.Persistent.SetBool(path + "_Expanded", expanded);
  101. isExpanded = expanded;
  102. guiContentPanel.Active = expanded;
  103. }
  104. }
  105. /** @} */
  106. }