BsGUIIntField.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "CmViewport.h"
  13. #include <regex>
  14. namespace BansheeEngine
  15. {
  16. const INT32 GUIIntField::DRAG_SPEED = 5;
  17. GUIIntField::GUIIntField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
  18. const String& labelStyle, const String& inputBoxStyle, const GUILayoutOptions& layoutOptions, bool withLabel)
  19. :TGUIField(dummy, labelContent, labelWidth, labelStyle, layoutOptions, withLabel), mInputBox(nullptr), mIsDragging(false),
  20. mLastDragPos(0), mIsDragCursorSet(false)
  21. {
  22. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::flexibleWidth()), inputBoxStyle);
  23. mInputBox->setFilter(&GUIIntField::intFilter);
  24. mLayout->addElement(mInputBox);
  25. setValue(0);
  26. }
  27. GUIIntField::~GUIIntField()
  28. {
  29. }
  30. bool GUIIntField::_hasCustomCursor(const Vector2I position, CursorType& type) const
  31. {
  32. RectI draggableArea;
  33. if(mLabel != nullptr)
  34. draggableArea = mLabel->getBounds();
  35. if(draggableArea.contains(position))
  36. {
  37. type = CursorType::ArrowLeftRight;
  38. return true;
  39. }
  40. return false;
  41. }
  42. bool GUIIntField::mouseEvent(const GUIMouseEvent& event)
  43. {
  44. GUIElementContainer::mouseEvent(event);
  45. RectI draggableArea;
  46. if(mLabel != nullptr)
  47. draggableArea = mLabel->getBounds();
  48. if(event.getType() == GUIMouseEventType::MouseDragStart)
  49. {
  50. if(draggableArea.contains(event.getPosition()))
  51. {
  52. mLastDragPos = event.getPosition().x;
  53. mIsDragging = true;
  54. }
  55. return true;
  56. }
  57. else if(event.getType() == GUIMouseEventType::MouseDrag)
  58. {
  59. if(mIsDragging)
  60. {
  61. INT32 xDiff = event.getPosition().x - mLastDragPos;
  62. INT32 jumpAmount = 0;
  63. if(event.getPosition().x < 0)
  64. {
  65. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  66. cursorScreenPos.x += _getParentWidget()->getTarget()->getWidth();
  67. jumpAmount = _getParentWidget()->getTarget()->getWidth();
  68. Cursor::instance().setScreenPosition(cursorScreenPos);
  69. }
  70. else if(event.getPosition().x >= _getParentWidget()->getTarget()->getWidth())
  71. {
  72. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  73. cursorScreenPos.x -= _getParentWidget()->getTarget()->getWidth();
  74. jumpAmount = -_getParentWidget()->getTarget()->getWidth();
  75. Cursor::instance().setScreenPosition(cursorScreenPos);
  76. }
  77. INT32 oldValue = getValue();
  78. INT32 newValue = oldValue;
  79. if(xDiff >= DRAG_SPEED)
  80. {
  81. while(xDiff >= DRAG_SPEED)
  82. {
  83. newValue++;
  84. xDiff -= DRAG_SPEED;
  85. }
  86. }
  87. else if(xDiff <= -DRAG_SPEED)
  88. {
  89. while(xDiff <= -DRAG_SPEED)
  90. {
  91. newValue--;
  92. xDiff += DRAG_SPEED;
  93. }
  94. }
  95. mLastDragPos += (newValue - oldValue) * DRAG_SPEED + jumpAmount;
  96. if(oldValue != newValue)
  97. setValue(newValue);
  98. }
  99. return true;
  100. }
  101. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  102. {
  103. mIsDragging = false;
  104. return true;
  105. }
  106. return false;
  107. }
  108. INT32 GUIIntField::getValue() const
  109. {
  110. return parseInt(mInputBox->getText());
  111. }
  112. void GUIIntField::setValue(INT32 value)
  113. {
  114. mInputBox->setText(toWString(value));
  115. }
  116. void GUIIntField::updateClippedBounds()
  117. {
  118. Vector2I offset = _getOffset();
  119. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  120. }
  121. const String& GUIIntField::getGUITypeName()
  122. {
  123. static String typeName = "GUIIntField";
  124. return typeName;
  125. }
  126. bool GUIIntField::intFilter(const WString& str)
  127. {
  128. return std::regex_match(str, std::wregex(L"-?(\\d+)?"));
  129. }
  130. }