BsGUIIntField.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "BsGUIIntField.h"
  2. #include "BsGUIArea.h"
  3. #include "BsGUILayout.h"
  4. #include "BsGUILabel.h"
  5. #include "BsGUIInputBox.h"
  6. #include "BsGUISpace.h"
  7. #include "BsBuiltinResources.h"
  8. #include "BsGUIWidget.h"
  9. #include "BsGUIMouseEvent.h"
  10. #include "BsGUIWidget.h"
  11. #include "BsCursor.h"
  12. #include "BsUndoRedo.h"
  13. #include "BsViewport.h"
  14. #include "BsCmdInputFieldValueChange.h"
  15. #include <regex>
  16. using namespace std::placeholders;
  17. namespace BansheeEngine
  18. {
  19. const INT32 GUIIntField::DRAG_SPEED = 5;
  20. GUIIntField::GUIIntField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
  21. const String& labelStyle, const String& inputBoxStyle, const GUILayoutOptions& layoutOptions, bool withLabel)
  22. :TGUIField(dummy, labelContent, labelWidth, labelStyle, layoutOptions, withLabel), mInputBox(nullptr), mIsDragging(false),
  23. mLastDragPos(0), mIsDragCursorSet(false)
  24. {
  25. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::flexibleWidth()), inputBoxStyle);
  26. mInputBox->setFilter(&GUIIntField::intFilter);
  27. mInputBox->onValueChanged.connect(std::bind(&GUIIntField::valueChanged, this, _1));
  28. mInputBox->onFocusGained.connect(std::bind(&GUIIntField::focusGained, this));
  29. mInputBox->onFocusLost.connect(std::bind(&GUIIntField::focusLost, this));
  30. mLayout->addElement(mInputBox);
  31. setValue(0);
  32. }
  33. GUIIntField::~GUIIntField()
  34. {
  35. }
  36. Vector2I GUIIntField::_getOptimalSize() const
  37. {
  38. UINT32 width = (UINT32)mInputBox->_getOptimalSize().x;
  39. UINT32 height = (UINT32)mInputBox->_getOptimalSize().y;
  40. if (mLabel != nullptr)
  41. {
  42. width += mLabel->_getOptimalSize().x;
  43. height = std::max(height, (UINT32)mLabel->_getOptimalSize().y);
  44. }
  45. return Vector2I(width, height);
  46. }
  47. bool GUIIntField::_hasCustomCursor(const Vector2I position, CursorType& type) const
  48. {
  49. RectI draggableArea;
  50. if(mLabel != nullptr)
  51. draggableArea = mLabel->_getCachedBounds();
  52. if(draggableArea.contains(position))
  53. {
  54. type = CursorType::ArrowLeftRight;
  55. return true;
  56. }
  57. return false;
  58. }
  59. bool GUIIntField::mouseEvent(const GUIMouseEvent& event)
  60. {
  61. GUIElementContainer::mouseEvent(event);
  62. RectI draggableArea;
  63. if(mLabel != nullptr)
  64. draggableArea = mLabel->_getCachedBounds();
  65. if(event.getType() == GUIMouseEventType::MouseDragStart)
  66. {
  67. if(draggableArea.contains(event.getPosition()))
  68. {
  69. mLastDragPos = event.getPosition().x;
  70. mIsDragging = true;
  71. }
  72. return true;
  73. }
  74. else if(event.getType() == GUIMouseEventType::MouseDrag)
  75. {
  76. if(mIsDragging)
  77. {
  78. INT32 xDiff = event.getPosition().x - mLastDragPos;
  79. INT32 jumpAmount = 0;
  80. if(event.getPosition().x < 0)
  81. {
  82. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  83. cursorScreenPos.x += _getParentWidget()->getTarget()->getWidth();
  84. jumpAmount = _getParentWidget()->getTarget()->getWidth();
  85. Cursor::instance().setScreenPosition(cursorScreenPos);
  86. }
  87. else if(event.getPosition().x >= _getParentWidget()->getTarget()->getWidth())
  88. {
  89. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  90. cursorScreenPos.x -= _getParentWidget()->getTarget()->getWidth();
  91. jumpAmount = -_getParentWidget()->getTarget()->getWidth();
  92. Cursor::instance().setScreenPosition(cursorScreenPos);
  93. }
  94. INT32 oldValue = getValue();
  95. INT32 newValue = oldValue;
  96. if(xDiff >= DRAG_SPEED)
  97. {
  98. while(xDiff >= DRAG_SPEED)
  99. {
  100. newValue++;
  101. xDiff -= DRAG_SPEED;
  102. }
  103. }
  104. else if(xDiff <= -DRAG_SPEED)
  105. {
  106. while(xDiff <= -DRAG_SPEED)
  107. {
  108. newValue--;
  109. xDiff += DRAG_SPEED;
  110. }
  111. }
  112. mLastDragPos += (newValue - oldValue) * DRAG_SPEED + jumpAmount;
  113. if(oldValue != newValue)
  114. setValue(newValue);
  115. }
  116. return true;
  117. }
  118. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  119. {
  120. mIsDragging = false;
  121. return true;
  122. }
  123. return false;
  124. }
  125. void GUIIntField::setValue(INT32 value)
  126. {
  127. mValue = value;
  128. mInputBox->setText(toWString(value));
  129. }
  130. void GUIIntField::updateClippedBounds()
  131. {
  132. Vector2I offset = _getOffset();
  133. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  134. }
  135. const String& GUIIntField::getGUITypeName()
  136. {
  137. static String typeName = "GUIIntField";
  138. return typeName;
  139. }
  140. void GUIIntField::valueChanged(const WString& newValue)
  141. {
  142. INT32 newIntValue = parseInt(newValue);
  143. CmdInputFieldValueChange<GUIIntField, INT32>::execute(this, newIntValue);
  144. if(!onValueChanged.empty())
  145. onValueChanged(newIntValue);
  146. }
  147. void GUIIntField::focusGained()
  148. {
  149. UndoRedo::instance().pushGroup("InputBox");
  150. }
  151. void GUIIntField::focusLost()
  152. {
  153. UndoRedo::instance().popGroup("InputBox");
  154. }
  155. bool GUIIntField::intFilter(const WString& str)
  156. {
  157. return std::regex_match(str, std::wregex(L"-?(\\d+)?"));
  158. }
  159. }