GenericInspector.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections.Generic;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Default implementation of the inspector used when no specified inspector is provided for the type. Inspector
  9. /// displays GUI for all the inspectable fields in the object.
  10. /// </summary>
  11. internal sealed class GenericInspector : Inspector
  12. {
  13. private bool isEmpty = true;
  14. private List<InspectableField> inspectableFields = new List<InspectableField>();
  15. /// <inheritdoc/>
  16. protected internal override void Initialize()
  17. {
  18. if (InspectedObject != null)
  19. {
  20. int currentIndex = 0;
  21. SerializableObject serializableObject = new SerializableObject(InspectedObject.GetType(), InspectedObject);
  22. foreach (var field in serializableObject.Fields)
  23. {
  24. if (!field.Inspectable)
  25. continue;
  26. string path = field.Name;
  27. InspectableField inspectableField = InspectableField.CreateInspectable(this, field.Name, path,
  28. currentIndex, 0, new InspectableFieldLayout(Layout), field.GetProperty());
  29. inspectableFields.Add(inspectableField);
  30. isEmpty = false;
  31. currentIndex += inspectableField.GetNumLayoutElements();
  32. }
  33. base.SetVisible(!isEmpty);
  34. }
  35. }
  36. /// <inheritdoc/>
  37. protected internal override InspectableState Refresh()
  38. {
  39. InspectableState state = InspectableState.NotModified;
  40. int currentIndex = 0;
  41. foreach (var field in inspectableFields)
  42. {
  43. state |= field.Refresh(currentIndex);
  44. currentIndex += field.GetNumLayoutElements();
  45. }
  46. return state;
  47. }
  48. /// <inheritdoc/>
  49. internal override void SetVisible(bool visible)
  50. {
  51. base.SetVisible(!isEmpty && visible);
  52. }
  53. }
  54. }