BsGUIFloatField.cpp 5.9 KB

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