BsGUIIntField.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. bool GUIIntField::_hasCustomCursor(const Vector2I position, CursorType& type) const
  37. {
  38. RectI draggableArea;
  39. if(mLabel != nullptr)
  40. draggableArea = mLabel->_getCachedBounds();
  41. if(draggableArea.contains(position))
  42. {
  43. type = CursorType::ArrowLeftRight;
  44. return true;
  45. }
  46. return false;
  47. }
  48. bool GUIIntField::mouseEvent(const GUIMouseEvent& event)
  49. {
  50. GUIElementContainer::mouseEvent(event);
  51. RectI draggableArea;
  52. if(mLabel != nullptr)
  53. draggableArea = mLabel->_getCachedBounds();
  54. if(event.getType() == GUIMouseEventType::MouseDragStart)
  55. {
  56. if(draggableArea.contains(event.getPosition()))
  57. {
  58. mLastDragPos = event.getPosition().x;
  59. mIsDragging = true;
  60. }
  61. return true;
  62. }
  63. else if(event.getType() == GUIMouseEventType::MouseDrag)
  64. {
  65. if(mIsDragging)
  66. {
  67. INT32 xDiff = event.getPosition().x - mLastDragPos;
  68. INT32 jumpAmount = 0;
  69. if(event.getPosition().x < 0)
  70. {
  71. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  72. cursorScreenPos.x += _getParentWidget()->getTarget()->getWidth();
  73. jumpAmount = _getParentWidget()->getTarget()->getWidth();
  74. Cursor::instance().setScreenPosition(cursorScreenPos);
  75. }
  76. else if(event.getPosition().x >= _getParentWidget()->getTarget()->getWidth())
  77. {
  78. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  79. cursorScreenPos.x -= _getParentWidget()->getTarget()->getWidth();
  80. jumpAmount = -_getParentWidget()->getTarget()->getWidth();
  81. Cursor::instance().setScreenPosition(cursorScreenPos);
  82. }
  83. INT32 oldValue = getValue();
  84. INT32 newValue = oldValue;
  85. if(xDiff >= DRAG_SPEED)
  86. {
  87. while(xDiff >= DRAG_SPEED)
  88. {
  89. newValue++;
  90. xDiff -= DRAG_SPEED;
  91. }
  92. }
  93. else if(xDiff <= -DRAG_SPEED)
  94. {
  95. while(xDiff <= -DRAG_SPEED)
  96. {
  97. newValue--;
  98. xDiff += DRAG_SPEED;
  99. }
  100. }
  101. mLastDragPos += (newValue - oldValue) * DRAG_SPEED + jumpAmount;
  102. if(oldValue != newValue)
  103. setValue(newValue);
  104. }
  105. return true;
  106. }
  107. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  108. {
  109. mIsDragging = false;
  110. return true;
  111. }
  112. return false;
  113. }
  114. void GUIIntField::setValue(INT32 value)
  115. {
  116. mValue = value;
  117. mInputBox->setText(toWString(value));
  118. }
  119. void GUIIntField::updateClippedBounds()
  120. {
  121. Vector2I offset = _getOffset();
  122. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  123. }
  124. const String& GUIIntField::getGUITypeName()
  125. {
  126. static String typeName = "GUIIntField";
  127. return typeName;
  128. }
  129. void GUIIntField::valueChanged(const WString& newValue)
  130. {
  131. INT32 newIntValue = parseInt(newValue);
  132. CmdInputFieldValueChange<GUIIntField, INT32>::execute(this, newIntValue);
  133. if(!onValueChanged.empty())
  134. onValueChanged(newIntValue);
  135. }
  136. void GUIIntField::focusGained()
  137. {
  138. UndoRedo::instance().pushGroup("InputBox");
  139. }
  140. void GUIIntField::focusLost()
  141. {
  142. UndoRedo::instance().popGroup("InputBox");
  143. }
  144. bool GUIIntField::intFilter(const WString& str)
  145. {
  146. return std::regex_match(str, std::wregex(L"-?(\\d+)?"));
  147. }
  148. }