BsGUIFloatField.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "BsGUIFloatField.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 "BsCursor.h"
  11. #include "BsGUIWidget.h"
  12. #include "CmViewport.h"
  13. #include <regex>
  14. namespace BansheeEngine
  15. {
  16. const float GUIFloatField::DRAG_SPEED = 0.05f;
  17. GUIFloatField::GUIFloatField(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)
  21. {
  22. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::flexibleWidth()), inputBoxStyle);
  23. mInputBox->setFilter(&GUIFloatField::floatFilter);
  24. mLayout->addElement(mInputBox);
  25. setValue(0);
  26. }
  27. GUIFloatField::~GUIFloatField()
  28. {
  29. }
  30. bool GUIFloatField::_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 GUIFloatField::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. float oldValue = getValue();
  78. float newValue = oldValue + xDiff * DRAG_SPEED;
  79. mLastDragPos = event.getPosition().x + jumpAmount;
  80. if(oldValue != newValue)
  81. setValue(newValue);
  82. }
  83. return true;
  84. }
  85. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  86. {
  87. mIsDragging = false;
  88. return true;
  89. }
  90. return false;
  91. }
  92. float GUIFloatField::getValue() const
  93. {
  94. return parseFloat(mInputBox->getText());
  95. }
  96. void GUIFloatField::setValue(float value)
  97. {
  98. mInputBox->setText(toWString(value));
  99. }
  100. void GUIFloatField::updateClippedBounds()
  101. {
  102. Vector2I offset = _getOffset();
  103. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  104. }
  105. const String& GUIFloatField::getGUITypeName()
  106. {
  107. static String typeName = "GUIFloatField";
  108. return typeName;
  109. }
  110. bool GUIFloatField::floatFilter(const WString& str)
  111. {
  112. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  113. }
  114. }