InspectableResourceRef.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /// <summary>
  10. /// Displays GUI for a serializable property containing a <see cref="Resource"/> reference.
  11. /// </summary>
  12. public class InspectableResourceRef : InspectableField
  13. {
  14. private Resource propertyValue;
  15. private GUIResourceField guiField;
  16. private bool isInitialized;
  17. /// <summary>
  18. /// Creates a new inspectable resource reference GUI for the specified property.
  19. /// </summary>
  20. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  21. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  22. /// contain other fields, in which case you should increase this value by one.</param>
  23. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  24. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  25. public InspectableResourceRef(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  26. : base(title, depth, layout, property)
  27. {
  28. }
  29. /// <summary>
  30. /// Initializes the GUI elements the first time <see cref="Update"/> gets called.
  31. /// </summary>
  32. /// <param name="layoutIndex">Index at which to insert the GUI elements.</param>
  33. private void Initialize(int layoutIndex)
  34. {
  35. if (property.Type == SerializableProperty.FieldType.ResourceRef)
  36. {
  37. guiField = new GUIResourceField(property.InternalType, new GUIContent(title));
  38. guiField.OnChanged += OnFieldValueChanged;
  39. layout.AddElement(layoutIndex, guiField);
  40. }
  41. isInitialized = true;
  42. }
  43. /// <inheritdoc/>
  44. protected override bool IsModified()
  45. {
  46. if (!isInitialized)
  47. return true;
  48. Resource newPropertyValue = property.GetValue<Resource>();
  49. if (propertyValue != newPropertyValue)
  50. return true;
  51. return base.IsModified();
  52. }
  53. /// <inheritdoc/>
  54. protected override void Update(int layoutIndex)
  55. {
  56. base.Update(layoutIndex);
  57. if (!isInitialized)
  58. Initialize(layoutIndex);
  59. propertyValue = property.GetValue<Resource>();
  60. if (guiField != null)
  61. guiField.Value = propertyValue;
  62. }
  63. /// <summary>
  64. /// Triggered when the user drops a new resource onto the field, or clears the current value.
  65. /// </summary>
  66. /// <param name="newValue">New resource to reference.</param>
  67. private void OnFieldValueChanged(Resource newValue)
  68. {
  69. property.SetValue(newValue);
  70. }
  71. }
  72. }