BsGUITextField.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. using namespace CamelotFramework;
  11. using namespace BansheeEngine;
  12. namespace BansheeEditor
  13. {
  14. GUITextField::GUITextField(const PrivatelyConstruct& dummy, GUIWidget& parent, const GUIContent& labelContent,
  15. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions)
  16. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mInputBox(nullptr), mLabelWidth(100)
  17. {
  18. const GUIElementStyle* curLabelStyle = labelStyle;
  19. const GUIElementStyle* curInputBoxStyle = inputBoxStyle;
  20. if(curLabelStyle == nullptr)
  21. curLabelStyle = parent.getSkin().getStyle("Label");
  22. if(curInputBoxStyle == nullptr)
  23. curInputBoxStyle = parent.getSkin().getStyle("InputBox");
  24. mLabel = GUILabel::create(parent, labelContent, curLabelStyle);
  25. mInputBox = GUIInputBox::create(parent, false, inputBoxStyle);
  26. _registerChildElement(mLabel);
  27. _registerChildElement(mInputBox);
  28. }
  29. GUITextField::GUITextField(const PrivatelyConstruct& dummy, GUIWidget& parent,
  30. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions)
  31. :GUIElementContainer(parent, layoutOptions), mLabel(nullptr), mInputBox(nullptr), mLabelWidth(100)
  32. {
  33. const GUIElementStyle* curInputBoxStyle = inputBoxStyle;
  34. if(curInputBoxStyle == nullptr)
  35. curInputBoxStyle = parent.getSkin().getStyle("InputBox");
  36. mInputBox = GUIInputBox::create(parent, false, inputBoxStyle);
  37. _registerChildElement(mInputBox);
  38. }
  39. GUITextField::~GUITextField()
  40. {
  41. }
  42. GUITextField* GUITextField::create(GUIWidget& parent, const GUIContent& labelContent, const GUIOptions& layoutOptions,
  43. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  44. {
  45. return cm_new<GUITextField>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  46. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  47. }
  48. GUITextField* GUITextField::create(GUIWidget& parent, const GUIContent& labelContent, GUIElementStyle* labelStyle,
  49. GUIElementStyle* inputBoxStyle)
  50. {
  51. return cm_new<GUITextField>(PrivatelyConstruct(), parent, labelContent, labelStyle, inputBoxStyle,
  52. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  53. }
  54. GUITextField* GUITextField::create(GUIWidget& parent, const HString& labelContent, const GUIOptions& layoutOptions,
  55. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  56. {
  57. return cm_new<GUITextField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle,
  58. inputBoxStyle, GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  59. }
  60. GUITextField* GUITextField::create(GUIWidget& parent, const HString& labelContent, GUIElementStyle* labelStyle,
  61. GUIElementStyle* inputBoxStyle)
  62. {
  63. return cm_new<GUITextField>(PrivatelyConstruct(), parent, GUIContent(labelContent), labelStyle, inputBoxStyle,
  64. GUILayoutOptions::create(&GUISkin::DefaultStyle));
  65. }
  66. GUITextField* GUITextField::create(GUIWidget& parent, const GUIOptions& layoutOptions, GUIElementStyle* labelStyle,
  67. GUIElementStyle* inputBoxStyle)
  68. {
  69. return cm_new<GUITextField>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle,
  70. GUILayoutOptions::create(layoutOptions, &GUISkin::DefaultStyle));
  71. }
  72. GUITextField* GUITextField::create(GUIWidget& parent, GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle)
  73. {
  74. return cm_new<GUITextField>(PrivatelyConstruct(), parent, labelStyle, inputBoxStyle, GUILayoutOptions::create(&GUISkin::DefaultStyle));
  75. }
  76. WString GUITextField::getValue() const
  77. {
  78. return mInputBox->getText();
  79. }
  80. void GUITextField::setValue(const WString& value)
  81. {
  82. mInputBox->setText(value);
  83. }
  84. void GUITextField::setLabelWidth(UINT32 width)
  85. {
  86. mLabelWidth = width;
  87. markContentAsDirty();
  88. }
  89. void GUITextField::_updateLayoutInternal(INT32 x, INT32 y, UINT32 width, UINT32 height,
  90. RectI clipRect, UINT8 widgetDepth, UINT16 areaDepth)
  91. {
  92. UINT32 inputBoxOffset = 0;
  93. UINT32 inputBoxWidth = width;
  94. if(mLabel != nullptr)
  95. {
  96. UINT32 labelWidth = mLabelWidth;
  97. Vector2I optimalSize = mLabel->_getOptimalSize();
  98. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  99. Vector2I offset(x, y + yOffset);
  100. mLabel->_setOffset(offset);
  101. mLabel->_setWidth(labelWidth);
  102. mLabel->_setHeight(optimalSize.y);
  103. mLabel->_setAreaDepth(areaDepth);
  104. mLabel->_setWidgetDepth(widgetDepth);
  105. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  106. mLabel->_setClipRect(elemClipRect);
  107. inputBoxOffset = labelWidth;
  108. inputBoxWidth = width - labelWidth;
  109. }
  110. Vector2I inputBoxSize = mInputBox->_getOptimalSize();
  111. {
  112. Vector2I optimalSize = mInputBox->_getOptimalSize();
  113. INT32 yOffset = Math::roundToInt((height - optimalSize.y) * 0.5f);
  114. Vector2I offset(x + inputBoxOffset, y + yOffset);
  115. mInputBox->_setOffset(offset);
  116. mInputBox->_setWidth(inputBoxWidth);
  117. mInputBox->_setHeight(optimalSize.y);
  118. mInputBox->_setAreaDepth(areaDepth);
  119. mInputBox->_setWidgetDepth(widgetDepth);
  120. RectI elemClipRect(clipRect.x - offset.x, clipRect.y - offset.y, clipRect.width, clipRect.height);
  121. mInputBox->_setClipRect(elemClipRect);
  122. }
  123. }
  124. Vector2I GUITextField::_getOptimalSize() const
  125. {
  126. Vector2I optimalsize = mInputBox->_getOptimalSize();
  127. if(mLabel != nullptr)
  128. {
  129. optimalsize.x += mLabel->_getOptimalSize().x;
  130. optimalsize.y = std::max(optimalsize.y, mLabel->_getOptimalSize().y);
  131. }
  132. return optimalsize;
  133. }
  134. const String& GUITextField::getGUITypeName()
  135. {
  136. static String typeName = "GUITextField";
  137. return typeName;
  138. }
  139. }