BsGUIFloatField.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, GUIWidget& parent, const GUIContent& labelContent, CM::UINT32 labelWidth,
  20. GUIElementStyle* labelStyle, GUIElementStyle* inputBoxStyle, const GUILayoutOptions& layoutOptions, bool withLabel)
  21. :TGUIField(dummy, parent, labelContent, labelWidth, labelStyle, layoutOptions, withLabel), mInputBox(nullptr), mIsDragging(false),
  22. mLastDragPos(0), mIsDragCursorSet(false)
  23. {
  24. const GUIElementStyle* curInputBoxStyle = inputBoxStyle;
  25. if(curInputBoxStyle == nullptr)
  26. curInputBoxStyle = parent.getSkin().getStyle("InputBox");
  27. mInputBox = GUIInputBox::create(parent, false, GUIOptions(GUIOption::flexibleWidth()), inputBoxStyle);
  28. mInputBox->setFilter(&GUIFloatField::floatFilter);
  29. mLayout->addElement(mInputBox);
  30. setValue(0);
  31. }
  32. GUIFloatField::~GUIFloatField()
  33. {
  34. }
  35. bool GUIFloatField::mouseEvent(const GUIMouseEvent& event)
  36. {
  37. GUIElementContainer::mouseEvent(event);
  38. RectI draggableArea;
  39. if(mLabel != nullptr)
  40. draggableArea = mLabel->getBounds();
  41. if(event.getType() == GUIMouseEventType::MouseDragStart)
  42. {
  43. if(draggableArea.contains(event.getPosition()))
  44. {
  45. mLastDragPos = event.getPosition().x;
  46. mIsDragging = true;
  47. Cursor::instance().setCursor(CursorType::ArrowLeftRight);
  48. mIsDragCursorSet = true;
  49. }
  50. return true;
  51. }
  52. else if(event.getType() == GUIMouseEventType::MouseDrag)
  53. {
  54. if(mIsDragging)
  55. {
  56. INT32 xDiff = event.getPosition().x - mLastDragPos;
  57. INT32 jumpAmount = 0;
  58. if(event.getPosition().x < 0)
  59. {
  60. Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
  61. cursorScreenPos.x += _getParentWidget().getTarget()->getWidth();
  62. jumpAmount = _getParentWidget().getTarget()->getWidth();
  63. Cursor::instance().setScreenPosition(cursorScreenPos);
  64. }
  65. else if(event.getPosition().x >= _getParentWidget().getTarget()->getWidth())
  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. float oldValue = getValue();
  73. float newValue = oldValue + xDiff * DRAG_SPEED;
  74. mLastDragPos = event.getPosition().x + jumpAmount;
  75. if(oldValue != newValue)
  76. setValue(newValue);
  77. }
  78. return true;
  79. }
  80. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  81. {
  82. mIsDragging = false;
  83. if(mIsDragCursorSet)
  84. {
  85. Cursor::instance().setCursor(CursorType::Arrow);
  86. mIsDragCursorSet = false;
  87. }
  88. return true;
  89. }
  90. else if(event.getType() == GUIMouseEventType::MouseOut)
  91. {
  92. if(!mIsDragging)
  93. {
  94. if(mIsDragCursorSet)
  95. {
  96. Cursor::instance().setCursor(CursorType::Arrow);
  97. mIsDragCursorSet = false;
  98. }
  99. }
  100. }
  101. else if(event.getType() == GUIMouseEventType::MouseMove)
  102. {
  103. if(draggableArea.contains(event.getPosition()))
  104. {
  105. Cursor::instance().setCursor(CursorType::ArrowLeftRight);
  106. mIsDragCursorSet = true;
  107. }
  108. else
  109. {
  110. if(!mIsDragging)
  111. {
  112. if(mIsDragCursorSet)
  113. {
  114. Cursor::instance().setCursor(CursorType::Arrow);
  115. mIsDragCursorSet = false;
  116. }
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. float GUIFloatField::getValue() const
  123. {
  124. return parseFloat(mInputBox->getText());
  125. }
  126. void GUIFloatField::setValue(float value)
  127. {
  128. mInputBox->setText(toWString(value));
  129. }
  130. void GUIFloatField::updateClippedBounds()
  131. {
  132. Vector2I offset = _getOffset();
  133. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  134. }
  135. const String& GUIFloatField::getGUITypeName()
  136. {
  137. static String typeName = "GUIFloatField";
  138. return typeName;
  139. }
  140. bool GUIFloatField::floatFilter(const CM::WString& str)
  141. {
  142. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  143. }
  144. }