BsGUISliderField.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. {
  22. mSlider = GUISliderHorz::create(GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getSliderStyleType()));
  23. mSlider->onChanged.connect(std::bind(&GUISliderField::sliderChanged, this, _1));
  24. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::fixedWidth(75)), getSubStyleName(getInputStyleType()));
  25. mInputBox->setFilter(&GUISliderField::floatFilter);
  26. mInputBox->onValueChanged.connect(std::bind((void(GUISliderField::*)(const WString&))&GUISliderField::valueChanged, this, _1));
  27. mLayout->addElement(mSlider);
  28. mLayout->addNewElement<GUIFixedSpace>(5);
  29. mLayout->addElement(mInputBox);
  30. setValue(0);
  31. mInputBox->setText(L"0");
  32. }
  33. GUISliderField::~GUISliderField()
  34. {
  35. }
  36. float GUISliderField::getValue() const
  37. {
  38. return mSlider->getValue();
  39. }
  40. void GUISliderField::setValue(float value)
  41. {
  42. float origValue = mSlider->getValue();
  43. if (origValue != value)
  44. mSlider->setValue(value);
  45. float clampedValue = mSlider->getValue();
  46. // Only update with new value if it actually changed, otherwise
  47. // problems can occur when user types in "0." and the field
  48. // updates back to "0" effectively making "." unusable
  49. float curValue = parseFloat(mInputBox->getText());
  50. if (clampedValue != curValue)
  51. mInputBox->setText(toWString(clampedValue));
  52. }
  53. void GUISliderField::setRange(float min, float max)
  54. {
  55. mSlider->setRange(min, max);
  56. }
  57. void GUISliderField::setStep(float step)
  58. {
  59. mSlider->setStep(step);
  60. }
  61. void GUISliderField::setTint(const Color& color)
  62. {
  63. if (mLabel != nullptr)
  64. mLabel->setTint(color);
  65. mInputBox->setTint(color);
  66. }
  67. const String& GUISliderField::getGUITypeName()
  68. {
  69. static String typeName = "GUISliderField";
  70. return typeName;
  71. }
  72. const String& GUISliderField::getInputStyleType()
  73. {
  74. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  75. return LABEL_STYLE_TYPE;
  76. }
  77. const String& GUISliderField::getSliderStyleType()
  78. {
  79. static String SLIDER_STYLE_TYPE = "EditorSliderInput";
  80. return SLIDER_STYLE_TYPE;
  81. }
  82. void GUISliderField::styleUpdated()
  83. {
  84. if (mLabel != nullptr)
  85. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  86. mSlider->setStyle(getSubStyleName(getSliderStyleType()));
  87. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  88. }
  89. void GUISliderField::valueChanged(const WString& newValue)
  90. {
  91. float newFloatValue = parseFloat(newValue);
  92. CmdInputFieldValueChange<GUISliderField, float>::execute(this, newFloatValue);
  93. }
  94. void GUISliderField::sliderChanged(float newValue)
  95. {
  96. setValue(mSlider->getValue());
  97. onValueChanged(mSlider->getValue());
  98. }
  99. bool GUISliderField::floatFilter(const WString& str)
  100. {
  101. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  102. }
  103. }