BsGUIFloatField.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGUIFloatField.h"
  4. #include "BsGUILayout.h"
  5. #include "BsGUILabel.h"
  6. #include "BsGUIInputBox.h"
  7. #include "BsGUIWidget.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsViewport.h"
  10. #include "BsCursor.h"
  11. #include "BsCmdInputFieldValueChange.h"
  12. #include <regex>
  13. using namespace std::placeholders;
  14. namespace BansheeEngine
  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), mIsDragging(false),
  20. mLastDragPos(0), mHasInputFocus(false), mValue(0.0f), mMinValue(std::numeric_limits<float>::lowest()),
  21. mMaxValue(std::numeric_limits<float>::max())
  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::valueChanged, 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. {
  95. setValue(newValue);
  96. valueChanged(newValue);
  97. }
  98. }
  99. }
  100. return true;
  101. }
  102. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  103. {
  104. if (!_isDisabled())
  105. mIsDragging = false;
  106. return true;
  107. }
  108. return false;
  109. }
  110. void GUIFloatField::setValue(float value)
  111. {
  112. mValue = Math::clamp(value, mMinValue, mMaxValue);
  113. // Only update with new value if it actually changed, otherwise
  114. // problems can occur when user types in "0." and the field
  115. // updates back to "0" effectively making "." unusable
  116. float curValue = parseFloat(mInputBox->getText());
  117. if (mValue != curValue)
  118. mInputBox->setText(toWString(mValue));
  119. }
  120. void GUIFloatField::setRange(float min, float max)
  121. {
  122. mMinValue = min;
  123. mMaxValue = max;
  124. }
  125. void GUIFloatField::setTint(const Color& color)
  126. {
  127. if (mLabel != nullptr)
  128. mLabel->setTint(color);
  129. mInputBox->setTint(color);
  130. }
  131. void GUIFloatField::_setValue(float value, bool triggerEvent)
  132. {
  133. setValue(value);
  134. if(triggerEvent)
  135. onValueChanged(value);
  136. }
  137. const String& GUIFloatField::getGUITypeName()
  138. {
  139. static String typeName = "GUIFloatField";
  140. return typeName;
  141. }
  142. const String& GUIFloatField::getInputStyleType()
  143. {
  144. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  145. return LABEL_STYLE_TYPE;
  146. }
  147. void GUIFloatField::styleUpdated()
  148. {
  149. if (mLabel != nullptr)
  150. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  151. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  152. }
  153. void GUIFloatField::valueChanged(const WString& newValue)
  154. {
  155. valueChanged(parseFloat(newValue));
  156. }
  157. void GUIFloatField::valueChanged(float newValue)
  158. {
  159. CmdInputFieldValueChange<GUIFloatField, float>::execute(this, newValue);
  160. }
  161. void GUIFloatField::focusChanged(bool focus)
  162. {
  163. if (focus)
  164. {
  165. UndoRedo::instance().pushGroup("InputBox");
  166. mHasInputFocus = true;
  167. }
  168. else
  169. {
  170. UndoRedo::instance().popGroup("InputBox");
  171. mHasInputFocus = false;
  172. }
  173. }
  174. void GUIFloatField::inputConfirmed()
  175. {
  176. onConfirm();
  177. }
  178. bool GUIFloatField::floatFilter(const WString& str)
  179. {
  180. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  181. }
  182. }