From: James Turner Date: Sun, 10 Feb 2013 15:07:55 +0000 (+0000) Subject: Support arbitrary parameters to bindings. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=79fefc1ff92f550c40c9d5c3e7defad5a1e77537;p=simgear.git Support arbitrary parameters to bindings. Use this to supply window X/Y to hover bindings. --- diff --git a/simgear/scene/model/SGPickAnimation.cxx b/simgear/scene/model/SGPickAnimation.cxx index 34516539..f5642c6f 100644 --- a/simgear/scene/model/SGPickAnimation.cxx +++ b/simgear/scene/model/SGPickAnimation.cxx @@ -100,8 +100,10 @@ static void readOptionalBindingList(const SGPropertyNode* aNode, SGPropertyNode* return false; } - // FIXME - make x,y available to the binding - fireBindingList(_hover); + SGPropertyNode_ptr params(new SGPropertyNode); + params->setDoubleValue("x", windowPos.x()); + params->setDoubleValue("y", windowPos.y()); + fireBindingList(_hover, params.ptr()); return true; } private: @@ -439,8 +441,10 @@ public: return false; } - // FIXME - make x,y available to the binding - fireBindingList(_hover); + SGPropertyNode_ptr params(new SGPropertyNode); + params->setDoubleValue("x", windowPos.x()); + params->setDoubleValue("y", windowPos.y()); + fireBindingList(_hover, params.ptr()); return true; } private: diff --git a/simgear/structure/SGBinding.cxx b/simgear/structure/SGBinding.cxx index 43f55baf..a96307cc 100644 --- a/simgear/structure/SGBinding.cxx +++ b/simgear/structure/SGBinding.cxx @@ -15,6 +15,7 @@ #include #include "SGBinding.hxx" +#include #include SGBinding::SGBinding() @@ -64,25 +65,42 @@ SGBinding::read(const SGPropertyNode* node, SGPropertyNode* root) } void -SGBinding::fire () const +SGBinding::fire() const { if (test()) { - if (_command == 0) - _command = SGCommandMgr::instance()->getCommand(_command_name); - if (_command == 0) { - SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding"); - } else - { - try { - if (!(*_command)(_arg)) { - SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command " - << _command_name); - } - } catch (sg_exception& e) { - SG_LOG(SG_GENERAL, SG_ALERT, "command '" << _command_name << "' failed with exception\n" - << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")"); - } + innerFire(); + } +} + +void +SGBinding::innerFire () const +{ + if (_command == 0) + _command = SGCommandMgr::instance()->getCommand(_command_name); + if (_command == 0) { + SG_LOG(SG_INPUT, SG_WARN, "No command attached to binding:" << _command_name); + } else { + try { + if (!(*_command)(_arg)) { + SG_LOG(SG_INPUT, SG_ALERT, "Failed to execute command " + << _command_name); + } + } catch (sg_exception& e) { + SG_LOG(SG_GENERAL, SG_ALERT, "command '" << _command_name << "' failed with exception\n" + << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")"); + } + } +} + +void +SGBinding::fire (SGPropertyNode* params) const +{ + if (test()) { + if (params != NULL) { + copyProperties(params, _arg); } + + innerFire(); } } @@ -91,7 +109,7 @@ SGBinding::fire (double offset, double max) const { if (test()) { _arg->setDoubleValue("offset", offset/max); - fire(); + innerFire(); } } @@ -104,14 +122,14 @@ SGBinding::fire (double setting) const if (_setting == 0) // save the setting node for efficiency _setting = _arg->getChild("setting", 0, true); _setting->setDoubleValue(setting); - fire(); + innerFire(); } } -void fireBindingList(const SGBindingList& aBindings) +void fireBindingList(const SGBindingList& aBindings, SGPropertyNode* params) { BOOST_FOREACH(SGBinding_ptr b, aBindings) { - b->fire(); + b->fire(params); } } diff --git a/simgear/structure/SGBinding.hxx b/simgear/structure/SGBinding.hxx index eb12f3d2..23e921c4 100644 --- a/simgear/structure/SGBinding.hxx +++ b/simgear/structure/SGBinding.hxx @@ -114,8 +114,15 @@ public: */ void fire (double setting) const; - + /** + * Fire a binding with a number of additional parameters + * + * The children of params will be merged with the fixed arguments. + */ + void fire (SGPropertyNode* params) const; + private: + void innerFire() const; // just to be safe. SGBinding (const SGBinding &binding); @@ -134,7 +141,7 @@ typedef std::map SGBindingMap; * fire every binding in a list, in sequence */ -void fireBindingList(const SGBindingList& aBindings); +void fireBindingList(const SGBindingList& aBindings, SGPropertyNode* params = NULL); /** * fire every binding in a list with a setting value