GUIGameObjectField.cs 4.7 KB

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