BsGUIFloatField.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(value));
  107. }
  108. void GUIFloatField::updateClippedBounds()
  109. {
  110. Vector2I offset = _getOffset();
  111. mClippedBounds = Rect2I(offset.x, offset.y, _getWidth(), _getHeight());
  112. }
  113. const String& GUIFloatField::getGUITypeName()
  114. {
  115. static String typeName = "GUIFloatField";
  116. return typeName;
  117. }
  118. const String& GUIFloatField::getInputStyleType()
  119. {
  120. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  121. return LABEL_STYLE_TYPE;
  122. }
  123. void GUIFloatField::styleUpdated()
  124. {
  125. if (mLabel != nullptr)
  126. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  127. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  128. }
  129. void GUIFloatField::valueChanged(const WString& newValue)
  130. {
  131. float newFloatValue = parseFloat(newValue);
  132. CmdInputFieldValueChange<GUIFloatField, float>::execute(this, newFloatValue);
  133. if (!onValueChanged.empty())
  134. onValueChanged(newFloatValue);
  135. }
  136. void GUIFloatField::focusGained()
  137. {
  138. UndoRedo::instance().pushGroup("InputBox");
  139. mHasInputFocus = true;
  140. }
  141. void GUIFloatField::focusLost()
  142. {
  143. UndoRedo::instance().popGroup("InputBox");
  144. mHasInputFocus = false;
  145. }
  146. bool GUIFloatField::floatFilter(const WString& str)
  147. {
  148. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  149. }
  150. }