InspectableResourceRef.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. /// <summary>
  5. /// Displays GUI for a serializable property containing a <see cref="Resource"/> reference.
  6. /// </summary>
  7. public class InspectableResourceRef : InspectableField
  8. {
  9. private GUIResourceField guiField;
  10. private InspectableState state;
  11. /// <summary>
  12. /// Creates a new inspectable resource reference GUI for the specified property.
  13. /// </summary>
  14. /// <param name="parent">Parent Inspector this field belongs to.</param>
  15. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  16. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  17. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  18. /// contain other fields, in which case you should increase this value by one.</param>
  19. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  20. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  21. public InspectableResourceRef(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
  22. SerializableProperty property)
  23. : base(parent, title, path, SerializableProperty.FieldType.ResourceRef, depth, layout, property)
  24. {
  25. }
  26. /// <inheritoc/>
  27. protected internal override void Initialize(int layoutIndex)
  28. {
  29. if (property.Type == SerializableProperty.FieldType.ResourceRef)
  30. {
  31. guiField = new GUIResourceField(property.InternalType, new GUIContent(title));
  32. guiField.OnChanged += OnFieldValueChanged;
  33. layout.AddElement(layoutIndex, guiField);
  34. }
  35. }
  36. /// <inheritdoc/>
  37. public override InspectableState Refresh(int layoutIndex)
  38. {
  39. if (guiField != null)
  40. guiField.Value = property.GetValue<Resource>();
  41. InspectableState oldState = state;
  42. if (state.HasFlag(InspectableState.Modified))
  43. state = InspectableState.NotModified;
  44. return oldState;
  45. }
  46. /// <summary>
  47. /// Triggered when the user drops a new resource onto the field, or clears the current value.
  48. /// </summary>
  49. /// <param name="newValue">New resource to reference.</param>
  50. private void OnFieldValueChanged(ResourceRef newValue)
  51. {
  52. Resource res = Resources.Load<Resource>(newValue);
  53. property.SetValue(res);
  54. state = InspectableState.Modified;
  55. }
  56. }
  57. }