BsGUITextField.h 9.5 KB

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