BsGUITextField.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsEditorPrerequisites.h"
  5. #include "GUI/BsGUIElementContainer.h"
  6. namespace bs
  7. {
  8. /** @addtogroup GUI-Editor
  9. * @{
  10. */
  11. /**
  12. * A composite GUI object representing an editor field. Editor fields are a combination of a label and an input field.
  13. * Label is optional. This specific implementation displays a text input field.
  14. */
  15. class BS_ED_EXPORT GUITextField : public GUIElementContainer
  16. {
  17. struct PrivatelyConstruct {};
  18. public:
  19. /** Returns type name of the GUI element used for finding GUI element styles. */
  20. static const String& getGUITypeName();
  21. /** Returns style type name of the internal GUILabel element. */
  22. static const String& getLabelStyleType();
  23. /** Returns style type name of the internal GUIInputBox element. */
  24. static const String& getInputStyleType();
  25. /**
  26. * Creates a new GUI text editor field with a label.
  27. *
  28. * @param[in] multiline Should the input box accept multiple lines of input.
  29. * @param[in] labelContent Content to display in the editor field label.
  30. * @param[in] labelWidth Width of the label in pixels.
  31. * @param[in] options Options that allow you to control how is the element positioned and sized. This will
  32. * override any similar options set by style.
  33. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  34. * GUIWidget the element is used on. If not specified default style is used.
  35. */
  36. static GUITextField* create(bool multiline, const GUIContent& labelContent, UINT32 labelWidth, const GUIOptions& options,
  37. const String& style = StringUtil::BLANK);
  38. /**
  39. * Creates a new GUI text editor field with a label.
  40. *
  41. * @param[in] multiline Should the input box accept multiple lines of input.
  42. * @param[in] labelContent Content to display in the editor field label.
  43. * @param[in] options Options that allow you to control how is the element positioned and sized.
  44. * This will override any similar options set by style.
  45. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  46. * GUIWidget the element is used on. If not specified default style is used.
  47. */
  48. static GUITextField* create(bool multiline, const GUIContent& labelContent, const GUIOptions& options,
  49. const String& style = StringUtil::BLANK);
  50. /**
  51. * Creates a new GUI text editor field with a label.
  52. *
  53. * @param[in] multiline Should the input box accept multiple lines of input.
  54. * @param[in] labelText String to display in the editor field label.
  55. * @param[in] labelWidth Width of the label in pixels.
  56. * @param[in] options Options that allow you to control how is the element positioned and sized.
  57. * This will override any similar options set by style.
  58. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  59. * GUIWidget the element is used on. If not specified default style is used.
  60. */
  61. static GUITextField* create(bool multiline, const HString& labelText, UINT32 labelWidth, const GUIOptions& options,
  62. const String& style = StringUtil::BLANK);
  63. /**
  64. * Creates a new GUI text editor field with a label.
  65. *
  66. * @param[in] multiline Should the input box accept multiple lines of input.
  67. * @param[in] labelText String to display in the editor field label.
  68. * @param[in] options Options that allow you to control how is the element positioned and sized.
  69. * This will override any similar options set by style.
  70. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  71. * GUIWidget the element is used on. If not specified default style is used.
  72. */
  73. static GUITextField* create(bool multiline, const HString& labelText, const GUIOptions& options,
  74. const String& style = StringUtil::BLANK);
  75. /**
  76. * Creates a new GUI text editor field without a label.
  77. *
  78. * @param[in] multiline Should the input box accept multiple lines of input.
  79. * @param[in] options Options that allow you to control how is the element positioned and sized.
  80. * This will override any similar options set by style.
  81. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  82. * GUIWidget the element is used on. If not specified default style is used.
  83. */
  84. static GUITextField* create(bool multiline, const GUIOptions& options, const String& style = StringUtil::BLANK);
  85. /**
  86. * Creates a new GUI text editor field with a label.
  87. *
  88. * @param[in] multiline Should the input box accept multiple lines of input.
  89. * @param[in] labelContent Content to display in the editor field label.
  90. * @param[in] labelWidth Width of the label in pixels.
  91. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  92. * GUIWidget the element is used on. If not specified default style is used.
  93. */
  94. static GUITextField* create(bool multiline, const GUIContent& labelContent, UINT32 labelWidth,
  95. const String& style = StringUtil::BLANK);
  96. /**
  97. * Creates a new GUI text editor field with a label.
  98. *
  99. * @param[in] multiline Should the input box accept multiple lines of input.
  100. * @param[in] labelContent Content to display in the editor field label.
  101. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  102. * GUIWidget the element is used on. If not specified default style is used.
  103. */
  104. static GUITextField* create(bool multiline, const GUIContent& labelContent,
  105. const String& style = StringUtil::BLANK);
  106. /**
  107. * Creates a new GUI text editor field with a label.
  108. *
  109. * @param[in] multiline Should the input box accept multiple lines of input.
  110. * @param[in] labelText String to display in the editor field label.
  111. * @param[in] labelWidth Width of the label in pixels.
  112. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  113. * GUIWidget the element is used on. If not specified default style is used.
  114. */
  115. static GUITextField* create(bool multiline, const HString& labelText, UINT32 labelWidth,
  116. const String& style = StringUtil::BLANK);
  117. /**
  118. * Creates a new GUI text editor field with a label.
  119. *
  120. * @param[in] multiline Should the input box accept multiple lines of input.
  121. * @param[in] labelText String to display in the editor field label.
  122. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  123. * GUIWidget the element is used on. If not specified default style is used.
  124. */
  125. static GUITextField* create(bool multiline, const HString& labelText,
  126. const String& style = StringUtil::BLANK);
  127. /**
  128. * Creates a new GUI text editor field without a label.
  129. *
  130. * @param[in] multiline Should the input box accept multiple lines of input.
  131. * @param[in] style Optional style to use for the element. Style will be retrieved from GUISkin of the
  132. * GUIWidget the element is used on. If not specified default style is used.
  133. */
  134. static GUITextField* create(bool multiline, const String& style = StringUtil::BLANK);
  135. GUITextField(const PrivatelyConstruct& dummy, bool multiline, const GUIContent& labelContent, UINT32 labelWidth,
  136. const String& style, const GUIDimensions& dimensions, bool withLabel);
  137. /** Returns the value currently input in the text field. */
  138. String getValue() const { return mValue; }
  139. /** Sets a new value in the text field. */
  140. void setValue(const String& value);
  141. /** Checks whether the element currently has keyboard focus. */
  142. bool hasInputFocus() const { return mHasInputFocus; }
  143. /** @copydoc GUIElement::setTint */
  144. void setTint(const Color& color) override;
  145. Event<void(const String&)> onValueChanged; /** Triggered when the value in the field changes. */
  146. Event<void()> onConfirm; /**< Triggered when the user hits the Enter key with the input box in focus. */
  147. /** @name Internal
  148. * @{
  149. */
  150. /**
  151. * Sets a new value in the input field, and also allows you to choose should the field trigger an onValueChanged
  152. * event.
  153. */
  154. void _setValue(const String& value, bool triggerEvent);
  155. /** @} */
  156. protected:
  157. static const UINT32 DEFAULT_LABEL_WIDTH;
  158. /** @copydoc GUIElement::styleUpdated */
  159. void styleUpdated() override;
  160. /** @copydoc GUIElement::_updateLayoutInternal */
  161. void _updateLayoutInternal(const GUILayoutData& data) override;
  162. /** @copydoc GUIElement::_getOptimalSize */
  163. Vector2I _getOptimalSize() const override;
  164. /** Triggered when the value in the internal input box changes. */
  165. void valueChanged(const String& newValue);
  166. /** Triggers when the input box receives or loses keyboard focus. */
  167. void focusChanged(bool focus);
  168. /** Triggered when the users confirms input in the input box. */
  169. void inputConfirmed();
  170. GUIInputBox* mInputBox;
  171. GUILayout* mLayout;
  172. GUILabel* mLabel;
  173. bool mHasInputFocus;
  174. String mValue;
  175. };
  176. /** @} */
  177. }