return naNil();
}
+// setlistener(func, property) extension function. Falls through to
+// FGNasalSys::setListener(). See there for docs.
+static naRef f_setlistener(naContext c, naRef me, int argc, naRef* args)
+{
+ FGNasalSys* nasal = (FGNasalSys*)globals->get_subsystem("nasal");
+ nasal->setListener(argc, args);
+ return naNil();
+}
+
// Returns a ghost handle to the argument to the currently executing
// command
static naRef f_cmdarg(naContext c, naRef me, int argc, naRef* args)
{ "print", f_print },
{ "_fgcommand", f_fgcommand },
{ "settimer", f_settimer },
+ { "_setlistener", f_setlistener },
{ "_cmdarg", f_cmdarg },
{ "_interpolate", f_interpolate },
{ "rand", f_rand },
nasal->handleTimer(this);
delete this;
}
+
+// setlistener(property, func) extension function. The first argument
+// is either a ghost (SGPropertyNode_ptr*) or a string (global property
+// path), the second is a Nasal function.
+void FGNasalSys::setListener(int argc, naRef* args)
+{
+ SGPropertyNode* node;
+ naRef prop = argc > 0 ? args[0] : naNil();
+ if(naIsString(prop)) node = fgGetNode(naStr_data(prop), true);
+ else if(naIsGhost(prop)) node = *(SGPropertyNode_ptr*)naGhost_ptr(prop);
+ else return;
+
+ naRef handler = argc > 1 ? args[1] : naNil();
+ if(!(naIsCode(handler) || naIsCCode(handler) || naIsFunc(handler)))
+ return;
+
+ node->addChangeListener(new FGNasalListener(handler, this, gcSave(handler)));
+}
+
// Implementation of the settimer extension function
void setTimer(int argc, naRef* args);
+ // Implementation of the setlistener extension function
+ void setListener(int argc, naRef* args);
+
// Returns a ghost wrapper for the current _cmdArg
naRef cmdArgGhost();
private:
friend class FGNasalScript;
+ friend class FGNasalListener;
//
// FGTimer subclass for handling Nasal timer callbacks.
FGNasalSys* _nas;
};
+class FGNasalListener : public SGPropertyChangeListener {
+public:
+ FGNasalListener(naRef handler, FGNasalSys* nasal, int gcKey)
+ : _handler(handler), _gcKey(gcKey), _nas(nasal) {}
+
+ void valueChanged(SGPropertyNode* node) {
+ _nas->_cmdArg = node;
+ naCall(_nas->_context, _handler, 0, 0, naNil(), naNil());
+ if(naGetError(_nas->_context))
+ _nas->logError();
+ }
+
+private:
+ friend class FGNasalSys;
+ naRef _handler;
+ int _gcKey;
+ FGNasalSys* _nas;
+};
+
#endif // __NASALSYS_HXX