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