InspectableRRef.cs 3.2 KB

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