BsGUISliderField.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "BsGUISliderField.h"
  2. #include "BsGUILayout.h"
  3. #include "BsGUILabel.h"
  4. #include "BsGUIInputBox.h"
  5. #include "BsGUISpace.h"
  6. #include "BsBuiltinResources.h"
  7. #include "BsCGUIWidget.h"
  8. #include "BsGUIMouseEvent.h"
  9. #include "BsCursor.h"
  10. #include "BsCGUIWidget.h"
  11. #include "BsViewport.h"
  12. #include "BsGUISlider.h"
  13. #include "BsCmdInputFieldValueChange.h"
  14. #include <regex>
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. GUISliderField::GUISliderField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
  19. const String& style, const GUIDimensions& dimensions, bool withLabel)
  20. :TGUIField(dummy, labelContent, labelWidth, style, dimensions, withLabel), mInputBox(nullptr), mSlider(nullptr),
  21. mHasInputFocus(false)
  22. {
  23. mSlider = GUISliderHorz::create(GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getSliderStyleType()));
  24. mSlider->onChanged.connect(std::bind(&GUISliderField::sliderChanged, this, _1));
  25. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::fixedWidth(75)), getSubStyleName(getInputStyleType()));
  26. mInputBox->setFilter(&GUISliderField::floatFilter);
  27. mInputBox->onValueChanged.connect(std::bind((void(GUISliderField::*)(const WString&))&GUISliderField::valueChanged, this, _1));
  28. mInputBox->onFocusGained.connect(std::bind(&GUISliderField::focusGained, this));
  29. mInputBox->onFocusLost.connect(std::bind(&GUISliderField::focusLost, this));
  30. mLayout->addElement(mSlider);
  31. mLayout->addNewElement<GUIFixedSpace>(5);
  32. mLayout->addElement(mInputBox);
  33. setValue(0);
  34. mInputBox->setText(L"0");
  35. }
  36. GUISliderField::~GUISliderField()
  37. {
  38. }
  39. float GUISliderField::getValue() const
  40. {
  41. return mSlider->getValue();
  42. }
  43. void GUISliderField::setValue(float value)
  44. {
  45. float origValue = mSlider->getValue();
  46. if (origValue != value)
  47. mSlider->setValue(value);
  48. float clampedValue = mSlider->getValue();
  49. // Only update with new value if it actually changed, otherwise
  50. // problems can occur when user types in "0." and the field
  51. // updates back to "0" effectively making "." unusable
  52. float curValue = parseFloat(mInputBox->getText());
  53. if (clampedValue != curValue)
  54. mInputBox->setText(toWString(clampedValue));
  55. }
  56. void GUISliderField::setRange(float min, float max)
  57. {
  58. mSlider->setRange(min, max);
  59. }
  60. void GUISliderField::setStep(float step)
  61. {
  62. mSlider->setStep(step);
  63. }
  64. void GUISliderField::setTint(const Color& color)
  65. {
  66. if (mLabel != nullptr)
  67. mLabel->setTint(color);
  68. mInputBox->setTint(color);
  69. }
  70. void GUISliderField::updateClippedBounds()
  71. {
  72. mClippedBounds = mLayoutData.area;
  73. }
  74. const String& GUISliderField::getGUITypeName()
  75. {
  76. static String typeName = "GUISliderField";
  77. return typeName;
  78. }
  79. const String& GUISliderField::getInputStyleType()
  80. {
  81. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  82. return LABEL_STYLE_TYPE;
  83. }
  84. const String& GUISliderField::getSliderStyleType()
  85. {
  86. static String SLIDER_STYLE_TYPE = "EditorSliderInput";
  87. return SLIDER_STYLE_TYPE;
  88. }
  89. void GUISliderField::styleUpdated()
  90. {
  91. if (mLabel != nullptr)
  92. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  93. mSlider->setStyle(getSubStyleName(getSliderStyleType()));
  94. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  95. }
  96. void GUISliderField::valueChanged(const WString& newValue)
  97. {
  98. float newFloatValue = parseFloat(newValue);
  99. CmdInputFieldValueChange<GUISliderField, float>::execute(this, newFloatValue);
  100. }
  101. void GUISliderField::sliderChanged(float newValue)
  102. {
  103. setValue(mSlider->getValue());
  104. onValueChanged(mSlider->getValue());
  105. }
  106. void GUISliderField::focusGained()
  107. {
  108. UndoRedo::instance().pushGroup("InputBox");
  109. mHasInputFocus = true;
  110. }
  111. void GUISliderField::focusLost()
  112. {
  113. UndoRedo::instance().popGroup("InputBox");
  114. mHasInputFocus = false;
  115. }
  116. bool GUISliderField::floatFilter(const WString& str)
  117. {
  118. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  119. }
  120. }