2
0

GUITextField.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Editor GUI element that displays a textual input field and an optional label.
  8. /// </summary>
  9. public sealed class GUITextField : GUIElement
  10. {
  11. /// <summary>
  12. /// Triggered when the value in the field changes.
  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. /// Value displayed by the field input box.
  21. /// </summary>
  22. public string Value
  23. {
  24. get
  25. {
  26. String value;
  27. Internal_GetValue(mCachedPtr, out value);
  28. return value;
  29. }
  30. set { Internal_SetValue(mCachedPtr, value); }
  31. }
  32. /// <summary>
  33. /// Checks does the element currently has input focus. Input focus means the element has an input caret displayed
  34. /// and will accept input from the keyboard.
  35. /// </summary>
  36. public bool HasInputFocus
  37. {
  38. get
  39. {
  40. bool value;
  41. Internal_HasInputFocus(mCachedPtr, out value);
  42. return value;
  43. }
  44. }
  45. /// <summary>
  46. /// Creates a new text field element with a label.
  47. /// </summary>
  48. /// <param name="title">Content to display on the label.</param>
  49. /// <param name="titleWidth">Width of the title label in pixels.</param>
  50. /// <param name="multiline">Determines should the input field accept multiple lines of text.</param>
  51. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  52. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  53. /// default element style is used.</param>
  54. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  55. /// override any similar options set by style.</param>
  56. public GUITextField(GUIContent title, int titleWidth = 100, bool multiline = false, string style = "", params GUIOption[] options)
  57. {
  58. Internal_CreateInstance(this, multiline, title, titleWidth, style, options, true);
  59. }
  60. /// <summary>
  61. /// Creates a new text field element without a label.
  62. /// </summary>
  63. /// <param name="multiline">Determines should the input field accept multiple lines of text.</param>
  64. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  65. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  66. /// default element style is used.</param>
  67. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  68. /// override any similar options set by style.</param>
  69. public GUITextField(bool multiline = false, string style = "", params GUIOption[] options)
  70. {
  71. Internal_CreateInstance(this, multiline, null, 0, style, options, false);
  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, color);
  80. }
  81. /// <summary>
  82. /// Triggered by the runtime when the value of the text field changes.
  83. /// </summary>
  84. /// <param name="newValue">New value of the text field.</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(GUITextField instance, bool multiline, GUIContent title, int titleWidth,
  100. string style, GUIOption[] options, bool withTitle);
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. private static extern void Internal_GetValue(IntPtr nativeInstance, out String value);
  103. [MethodImpl(MethodImplOptions.InternalCall)]
  104. private static extern void Internal_SetValue(IntPtr nativeInstance, String value);
  105. [MethodImpl(MethodImplOptions.InternalCall)]
  106. private static extern void Internal_HasInputFocus(IntPtr nativeInstance, out bool value);
  107. [MethodImpl(MethodImplOptions.InternalCall)]
  108. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  109. }
  110. }