BsGUIIntField.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGUIIntField.h"
  4. #include "BsGUILayout.h"
  5. #include "BsGUILabel.h"
  6. #include "BsGUIInputBox.h"
  7. #include "BsGUIWidget.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsCursor.h"
  10. #include "BsUndoRedo.h"
  11. #include "BsViewport.h"
  12. #include "BsCmdInputFieldValueChange.h"
  13. #include <regex>
  14. using namespace std::placeholders;
  15. namespace BansheeEngine
  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())
  22. , mMaxValue(std::numeric_limits<INT32>::max()), mIsDragging(false), mIsDragCursorSet(false), mHasInputFocus(false), mStep(1)
  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. {
  112. setValue(newValue);
  113. valueChanged(newValue);
  114. }
  115. }
  116. }
  117. return true;
  118. }
  119. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  120. {
  121. if (!_isDisabled())
  122. mIsDragging = false;
  123. return true;
  124. }
  125. return false;
  126. }
  127. void GUIIntField::styleUpdated()
  128. {
  129. if (mLabel != nullptr)
  130. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  131. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  132. }
  133. INT32 GUIIntField::setValue(INT32 value)
  134. {
  135. if (mStep != 0)
  136. mValue = mValue - mValue % mStep;
  137. mValue = Math::clamp(value, mMinValue, mMaxValue);
  138. // Only update with new value if it actually changed, otherwise
  139. // problems can occur when user types in "0." and the field
  140. // updates back to "0" effectively making "." unusable
  141. float curValue = parseFloat(mInputBox->getText());
  142. if (mValue != curValue)
  143. mInputBox->setText(toWString(mValue));
  144. return mValue;
  145. }
  146. void GUIIntField::setRange(INT32 min, INT32 max)
  147. {
  148. mMinValue = min;
  149. mMaxValue = max;
  150. }
  151. void GUIIntField::setStep(INT32 step)
  152. {
  153. mStep = step;
  154. }
  155. void GUIIntField::setTint(const Color& color)
  156. {
  157. if (mLabel != nullptr)
  158. mLabel->setTint(color);
  159. mInputBox->setTint(color);
  160. }
  161. void GUIIntField::_setValue(INT32 value, bool triggerEvent)
  162. {
  163. setValue(value);
  164. if (triggerEvent)
  165. onValueChanged(mValue);
  166. }
  167. const String& GUIIntField::getGUITypeName()
  168. {
  169. static String typeName = "GUIIntField";
  170. return typeName;
  171. }
  172. const String& GUIIntField::getInputStyleType()
  173. {
  174. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  175. return LABEL_STYLE_TYPE;
  176. }
  177. void GUIIntField::valueChanged(const WString& newValue)
  178. {
  179. valueChanged(parseINT32(newValue));
  180. }
  181. void GUIIntField::valueChanged(INT32 newValue)
  182. {
  183. CmdInputFieldValueChange<GUIIntField, INT32>::execute(this, newValue);
  184. }
  185. void GUIIntField::focusChanged(bool focus)
  186. {
  187. if (focus)
  188. {
  189. UndoRedo::instance().pushGroup("InputBox");
  190. mHasInputFocus = true;
  191. }
  192. else
  193. {
  194. UndoRedo::instance().popGroup("InputBox");
  195. mHasInputFocus = false;
  196. }
  197. }
  198. void GUIIntField::inputConfirmed()
  199. {
  200. onConfirm();
  201. }
  202. bool GUIIntField::intFilter(const WString& str)
  203. {
  204. return std::regex_match(str, std::wregex(L"-?(\\d+)?"));
  205. }
  206. }