BsGUITextField.cpp 6.9 KB

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