BsGUIFloatField.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "BsGUIFloatField.h"
  2. #include "BsGUILayout.h"
  3. #include "BsGUILabel.h"
  4. #include "BsGUIInputBox.h"
  5. #include "BsGUISpace.h"
  6. #include "BsBuiltinResources.h"
  7. #include "BsGUIWidget.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsCursor.h"
  10. #include "BsGUIWidget.h"
  11. #include "BsViewport.h"
  12. #include "BsCmdInputFieldValueChange.h"
  13. #include <regex>
  14. using namespace std::placeholders;
  15. namespace BansheeEngine
  16. {
  17. const float GUIFloatField::DRAG_SPEED = 0.05f;
  18. GUIFloatField::GUIFloatField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
  19. const String& style, const GUIDimensions& dimensions, bool withLabel)
  20. :TGUIField(dummy, labelContent, labelWidth, style, dimensions, withLabel), mInputBox(nullptr), mIsDragging(false),
  21. mLastDragPos(0), mHasInputFocus(false), mValue(0.0f)
  22. {
  23. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getInputStyleType()));
  24. mInputBox->setFilter(&GUIFloatField::floatFilter);
  25. mInputBox->onValueChanged.connect(std::bind((void(GUIFloatField::*)(const WString&))&GUIFloatField::valueChanged, this, _1));
  26. mInputBox->onFocusGained.connect(std::bind(&GUIFloatField::focusGained, this));
  27. mInputBox->onFocusLost.connect(std::bind(&GUIFloatField::focusLost, this));
  28. mLayout->addElement(mInputBox);
  29. setValue(0);
  30. mInputBox->setText(L"0");
  31. }
  32. GUIFloatField::~GUIFloatField()
  33. {
  34. }
  35. bool GUIFloatField::_hasCustomCursor(const Vector2I position, CursorType& type) const
  36. {
  37. Rect2I draggableArea;
  38. if(mLabel != nullptr)
  39. draggableArea = mLabel->_getLayoutData().area;
  40. if(draggableArea.contains(position))
  41. {
  42. type = CursorType::ArrowLeftRight;
  43. return true;
  44. }
  45. return false;
  46. }
  47. bool GUIFloatField::_mouseEvent(const GUIMouseEvent& event)
  48. {
  49. GUIElementContainer::_mouseEvent(event);
  50. Rect2I draggableArea;
  51. if(mLabel != nullptr)
  52. draggableArea = mLabel->_getLayoutData().area;
  53. if(event.getType() == GUIMouseEventType::MouseDragStart)
  54. {
  55. if(draggableArea.contains(event.getDragStartPosition()))
  56. {
  57. mLastDragPos = event.getPosition().x;
  58. mIsDragging = true;
  59. }
  60. return true;
  61. }
  62. else if(event.getType() == GUIMouseEventType::MouseDrag)
  63. {
  64. if(mIsDragging)
  65. {
  66. INT32 xDiff = event.getPosition().x - mLastDragPos;
  67. INT32 jumpAmount = 0;
  68. if(event.getPosition().x < 0)
  69. {
  70. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  71. cursorScreenPos.x += _getParentWidget()->getTarget()->getWidth();
  72. jumpAmount = _getParentWidget()->getTarget()->getWidth();
  73. Cursor::instance().setScreenPosition(cursorScreenPos);
  74. }
  75. else if(event.getPosition().x >= _getParentWidget()->getTarget()->getWidth())
  76. {
  77. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  78. cursorScreenPos.x -= _getParentWidget()->getTarget()->getWidth();
  79. jumpAmount = -_getParentWidget()->getTarget()->getWidth();
  80. Cursor::instance().setScreenPosition(cursorScreenPos);
  81. }
  82. float oldValue = getValue();
  83. float newValue = oldValue + xDiff * DRAG_SPEED;
  84. mLastDragPos = event.getPosition().x + jumpAmount;
  85. if (oldValue != newValue)
  86. {
  87. setValue(newValue);
  88. valueChanged(newValue);
  89. }
  90. }
  91. return true;
  92. }
  93. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  94. {
  95. mIsDragging = false;
  96. return true;
  97. }
  98. return false;
  99. }
  100. void GUIFloatField::setValue(float value)
  101. {
  102. mValue = value;
  103. // Only update with new value if it actually changed, otherwise
  104. // problems can occur when user types in "0." and the field
  105. // updates back to "0" effectively making "." unusable
  106. float curValue = parseFloat(mInputBox->getText());
  107. if (mValue != curValue)
  108. mInputBox->setText(toWString(mValue));
  109. }
  110. void GUIFloatField::setTint(const Color& color)
  111. {
  112. if (mLabel != nullptr)
  113. mLabel->setTint(color);
  114. mInputBox->setTint(color);
  115. }
  116. void GUIFloatField::updateClippedBounds()
  117. {
  118. mClippedBounds = mLayoutData.area;
  119. }
  120. const String& GUIFloatField::getGUITypeName()
  121. {
  122. static String typeName = "GUIFloatField";
  123. return typeName;
  124. }
  125. const String& GUIFloatField::getInputStyleType()
  126. {
  127. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  128. return LABEL_STYLE_TYPE;
  129. }
  130. void GUIFloatField::styleUpdated()
  131. {
  132. if (mLabel != nullptr)
  133. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  134. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  135. }
  136. void GUIFloatField::valueChanged(const WString& newValue)
  137. {
  138. valueChanged(parseFloat(newValue));
  139. }
  140. void GUIFloatField::valueChanged(float newValue)
  141. {
  142. CmdInputFieldValueChange<GUIFloatField, float>::execute(this, newValue);
  143. if (!onValueChanged.empty())
  144. onValueChanged(newValue);
  145. }
  146. void GUIFloatField::focusGained()
  147. {
  148. UndoRedo::instance().pushGroup("InputBox");
  149. mHasInputFocus = true;
  150. }
  151. void GUIFloatField::focusLost()
  152. {
  153. UndoRedo::instance().popGroup("InputBox");
  154. mHasInputFocus = false;
  155. }
  156. bool GUIFloatField::floatFilter(const WString& str)
  157. {
  158. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  159. }
  160. }