BsGUIFloatField.cpp 6.2 KB

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