BsGUIFloatField.cpp 3.7 KB

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