BsGUIIntField.cpp 4.1 KB

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