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