GUITextField.cs 5.5 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. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// Editor GUI element that displays a textual input field and an optional label.
  10. /// </summary>
  11. public sealed class GUITextField : GUIElement
  12. {
  13. /// <summary>
  14. /// Triggered when the value in the field changes.
  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. /// Value displayed by the field input box.
  23. /// </summary>
  24. public string Value
  25. {
  26. get
  27. {
  28. String value;
  29. Internal_GetValue(mCachedPtr, out value);
  30. return value;
  31. }
  32. set { Internal_SetValue(mCachedPtr, value); }
  33. }
  34. /// <summary>
  35. /// Checks does the element currently has input focus. Input focus means the element has an input caret displayed
  36. /// and will accept input from the keyboard.
  37. /// </summary>
  38. public bool HasInputFocus
  39. {
  40. get
  41. {
  42. bool value;
  43. Internal_HasInputFocus(mCachedPtr, out value);
  44. return value;
  45. }
  46. }
  47. /// <summary>
  48. /// Creates a new text field element with a label.
  49. /// </summary>
  50. /// <param name="title">Content to display on the label.</param>
  51. /// <param name="titleWidth">Width of the title label in pixels.</param>
  52. /// <param name="multiline">Determines should the input field accept multiple lines of text.</param>
  53. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  54. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  55. /// default element style is used.</param>
  56. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  57. /// override any similar options set by style.</param>
  58. public GUITextField(GUIContent title, int titleWidth = 100, bool multiline = false, string style = "", params GUIOption[] options)
  59. {
  60. Internal_CreateInstance(this, multiline, title, titleWidth, style, options, true);
  61. }
  62. /// <summary>
  63. /// Creates a new text field element without a label.
  64. /// </summary>
  65. /// <param name="multiline">Determines should the input field accept multiple lines of text.</param>
  66. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  67. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  68. /// default element style is used.</param>
  69. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  70. /// override any similar options set by style.</param>
  71. public GUITextField(bool multiline = false, string style = "", params GUIOption[] options)
  72. {
  73. Internal_CreateInstance(this, multiline, null, 0, style, options, false);
  74. }
  75. /// <summary>
  76. /// Colors the element with a specific tint.
  77. /// </summary>
  78. /// <param name="color">Tint to apply to the element.</param>
  79. public void SetTint(Color color)
  80. {
  81. Internal_SetTint(mCachedPtr, ref color);
  82. }
  83. /// <summary>
  84. /// Triggered by the runtime when the value of the text field changes.
  85. /// </summary>
  86. /// <param name="newValue">New value of the text field.</param>
  87. private void Internal_DoOnChanged(String newValue)
  88. {
  89. if (OnChanged != null)
  90. OnChanged(newValue);
  91. }
  92. /// <summary>
  93. /// Triggered by the native interop object when the user confirms the input.
  94. /// </summary>
  95. private void Internal_DoOnConfirmed()
  96. {
  97. if (OnConfirmed != null)
  98. OnConfirmed();
  99. }
  100. [MethodImpl(MethodImplOptions.InternalCall)]
  101. private static extern void Internal_CreateInstance(GUITextField instance, bool multiline, GUIContent title, int titleWidth,
  102. string style, GUIOption[] options, bool withTitle);
  103. [MethodImpl(MethodImplOptions.InternalCall)]
  104. private static extern void Internal_GetValue(IntPtr nativeInstance, out String value);
  105. [MethodImpl(MethodImplOptions.InternalCall)]
  106. private static extern void Internal_SetValue(IntPtr nativeInstance, String value);
  107. [MethodImpl(MethodImplOptions.InternalCall)]
  108. private static extern void Internal_HasInputFocus(IntPtr nativeInstance, out bool value);
  109. [MethodImpl(MethodImplOptions.InternalCall)]
  110. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  111. }
  112. }