BsGUIFloatField.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "BsCGUIWidget.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsCursor.h"
  10. #include "BsCGUIWidget.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), mMinValue(std::numeric_limits<float>::lowest()),
  22. mMaxValue(std::numeric_limits<float>::max())
  23. {
  24. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getInputStyleType()));
  25. mInputBox->setFilter(&GUIFloatField::floatFilter);
  26. mInputBox->onValueChanged.connect(std::bind((void(GUIFloatField::*)(const WString&))&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->_getLayoutData().area;
  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->_getLayoutData().area;
  54. if(event.getType() == GUIMouseEventType::MouseDragStart)
  55. {
  56. if(draggableArea.contains(event.getDragStartPosition()))
  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. {
  88. setValue(newValue);
  89. valueChanged(newValue);
  90. }
  91. }
  92. return true;
  93. }
  94. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  95. {
  96. mIsDragging = false;
  97. return true;
  98. }
  99. return false;
  100. }
  101. void GUIFloatField::setValue(float value)
  102. {
  103. mValue = Math::clamp(value, mMinValue, mMaxValue);
  104. // Only update with new value if it actually changed, otherwise
  105. // problems can occur when user types in "0." and the field
  106. // updates back to "0" effectively making "." unusable
  107. float curValue = parseFloat(mInputBox->getText());
  108. if (mValue != curValue)
  109. mInputBox->setText(toWString(mValue));
  110. }
  111. void GUIFloatField::setRange(float min, float max)
  112. {
  113. mMinValue = min;
  114. mMaxValue = max;
  115. }
  116. void GUIFloatField::setTint(const Color& color)
  117. {
  118. if (mLabel != nullptr)
  119. mLabel->setTint(color);
  120. mInputBox->setTint(color);
  121. }
  122. void GUIFloatField::updateClippedBounds()
  123. {
  124. mClippedBounds = mLayoutData.area;
  125. }
  126. const String& GUIFloatField::getGUITypeName()
  127. {
  128. static String typeName = "GUIFloatField";
  129. return typeName;
  130. }
  131. const String& GUIFloatField::getInputStyleType()
  132. {
  133. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  134. return LABEL_STYLE_TYPE;
  135. }
  136. void GUIFloatField::styleUpdated()
  137. {
  138. if (mLabel != nullptr)
  139. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  140. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  141. }
  142. void GUIFloatField::valueChanged(const WString& newValue)
  143. {
  144. valueChanged(parseFloat(newValue));
  145. }
  146. void GUIFloatField::valueChanged(float newValue)
  147. {
  148. CmdInputFieldValueChange<GUIFloatField, float>::execute(this, newValue);
  149. if (!onValueChanged.empty())
  150. onValueChanged(newValue);
  151. }
  152. void GUIFloatField::focusGained()
  153. {
  154. UndoRedo::instance().pushGroup("InputBox");
  155. mHasInputFocus = true;
  156. }
  157. void GUIFloatField::focusLost()
  158. {
  159. UndoRedo::instance().popGroup("InputBox");
  160. mHasInputFocus = false;
  161. }
  162. bool GUIFloatField::floatFilter(const WString& str)
  163. {
  164. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  165. }
  166. }