BsGUIFloatField.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. , mStep(0.0f), mIsDragging(false), mHasInputFocus(false)
  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 String&))&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("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. Rect2I viewArea = _getParentWidget()->getTarget()->getPixelArea();
  77. if (event.getPosition().x <= 0)
  78. {
  79. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  80. jumpAmount = viewArea.width - event.getPosition().x - 1;
  81. cursorScreenPos.x += jumpAmount;
  82. Cursor::instance().setScreenPosition(cursorScreenPos);
  83. }
  84. else if (event.getPosition().x >= (INT32)viewArea.width)
  85. {
  86. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  87. jumpAmount = -(INT32)viewArea.width - (event.getPosition().x - (INT32)viewArea.width) + 1;
  88. cursorScreenPos.x += jumpAmount;
  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. valueChanged(newValue);
  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. float GUIFloatField::getValue() const
  109. {
  110. return applyRangeAndStep(mValue);
  111. }
  112. float GUIFloatField::setValue(float value)
  113. {
  114. if (mValue == value)
  115. return value;
  116. mValue = value;
  117. value = applyRangeAndStep(value);
  118. setText(value);
  119. return value;
  120. }
  121. void GUIFloatField::setRange(float min, float max)
  122. {
  123. mMinValue = min;
  124. mMaxValue = max;
  125. }
  126. void GUIFloatField::setStep(float step)
  127. {
  128. mStep = step;
  129. }
  130. void GUIFloatField::setTint(const Color& color)
  131. {
  132. if (mLabel != nullptr)
  133. mLabel->setTint(color);
  134. mInputBox->setTint(color);
  135. }
  136. void GUIFloatField::_setValue(float value, bool triggerEvent)
  137. {
  138. mValue = value;
  139. setText(value);
  140. if(triggerEvent)
  141. onValueChanged(mValue);
  142. }
  143. const String& GUIFloatField::getGUITypeName()
  144. {
  145. static String typeName = "GUIFloatField";
  146. return typeName;
  147. }
  148. const String& GUIFloatField::getInputStyleType()
  149. {
  150. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  151. return LABEL_STYLE_TYPE;
  152. }
  153. void GUIFloatField::styleUpdated()
  154. {
  155. if (mLabel != nullptr)
  156. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  157. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  158. }
  159. void GUIFloatField::valueChanging(const String& newValue)
  160. {
  161. valueChanged(parseFloat(newValue));
  162. }
  163. void GUIFloatField::valueChanged(float newValue)
  164. {
  165. CmdInputFieldValueChange<GUIFloatField, float>::execute(this, newValue);
  166. }
  167. void GUIFloatField::focusChanged(bool focus)
  168. {
  169. if (focus)
  170. {
  171. UndoRedo::instance().pushGroup("InputBox");
  172. mHasInputFocus = true;
  173. onFocusChanged(true);
  174. }
  175. else
  176. {
  177. UndoRedo::instance().popGroup("InputBox");
  178. setText(applyRangeAndStep(mValue));
  179. mHasInputFocus = false;
  180. onFocusChanged(false);
  181. }
  182. }
  183. void GUIFloatField::inputConfirmed()
  184. {
  185. setText(applyRangeAndStep(mValue));
  186. onConfirm();
  187. }
  188. void GUIFloatField::setText(float value)
  189. {
  190. // Only update with new value if it actually changed, otherwise
  191. // problems can occur when user types in "0." and the field
  192. // updates back to "0" effectively making "." unusable
  193. float curValue = parseFloat(mInputBox->getText());
  194. if (value != curValue)
  195. mInputBox->setText(toString(value));
  196. }
  197. float GUIFloatField::applyRangeAndStep(float value) const
  198. {
  199. if (mStep != 0.0f)
  200. value = value - fmod(value, mStep);
  201. return Math::clamp(value, mMinValue, mMaxValue);
  202. }
  203. bool GUIFloatField::floatFilter(const String& str)
  204. {
  205. return std::regex_match(str, std::regex("-?(\\d*(\\.\\d*)?)?"));
  206. }
  207. }