Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<background_color>,
<slider_color>
Expand Down
41 changes: 39 additions & 2 deletions src/ExtraEventHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ struct PDSliderEventHandler::PrivateData
bool usingDefault;
bool usingLog;
bool steadyOnClick;
bool isInteger;
bool dragging;
bool inverted;
bool valueIsSet;
Expand All @@ -261,6 +262,7 @@ struct PDSliderEventHandler::PrivateData
usingDefault(false),
usingLog(false),
steadyOnClick(false),
isInteger(false),
dragging(false),
inverted(false),
valueIsSet(false),
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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);
Comment thread
dromer marked this conversation as resolved.
}

if (d_isEqual(value, value2))
return false;

Expand All @@ -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);
}
}
};

// --------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<int>(x, y);
Expand Down
1 change: 1 addition & 0 deletions src/ExtraEventHandlers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down