BsGUIIntField.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. using namespace CamelotFramework;
  15. using namespace BansheeEngine;
  16. namespace BansheeEditor
  17. {
  18. const INT32 GUIIntField::DRAG_SPEED = 5;
  19. GUIIntField::GUIIntField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, 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), mIsDragCursorSet(false)
  23. {
  24. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::flexibleWidth()), inputBoxStyle);
  25. mInputBox->setFilter(&GUIIntField::intFilter);
  26. mLayout->addElement(mInputBox);
  27. setValue(0);
  28. }
  29. GUIIntField::~GUIIntField()
  30. {
  31. }
  32. bool GUIIntField::_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 GUIIntField::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. INT32 oldValue = getValue();
  80. INT32 newValue = oldValue;
  81. if(xDiff >= DRAG_SPEED)
  82. {
  83. while(xDiff >= DRAG_SPEED)
  84. {
  85. newValue++;
  86. xDiff -= DRAG_SPEED;
  87. }
  88. }
  89. else if(xDiff <= -DRAG_SPEED)
  90. {
  91. while(xDiff <= -DRAG_SPEED)
  92. {
  93. newValue--;
  94. xDiff += DRAG_SPEED;
  95. }
  96. }
  97. mLastDragPos += (newValue - oldValue) * DRAG_SPEED + jumpAmount;
  98. if(oldValue != newValue)
  99. setValue(newValue);
  100. }
  101. return true;
  102. }
  103. else if(event.getType() == GUIMouseEventType::MouseDragEnd)
  104. {
  105. mIsDragging = false;
  106. return true;
  107. }
  108. return false;
  109. }
  110. CM::INT32 GUIIntField::getValue() const
  111. {
  112. return parseInt(mInputBox->getText());
  113. }
  114. void GUIIntField::setValue(CM::INT32 value)
  115. {
  116. mInputBox->setText(toWString(value));
  117. }
  118. void GUIIntField::updateClippedBounds()
  119. {
  120. Vector2I offset = _getOffset();
  121. mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
  122. }
  123. const String& GUIIntField::getGUITypeName()
  124. {
  125. static String typeName = "GUIIntField";
  126. return typeName;
  127. }
  128. bool GUIIntField::intFilter(const CM::WString& str)
  129. {
  130. return std::regex_match(str, std::wregex(L"-?(\\d+)?"));
  131. }
  132. }