BsGUITextField.cpp 6.9 KB

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