GUIIntField.cs 5.5 KB

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