X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Fcontrol.cpp;h=b2f9badd45b673026b90645bca2fcfef104cb648;hb=207eec91afc6b538753ddc44905ac9ddbc907ac0;hp=0519d8fe42117a1f7304a3daa30fbefbce787f6d;hpb=b453da0696b24cc02a7fcf5127afcd7baef9c801;p=libs%2Fgui.git diff --git a/source/input/control.cpp b/source/input/control.cpp index 0519d8f..b2f9bad 100644 --- a/source/input/control.cpp +++ b/source/input/control.cpp @@ -1,15 +1,11 @@ -/* $Id$ - -This file is part of libmspgbase -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include +#include #include +#include "binarycontrol.h" #include "control.h" #include "device.h" +using namespace std; + namespace Msp { namespace Input { @@ -29,61 +25,54 @@ std::string ControlSource::str() const { if(type==BUTTON) return dev->get_button_name(index); - else if(type==AXIS_POS || type==AXIS_NEG) - return dev->get_axis_name(index); + else if(type==AXIS_POS) + return dev->get_axis_name(index)+" +"; + else if(type==AXIS_NEG) + return dev->get_axis_name(index)+" -"; else if(type==NONE) return "None"; - return lexical_cast(index); + return lexical_cast(index); } 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) -{ - connect_signals(); -} - -Control::Control(const Control &c): - trackable(c), - src(c.src), - capture_dev(0) + capture_dev(0), + activator(0), + origin(0) { connect_signals(); } -Control &Control::operator=(const Control &c) -{ - notify_callbacks(); - src=c.src; - capture_dev=0; - connect_signals(); - - return *this; -} +Control::~Control() +{ } void Control::capture(Device &d) { notify_callbacks(); - capture_dev=&d; - capture_dev->signal_button_press.connect(sigc::mem_fun(this, &Control::button_press)); - capture_dev->signal_axis_motion.connect(sigc::mem_fun(this, &Control::axis_motion)); + capture_dev = &d; + capture_dev->signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Control::button_press), false)); + capture_dev->signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Control::axis_motion), false)); } void Control::cancel_capture() { notify_callbacks(); - capture_dev=0; + capture_dev = 0; connect_signals(); } @@ -95,7 +84,14 @@ void Control::set_source(Device &d, ControlSrcType t, unsigned i) void Control::set_source(const ControlSource &s) { notify_callbacks(); - src=s; + src = s; + connect_signals(); +} + +void Control::set_activator(BinaryControl *ctrl) +{ + notify_callbacks(); + activator = ctrl; connect_signals(); } @@ -106,38 +102,39 @@ void Control::connect_signals() case NONE: break; case BUTTON: - src.dev->signal_button_press.connect(sigc::mem_fun(this, &Control::button_press)); - src.dev->signal_button_release.connect(sigc::mem_fun(this, &Control::button_release)); + src.dev->signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Control::button_press), false)); + src.dev->signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &Control::button_release), false)); break; case AXIS_POS: case AXIS_NEG: - src.dev->signal_axis_motion.connect(sigc::mem_fun(this, &Control::axis_motion)); + src.dev->signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Control::axis_motion), false)); break; - default: - throw Exception("Invalid source in Control"); } + + if(activator) + activator->signal_release.connect(sigc::mem_fun(this, &Control::deactivate)); } void Control::button_press(unsigned i) { if(capture_dev) { - src.dev=capture_dev; - src.type=BUTTON; - src.index=i; + src.dev = capture_dev; + src.type = BUTTON; + src.index = i; notify_callbacks(); - capture_dev=0; + capture_dev = 0; 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(); } @@ -145,28 +142,42 @@ void Control::axis_motion(unsigned i, float v, float r) { if(capture_dev) { - ControlSrcType type=NONE; - if(v<-src.dev->get_axis_threshold()) - type=AXIS_NEG; - else if(v>src.dev->get_axis_threshold()) - type=AXIS_POS; + ControlSrcType type = NONE; + if(v<-0.9) + type = AXIS_NEG; + else if(v>0.9) + type = AXIS_POS; if(type!=NONE) { - src.dev=capture_dev; - src.type=type; - src.index=i; + src.dev = capture_dev; + src.type = type; + src.index = i; notify_callbacks(); - capture_dev=0; + capture_dev = 0; connect_signals(); 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