X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Fsmoothcontrol.cpp;h=0532aef68ca66d62dc6d38808da0a08c07529eb9;hb=27356249e3607c78f5da9823c88703a6f4f7bed1;hp=f32e551d89c682e34e9de5157c83e400a6dd8938;hpb=821cea8627597a5458c1cb02c0652384bb3431a4;p=libs%2Fgui.git diff --git a/source/input/smoothcontrol.cpp b/source/input/smoothcontrol.cpp index f32e551..0532aef 100644 --- a/source/input/smoothcontrol.cpp +++ b/source/input/smoothcontrol.cpp @@ -1,43 +1,43 @@ #include "smoothcontrol.h" +#include + +using namespace std; namespace Msp { namespace Input { -SmoothControl::SmoothControl(): - value(0), - paired_ctrl(0), - dead_zone(0.1), - threshold(0.9) -{ } - SmoothControl::SmoothControl(const ControlSource &s): - Control(s), - value(0), - paired_ctrl(0), - dead_zone(0.1), - threshold(0.9) + Control(s) { } SmoothControl::SmoothControl(Device &d, ControlSrcType t, unsigned i): - Control(d, t, i), - value(0), - paired_ctrl(0), - dead_zone(0.1), - threshold(0.9) + Control(d, t, i) { } SmoothControl::~SmoothControl() { - pair(0); + pair(nullptr); } 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; } @@ -49,8 +49,8 @@ void SmoothControl::pair(SmoothControl *ctrl) if(paired_ctrl) { SmoothControl *old_pair = paired_ctrl; - paired_ctrl = 0; - old_pair->pair(0); + paired_ctrl = nullptr; + old_pair->pair(nullptr); } paired_ctrl = ctrl; @@ -72,17 +72,29 @@ 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); + float old_value = value; + 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; + } + + if(value && !old_value) + rising_edge = true; + else if(!value && old_value) + falling_edge = true; + signal_motion.emit(value); if(paired_ctrl && (v>0 || (v==0 && paired_ctrl->value!=0)))