]> git.tdb.fi Git - libs/gui.git/blobdiff - source/input/smoothcontrol.cpp
Fix the Windows version check in Touchscreen::is_available
[libs/gui.git] / source / input / smoothcontrol.cpp
index d45c9536d5e3a5b586f7d28b27e42d8a629d2ecd..6f4f5a487857deebe9fdaeaec277def92126937e 100644 (file)
@@ -1,12 +1,8 @@
-/* $Id$
-
-This file is part of libmspgbase
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <stdexcept>
 #include "smoothcontrol.h"
 
+using namespace std;
+
 namespace Msp {
 namespace Input {
 
@@ -40,12 +36,24 @@ SmoothControl::~SmoothControl()
 
 void SmoothControl::set_dead_zone(float d)
 {
-       dead_zone=d;
+       if(d<0 || (threshold>0 && d>=threshold))
+               throw invalid_argument("SmoothControl::set_dead_zone");
+       dead_zone = d;
 }
 
 void SmoothControl::set_threshold(float t)
 {
-       threshold=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;
 }
 
 void SmoothControl::pair(SmoothControl *ctrl)
@@ -55,12 +63,12 @@ void SmoothControl::pair(SmoothControl *ctrl)
 
        if(paired_ctrl)
        {
-               SmoothControl *old_pair=paired_ctrl;
-               paired_ctrl=0;
+               SmoothControl *old_pair = paired_ctrl;
+               paired_ctrl = 0;
                old_pair->pair(0);
        }
 
-       paired_ctrl=ctrl;
+       paired_ctrl = ctrl;
 
        if(paired_ctrl)
                paired_ctrl->pair(this);
@@ -79,16 +87,22 @@ 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;
+               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);