GUITextBox.cs 5.1 KB

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