virtualx-engine/core/variant.cpp

3122 lines
58 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* variant.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "variant.h"
2014-02-10 02:10:30 +01:00
#include "resource.h"
#include "print_string.h"
#include "scene/main/node.h"
#include "scene/gui/control.h"
#include "io/marshalls.h"
#include "core_string_names.h"
#include "variant_parser.h"
2014-02-10 02:10:30 +01:00
String Variant::get_type_name(Variant::Type p_type) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
switch( p_type ) {
case NIL: {
return "Nil";
} break;
// atomic types
case BOOL: {
return "bool";
} break;
case INT: {
return "int";
} break;
case REAL: {
return "float";
2014-02-10 02:10:30 +01:00
} break;
case STRING: {
return "String";
} break;
// math types
case VECTOR2: {
return "Vector2";
} break;
case RECT2: {
return "Rect2";
} break;
case TRANSFORM2D: {
2014-02-10 02:10:30 +01:00
return "Transform2D";
2014-02-10 02:10:30 +01:00
} break;
case VECTOR3: {
return "Vector3";
} break;
case PLANE: {
return "Plane";
} break;
/*
case QUAT: {
} break;*/
case RECT3: {
2014-02-10 02:10:30 +01:00
return "Rect3";
2014-02-10 02:10:30 +01:00
} break;
case QUAT: {
return "Quat";
} break;
case BASIS: {
2014-02-10 02:10:30 +01:00
return "Basis";
2014-02-10 02:10:30 +01:00
} break;
case TRANSFORM: {
return "Transform";
} break;
// misc types
case COLOR: {
return "Color";
} break;
case IMAGE: {
return "Image";
} break;
case _RID: {
return "RID";
} break;
case OBJECT: {
return "Object";
} break;
case NODE_PATH: {
return "NodePath";
} break;
case INPUT_EVENT: {
return "InputEvent";
} break;
case DICTIONARY: {
return "Dictionary";
} break;
case ARRAY: {
return "Array";
} break;
// arrays
case POOL_BYTE_ARRAY: {
2014-02-10 02:10:30 +01:00
return "PoolByteArray";
2014-02-10 02:10:30 +01:00
} break;
case POOL_INT_ARRAY: {
2014-02-10 02:10:30 +01:00
return "PoolIntArray";
2014-02-10 02:10:30 +01:00
} break;
case POOL_REAL_ARRAY: {
2014-02-10 02:10:30 +01:00
return "PoolFloatArray";
2014-02-10 02:10:30 +01:00
} break;
case POOL_STRING_ARRAY: {
2014-02-10 02:10:30 +01:00
return "PoolStringArray";
2014-02-10 02:10:30 +01:00
} break;
case POOL_VECTOR2_ARRAY: {
2014-02-10 02:10:30 +01:00
return "PoolVector2Array";
2014-02-10 02:10:30 +01:00
} break;
case POOL_VECTOR3_ARRAY: {
2014-02-10 02:10:30 +01:00
return "PoolVector3Array";
2014-02-10 02:10:30 +01:00
} break;
case POOL_COLOR_ARRAY: {
2014-02-10 02:10:30 +01:00
return "PoolColorArray";
2014-02-10 02:10:30 +01:00
} break;
default: {}
}
return "";
}
bool Variant::can_convert(Variant::Type p_type_from,Variant::Type p_type_to) {
if (p_type_from==p_type_to)
return true;
if (p_type_to==NIL && p_type_from!=NIL) //nil can convert to anything
return true;
if (p_type_from == NIL) {
return (p_type_to == OBJECT);
};
const Type *valid_types=NULL;
const Type *invalid_types=NULL;
switch(p_type_to) {
case BOOL: {
static const Type valid[]={
INT,
REAL,
STRING,
2014-02-10 02:10:30 +01:00
NIL,
};
valid_types=valid;
} break;
case INT: {
static const Type valid[]={
BOOL,
REAL,
STRING,
2014-02-10 02:10:30 +01:00
NIL,
};
valid_types=valid;
} break;
case REAL: {
static const Type valid[]={
BOOL,
INT,
STRING,
2014-02-10 02:10:30 +01:00
NIL,
};
valid_types=valid;
} break;
case STRING: {
static const Type invalid[]={
OBJECT,
IMAGE,
NIL
};
invalid_types=invalid;
} break;
case TRANSFORM2D: {
2014-02-10 02:10:30 +01:00
static const Type valid[]={
2014-02-10 02:10:30 +01:00
TRANSFORM,
NIL
};
valid_types=valid;
2014-02-10 02:10:30 +01:00
} break;
case QUAT: {
static const Type valid[]={
BASIS,
2014-02-10 02:10:30 +01:00
NIL
};
valid_types=valid;
} break;
case BASIS: {
2014-02-10 02:10:30 +01:00
static const Type valid[]={
QUAT,
NIL
};
valid_types=valid;
} break;
case TRANSFORM: {
static const Type valid[]={
TRANSFORM2D,
2014-02-10 02:10:30 +01:00
QUAT,
BASIS,
2014-02-10 02:10:30 +01:00
NIL
};
valid_types=valid;
2016-03-09 00:00:52 +01:00
} break;
case COLOR: {
static const Type valid[] = {
STRING,
INT,
NIL,
};
valid_types = valid;
} break;
case _RID: {
static const Type valid[]={
OBJECT,
NIL
};
valid_types=valid;
} break;
case OBJECT: {
static const Type valid[]={
NIL
};
valid_types=valid;
} break;
case NODE_PATH: {
static const Type valid[]={
STRING,
NIL
};
valid_types=valid;
} break;
case ARRAY: {
static const Type valid[]={
POOL_BYTE_ARRAY,
POOL_INT_ARRAY,
POOL_STRING_ARRAY,
POOL_REAL_ARRAY,
POOL_COLOR_ARRAY,
POOL_VECTOR2_ARRAY,
POOL_VECTOR3_ARRAY,
NIL
};
valid_types=valid;
} break;
// arrays
case POOL_BYTE_ARRAY: {
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_INT_ARRAY: {
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_REAL_ARRAY: {
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_STRING_ARRAY: {
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_VECTOR2_ARRAY: {
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_VECTOR3_ARRAY: {
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_COLOR_ARRAY: {
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
default: {}
}
if (valid_types) {
int i=0;
while(valid_types[i]!=NIL) {
if (p_type_from==valid_types[i])
return true;
i++;
}
2016-10-04 12:36:31 +02:00
} else if (invalid_types) {
int i=0;
while(invalid_types[i]!=NIL) {
if (p_type_from==invalid_types[i])
return false;
i++;
}
2016-10-04 12:36:31 +02:00
return true;
}
return false;
}
bool Variant::can_convert_strict(Variant::Type p_type_from,Variant::Type p_type_to) {
if (p_type_from==p_type_to)
return true;
if (p_type_to==NIL && p_type_from!=NIL) //nil can convert to anything
return true;
if (p_type_from == NIL) {
return (p_type_to == OBJECT);
};
const Type *valid_types=NULL;
switch(p_type_to) {
case BOOL: {
static const Type valid[]={
INT,
REAL,
//STRING,
NIL,
};
valid_types=valid;
} break;
case INT: {
static const Type valid[]={
BOOL,
REAL,
//STRING,
NIL,
};
valid_types=valid;
} break;
case REAL: {
static const Type valid[]={
BOOL,
INT,
//STRING,
NIL,
};
valid_types=valid;
} break;
case STRING: {
static const Type valid[]={
NODE_PATH,
NIL
};
valid_types=valid;
} break;
case TRANSFORM2D: {
static const Type valid[]={
TRANSFORM,
NIL
};
valid_types=valid;
} break;
case QUAT: {
static const Type valid[]={
BASIS,
NIL
};
valid_types=valid;
} break;
case BASIS: {
static const Type valid[]={
QUAT,
NIL
};
valid_types=valid;
} break;
case TRANSFORM: {
static const Type valid[]={
TRANSFORM2D,
QUAT,
BASIS,
NIL
};
valid_types=valid;
} break;
case COLOR: {
static const Type valid[] = {
STRING,
INT,
NIL,
};
valid_types = valid;
} break;
2014-02-10 02:10:30 +01:00
case _RID: {
static const Type valid[]={
OBJECT,
NIL
};
valid_types=valid;
} break;
case OBJECT: {
static const Type valid[]={
NIL
};
valid_types=valid;
} break;
case NODE_PATH: {
static const Type valid[]={
STRING,
NIL
};
valid_types=valid;
} break;
case ARRAY: {
static const Type valid[]={
POOL_BYTE_ARRAY,
POOL_INT_ARRAY,
POOL_STRING_ARRAY,
POOL_REAL_ARRAY,
POOL_COLOR_ARRAY,
POOL_VECTOR2_ARRAY,
POOL_VECTOR3_ARRAY,
2014-02-10 02:10:30 +01:00
NIL
};
valid_types=valid;
} break;
// arrays
case POOL_BYTE_ARRAY: {
2014-02-10 02:10:30 +01:00
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_INT_ARRAY: {
2014-02-10 02:10:30 +01:00
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_REAL_ARRAY: {
2014-02-10 02:10:30 +01:00
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_STRING_ARRAY: {
2014-02-10 02:10:30 +01:00
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_VECTOR2_ARRAY: {
2014-02-10 02:10:30 +01:00
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_VECTOR3_ARRAY: {
2014-02-10 02:10:30 +01:00
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
case POOL_COLOR_ARRAY: {
2014-02-10 02:10:30 +01:00
static const Type valid[]={
ARRAY,
NIL
};
valid_types=valid;
} break;
default: {}
}
if (valid_types) {
int i=0;
while(valid_types[i]!=NIL) {
if (p_type_from==valid_types[i])
return true;
i++;
}
}
return false;
}
bool Variant::operator==(const Variant& p_variant) const {
if (type!=p_variant.type) //evaluation of operator== needs to be more strict
return false;
bool v;
Variant r;
evaluate(OP_EQUAL,*this,p_variant,r,v);
return r;
}
bool Variant::operator!=(const Variant& p_variant) const {
if (type!=p_variant.type) //evaluation of operator== needs to be more strict
return true;
bool v;
Variant r;
evaluate(OP_NOT_EQUAL,*this,p_variant,r,v);
return r;
}
bool Variant::operator<(const Variant& p_variant) const {
if (type!=p_variant.type) //if types differ, then order by type first
return type<p_variant.type;
bool v;
Variant r;
evaluate(OP_LESS,*this,p_variant,r,v);
return r;
}
2014-02-10 02:10:30 +01:00
bool Variant::is_zero() const {
switch( type ) {
case NIL: {
return true;
} break;
// atomic types
case BOOL: {
return _data._bool==false;
} break;
case INT: {
return _data._int==0;
} break;
case REAL: {
return _data._real==0;
} break;
case STRING: {
return *reinterpret_cast<const String*>(_data._mem)==String();
} break;
// math types
case VECTOR2: {
return *reinterpret_cast<const Vector2*>(_data._mem)==Vector2();
} break;
case RECT2: {
return *reinterpret_cast<const Rect2*>(_data._mem)==Rect2();
} break;
case TRANSFORM2D: {
2014-02-10 02:10:30 +01:00
return *_data._transform2d==Transform2D();
2014-02-10 02:10:30 +01:00
} break;
case VECTOR3: {
return *reinterpret_cast<const Vector3*>(_data._mem)==Vector3();
} break;
case PLANE: {
return *reinterpret_cast<const Plane*>(_data._mem)==Plane();
} break;
/*
case QUAT: {
} break;*/
case RECT3: {
2014-02-10 02:10:30 +01:00
return *_data._rect3==Rect3();
2014-02-10 02:10:30 +01:00
} break;
case QUAT: {
2015-05-04 05:53:33 +02:00
return *reinterpret_cast<const Quat*>(_data._mem)==Quat();
2014-02-10 02:10:30 +01:00
} break;
case BASIS: {
2014-02-10 02:10:30 +01:00
return *_data._basis==Basis();
2014-02-10 02:10:30 +01:00
} break;
case TRANSFORM: {
return *_data._transform == Transform();
} break;
// misc types
case COLOR: {
return *reinterpret_cast<const Color*>(_data._mem)==Color();
} break;
case IMAGE: {
return _data._image->empty();
} break;
case _RID: {
return *reinterpret_cast<const RID*>(_data._mem)==RID();
} break;
case OBJECT: {
return _get_obj().obj==NULL;
} break;
case NODE_PATH: {
return reinterpret_cast<const NodePath*>(_data._mem)->is_empty();
} break;
case INPUT_EVENT: {
return _data._input_event->type==InputEvent::NONE;
} break;
case DICTIONARY: {
return reinterpret_cast<const Dictionary*>(_data._mem)->empty();
} break;
case ARRAY: {
return reinterpret_cast<const Array*>(_data._mem)->empty();
} break;
// arrays
case POOL_BYTE_ARRAY: {
2014-02-10 02:10:30 +01:00
return reinterpret_cast<const PoolVector<uint8_t>*>(_data._mem)->size()==0;
2014-02-10 02:10:30 +01:00
} break;
case POOL_INT_ARRAY: {
2014-02-10 02:10:30 +01:00
return reinterpret_cast<const PoolVector<int>*>(_data._mem)->size()==0;
2014-02-10 02:10:30 +01:00
} break;
case POOL_REAL_ARRAY: {
2014-02-10 02:10:30 +01:00
return reinterpret_cast<const PoolVector<real_t>*>(_data._mem)->size()==0;
2014-02-10 02:10:30 +01:00
} break;
case POOL_STRING_ARRAY: {
2014-02-10 02:10:30 +01:00
return reinterpret_cast<const PoolVector<String>*>(_data._mem)->size()==0;
2014-02-10 02:10:30 +01:00
} break;
case POOL_VECTOR2_ARRAY: {
2014-02-10 02:10:30 +01:00
return reinterpret_cast<const PoolVector<Vector2>*>(_data._mem)->size()==0;
2014-02-10 02:10:30 +01:00
} break;
case POOL_VECTOR3_ARRAY: {
2014-02-10 02:10:30 +01:00
return reinterpret_cast<const PoolVector<Vector3>*>(_data._mem)->size()==0;
2014-02-10 02:10:30 +01:00
} break;
case POOL_COLOR_ARRAY: {
2014-02-10 02:10:30 +01:00
return reinterpret_cast<const PoolVector<Color>*>(_data._mem)->size()==0;
2014-02-10 02:10:30 +01:00
} break;
default: {}
}
return false;
}
bool Variant::is_one() const {
switch( type ) {
case NIL: {
return true;
} break;
// atomic types
case BOOL: {
return _data._bool==true;
} break;
case INT: {
return _data._int==1;
} break;
case REAL: {
return _data._real==1;
} break;
case VECTOR2: {
return *reinterpret_cast<const Vector2*>(_data._mem)==Vector2(1,1);
} break;
case RECT2: {
return *reinterpret_cast<const Rect2*>(_data._mem)==Rect2(1,1,1,1);
} break;
case VECTOR3: {
return *reinterpret_cast<const Vector3*>(_data._mem)==Vector3(1,1,1);
} break;
case PLANE: {
return *reinterpret_cast<const Plane*>(_data._mem)==Plane(1,1,1,1);
} break;
case COLOR: {
return *reinterpret_cast<const Color*>(_data._mem)==Color(1,1,1,1);
} break;
default: { return !is_zero(); }
}
return false;
}
2014-02-10 02:10:30 +01:00
void Variant::reference(const Variant& p_variant) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (this == &p_variant)
return;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
clear();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
type=p_variant.type;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
switch( p_variant.type ) {
case NIL: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
// none
} break;
2016-03-09 00:00:52 +01:00
// atomic types
2014-02-10 02:10:30 +01:00
case BOOL: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
_data._bool=p_variant._data._bool;
} break;
case INT: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
_data._int=p_variant._data._int;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case REAL: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
_data._real=p_variant._data._real;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case STRING: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, String( *reinterpret_cast<const String*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
// math types
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
case VECTOR2: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, Vector2( *reinterpret_cast<const Vector2*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case RECT2: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, Rect2( *reinterpret_cast<const Rect2*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case TRANSFORM2D: {
2014-02-10 02:10:30 +01:00
_data._transform2d = memnew( Transform2D( *p_variant._data._transform2d ) );
2014-02-10 02:10:30 +01:00
} break;
case VECTOR3: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, Vector3( *reinterpret_cast<const Vector3*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case PLANE: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, Plane( *reinterpret_cast<const Plane*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
2016-03-09 00:00:52 +01:00
/*
2014-02-10 02:10:30 +01:00
case QUAT: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;*/
case RECT3: {
2016-03-09 00:00:52 +01:00
_data._rect3 = memnew( Rect3( *p_variant._data._rect3 ) );
2014-02-10 02:10:30 +01:00
} break;
case QUAT: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, Quat( *reinterpret_cast<const Quat*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case BASIS: {
2016-03-09 00:00:52 +01:00
_data._basis = memnew( Basis( *p_variant._data._basis ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case TRANSFORM: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
_data._transform = memnew( Transform( *p_variant._data._transform ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
2016-03-09 00:00:52 +01:00
// misc types
2014-02-10 02:10:30 +01:00
case COLOR: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, Color( *reinterpret_cast<const Color*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case IMAGE: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
_data._image = memnew( Image( *p_variant._data._image ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case _RID: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, RID( *reinterpret_cast<const RID*>(p_variant._data._mem) ) );
} break;
case OBJECT: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, ObjData( p_variant._get_obj() ) );
} break;
case NODE_PATH: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, NodePath( *reinterpret_cast<const NodePath*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case INPUT_EVENT: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
_data._input_event= memnew( InputEvent( *p_variant._data._input_event ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case DICTIONARY: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, Dictionary( *reinterpret_cast<const Dictionary*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case ARRAY: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, Array ( *reinterpret_cast<const Array*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
// arrays
case POOL_BYTE_ARRAY: {
2016-03-09 00:00:52 +01:00
memnew_placement( _data._mem, PoolVector<uint8_t> ( *reinterpret_cast<const PoolVector<uint8_t>*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_INT_ARRAY: {
2016-03-09 00:00:52 +01:00
memnew_placement( _data._mem, PoolVector<int> ( *reinterpret_cast<const PoolVector<int>*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_REAL_ARRAY: {
2016-03-09 00:00:52 +01:00
memnew_placement( _data._mem, PoolVector<real_t> ( *reinterpret_cast<const PoolVector<real_t>*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_STRING_ARRAY: {
2016-03-09 00:00:52 +01:00
memnew_placement( _data._mem, PoolVector<String> ( *reinterpret_cast<const PoolVector<String>*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_VECTOR2_ARRAY: {
2014-02-10 02:10:30 +01:00
memnew_placement( _data._mem, PoolVector<Vector2> ( *reinterpret_cast<const PoolVector<Vector2>*>(p_variant._data._mem) ) );
2014-02-10 02:10:30 +01:00
} break;
case POOL_VECTOR3_ARRAY: {
2016-03-09 00:00:52 +01:00
memnew_placement( _data._mem, PoolVector<Vector3> ( *reinterpret_cast<const PoolVector<Vector3>*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_COLOR_ARRAY: {
2016-03-09 00:00:52 +01:00
memnew_placement( _data._mem, PoolVector<Color> ( *reinterpret_cast<const PoolVector<Color>*>(p_variant._data._mem) ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
default: {}
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
void Variant::zero() {
switch(type) {
case NIL: break;
case BOOL: this->_data._bool = false; break;
case INT: this->_data._int = 0; break;
case REAL: this->_data._real = 0; break;
case VECTOR2: *reinterpret_cast<Vector2*>(this->_data._mem) = Vector2(); break;
case RECT2: *reinterpret_cast<Rect2*>(this->_data._mem) = Rect2(); break;
case VECTOR3: *reinterpret_cast<Vector3*>(this->_data._mem) = Vector3(); break;
case PLANE: *reinterpret_cast<Plane*>(this->_data._mem) = Plane(); break;
case QUAT: *reinterpret_cast<Quat*>(this->_data._mem) = Quat(); break;
case COLOR: *reinterpret_cast<Color*>(this->_data._mem) = Color(); break;
default: this->clear(); break;
}
}
2014-02-10 02:10:30 +01:00
void Variant::clear() {
switch(type) {
case STRING: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
reinterpret_cast<String*>(_data._mem)->~String();
} break;
/*
// no point, they don't allocate memory
VECTOR3,
PLANE,
QUAT,
COLOR,
VECTOR2,
RECT2
*/
case TRANSFORM2D: {
2014-02-10 02:10:30 +01:00
memdelete( _data._transform2d );
2014-02-10 02:10:30 +01:00
} break;
case RECT3: {
2016-03-09 00:00:52 +01:00
memdelete( _data._rect3 );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case BASIS: {
2016-03-09 00:00:52 +01:00
memdelete( _data._basis );
2014-02-10 02:10:30 +01:00
} break;
case TRANSFORM: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memdelete( _data._transform );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
2016-03-09 00:00:52 +01:00
// misc types
2014-02-10 02:10:30 +01:00
case IMAGE: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memdelete( _data._image );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case NODE_PATH: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
reinterpret_cast<NodePath*>(_data._mem)->~NodePath();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case OBJECT: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
_get_obj().obj=NULL;
_get_obj().ref.unref();
} break;
case _RID: {
// not much need probably
reinterpret_cast<RID*>(_data._mem)->~RID();
} break;
case DICTIONARY: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
reinterpret_cast<Dictionary*>(_data._mem)->~Dictionary();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case ARRAY: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
reinterpret_cast<Array*>(_data._mem)->~Array();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case INPUT_EVENT: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memdelete( _data._input_event );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
// arrays
case POOL_BYTE_ARRAY: {
2016-03-09 00:00:52 +01:00
reinterpret_cast< PoolVector<uint8_t>* >(_data._mem)->~PoolVector<uint8_t>();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_INT_ARRAY: {
2016-03-09 00:00:52 +01:00
reinterpret_cast< PoolVector<int>* >(_data._mem)->~PoolVector<int>();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_REAL_ARRAY: {
2016-03-09 00:00:52 +01:00
reinterpret_cast< PoolVector<real_t>* >(_data._mem)->~PoolVector<real_t>();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_STRING_ARRAY: {
2016-03-09 00:00:52 +01:00
reinterpret_cast< PoolVector<String>* >(_data._mem)->~PoolVector<String>();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_VECTOR2_ARRAY: {
2014-02-10 02:10:30 +01:00
reinterpret_cast< PoolVector<Vector2>* >(_data._mem)->~PoolVector<Vector2>();
2014-02-10 02:10:30 +01:00
} break;
case POOL_VECTOR3_ARRAY: {
2016-03-09 00:00:52 +01:00
reinterpret_cast< PoolVector<Vector3>* >(_data._mem)->~PoolVector<Vector3>();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
case POOL_COLOR_ARRAY: {
2016-03-09 00:00:52 +01:00
reinterpret_cast< PoolVector<Color>* >(_data._mem)->~PoolVector<Color>();
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break;
default: {} /* not needed */
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
type=NIL;
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
Variant::operator signed int() const {
switch( type ) {
2016-03-09 00:00:52 +01:00
case NIL: return 0;
2014-02-10 02:10:30 +01:00
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
}
Variant::operator unsigned int() const {
switch( type ) {
2016-03-09 00:00:52 +01:00
case NIL: return 0;
2014-02-10 02:10:30 +01:00
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
}
Variant::operator int64_t() const {
switch( type ) {
case NIL: return 0;
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
return 0;
}
}
return 0;
}
/*
Variant::operator long unsigned int() const {
switch( type ) {
case NIL: return 0;
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
return 0;
}
}
return 0;
};
*/
Variant::operator uint64_t() const {
switch( type ) {
case NIL: return 0;
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
return 0;
}
}
return 0;
}
#ifdef NEED_LONG_INT
Variant::operator signed long() const {
switch( type ) {
case NIL: return 0;
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
return 0;
}
}
return 0;
};
Variant::operator unsigned long() const {
switch( type ) {
case NIL: return 0;
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
return 0;
}
}
return 0;
};
#endif
Variant::operator signed short() const {
switch( type ) {
2016-03-09 00:00:52 +01:00
case NIL: return 0;
2014-02-10 02:10:30 +01:00
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
}
Variant::operator unsigned short() const {
switch( type ) {
2016-03-09 00:00:52 +01:00
case NIL: return 0;
2014-02-10 02:10:30 +01:00
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
}
Variant::operator signed char() const {
switch( type ) {
2016-03-09 00:00:52 +01:00
case NIL: return 0;
2014-02-10 02:10:30 +01:00
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
}
Variant::operator unsigned char() const {
switch( type ) {
2016-03-09 00:00:52 +01:00
case NIL: return 0;
2014-02-10 02:10:30 +01:00
case BOOL: return _data._bool ? 1 : 0;
case INT: return _data._int;
case REAL: return _data._real;
case STRING: return operator String().to_int();
default: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
}
2014-02-10 02:10:30 +01:00
Variant::operator CharType() const {
return operator unsigned int();
}
2014-02-10 02:10:30 +01:00
Variant::operator float() const {
switch( type ) {
2016-03-09 00:00:52 +01:00
case NIL: return 0;
2014-02-10 02:10:30 +01:00
case BOOL: return _data._bool ? 1.0 : 0.0;
case INT: return (float)_data._int;
case REAL: return _data._real;
case STRING: return operator String().to_double();
default: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
}
Variant::operator double() const {
switch( type ) {
2016-03-09 00:00:52 +01:00
case NIL: return 0;
2014-02-10 02:10:30 +01:00
case BOOL: return _data._bool ? 1.0 : 0.0;
case INT: return (double)_data._int;
2014-02-10 02:10:30 +01:00
case REAL: return _data._real;
case STRING: return operator String().to_double();
default: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return 0;
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return true;
}
Variant::operator StringName() const {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (type==NODE_PATH) {
return reinterpret_cast<const NodePath*>(_data._mem)->get_sname();
}
return StringName(operator String());
}
struct _VariantStrPair {
String key;
String value;
bool operator<(const _VariantStrPair& p) const {
return key < p.key;
}
};
Variant::operator String() const {
switch( type ) {
2016-03-09 00:00:52 +01:00
case NIL: return "Null";
2014-02-10 02:10:30 +01:00
case BOOL: return _data._bool ? "True" : "False";
case INT: return itos(_data._int);
case REAL: return rtos(_data._real);
2014-02-10 02:10:30 +01:00
case STRING: return *reinterpret_cast<const String*>(_data._mem);
case VECTOR2: return "("+operator Vector2()+")";
case RECT2: return "("+operator Rect2()+")";
case TRANSFORM2D: {
Transform2D mat32 = operator Transform2D();
return "("+Variant(mat32.elements[0]).operator String()+", "+Variant(mat32.elements[1]).operator String()+", "+Variant(mat32.elements[2]).operator String()+")";
} break;
case VECTOR3: return "("+operator Vector3()+")";
2014-02-10 02:10:30 +01:00
case PLANE: return operator Plane();
2016-03-09 00:00:52 +01:00
//case QUAT:
case RECT3: return operator Rect3();
case QUAT: return "("+operator Quat()+")";
case BASIS: {
Basis mat3 = operator Basis();
String mtx("(");
for (int i=0;i<3;i++) {
if (i!=0)
mtx+=", ";
mtx+="(";
for (int j=0;j<3;j++) {
if (j!=0)
mtx+=", ";
mtx+=Variant( mat3.elements[i][j] ).operator String();
}
mtx+=")";
}
return mtx+")";
} break;
2014-02-10 02:10:30 +01:00
case TRANSFORM: return operator Transform();
case NODE_PATH: return operator NodePath();
case INPUT_EVENT: return operator InputEvent();
case COLOR: return String::num( operator Color().r)+","+String::num( operator Color().g)+","+String::num( operator Color().b)+","+String::num( operator Color().a) ;
case DICTIONARY: {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
const Dictionary &d =*reinterpret_cast<const Dictionary*>(_data._mem);
//const String *K=NULL;
String str;
List<Variant> keys;
d.get_key_list(&keys);
Vector<_VariantStrPair> pairs;
for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
_VariantStrPair sp;
sp.key=String(E->get());
sp.value=d[E->get()];
pairs.push_back(sp);
}
pairs.sort();
for(int i=0;i<pairs.size();i++) {
if (i>0)
str+=", ";
str+="("+pairs[i].key+":"+pairs[i].value+")";
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return str;
} break;
case POOL_VECTOR2_ARRAY: {
PoolVector<Vector2> vec = operator PoolVector<Vector2>();
2016-07-02 15:54:37 +02:00
String str("[");
for(int i=0;i<vec.size();i++) {
if (i>0)
str+=", ";
str=str+Variant( vec[i] );
}
2016-07-02 15:54:37 +02:00
str += "]";
return str;
} break;
case POOL_VECTOR3_ARRAY: {
2016-03-09 00:00:52 +01:00
PoolVector<Vector3> vec = operator PoolVector<Vector3>();
2016-07-02 15:54:37 +02:00
String str("[");
2014-02-10 02:10:30 +01:00
for(int i=0;i<vec.size();i++) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (i>0)
str+=", ";
str=str+Variant( vec[i] );
}
2016-07-02 15:54:37 +02:00
str += "]";
2014-02-10 02:10:30 +01:00
return str;
} break;
case POOL_STRING_ARRAY: {
2014-02-10 02:10:30 +01:00
PoolVector<String> vec = operator PoolVector<String>();
2016-07-02 15:54:37 +02:00
String str("[");
2014-02-10 02:10:30 +01:00
for(int i=0;i<vec.size();i++) {
if (i>0)
str+=", ";
str=str+vec[i];
}
2016-07-02 15:54:37 +02:00
str += "]";
2014-02-10 02:10:30 +01:00
return str;
} break;
case POOL_INT_ARRAY: {
2014-02-10 02:10:30 +01:00
PoolVector<int> vec = operator PoolVector<int>();
2016-07-02 15:54:37 +02:00
String str("[");
2014-02-10 02:10:30 +01:00
for(int i=0;i<vec.size();i++) {
if (i>0)
str+=", ";
str=str+itos(vec[i]);
}
2016-07-02 15:54:37 +02:00
str += "]";
2014-02-10 02:10:30 +01:00
return str;
} break;
case POOL_REAL_ARRAY: {
2014-02-10 02:10:30 +01:00
PoolVector<real_t> vec = operator PoolVector<real_t>();
2016-07-02 15:54:37 +02:00
String str("[");
2014-02-10 02:10:30 +01:00
for(int i=0;i<vec.size();i++) {
if (i>0)
str+=", ";
str=str+rtos(vec[i]);
}
2016-07-02 15:54:37 +02:00
str += "]";
2014-02-10 02:10:30 +01:00
return str;
} break;
case ARRAY: {
Array arr = operator Array();
2016-07-02 15:54:37 +02:00
String str("[");
2014-02-10 02:10:30 +01:00
for (int i=0; i<arr.size(); i++) {
if (i)
str+=", ";
str += String(arr[i]);
};
2016-07-02 15:54:37 +02:00
str += "]";
2014-02-10 02:10:30 +01:00
return str;
} break;
case OBJECT: {
2015-09-04 04:24:55 +02:00
if (_get_obj().obj) {
#ifdef DEBUG_ENABLED
if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null()) {
//only if debugging!
if (!ObjectDB::instance_validate(_get_obj().obj)) {
return "[Deleted Object]";
};
};
#endif
return "["+_get_obj().obj->get_class()+":"+itos(_get_obj().obj->get_instance_ID())+"]";
2015-09-04 04:24:55 +02:00
} else
2014-02-10 02:10:30 +01:00
return "[Object:null]";
} break;
default: {
return "["+get_type_name(type)+"]";
2016-03-09 00:00:52 +01:00
}
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return "";
}
Variant::operator Vector2() const {
if (type==VECTOR2)
return *reinterpret_cast<const Vector2*>(_data._mem);
else if (type==VECTOR3)
return Vector2(reinterpret_cast<const Vector3*>(_data._mem)->x,reinterpret_cast<const Vector3*>(_data._mem)->y);
else
return Vector2();
}
Variant::operator Rect2() const {
if (type==RECT2)
return *reinterpret_cast<const Rect2*>(_data._mem);
else
return Rect2();
}
Variant::operator Vector3() const {
if (type==VECTOR3)
return *reinterpret_cast<const Vector3*>(_data._mem);
else
return Vector3();
}
Variant::operator Plane() const {
if (type==PLANE)
return *reinterpret_cast<const Plane*>(_data._mem);
else
return Plane();
}
Variant::operator Rect3() const {
2014-02-10 02:10:30 +01:00
if (type==RECT3)
return *_data._rect3;
2014-02-10 02:10:30 +01:00
else
return Rect3();
2014-02-10 02:10:30 +01:00
}
Variant::operator Basis() const {
2014-02-10 02:10:30 +01:00
if (type==BASIS)
return *_data._basis;
else if (type==QUAT)
return *reinterpret_cast<const Quat*>(_data._mem);
else if (type==TRANSFORM)
return _data._transform->basis;
2014-02-10 02:10:30 +01:00
else
return Basis();
2014-02-10 02:10:30 +01:00
}
Variant::operator Quat() const {
if (type==QUAT)
return *reinterpret_cast<const Quat*>(_data._mem);
else if (type==BASIS)
return *_data._basis;
else if (type==TRANSFORM)
return _data._transform->basis;
2014-02-10 02:10:30 +01:00
else
return Quat();
}
Variant::operator Transform() const {
if (type==TRANSFORM)
return *_data._transform;
else if (type==BASIS)
return Transform(*_data._basis,Vector3());
else if (type==QUAT)
return Transform(Basis(*reinterpret_cast<const Quat*>(_data._mem)),Vector3());
2014-02-10 02:10:30 +01:00
else
return Transform();
}
Variant::operator Transform2D() const {
2014-02-10 02:10:30 +01:00
if (type==TRANSFORM2D) {
return *_data._transform2d;
2014-02-10 02:10:30 +01:00
} else if (type==TRANSFORM) {
2017-01-14 18:03:38 +01:00
const Transform& t = *_data._transform;
Transform2D m;
2014-02-10 02:10:30 +01:00
m.elements[0][0]=t.basis.elements[0][0];
m.elements[0][1]=t.basis.elements[1][0];
m.elements[1][0]=t.basis.elements[0][1];
m.elements[1][1]=t.basis.elements[1][1];
m.elements[2][0]=t.origin[0];
m.elements[2][1]=t.origin[1];
return m;
} else
return Transform2D();
2014-02-10 02:10:30 +01:00
}
Variant::operator Color() const {
if (type==COLOR)
return *reinterpret_cast<const Color*>(_data._mem);
else if (type==STRING)
return Color::html( operator String() );
else if (type==INT)
return Color::hex( operator int() );
2014-02-10 02:10:30 +01:00
else
return Color();
}
Variant::operator Image() const {
if (type==IMAGE)
return *_data._image;
else
return Image();
}
Variant::operator NodePath() const {
if (type==NODE_PATH)
return *reinterpret_cast<const NodePath*>(_data._mem);
else if (type==STRING)
return NodePath(operator String());
else
return NodePath();
}
Variant::operator RefPtr() const {
if (type==OBJECT)
return _get_obj().ref;
else
return RefPtr();
}
Variant::operator RID() const {
if (type==_RID)
return *reinterpret_cast<const RID*>(_data._mem);
else if (type==OBJECT && !_get_obj().ref.is_null()) {
return _get_obj().ref.get_rid();
} else if (type==OBJECT && _get_obj().obj) {
Variant::CallError ce;
Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid,NULL,0,ce);
if (ce.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::_RID) {
return ret;
}
2014-02-10 02:10:30 +01:00
return RID();
} else {
return RID();
}
2014-02-10 02:10:30 +01:00
}
Variant::operator Object*() const {
if (type==OBJECT)
return _get_obj().obj;
else
return NULL;
}
Variant::operator Node*() const {
if (type==OBJECT)
return _get_obj().obj?_get_obj().obj->cast_to<Node>():NULL;
else
return NULL;
}
Variant::operator Control*() const {
if (type==OBJECT)
return _get_obj().obj?_get_obj().obj->cast_to<Control>():NULL;
else
return NULL;
}
Variant::operator InputEvent() const {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
if (type==INPUT_EVENT)
return *reinterpret_cast<const InputEvent*>(_data._input_event);
else
return InputEvent();
}
Variant::operator Dictionary() const {
2016-03-09 00:00:52 +01:00
if (type==DICTIONARY)
2014-02-10 02:10:30 +01:00
return *reinterpret_cast<const Dictionary*>(_data._mem);
else
return Dictionary();
}
template<class DA,class SA>
inline DA _convert_array(const SA& p_array) {
DA da;
da.resize(p_array.size());
for(int i=0;i<p_array.size();i++) {
da.set( i, Variant(p_array.get(i)) );
}
return da;
}
template<class DA>
inline DA _convert_array_from_variant(const Variant& p_variant) {
switch(p_variant.get_type()) {
case Variant::ARRAY: { return _convert_array<DA,Array >( p_variant.operator Array () ); }
case Variant::POOL_BYTE_ARRAY: { return _convert_array<DA,PoolVector<uint8_t> >( p_variant.operator PoolVector<uint8_t> () ); }
case Variant::POOL_INT_ARRAY: { return _convert_array<DA,PoolVector<int> >( p_variant.operator PoolVector<int> () ); }
case Variant::POOL_REAL_ARRAY: { return _convert_array<DA,PoolVector<real_t> >( p_variant.operator PoolVector<real_t> () ); }
case Variant::POOL_STRING_ARRAY: { return _convert_array<DA,PoolVector<String> >( p_variant.operator PoolVector<String> () ); }
case Variant::POOL_VECTOR2_ARRAY: { return _convert_array<DA,PoolVector<Vector2> >( p_variant.operator PoolVector<Vector2> () ); }
case Variant::POOL_VECTOR3_ARRAY: { return _convert_array<DA,PoolVector<Vector3> >( p_variant.operator PoolVector<Vector3> () ); }
case Variant::POOL_COLOR_ARRAY: { return _convert_array<DA,PoolVector<Color> >( p_variant.operator PoolVector<Color>() ); }
2014-02-10 02:10:30 +01:00
default: { return DA(); }
}
return DA();
}
Variant::operator Array() const {
2016-03-09 00:00:52 +01:00
if (type==ARRAY)
2014-02-10 02:10:30 +01:00
return *reinterpret_cast<const Array*>(_data._mem);
2016-03-09 00:00:52 +01:00
else
2014-02-10 02:10:30 +01:00
return _convert_array_from_variant<Array >(*this);
}
Variant::operator PoolVector<uint8_t>() const {
2014-02-10 02:10:30 +01:00
if (type==POOL_BYTE_ARRAY)
return *reinterpret_cast<const PoolVector<uint8_t>* >(_data._mem);
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant<PoolVector<uint8_t> >(*this);
2014-02-10 02:10:30 +01:00
}
Variant::operator PoolVector<int>() const {
2014-02-10 02:10:30 +01:00
if (type==POOL_INT_ARRAY)
return *reinterpret_cast<const PoolVector<int>* >(_data._mem);
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant<PoolVector<int> >(*this);
2014-02-10 02:10:30 +01:00
}
Variant::operator PoolVector<real_t>() const {
2014-02-10 02:10:30 +01:00
if (type==POOL_REAL_ARRAY)
return *reinterpret_cast<const PoolVector<real_t>* >(_data._mem);
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant<PoolVector<real_t> >(*this);
2014-02-10 02:10:30 +01:00
}
Variant::operator PoolVector<String>() const {
2014-02-10 02:10:30 +01:00
if (type==POOL_STRING_ARRAY)
return *reinterpret_cast<const PoolVector<String>* >(_data._mem);
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant<PoolVector<String> >(*this);
2014-02-10 02:10:30 +01:00
}
Variant::operator PoolVector<Vector3>() const {
2014-02-10 02:10:30 +01:00
if (type==POOL_VECTOR3_ARRAY)
return *reinterpret_cast<const PoolVector<Vector3>* >(_data._mem);
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant<PoolVector<Vector3> >(*this);
2014-02-10 02:10:30 +01:00
}
Variant::operator PoolVector<Vector2>() const {
2014-02-10 02:10:30 +01:00
if (type==POOL_VECTOR2_ARRAY)
return *reinterpret_cast<const PoolVector<Vector2>* >(_data._mem);
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant<PoolVector<Vector2> >(*this);
2014-02-10 02:10:30 +01:00
}
Variant::operator PoolVector<Color>() const {
2014-02-10 02:10:30 +01:00
if (type==POOL_COLOR_ARRAY)
return *reinterpret_cast<const PoolVector<Color>* >(_data._mem);
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant<PoolVector<Color> >(*this);
2014-02-10 02:10:30 +01:00
}
/* helpers */
Variant::operator Vector<RID>() const {
Array va= operator Array();
Vector<RID> rids;
rids.resize(va.size());
for(int i=0;i<rids.size();i++)
rids[i]=va[i];
return rids;
}
Variant::operator Vector<Vector2>() const {
PoolVector<Vector2> from=operator PoolVector<Vector2>();
2014-02-10 02:10:30 +01:00
Vector<Vector2> to;
int len=from.size();
if (len==0)
return Vector<Vector2>();
to.resize(len);
PoolVector<Vector2>::Read r = from.read();
2014-02-10 02:10:30 +01:00
Vector2 *w = &to[0];
for (int i=0;i<len;i++) {
w[i]=r[i];
}
return to;
}
Variant::operator PoolVector<Plane>() const {
2014-02-10 02:10:30 +01:00
Array va= operator Array();
PoolVector<Plane> planes;
2014-02-10 02:10:30 +01:00
int va_size=va.size();
if (va_size==0)
return planes;
planes.resize(va_size);
PoolVector<Plane>::Write w = planes.write();
2014-02-10 02:10:30 +01:00
for(int i=0;i<va_size;i++)
w[i]=va[i];
return planes;
}
Variant::operator PoolVector<Face3>() const {
2014-02-10 02:10:30 +01:00
PoolVector<Vector3> va= operator PoolVector<Vector3>();
PoolVector<Face3> faces;
2014-02-10 02:10:30 +01:00
int va_size=va.size();
if (va_size==0)
return faces;
faces.resize(va_size/3);
PoolVector<Face3>::Write w = faces.write();
PoolVector<Vector3>::Read r = va.read();
2014-02-10 02:10:30 +01:00
for(int i=0;i<va_size;i++)
w[i/3].vertex[i%3]=r[i];
return faces;
}
Variant::operator Vector<Plane>() const {
Array va= operator Array();
Vector<Plane> planes;
int va_size=va.size();
if (va_size==0)
return planes;
planes.resize(va_size);
for(int i=0;i<va_size;i++)
planes[i]=va[i];
return planes;
}
Variant::operator Vector<Variant>() const {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
Array from=operator Array();
Vector<Variant> to;
int len=from.size();
to.resize(len);
for (int i=0;i<len;i++) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
to[i]=from[i];
}
return to;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
}
Variant::operator Vector<uint8_t>() const {
PoolVector<uint8_t> from=operator PoolVector<uint8_t>();
2014-02-10 02:10:30 +01:00
Vector<uint8_t> to;
int len=from.size();
to.resize(len);
for (int i=0;i<len;i++) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
to[i]=from[i];
}
return to;
}
Variant::operator Vector<int>() const {
PoolVector<int> from=operator PoolVector<int>();
2014-02-10 02:10:30 +01:00
Vector<int> to;
int len=from.size();
to.resize(len);
for (int i=0;i<len;i++) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
to[i]=from[i];
}
return to;
}
Variant::operator Vector<real_t>() const {
PoolVector<real_t> from=operator PoolVector<real_t>();
2014-02-10 02:10:30 +01:00
Vector<real_t> to;
int len=from.size();
to.resize(len);
for (int i=0;i<len;i++) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
to[i]=from[i];
}
return to;
}
Variant::operator Vector<String>() const {
PoolVector<String> from=operator PoolVector<String>();
2014-02-10 02:10:30 +01:00
Vector<String> to;
int len=from.size();
to.resize(len);
for (int i=0;i<len;i++) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
to[i]=from[i];
}
return to;
}
Variant::operator Vector<Vector3>() const {
PoolVector<Vector3> from=operator PoolVector<Vector3>();
2014-02-10 02:10:30 +01:00
Vector<Vector3> to;
int len=from.size();
if (len==0)
return Vector<Vector3>();
to.resize(len);
PoolVector<Vector3>::Read r = from.read();
2014-02-10 02:10:30 +01:00
Vector3 *w = &to[0];
for (int i=0;i<len;i++) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
w[i]=r[i];
}
return to;
}
Variant::operator Vector<Color>() const {
PoolVector<Color> from=operator PoolVector<Color>();
2014-02-10 02:10:30 +01:00
Vector<Color> to;
int len=from.size();
if (len==0)
return Vector<Color>();
to.resize(len);
PoolVector<Color>::Read r = from.read();
2014-02-10 02:10:30 +01:00
Color *w = &to[0];
for (int i=0;i<len;i++) {
w[i]=r[i];
}
return to;
}
Variant::operator Margin() const {
return (Margin)operator int();
}
Variant::operator Orientation() const {
return (Orientation)operator int();
}
Variant::operator IP_Address() const {
if (type==POOL_REAL_ARRAY || type==POOL_INT_ARRAY || type==POOL_BYTE_ARRAY) {
2014-02-10 02:10:30 +01:00
PoolVector<int> addr=operator PoolVector<int>();
2014-02-10 02:10:30 +01:00
if (addr.size()==4) {
return IP_Address(addr.get(0),addr.get(1),addr.get(2),addr.get(3));
}
}
return IP_Address( operator String() );
}
Variant::Variant(bool p_bool) {
type=BOOL;
_data._bool=p_bool;
}
/*
Variant::Variant(long unsigned int p_long) {
type=INT;
_data._int=p_long;
};
*/
Variant::Variant(signed int p_int) {
type=INT;
_data._int=p_int;
}
Variant::Variant(unsigned int p_int) {
type=INT;
_data._int=p_int;
}
#ifdef NEED_LONG_INT
Variant::Variant(signed long p_int) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
type=INT;
_data._int=p_int;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
}
Variant::Variant(unsigned long p_int) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
type=INT;
_data._int=p_int;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
}
#endif
Variant::Variant(int64_t p_int) {
type=INT;
_data._int=p_int;
}
Variant::Variant(uint64_t p_int) {
type=INT;
_data._int=p_int;
}
Variant::Variant(signed short p_short) {
type=INT;
_data._int=p_short;
}
Variant::Variant(unsigned short p_short) {
type=INT;
_data._int=p_short;
}
Variant::Variant(signed char p_char) {
type=INT;
_data._int=p_char;
}
Variant::Variant(unsigned char p_char) {
type=INT;
_data._int=p_char;
}
Variant::Variant(float p_float) {
type=REAL;
_data._real=p_float;
}
Variant::Variant(double p_double) {
type=REAL;
_data._real=p_double;
}
Variant::Variant(const StringName& p_string) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
type=STRING;
memnew_placement( _data._mem, String( p_string.operator String() ) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const String& p_string) {
type=STRING;
memnew_placement( _data._mem, String( p_string ) );
}
Variant::Variant(const char * const p_cstring) {
type=STRING;
memnew_placement( _data._mem, String( (const char*)p_cstring ) );
}
Variant::Variant(const CharType * p_wstring) {
type=STRING;
memnew_placement( _data._mem, String( p_wstring ) );
}
Variant::Variant(const Vector3& p_vector3) {
type=VECTOR3;
memnew_placement( _data._mem, Vector3( p_vector3 ) );
}
Variant::Variant(const Vector2& p_vector2) {
type=VECTOR2;
memnew_placement( _data._mem, Vector2( p_vector2 ) );
}
Variant::Variant(const Rect2& p_rect2) {
type=RECT2;
memnew_placement( _data._mem, Rect2( p_rect2 ) );
}
Variant::Variant(const Plane& p_plane) {
type=PLANE;
memnew_placement( _data._mem, Plane( p_plane ) );
}
Variant::Variant(const Rect3& p_aabb) {
2014-02-10 02:10:30 +01:00
type=RECT3;
_data._rect3 = memnew( Rect3( p_aabb ) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const Basis& p_matrix) {
2014-02-10 02:10:30 +01:00
type=BASIS;
_data._basis= memnew( Basis( p_matrix ) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const Quat& p_quat) {
type=QUAT;
memnew_placement( _data._mem, Quat( p_quat ) );
}
Variant::Variant(const Transform& p_transform) {
type=TRANSFORM;
_data._transform = memnew( Transform( p_transform ) );
}
Variant::Variant(const Transform2D& p_transform) {
2014-02-10 02:10:30 +01:00
type=TRANSFORM2D;
_data._transform2d = memnew( Transform2D( p_transform ) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const Color& p_color) {
type=COLOR;
memnew_placement( _data._mem, Color(p_color) );
}
Variant::Variant(const Image& p_image) {
type=IMAGE;
_data._image=memnew( Image(p_image) );
}
Variant::Variant(const NodePath& p_node_path) {
type=NODE_PATH;
memnew_placement( _data._mem, NodePath(p_node_path) );
}
Variant::Variant(const InputEvent& p_input_event) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
type=INPUT_EVENT;
2016-03-09 00:00:52 +01:00
_data._input_event = memnew( InputEvent(p_input_event) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const RefPtr& p_resource) {
type=OBJECT;
memnew_placement( _data._mem, ObjData );
REF ref = p_resource;
_get_obj().obj=ref.ptr();
_get_obj().ref=p_resource;
}
Variant::Variant(const RID& p_rid) {
type=_RID;
memnew_placement( _data._mem, RID(p_rid) );
}
Variant::Variant(const Object* p_object) {
type=OBJECT;
memnew_placement( _data._mem, ObjData );
_get_obj().obj=const_cast<Object*>(p_object);
}
Variant::Variant(const Dictionary& p_dictionary) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
type=DICTIONARY;
memnew_placement( _data._mem, (Dictionary)( p_dictionary) );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const Array& p_array) {
type=ARRAY;
memnew_placement( _data._mem, Array(p_array) );
}
Variant::Variant(const PoolVector<Plane>& p_array) {
2014-02-10 02:10:30 +01:00
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
type=ARRAY;
Array *plane_array=memnew_placement( _data._mem, Array );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
plane_array->resize( p_array.size() );
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
for (int i=0;i<p_array.size();i++) {
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
plane_array->operator [](i)=Variant(p_array[i]);
}
}
Variant::Variant(const Vector<Plane>& p_array) {
type=ARRAY;
Array *plane_array=memnew_placement( _data._mem, Array );
plane_array->resize( p_array.size() );
for (int i=0;i<p_array.size();i++) {
plane_array->operator [](i)=Variant(p_array[i]);
}
}
Variant::Variant(const Vector<RID>& p_array) {
type=ARRAY;
Array *rid_array=memnew_placement( _data._mem, Array );
rid_array->resize( p_array.size() );
for (int i=0;i<p_array.size();i++) {
rid_array->set(i,Variant(p_array[i]));
}
}
Variant::Variant(const Vector<Vector2>& p_array) {
type=NIL;
PoolVector<Vector2> v;
2014-02-10 02:10:30 +01:00
int len=p_array.size();
if (len>0) {
v.resize(len);
PoolVector<Vector2>::Write w = v.write();
2014-02-10 02:10:30 +01:00
const Vector2 *r = p_array.ptr();
for (int i=0;i<len;i++)
w[i]=r[i];
}
*this=v;
}
Variant::Variant(const PoolVector<uint8_t>& p_raw_array) {
2014-02-10 02:10:30 +01:00
type=POOL_BYTE_ARRAY;
memnew_placement( _data._mem, PoolVector<uint8_t>(p_raw_array) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const PoolVector<int>& p_int_array) {
2014-02-10 02:10:30 +01:00
type=POOL_INT_ARRAY;
memnew_placement( _data._mem, PoolVector<int>(p_int_array) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const PoolVector<real_t>& p_real_array) {
2014-02-10 02:10:30 +01:00
type=POOL_REAL_ARRAY;
memnew_placement( _data._mem, PoolVector<real_t>(p_real_array) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const PoolVector<String>& p_string_array) {
2014-02-10 02:10:30 +01:00
type=POOL_STRING_ARRAY;
memnew_placement( _data._mem, PoolVector<String>(p_string_array) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const PoolVector<Vector3>& p_vector3_array) {
2014-02-10 02:10:30 +01:00
type=POOL_VECTOR3_ARRAY;
memnew_placement( _data._mem, PoolVector<Vector3>(p_vector3_array) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const PoolVector<Vector2>& p_vector2_array) {
2014-02-10 02:10:30 +01:00
type=POOL_VECTOR2_ARRAY;
memnew_placement( _data._mem, PoolVector<Vector2>(p_vector2_array) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const PoolVector<Color>& p_color_array) {
2014-02-10 02:10:30 +01:00
type=POOL_COLOR_ARRAY;
memnew_placement( _data._mem, PoolVector<Color>(p_color_array) );
2014-02-10 02:10:30 +01:00
}
Variant::Variant(const PoolVector<Face3>& p_face_array) {
2014-02-10 02:10:30 +01:00
PoolVector<Vector3> vertices;
2014-02-10 02:10:30 +01:00
int face_count=p_face_array.size();
vertices.resize(face_count*3);
if (face_count) {
PoolVector<Face3>::Read r = p_face_array.read();
PoolVector<Vector3>::Write w = vertices.write();
2014-02-10 02:10:30 +01:00
for(int i=0;i<face_count;i++) {
for(int j=0;j<3;j++)
w[i*3+j]=r[i].vertex[j];
}
r=PoolVector<Face3>::Read();
w=PoolVector<Vector3>::Write();
2014-02-10 02:10:30 +01:00
}
type = NIL;
*this = vertices;
}
/* helpers */
Variant::Variant(const Vector<Variant>& p_array) {
type=NIL;
Array v;
int len=p_array.size();
v.resize(len);
for (int i=0;i<len;i++)
v.set(i,p_array[i]);
*this=v;
}
Variant::Variant(const Vector<uint8_t>& p_array) {
type=NIL;
PoolVector<uint8_t> v;
2014-02-10 02:10:30 +01:00
int len=p_array.size();
v.resize(len);
for (int i=0;i<len;i++)
v.set(i,p_array[i]);
*this=v;
}
Variant::Variant(const Vector<int>& p_array) {
type=NIL;
PoolVector<int> v;
2014-02-10 02:10:30 +01:00
int len=p_array.size();
v.resize(len);
for (int i=0;i<len;i++)
v.set(i,p_array[i]);
*this=v;
}
Variant::Variant(const Vector<real_t>& p_array) {
type=NIL;
PoolVector<real_t> v;
2014-02-10 02:10:30 +01:00
int len=p_array.size();
v.resize(len);
for (int i=0;i<len;i++)
v.set(i,p_array[i]);
*this=v;
}
Variant::Variant(const Vector<String>& p_array) {
type=NIL;
PoolVector<String> v;
2014-02-10 02:10:30 +01:00
int len=p_array.size();
v.resize(len);
for (int i=0;i<len;i++)
v.set(i,p_array[i]);
*this=v;
}
Variant::Variant(const Vector<Vector3>& p_array) {
type=NIL;
PoolVector<Vector3> v;
2014-02-10 02:10:30 +01:00
int len=p_array.size();
if (len>0) {
v.resize(len);
PoolVector<Vector3>::Write w = v.write();
2014-02-10 02:10:30 +01:00
const Vector3 *r = p_array.ptr();
for (int i=0;i<len;i++)
w[i]=r[i];
}
*this=v;
}
Variant::Variant(const Vector<Color>& p_array) {
type=NIL;
PoolVector<Color> v;
2014-02-10 02:10:30 +01:00
int len=p_array.size();
v.resize(len);
for (int i=0;i<len;i++)
v.set(i,p_array[i]);
*this=v;
}
void Variant::operator=(const Variant& p_variant) {
reference(p_variant);
}
Variant::Variant(const IP_Address& p_address) {
type=STRING;
memnew_placement( _data._mem, String( p_address ) );
}
Variant::Variant(const Variant& p_variant) {
type=NIL;
reference(p_variant);
}
/*
Variant::~Variant() {
clear();
}*/
uint32_t Variant::hash() const {
switch( type ) {
case NIL: {
return 0;
} break;
case BOOL: {
return _data._bool?1:0;
} break;
case INT: {
return _data._int;
} break;
case REAL: {
MarshallFloat mf;
mf.f=_data._real;
return mf.i;
} break;
case STRING: {
return reinterpret_cast<const String*>(_data._mem)->hash();
} break;
// math types
case VECTOR2: {
uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->x);
return hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->y,hash);
} break;
case RECT2: {
uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.x);
hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.y,hash);
hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.x,hash);
return hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.y,hash);
} break;
case TRANSFORM2D: {
2014-02-10 02:10:30 +01:00
uint32_t hash = 5831;
for(int i=0;i<3;i++) {
for(int j=0;j<2;j++) {
hash = hash_djb2_one_float(_data._transform2d->elements[i][j],hash);
2014-02-10 02:10:30 +01:00
}
}
return hash;
} break;
case VECTOR3: {
uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->x);
hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->y,hash);
return hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->z,hash);
} break;
case PLANE: {
uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.x);
hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.y,hash);
hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.z,hash);
return hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->d,hash);
} break;
/*
case QUAT: {
} break;*/
case RECT3: {
2014-02-10 02:10:30 +01:00
uint32_t hash = 5831;
for(int i=0;i<3;i++) {
hash = hash_djb2_one_float(_data._rect3->pos[i],hash);
hash = hash_djb2_one_float(_data._rect3->size[i],hash);
2014-02-10 02:10:30 +01:00
}
return hash;
} break;
case QUAT: {
uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->x);
hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->y,hash);
hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->z,hash);
return hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->w,hash);
} break;
case BASIS: {
2014-02-10 02:10:30 +01:00
uint32_t hash = 5831;
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
hash = hash_djb2_one_float(_data._basis->elements[i][j],hash);
2014-02-10 02:10:30 +01:00
}
}
return hash;
} break;
case TRANSFORM: {
uint32_t hash = 5831;
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
hash = hash_djb2_one_float(_data._transform->basis.elements[i][j],hash);
}
hash = hash_djb2_one_float(_data._transform->origin[i],hash);
}
return hash;
} break;
// misc types
case COLOR: {
uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->r);
hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->g,hash);
hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->b,hash);
return hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->a,hash);
} break;
case IMAGE: {
return 0;
} break;
case _RID: {
return hash_djb2_one_64(reinterpret_cast<const RID*>(_data._mem)->get_id());
} break;
case OBJECT: {
return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
} break;
case NODE_PATH: {
return reinterpret_cast<const NodePath*>(_data._mem)->hash();
} break;
case INPUT_EVENT: {
return hash_djb2_buffer((uint8_t*)_data._input_event,sizeof(InputEvent));
} break;
case DICTIONARY: {
return reinterpret_cast<const Dictionary*>(_data._mem)->hash();
} break;
case ARRAY: {
const Array& arr = *reinterpret_cast<const Array* >(_data._mem);
return arr.hash();
} break;
case POOL_BYTE_ARRAY: {
2014-02-10 02:10:30 +01:00
const PoolVector<uint8_t>& arr = *reinterpret_cast<const PoolVector<uint8_t>* >(_data._mem);
2014-02-10 02:10:30 +01:00
int len = arr.size();
PoolVector<uint8_t>::Read r = arr.read();
2014-02-10 02:10:30 +01:00
return hash_djb2_buffer((uint8_t*)&r[0],len);
} break;
case POOL_INT_ARRAY: {
2014-02-10 02:10:30 +01:00
const PoolVector<int>& arr = *reinterpret_cast<const PoolVector<int>* >(_data._mem);
2014-02-10 02:10:30 +01:00
int len = arr.size();
PoolVector<int>::Read r = arr.read();
2014-02-10 02:10:30 +01:00
return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(int));
} break;
case POOL_REAL_ARRAY: {
2014-02-10 02:10:30 +01:00
const PoolVector<real_t>& arr = *reinterpret_cast<const PoolVector<real_t>* >(_data._mem);
2014-02-10 02:10:30 +01:00
int len = arr.size();
PoolVector<real_t>::Read r = arr.read();
2014-02-10 02:10:30 +01:00
return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(real_t));
} break;
case POOL_STRING_ARRAY: {
2014-02-10 02:10:30 +01:00
uint32_t hash=5831;
const PoolVector<String>& arr = *reinterpret_cast<const PoolVector<String>* >(_data._mem);
2014-02-10 02:10:30 +01:00
int len = arr.size();
PoolVector<String>::Read r = arr.read();
2014-02-10 02:10:30 +01:00
for(int i=0;i<len;i++) {
hash = hash_djb2_one_32(r[i].hash(),hash);
}
return hash;
} break;
case POOL_VECTOR2_ARRAY: {
2014-02-10 02:10:30 +01:00
uint32_t hash=5831;
const PoolVector<Vector2>& arr = *reinterpret_cast<const PoolVector<Vector2>* >(_data._mem);
2014-02-10 02:10:30 +01:00
int len = arr.size();
PoolVector<Vector2>::Read r = arr.read();
2014-02-10 02:10:30 +01:00
for(int i=0;i<len;i++) {
hash = hash_djb2_one_float(r[i].x,hash);
hash = hash_djb2_one_float(r[i].y,hash);
}
return hash;
} break;
case POOL_VECTOR3_ARRAY: {
2014-02-10 02:10:30 +01:00
uint32_t hash=5831;
const PoolVector<Vector3>& arr = *reinterpret_cast<const PoolVector<Vector3>* >(_data._mem);
2014-02-10 02:10:30 +01:00
int len = arr.size();
PoolVector<Vector3>::Read r = arr.read();
2014-02-10 02:10:30 +01:00
for(int i=0;i<len;i++) {
hash = hash_djb2_one_float(r[i].x,hash);
hash = hash_djb2_one_float(r[i].y,hash);
hash = hash_djb2_one_float(r[i].z,hash);
}
return hash;
} break;
case POOL_COLOR_ARRAY: {
2014-02-10 02:10:30 +01:00
uint32_t hash=5831;
const PoolVector<Color>& arr = *reinterpret_cast<const PoolVector<Color>* >(_data._mem);
2014-02-10 02:10:30 +01:00
int len = arr.size();
PoolVector<Color>::Read r = arr.read();
2014-02-10 02:10:30 +01:00
for(int i=0;i<len;i++) {
hash = hash_djb2_one_float(r[i].r,hash);
hash = hash_djb2_one_float(r[i].g,hash);
hash = hash_djb2_one_float(r[i].b,hash);
hash = hash_djb2_one_float(r[i].a,hash);
}
return hash;
} break;
default: {}
}
return 0;
}
bool Variant::is_ref() const {
return type==OBJECT && !_get_obj().ref.is_null();
}
Vector<Variant> varray() {
return Vector<Variant>();
}
Vector<Variant> varray(const Variant& p_arg1) {
Vector<Variant> v;
v.push_back(p_arg1);
return v;
}
Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2) {
Vector<Variant> v;
v.push_back(p_arg1);
v.push_back(p_arg2);
return v;
}
Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3) {
Vector<Variant> v;
v.push_back(p_arg1);
v.push_back(p_arg2);
v.push_back(p_arg3);
return v;
}
Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4) {
Vector<Variant> v;
v.push_back(p_arg1);
v.push_back(p_arg2);
v.push_back(p_arg3);
v.push_back(p_arg4);
return v;
}
Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4,const Variant& p_arg5) {
Vector<Variant> v;
v.push_back(p_arg1);
v.push_back(p_arg2);
v.push_back(p_arg3);
v.push_back(p_arg4);
v.push_back(p_arg5);
return v;
}
void Variant::static_assign(const Variant& p_variant) {
}
bool Variant::is_shared() const {
switch(type) {
case OBJECT: return true;
case ARRAY: return true;
case DICTIONARY: return true;
2014-02-10 02:10:30 +01:00
default: {}
}
return false;
}
Variant Variant::call(const StringName& p_method,VARIANT_ARG_DECLARE) {
VARIANT_ARGPTRS;
int argc=0;
for(int i=0;i<VARIANT_ARG_MAX;i++) {
if (argptr[i]->get_type()==Variant::NIL)
break;
argc++;
}
CallError error;
Variant ret = call(p_method,argptr,argc,error);
switch(error.error) {
case CallError::CALL_ERROR_INVALID_ARGUMENT: {
String err = "Invalid type for argument #"+itos(error.argument)+", expected '"+Variant::get_type_name(error.expected)+"'.";
ERR_PRINT(err.utf8().get_data());
} break;
case CallError::CALL_ERROR_INVALID_METHOD: {
String err = "Invalid method '"+p_method+"' for type '"+Variant::get_type_name(type)+"'.";
ERR_PRINT(err.utf8().get_data());
} break;
case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
String err = "Too many arguments for method '"+p_method+"'";
ERR_PRINT(err.utf8().get_data());
} break;
default: {}
}
return ret;
}
void Variant::construct_from_string(const String& p_string,Variant& r_value,ObjectConstruct p_obj_construct,void *p_construct_ud) {
r_value=Variant();
}
String Variant::get_construct_string() const {
String vars;
VariantWriter::write_to_string(*this,vars);
return vars;
}
String Variant::get_call_error_text(Object* p_base, const StringName& p_method,const Variant** p_argptrs,int p_argcount,const Variant::CallError &ce) {
String err_text;
if (ce.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
int errorarg=ce.argument;
err_text="Cannot convert argument "+itos(errorarg+1)+" from "+Variant::get_type_name(p_argptrs[errorarg]->get_type())+" to "+Variant::get_type_name(ce.expected)+".";
} else if (ce.error==Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
err_text="Method expected "+itos(ce.argument)+" arguments, but called with "+itos(p_argcount)+".";
} else if (ce.error==Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
err_text="Method expected "+itos(ce.argument)+" arguments, but called with "+itos(p_argcount)+".";
} else if (ce.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
err_text="Method not found.";
} else if (ce.error==Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
err_text="Instance is null";
} else if (ce.error==Variant::CallError::CALL_OK){
return "Call OK";
}
String class_name = p_base->get_class();
Ref<Script> script = p_base->get_script();
if (script.is_valid() && script->get_path().is_resource_file()) {
class_name+="("+script->get_path().get_file()+")";
}
return "'"+class_name+"::"+String(p_method)+"': "+err_text;
}
String vformat(const String& p_text, const Variant& p1,const Variant& p2,const Variant& p3,const Variant& p4,const Variant& p5) {
Array args;
if (p1.get_type()!=Variant::NIL) {
args.push_back(p1);
if (p2.get_type()!=Variant::NIL) {
args.push_back(p2);
if (p3.get_type()!=Variant::NIL) {
args.push_back(p3);
if (p4.get_type()!=Variant::NIL) {
args.push_back(p4);
if (p5.get_type()!=Variant::NIL) {
args.push_back(p5);
}
}
}
}
}
bool error=false;
String fmt = p_text.sprintf(args,&error);
ERR_FAIL_COND_V(error,String());
return fmt;
}