]> git.tdb.fi Git - libs/gui.git/blob - source/input/control.h
Add an activator system to Control
[libs/gui.git] / source / input / control.h
1 #ifndef MSP_INPUT_CONTROL_H_
2 #define MSP_INPUT_CONTROL_H_
3
4 #include <string>
5 #include <sigc++/signal.h>
6 #include <sigc++/trackable.h>
7
8 namespace Msp {
9 namespace Input {
10
11 class BinaryControl;
12 class Device;
13
14 enum ControlSrcType
15 {
16         NONE,
17         BUTTON,
18         AXIS_POS,
19         AXIS_NEG
20 };
21
22 /**
23 Specifies the source of a control.  This provides a way for setting sources for
24 different types of controls in a uniform way.
25 */
26 struct ControlSource
27 {
28         Device *dev;
29         ControlSrcType type;
30         unsigned index;
31
32         ControlSource();
33         ControlSource(Device &, ControlSrcType, unsigned);
34         std::string str() const;
35 };
36
37 /**
38 Provides further abstraction on top of input devices.  There are two types of
39 controls currently defined: BinaryControl and SmoothControl.
40
41 A control uses either a button or half of an axis (positive or negative) as its
42 source.  How the source values are interpreted depends on the exact type of the
43 control.  Controls also support interactive binding by capturing a button press
44 or axis motion.
45
46 A BinaryControl can be used as an activator, requiring that control to be
47 active for any events to be processed.  This can be used to implement shifted
48 controls (when used on a BinaryControl) or click-and-drag functionality (when
49 used on a SmoothControl).
50 */
51 class Control: public sigc::trackable
52 {
53 public:
54         sigc::signal<void> signal_capture_complete;
55
56 protected:
57         ControlSource src;
58         Device *capture_dev;
59         BinaryControl *activator;
60         float origin;
61
62         Control();
63         Control(const ControlSource &);
64         Control(Device &, ControlSrcType, unsigned);
65 public:
66         virtual ~Control();
67
68         void capture(Device &);
69         void cancel_capture();
70         void set_source(Device &, ControlSrcType, unsigned);
71         void set_source(const ControlSource &);
72         const ControlSource &get_source() const { return src; }
73         void set_activator(BinaryControl *);
74         BinaryControl *get_activator() const { return activator; }
75 protected:
76         virtual void on_press() = 0;
77         virtual void on_release() = 0;
78         virtual void on_motion(float, float) = 0;
79
80 private:
81         void connect_signals();
82         void button_press(unsigned);
83         void button_release(unsigned);
84         void axis_motion(unsigned, float, float);
85         void deactivate();
86
87         Control(const Control &);
88         Control &operator=(const Control &);
89 };
90
91 } // namespace Input
92 } // namespace Msp
93
94 #endif