BsGUISliderField.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <regex>
  10. using namespace std::placeholders;
  11. namespace bs
  12. {
  13. GUISliderField::GUISliderField(const PrivatelyConstruct& dummy, const GUIContent& labelContent, UINT32 labelWidth,
  14. const String& style, const GUIDimensions& dimensions, bool withLabel)
  15. :TGUIField(dummy, labelContent, labelWidth, style, dimensions, withLabel), mInputBox(nullptr), mSlider(nullptr), mHasInputFocus(false)
  16. {
  17. mSlider = GUISliderHorz::create(GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getSliderStyleType()));
  18. mSlider->onChanged.connect(std::bind(&GUISliderField::sliderChanged, this, _1));
  19. mInputBox = GUIInputBox::create(false, GUIOptions(GUIOption::fixedWidth(75)), getSubStyleName(getInputStyleType()));
  20. mInputBox->setFilter(&GUISliderField::floatFilter);
  21. mInputBox->onValueChanged.connect(std::bind((void(GUISliderField::*)(const String&))&GUISliderField::inputBoxValueChanging, this, _1));
  22. mInputBox->onConfirm.connect(std::bind(&GUISliderField::inputBoxValueChanged, this, true));
  23. mInputBox->onFocusChanged.connect(std::bind(&GUISliderField::inputBoxFocusChanged, this, _1));
  24. mLayout->addElement(mSlider);
  25. mLayout->addNewElement<GUIFixedSpace>(5);
  26. mLayout->addElement(mInputBox);
  27. setValue(0);
  28. mInputBox->setText("0");
  29. mFocusElement = mInputBox;
  30. }
  31. float GUISliderField::getValue() const
  32. {
  33. return mSlider->getValue();
  34. }
  35. float GUISliderField::getStep() const
  36. {
  37. return mSlider->getStep();
  38. }
  39. float GUISliderField::setValue(float value)
  40. {
  41. float origValue = mSlider->getValue();
  42. if (origValue != value)
  43. mSlider->setValue(value);
  44. float clampedValue = mSlider->getValue();
  45. // Only update with new value if it actually changed, otherwise problems can occur when user types in "0." and the
  46. // field updates back to "0" effectively making "." unusable
  47. float curValue = parseFloat(mInputBox->getText());
  48. if (clampedValue != curValue)
  49. mInputBox->setText(toString(clampedValue));
  50. return clampedValue;
  51. }
  52. void GUISliderField::setRange(float min, float max)
  53. {
  54. mSlider->setRange(min, max);
  55. }
  56. void GUISliderField::setStep(float step)
  57. {
  58. mSlider->setStep(step);
  59. }
  60. void GUISliderField::setTint(const Color& color)
  61. {
  62. if (mLabel != nullptr)
  63. mLabel->setTint(color);
  64. mInputBox->setTint(color);
  65. }
  66. void GUISliderField::_setValue(float value, bool triggerEvent)
  67. {
  68. float clamped = setValue(value);
  69. if (triggerEvent)
  70. onValueChanged(clamped);
  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::inputBoxValueChanging(const String& newValue)
  95. {
  96. inputBoxValueChanged(false);
  97. }
  98. void GUISliderField::inputBoxValueChanged(bool confirmed)
  99. {
  100. float newFloatValue = parseFloat(mInputBox->getText());
  101. if (mSlider->getValue() != newFloatValue) {
  102. if (confirmed)
  103. _setValue(newFloatValue, true);
  104. else
  105. {
  106. mSlider->setValue(newFloatValue);
  107. onValueChanged(mSlider->getValue());
  108. }
  109. }
  110. else if (mInputBox->getText().empty() && confirmed) //Avoid leaving label blank
  111. {
  112. mInputBox->setText("0");
  113. }
  114. }
  115. void GUISliderField::inputBoxFocusChanged(bool focus)
  116. {
  117. if (focus)
  118. {
  119. mHasInputFocus = true;
  120. onFocusChanged(true);
  121. }
  122. else
  123. {
  124. inputBoxValueChanged();
  125. mHasInputFocus = false;
  126. onFocusChanged(false);
  127. }
  128. }
  129. void GUISliderField::sliderChanged(float newValue)
  130. {
  131. _setValue(newValue, true);
  132. }
  133. bool GUISliderField::floatFilter(const String& str)
  134. {
  135. bool result = std::regex_match(str, std::regex("-?(\\d*(\\.\\d*)?)?"));
  136. return result;
  137. }
  138. }