| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "GUI/BsGUIIntField.h"
- #include "GUI/BsGUILayout.h"
- #include "GUI/BsGUILabel.h"
- #include "GUI/BsGUIInputBox.h"
- #include "GUI/BsGUIWidget.h"
- #include "GUI/BsGUIMouseEvent.h"
- #include "Platform/BsCursor.h"
- #include "UndoRedo/BsUndoRedo.h"
- #include "RenderAPI/BsViewport.h"
- #include "UndoRedo/BsCmdInputFieldValueChange.h"
- #include <regex>
- using namespace std::placeholders;
- namespace bs
- {
- const INT32 GUIIntField::DRAG_SPEED = 5;
- GUIIntField::GUIIntField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
- const String& style, const GUIDimensions& dimensions, bool withLabel)
- : TGUIField(dummy, labelContent, labelWidth, style, dimensions, withLabel), mInputBox(nullptr), mValue(0)
- , mLastDragPos(0), mMinValue(std::numeric_limits<INT32>::lowest()), mMaxValue(std::numeric_limits<INT32>::max())
- , mStep(1), mIsDragging(false), mIsDragCursorSet(false), mHasInputFocus(false)
- {
- mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getInputStyleType()));
- mInputBox->setFilter(&GUIIntField::intFilter);
- mInputBox->onValueChanged.connect(std::bind((void(GUIIntField::*)(const String&))&GUIIntField::valueChanged, this, _1));
- mInputBox->onFocusChanged.connect(std::bind(&GUIIntField::focusChanged, this, _1));
- mInputBox->onConfirm.connect(std::bind(&GUIIntField::inputConfirmed, this));
- mLayout->addElement(mInputBox);
- setValue(0);
- mInputBox->setText("0");
- }
- GUIIntField::~GUIIntField()
- {
- }
- bool GUIIntField::_hasCustomCursor(const Vector2I position, CursorType& type) const
- {
- if (!_isDisabled())
- {
- Rect2I draggableArea;
- if (mLabel != nullptr)
- draggableArea = mLabel->_getLayoutData().area;
- if (draggableArea.contains(position))
- {
- type = CursorType::ArrowLeftRight;
- return true;
- }
- }
- return false;
- }
- bool GUIIntField::_mouseEvent(const GUIMouseEvent& event)
- {
- GUIElementContainer::_mouseEvent(event);
- Rect2I draggableArea;
- if(mLabel != nullptr)
- draggableArea = mLabel->_getLayoutData().area;
- if(event.getType() == GUIMouseEventType::MouseDragStart)
- {
- if (!_isDisabled())
- {
- if (draggableArea.contains(event.getDragStartPosition()))
- {
- mLastDragPos = event.getPosition().x;
- mIsDragging = true;
- }
- }
- return true;
- }
- else if(event.getType() == GUIMouseEventType::MouseDrag)
- {
- if (!_isDisabled())
- {
- if (mIsDragging)
- {
- INT32 xDiff = event.getPosition().x - mLastDragPos;
- INT32 jumpAmount = 0;
- Rect2I viewArea = _getParentWidget()->getTarget()->getPixelArea();
- if (event.getPosition().x <= 0)
- {
- Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
- jumpAmount = viewArea.width - event.getPosition().x - 1;
- cursorScreenPos.x += jumpAmount;
- Cursor::instance().setScreenPosition(cursorScreenPos);
- }
- else if (event.getPosition().x >= (INT32)viewArea.width)
- {
- Vector2I cursorScreenPos = Cursor::instance().getScreenPosition();
- jumpAmount = -(INT32)viewArea.width - (event.getPosition().x - (INT32)viewArea.width) + 1;
- cursorScreenPos.x += jumpAmount;
- 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)
- valueChanged(newValue);
- }
- }
- return true;
- }
- else if(event.getType() == GUIMouseEventType::MouseDragEnd)
- {
- if (!_isDisabled())
- mIsDragging = false;
- return true;
- }
- return false;
- }
- void GUIIntField::styleUpdated()
- {
- if (mLabel != nullptr)
- mLabel->setStyle(getSubStyleName(getLabelStyleType()));
- mInputBox->setStyle(getSubStyleName(getInputStyleType()));
- }
- INT32 GUIIntField::getValue() const
- {
- return applyRangeAndStep(mValue);
- }
- INT32 GUIIntField::setValue(INT32 value)
- {
- if (mValue == value)
- return value;
- mValue = value;
- value = applyRangeAndStep(value);
- setText(value);
- return value;
- }
- void GUIIntField::setRange(INT32 min, INT32 max)
- {
- mMinValue = min;
- mMaxValue = max;
- }
- void GUIIntField::setStep(INT32 step)
- {
- mStep = step;
- }
- void GUIIntField::setTint(const Color& color)
- {
- if (mLabel != nullptr)
- mLabel->setTint(color);
- mInputBox->setTint(color);
- }
- void GUIIntField::_setValue(INT32 value, bool triggerEvent)
- {
- mValue = value;
- setText(value);
- if (triggerEvent)
- onValueChanged(mValue);
- }
- 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 String& newValue)
- {
- valueChanged(parseINT32(newValue));
- }
- void GUIIntField::valueChanged(INT32 newValue)
- {
- CmdInputFieldValueChange<GUIIntField, INT32>::execute(this, newValue);
- }
- void GUIIntField::focusChanged(bool focus)
- {
- if (focus)
- {
- UndoRedo::instance().pushGroup("InputBox");
- mHasInputFocus = true;
- onFocusChanged(true);
- }
- else
- {
- UndoRedo::instance().popGroup("InputBox");
- setText(applyRangeAndStep(mValue));
- mHasInputFocus = false;
- onFocusChanged(false);
- }
- }
- void GUIIntField::inputConfirmed()
- {
- onConfirm();
- }
- void GUIIntField::setText(INT32 value)
- {
- // Only update with new value if it actually changed, otherwise
- // problems can occur when user types in "0." and the field
- // updates back to "0" effectively making "." unusable
- float curValue = parseFloat(mInputBox->getText());
- if (value != curValue)
- mInputBox->setText(toString(value));
- }
- INT32 GUIIntField::applyRangeAndStep(INT32 value) const
- {
- if (mStep != 0)
- value = value - value % mStep;
- return Math::clamp(value, mMinValue, mMaxValue);
- }
- bool GUIIntField::intFilter(const String& str)
- {
- return std::regex_match(str, std::regex("-?(\\d+)?"));
- }
- }
|