BsGUITextField.cpp 6.9 KB

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