GUITextBox.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. namespace BansheeEngine
  6. {
  7. /** @addtogroup GUI_Engine
  8. * @{
  9. */
  10. /// <summary>
  11. /// GUI element that accepts Unicode textual input. It can be single or multi-line and handles various types of text
  12. /// manipulation.
  13. /// </summary>
  14. public sealed class GUITextBox : GUIElement
  15. {
  16. /// <summary>
  17. /// Triggered whenever input text has changed.
  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. /// Creates a new text box element.
  26. /// </summary>
  27. /// <param name="multiline">Determines should the input box allow text that spans multiple lines.</param>
  28. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  29. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  30. /// default element style is used.</param>
  31. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  32. /// override any similar options set by style.</param>
  33. public GUITextBox(bool multiline, string style, params GUIOption[] options)
  34. {
  35. Internal_CreateInstance(this, multiline, style, options);
  36. }
  37. /// <summary>
  38. /// Creates a new text box element.
  39. /// </summary>
  40. /// <param name="multiline">Determines should the input box allow text that spans multiple lines.</param>
  41. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  42. /// override any similar options set by style.</param>
  43. public GUITextBox(bool multiline, params GUIOption[] options)
  44. {
  45. Internal_CreateInstance(this, multiline, "", options);
  46. }
  47. /// <summary>
  48. /// Creates a new single-line text box element.
  49. /// </summary>
  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 GUITextBox(string style, params GUIOption[] options)
  56. {
  57. Internal_CreateInstance(this, false, style, options);
  58. }
  59. /// <summary>
  60. /// Creates a new single-line text box element.
  61. /// </summary>
  62. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  63. /// override any similar options set by style.</param>
  64. public GUITextBox(params GUIOption[] options)
  65. {
  66. Internal_CreateInstance(this, false, "", options);
  67. }
  68. /// <summary>
  69. /// Text currently entered in the text box.
  70. /// </summary>
  71. public string Text
  72. {
  73. get { string value; Internal_GetText(mCachedPtr, out value); return value; }
  74. set { Internal_SetText(mCachedPtr, value); }
  75. }
  76. /// <summary>
  77. /// Colors the element with a specific tint.
  78. /// </summary>
  79. /// <param name="color">Tint to apply to the element.</param>
  80. public void SetTint(Color color)
  81. {
  82. Internal_SetTint(mCachedPtr, ref color);
  83. }
  84. /// <summary>
  85. /// Triggered by the native interop object when the text box value is changed.
  86. /// </summary>
  87. /// <param name="newValue">New value in the text box.</param>
  88. private void Internal_DoOnChanged(string newValue)
  89. {
  90. if (OnChanged != null)
  91. OnChanged(newValue);
  92. }
  93. /// <summary>
  94. /// Triggered by the native interop object when the user confirms the input.
  95. /// </summary>
  96. private void Internal_DoOnConfirmed()
  97. {
  98. if (OnConfirmed != null)
  99. OnConfirmed();
  100. }
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. private static extern void Internal_CreateInstance(GUITextBox instance, bool multiline, string style, GUIOption[] options);
  103. [MethodImpl(MethodImplOptions.InternalCall)]
  104. private static extern void Internal_SetText(IntPtr nativeInstance, string text);
  105. [MethodImpl(MethodImplOptions.InternalCall)]
  106. private static extern void Internal_GetText(IntPtr nativeInstance, out string text);
  107. [MethodImpl(MethodImplOptions.InternalCall)]
  108. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  109. }
  110. /** @} */
  111. }