GUITextField.cs 5.5 KB

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