BsGUIFloatField.cpp 5.6 KB

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