BsGUISliderField.cpp 3.9 KB

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