GUIFloatField.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 floating point input field and an optional label.
  13. /// </summary>
  14. public sealed class GUIFloatField : GUIElement
  15. {
  16. /// <summary>
  17. /// Triggered when the value in the field changes.
  18. /// </summary>
  19. public event Action<float> OnChanged;
  20. /// <summary>
  21. /// Triggered whenever user confirms input.
  22. /// </summary>
  23. public event Action OnConfirmed;
  24. /// <summary>
  25. /// Value displayed by the field input box.
  26. /// </summary>
  27. public float Value
  28. {
  29. get { return Internal_GetValue(mCachedPtr); }
  30. set { Internal_SetValue(mCachedPtr, value); }
  31. }
  32. /// <summary>
  33. /// Minimum change of the field.
  34. /// </summary>
  35. public float Step
  36. {
  37. get { return Internal_GetStep(mCachedPtr); }
  38. set { Internal_SetStep(mCachedPtr, value); }
  39. }
  40. /// <summary>
  41. /// Checks does the element currently has input focus. Input focus means the element has an input caret displayed
  42. /// and will accept input from the keyboard.
  43. /// </summary>
  44. public bool HasInputFocus
  45. {
  46. get { return Internal_HasInputFocus(mCachedPtr); }
  47. }
  48. /// <summary>
  49. /// Creates a new float field element with a label.
  50. /// </summary>
  51. /// <param name="title">Content to display on the label.</param>
  52. /// <param name="titleWidth">Width of the title label in pixels.</param>
  53. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  54. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  55. /// default element style is used.</param>
  56. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  57. /// override any similar options set by style.</param>
  58. public GUIFloatField(GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  59. {
  60. Internal_CreateInstance(this, ref title, titleWidth, style, options, true);
  61. }
  62. /// <summary>
  63. /// Creates a new float field element without a label.
  64. /// </summary>
  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 GUIFloatField(string style = "", params GUIOption[] options)
  71. {
  72. GUIContent emptyContent = new GUIContent();
  73. Internal_CreateInstance(this, ref emptyContent, 0, style, options, false);
  74. }
  75. /// <summary>
  76. /// Colors the element with a specific tint.
  77. /// </summary>
  78. /// <param name="color">Tint to apply to the element.</param>
  79. public void SetTint(Color color)
  80. {
  81. Internal_SetTint(mCachedPtr, ref color);
  82. }
  83. /// <summary>
  84. /// Sets a range that will input field values will be clamped to. Set to large negative/positive values if clamping
  85. /// is not required.
  86. /// </summary>
  87. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  88. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  89. public void SetRange(float min, float max)
  90. {
  91. Internal_SetRange(mCachedPtr, min, max);
  92. }
  93. /// <summary>
  94. /// Triggered by the runtime when the value of the float field changes.
  95. /// </summary>
  96. /// <param name="newValue">New value of the float field.</param>
  97. private void Internal_DoOnChanged(float newValue)
  98. {
  99. if (OnChanged != null)
  100. OnChanged(newValue);
  101. }
  102. /// <summary>
  103. /// Triggered by the native interop object when the user confirms the input.
  104. /// </summary>
  105. private void Internal_DoOnConfirmed()
  106. {
  107. if (OnConfirmed != null)
  108. OnConfirmed();
  109. }
  110. [MethodImpl(MethodImplOptions.InternalCall)]
  111. private static extern void Internal_CreateInstance(GUIFloatField instance, ref GUIContent title, int titleWidth,
  112. string style, GUIOption[] options, bool withTitle);
  113. [MethodImpl(MethodImplOptions.InternalCall)]
  114. private static extern float Internal_GetValue(IntPtr nativeInstance);
  115. [MethodImpl(MethodImplOptions.InternalCall)]
  116. private static extern float Internal_SetValue(IntPtr nativeInstance, float value);
  117. [MethodImpl(MethodImplOptions.InternalCall)]
  118. private static extern bool Internal_HasInputFocus(IntPtr nativeInstance);
  119. [MethodImpl(MethodImplOptions.InternalCall)]
  120. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  121. [MethodImpl(MethodImplOptions.InternalCall)]
  122. private static extern void Internal_SetRange(IntPtr nativeInstance, float min, float max);
  123. [MethodImpl(MethodImplOptions.InternalCall)]
  124. private static extern void Internal_SetStep(IntPtr nativeInstance, float step);
  125. [MethodImpl(MethodImplOptions.InternalCall)]
  126. private static extern float Internal_GetStep(IntPtr nativeInstance);
  127. }
  128. /** @} */
  129. }