| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #include "BsGUIIntField.h"
- #include "BsGUIArea.h"
- #include "BsGUILayout.h"
- #include "BsGUILabel.h"
- #include "BsGUIInputBox.h"
- #include "BsGUISpace.h"
- #include "BsBuiltinResources.h"
- #include "BsGUIWidget.h"
- #include "BsGUIMouseEvent.h"
- #include "BsGUIWidget.h"
- #include "BsCursor.h"
- #include "BsUndoRedo.h"
- #include "BsViewport.h"
- #include "BsCmdInputFieldValueChange.h"
- #include <regex>
- using namespace std::placeholders;
- namespace BansheeEngine
- {
- const INT32 GUIIntField::DRAG_SPEED = 5;
- GUIIntField::GUIIntField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
- const String& style, const GUILayoutOptions& layoutOptions, bool withLabel)
- :TGUIField(dummy, labelContent, labelWidth, style, layoutOptions, withLabel), mInputBox(nullptr), mIsDragging(false),
- mLastDragPos(0), mIsDragCursorSet(false), mHasInputFocus(false)
- {
- mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getInputStyleType()));
- mInputBox->setFilter(&GUIIntField::intFilter);
- mInputBox->onValueChanged.connect(std::bind(&GUIIntField::valueChanged, this, _1));
- mInputBox->onFocusGained.connect(std::bind(&GUIIntField::focusGained, this));
- mInputBox->onFocusLost.connect(std::bind(&GUIIntField::focusLost, this));
- mLayout->addElement(mInputBox);
- setValue(0);
- }
- GUIIntField::~GUIIntField()
- {
- }
- bool GUIIntField::_hasCustomCursor(const Vector2I position, CursorType& type) const
- {
- RectI draggableArea;
- if(mLabel != nullptr)
- draggableArea = mLabel->_getCachedBounds();
- if(draggableArea.contains(position))
- {
- type = CursorType::ArrowLeftRight;
- return true;
- }
- return false;
- }
- bool GUIIntField::mouseEvent(const GUIMouseEvent& event)
- {
- GUIElementContainer::mouseEvent(event);
- RectI draggableArea;
- if(mLabel != nullptr)
- draggableArea = mLabel->_getCachedBounds();
- if(event.getType() == GUIMouseEventType::MouseDragStart)
- {
- if(draggableArea.contains(event.getPosition()))
- {
- mLastDragPos = event.getPosition().x;
- mIsDragging = true;
- }
- return true;
- }
- else if(event.getType() == GUIMouseEventType::MouseDrag)
- {
- if(mIsDragging)
- {
- INT32 xDiff = event.getPosition().x - mLastDragPos;
- INT32 jumpAmount = 0;
- if(event.getPosition().x < 0)
- {
- Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
- cursorScreenPos.x += _getParentWidget()->getTarget()->getWidth();
- jumpAmount = _getParentWidget()->getTarget()->getWidth();
- Cursor::instance().setScreenPosition(cursorScreenPos);
- }
- else if(event.getPosition().x >= _getParentWidget()->getTarget()->getWidth())
- {
- Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
- cursorScreenPos.x -= _getParentWidget()->getTarget()->getWidth();
- jumpAmount = -_getParentWidget()->getTarget()->getWidth();
- Cursor::instance().setScreenPosition(cursorScreenPos);
- }
- INT32 oldValue = getValue();
- INT32 newValue = oldValue;
- if(xDiff >= DRAG_SPEED)
- {
- while(xDiff >= DRAG_SPEED)
- {
- newValue++;
- xDiff -= DRAG_SPEED;
- }
- }
- else if(xDiff <= -DRAG_SPEED)
- {
- while(xDiff <= -DRAG_SPEED)
- {
- newValue--;
- xDiff += DRAG_SPEED;
- }
- }
- mLastDragPos += (newValue - oldValue) * DRAG_SPEED + jumpAmount;
- if(oldValue != newValue)
- setValue(newValue);
- }
- return true;
- }
- else if(event.getType() == GUIMouseEventType::MouseDragEnd)
- {
- mIsDragging = false;
- return true;
- }
- return false;
- }
- void GUIIntField::styleUpdated()
- {
- if (mLabel != nullptr)
- mLabel->setStyle(getSubStyleName(getLabelStyleType()));
- mInputBox->setStyle(getSubStyleName(getInputStyleType()));
- }
- void GUIIntField::setValue(INT32 value)
- {
- mValue = value;
- mInputBox->setText(toWString(value));
- }
- void GUIIntField::updateClippedBounds()
- {
- Vector2I offset = _getOffset();
- mClippedBounds = RectI(offset.x, offset.y, _getWidth(), _getHeight());
- }
- const String& GUIIntField::getGUITypeName()
- {
- static String typeName = "GUIIntField";
- return typeName;
- }
- const String& GUIIntField::getInputStyleType()
- {
- static String LABEL_STYLE_TYPE = "EditorFieldInput";
- return LABEL_STYLE_TYPE;
- }
- void GUIIntField::valueChanged(const WString& newValue)
- {
- INT32 newIntValue = parseInt(newValue);
- CmdInputFieldValueChange<GUIIntField, INT32>::execute(this, newIntValue);
- if(!onValueChanged.empty())
- onValueChanged(newIntValue);
- }
- void GUIIntField::focusGained()
- {
- UndoRedo::instance().pushGroup("InputBox");
- mHasInputFocus = true;
- }
- void GUIIntField::focusLost()
- {
- UndoRedo::instance().popGroup("InputBox");
- mHasInputFocus = false;
- }
- bool GUIIntField::intFilter(const WString& str)
- {
- return std::regex_match(str, std::wregex(L"-?(\\d+)?"));
- }
- }
|