Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410#include "Slider.h"#include "IconButton.h"#include "MaterialIcons.h"#include <algorithm>#include <cmath>#include <cstdio>#include <limits>
namespace UI {
// TODO: make magic numbers less obnoxius than a unnamed namespace, for now putting them in the class
// this is slop slop
Slider::Slider(double minValue, double maxValue, double step) : minValue(minValue), maxValue(maxValue), minFormat(minValue), maxFormat(maxValue), step(step), value(minValue) { minSize.height = 28.0f; maxSize.height = 28.0f;
auto decrease = std::make_unique<IconButton>(ICON_MS_REMOVE, "", [this]() { stepValue(-1.0); }); configureButton(*decrease); decrease->SetPadding(Thickness(2.0f, 2.0f)); decreaseButton = decrease.get();
auto valueDisplay = std::make_unique<Button>(formatValue(value)); configureButton(*valueDisplay); valueButton = valueDisplay.get();
auto increase = std::make_unique<IconButton>(ICON_MS_ADD, "", [this]() { stepValue(1.0); }); configureButton(*increase); increase->SetPadding(Thickness(2.0f, 2.0f)); increaseButton = increase.get();
Widget::AddChild(std::move(decrease)); Widget::AddChild(std::move(valueDisplay)); Widget::AddChild(std::move(increase));}
Slider::Slider(double minValue, double maxValue, double step, double exponent, double integerPrecisionStart) : Slider(minValue, maxValue, step) { SetExponent(exponent); SetIntegerPrecisionStart(integerPrecisionStart);}
Slider::~Slider() = default;
void Slider::SetRange(double minValue, double maxValue) { if (maxValue < minValue) { std::swap(minValue, maxValue); } this->minValue = minValue; this->maxValue = maxValue; this->value = snapValue(this->value); refreshDisplay(); MarkDirty(DirtyFlags::Layout | DirtyFlags::Render);}
void Slider::SetFormatRange(double minFormat, double maxFormat) { this->minFormat = minFormat; this->maxFormat = maxFormat; refreshDisplay(); MarkDirty(DirtyFlags::Layout | DirtyFlags::Render);}
void Slider::SetStep(double step) { this->step = std::max(step, 0.0); this->value = snapValue(this->value); refreshDisplay(); MarkDirty(DirtyFlags::Layout | DirtyFlags::Render);}
void Slider::SetExponent(double exponent) { this->exponent = exponent > 0.0 ? exponent : 1.0; refreshDisplay(); MarkDirty(DirtyFlags::Render);}
void Slider::SetIntegerPrecisionStart(double integerPrecisionStart) { this->integerPrecisionStart = integerPrecisionStart; this->value = snapValue(this->value); refreshDisplay(); MarkDirty(DirtyFlags::Layout | DirtyFlags::Render);}
void Slider::SetSmoothDrag(bool smoothDrag) { this->smoothDrag = smoothDrag; visualPosition = getPositionForValue(value); updateTrackPrimitives(); MarkDirty(DirtyFlags::Render);}
void Slider::SetSuffix(const std::string& suffix) { this->suffix = suffix; refreshDisplay(); MarkDirty(DirtyFlags::Layout | DirtyFlags::Render);}
void Slider::SetOnValueChanged(std::function<void(double)> callback) { onValueChanged = std::move(callback);}
void Slider::SetValue(double value) { const double snappedValue = snapValue(value); if (snappedValue == this->value) { return; }
this->value = snappedValue; refreshDisplay();
if (onValueChanged) { onValueChanged(this->value); }}
double Slider::GetValue() const { return value;}
double Slider::GetStep() const { return step;}
bool Slider::IsSmoothDrag() const { return smoothDrag;}
void Slider::configureButton(Button& button) { button.SetNormalColor(Color(51.0f/255.0f, 51.0f/255.0f, 51.0f/255.0f, 0.5f)); button.SetHoverColor(Color(0.2f, 0.2f, 0.2f, 0.25f)); button.SetActiveColor(Color(0.2f, 0.2f, 0.2f, 0.5f)); button.SetTextColor(Color(1.0f, 1.0f, 1.0f, 1.0f)); button.SetBorder(Color(0.2f, 0.2f, 0.2f, 0.75f), 2.0f, 28.0f * 0.5f); button.SetPadding(Thickness(0.0f, 0.0f));}
float Slider::getValueButtonWidth() const { const Size minTextSize = Text::Measure(formatValue(minValue)); const Size maxTextSize = Text::Measure(formatValue(maxValue)); const float textWidth = std::max(minTextSize.width, maxTextSize.width); return std::max(96.0f, textWidth + 24.0f);}
std::string Slider::formatValue(double value) const { const double valueSpan = maxValue - minValue; const double formatSpan = maxFormat - minFormat; const double normalized = valueSpan > 0.0 ? (value - minValue) / valueSpan : 0.0; const double formattedValue = minFormat + normalized * formatSpan;
// The displayed step maps the value's step through the format range, so // the precision follows what the user actually sees. A 0.01 step over // 0..1 shown as 0..100 is a displayed step of 1 and therefore no decimals. const double effectiveStep = getEffectiveStep(value); const double displayedStep = valueSpan > 0.0 ? std::abs(effectiveStep / valueSpan * formatSpan) : effectiveStep;
int decimals = 0; double scaledStep = displayedStep; if (!std::isfinite(scaledStep) || scaledStep <= 0.0) { scaledStep = 1.0; } while (scaledStep < 1.0 - 1.0e-9 && decimals < 6) { // no more than 6 decimals scaledStep *= 10.0; decimals++; }
char buffer[64]; std::snprintf(buffer, sizeof(buffer), "%.*f", decimals, formattedValue); return std::string(buffer) + suffix;}
double Slider::getEffectiveStep(double value) const { if (integerPrecisionStart != 0.0 && std::abs(value) >= std::abs(integerPrecisionStart)) { return 1.0; } return step;}
double Slider::getPositionForValue(double value) const { const double span = maxValue - minValue; if (span <= 0.0) { return 0.0; } const double normalized = std::clamp((value - minValue) / span, 0.0, 1.0); return std::pow(normalized, 1.0 / exponent);}
double Slider::getValueForPosition(double position) const { const double normalized = std::pow(std::clamp(position, 0.0, 1.0), exponent); return minValue + normalized * (maxValue - minValue);}
double Slider::snapValue(double rawValue) const { if (integerPrecisionStart != 0.0 && std::abs(rawValue) >= std::abs(integerPrecisionStart)) { return std::clamp(std::round(rawValue), minValue, maxValue); }
if (step <= 0.0) { return std::clamp(rawValue, minValue, maxValue); }
const double stepCount = std::round((rawValue - minValue) / step); const double snappedValue = minValue + stepCount * step; return std::clamp(snappedValue, minValue, maxValue);}
void Slider::refreshDisplay() { valueButton->SetLabel(formatValue(value)); updateTrackPrimitives(); MarkDirty(DirtyFlags::Render);}
void Slider::updateTrackPrimitives() { trackRect = std::make_unique<Rectangle>( trackBounds.X, trackBounds.Y, trackBounds.Width, trackBounds.Height ); trackRect->SetColor(0.2f, 0.2f, 0.2f, 0.5f); trackRect->SetBorderColor(0.2f, 0.2f, 0.2f, 0.75f); trackRect->SetBorderWidth(2.0f); trackRect->SetRoundness(trackBounds.Height * 0.5f);
fillRect.reset(); knobRect.reset(); if (trackBounds.Width <= 0.0f) { return; }
double position = getPositionForValue(value); if (dragging && smoothDrag) { position = visualPosition; } const float knobSize = 28.0f; const float travel = std::max(0.0f, trackBounds.Width - knobSize); const float centerX = trackBounds.X + knobSize * 0.5f + static_cast<float>(position) * travel;
const float fillLeft = trackBounds.X + 2.0f; const float fillRight = std::min(trackBounds.X + trackBounds.Width, centerX + knobSize * 0.5f); const float fillWidth = std::max(0.0f, fillRight - fillLeft); fillRect = std::make_unique<Rectangle>( fillLeft, trackBounds.Y + 2.0f, fillWidth, std::max(0.0f, trackBounds.Height - 4.0f) ); fillRect->SetColor(0.0f, 0.88f, 0.55f, 1.0f); fillRect->SetBorderWidth(0.0f); fillRect->SetRoundness(std::max(0.0f, trackBounds.Height * 0.5f - 2.0f));
knobRect = std::make_unique<Rectangle>( centerX - knobSize * 0.5f, trackBounds.Y, knobSize, knobSize ); knobRect->SetColor(1.0f, 1.0f, 1.0f, 1.0f); knobRect->SetBorderColor(0.2f, 0.2f, 0.2f, 0.75f); knobRect->SetBorderWidth(2.0f); knobRect->SetRoundness(knobSize * 0.5f);}
void Slider::setValueFromPointer(float pointerX) { if (trackBounds.Width <= 0.0f) { return; } const float knobSize = 28.0f;
const float travel = std::max(0.0f, trackBounds.Width - knobSize); if (travel <= 0.0f) { SetValue(maxValue); return; }
const double normalizedPosition = std::clamp( static_cast<double>(pointerX - (trackBounds.X + knobSize * 0.5f)) / travel, 0.0, 1.0 ); visualPosition = normalizedPosition; SetValue(getValueForPosition(normalizedPosition)); if (smoothDrag) { updateTrackPrimitives(); MarkDirty(DirtyFlags::Render); }}
void Slider::stepValue(double direction) { // Step out of the current value's precision region first, so leaving the // integer range at exactly the threshold uses the finer step below it // (20.0 - step => 19.9, not 19.0). const double infinity = std::numeric_limits<double>::infinity(); const double probe = std::nextafter(value, direction > 0.0 ? infinity : -infinity); const double stepSize = getEffectiveStep(probe); SetValue(value + stepSize * direction);}
void Slider::Measure(Size availableSpace) { const float valueButtonWidth = getValueButtonWidth(); const float naturalWidth = 28.0f * 2.0f + valueButtonWidth + 8.0f * 3.0f + // button spacing 80.0f + // minimum track padding.HorizontalTotal() + margin.HorizontalTotal();
desiredSize.width = std::clamp(std::min(naturalWidth, availableSpace.width), minSize.width, maxSize.width); desiredSize.height = std::clamp( 28.0f + padding.VerticalTotal() + margin.VerticalTotal(), minSize.height, maxSize.height );}
void Slider::Arrange(Rect finalBounds) { ArrangeCore(finalBounds);
const float contentHeight = 28.0f; const float y = padding.Top + (localBounds.Height - padding.VerticalTotal() - contentHeight) * 0.5f; const float buttonSize = contentHeight; float x = padding.Left;
decreaseButton->Arrange(Rect(x, y, buttonSize, buttonSize)); x += buttonSize + 8.0f;
const float valueButtonWidth = getValueButtonWidth(); valueButton->Arrange(Rect(x, y, valueButtonWidth, buttonSize)); x += valueButtonWidth + 8.0f;
increaseButton->Arrange(Rect(x, y, buttonSize, buttonSize)); x += buttonSize + 8.0f;
const float trackWidth = std::max(0.0f, localBounds.Width - padding.Right - x); trackBounds = Rect(globalBounds.X + x, globalBounds.Y + y, trackWidth, contentHeight); updateTrackPrimitives();
ClearDirty(DirtyFlags::Layout);}
void Slider::Draw() { if (!visible) { return; }
if (!trackRect) { updateTrackPrimitives(); }
trackRect->Render(); if (fillRect) { fillRect->Render(); } if (knobRect) { knobRect->Render(); }
Widget::Draw();}
bool Slider::OnPointerStream(const std::shared_ptr<PointerStream>& stream) { if (!enabled || !visible) { return false; }
const auto event = stream->PeekLatestInput();
if (event.actDirection == PointerDirection::Down) { if (!trackBounds.Contains(event.scaledX, event.scaledY)) { return false; } dragging = true; activePointerId = event.pointerId; setValueFromPointer(event.scaledX); return true; }
if (!dragging || event.pointerId != activePointerId) { return false; }
if (event.actDirection == PointerDirection::None) { setValueFromPointer(event.scaledX); return true; }
if (event.actDirection == PointerDirection::Up) { setValueFromPointer(event.scaledX); dragging = false; if (smoothDrag) { updateTrackPrimitives(); MarkDirty(DirtyFlags::Render); } return true; }
return false;}
}