BsGUIIntField.cpp 5.8 KB

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