BsGUIIntField.cpp 6.4 KB

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