From 84e5e859191eeca5cf8b58da29b5f369be25c93c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 19 Dec 2014 05:04:11 +0200 Subject: [PATCH] Improve SmoothControl range API Since gestures can generate axis values greater than one, the ability to disable the upper threshold becomes necessary. --- source/input/smoothcontrol.cpp | 35 +++++++++++++++++++++++++++------- source/input/smoothcontrol.h | 6 +++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/source/input/smoothcontrol.cpp b/source/input/smoothcontrol.cpp index f32e551..6f4f5a4 100644 --- a/source/input/smoothcontrol.cpp +++ b/source/input/smoothcontrol.cpp @@ -1,5 +1,8 @@ +#include #include "smoothcontrol.h" +using namespace std; + namespace Msp { namespace Input { @@ -33,11 +36,23 @@ SmoothControl::~SmoothControl() void SmoothControl::set_dead_zone(float d) { + if(d<0 || (threshold>0 && d>=threshold)) + throw invalid_argument("SmoothControl::set_dead_zone"); dead_zone = d; } void SmoothControl::set_threshold(float t) { + if(t>=0 && t<=dead_zone) + throw invalid_argument("SmoothControl::set_threshold"); + threshold = t; +} + +void SmoothControl::set_range(float d, float t) +{ + if(d<0 || (t>=0 && d>=t)) + throw invalid_argument("SmoothControl::set_range"); + dead_zone = d; threshold = t; } @@ -72,17 +87,23 @@ void SmoothControl::on_release() void SmoothControl::on_motion(float v, float r) { - if(v<-threshold) - value = -1; - else if(v>threshold) - value = 1; - else if(v<-dead_zone) - value = (v+dead_zone)/(threshold-dead_zone); + if(v<-dead_zone) + value = v+dead_zone; else if(v>dead_zone) - value = (v-dead_zone)/(threshold-dead_zone); + value = v-dead_zone; else value = 0; + if(threshold>=0) + { + if(v<-threshold) + value = -1; + else if(v>threshold) + value = 1; + else + value /= threshold-dead_zone; + } + signal_motion.emit(value); if(paired_ctrl && (v>0 || (v==0 && paired_ctrl->value!=0))) diff --git a/source/input/smoothcontrol.h b/source/input/smoothcontrol.h index 78edbb3..f16177b 100644 --- a/source/input/smoothcontrol.h +++ b/source/input/smoothcontrol.h @@ -34,9 +34,13 @@ public: /// Sets the dead zone value. Any value below this will be treated as 0. void set_dead_zone(float); - /// Sets the max-out threshold. Any value above this will be treated as 1. + /** Sets the max-out threshold. Any value above this will be treated as 1. + A negative value can be used to disable the threshold entirely. */ void set_threshold(float); + /// Sets dead zone and threshold in a single function call. + void set_range(float, float); + void pair(SmoothControl *ctrl); float get_value() const { return value; } -- 2.43.0