1
0
Fork 0

Support for VEC3D and VEC4D properties.

Tiny update to property_list for extended properties.

New property types in nasal.
This commit is contained in:
timoore 2009-07-15 23:15:57 +00:00 committed by Tim Moore
parent 4cc3084256
commit 5cf18d30c6
2 changed files with 57 additions and 6 deletions

View file

@ -64,6 +64,10 @@ static string getValueTypeString(const SGPropertyNode *node)
result = "double"; result = "double";
else if (type == STRING) else if (type == STRING)
result = "string"; result = "string";
else if (type == VEC3D)
result = "vec3d";
else if (type == VEC4D)
result = "vec4d";
return result; return result;
} }
@ -88,14 +92,18 @@ static void dumpProperties(const SGPropertyNode *node)
switch (c->getType()) { switch (c->getType()) {
case DOUBLE: case DOUBLE:
case FLOAT: case FLOAT:
cout << std::setprecision(15) << c->getDoubleValue(); case VEC3D:
case VEC4D:
{
streamsize precision = cout.precision(15);
c->printOn(cout);
cout.precision(precision);
}
break; break;
case LONG: case LONG:
case INT: case INT:
cout << c->getLongValue();
break;
case BOOL: case BOOL:
cout << (c->getBoolValue() ? "true" : "false"); c->printOn(cout);
break; break;
case STRING: case STRING:
cout << '"' << c->getStringValue() << '"'; cout << '"' << c->getStringValue() << '"';

View file

@ -71,6 +71,8 @@ static naRef f_getType(naContext c, naRef me, int argc, naRef* args)
case DOUBLE: t = "DOUBLE"; break; case DOUBLE: t = "DOUBLE"; break;
case STRING: t = "STRING"; break; case STRING: t = "STRING"; break;
case UNSPECIFIED: t = "UNSPECIFIED"; break; case UNSPECIFIED: t = "UNSPECIFIED"; break;
case VEC3D: t = "VEC3D"; break;
case VEC4D: t = "VEC4D"; break;
} }
return NASTR(t); return NASTR(t);
} }
@ -141,6 +143,18 @@ static naRef f_getIndex(naContext c, naRef me, int argc, naRef* args)
return naNum((*node)->getIndex()); return naNum((*node)->getIndex());
} }
template<typename T>
naRef makeVectorFromVec(naContext c, const T& vec)
{
const unsigned 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) static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
{ {
using namespace simgear::props; using namespace simgear::props;
@ -153,18 +167,47 @@ static naRef f_getValue(naContext c, naRef me, int argc, naRef* args)
case STRING: case STRING:
case UNSPECIFIED: case UNSPECIFIED:
return NASTR((*node)->getStringValue()); return NASTR((*node)->getStringValue());
case VEC3D:
return makeVectorFromVec(c, (*node)->getValue<SGVec3d>());
case VEC4D:
return makeVectorFromVec(c, (*node)->getValue<SGVec4d>());
default: default:
return naNil(); return naNil();
} }
} }
template<typename T>
T makeVecFromVector(naRef vector)
{
T vec;
const unsigned 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) static naRef f_setValue(naContext c, naRef me, int argc, naRef* args)
{ {
NODEARG(); NODEARG();
naRef val = naVec_get(argv, 0); naRef val = naVec_get(argv, 0);
bool result = false; bool result = false;
if(naIsString(val)) result = (*node)->setStringValue(naStr_data(val)); if(naIsString(val)) {
else { 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); naRef n = naNumValue(val);
if(naIsNil(n)) if(naIsNil(n))
naRuntimeError(c, "props.setValue() with non-number"); naRuntimeError(c, "props.setValue() with non-number");