BsGUIIntField.cpp 5.4 KB

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