BsGUIIntField.cpp 5.8 KB

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