BsGUISliderField.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GUI/BsGUISliderField.h"
  4. #include "GUI/BsGUILayout.h"
  5. #include "GUI/BsGUIInputBox.h"
  6. #include "GUI/BsGUISpace.h"
  7. #include "GUI/BsGUISlider.h"
  8. #include "GUI/BsGUILabel.h"
  9. #include "UndoRedo/BsCmdInputFieldValueChange.h"
  10. #include <regex>
  11. using namespace std::placeholders;
  12. namespace bs
  13. {
  14. GUISliderField::GUISliderField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
  15. const String& style, const GUIDimensions& dimensions, bool withLabel)
  16. :TGUIField(dummy, labelContent, labelWidth, style, dimensions, withLabel), mInputBox(nullptr), mSlider(nullptr), mHasInputFocus(false)
  17. {
  18. mSlider = GUISliderHorz::create(GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getSliderStyleType()));
  19. mSlider->onChanged.connect(std::bind(&GUISliderField::sliderChanged, this, _1));
  20. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::fixedWidth(75)), getSubStyleName(getInputStyleType()));
  21. mInputBox->setFilter(&GUISliderField::floatFilter);
  22. mInputBox->onValueChanged.connect(std::bind((void(GUISliderField::*)(const String&))&GUISliderField::inputBoxValueChanging, this, _1));
  23. mInputBox->onConfirm.connect(std::bind(&GUISliderField::inputBoxValueChanged, this, true));
  24. mInputBox->onFocusChanged.connect(std::bind(&GUISliderField::inputBoxFocusChanged, this, _1));
  25. mLayout->addElement(mSlider);
  26. mLayout->addNewElement<GUIFixedSpace>(5);
  27. mLayout->addElement(mInputBox);
  28. setValue(0);
  29. mInputBox->setText("0");
  30. }
  31. GUISliderField::~GUISliderField()
  32. {
  33. }
  34. float GUISliderField::getValue() const
  35. {
  36. return mSlider->getValue();
  37. }
  38. float GUISliderField::getStep() const
  39. {
  40. return mSlider->getStep();
  41. }
  42. float GUISliderField::setValue(float value)
  43. {
  44. float origValue = mSlider->getValue();
  45. if (origValue != value)
  46. mSlider->setValue(value);
  47. float clampedValue = mSlider->getValue();
  48. // Only update with new value if it actually changed, otherwise problems can occur when user types in "0." and the
  49. // field updates back to "0" effectively making "." unusable
  50. float curValue = parseFloat(mInputBox->getText());
  51. if (clampedValue != curValue)
  52. mInputBox->setText(toString(clampedValue));
  53. return clampedValue;
  54. }
  55. void GUISliderField::setRange(float min, float max)
  56. {
  57. mSlider->setRange(min, max);
  58. }
  59. void GUISliderField::setStep(float step)
  60. {
  61. mSlider->setStep(step / (mSlider->getRangeMaximum() - mSlider->getRangeMinimum()));
  62. }
  63. void GUISliderField::setTint(const Color& color)
  64. {
  65. if (mLabel != nullptr)
  66. mLabel->setTint(color);
  67. mInputBox->setTint(color);
  68. }
  69. void GUISliderField::_setValue(float value, bool triggerEvent)
  70. {
  71. float clamped = setValue(value);
  72. if (triggerEvent)
  73. onValueChanged(clamped);
  74. }
  75. const String& GUISliderField::getGUITypeName()
  76. {
  77. static String typeName = "GUISliderField";
  78. return typeName;
  79. }
  80. const String& GUISliderField::getInputStyleType()
  81. {
  82. static String LABEL_STYLE_TYPE = "EditorFieldInput";
  83. return LABEL_STYLE_TYPE;
  84. }
  85. const String& GUISliderField::getSliderStyleType()
  86. {
  87. static String SLIDER_STYLE_TYPE = "EditorSliderInput";
  88. return SLIDER_STYLE_TYPE;
  89. }
  90. void GUISliderField::styleUpdated()
  91. {
  92. if (mLabel != nullptr)
  93. mLabel->setStyle(getSubStyleName(getLabelStyleType()));
  94. mSlider->setStyle(getSubStyleName(getSliderStyleType()));
  95. mInputBox->setStyle(getSubStyleName(getInputStyleType()));
  96. }
  97. void GUISliderField::inputBoxValueChanging(const String& newValue)
  98. {
  99. inputBoxValueChanged(false);
  100. }
  101. void GUISliderField::inputBoxValueChanged(bool confirmed)
  102. {
  103. float newFloatValue = parseFloat(mInputBox->getText());
  104. if (mSlider->getValue() != newFloatValue) {
  105. if (confirmed) {
  106. CmdInputFieldValueChange<GUISliderField, float>::execute(this, newFloatValue);
  107. }
  108. else
  109. {
  110. mSlider->setValue(newFloatValue);
  111. onValueChanged(mSlider->getValue());
  112. }
  113. }
  114. else if (mInputBox->getText() == "" && confirmed) //Avoid leaving label blank
  115. {
  116. mInputBox->setText("0");
  117. }
  118. }
  119. void GUISliderField::inputBoxFocusChanged(bool focus)
  120. {
  121. if (focus)
  122. {
  123. UndoRedo::instance().pushGroup("InputBox");
  124. mHasInputFocus = true;
  125. onFocusChanged(true);
  126. }
  127. else
  128. {
  129. UndoRedo::instance().popGroup("InputBox");
  130. inputBoxValueChanged();
  131. mHasInputFocus = false;
  132. onFocusChanged(false);
  133. }
  134. }
  135. void GUISliderField::sliderChanged(float newValue)
  136. {
  137. _setValue(newValue, true);
  138. }
  139. bool GUISliderField::floatFilter(const String& str)
  140. {
  141. bool result = std::regex_match(str, std::regex("-?(\\d*(\\.\\d*)?)?"));
  142. return result;
  143. }
  144. }