More work on the Variant type. Also modified material editor to use it.

--HG--
branch : gsoc2013-dfighter
hg/feature/gsoc2013-dfighter
dfighter1985 12 years ago
parent a113c81db3
commit 192e9c058a

@ -44,7 +44,7 @@ namespace NL3D
std::string label;
uint8 type;
NLMISC::Variant value;
NLMISC::CVariant value;
void serial( NLMISC::IStream &f );
};

@ -0,0 +1,109 @@
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef NLVARIANT_H
#define NLVARIANT_H
#include "nel/misc/stream.h"
#include <string>
#define VARIANT_VVAL_END 16
namespace NLMISC
{
class CVariant : public IStreamable
{
public:
enum EVarType
{
Float,
Double,
Int,
UInt,
String,
Vector4,
Matrix4
};
CVariant();
~CVariant();
std::string getClassName(){ return "CVariant"; }
void serial( IStream &f );
void setDouble( double d ){
type = Double;
uvalue.dval = d;
}
void setFloat( float f ){
type = Float;
uvalue.fval = f;
}
void setInt( int i ){
type = Int;
uvalue.ival = i;
}
void setUInt( unsigned int u ){
type = UInt;
uvalue.uval = u;
}
void setString( const std::string &s ){
type = String;
sval = s;
}
void setVector4( const float *v );
void setMatrix4( const float *m );
double toDouble() const{ return uvalue.dval; }
float toFloat() const{ return uvalue.fval; }
int toInt() const{ return uvalue.ival; }
unsigned int toUInt() const{ return uvalue.uval; }
std::string toString() const{ return sval; }
void getVector4( float *v ) const;
void getMatrix4( float *m ) const;
bool valueAsString( std::string &s ) const;
void fromString( const std::string &s, EVarType t );
EVarType getType() const{ return type; }
private:
union{
double dval;
float fval;
int ival;
unsigned int uval;
float vval[ VARIANT_VVAL_END ];
}uvalue;
std::string sval;
EVarType type;
};
}
#endif

@ -0,0 +1,320 @@
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "nel/misc/variant.h"
#include <sstream>
namespace NLMISC
{
CVariant::CVariant()
{
std::fill( uvalue.vval, uvalue.vval + VARIANT_VVAL_END, 0.0f );
type = String;
}
CVariant::~CVariant()
{
}
void CVariant::serial( IStream &f )
{
uint v = f.serialVersion( 1 );
f.xmlPush( "type" );
if( !f.isReading() )
{
uint t = type;
f.serial( t );
}
else
{
uint t;
f.serial( t );
type = EVarType( t );
}
f.xmlPop();
f.xmlPush( "value" );
switch( type )
{
case Double:
{
f.serial( uvalue.dval );
break;
}
case Float:
{
f.serial( uvalue.fval );
break;
}
case Int:
{
f.serial( uvalue.ival );
break;
}
case UInt:
{
f.serial( uvalue.uval );
break;
}
case String:
{
f.serial( sval );
break;
}
}
if( !f.isReading() )
{
switch( type )
{
case Vector4:
{
float fval;
for( int i = 0; i < 4; i++ )
{
fval = uvalue.vval[ i ];
f.serial( fval );
}
break;
}
case Matrix4:
{
float fval;
for( int i = 0; i < 16; i++ )
{
fval = uvalue.vval[ i ];
f.serial( fval );
}
break;
}
}
}
else
{
switch( type )
{
case Vector4:
{
float fval;
for( int i = 0; i < 4; i++ )
{
f.serial( fval );
uvalue.vval[ i ] = fval;
}
break;
}
case Matrix4:
{
float fval;
for( int i = 0; i < 16; i++ )
{
f.serial( fval );
uvalue.vval[ i ] = fval;
}
break;
}
}
}
f.xmlPop();
}
void CVariant::setVector4( const float *v )
{
for( int i = 0; i < 4; i++ )
uvalue.vval[ i ] = v[ i ];
type = Vector4;
}
void CVariant::setMatrix4( const float *m )
{
for( int i = 0; i < 16; i++ )
uvalue.vval[ i ] = m[ i ];
type = Matrix4;
}
void CVariant::getVector4( float *v ) const
{
for( int i = 0; i < 4; i++ )
v[ i ] = uvalue.vval[ i ];
}
void CVariant::getMatrix4( float *m ) const
{
for( int i = 0; i < 16; i++ )
m[ i ] = uvalue.vval[ i ];
}
bool CVariant::valueAsString( std::string &s ) const
{
std::stringstream ss;
switch( type )
{
case Double:
{
ss << uvalue.dval;
s = ss.str();
break;
}
case Float:
{
ss << uvalue.fval;
s = ss.str();
break;
}
case Int:
{
ss << uvalue.ival;
s = ss.str();
break;
}
case UInt:
{
ss << uvalue.uval;
s = ss.str();
break;
}
case String:
{
s = sval;
break;
}
case Vector4:
{
for( int i = 0; i < 4; i++ )
ss << uvalue.vval[ i ] << " ";
s = ss.str();
s.resize( s.size() - 1 );
break;
}
case Matrix4:
{
for( int i = 0; i < 16; i++ )
ss << uvalue.vval[ i ] << " ";
s = ss.str();
s.resize( s.size() - 1 );
break;
}
default:
{
return false;
break;
}
}
return true;
}
void CVariant::fromString( const std::string &s, EVarType t )
{
if( s.empty() )
return;
switch( t )
{
case Double:
{
uvalue.dval = strtod( s.c_str(), NULL );
break;
}
case Float:
{
uvalue.fval = strtod( s.c_str(), NULL );
break;
}
case Int:
{
uvalue.ival = strtod( s.c_str(), NULL );
break;
}
case UInt:
{
uvalue.uval = strtod( s.c_str(), NULL );
break;
}
case String:
{
sval = s;
break;
}
case Vector4:
{
std::fill( uvalue.vval, uvalue.vval + VARIANT_VVAL_END, 0.0 );
std::stringstream ss = s;
for( int i = 0; i < 4; i++ )
{
ss >> uvalue.vval[ i ];
if( !ss.good() )
break;
}
break;
}
case Matrix4:
{
std::fill( uvalue.vval, uvalue.vval + VARIANT_VVAL_END, 0.0 );
std::stringstream ss = s;
for( int i = 0; i < 4; i++ )
{
ss >> uvalue.vval[ i ];
if( !ss.good() )
break;
}
break;
}
}
type = t;
}
}

@ -47,6 +47,33 @@ namespace MaterialEditor
"Texture"
};
const NLMISC::CVariant::EVarType evartypes[] =
{
NLMISC::CVariant::Vector4,
NLMISC::CVariant::Vector4,
NLMISC::CVariant::Float,
NLMISC::CVariant::Double,
NLMISC::CVariant::Int,
NLMISC::CVariant::UInt,
NLMISC::CVariant::Matrix4,
NLMISC::CVariant::String
};
NLMISC::CVariant::EVarType toEVarType( uint8 t )
{
NLMISC::CVariant::EVarType rt = NLMISC::CVariant::Float;
unsigned long s = sizeof( evartypes ) / sizeof( NLMISC::CVariant::EVarType );
if( t >= s )
return rt;
else
rt = evartypes[ t ];
return rt;
}
std::string SMatProp::typeIdToString( unsigned char id )
{
if( id >= EType_count )
@ -74,7 +101,7 @@ namespace MaterialEditor
prop.id = p.prop;
prop.label = p.label;
prop.type = p.type;
prop.value = p.value;
p.value.valueAsString( prop.value );
v.push_back( prop );
}
@ -91,7 +118,7 @@ namespace MaterialEditor
p.prop = itr->id;
p.label = itr->label;
p.type = itr->type;
p.value = itr->value;
p.value.fromString( itr->value, toEVarType( p.type ) );
pass->addProperty( p );
@ -137,7 +164,7 @@ namespace MaterialEditor
p.id = prop->prop;
p.label = prop->label;
p.type = prop->type;
p.value = prop->value;
prop->value.valueAsString( p.value );
return true;
}
@ -148,7 +175,7 @@ namespace MaterialEditor
prop.prop = p.id;
prop.label = p.label;
prop.type = p.type;
prop.value = p.value;
prop.value.fromString( p.value, toEVarType( prop.type ) );
return pass->changeProperty( prop.prop, prop );
}

Loading…
Cancel
Save