BsGUIFloatField.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "BsGUIFloatField.h"
  2. #include "BsGUILayout.h"
  3. #include "BsGUILabel.h"
  4. #include "BsGUIInputBox.h"
  5. #include "BsGUIWidget.h"
  6. #include "BsGUIMouseEvent.h"
  7. #include "BsViewport.h"
  8. #include "BsCursor.h"
  9. #include "BsCmdInputFieldValueChange.h"
  10. #include <regex>
  11. using namespace std::placeholders;
  12. namespace BansheeEngine
  13. {
  14. const float GUIFloatField::DRAG_SPEED = 0.05f;
  15. GUIFloatField::GUIFloatField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
  16. const String& style, const GUIDimensions& dimensions, bool withLabel)
  17. :TGUIField(dummy, labelContent, labelWidth, style, dimensions, withLabel), mInputBox(nullptr), mIsDragging(false),
  18. mLastDragPos(0), mHasInputFocus(false), mValue(0.0f), mMinValue(std::numeric_limits<float>::lowest()),
  19. mMaxValue(std::numeric_limits<float>::max())
  20. {
  21. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getInputStyleType()));
  22. mInputBox->setFilter(&GUIFloatField::floatFilter);
  23. mInputBox->onValueChanged.connect(std::bind((void(GUIFloatField::*)(const WString&))&GUIFloatField::valueChanged, this, _1));
  24. mInputBox->onFocusChanged.connect(std::bind(&GUIFloatField::focusChanged, this, _1));
  25. mInputBox->onConfirm.connect(std::bind(&GUIFloatField::inputConfirmed, this));
  26. mLayout->addElement(mInputBox);
  27. setValue(0);
  28. mInputBox->setText(L"0");
  29. }
  30. GUIFloatField::~GUIFloatField()
  31. {
  32. }
  33. bool GUIFloatField::_hasCustomCursor(const Vector2I position, CursorType& type) const
  34. {
  35. if (!_isDisabled())
  36. {
  37. Rect2I draggableArea;
  38. if (mLabel != nullptr)
  39. draggableArea = mLabel->_getLayoutData().area;
  40. if (draggableArea.contains(position))
  41. {
  42. type = CursorType::ArrowLeftRight;
  43. return true;
  44. }
  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 (!_isDisabled())
  57. {
  58. if (draggableArea.contains(event.getDragStartPosition()))
  59. {
  60. mLastDragPos = event.getPosition().x;
  61. mIsDragging = true;
  62. }
  63. }
  64. return true;
  65. }
  66. else if(event.getType() == GUIMouseEventType::MouseDrag)
  67. {
  68. if (!_isDisabled())
  69. {
  70. if (mIsDragging)
  71. {
  72. INT32 xDiff = event.getPosition().x - mLastDragPos;
  73. INT32 jumpAmount = 0;
  74. if (event.getPosition().x < 0)
  75. {
  76. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  77. cursorScreenPos.x += _getParentWidget()->getTarget()->getWidth();
  78. jumpAmount = _getParentWidget()->getTarget()->getWidth();
  79. Cursor::instance().setScreenPosition(cursorScreenPos);
  80. }
  81. else if (event.getPosition().x >= _getParentWidget()->getTarget()->getWidth())
  82. {
  83. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  84. cursorScreenPos.x -= _getParentWidget()->getTarget()->getWidth();
  85. jumpAmount = -_getParentWidget()->getTarget()->getWidth();
  86. Cursor::instance().setScreenPosition(cursorScreenPos);
  87. }
  88. float oldValue = getValue();
  89. float newValue = oldValue + xDiff * DRAG_SPEED;
  90. mLastDragPos = event.getPosition().x + jumpAmount;
  91. if (oldValue != newValue)
  92. {
  93. setValue(newValue);
  94. valueChanged(newValue);
  95. }
  96. }
  97. }
  98. return true;
  99. }
  100. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  101. {
  102. if (!_isDisabled())
  103. mIsDragging = false;
  104. return true;
  105. }
  106. return false;
  107. }
  108. void GUIFloatField::setValue(float value)
  109. {
  110. mValue = Math::clamp(value, mMinValue, mMaxValue);
  111. // Only update with new value if it actually changed, otherwise
  112. // problems can occur when user types in "0." and the field
  113. // updates back to "0" effectively making "." unusable
  114. float curValue = parseFloat(mInputBox->getText());
  115. if (mValue != curValue)
  116. mInputBox->setText(toWString(mValue));
  117. }
  118. void GUIFloatField::setRange(float min, float max)
  119. {
  120. mMinValue = min;
  121. mMaxValue = max;
  122. }
  123. void GUIFloatField::setTint(const Color& color)
  124. {
  125. if (mLabel != nullptr)
  126. mLabel->setTint(color);
  127. mInputBox->setTint(color);
  128. }
  129. void GUIFloatField::_setValue(float value, bool triggerEvent)
  130. {
  131. setValue(value);
  132. if(triggerEvent)
  133. onValueChanged(value);
  134. }
  135. const String& GUIFloatField::getGUITypeName()
  136. {
  137. static String typeName = "GUIFloatField";
  138. return typeName;
  139. }
  140. const String& GUIFloatField::getInputStyleType()
  141. {
  142. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  143. return LABEL_STYLE_TYPE;
  144. }
  145. void GUIFloatField::styleUpdated()
  146. {
  147. if (mLabel != nullptr)
  148. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  149. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  150. }
  151. void GUIFloatField::valueChanged(const WString& newValue)
  152. {
  153. valueChanged(parseFloat(newValue));
  154. }
  155. void GUIFloatField::valueChanged(float newValue)
  156. {
  157. CmdInputFieldValueChange<GUIFloatField, float>::execute(this, newValue);
  158. }
  159. void GUIFloatField::focusChanged(bool focus)
  160. {
  161. if (focus)
  162. {
  163. UndoRedo::instance().pushGroup("InputBox");
  164. mHasInputFocus = true;
  165. }
  166. else
  167. {
  168. UndoRedo::instance().popGroup("InputBox");
  169. mHasInputFocus = false;
  170. }
  171. }
  172. void GUIFloatField::inputConfirmed()
  173. {
  174. onConfirm();
  175. }
  176. bool GUIFloatField::floatFilter(const WString& str)
  177. {
  178. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  179. }
  180. }