]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/nasal-props.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / Scripting / nasal-props.cxx
index 0b97e42add4dbf74ecb8fc561659cabb88ec10be..214cc5c9a4065e0add761112fbb15689dbfb8df9 100644 (file)
@@ -3,6 +3,9 @@
 #  include "config.h"
 #endif
 
+#include <cstring>
+
+#include <simgear/math/SGMath.hxx>
 #include <simgear/nasal/nasal.h>
 #include <simgear/props/props.hxx>
 
@@ -10,6 +13,8 @@
 
 #include "NasalSys.hxx"
 
+using namespace std;
+
 // Implementation of a Nasal wrapper for the SGPropertyNode class,
 // using the Nasal "ghost" (er... Garbage collection Handle for
 // OutSide Thingy) facility.
@@ -58,18 +63,22 @@ naRef FGNasalSys::propNodeGhost(SGPropertyNode* handle)
 
 static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
 {
+    using namespace simgear;
     NODEARG();
     const char* t = "unknown";
     switch((*node)->getType()) {
-    case SGPropertyNode::NONE:   t = "NONE";   break;
-    case SGPropertyNode::ALIAS:  t = "ALIAS";  break;
-    case SGPropertyNode::BOOL:   t = "BOOL";   break;
-    case SGPropertyNode::INT:    t = "INT";    break;
-    case SGPropertyNode::LONG:   t = "LONG";   break;
-    case SGPropertyNode::FLOAT:  t = "FLOAT";  break;
-    case SGPropertyNode::DOUBLE: t = "DOUBLE"; break;
-    case SGPropertyNode::STRING: t = "STRING"; break;
-    case SGPropertyNode::UNSPECIFIED: t = "UNSPECIFIED"; break;
+    case props::NONE:   t = "NONE";   break;
+    case props::ALIAS:  t = "ALIAS";  break;
+    case props::BOOL:   t = "BOOL";   break;
+    case props::INT:    t = "INT";    break;
+    case props::LONG:   t = "LONG";   break;
+    case props::FLOAT:  t = "FLOAT";  break;
+    case props::DOUBLE: t = "DOUBLE"; break;
+    case props::STRING: t = "STRING"; break;
+    case props::UNSPECIFIED: t = "UNSPECIFIED"; break;
+    case props::VEC3D:  t = "VEC3D";  break;
+    case props::VEC4D:  t = "VEC4D";  break;
+    case props::EXTENDED: t = "EXTENDED";  break; // shouldn't happen
     }
     return NASTR(t);
 }
@@ -140,33 +149,90 @@ static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
     return naNum((*node)->getIndex());
 }
 
+template<typename T>
+naRef makeVectorFromVec(naContext c, const T& vec)
+{
+    const int num_components
+        = sizeof(vec.data()) / sizeof(typename T::value_type);
+    naRef vector = naNewVector(c);
+    naVec_setsize(vector, num_components);
+    for (int i = 0; i < num_components; ++i)
+        naVec_set(vector, i, naNum(vec[i]));
+    return vector;
+}
+
 static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
 {
+    using namespace simgear;
     NODEARG();
     switch((*node)->getType()) {
-    case SGPropertyNode::BOOL:   case SGPropertyNode::INT:
-    case SGPropertyNode::LONG:   case SGPropertyNode::FLOAT:
-    case SGPropertyNode::DOUBLE:
-        return naNum((*node)->getDoubleValue());
-    case SGPropertyNode::STRING:
-    case SGPropertyNode::UNSPECIFIED:
+    case props::BOOL:   case props::INT:
+    case props::LONG:   case props::FLOAT:
+    case props::DOUBLE:
+    {
+        double dv = (*node)->getDoubleValue();
+        if (osg::isNaN(dv)) {
+          SG_LOG(SG_GENERAL, SG_ALERT, "Nasal getValue: property " << (*node)->getPath() << " is NaN");
+          return naNil();
+        }
+        
+        return naNum(dv);
+    }
+    
+    case props::STRING:
+    case props::UNSPECIFIED:
         return NASTR((*node)->getStringValue());
+    case props::VEC3D:
+        return makeVectorFromVec(c, (*node)->getValue<SGVec3d>());
+    case props::VEC4D:
+        return makeVectorFromVec(c, (*node)->getValue<SGVec4d>());
     default:
         return naNil();
     }
 }
 
+template<typename T>
+T makeVecFromVector(naRef vector)
+{
+    T vec;
+    const int num_components
+        = sizeof(vec.data()) / sizeof(typename T::value_type);
+    int size = naVec_size(vector);
+
+    for (int i = 0; i < num_components && i < size; ++i) {
+        naRef element = naVec_get(vector, i);
+        naRef n = naNumValue(element);
+        if (!naIsNil(n))
+            vec[i] = n.num;
+    }
+    return vec;
+}
+
 static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
 {
     NODEARG();
     naRef val = naVec_get(argv, 0);
     bool result = false;
-    if(naIsString(val)) result = (*node)->setStringValue(naStr_data(val));
-    else {
+    if(naIsString(val)) {
+        result = (*node)->setStringValue(naStr_data(val));
+    } else if(naIsVector(val)) {
+        if(naVec_size(val) == 3)
+            result = (*node)->setValue(makeVecFromVector<SGVec3d>(val));
+        else if(naVec_size(val) == 4)
+            result = (*node)->setValue(makeVecFromVector<SGVec4d>(val));
+        else
+            naRuntimeError(c, "props.setValue() vector value has wrong size");
+    } else {
         naRef n = naNumValue(val);
         if(naIsNil(n))
             naRuntimeError(c, "props.setValue() with non-number");
-        result = (*node)->setDoubleValue(naNumValue(val).num);
+            
+        double d = naNumValue(val).num;
+        if (osg::isNaN(d)) {
+          naRuntimeError(c, "props.setValue() passed a NaN");
+        }
+        
+        result = (*node)->setDoubleValue(d);
     }
     return naNum(result);
 }
@@ -199,8 +265,13 @@ static naRef f_setDoubleValue(naContext c, naRef me, int argc, naRef* args)
 {
     NODEARG();
     naRef r = naNumValue(naVec_get(argv, 0));
-    if(naIsNil(r))
+    if (naIsNil(r))
         naRuntimeError(c, "props.setDoubleValue() with non-number");
+        
+    if (osg::isNaN(r.num)) {
+      naRuntimeError(c, "props.setDoubleValue() passed a NaN");
+    }
+        
     return naNum((*node)->setDoubleValue(r.num));
 }