GUIGameObjectField.cs 4.6 KB

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