BsGUIFloatField.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if (!_isDisabled())
  39. {
  40. Rect2I draggableArea;
  41. if (mLabel != nullptr)
  42. draggableArea = mLabel->_getLayoutData().area;
  43. if (draggableArea.contains(position))
  44. {
  45. type = CursorType::ArrowLeftRight;
  46. return true;
  47. }
  48. }
  49. return false;
  50. }
  51. bool GUIFloatField::_mouseEvent(const GUIMouseEvent& event)
  52. {
  53. GUIElementContainer::_mouseEvent(event);
  54. Rect2I draggableArea;
  55. if(mLabel != nullptr)
  56. draggableArea = mLabel->_getLayoutData().area;
  57. if(event.getType() == GUIMouseEventType::MouseDragStart)
  58. {
  59. if(draggableArea.contains(event.getDragStartPosition()))
  60. {
  61. mLastDragPos = event.getPosition().x;
  62. mIsDragging = true;
  63. }
  64. return true;
  65. }
  66. else if(event.getType() == GUIMouseEventType::MouseDrag)
  67. {
  68. if(mIsDragging)
  69. {
  70. INT32 xDiff = event.getPosition().x - mLastDragPos;
  71. INT32 jumpAmount = 0;
  72. if(event.getPosition().x < 0)
  73. {
  74. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  75. cursorScreenPos.x += _getParentWidget()->getTarget()->getWidth();
  76. jumpAmount = _getParentWidget()->getTarget()->getWidth();
  77. Cursor::instance().setScreenPosition(cursorScreenPos);
  78. }
  79. else if(event.getPosition().x >= _getParentWidget()->getTarget()->getWidth())
  80. {
  81. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  82. cursorScreenPos.x -= _getParentWidget()->getTarget()->getWidth();
  83. jumpAmount = -_getParentWidget()->getTarget()->getWidth();
  84. Cursor::instance().setScreenPosition(cursorScreenPos);
  85. }
  86. float oldValue = getValue();
  87. float newValue = oldValue + xDiff * DRAG_SPEED;
  88. mLastDragPos = event.getPosition().x + jumpAmount;
  89. if (oldValue != newValue)
  90. {
  91. setValue(newValue);
  92. valueChanged(newValue);
  93. }
  94. }
  95. return true;
  96. }
  97. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  98. {
  99. mIsDragging = false;
  100. return true;
  101. }
  102. return false;
  103. }
  104. void GUIFloatField::setValue(float value)
  105. {
  106. mValue = Math::clamp(value, mMinValue, mMaxValue);
  107. // Only update with new value if it actually changed, otherwise
  108. // problems can occur when user types in "0." and the field
  109. // updates back to "0" effectively making "." unusable
  110. float curValue = parseFloat(mInputBox->getText());
  111. if (mValue != curValue)
  112. mInputBox->setText(toWString(mValue));
  113. }
  114. void GUIFloatField::setRange(float min, float max)
  115. {
  116. mMinValue = min;
  117. mMaxValue = max;
  118. }
  119. void GUIFloatField::setTint(const Color& color)
  120. {
  121. if (mLabel != nullptr)
  122. mLabel->setTint(color);
  123. mInputBox->setTint(color);
  124. }
  125. void GUIFloatField::updateClippedBounds()
  126. {
  127. mClippedBounds = mLayoutData.area;
  128. }
  129. const String& GUIFloatField::getGUITypeName()
  130. {
  131. static String typeName = "GUIFloatField";
  132. return typeName;
  133. }
  134. const String& GUIFloatField::getInputStyleType()
  135. {
  136. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  137. return LABEL_STYLE_TYPE;
  138. }
  139. void GUIFloatField::styleUpdated()
  140. {
  141. if (mLabel != nullptr)
  142. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  143. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  144. }
  145. void GUIFloatField::valueChanged(const WString& newValue)
  146. {
  147. valueChanged(parseFloat(newValue));
  148. }
  149. void GUIFloatField::valueChanged(float newValue)
  150. {
  151. CmdInputFieldValueChange<GUIFloatField, float>::execute(this, newValue);
  152. if (!onValueChanged.empty())
  153. onValueChanged(newValue);
  154. }
  155. void GUIFloatField::focusGained()
  156. {
  157. UndoRedo::instance().pushGroup("InputBox");
  158. mHasInputFocus = true;
  159. }
  160. void GUIFloatField::focusLost()
  161. {
  162. UndoRedo::instance().popGroup("InputBox");
  163. mHasInputFocus = false;
  164. }
  165. bool GUIFloatField::floatFilter(const WString& str)
  166. {
  167. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  168. }
  169. }