diff --git a/README.md b/README.md index b7c16a6..cb83091 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ mySlider->setRange(0.0001f, 10.0f); // Set min/max range mySlider->setDefault(3.0f); // Default value mySlider->setUsingLogScale(true); // Enable log scale mySlider->setSteadyOnClick(true); // Enable steady-on-click behavior +mySlider->setInteger(true); // Quantize slider to whole integers mySlider->setColors( // Configure colors , diff --git a/src/ExtraEventHandlers.cpp b/src/ExtraEventHandlers.cpp index 459f9dc..059c519 100644 --- a/src/ExtraEventHandlers.cpp +++ b/src/ExtraEventHandlers.cpp @@ -237,6 +237,7 @@ struct PDSliderEventHandler::PrivateData bool usingDefault; bool usingLog; bool steadyOnClick; + bool isInteger; bool dragging; bool inverted; bool valueIsSet; @@ -261,6 +262,7 @@ struct PDSliderEventHandler::PrivateData usingDefault(false), usingLog(false), steadyOnClick(false), + isInteger(false), dragging(false), inverted(false), valueIsSet(false), @@ -287,6 +289,7 @@ struct PDSliderEventHandler::PrivateData usingDefault(other->usingDefault), usingLog(other->usingLog), steadyOnClick(other->steadyOnClick), + isInteger(other->isInteger), startPos(other->startPos), endPos(other->endPos), dragging(false), @@ -310,6 +313,7 @@ struct PDSliderEventHandler::PrivateData usingDefault = other->usingDefault; usingLog = other->usingLog; steadyOnClick = other->steadyOnClick; + isInteger = other->isInteger; } inline float logscale(const float v) const @@ -497,10 +501,24 @@ struct PDSliderEventHandler::PrivateData minimum = min; maximum = max; + + if (isInteger) + setValue(value, false); } - bool setValue(const float value2, const bool sendCallback) + bool setValue(float value2, const bool sendCallback) { + if (isInteger) + { + value2 = std::round(value2); + const float minInt = std::ceil(minimum); + const float maxInt = std::floor(maximum); + if (minInt <= maxInt) + value2 = clamp(value2, maxInt, minInt); + else + value2 = clamp(value2, maximum, minimum); + } + if (d_isEqual(value, value2)) return false; @@ -527,6 +545,20 @@ struct PDSliderEventHandler::PrivateData inverted = inv; widget->repaint(); } + + void setInteger(const bool yesNo) noexcept + { + if (isInteger == yesNo) + return; + + isInteger = yesNo; + + if (isInteger) + { + setValue(value, false); + valueDef = std::round(valueDef); + } + } }; // -------------------------------------------------------------------------------------------------------------------- @@ -565,7 +597,7 @@ float PDSliderEventHandler::getNormalizedValue() const noexcept void PDSliderEventHandler::setDefault(const float def) noexcept { - pData->valueDef = def; + pData->valueDef = pData->isInteger ? std::round(def) : def; pData->usingDefault = true; } @@ -590,6 +622,11 @@ void PDSliderEventHandler::setSteadyOnClick(const bool yesNo) noexcept pData->steadyOnClick = yesNo; } +void PDSliderEventHandler::setInteger(const bool yesNo) noexcept +{ + pData->setInteger(yesNo); +} + void PDSliderEventHandler::setStartPos(const int x, const int y) noexcept { pData->startPos = Point(x, y); diff --git a/src/ExtraEventHandlers.hpp b/src/ExtraEventHandlers.hpp index d6ecf2c..331b328 100644 --- a/src/ExtraEventHandlers.hpp +++ b/src/ExtraEventHandlers.hpp @@ -122,6 +122,7 @@ class PDSliderEventHandler void setRange(float min, float max) noexcept; void setUsingLogScale(bool yesNo) noexcept; void setSteadyOnClick(bool yesNo) noexcept; + void setInteger(bool yesNo = true) noexcept; void setStartPos(const int x, const int y) noexcept; void setEndPos(const int x, const int y) noexcept; void setCallback(Callback *callback) noexcept;