]> git.tdb.fi Git - libs/gui.git/blob - source/input/smoothcontrol.cpp
Use nullptr in place of 0 or NULL
[libs/gui.git] / source / input / smoothcontrol.cpp
1 #include "smoothcontrol.h"
2 #include <stdexcept>
3
4 using namespace std;
5
6 namespace Msp {
7 namespace Input {
8
9 SmoothControl::SmoothControl(const ControlSource &s):
10         Control(s)
11 { }
12
13 SmoothControl::SmoothControl(Device &d, ControlSrcType t, unsigned i):
14         Control(d, t, i)
15 { }
16
17 SmoothControl::~SmoothControl()
18 {
19         pair(nullptr);
20 }
21
22 void SmoothControl::set_dead_zone(float d)
23 {
24         if(d<0 || (threshold>0 && d>=threshold))
25                 throw invalid_argument("SmoothControl::set_dead_zone");
26         dead_zone = d;
27 }
28
29 void SmoothControl::set_threshold(float t)
30 {
31         if(t>=0 && t<=dead_zone)
32                 throw invalid_argument("SmoothControl::set_threshold");
33         threshold = t;
34 }
35
36 void SmoothControl::set_range(float d, float t)
37 {
38         if(d<0 || (t>=0 && d>=t))
39                 throw invalid_argument("SmoothControl::set_range");
40         dead_zone = d;
41         threshold = t;
42 }
43
44 void SmoothControl::pair(SmoothControl *ctrl)
45 {
46         if(ctrl==paired_ctrl)
47                 return;
48
49         if(paired_ctrl)
50         {
51                 SmoothControl *old_pair = paired_ctrl;
52                 paired_ctrl = nullptr;
53                 old_pair->pair(nullptr);
54         }
55
56         paired_ctrl = ctrl;
57
58         if(paired_ctrl)
59                 paired_ctrl->pair(this);
60 }
61
62 void SmoothControl::on_press()
63 {
64         on_motion(1, 1-value);
65 }
66
67 void SmoothControl::on_release()
68 {
69         if(value>0)
70                 on_motion(0, -value);
71 }
72
73 void SmoothControl::on_motion(float v, float r)
74 {
75         float old_value = value;
76         if(v<-dead_zone)
77                 value = v+dead_zone;
78         else if(v>dead_zone)
79                 value = v-dead_zone;
80         else
81                 value = 0;
82
83         if(threshold>=0)
84         {
85                 if(v<-threshold)
86                         value = -1;
87                 else if(v>threshold)
88                         value = 1;
89                 else
90                         value /= threshold-dead_zone;
91         }
92
93         if(value && !old_value)
94                 rising_edge = true;
95         else if(!value && old_value)
96                 falling_edge = true;
97
98         signal_motion.emit(value);
99
100         if(paired_ctrl && (v>0 || (v==0 && paired_ctrl->value!=0)))
101                 paired_ctrl->on_motion(-v, -r);
102 }
103
104 } // namespace Input
105 } // namespace Msp