BsGUIFloatField.cpp 5.1 KB

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