GUIResourceField.cs 5.4 KB

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