GUIResourceField.cs 5.5 KB

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