GUIFloatField.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Editor GUI element that displays a floating point input field and an optional label.
  8. /// </summary>
  9. public sealed class GUIFloatField : GUIElement
  10. {
  11. public delegate void OnChangedDelegate(float newValue);
  12. /// <summary>
  13. /// Triggered when the value in the field changes.
  14. /// </summary>
  15. public event OnChangedDelegate OnChanged;
  16. /// <summary>
  17. /// Value displayed by the field input box.
  18. /// </summary>
  19. public float Value
  20. {
  21. get
  22. {
  23. float value;
  24. Internal_GetValue(mCachedPtr, out value);
  25. return value;
  26. }
  27. set { Internal_SetValue(mCachedPtr, value); }
  28. }
  29. /// <summary>
  30. /// Creates a new float field element with a label.
  31. /// </summary>
  32. /// <param name="title">Content to display on the label.</param>
  33. /// <param name="titleWidth">Width of the title label in pixels.</param>
  34. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  35. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  36. /// default element style is used.</param>
  37. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  38. /// override any similar options set by style.</param>
  39. public GUIFloatField(GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  40. {
  41. Internal_CreateInstance(this, title, titleWidth, style, options, true);
  42. }
  43. /// <summary>
  44. /// Creates a new float field element without a label.
  45. /// </summary>
  46. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  47. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  48. /// default element style is used.</param>
  49. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  50. /// override any similar options set by style.</param>
  51. public GUIFloatField(string style = "", params GUIOption[] options)
  52. {
  53. Internal_CreateInstance(this, null, 0, style, options, false);
  54. }
  55. /// <summary>
  56. /// Checks does the element currently has input focus. Input focus means the element has an input caret displayed
  57. /// and will accept input from the keyboard.
  58. /// </summary>
  59. /// <returns>True if the element has input focus.</returns>
  60. public bool HasInputFocus()
  61. {
  62. bool value;
  63. Internal_HasInputFocus(mCachedPtr, out value);
  64. return value;
  65. }
  66. /// <summary>
  67. /// Colors the element with a specific tint.
  68. /// </summary>
  69. /// <param name="color">Tint to apply to the element.</param>
  70. public void SetTint(Color color)
  71. {
  72. Internal_SetTint(mCachedPtr, color);
  73. }
  74. /// <summary>
  75. /// Sets a range that will input field values will be clamped to. Set to large negative/positive values if clamping
  76. /// is not required.
  77. /// </summary>
  78. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  79. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  80. public void SetRange(float min, float max)
  81. {
  82. Internal_SetRange(mCachedPtr, min, max);
  83. }
  84. /// <summary>
  85. /// Triggered by the runtime when the value of the float field changes.
  86. /// </summary>
  87. /// <param name="newValue">New value of the float field.</param>
  88. private void DoOnChanged(float newValue)
  89. {
  90. if (OnChanged != null)
  91. OnChanged(newValue);
  92. }
  93. [MethodImpl(MethodImplOptions.InternalCall)]
  94. private static extern void Internal_CreateInstance(GUIFloatField instance, GUIContent title, int titleWidth,
  95. string style, GUIOption[] options, bool withTitle);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. private static extern void Internal_GetValue(IntPtr nativeInstance, out float value);
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. private static extern void Internal_SetValue(IntPtr nativeInstance, float value);
  100. [MethodImpl(MethodImplOptions.InternalCall)]
  101. private static extern void Internal_HasInputFocus(IntPtr nativeInstance, out bool value);
  102. [MethodImpl(MethodImplOptions.InternalCall)]
  103. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. private static extern void Internal_SetRange(IntPtr nativeInstance, float min, float max);
  106. }
  107. }