BsGUIIntField.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 String&))&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("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. Rect2I viewArea = _getParentWidget()->getTarget()->getPixelArea();
  78. if (event.getPosition().x <= 0)
  79. {
  80. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  81. jumpAmount = viewArea.width - event.getPosition().x - 1;
  82. cursorScreenPos.x += jumpAmount;
  83. Cursor::instance().setScreenPosition(cursorScreenPos);
  84. }
  85. else if (event.getPosition().x >= (INT32)viewArea.width)
  86. {
  87. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  88. jumpAmount = -(INT32)viewArea.width - (event.getPosition().x - (INT32)viewArea.width) + 1;
  89. cursorScreenPos.x += jumpAmount;
  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. valueChanged(newValue);
  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. INT32 GUIIntField::getValue() const
  132. {
  133. return applyRangeAndStep(mValue);
  134. }
  135. INT32 GUIIntField::setValue(INT32 value)
  136. {
  137. if (mValue == value)
  138. return value;
  139. mValue = value;
  140. value = applyRangeAndStep(value);
  141. setText(value);
  142. return value;
  143. }
  144. void GUIIntField::setRange(INT32 min, INT32 max)
  145. {
  146. mMinValue = min;
  147. mMaxValue = max;
  148. }
  149. void GUIIntField::setStep(INT32 step)
  150. {
  151. mStep = step;
  152. }
  153. void GUIIntField::setTint(const Color& color)
  154. {
  155. if (mLabel != nullptr)
  156. mLabel->setTint(color);
  157. mInputBox->setTint(color);
  158. }
  159. void GUIIntField::_setValue(INT32 value, bool triggerEvent)
  160. {
  161. mValue = value;
  162. setText(value);
  163. if (triggerEvent)
  164. onValueChanged(mValue);
  165. }
  166. const String& GUIIntField::getGUITypeName()
  167. {
  168. static String typeName = "GUIIntField";
  169. return typeName;
  170. }
  171. const String& GUIIntField::getInputStyleType()
  172. {
  173. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  174. return LABEL_STYLE_TYPE;
  175. }
  176. void GUIIntField::valueChanged(const String& newValue)
  177. {
  178. valueChanged(parseINT32(newValue));
  179. }
  180. void GUIIntField::valueChanged(INT32 newValue)
  181. {
  182. CmdInputFieldValueChange<GUIIntField, INT32>::execute(this, newValue);
  183. }
  184. void GUIIntField::focusChanged(bool focus)
  185. {
  186. if (focus)
  187. {
  188. UndoRedo::instance().pushGroup("InputBox");
  189. mHasInputFocus = true;
  190. onFocusChanged(true);
  191. }
  192. else
  193. {
  194. UndoRedo::instance().popGroup("InputBox");
  195. setText(applyRangeAndStep(mValue));
  196. mHasInputFocus = false;
  197. onFocusChanged(false);
  198. }
  199. }
  200. void GUIIntField::inputConfirmed()
  201. {
  202. onConfirm();
  203. }
  204. void GUIIntField::setText(INT32 value)
  205. {
  206. // Only update with new value if it actually changed, otherwise
  207. // problems can occur when user types in "0." and the field
  208. // updates back to "0" effectively making "." unusable
  209. float curValue = parseFloat(mInputBox->getText());
  210. if (value != curValue)
  211. mInputBox->setText(toString(value));
  212. }
  213. INT32 GUIIntField::applyRangeAndStep(INT32 value) const
  214. {
  215. if (mStep != 0)
  216. value = value - value % mStep;
  217. return Math::clamp(value, mMinValue, mMaxValue);
  218. }
  219. bool GUIIntField::intFilter(const String& str)
  220. {
  221. return std::regex_match(str, std::regex("-?(\\d+)?"));
  222. }
  223. }