GUIResourceField.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Editor GUI element that displays a reference to a <see cref="Resource"/> and an optional label. Resources can
  8. /// be dragged and dropped onto the field to update the reference.
  9. /// </summary>
  10. public sealed class GUIResourceField : GUIElement
  11. {
  12. public delegate void OnChangedDelegate(ResourceRef newValue);
  13. /// <summary>
  14. /// Triggered when the value in the field changes.
  15. /// </summary>
  16. public event OnChangedDelegate OnChanged;
  17. /// <summary>
  18. /// <see cref="Resource"/> referenced by the field. This will load the resource if it is not already loaded. Use
  19. /// <see cref="ValueRef"/> if you don't require a loaded resource.
  20. /// </summary>
  21. public Resource Value
  22. {
  23. get
  24. {
  25. Resource value;
  26. Internal_GetValue(mCachedPtr, out value);
  27. return value;
  28. }
  29. set { Internal_SetValue(mCachedPtr, value); }
  30. }
  31. /// <summary>
  32. /// Reference to the <see cref="Resource"/> referenced by the field.
  33. /// </summary>
  34. public ResourceRef ValueRef
  35. {
  36. get
  37. {
  38. ResourceRef value;
  39. Internal_GetValueRef(mCachedPtr, out value);
  40. return value;
  41. }
  42. set { Internal_SetValueRef(mCachedPtr, value); }
  43. }
  44. /// <summary>
  45. /// Creates a new resource field element with a label.
  46. /// </summary>
  47. /// <param name="type">Specific type of <see cref="Resource"/> this field accepts.</param>
  48. /// <param name="title">Content to display on the label.</param>
  49. /// <param name="titleWidth">Width of the title label in pixels.</param>
  50. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  51. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  52. /// default element style is used.</param>
  53. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  54. /// override any similar options set by style.</param>
  55. public GUIResourceField(Type type, GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  56. {
  57. Internal_CreateInstance(this, type, title, titleWidth, style, options, true);
  58. }
  59. /// <summary>
  60. /// Creates a new resource field element without a label.
  61. /// </summary>
  62. /// <param name="type">Specific type of <see cref="Resource"/> this field accepts.</param>
  63. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  64. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  65. /// default element style is used.</param>
  66. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  67. /// override any similar options set by style.</param>
  68. public GUIResourceField(Type type, string style = "", params GUIOption[] options)
  69. {
  70. Internal_CreateInstance(this, type, null, 0, style, options, false);
  71. }
  72. /// <summary>
  73. /// Colors the element with a specific tint.
  74. /// </summary>
  75. /// <param name="color">Tint to apply to the element.</param>
  76. public void SetTint(Color color)
  77. {
  78. Internal_SetTint(mCachedPtr, ref color);
  79. }
  80. /// <summary>
  81. /// Triggered by the runtime when the value of the field changes.
  82. /// </summary>
  83. /// <param name="newValue">New resource referenced by the field.</param>
  84. private void DoOnChanged(ResourceRef newValue)
  85. {
  86. if (OnChanged != null)
  87. OnChanged(newValue);
  88. }
  89. [MethodImpl(MethodImplOptions.InternalCall)]
  90. private static extern void Internal_CreateInstance(GUIResourceField instance, Type type, GUIContent title, int titleWidth,
  91. string style, GUIOption[] options, bool withTitle);
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern void Internal_GetValue(IntPtr nativeInstance, out Resource value);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. private static extern void Internal_SetValue(IntPtr nativeInstance, Resource value);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. private static extern void Internal_GetValueRef(IntPtr nativeInstance, out ResourceRef value);
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. private static extern void Internal_SetValueRef(IntPtr nativeInstance, ResourceRef value);
  100. [MethodImpl(MethodImplOptions.InternalCall)]
  101. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  102. }
  103. }