GenericInspector.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. using bs;
  5. namespace bs.Editor
  6. {
  7. /** @addtogroup Inspector
  8. * @{
  9. */
  10. /// <summary>
  11. /// Default implementation of the inspector used when no specified inspector is provided for the type. Inspector
  12. /// displays GUI for all the inspectable fields in the object.
  13. /// </summary>
  14. internal sealed class GenericInspector : Inspector
  15. {
  16. private bool isEmpty = true;
  17. private GenericInspectorDrawer drawer;
  18. /// <inheritdoc/>
  19. protected internal override void Initialize()
  20. {
  21. if (InspectedObject == null)
  22. LoadResource();
  23. drawer = new GenericInspectorDrawer(InspectedObject, new InspectableContext(Persistent), Layout);
  24. isEmpty = drawer.Fields.Count == 0;
  25. base.SetVisible(!isEmpty);
  26. }
  27. /// <inheritdoc/>
  28. protected internal override InspectableState Refresh()
  29. {
  30. return drawer.Refresh();
  31. }
  32. /// <inheritdoc/>
  33. internal override void SetVisible(bool visible)
  34. {
  35. base.SetVisible(!isEmpty && visible);
  36. }
  37. }
  38. /// <summary>
  39. /// Helper class that draws the default inspector elements for an object, with an optional callback to render custom
  40. /// inspectors for certain types.
  41. /// </summary>
  42. internal sealed class GenericInspectorDrawer
  43. {
  44. /// <summary>
  45. /// List of fields created and updated by the drawer.
  46. /// </summary>
  47. public List<InspectableField> Fields { get; } = new List<InspectableField>();
  48. /// <summary>
  49. /// Creates new generic inspector field drawer for the specified object.
  50. /// </summary>
  51. /// <param name="obj">Object whose fields to create the GUI for.</param>
  52. /// <param name="context">Context shared by all inspectable fields created by the same parent.</param>
  53. /// <param name="layout">Parent layout that all the field GUI elements will be added to.</param>
  54. /// <param name="overrideCallback">
  55. /// Optional callback that allows you to override the look of individual fields in the object. If non-null the
  56. /// callback will be called with information about every field in the provided object. If the callback returns
  57. /// non-null that inspectable field will be used for drawing the GUI, otherwise the default inspector field type
  58. /// will be used.
  59. /// </param>
  60. public GenericInspectorDrawer(object obj, InspectableContext context, GUILayoutY layout,
  61. InspectableField.FieldOverrideCallback overrideCallback = null)
  62. {
  63. if (obj == null)
  64. return;
  65. SerializableObject serializableObject = new SerializableObject(obj.GetType(), obj);
  66. Fields = InspectableField.CreateFields(serializableObject, context, "", 0, layout, overrideCallback);
  67. }
  68. /// <summary>
  69. /// Checks if contents of the inspector fields have been modified, and updates them if needed.
  70. /// </summary>
  71. /// <returns>State representing was anything modified between two last calls to <see cref="Refresh"/>.</returns>
  72. public InspectableState Refresh()
  73. {
  74. InspectableState state = InspectableState.NotModified;
  75. int currentIndex = 0;
  76. foreach (var field in Fields)
  77. {
  78. state |= field.Refresh(currentIndex);
  79. currentIndex += field.GetNumLayoutElements();
  80. }
  81. return state;
  82. }
  83. }
  84. /** @} */
  85. }