From 754d00736b05c0f07f2fe34889527f8e3013dd37 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 19 Dec 2014 04:24:55 +0200 Subject: [PATCH] Add an activator system to Control --- source/input/control.cpp | 49 ++++++++++++++++++++++++++++++++-------- source/input/control.h | 11 +++++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/source/input/control.cpp b/source/input/control.cpp index 5060101..b2f9bad 100644 --- a/source/input/control.cpp +++ b/source/input/control.cpp @@ -1,5 +1,6 @@ #include #include +#include "binarycontrol.h" #include "control.h" #include "device.h" @@ -36,17 +37,23 @@ std::string ControlSource::str() const Control::Control(): - capture_dev(0) + capture_dev(0), + activator(0), + origin(0) { } Control::Control(const ControlSource &s): src(s), - capture_dev(0) + capture_dev(0), + activator(0), + origin(0) { } Control::Control(Device &d, ControlSrcType t, unsigned i): src(d, t, i), - capture_dev(0) + capture_dev(0), + activator(0), + origin(0) { connect_signals(); } @@ -81,6 +88,13 @@ void Control::set_source(const ControlSource &s) connect_signals(); } +void Control::set_activator(BinaryControl *ctrl) +{ + notify_callbacks(); + activator = ctrl; + connect_signals(); +} + void Control::connect_signals() { switch(src.type) @@ -96,6 +110,9 @@ void Control::connect_signals() src.dev->signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Control::axis_motion), false)); break; } + + if(activator) + activator->signal_release.connect(sigc::mem_fun(this, &Control::deactivate)); } void Control::button_press(unsigned i) @@ -111,13 +128,13 @@ void Control::button_press(unsigned i) connect_signals(); signal_capture_complete.emit(); } - else if(src.type==BUTTON && i==src.index) + else if(src.type==BUTTON && i==src.index && (!activator || activator->get_state())) on_press(); } void Control::button_release(unsigned i) { - if(src.type==BUTTON && i==src.index) + if(src.type==BUTTON && i==src.index && (!activator || activator->get_state())) on_release(); } @@ -143,10 +160,24 @@ void Control::axis_motion(unsigned i, float v, float r) signal_capture_complete.emit(); } } - else if(src.type==AXIS_POS && i==src.index && v>=0) - on_motion(v, r); - else if(src.type==AXIS_NEG && i==src.index && v<=0) - on_motion(-v, -r); + else if(activator && !activator->get_state() && i==src.index) + origin = v; + else if(src.type==AXIS_POS && i==src.index && v>=origin) + on_motion(v-origin, r); + else if(src.type==AXIS_NEG && i==src.index && v<=origin) + on_motion(origin-v, -r); +} + +void Control::deactivate() +{ + if(src.type==BUTTON) + on_release(); + else if(src.type==AXIS_POS || src.type==AXIS_NEG) + { + float v = src.dev->get_axis_value(src.index); + on_motion(0, (src.type==AXIS_POS ? origin-v : v-origin)); + origin = v; + } } } // namespace Input diff --git a/source/input/control.h b/source/input/control.h index e567963..450b057 100644 --- a/source/input/control.h +++ b/source/input/control.h @@ -8,6 +8,7 @@ namespace Msp { namespace Input { +class BinaryControl; class Device; enum ControlSrcType @@ -41,6 +42,11 @@ A control uses either a button or half of an axis (positive or negative) as its source. How the source values are interpreted depends on the exact type of the control. Controls also support interactive binding by capturing a button press or axis motion. + +A BinaryControl can be used as an activator, requiring that control to be +active for any events to be processed. This can be used to implement shifted +controls (when used on a BinaryControl) or click-and-drag functionality (when +used on a SmoothControl). */ class Control: public sigc::trackable { @@ -50,6 +56,8 @@ public: protected: ControlSource src; Device *capture_dev; + BinaryControl *activator; + float origin; Control(); Control(const ControlSource &); @@ -62,6 +70,8 @@ public: void set_source(Device &, ControlSrcType, unsigned); void set_source(const ControlSource &); const ControlSource &get_source() const { return src; } + void set_activator(BinaryControl *); + BinaryControl *get_activator() const { return activator; } protected: virtual void on_press() = 0; virtual void on_release() = 0; @@ -72,6 +82,7 @@ private: void button_press(unsigned); void button_release(unsigned); void axis_motion(unsigned, float, float); + void deactivate(); Control(const Control &); Control &operator=(const Control &); -- 2.45.2