BsGUIIntField.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "RenderAPI/BsViewport.h"
  11. #include <regex>
  12. using namespace std::placeholders;
  13. namespace bs
  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), mValue(0)
  19. , mLastDragPos(0), mMinValue(std::numeric_limits<INT32>::lowest()), mMaxValue(std::numeric_limits<INT32>::max())
  20. , mStep(1), mIsDragging(false), mIsDragCursorSet(false), mHasInputFocus(false)
  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 String&))&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("0");
  30. mFocusElement = mInputBox;
  31. }
  32. bool GUIIntField::_hasCustomCursor(const Vector2I position, CursorType& type) const
  33. {
  34. if (!_isDisabled())
  35. {
  36. Rect2I draggableArea;
  37. if (mLabel != nullptr)
  38. draggableArea = mLabel->_getLayoutData().area;
  39. if (draggableArea.contains(position))
  40. {
  41. type = CursorType::ArrowLeftRight;
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. bool GUIIntField::_mouseEvent(const GUIMouseEvent& event)
  48. {
  49. GUIElementContainer::_mouseEvent(event);
  50. Rect2I draggableArea;
  51. if(mLabel != nullptr)
  52. draggableArea = mLabel->_getLayoutData().area;
  53. if(event.getType() == GUIMouseEventType::MouseDragStart)
  54. {
  55. if (!_isDisabled())
  56. {
  57. if (draggableArea.contains(event.getDragStartPosition()))
  58. {
  59. mLastDragPos = event.getPosition().x;
  60. mIsDragging = true;
  61. }
  62. }
  63. return true;
  64. }
  65. else if(event.getType() == GUIMouseEventType::MouseDrag)
  66. {
  67. if (!_isDisabled())
  68. {
  69. if (mIsDragging)
  70. {
  71. INT32 xDiff = event.getPosition().x - mLastDragPos;
  72. INT32 jumpAmount = 0;
  73. Rect2I viewArea = _getParentWidget()->getTarget()->getPixelArea();
  74. if (event.getPosition().x <= 0)
  75. {
  76. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  77. jumpAmount = viewArea.width - event.getPosition().x - 1;
  78. cursorScreenPos.x += jumpAmount;
  79. Cursor::instance().setScreenPosition(cursorScreenPos);
  80. }
  81. else if (event.getPosition().x >= (INT32)viewArea.width)
  82. {
  83. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  84. jumpAmount = -(INT32)viewArea.width - (event.getPosition().x - (INT32)viewArea.width) + 1;
  85. cursorScreenPos.x += jumpAmount;
  86. Cursor::instance().setScreenPosition(cursorScreenPos);
  87. }
  88. INT32 oldValue = getValue();
  89. INT32 newValue = oldValue;
  90. if (xDiff >= DRAG_SPEED)
  91. {
  92. while (xDiff >= DRAG_SPEED)
  93. {
  94. newValue++;
  95. xDiff -= DRAG_SPEED;
  96. }
  97. }
  98. else if (xDiff <= -DRAG_SPEED)
  99. {
  100. while (xDiff <= -DRAG_SPEED)
  101. {
  102. newValue--;
  103. xDiff += DRAG_SPEED;
  104. }
  105. }
  106. mLastDragPos += (newValue - oldValue) * DRAG_SPEED + jumpAmount;
  107. if (oldValue != newValue)
  108. _setValue(newValue, true);
  109. }
  110. }
  111. return true;
  112. }
  113. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  114. {
  115. if (!_isDisabled())
  116. mIsDragging = false;
  117. return true;
  118. }
  119. return false;
  120. }
  121. void GUIIntField::styleUpdated()
  122. {
  123. if (mLabel != nullptr)
  124. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  125. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  126. }
  127. INT32 GUIIntField::getValue() const
  128. {
  129. return applyRangeAndStep(mValue);
  130. }
  131. INT32 GUIIntField::setValue(INT32 value)
  132. {
  133. if (mValue == value)
  134. return value;
  135. mValue = value;
  136. value = applyRangeAndStep(value);
  137. setText(value);
  138. return value;
  139. }
  140. void GUIIntField::setRange(INT32 min, INT32 max)
  141. {
  142. mMinValue = min;
  143. mMaxValue = max;
  144. }
  145. void GUIIntField::setStep(INT32 step)
  146. {
  147. mStep = step;
  148. }
  149. void GUIIntField::setTint(const Color& color)
  150. {
  151. if (mLabel != nullptr)
  152. mLabel->setTint(color);
  153. mInputBox->setTint(color);
  154. }
  155. void GUIIntField::_setValue(INT32 value, bool triggerEvent)
  156. {
  157. mValue = value;
  158. setText(value);
  159. if (triggerEvent)
  160. onValueChanged(mValue);
  161. }
  162. const String& GUIIntField::getGUITypeName()
  163. {
  164. static String typeName = "GUIIntField";
  165. return typeName;
  166. }
  167. const String& GUIIntField::getInputStyleType()
  168. {
  169. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  170. return LABEL_STYLE_TYPE;
  171. }
  172. void GUIIntField::valueChanged(const String& newValue)
  173. {
  174. _setValue(parseINT32(newValue), true);
  175. }
  176. void GUIIntField::focusChanged(bool focus)
  177. {
  178. if (focus)
  179. {
  180. mHasInputFocus = true;
  181. onFocusChanged(true);
  182. }
  183. else
  184. {
  185. setText(applyRangeAndStep(mValue));
  186. mHasInputFocus = false;
  187. onFocusChanged(false);
  188. }
  189. }
  190. void GUIIntField::inputConfirmed()
  191. {
  192. onConfirm();
  193. }
  194. void GUIIntField::setText(INT32 value)
  195. {
  196. // Only update with new value if it actually changed, otherwise
  197. // problems can occur when user types in "0." and the field
  198. // updates back to "0" effectively making "." unusable
  199. float curValue = parseFloat(mInputBox->getText());
  200. if (value != curValue)
  201. mInputBox->setText(toString(value));
  202. }
  203. INT32 GUIIntField::applyRangeAndStep(INT32 value) const
  204. {
  205. if (mStep != 0)
  206. value = value - value % mStep;
  207. return Math::clamp(value, mMinValue, mMaxValue);
  208. }
  209. bool GUIIntField::intFilter(const String& str)
  210. {
  211. return std::regex_match(str, std::regex("-?(\\d+)?"));
  212. }
  213. }