BsGUITextField.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/BsUndoRedo.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::create(bool multiline, const GUIContent& labelContent, UINT32 labelWidth, const GUIOptions& options,
  31. const String& style)
  32. {
  33. const String* curStyle = &style;
  34. if (*curStyle == StringUtil::BLANK)
  35. curStyle = &GUITextField::getGUITypeName();
  36. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, labelContent, labelWidth, *curStyle,
  37. GUIDimensions::create(options), true);
  38. }
  39. GUITextField* GUITextField::create(bool multiline, const GUIContent& labelContent, const GUIOptions& options,
  40. const String& style)
  41. {
  42. const String* curStyle = &style;
  43. if (*curStyle == StringUtil::BLANK)
  44. curStyle = &GUITextField::getGUITypeName();
  45. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  46. GUIDimensions::create(options), true);
  47. }
  48. GUITextField* GUITextField::create(bool multiline, const HString& labelText, UINT32 labelWidth, const GUIOptions& options,
  49. const String& style)
  50. {
  51. const String* curStyle = &style;
  52. if (*curStyle == StringUtil::BLANK)
  53. curStyle = &GUITextField::getGUITypeName();
  54. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(labelText), labelWidth, *curStyle,
  55. GUIDimensions::create(options), true);
  56. }
  57. GUITextField* GUITextField::create(bool multiline, const HString& labelText, const GUIOptions& options,
  58. const String& style)
  59. {
  60. const String* curStyle = &style;
  61. if (*curStyle == StringUtil::BLANK)
  62. curStyle = &GUITextField::getGUITypeName();
  63. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  64. GUIDimensions::create(options), true);
  65. }
  66. GUITextField* GUITextField::create(bool multiline, const GUIOptions& options, const String& style)
  67. {
  68. const String* curStyle = &style;
  69. if (*curStyle == StringUtil::BLANK)
  70. curStyle = &GUITextField::getGUITypeName();
  71. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(), 0, *curStyle,
  72. GUIDimensions::create(options), false);
  73. }
  74. GUITextField* GUITextField::create(bool multiline, const GUIContent& labelContent, UINT32 labelWidth,
  75. const String& style)
  76. {
  77. const String* curStyle = &style;
  78. if (*curStyle == StringUtil::BLANK)
  79. curStyle = &GUITextField::getGUITypeName();
  80. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, labelContent, labelWidth, *curStyle,
  81. GUIDimensions::create(), true);
  82. }
  83. GUITextField* GUITextField::create(bool multiline, const GUIContent& labelContent,
  84. const String& style)
  85. {
  86. const String* curStyle = &style;
  87. if (*curStyle == StringUtil::BLANK)
  88. curStyle = &GUITextField::getGUITypeName();
  89. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, labelContent, DEFAULT_LABEL_WIDTH, *curStyle,
  90. GUIDimensions::create(), true);
  91. }
  92. GUITextField* GUITextField::create(bool multiline, const HString& labelText, UINT32 labelWidth,
  93. const String& style)
  94. {
  95. const String* curStyle = &style;
  96. if (*curStyle == StringUtil::BLANK)
  97. curStyle = &GUITextField::getGUITypeName();
  98. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(labelText), labelWidth, *curStyle,
  99. GUIDimensions::create(), true);
  100. }
  101. GUITextField* GUITextField::create(bool multiline, const HString& labelText,
  102. const String& style)
  103. {
  104. const String* curStyle = &style;
  105. if (*curStyle == StringUtil::BLANK)
  106. curStyle = &GUITextField::getGUITypeName();
  107. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(labelText), DEFAULT_LABEL_WIDTH, *curStyle,
  108. GUIDimensions::create(), true);
  109. }
  110. GUITextField* GUITextField::create(bool multiline, const String& style)
  111. {
  112. const String* curStyle = &style;
  113. if (*curStyle == StringUtil::BLANK)
  114. curStyle = &GUITextField::getGUITypeName();
  115. return bs_new<GUITextField>(PrivatelyConstruct(), multiline, GUIContent(), 0, *curStyle,
  116. GUIDimensions::create(), false);
  117. }
  118. void GUITextField::setValue(const String& value)
  119. {
  120. mValue = value;
  121. mInputBox->setText(value);
  122. }
  123. void GUITextField::setTint(const Color& color)
  124. {
  125. if (mLabel != nullptr)
  126. mLabel->setTint(color);
  127. mInputBox->setTint(color);
  128. }
  129. void GUITextField::_setValue(const String& value, bool triggerEvent)
  130. {
  131. setValue(value);
  132. if (triggerEvent)
  133. onValueChanged(value);
  134. }
  135. void GUITextField::_updateLayoutInternal(const GUILayoutData& data)
  136. {
  137. mLayout->_setLayoutData(data);
  138. mLayout->_updateLayoutInternal(data);
  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 String& newValue)
  151. {
  152. _setValue(newValue, true);
  153. }
  154. void GUITextField::focusChanged(bool focus)
  155. {
  156. if (focus)
  157. {
  158. mHasInputFocus = true;
  159. onFocusChanged(true);
  160. }
  161. else
  162. {
  163. mHasInputFocus = false;
  164. onFocusChanged(false);
  165. }
  166. }
  167. void GUITextField::inputConfirmed()
  168. {
  169. onConfirm();
  170. }
  171. const String& GUITextField::getGUITypeName()
  172. {
  173. static String typeName = "GUITextField";
  174. return typeName;
  175. }
  176. const String& GUITextField::getInputStyleType()
  177. {
  178. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  179. return LABEL_STYLE_TYPE;
  180. }
  181. const String& GUITextField::getLabelStyleType()
  182. {
  183. static String LABEL_STYLE_TYPE = "EditorFieldLabel";
  184. return LABEL_STYLE_TYPE;
  185. }
  186. }