GenericInspector.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /** @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 List<InspectableField> inspectableFields = new List<InspectableField>();
  18. /// <inheritdoc/>
  19. protected internal override void Initialize()
  20. {
  21. if (InspectedObject == null)
  22. LoadResource();
  23. if (InspectedObject != null)
  24. {
  25. int currentIndex = 0;
  26. SerializableObject serializableObject = new SerializableObject(InspectedObject.GetType(), InspectedObject);
  27. foreach (var field in serializableObject.Fields)
  28. {
  29. if (!field.Inspectable)
  30. continue;
  31. string path = field.Name;
  32. InspectableField inspectableField = InspectableField.CreateInspectable(this, field.Name, path,
  33. currentIndex, 0, new InspectableFieldLayout(Layout), field.GetProperty(), InspectableFieldStyle.Create(field));
  34. inspectableFields.Add(inspectableField);
  35. isEmpty = false;
  36. currentIndex += inspectableField.GetNumLayoutElements();
  37. }
  38. base.SetVisible(!isEmpty);
  39. }
  40. }
  41. /// <inheritdoc/>
  42. protected internal override InspectableState Refresh()
  43. {
  44. InspectableState state = InspectableState.NotModified;
  45. int currentIndex = 0;
  46. foreach (var field in inspectableFields)
  47. {
  48. state |= field.Refresh(currentIndex);
  49. currentIndex += field.GetNumLayoutElements();
  50. }
  51. return state;
  52. }
  53. /// <inheritdoc/>
  54. internal override void SetVisible(bool visible)
  55. {
  56. base.SetVisible(!isEmpty && visible);
  57. }
  58. }
  59. /** @} */
  60. }