GUIIntField.cs 5.7 KB

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