BsGUITextField.h 9.3 KB

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