BsGUITextField.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GUI/BsGUITextField.h"
  4. #include "GUI/BsGUILayoutX.h"
  5. #include "GUI/BsGUILabel.h"
  6. #include "GUI/BsGUIInputBox.h"
  7. #include "UndoRedo/BsCmdInputFieldValueChange.h"
  8. using namespace std::placeholders;
  9. namespace bs
  10. {
  11. const UINT32 GUITextField::DEFAULT_LABEL_WIDTH = 100;
  12. GUITextField::GUITextField(const PrivatelyConstruct& dummy, bool multiline, const GUIContent& labelContent,
  13. UINT32 labelWidth, const String& style, const GUIDimensions& dimensions, bool withLabel)
  14. :GUIElementContainer(dimensions, style),
  15. mInputBox(nullptr), mLayout(nullptr), mLabel(nullptr), mHasInputFocus(false), mValue("")
  16. {
  17. mLayout = GUILayoutX::create();
  18. _registerChildElement(mLayout);
  19. if (withLabel)
  20. {
  21. mLabel = GUILabel::create(labelContent, GUIOptions(GUIOption::fixedWidth(labelWidth)), getSubStyleName(getLabelStyleType()));
  22. mLayout->addElement(mLabel);
  23. }
  24. mInputBox = GUIInputBox::create(multiline, getSubStyleName(getInputStyleType()));
  25. mLayout->addElement(mInputBox);
  26. mInputBox->onValueChanged.connect(std::bind(&GUITextField::valueChanged, this, _1));
  27. mInputBox->onFocusChanged.connect(std::bind(&GUITextField::focusChanged, this, _1));
  28. mInputBox->onConfirm.connect(std::bind(&GUITextField::inputConfirmed, this));
  29. }
  30. GUITextField::~GUITextField()
  31. {
  32. }
  33. GUITextField* GUITextField::create(bool multiline, const GUIContent& labelContent, UINT32 labelWidth, const GUIOptions& options,
  34. const String& style)
  35. {
  36. const String* curStyle = &style;
  37. if (*curStyle == StringUtil::BLANK)
  38. curStyle = &GUITextField::getGUITypeName();
  39. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, labelContent, labelWidth, *curStyle,
  40. GUIDimensions::create(options), true);
  41. }
  42. GUITextField* GUITextField::create(bool multiline, const GUIContent& labelContent, const GUIOptions& options,
  43. const String& style)
  44. {
  45. const String* curStyle = &style;
  46. if (*curStyle == StringUtil::BLANK)
  47. curStyle = &GUITextField::getGUITypeName();
  48. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  49. GUIDimensions::create(options), true);
  50. }
  51. GUITextField* GUITextField::create(bool multiline, const HString& labelText, UINT32 labelWidth, const GUIOptions& options,
  52. const String& style)
  53. {
  54. const String* curStyle = &style;
  55. if (*curStyle == StringUtil::BLANK)
  56. curStyle = &GUITextField::getGUITypeName();
  57. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(labelText), labelWidth, *curStyle,
  58. GUIDimensions::create(options), true);
  59. }
  60. GUITextField* GUITextField::create(bool multiline, const HString& labelText, const GUIOptions& options,
  61. const String& style)
  62. {
  63. const String* curStyle = &style;
  64. if (*curStyle == StringUtil::BLANK)
  65. curStyle = &GUITextField::getGUITypeName();
  66. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  67. GUIDimensions::create(options), true);
  68. }
  69. GUITextField* GUITextField::create(bool multiline, const GUIOptions& options, const String& style)
  70. {
  71. const String* curStyle = &style;
  72. if (*curStyle == StringUtil::BLANK)
  73. curStyle = &GUITextField::getGUITypeName();
  74. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(), 0, *curStyle,
  75. GUIDimensions::create(options), false);
  76. }
  77. GUITextField* GUITextField::create(bool multiline, const GUIContent& labelContent, UINT32 labelWidth,
  78. const String& style)
  79. {
  80. const String* curStyle = &style;
  81. if (*curStyle == StringUtil::BLANK)
  82. curStyle = &GUITextField::getGUITypeName();
  83. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, labelContent, labelWidth, *curStyle,
  84. GUIDimensions::create(), true);
  85. }
  86. GUITextField* GUITextField::create(bool multiline, const GUIContent& labelContent,
  87. const String& style)
  88. {
  89. const String* curStyle = &style;
  90. if (*curStyle == StringUtil::BLANK)
  91. curStyle = &GUITextField::getGUITypeName();
  92. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  93. GUIDimensions::create(), true);
  94. }
  95. GUITextField* GUITextField::create(bool multiline, const HString& labelText, UINT32 labelWidth,
  96. const String& style)
  97. {
  98. const String* curStyle = &style;
  99. if (*curStyle == StringUtil::BLANK)
  100. curStyle = &GUITextField::getGUITypeName();
  101. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(labelText), labelWidth, *curStyle,
  102. GUIDimensions::create(), true);
  103. }
  104. GUITextField* GUITextField::create(bool multiline, const HString& labelText,
  105. const String& style)
  106. {
  107. const String* curStyle = &style;
  108. if (*curStyle == StringUtil::BLANK)
  109. curStyle = &GUITextField::getGUITypeName();
  110. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  111. GUIDimensions::create(), true);
  112. }
  113. GUITextField* GUITextField::create(bool multiline, const String& style)
  114. {
  115. const String* curStyle = &style;
  116. if (*curStyle == StringUtil::BLANK)
  117. curStyle = &GUITextField::getGUITypeName();
  118. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(), 0, *curStyle,
  119. GUIDimensions::create(), false);
  120. }
  121. void GUITextField::setValue(const String& value)
  122. {
  123. mValue = value;
  124. mInputBox->setText(value);
  125. }
  126. void GUITextField::setTint(const Color& color)
  127. {
  128. if (mLabel != nullptr)
  129. mLabel->setTint(color);
  130. mInputBox->setTint(color);
  131. }
  132. void GUITextField::_setValue(const String& value, bool triggerEvent)
  133. {
  134. setValue(value);
  135. if (triggerEvent)
  136. onValueChanged(value);
  137. }
  138. void GUITextField::_updateLayoutInternal(const GUILayoutData& data)
  139. {
  140. mLayout->_setLayoutData(data);
  141. mLayout->_updateLayoutInternal(data);
  142. }
  143. Vector2I GUITextField::_getOptimalSize() const
  144. {
  145. return mLayout->_getOptimalSize();
  146. }
  147. void GUITextField::styleUpdated()
  148. {
  149. if (mLabel != nullptr)
  150. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  151. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  152. }
  153. void GUITextField::valueChanged(const String& newValue)
  154. {
  155. CmdInputFieldValueChange<GUITextField, String>::execute(this, newValue);
  156. }
  157. void GUITextField::focusChanged(bool focus)
  158. {
  159. if (focus)
  160. {
  161. UndoRedo::instance().pushGroup("InputBox");
  162. mHasInputFocus = true;
  163. onFocusChanged(true);
  164. }
  165. else
  166. {
  167. UndoRedo::instance().popGroup("InputBox");
  168. mHasInputFocus = false;
  169. onFocusChanged(false);
  170. }
  171. }
  172. void GUITextField::inputConfirmed()
  173. {
  174. onConfirm();
  175. }
  176. const String& GUITextField::getGUITypeName()
  177. {
  178. static String typeName = "GUITextField";
  179. return typeName;
  180. }
  181. const String& GUITextField::getInputStyleType()
  182. {
  183. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  184. return LABEL_STYLE_TYPE;
  185. }
  186. const String& GUITextField::getLabelStyleType()
  187. {
  188. static String LABEL_STYLE_TYPE = "EditorFieldLabel";
  189. return LABEL_STYLE_TYPE;
  190. }
  191. }