GUITextBox.cs 5.2 KB

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