InspectableRRef.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. namespace bs.Editor
  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. private InspectableFieldStyleInfo style;
  17. /// <summary>
  18. /// Creates a new inspectable resource reference GUI for the specified property.
  19. /// </summary>
  20. /// <param name="context">Context shared by all inspectable fields created by the same parent.</param>
  21. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  22. /// <param name="path">Full path to this property (includes name of this property and all parent properties).</param>
  23. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  24. /// contain other fields, in which case you should increase this value by one.</param>
  25. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  26. /// <param name="property">Serializable property referencing the field whose contents to display.</param>
  27. /// <param name="style">Contains information about the field style.</param>
  28. public InspectableRRef(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  29. SerializableProperty property, InspectableFieldStyleInfo style)
  30. : base(context, title, path, SerializableProperty.FieldType.RRef, depth, layout, property)
  31. {
  32. this.style = style;
  33. }
  34. /// <inheritoc/>
  35. protected internal override void Initialize(int layoutIndex)
  36. {
  37. if (property.Type == SerializableProperty.FieldType.RRef)
  38. {
  39. System.Type type = property.InternalType;
  40. if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(RRef<>))
  41. type = type.GenericTypeArguments[0];
  42. guiField = new GUIResourceField(type, new GUIContent(title));
  43. guiField.OnChanged += OnFieldValueChanged;
  44. layout.AddElement(layoutIndex, guiField);
  45. }
  46. }
  47. /// <inheritdoc/>
  48. public override InspectableState Refresh(int layoutIndex)
  49. {
  50. if (guiField != null)
  51. guiField.ValueRef = property.GetValue<RRefBase>();
  52. InspectableState oldState = state;
  53. if (state.HasFlag(InspectableState.Modified))
  54. state = InspectableState.NotModified;
  55. return oldState;
  56. }
  57. /// <summary>
  58. /// Triggered when the user drops a new resource onto the field, or clears the current value.
  59. /// </summary>
  60. /// <param name="newValue">New resource to reference.</param>
  61. private void OnFieldValueChanged(RRefBase newValue)
  62. {
  63. if (newValue != null && !newValue.IsLoaded && style.StyleFlags.HasFlag(InspectableFieldStyleFlags.LoadOnAssign))
  64. Resources.Load<Resource>(newValue.UUID);
  65. StartUndo();
  66. property.SetValue(newValue);
  67. state = InspectableState.Modified;
  68. EndUndo();
  69. }
  70. }
  71. /** @} */
  72. }