#include <simgear/math/sg_random.h>
#include <simgear/misc/sg_path.hxx>
#include <simgear/misc/sg_dir.hxx>
-#include <simgear/misc/interpolator.hxx>
#include <simgear/structure/commands.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/structure/event_mgr.hxx>
#include <simgear/debug/BufferedLogCallback.hxx>
+#include <simgear/nasal/cppbind/from_nasal.hxx>
+#include <simgear/nasal/cppbind/to_nasal.hxx>
+#include <simgear/nasal/cppbind/Ghost.hxx>
+#include <simgear/nasal/cppbind/NasalHash.hxx>
+
#include "NasalSys.hxx"
#include "NasalSys_private.hxx"
#include "NasalModelData.hxx"
#include <Main/util.hxx>
#include <Main/fg_props.hxx>
-
using std::map;
void postinitNasalGUI(naRef globals, naContext c);
}
}
+//////////////////////////////////////////////////////////////////////////
+
+
+class TimerObj : public SGReferenced
+{
+public:
+ TimerObj(FGNasalSys* sys, naRef f, naRef self, double interval) :
+ _sys(sys),
+ _func(f),
+ _self(self),
+ _isRunning(false),
+ _interval(interval),
+ _singleShot(false)
+ {
+ char nm[128];
+ snprintf(nm, 128, "nasal-timer-%p", this);
+ _name = nm;
+ _gcRoot = sys->gcSave(f);
+ _gcSelf = sys->gcSave(self);
+ }
+
+ virtual ~TimerObj()
+ {
+ stop();
+ _sys->gcRelease(_gcRoot);
+ _sys->gcRelease(_gcSelf);
+ }
+
+ bool isRunning() const { return _isRunning; }
+
+ void stop()
+ {
+ if (_isRunning) {
+ globals->get_event_mgr()->removeTask(_name);
+ _isRunning = false;
+ }
+ }
+
+ void start()
+ {
+ if (_isRunning) {
+ return;
+ }
+
+ _isRunning = true;
+ if (_singleShot) {
+ globals->get_event_mgr()->addEvent(_name, this, &TimerObj::invoke, _interval);
+ } else {
+ globals->get_event_mgr()->addTask(_name, this, &TimerObj::invoke, _interval);
+ }
+ }
+
+ // stop and then start -
+ void restart(double newInterval)
+ {
+ _interval = newInterval;
+ stop();
+ start();
+ }
+
+ void invoke()
+ {
+ naRef *args = NULL;
+ _sys->callMethod(_func, _self, 0, args, naNil() /* locals */);
+ if (_singleShot) {
+ _isRunning = false;
+ }
+ }
+
+ void setSingleShot(bool aSingleShot)
+ {
+ _singleShot = aSingleShot;
+ }
+
+ bool isSingleShot() const
+ { return _singleShot; }
+private:
+ std::string _name;
+ FGNasalSys* _sys;
+ naRef _func, _self;
+ int _gcRoot, _gcSelf;
+ bool _isRunning;
+ double _interval;
+ bool _singleShot;
+};
+
+typedef SGSharedPtr<TimerObj> TimerObjRef;
+typedef nasal::Ghost<TimerObjRef> NasalTimerObj;
+
+///////////////////////////////////////////////////////////////////////////
// Read and return file contents in a single buffer. Note use of
// stat() to get the file size. This is a win32 function, believe it
return naNil();
}
+static naRef f_makeTimer(naContext c, naRef me, int argc, naRef* args)
+{
+ if (!naIsNum(args[0])) {
+ naRuntimeError(c, "bad interval argument to maketimer");
+ }
+
+ naRef func, self = naNil();
+ if (naIsFunc(args[1])) {
+ func = args[1];
+ } else if ((argc == 3) && naIsFunc(args[2])) {
+ self = args[1];
+ func = args[2];
+ }
+
+ TimerObj* timerObj = new TimerObj(nasalSys, func, self, args[0].num);
+ return NasalTimerObj::create(c, timerObj);
+}
+
// setlistener(func, property, bool) extension function. Falls through to
// FGNasalSys::setListener(). See there for docs.
static naRef f_setlistener(naContext c, naRef me, int argc, naRef* args)
// value/delta numbers.
static naRef f_interpolate(naContext c, naRef me, 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 naNil();
-
- naRef curve = argc > 1 ? args[1] : naNil();
- if(!naIsVector(curve)) return naNil();
- int nPoints = naVec_size(curve) / 2;
- double* values = new double[nPoints];
- double* deltas = new double[nPoints];
- for(int i=0; i<nPoints; i++) {
- values[i] = naNumValue(naVec_get(curve, 2*i)).num;
- deltas[i] = naNumValue(naVec_get(curve, 2*i+1)).num;
- }
-
- ((SGInterpolator*)globals->get_subsystem_mgr()
- ->get_group(SGSubsystemMgr::INIT)->get_subsystem("interpolator"))
- ->interpolate(node, nPoints, values, deltas);
+ 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 naNil();
+
+ naRef curve = argc > 1 ? args[1] : naNil();
+ if(!naIsVector(curve)) return naNil();
+ int nPoints = naVec_size(curve) / 2;
+
+ simgear::PropertyList value_nodes;
+ value_nodes.reserve(nPoints);
+ double_list deltas;
+ deltas.reserve(nPoints);
+
+ for( int i = 0; i < nPoints; ++i )
+ {
+ SGPropertyNode* val = new SGPropertyNode;
+ val->setDoubleValue(naNumValue(naVec_get(curve, 2*i)).num);
+ value_nodes.push_back(val);
+ deltas.push_back(naNumValue(naVec_get(curve, 2*i+1)).num);
+ }
- delete[] values;
- delete[] deltas;
- return naNil();
+ node->interpolate("numeric", value_nodes, deltas, "linear");
+ return naNil();
}
// This is a better RNG than the one in the default Nasal distribution
{
_sys->setCmdArg(const_cast<SGPropertyNode*>(aNode));
naRef args[1];
- args[0] = _sys->cmdArgGhost();
+ args[0] = _sys->wrappedPropsNode(const_cast<SGPropertyNode*>(aNode));
_sys->callMethod(_func, naNil(), 1, args, naNil() /* locals */);
{ "logprint", f_logprint },
{ "_fgcommand", f_fgcommand },
{ "settimer", f_settimer },
+ { "maketimer", f_makeTimer },
{ "_setlistener", f_setlistener },
{ "removelistener", f_removelistener },
{ "addcommand", f_addCommand },
initNasalString(_globals, _string, _context, _gcHash);
initNasalPositioned(_globals, _context, _gcHash);
+ initNasalPositioned_cppbind(_globals, _context, _gcHash);
NasalClipboard::init(this);
initNasalCanvas(_globals, _context, _gcHash);
initNasalCondition(_globals, _context, _gcHash);
+ NasalTimerObj::init("Timer")
+ .method("start", &TimerObj::start)
+ .method("stop", &TimerObj::stop)
+ .method("restart", &TimerObj::restart)
+ .member("singleShot", &TimerObj::isSingleShot, &TimerObj::setSingleShot)
+ .member("isRunning", &TimerObj::isRunning);
+
// Now load the various source files in the Nasal directory
simgear::Dir nasalDir(SGPath(globals->get_fg_root(), "Nasal"));
loadScriptDirectory(nasalDir);
postinitNasalGUI(_globals, _context);
}
+naRef FGNasalSys::wrappedPropsNode(SGPropertyNode* aProps)
+{
+ static naRef wrapNodeFunc = naNil();
+ if (naIsNil(wrapNodeFunc)) {
+ nasal::Hash props = getGlobals().get<nasal::Hash>("props");
+ wrapNodeFunc = props.get("wrapNode");
+ }
+
+ naRef args[1];
+ args[0] = propNodeGhost(aProps);
+ return naCall(_context, wrapNodeFunc, 1, args, naNil(), naNil());
+}
+
void FGNasalSys::update(double)
{
if( NasalClipboard::getInstance() )
if (!_loadList.empty())
{
- // process Nasal load hook (only one per update loop to avoid excessive lags)
- _loadList.pop()->load();
+ if( _delay_load )
+ _delay_load = false;
+ else
+ // process Nasal load hook (only one per update loop to avoid excessive lags)
+ _loadList.pop()->load();
}
else
if (!_unloadList.empty())
void FGNasalSys::registerToLoad(FGNasalModelData *data)
{
- _loadList.push(data);
+ if( _loadList.empty() )
+ _delay_load = true;
+ _loadList.push(data);
}
void FGNasalSys::registerToUnload(FGNasalModelData *data)
_unloadList.push(data);
}
-
+//////////////////////////////////////////////////////////////////////////
// FGNasalListener class.
FGNasalListener::FGNasalListener(SGPropertyNode *node, naRef code,