]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/NasalSys.cxx
Canvas: update for SimGear changes.
[flightgear.git] / src / Scripting / NasalSys.cxx
index f9e29b9afde8bb6d4e99402ab1e129ffb2331a49..d4c1ae0e25d9ba89594ebefda4b164b89d73ed60 100644 (file)
 #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"
@@ -42,7 +46,6 @@
 #include <Main/util.hxx>
 #include <Main/fg_props.hxx>
 
-
 using std::map;
 
 void postinitNasalGUI(naRef globals, naContext c);
@@ -74,6 +77,96 @@ void FGNasalModuleListener::valueChanged(SGPropertyNode*)
     }
 }
 
+//////////////////////////////////////////////////////////////////////////
+
+
+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
@@ -379,6 +472,24 @@ static naRef f_settimer(naContext c, naRef me, int argc, naRef* args)
     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)
@@ -406,29 +517,31 @@ static naRef f_cmdarg(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
@@ -483,6 +596,16 @@ static naRef f_resolveDataPath(naContext c, naRef me, int argc, naRef* args)
     return naStr_fromdata(naNewString(c), const_cast<char*>(pdata), strlen(pdata));
 }
 
+static naRef f_findDataDir(naContext c, naRef me, int argc, naRef* args)
+{
+    if(argc != 1 || !naIsString(args[0]))
+        naRuntimeError(c, "bad arguments to findDataDir()");
+    
+    SGPath p = globals->find_data_dir(naStr_data(args[0]));
+    const char* pdata = p.c_str();
+    return naStr_fromdata(naNewString(c), const_cast<char*>(pdata), strlen(pdata));
+}
+
 class NasalCommand : public SGCommandMgr::Command
 {
 public:
@@ -502,7 +625,7 @@ public:
     {
         _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 */);
 
@@ -599,6 +722,7 @@ static struct { const char* name; naCFunction func; } funcs[] = {
     { "logprint",  f_logprint },
     { "_fgcommand", f_fgcommand },
     { "settimer",  f_settimer },
+    { "maketimer", f_makeTimer },
     { "_setlistener", f_setlistener },
     { "removelistener", f_removelistener },
     { "addcommand", f_addCommand },
@@ -610,6 +734,7 @@ static struct { const char* name; naCFunction func; } funcs[] = {
     { "abort", f_abort },
     { "directory", f_directory },
     { "resolvepath", f_resolveDataPath },
+    { "finddata", f_findDataDir },
     { "parsexml", f_parsexml },
     { "systime", f_systime },
     { 0, 0 }
@@ -670,6 +795,13 @@ void FGNasalSys::init()
     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);
@@ -697,6 +829,19 @@ void FGNasalSys::init()
     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() )
@@ -710,8 +855,11 @@ void FGNasalSys::update(double)
 
     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())
@@ -756,7 +904,7 @@ void FGNasalSys::loadScriptDirectory(simgear::Dir nasalDir)
 // Create module with list of scripts
 void FGNasalSys::addModule(string moduleName, simgear::PathList scripts)
 {
-    if (scripts.size()>0)
+    if (! scripts.empty())
     {
         SGPropertyNode* nasal = globals->get_props()->getNode("nasal");
         SGPropertyNode* module_node = nasal->getChild(moduleName,0,true);
@@ -1106,7 +1254,9 @@ naRef FGNasalSys::removeListener(naContext c, int argc, naRef* args)
 
 void FGNasalSys::registerToLoad(FGNasalModelData *data)
 {
-    _loadList.push(data);
+  if( _loadList.empty() )
+    _delay_load = true;
+  _loadList.push(data);
 }
 
 void FGNasalSys::registerToUnload(FGNasalModelData *data)
@@ -1114,7 +1264,7 @@ void FGNasalSys::registerToUnload(FGNasalModelData *data)
     _unloadList.push(data);
 }
 
-
+//////////////////////////////////////////////////////////////////////////
 // FGNasalListener class.
 
 FGNasalListener::FGNasalListener(SGPropertyNode *node, naRef code,
@@ -1145,7 +1295,6 @@ FGNasalListener::~FGNasalListener()
 void FGNasalListener::call(SGPropertyNode* which, naRef mode)
 {
     if(_active || _dead) return;
-    SG_LOG(SG_NASAL, SG_DEBUG, "trigger listener #" << _id);
     _active++;
     naRef arg[4];
     arg[0] = _nas->propNodeGhost(which);