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