GUITextureField.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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="Texture"/> or <see cref="SpriteTexture"/> and an
  13. /// optional label. Textures can be dragged and dropped onto the field to update the reference. This is similar
  14. /// to <see cref="GUIResourceField"/> but the will display the texture contents and not only the name.
  15. /// </summary>
  16. public sealed class GUITextureField : GUIElement
  17. {
  18. public delegate void OnChangedDelegate(RRef<Resource> newValue);
  19. /// <summary>
  20. /// Triggered when the value in the field changes.
  21. /// </summary>
  22. public event OnChangedDelegate OnChanged;
  23. /// <summary>
  24. /// Resource referenced by the field, which could be either a normal or a sprite texture. This will load the
  25. /// resource if it is not already loaded. Use <see cref="ValueRef"/> if you don't require a loaded resource.
  26. /// </summary>
  27. public Resource Value
  28. {
  29. get
  30. {
  31. Resource value;
  32. Internal_GetValue(mCachedPtr, out value);
  33. return value;
  34. }
  35. set { Internal_SetValue(mCachedPtr, value); }
  36. }
  37. /// <summary>
  38. /// Handle to the resource referenced by the field, which could be either a normal or a sprite texture.
  39. /// </summary>
  40. public RRef<Resource> ValueRef
  41. {
  42. get
  43. {
  44. RRef<Resource> value;
  45. Internal_GetValueRef(mCachedPtr, out value);
  46. return value;
  47. }
  48. set { Internal_SetValueRef(mCachedPtr, value); }
  49. }
  50. /// <summary>
  51. /// <see cref="Texture"/> referenced by the field. This will load the resource if it is not already loaded. Use
  52. /// <see cref="TextureRef"/> if you don't require a loaded resource. Returns null if the field contains a
  53. /// <see cref="SpriteTexture"/> instead.
  54. /// </summary>
  55. public Texture Texture
  56. {
  57. get
  58. {
  59. Texture value;
  60. Internal_GetTexture(mCachedPtr, out value);
  61. return value;
  62. }
  63. set { Internal_SetTexture(mCachedPtr, value); }
  64. }
  65. /// <summary>
  66. /// Handle to the <see cref="Texture"/> referenced by the field. Returns null if the field contains a
  67. /// <see cref="SpriteTexture"/> instead.
  68. /// </summary>
  69. public RRef<Texture> TextureRef
  70. {
  71. get
  72. {
  73. RRef<Texture> value;
  74. Internal_GetTextureRef(mCachedPtr, out value);
  75. return value;
  76. }
  77. set { Internal_SetTextureRef(mCachedPtr, value); }
  78. }
  79. /// <summary>
  80. /// <see cref="SpriteTexture"/> referenced by the field. This will load the resource if it is not already loaded.
  81. /// Use <see cref="SpriteTextureRef"/> if you don't require a loaded resource. Returns null if the field contains a
  82. /// <see cref="Texture"/> instead.
  83. /// </summary>
  84. public SpriteTexture SpriteTexture
  85. {
  86. get
  87. {
  88. SpriteTexture value;
  89. Internal_GetSpriteTexture(mCachedPtr, out value);
  90. return value;
  91. }
  92. set { Internal_SetSpriteTexture(mCachedPtr, value); }
  93. }
  94. /// <summary>
  95. /// Handle to the <see cref="SpriteTexture"/> referenced by the field. Returns null if the field contains a
  96. /// <see cref="Texture"/> instead.
  97. /// </summary>
  98. public RRef<SpriteTexture> SpriteTextureRef
  99. {
  100. get
  101. {
  102. RRef<SpriteTexture> value;
  103. Internal_GetSpriteTextureRef(mCachedPtr, out value);
  104. return value;
  105. }
  106. set { Internal_SetSpriteTextureRef(mCachedPtr, value); }
  107. }
  108. /// <summary>
  109. /// Creates a new texture field element with a label. The field will accept textures but not sprite textures.
  110. /// </summary>
  111. /// <param name="title">Content to display on the label.</param>
  112. /// <param name="titleWidth">Width of the title label in pixels.</param>
  113. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  114. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  115. /// default element style is used.</param>
  116. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  117. /// override any similar options set by style.</param>
  118. public GUITextureField(GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  119. {
  120. Internal_CreateInstance(this, GUITextureFieldType.Texture, ref title, titleWidth, style, options, true);
  121. }
  122. /// <summary>
  123. /// Creates a new texture field element without a label. The field will accept textures but not sprite textures.
  124. /// </summary>
  125. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  126. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  127. /// default element style is used.</param>
  128. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  129. /// override any similar options set by style.</param>
  130. public GUITextureField(string style = "", params GUIOption[] options)
  131. {
  132. GUIContent emptyContent = new GUIContent();
  133. Internal_CreateInstance(this, GUITextureFieldType.Texture, ref emptyContent, 0, style, options, false);
  134. }
  135. /// <summary>
  136. /// Creates a new texture field element with a label.
  137. /// </summary>
  138. /// <param name="type">Determines the type of textures should the field accept.</param>
  139. /// <param name="title">Content to display on the label.</param>
  140. /// <param name="titleWidth">Width of the title label in pixels.</param>
  141. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  142. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  143. /// default element style is used.</param>
  144. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  145. /// override any similar options set by style.</param>
  146. public GUITextureField(GUITextureFieldType type, GUIContent title,
  147. int titleWidth = 100, string style = "", params GUIOption[] options)
  148. {
  149. Internal_CreateInstance(this, type, ref title, titleWidth, style, options, true);
  150. }
  151. /// <summary>
  152. /// Creates a new texture field element without a label.
  153. /// </summary>
  154. /// <param name="type">Determines the type of textures should the field accept.</param>
  155. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  156. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  157. /// default element style is used.</param>
  158. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  159. /// override any similar options set by style.</param>
  160. public GUITextureField(GUITextureFieldType type, string style = "", params GUIOption[] options)
  161. {
  162. GUIContent emptyContent = new GUIContent();
  163. Internal_CreateInstance(this, type, ref emptyContent, 0, style, options, false);
  164. }
  165. /// <summary>
  166. /// Colors the element with a specific tint.
  167. /// </summary>
  168. /// <param name="color">Tint to apply to the element.</param>
  169. public void SetTint(Color color)
  170. {
  171. Internal_SetTint(mCachedPtr, ref color);
  172. }
  173. /// <summary>
  174. /// Triggered by the runtime when the value of the field changes.
  175. /// </summary>
  176. /// <param name="newValue">New resource referenced by the field.</param>
  177. private void Internal_DoOnChanged(RRef<Resource> newValue)
  178. {
  179. if (OnChanged != null)
  180. OnChanged(newValue);
  181. }
  182. [MethodImpl(MethodImplOptions.InternalCall)]
  183. private static extern void Internal_CreateInstance(GUITextureField instance, GUITextureFieldType type,
  184. ref GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  185. [MethodImpl(MethodImplOptions.InternalCall)]
  186. private static extern void Internal_GetSpriteTexture(IntPtr nativeInstance, out SpriteTexture value);
  187. [MethodImpl(MethodImplOptions.InternalCall)]
  188. private static extern void Internal_SetSpriteTexture(IntPtr nativeInstance, SpriteTexture value);
  189. [MethodImpl(MethodImplOptions.InternalCall)]
  190. private static extern void Internal_GetSpriteTextureRef(IntPtr nativeInstance, out RRef<SpriteTexture> value);
  191. [MethodImpl(MethodImplOptions.InternalCall)]
  192. private static extern void Internal_SetSpriteTextureRef(IntPtr nativeInstance, RRef<SpriteTexture> value);
  193. [MethodImpl(MethodImplOptions.InternalCall)]
  194. private static extern void Internal_GetTexture(IntPtr nativeInstance, out Texture value);
  195. [MethodImpl(MethodImplOptions.InternalCall)]
  196. private static extern void Internal_SetTexture(IntPtr nativeInstance, Texture value);
  197. [MethodImpl(MethodImplOptions.InternalCall)]
  198. private static extern void Internal_GetTextureRef(IntPtr nativeInstance, out RRef<Texture> value);
  199. [MethodImpl(MethodImplOptions.InternalCall)]
  200. private static extern void Internal_SetTextureRef(IntPtr nativeInstance, RRef<Texture> value);
  201. [MethodImpl(MethodImplOptions.InternalCall)]
  202. private static extern void Internal_GetValue(IntPtr nativeInstance, out Resource value);
  203. [MethodImpl(MethodImplOptions.InternalCall)]
  204. private static extern void Internal_SetValue(IntPtr nativeInstance, Resource value);
  205. [MethodImpl(MethodImplOptions.InternalCall)]
  206. private static extern void Internal_GetValueRef(IntPtr nativeInstance, out RRef<Resource> value);
  207. [MethodImpl(MethodImplOptions.InternalCall)]
  208. private static extern void Internal_SetValueRef(IntPtr nativeInstance, RRef<Resource> value);
  209. [MethodImpl(MethodImplOptions.InternalCall)]
  210. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  211. }
  212. /** @} */
  213. }