GUITextBox.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. public delegate void OnChangedDelegate(string newValue);
  12. /// <summary>
  13. /// Triggered whenever input text has changed.
  14. /// </summary>
  15. public event OnChangedDelegate OnChanged;
  16. /// <summary>
  17. /// Creates a new text box element.
  18. /// </summary>
  19. /// <param name="multiline">Determines should the input box allow text that spans multiple lines.
  20. /// </param>
  21. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  22. /// default layout options. Style will be retrieved from the active GUISkin. If not specified default element style
  23. /// is used.
  24. /// </param>
  25. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  26. /// override any similar options set by style.
  27. /// </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.
  36. /// </param>
  37. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  38. /// override any similar options set by style.
  39. /// </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 default element style
  49. /// is used.
  50. /// </param>
  51. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  52. /// override any similar options set by style.
  53. /// </param>
  54. public GUITextBox(string style, params GUIOption[] options)
  55. {
  56. Internal_CreateInstance(this, false, style, options);
  57. }
  58. /// <summary>
  59. /// Creates a new single-line text box element.
  60. /// </summary>
  61. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  62. /// override any similar options set by style.
  63. /// </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, 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 DoOnChanged(string newValue)
  89. {
  90. if (OnChanged != null)
  91. OnChanged(newValue);
  92. }
  93. [MethodImpl(MethodImplOptions.InternalCall)]
  94. private static extern void Internal_CreateInstance(GUITextBox instance, bool multiline, string style, GUIOption[] options);
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern void Internal_SetText(IntPtr nativeInstance, string text);
  97. [MethodImpl(MethodImplOptions.InternalCall)]
  98. private static extern void Internal_GetText(IntPtr nativeInstance, out string text);
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  101. }
  102. }