]> git.tdb.fi Git - libs/gui.git/blob - source/input/control.cpp
Drop Id tags and copyright notices from files
[libs/gui.git] / source / input / control.cpp
1 #include <msp/core/except.h>
2 #include <msp/strings/lexicalcast.h>
3 #include "control.h"
4 #include "device.h"
5
6 namespace Msp {
7 namespace Input {
8
9 ControlSource::ControlSource():
10         dev(0),
11         type(NONE),
12         index(0)
13 { }
14
15 ControlSource::ControlSource(Device &d, ControlSrcType t, unsigned i):
16         dev(&d),
17         type(t),
18         index(i)
19 { }
20
21 std::string ControlSource::str() const
22 {
23         if(type==BUTTON)
24                 return dev->get_button_name(index);
25         else if(type==AXIS_POS)
26                 return dev->get_axis_name(index)+" +";
27         else if(type==AXIS_NEG)
28                 return dev->get_axis_name(index)+" -";
29         else if(type==NONE)
30                 return "None";
31
32         return lexical_cast(index);
33 }
34
35
36 Control::Control():
37         capture_dev(0)
38 { }
39
40 Control::Control(const ControlSource &s):
41         src(s),
42         capture_dev(0)
43 { }
44
45 Control::Control(Device &d, ControlSrcType t, unsigned i):
46         src(d, t, i),
47         capture_dev(0)
48 {
49         connect_signals();
50 }
51
52 void Control::capture(Device &d)
53 {
54         notify_callbacks();
55         capture_dev=&d;
56         capture_dev->signal_button_press.connect(sigc::mem_fun(this, &Control::button_press));
57         capture_dev->signal_axis_motion.connect(sigc::mem_fun(this, &Control::axis_motion));
58 }
59
60 void Control::cancel_capture()
61 {
62         notify_callbacks();
63         capture_dev=0;
64         connect_signals();
65 }
66
67 void Control::set_source(Device &d, ControlSrcType t, unsigned i)
68 {
69         set_source(ControlSource(d, t, i));
70 }
71
72 void Control::set_source(const ControlSource &s)
73 {
74         notify_callbacks();
75         src=s;
76         connect_signals();
77 }
78
79 void Control::connect_signals()
80 {
81         switch(src.type)
82         {
83         case NONE:
84                 break;
85         case BUTTON:
86                 src.dev->signal_button_press.connect(sigc::mem_fun(this, &Control::button_press));
87                 src.dev->signal_button_release.connect(sigc::mem_fun(this, &Control::button_release));
88                 break;
89         case AXIS_POS:
90         case AXIS_NEG:
91                 src.dev->signal_axis_motion.connect(sigc::mem_fun(this, &Control::axis_motion));
92                 break;
93         default:
94                 throw Exception("Invalid source in Control");
95         }
96 }
97
98 void Control::button_press(unsigned i)
99 {
100         if(capture_dev)
101         {
102                 src.dev=capture_dev;
103                 src.type=BUTTON;
104                 src.index=i;
105
106                 notify_callbacks();
107                 capture_dev=0;
108                 connect_signals();
109                 signal_capture_complete.emit();
110         }
111         else if(src.type==BUTTON && i==src.index)
112                 on_press();
113 }
114
115 void Control::button_release(unsigned i)
116 {
117         if(src.type==BUTTON && i==src.index)
118                 on_release();
119 }
120
121 void Control::axis_motion(unsigned i, float v, float r)
122 {
123         if(capture_dev)
124         {
125                 ControlSrcType type=NONE;
126                 if(v<-0.9)
127                         type=AXIS_NEG;
128                 else if(v>0.9)
129                         type=AXIS_POS;
130
131                 if(type!=NONE)
132                 {
133                         src.dev=capture_dev;
134                         src.type=type;
135                         src.index=i;
136
137                         notify_callbacks();
138                         capture_dev=0;
139                         connect_signals();
140                         signal_capture_complete.emit();
141                 }
142         }
143         else if(src.type==AXIS_POS && i==src.index && v>=0)
144                 on_motion(v, r);
145         else if(src.type==AXIS_NEG && i==src.index && v<=0)
146                 on_motion(-v, -r);
147 }
148
149 } // namespace Input
150 } // namespace Msp