From 3753ae9ba74fde7464c101fcdbc99b1bd83ce647 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 3 Jul 2016 13:42:42 +0200 Subject: compat/options: fix and simplify slider support - Introduce rounding. Before, slider pos didn't correspond to the saved setting until it "converged" several saves later. - Move copy-pasted code to .cpp file. --- opentrack-compat/options.cpp | 25 +++++++++++++++++++++++++ opentrack-compat/options.hpp | 2 ++ 2 files changed, 27 insertions(+) (limited to 'opentrack-compat') diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp index 4106f783..dc4998ad 100644 --- a/opentrack-compat/options.cpp +++ b/opentrack-compat/options.cpp @@ -282,5 +282,30 @@ bool slider_value::operator==(const slider_value& v) const fabs(v.max_ - max_) < eps); } +slider_value slider_value::update_from_slider(int pos, int q_min, int q_max) const +{ + slider_value v(*this); + + const int q_diff = q_max - q_min; + const double sv_pos = q_diff == 0 + ? -1e6 + : (((pos - q_min) * (v.max() - v.min())) / q_diff + v.min()); + + if (sv_pos < v.min()) + v = slider_value(v.min(), v.min(), v.max()); + else if (sv_pos > v.max()) + v = slider_value(v.max(), v.min(), v.max()); + else + v = slider_value(sv_pos, v.min(), v.max()); + return v; +} + +int slider_value::to_slider_pos(int q_min, int q_max) const +{ + const int q_diff = q_max - q_min; + + return int(std::round(((cur() - min()) * q_diff / (max() - min())) + q_min)); +} + } // end options diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp index 3e5ecbf4..86c699d6 100644 --- a/opentrack-compat/options.hpp +++ b/opentrack-compat/options.hpp @@ -65,6 +65,8 @@ namespace options double cur() const { return cur_; } double min() const { return min_; } double max() const { return max_; } + slider_value update_from_slider(int pos, int q_min, int q_max) const; + int to_slider_pos(int q_min, int q_max) const; }; } -- cgit v1.2.3