Variant ValueAnimation::SubstractAndMultiply(const Variant& value1, const Variant& value2, float t) const { switch (valueType_) { case VAR_FLOAT: return (value1.GetFloat() - value2.GetFloat()) * t; case VAR_VECTOR2: return (value1.GetVector2() - value2.GetVector2()) * t; case VAR_VECTOR3: return (value1.GetVector3() - value2.GetVector3()) * t; case VAR_VECTOR4: return (value1.GetVector4() - value2.GetVector4()) * t; case VAR_QUATERNION: return (value1.GetQuaternion() - value2.GetQuaternion()) * t; case VAR_COLOR: return (value1.GetColor() - value2.GetColor()) * t; case VAR_DOUBLE: return (value1.GetDouble() - value2.GetDouble()) * t; default: ATOMIC_LOGERROR("Invalid value type for spline interpolation's subtract and multiply operation"); return Variant::EMPTY; } }
/// Primitive Compare Method /// bool Variant::Equal(const Variant & rhs) const { //std::cout << "variant equal: "<< *this << "==" << rhs << endl; //If they are both integers, make equality be exact. //If one is a float, make equality be with some tolerance. if(this->IsInteger() && rhs.IsInteger()) { return (this->GetInteger() == rhs.GetInteger()); } else if (this->IsNumber() && rhs.IsNumber()) { return (fabs(this->GetFloat() - rhs.GetFloat()) < .00000000001); } else if (this->IsString() && rhs.IsString()) { //If both are strings, compare them //Get copies of the strings. std::string str1 = this->GetString(); std::string str2 = rhs.GetString(); //strcmp returns 0 if they are identical, a number otherwise bool b = (str1 == str2); return b; } else if (this->IsStackSignal() && rhs.IsStackSignal()) { return (this->GetSignal() == rhs.GetSignal()); } return false; }
/// /// Primitive Comparing Method bool Variant::Less(const Variant & rhs) const { if (this->IsNumber() && rhs.IsNumber()) { return ((this->GetFloat()) < (rhs.GetFloat())); } else if (this->IsString() && rhs.IsString()) { //If both are strings, compare them //Get copies of the strings. std::string str1 = this->GetString(); std::string str2 = rhs.GetString(); return str1 < str2; } else if (this->IsFunction() && rhs.IsFunction()) { //Get copies of the strings. std::string str1 = this->GetFunctionName(); std::string str2 = rhs.GetFunctionName(); return (str1 < str2); } //This should be handled more elegantly. return false; }
///This overloads the + operator for Variants. ///for strings, this concatenates them. Variant Variant::operator +(const Variant & rhs) const { pInt i; pDouble f; if(this->IsInteger() && rhs.IsInteger()) { i = this->GetInteger() + rhs.GetInteger(); return Variant(i); } else if (this->IsNumber() && rhs.IsNumber()) { f = this->GetFloat() + rhs.GetFloat(); return Variant(f); } else if (this->IsString() || rhs.IsString()) { std::string tmp1, tmp2; tmp1 = GetString(); tmp2 = rhs.GetString(); tmp1 += tmp2; return Variant(tmp1); } else //There should be an error emmitted here i=0; return Variant(i); }
void PushVariant(duk_context* ctx, const Variant& variant) { switch (variant.GetType()) { case Urho3D::VAR_BOOL: duk_push_boolean(ctx, variant.GetBool() ? 1 : 0); break; case Urho3D::VAR_INT: duk_push_number(ctx, (duk_double_t)variant.GetInt()); break; case Urho3D::VAR_FLOAT: duk_push_number(ctx, variant.GetFloat()); break; case Urho3D::VAR_DOUBLE: duk_push_number(ctx, variant.GetFloat()); break; case Urho3D::VAR_STRING: duk_push_string(ctx, variant.GetString().CString()); break; case Urho3D::VAR_VECTOR2: PushValueObjectCopy<float2>(ctx, float2(variant.GetVector2()), float2_ID, float2_Finalizer); break; case Urho3D::VAR_VECTOR3: PushValueObjectCopy<float3>(ctx, float3(variant.GetVector3()), float3_ID, float3_Finalizer); break; case Urho3D::VAR_VECTOR4: PushValueObjectCopy<float4>(ctx, float4(variant.GetVector4()), float4_ID, float4_Finalizer); break; case Urho3D::VAR_QUATERNION: PushValueObjectCopy<Quat>(ctx, Quat(variant.GetQuaternion()), Quat_ID, Quat_Finalizer); break; default: /// \todo More types duk_push_null(ctx); break; } }
void Graphics::SetShaderParameter(StringHash param, const Variant& value) { switch (value.GetType()) { case VAR_BOOL: SetShaderParameter(param, value.GetBool()); break; case VAR_INT: SetShaderParameter(param, value.GetInt()); break; case VAR_FLOAT: case VAR_DOUBLE: SetShaderParameter(param, value.GetFloat()); break; case VAR_VECTOR2: SetShaderParameter(param, value.GetVector2()); break; case VAR_VECTOR3: SetShaderParameter(param, value.GetVector3()); break; case VAR_VECTOR4: SetShaderParameter(param, value.GetVector4()); break; case VAR_COLOR: SetShaderParameter(param, value.GetColor()); break; case VAR_MATRIX3: SetShaderParameter(param, value.GetMatrix3()); break; case VAR_MATRIX3X4: SetShaderParameter(param, value.GetMatrix3x4()); break; case VAR_MATRIX4: SetShaderParameter(param, value.GetMatrix4()); break; case VAR_BUFFER: { const PODVector<unsigned char>& buffer = value.GetBuffer(); if (buffer.Size() >= sizeof(float)) SetShaderParameter(param, reinterpret_cast<const float*>(&buffer[0]), buffer.Size() / sizeof(float)); } break; default: // Unsupported parameter type, do nothing break; } }
Variant Spline::LinearInterpolation(const Variant& lhs, const Variant& rhs, float t) const { switch (lhs.GetType()) { case VAR_FLOAT: return Lerp(lhs.GetFloat(), rhs.GetFloat(), t); case VAR_VECTOR2: return lhs.GetVector2().Lerp(rhs.GetVector2(), t); case VAR_VECTOR3: return lhs.GetVector3().Lerp(rhs.GetVector3(), t); case VAR_VECTOR4: return lhs.GetVector4().Lerp(rhs.GetVector4(), t); case VAR_COLOR: return lhs.GetColor().Lerp(rhs.GetColor(), t); default: return Variant::EMPTY; } }
/// /// Assignment Operator (overloaded) Variant Variant::operator = (const Variant & value) { //First, clean up 'this' if it contains data on free store // (e.g., a Variable or a string) free_mData(); mDataType=value.GetDataType(); switch(mDataType) { case P_DATA_NUMBER_INTEGER: // an integer mData.iNumber = value.GetInteger(); break; case P_DATA_NUMBER_FLOAT: // a float mData.fNumber = value.GetFloat(); break; case P_DATA_STRING: mData.String = strdup(value.GetString().c_str()); break; case P_DATA_LOCALVARIABLE: // a char* variable name case P_DATA_GLOBALVARIABLE: // a char* variable name mData.Variable = strdup(value.GetVariableName().c_str()); break; case P_DATA_FUNCTION: mData.Function = strdup(value.GetFunctionName().c_str()); break; case P_DATA_FUNCTION_POINTER: mData.pFunction = value.GetFunctionPointer(); break; case P_DATA_STACK_SIGNAL: mData.Signal = value.GetSignal(); break; case P_DATA_COMPLEXDATA: { PComplexData * tmp = value.GetComplexData(); //tmp is a pointer to complex data, which //is just a holder for the object. We want to make a //copy of tmp mComplexData = new PComplexData(*tmp); break; } case P_DATA_UNDEFINED: // undefined, error default: PError::SignalFatalError( "Undefined Variant type in Variant::operator = (Variant)."); break; } return (*this); }
///This overloads the / operator for Variants Variant Variant::operator /(const Variant & rhs) const { pInt i; pDouble f; if(this->IsNumber() && rhs.IsNumber()) { f = this->GetFloat() / rhs.GetFloat(); return Variant(f); } else i=0; return Variant(i); }
void LuaScriptInstance::OnSetAttribute(const AttributeInfo& attr, const Variant& src) { if (attr.ptr_ != (void*)0xffffffff) { Serializable::OnSetAttribute(attr, src); return; } if (scriptObjectRef_ == LUA_REFNIL) return; String name = attr.name_; unsigned length = name.Length(); if (name.Back() == '_') length -= 1; int top = lua_gettop(luaState_); String functionName = String("Set") + name.Substring(0, 1).ToUpper() + name.Substring(1, length - 1); WeakPtr<LuaFunction> function = GetScriptObjectFunction(functionName); // If set function exist if (function) { if (function->BeginCall(this)) { function->PushVariant(src); function->EndCall(); } } else { lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_); lua_pushstring(luaState_, name.CString()); switch (attr.type_) { case VAR_BOOL: lua_pushboolean(luaState_, src.GetBool()); break; case VAR_FLOAT: lua_pushnumber(luaState_, src.GetFloat()); break; case VAR_STRING: tolua_pushurho3dstring(luaState_, src.GetString()); break; case VAR_VECTOR2: { Vector2* value = new Vector2(src.GetVector2()); tolua_pushusertype(luaState_, value, "Vector2"); tolua_register_gc(luaState_, lua_gettop(luaState_)); } break; case VAR_VECTOR3: { Vector3* value = new Vector3(src.GetVector3()); tolua_pushusertype(luaState_, value, "Vector3"); tolua_register_gc(luaState_, lua_gettop(luaState_)); } break; case VAR_VECTOR4: { Vector4* value = new Vector4(src.GetVector4()); tolua_pushusertype(luaState_, value, "Vector4"); tolua_register_gc(luaState_, lua_gettop(luaState_)); } break; case VAR_QUATERNION: { Quaternion* value = new Quaternion(src.GetQuaternion()); tolua_pushusertype(luaState_, value, "Quaternion"); tolua_register_gc(luaState_, lua_gettop(luaState_)); } break; case VAR_COLOR: { Color* value = new Color(src.GetColor()); tolua_pushusertype(luaState_, value, "Color"); tolua_register_gc(luaState_, lua_gettop(luaState_)); } break; case VAR_INTRECT: { IntRect* value = new IntRect(src.GetIntRect()); tolua_pushusertype(luaState_, value, "IntRect"); tolua_register_gc(luaState_, lua_gettop(luaState_)); } break; case VAR_INTVECTOR2: { IntVector2* value = new IntVector2(src.GetIntVector2()); tolua_pushusertype(luaState_, value, "IntVector2"); tolua_register_gc(luaState_, lua_gettop(luaState_)); } break; default: LOGERROR("Unsupported data type"); lua_settop(luaState_, top); return; } lua_settable(luaState_, -3); } lua_settop(luaState_, top); }
bool Serializer::WriteVariantData(const Variant& value) { switch (value.GetType()) { case VAR_NONE: return true; case VAR_INT: return WriteInt(value.GetInt()); case VAR_BOOL: return WriteBool(value.GetBool()); case VAR_FLOAT: return WriteFloat(value.GetFloat()); case VAR_VECTOR2: return WriteVector2(value.GetVector2()); case VAR_VECTOR3: return WriteVector3(value.GetVector3()); case VAR_VECTOR4: return WriteVector4(value.GetVector4()); case VAR_QUATERNION: return WriteQuaternion(value.GetQuaternion()); case VAR_COLOR: return WriteColor(value.GetColor()); case VAR_STRING: return WriteString(value.GetString()); case VAR_BUFFER: return WriteBuffer(value.GetBuffer()); // Serializing pointers is not supported. Write null case VAR_VOIDPTR: case VAR_PTR: return WriteUInt(0); case VAR_RESOURCEREF: return WriteResourceRef(value.GetResourceRef()); case VAR_RESOURCEREFLIST: return WriteResourceRefList(value.GetResourceRefList()); case VAR_VARIANTVECTOR: return WriteVariantVector(value.GetVariantVector()); case VAR_VARIANTMAP: return WriteVariantMap(value.GetVariantMap()); case VAR_INTRECT: return WriteIntRect(value.GetIntRect()); case VAR_INTVECTOR2: return WriteIntVector2(value.GetIntVector2()); case VAR_MATRIX3: return WriteMatrix3(value.GetMatrix3()); case VAR_MATRIX3X4: return WriteMatrix3x4(value.GetMatrix3x4()); case VAR_MATRIX4: return WriteMatrix4(value.GetMatrix4()); default: return false; } }
Variant::Variant(const Variant &v): mComplexData(NULL) { //This should behave differently depending on what type of variant v is mDataType = v.GetDataType(); switch(mDataType) { case P_DATA_NUMBER_INTEGER: // an integer mData.iNumber = v.GetInteger(); break; case P_DATA_NUMBER_FLOAT: // a float mData.fNumber = v.GetFloat(); break; case P_DATA_STRING: // cout << "testing case string:<" << v << ">"<<endl; mData.String = strdup(v.GetString().c_str()); break; case P_DATA_LOCALVARIABLE: // a variable name case P_DATA_GLOBALVARIABLE: // a variable name mData.Variable = strdup(v.GetVariableName().c_str()); break; case P_DATA_FUNCTION: //Memory problem here, diagnosed by efence: mData.Function = strdup(v.GetFunctionName().c_str()); break; case P_DATA_FUNCTION_POINTER: mData.pFunction = v.GetFunctionPointer(); break; case P_DATA_STACK_SIGNAL: mData.Signal = v.GetSignal(); break; //This needs to make a deep copy case P_DATA_COMPLEXDATA: { //cout<<"TYPE:" << v << endl; PComplexData * pcd = v.GetComplexData(); if(pcd) mComplexData = new PComplexData(*pcd); else mComplexData = NULL; } break; case P_DATA_UNDEFINED: // undefined, not an error break; default: cerr << "Undefined Data Type in Variant copy constructor. Type: " << mDataType << endl; cerr << v << endl; PError::SignalFatalError("Undefined Data Type."); break; } }
void JSONValue::SetVariantValue(const Variant& variant, Context* context) { if (!IsNull()) { LOGWARNING("JsonValue is not null"); } switch (variant.GetType()) { case VAR_BOOL: *this = variant.GetBool(); return; case VAR_INT: *this = variant.GetInt(); return; case VAR_FLOAT: *this = variant.GetFloat(); return; case VAR_DOUBLE: *this = variant.GetDouble(); return; case VAR_STRING: *this = variant.GetString(); return; case VAR_VARIANTVECTOR: SetVariantVector(variant.GetVariantVector(), context); return; case VAR_VARIANTMAP: SetVariantMap(variant.GetVariantMap(), context); return; case VAR_RESOURCEREF: { if (!context) { LOGERROR("Context must not null for ResourceRef"); return; } const ResourceRef& ref = variant.GetResourceRef(); *this = String(context->GetTypeName(ref.type_)) + ";" + ref.name_; } return; case VAR_RESOURCEREFLIST: { if (!context) { LOGERROR("Context must not null for ResourceRefList"); return; } const ResourceRefList& refList = variant.GetResourceRefList(); String str(context->GetTypeName(refList.type_)); for (unsigned i = 0; i < refList.names_.Size(); ++i) { str += ";"; str += refList.names_[i]; } *this = str; } return; case VAR_STRINGVECTOR: { const StringVector& vector = variant.GetStringVector(); Resize(vector.Size()); for (unsigned i = 0; i < vector.Size(); ++i) (*this)[i] = vector[i]; } return; default: *this = variant.ToString(); } }