InspectableTexture.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. namespace bs.Editor
  4. {
  5. /** @addtogroup Inspector
  6. * @{
  7. */
  8. /// <summary>
  9. /// Displays GUI for a serializable property containing a <see cref="Texture"/> reference.
  10. /// </summary>
  11. [CustomInspector(typeof(Texture))]
  12. public class InspectableTexture : InspectableField
  13. {
  14. private GUITextureField guiField;
  15. private InspectableState state;
  16. /// <summary>
  17. /// Creates a new inspectable texture reference GUI for the specified property.
  18. /// </summary>
  19. /// <param name="context">Context shared by all inspectable fields created by the same parent.</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 InspectableTexture(InspectableContext context, string title, string path, int depth, InspectableFieldLayout layout,
  27. SerializableProperty property, InspectableFieldStyleInfo style)
  28. : base(context, title, path, property.Type, depth, layout, property)
  29. { }
  30. /// <inheritoc/>
  31. protected internal override void Initialize(int layoutIndex)
  32. {
  33. guiField = new GUITextureField(GUITextureFieldType.Texture, new GUIContent(title));
  34. guiField.OnChanged += OnFieldValueChanged;
  35. layout.AddElement(layoutIndex, guiField);
  36. }
  37. /// <inheritdoc/>
  38. public override InspectableState Refresh(int layoutIndex)
  39. {
  40. if (guiField != null)
  41. {
  42. if (property.Type == SerializableProperty.FieldType.Resource)
  43. guiField.Texture = property.GetValue<Texture>();
  44. else
  45. guiField.TextureRef = property.GetValue<RRef<Texture>>();
  46. }
  47. InspectableState oldState = state;
  48. if (state.HasFlag(InspectableState.Modified))
  49. state = InspectableState.NotModified;
  50. return oldState;
  51. }
  52. /// <summary>
  53. /// Triggered when the user drops a new resource onto the field, or clears the current value.
  54. /// </summary>
  55. /// <param name="newValue">New resource to reference.</param>
  56. private void OnFieldValueChanged(RRefBase newValue)
  57. {
  58. StartUndo();
  59. if (property.Type == SerializableProperty.FieldType.Resource)
  60. property.SetValue(newValue.GenericValue);
  61. else
  62. property.SetValue(newValue);
  63. state = InspectableState.Modified;
  64. EndUndo();
  65. }
  66. }
  67. /** @} */
  68. }