BsGUISliderField.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. setValueInternal(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. setValueInternal(value);
  43. onValueChanged(value);
  44. }
  45. void GUISliderField::setValueInternal(float value)
  46. {
  47. float origValue = mSlider->getValue();
  48. if (origValue != value)
  49. mSlider->setValue(value);
  50. float clampedValue = mSlider->getValue();
  51. // Only update with new value if it actually changed, otherwise
  52. // problems can occur when user types in "0." and the field
  53. // updates back to "0" effectively making "." unusable
  54. float curValue = parseFloat(mInputBox->getText());
  55. if (clampedValue != curValue)
  56. mInputBox->setText(toWString(clampedValue));
  57. }
  58. void GUISliderField::setRange(float min, float max)
  59. {
  60. mSlider->setRange(min, max);
  61. }
  62. void GUISliderField::setStep(float step)
  63. {
  64. mSlider->setStep(step);
  65. }
  66. void GUISliderField::setTint(const Color& color)
  67. {
  68. if (mLabel != nullptr)
  69. mLabel->setTint(color);
  70. mInputBox->setTint(color);
  71. }
  72. const String& GUISliderField::getGUITypeName()
  73. {
  74. static String typeName = "GUISliderField";
  75. return typeName;
  76. }
  77. const String& GUISliderField::getInputStyleType()
  78. {
  79. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  80. return LABEL_STYLE_TYPE;
  81. }
  82. const String& GUISliderField::getSliderStyleType()
  83. {
  84. static String SLIDER_STYLE_TYPE = "EditorSliderInput";
  85. return SLIDER_STYLE_TYPE;
  86. }
  87. void GUISliderField::styleUpdated()
  88. {
  89. if (mLabel != nullptr)
  90. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  91. mSlider->setStyle(getSubStyleName(getSliderStyleType()));
  92. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  93. }
  94. void GUISliderField::valueChanged(const WString& newValue)
  95. {
  96. float newFloatValue = parseFloat(newValue);
  97. CmdInputFieldValueChange<GUISliderField, float>::execute(this, newFloatValue);
  98. }
  99. void GUISliderField::sliderChanged(float newValue)
  100. {
  101. setValue(mSlider->getValue());
  102. }
  103. bool GUISliderField::floatFilter(const WString& str)
  104. {
  105. return std::regex_match(str, std::wregex(L"-?(\\d+(\\.\\d*)?)?"));
  106. }
  107. }