2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* variant.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:11:45 +02:00
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
2017-01-01 22:01:57 +01:00
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
2017-04-08 00:45:00 +02:00
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
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"
2017-03-19 00:36:26 +01:00
# include "core_string_names.h"
# include "io/marshalls.h"
2017-02-15 14:41:16 +01:00
# include "math_funcs.h"
2014-02-10 02:10:30 +01:00
# include "print_string.h"
2017-03-19 00:36:26 +01:00
# include "resource.h"
2014-02-10 02:10:30 +01:00
# include "scene/gui/control.h"
2017-03-19 00:36:26 +01:00
# include "scene/main/node.h"
2015-12-31 04:54:00 +01:00
# 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
2017-03-19 00:36:26 +01:00
switch ( p_type ) {
2014-02-10 02:10:30 +01:00
case NIL : {
return " Nil " ;
} break ;
2017-03-19 00:36:26 +01:00
// atomic types
2014-02-10 02:10:30 +01:00
case BOOL : {
return " bool " ;
} break ;
case INT : {
return " int " ;
} break ;
case REAL : {
2014-04-05 23:50:09 +02:00
return " float " ;
2014-02-10 02:10:30 +01:00
} break ;
case STRING : {
return " String " ;
} break ;
2017-03-19 00:36:26 +01:00
// math types
2014-02-10 02:10:30 +01:00
case VECTOR2 : {
return " Vector2 " ;
} break ;
case RECT2 : {
return " Rect2 " ;
} break ;
case MATRIX32 : {
return " Matrix32 " ;
} break ;
case VECTOR3 : {
return " Vector3 " ;
} break ;
case PLANE : {
return " Plane " ;
} break ;
2017-03-19 00:36:26 +01:00
/*
2014-02-10 02:10:30 +01:00
case QUAT : {
} break ; */
case _AABB : {
return " AABB " ;
} break ;
case QUAT : {
return " Quat " ;
} break ;
case MATRIX3 : {
return " Matrix3 " ;
} break ;
case TRANSFORM : {
return " Transform " ;
} break ;
2017-03-19 00:36:26 +01:00
// misc types
2014-02-10 02:10:30 +01:00
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 ;
2017-03-19 00:36:26 +01:00
// arrays
2014-02-10 02:10:30 +01:00
case RAW_ARRAY : {
return " RawArray " ;
} break ;
case INT_ARRAY : {
return " IntArray " ;
} break ;
case REAL_ARRAY : {
return " RealArray " ;
} break ;
case STRING_ARRAY : {
return " StringArray " ;
} break ;
case VECTOR2_ARRAY : {
return " Vector2Array " ;
} break ;
case VECTOR3_ARRAY : {
return " Vector3Array " ;
} break ;
case COLOR_ARRAY : {
return " ColorArray " ;
} break ;
default : { }
2017-03-19 00:36:26 +01:00
}
2014-02-10 02:10:30 +01:00
return " " ;
}
2017-03-19 00:36:26 +01:00
bool Variant : : can_convert ( Variant : : Type p_type_from , Variant : : Type p_type_to ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( p_type_from = = p_type_to )
2014-02-10 02:10:30 +01:00
return true ;
2017-03-19 00:36:26 +01:00
if ( p_type_to = = NIL & & p_type_from ! = NIL ) //nil can convert to anything
2014-02-10 02:10:30 +01:00
return true ;
if ( p_type_from = = NIL ) {
return ( p_type_to = = OBJECT ) ;
} ;
2017-03-19 00:36:26 +01:00
const Type * valid_types = NULL ;
const Type * invalid_types = NULL ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
switch ( p_type_to ) {
2014-02-10 02:10:30 +01:00
case BOOL : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
INT ,
REAL ,
2015-05-04 23:30:57 +02:00
STRING ,
2014-02-10 02:10:30 +01:00
NIL ,
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case INT : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
BOOL ,
REAL ,
2015-05-04 23:30:57 +02:00
STRING ,
2014-02-10 02:10:30 +01:00
NIL ,
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case REAL : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
BOOL ,
INT ,
2015-05-04 23:30:57 +02:00
STRING ,
2014-02-10 02:10:30 +01:00
NIL ,
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case STRING : {
2017-03-19 00:36:26 +01:00
static const Type invalid [ ] = {
2014-02-10 02:10:30 +01:00
OBJECT ,
IMAGE ,
NIL
} ;
2017-03-19 00:36:26 +01:00
invalid_types = invalid ;
2014-02-10 02:10:30 +01:00
} break ;
case MATRIX32 : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
TRANSFORM ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case QUAT : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
MATRIX3 ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case MATRIX3 : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
QUAT ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case TRANSFORM : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
MATRIX32 ,
QUAT ,
MATRIX3 ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
2016-03-09 00:00:52 +01:00
} break ;
2014-04-10 05:18:27 +02:00
2015-05-04 23:30:57 +02:00
case COLOR : {
static const Type valid [ ] = {
2015-05-27 15:56:57 +02:00
STRING ,
INT ,
2015-05-04 23:30:57 +02:00
NIL ,
} ;
valid_types = valid ;
} break ;
case _RID : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
OBJECT ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case OBJECT : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case NODE_PATH : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
STRING ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
RAW_ARRAY ,
INT_ARRAY ,
STRING_ARRAY ,
REAL_ARRAY ,
COLOR_ARRAY ,
VECTOR2_ARRAY ,
VECTOR3_ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
2017-03-19 00:36:26 +01:00
// arrays
2015-05-04 23:30:57 +02:00
case RAW_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case INT_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case REAL_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case STRING_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case VECTOR2_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case VECTOR3_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case COLOR_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
default : { }
}
if ( valid_types ) {
2017-03-19 00:36:26 +01:00
int i = 0 ;
while ( valid_types [ i ] ! = NIL ) {
2015-05-04 23:30:57 +02:00
2017-03-19 00:36:26 +01:00
if ( p_type_from = = valid_types [ i ] )
2015-05-04 23:30:57 +02:00
return true ;
i + + ;
}
2016-10-04 12:36:31 +02:00
2015-05-04 23:30:57 +02:00
} else if ( invalid_types ) {
2017-03-19 00:36:26 +01:00
int i = 0 ;
while ( invalid_types [ i ] ! = NIL ) {
2015-05-04 23:30:57 +02:00
2017-03-19 00:36:26 +01:00
if ( p_type_from = = invalid_types [ i ] )
2015-05-04 23:30:57 +02:00
return false ;
i + + ;
}
2016-10-04 12:36:31 +02:00
return true ;
2015-05-04 23:30:57 +02:00
}
return false ;
}
2017-03-19 00:36:26 +01:00
bool Variant : : can_convert_strict ( Variant : : Type p_type_from , Variant : : Type p_type_to ) {
2015-05-04 23:30:57 +02:00
2017-03-19 00:36:26 +01:00
if ( p_type_from = = p_type_to )
2015-05-04 23:30:57 +02:00
return true ;
2017-03-19 00:36:26 +01:00
if ( p_type_to = = NIL & & p_type_from ! = NIL ) //nil can convert to anything
2015-05-04 23:30:57 +02:00
return true ;
if ( p_type_from = = NIL ) {
return ( p_type_to = = OBJECT ) ;
} ;
2017-03-19 00:36:26 +01:00
const Type * valid_types = NULL ;
2015-05-04 23:30:57 +02:00
2017-03-19 00:36:26 +01:00
switch ( p_type_to ) {
2015-05-04 23:30:57 +02:00
case BOOL : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-05 14:53:37 +02:00
INT ,
REAL ,
2015-05-04 23:30:57 +02:00
//STRING,
NIL ,
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case INT : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-05 14:53:37 +02:00
BOOL ,
2015-05-04 23:30:57 +02:00
REAL ,
//STRING,
NIL ,
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case REAL : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-05 14:53:37 +02:00
BOOL ,
2015-05-04 23:30:57 +02:00
INT ,
//STRING,
NIL ,
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case STRING : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
NODE_PATH ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case MATRIX32 : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
TRANSFORM ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case QUAT : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
MATRIX3 ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case MATRIX3 : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
QUAT ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
case TRANSFORM : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2015-05-04 23:30:57 +02:00
MATRIX32 ,
QUAT ,
MATRIX3 ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2015-05-04 23:30:57 +02:00
} break ;
2014-04-10 05:18:27 +02:00
case COLOR : {
static const Type valid [ ] = {
STRING ,
INT ,
NIL ,
} ;
valid_types = valid ;
} break ;
2014-02-10 02:10:30 +01:00
case _RID : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
OBJECT ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case OBJECT : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case NODE_PATH : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
STRING ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
RAW_ARRAY ,
INT_ARRAY ,
STRING_ARRAY ,
REAL_ARRAY ,
COLOR_ARRAY ,
VECTOR2_ARRAY ,
VECTOR3_ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
2017-03-19 00:36:26 +01:00
// arrays
2014-02-10 02:10:30 +01:00
case RAW_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case INT_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case REAL_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case STRING_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR2_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR3_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
case COLOR_ARRAY : {
2017-03-19 00:36:26 +01:00
static const Type valid [ ] = {
2014-02-10 02:10:30 +01:00
ARRAY ,
NIL
} ;
2017-03-19 00:36:26 +01:00
valid_types = valid ;
2014-02-10 02:10:30 +01:00
} break ;
default : { }
}
if ( valid_types ) {
2017-03-19 00:36:26 +01:00
int i = 0 ;
while ( valid_types [ i ] ! = NIL ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( p_type_from = = valid_types [ i ] )
2014-02-10 02:10:30 +01:00
return true ;
i + + ;
}
}
return false ;
}
2017-03-19 00:36:26 +01:00
bool Variant : : operator = = ( const Variant & p_variant ) const {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( type ! = p_variant . type ) //evaluation of operator== needs to be more strict
2014-02-10 02:10:30 +01:00
return false ;
bool v ;
Variant r ;
2017-03-19 00:36:26 +01:00
evaluate ( OP_EQUAL , * this , p_variant , r , v ) ;
2014-02-10 02:10:30 +01:00
return r ;
}
2017-03-19 00:36:26 +01:00
bool Variant : : operator ! = ( const Variant & p_variant ) const {
2016-01-03 00:17:31 +01:00
2017-03-19 00:36:26 +01:00
if ( type ! = p_variant . type ) //evaluation of operator== needs to be more strict
2016-01-03 00:17:31 +01:00
return true ;
bool v ;
Variant r ;
2017-03-19 00:36:26 +01:00
evaluate ( OP_NOT_EQUAL , * this , p_variant , r , v ) ;
2016-01-03 00:17:31 +01:00
return r ;
}
2017-03-19 00:36:26 +01:00
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 ;
2015-05-01 02:53:41 +02:00
bool v ;
Variant r ;
2017-03-19 00:36:26 +01:00
evaluate ( OP_LESS , * this , p_variant , r , v ) ;
2015-05-01 02:53:41 +02:00
return r ;
}
2014-02-10 02:10:30 +01:00
bool Variant : : is_zero ( ) const {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2014-02-10 02:10:30 +01:00
case NIL : {
return true ;
} break ;
// atomic types
case BOOL : {
2017-03-19 00:36:26 +01:00
return _data . _bool = = false ;
2014-02-10 02:10:30 +01:00
} break ;
case INT : {
2017-03-19 00:36:26 +01:00
return _data . _int = = 0 ;
2014-02-10 02:10:30 +01:00
} break ;
case REAL : {
2017-03-19 00:36:26 +01:00
return _data . _real = = 0 ;
2014-02-10 02:10:30 +01:00
} break ;
case STRING : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const String * > ( _data . _mem ) = = String ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
// math types
case VECTOR2 : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Vector2 * > ( _data . _mem ) = = Vector2 ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case RECT2 : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Rect2 * > ( _data . _mem ) = = Rect2 ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case MATRIX32 : {
2017-03-19 00:36:26 +01:00
return * _data . _matrix32 = = Matrix32 ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR3 : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Vector3 * > ( _data . _mem ) = = Vector3 ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case PLANE : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Plane * > ( _data . _mem ) = = Plane ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
2017-03-19 00:36:26 +01:00
/*
2014-02-10 02:10:30 +01:00
case QUAT : {
} break ; */
case _AABB : {
2017-03-19 00:36:26 +01:00
return * _data . _aabb = = AABB ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case QUAT : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Quat * > ( _data . _mem ) = = Quat ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case MATRIX3 : {
2017-03-19 00:36:26 +01:00
return * _data . _matrix3 = = Matrix3 ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case TRANSFORM : {
return * _data . _transform = = Transform ( ) ;
} break ;
// misc types
case COLOR : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Color * > ( _data . _mem ) = = Color ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case IMAGE : {
return _data . _image - > empty ( ) ;
} break ;
case _RID : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const RID * > ( _data . _mem ) = = RID ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case OBJECT : {
2017-03-19 00:36:26 +01:00
return _get_obj ( ) . obj = = NULL ;
2014-02-10 02:10:30 +01:00
} break ;
case NODE_PATH : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const NodePath * > ( _data . _mem ) - > is_empty ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case INPUT_EVENT : {
2017-03-19 00:36:26 +01:00
return _data . _input_event - > type = = InputEvent : : NONE ;
2014-02-10 02:10:30 +01:00
} break ;
case DICTIONARY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const Dictionary * > ( _data . _mem ) - > empty ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case ARRAY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const Array * > ( _data . _mem ) - > empty ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
// arrays
case RAW_ARRAY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const DVector < uint8_t > * > ( _data . _mem ) - > size ( ) = = 0 ;
2014-02-10 02:10:30 +01:00
} break ;
case INT_ARRAY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const DVector < int > * > ( _data . _mem ) - > size ( ) = = 0 ;
2014-02-10 02:10:30 +01:00
} break ;
case REAL_ARRAY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const DVector < real_t > * > ( _data . _mem ) - > size ( ) = = 0 ;
2014-02-10 02:10:30 +01:00
} break ;
case STRING_ARRAY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const DVector < String > * > ( _data . _mem ) - > size ( ) = = 0 ;
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR2_ARRAY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const DVector < Vector2 > * > ( _data . _mem ) - > size ( ) = = 0 ;
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR3_ARRAY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const DVector < Vector3 > * > ( _data . _mem ) - > size ( ) = = 0 ;
2014-02-10 02:10:30 +01:00
} break ;
case COLOR_ARRAY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const DVector < Color > * > ( _data . _mem ) - > size ( ) = = 0 ;
2014-02-10 02:10:30 +01:00
} break ;
default : { }
}
return false ;
}
2015-06-29 05:29:49 +02:00
bool Variant : : is_one ( ) const {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2015-06-29 05:29:49 +02:00
case NIL : {
return true ;
} break ;
// atomic types
case BOOL : {
2017-03-19 00:36:26 +01:00
return _data . _bool = = true ;
2015-06-29 05:29:49 +02:00
} break ;
case INT : {
2017-03-19 00:36:26 +01:00
return _data . _int = = 1 ;
2015-06-29 05:29:49 +02:00
} break ;
case REAL : {
2017-03-19 00:36:26 +01:00
return _data . _real = = 1 ;
2015-06-29 05:29:49 +02:00
} break ;
case VECTOR2 : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Vector2 * > ( _data . _mem ) = = Vector2 ( 1 , 1 ) ;
2015-06-29 05:29:49 +02:00
} break ;
case RECT2 : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Rect2 * > ( _data . _mem ) = = Rect2 ( 1 , 1 , 1 , 1 ) ;
2015-06-29 05:29:49 +02:00
} break ;
case VECTOR3 : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Vector3 * > ( _data . _mem ) = = Vector3 ( 1 , 1 , 1 ) ;
2015-06-29 05:29:49 +02:00
} break ;
case PLANE : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Plane * > ( _data . _mem ) = = Plane ( 1 , 1 , 1 , 1 ) ;
2015-06-29 05:29:49 +02:00
} break ;
case COLOR : {
2017-03-19 00:36:26 +01:00
return * reinterpret_cast < const Color * > ( _data . _mem ) = = Color ( 1 , 1 , 1 , 1 ) ;
2015-06-29 05:29:49 +02:00
} break ;
default : { return ! is_zero ( ) ; }
}
return false ;
}
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +01:00
type = p_variant . type ;
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
switch ( p_variant . type ) {
2014-02-10 02:10:30 +01:00
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
2017-03-19 00:36:26 +01:00
_data . _bool = p_variant . _data . _bool ;
2014-02-10 02:10:30 +01:00
} break ;
case INT : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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
case VECTOR2 : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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 MATRIX32 : {
2017-03-19 00:36:26 +01:00
_data . _matrix32 = memnew ( Matrix32 ( * p_variant . _data . _matrix32 ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR3 : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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 ;
2017-03-19 00:36:26 +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 _AABB : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
_data . _aabb = memnew ( AABB ( * p_variant . _data . _aabb ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case QUAT : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +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 MATRIX3 : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
_data . _matrix3 = memnew ( Matrix3 ( * p_variant . _data . _matrix3 ) ) ;
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
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , RID ( * reinterpret_cast < const RID * > ( p_variant . _data . _mem ) ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case OBJECT : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , ObjData ( p_variant . _get_obj ( ) ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case NODE_PATH : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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 RAW_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , DVector < uint8_t > ( * reinterpret_cast < const DVector < uint8_t > * > ( p_variant . _data . _mem ) ) ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case INT_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , DVector < int > ( * reinterpret_cast < const DVector < int > * > ( p_variant . _data . _mem ) ) ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case REAL_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , DVector < real_t > ( * reinterpret_cast < const DVector < real_t > * > ( p_variant . _data . _mem ) ) ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case STRING_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , DVector < String > ( * reinterpret_cast < const DVector < String > * > ( p_variant . _data . _mem ) ) ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR2_ARRAY : {
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , DVector < Vector2 > ( * reinterpret_cast < const DVector < Vector2 > * > ( p_variant . _data . _mem ) ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR3_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , DVector < Vector3 > ( * reinterpret_cast < const DVector < Vector3 > * > ( p_variant . _data . _mem ) ) ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case COLOR_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , DVector < Color > ( * reinterpret_cast < const DVector < 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
}
2016-02-18 04:34:49 +01:00
void Variant : : zero ( ) {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2016-02-18 04:34:49 +01:00
case NIL : break ;
case BOOL : this - > _data . _bool = false ; break ;
case INT : this - > _data . _int = 0 ; break ;
case REAL : this - > _data . _real = 0 ; break ;
2017-03-19 00:36:26 +01:00
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 ;
2016-02-18 04:34:49 +01:00
default : this - > clear ( ) ; break ;
}
}
2014-02-10 02:10:30 +01:00
void Variant : : clear ( ) {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2014-02-10 02:10:30 +01:00
case STRING : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
reinterpret_cast < String * > ( _data . _mem ) - > ~ String ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
2017-03-19 00:36:26 +01:00
/*
2014-02-10 02:10:30 +01:00
// no point, they don't allocate memory
VECTOR3 ,
PLANE ,
QUAT ,
COLOR ,
VECTOR2 ,
RECT2
*/
case MATRIX32 : {
2017-03-19 00:36:26 +01:00
memdelete ( _data . _matrix32 ) ;
2014-02-10 02:10:30 +01:00
} break ;
case _AABB : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
memdelete ( _data . _aabb ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case MATRIX3 : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
memdelete ( _data . _matrix3 ) ;
2014-02-10 02:10:30 +01:00
} break ;
case TRANSFORM : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +01:00
_get_obj ( ) . obj = NULL ;
2014-02-10 02:10:30 +01:00
_get_obj ( ) . ref . unref ( ) ;
} break ;
case _RID : {
// not much need probably
2017-03-19 00:36:26 +01:00
reinterpret_cast < RID * > ( _data . _mem ) - > ~ RID ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case DICTIONARY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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
2017-03-19 00:36:26 +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 RAW_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
reinterpret_cast < DVector < uint8_t > * > ( _data . _mem ) - > ~ DVector < uint8_t > ( ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case INT_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
reinterpret_cast < DVector < int > * > ( _data . _mem ) - > ~ DVector < int > ( ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case REAL_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
reinterpret_cast < DVector < real_t > * > ( _data . _mem ) - > ~ DVector < real_t > ( ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case STRING_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
reinterpret_cast < DVector < String > * > ( _data . _mem ) - > ~ DVector < String > ( ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR2_ARRAY : {
2017-03-19 00:36:26 +01:00
reinterpret_cast < DVector < Vector2 > * > ( _data . _mem ) - > ~ DVector < Vector2 > ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case VECTOR3_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
reinterpret_cast < DVector < Vector3 > * > ( _data . _mem ) - > ~ DVector < Vector3 > ( ) ;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
} break ;
case COLOR_ARRAY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
reinterpret_cast < DVector < Color > * > ( _data . _mem ) - > ~ DVector < 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
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
Variant : : operator signed int ( ) const {
2017-03-19 00:36:26 +01:00
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 {
2017-03-19 00:36:26 +01:00
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 {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2014-02-10 02:10:30 +01:00
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 {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2014-02-10 02:10:30 +01:00
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 {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2014-02-10 02:10:30 +01:00
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 {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2014-02-10 02:10:30 +01:00
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 {
2017-03-19 00:36:26 +01:00
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 {
2017-03-19 00:36:26 +01:00
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 {
2017-03-19 00:36:26 +01:00
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 {
2017-03-19 00:36:26 +01:00
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 ;
}
2016-06-19 00:05:23 +02:00
2014-02-10 02:10:30 +01:00
Variant : : operator CharType ( ) const {
return operator unsigned int ( ) ;
}
2016-06-19 00:05:23 +02:00
2014-02-10 02:10:30 +01:00
Variant : : operator float ( ) const {
2017-03-19 00:36:26 +01:00
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 {
2017-03-19 00:36:26 +01:00
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 true ;
}
Variant : : operator StringName ( ) const {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
if ( type = = NODE_PATH ) {
return reinterpret_cast < const NodePath * > ( _data . _mem ) - > get_sname ( ) ;
2014-02-10 02:10:30 +01:00
}
return StringName ( operator String ( ) ) ;
}
struct _VariantStrPair {
String key ;
String value ;
2017-03-19 00:36:26 +01:00
bool operator < ( const _VariantStrPair & p ) const {
2014-02-10 02:10:30 +01:00
return key < p . key ;
}
} ;
Variant : : operator String ( ) const {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2016-03-09 00:00:52 +01:00
case NIL : return " " ;
2014-02-10 02:10:30 +01:00
case BOOL : return _data . _bool ? " True " : " False " ;
case INT : return String : : num ( _data . _int ) ;
case REAL : return String : : num ( _data . _real ) ;
2017-03-19 00:36:26 +01:00
case STRING : return * reinterpret_cast < const String * > ( _data . _mem ) ;
case VECTOR2 : return " ( " + operator Vector2 ( ) + " ) " ;
case RECT2 : return " ( " + operator Rect2 ( ) + " ) " ;
2016-07-28 14:41:15 +02:00
case MATRIX32 : {
Matrix32 mat32 = operator Matrix32 ( ) ;
2017-03-19 00:36:26 +01:00
return " ( " + Variant ( mat32 . elements [ 0 ] ) . operator String ( ) + " , " + Variant ( mat32 . elements [ 1 ] ) . operator String ( ) + " , " + Variant ( mat32 . elements [ 2 ] ) . operator String ( ) + " ) " ;
2016-07-28 14:41:15 +02:00
} break ;
2017-03-19 00:36:26 +01:00
case VECTOR3 : return " ( " + operator Vector3 ( ) + " ) " ;
case PLANE :
return operator Plane ( ) ;
2016-03-09 00:00:52 +01:00
//case QUAT:
2014-02-10 02:10:30 +01:00
case _AABB : return operator AABB ( ) ;
2017-03-19 00:36:26 +01:00
case QUAT : return " ( " + operator Quat ( ) + " ) " ;
2016-07-28 14:41:15 +02:00
case MATRIX3 : {
Matrix3 mat3 = operator Matrix3 ( ) ;
String mtx ( " ( " ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < 3 ; i + + ) {
2016-07-28 14:41:15 +02:00
2017-03-19 00:36:26 +01:00
if ( i ! = 0 )
mtx + = " , " ;
2016-07-28 14:41:15 +02:00
2017-03-19 00:36:26 +01:00
mtx + = " ( " ;
2016-07-28 14:41:15 +02:00
2017-03-19 00:36:26 +01:00
for ( int j = 0 ; j < 3 ; j + + ) {
2016-07-28 14:41:15 +02:00
2017-03-19 00:36:26 +01:00
if ( j ! = 0 )
mtx + = " , " ;
2016-07-28 14:41:15 +02:00
2017-03-19 00:36:26 +01:00
mtx + = Variant ( mat3 . elements [ i ] [ j ] ) . operator String ( ) ;
2016-07-28 14:41:15 +02:00
}
2017-03-19 00:36:26 +01:00
mtx + = " ) " ;
2016-07-28 14:41:15 +02:00
}
2017-03-19 00:36:26 +01:00
return mtx + " ) " ;
2016-07-28 14:41:15 +02:00
} 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 ( ) ;
2017-03-19 00:36:26 +01:00
case COLOR : return String : : num ( operator Color ( ) . r ) + " , " + String : : num ( operator Color ( ) . g ) + " , " + String : : num ( operator Color ( ) . b ) + " , " + String : : num ( operator Color ( ) . a ) ;
2014-02-10 02:10:30 +01:00
case DICTIONARY : {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
const Dictionary & d = * reinterpret_cast < const Dictionary * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
//const String *K=NULL;
String str ;
List < Variant > keys ;
d . get_key_list ( & keys ) ;
Vector < _VariantStrPair > pairs ;
2017-03-19 00:36:26 +01:00
for ( List < Variant > : : Element * E = keys . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 02:10:30 +01:00
_VariantStrPair sp ;
2017-03-19 00:36:26 +01:00
sp . key = String ( E - > get ( ) ) ;
sp . value = d [ E - > get ( ) ] ;
2014-02-10 02:10:30 +01:00
pairs . push_back ( sp ) ;
}
pairs . sort ( ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < pairs . size ( ) ; i + + ) {
if ( i > 0 )
str + = " , " ;
str + = " ( " + pairs [ i ] . key + " : " + pairs [ i ] . value + " ) " ;
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 str ;
} break ;
2016-05-04 15:04:12 +02:00
case VECTOR2_ARRAY : {
DVector < Vector2 > vec = operator DVector < Vector2 > ( ) ;
2016-07-02 15:54:37 +02:00
String str ( " [ " ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < vec . size ( ) ; i + + ) {
2016-05-04 15:04:12 +02:00
2017-03-19 00:36:26 +01:00
if ( i > 0 )
str + = " , " ;
str = str + Variant ( vec [ i ] ) ;
2016-05-04 15:04:12 +02:00
}
2016-07-02 15:54:37 +02:00
str + = " ] " ;
2016-05-04 15:04:12 +02:00
return str ;
} break ;
2016-03-09 00:00:52 +01:00
case VECTOR3_ARRAY : {
2014-02-10 02:10:30 +01:00
DVector < Vector3 > vec = operator DVector < Vector3 > ( ) ;
2016-07-02 15:54:37 +02:00
String str ( " [ " ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < vec . size ( ) ; i + + ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
if ( i > 0 )
str + = " , " ;
str = str + Variant ( vec [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
2016-07-02 15:54:37 +02:00
str + = " ] " ;
2014-02-10 02:10:30 +01:00
return str ;
} break ;
case STRING_ARRAY : {
DVector < String > vec = operator DVector < String > ( ) ;
2016-07-02 15:54:37 +02:00
String str ( " [ " ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < vec . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( i > 0 )
str + = " , " ;
str = str + vec [ i ] ;
2014-02-10 02:10:30 +01:00
}
2016-07-02 15:54:37 +02:00
str + = " ] " ;
2014-02-10 02:10:30 +01:00
return str ;
} break ;
case INT_ARRAY : {
DVector < int > vec = operator DVector < int > ( ) ;
2016-07-02 15:54:37 +02:00
String str ( " [ " ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < vec . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( i > 0 )
str + = " , " ;
str = str + itos ( vec [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
2016-07-02 15:54:37 +02:00
str + = " ] " ;
2014-02-10 02:10:30 +01:00
return str ;
} break ;
case REAL_ARRAY : {
DVector < real_t > vec = operator DVector < real_t > ( ) ;
2016-07-02 15:54:37 +02:00
String str ( " [ " ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < vec . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( i > 0 )
str + = " , " ;
str = str + rtos ( vec [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
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 ( " [ " ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < arr . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
if ( i )
2017-03-19 00:36:26 +01:00
str + = " , " ;
2014-02-10 02:10:30 +01:00
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 ) {
2017-03-19 00:36:26 +01:00
# 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] " ;
2015-09-04 04:24:55 +02:00
} ;
2017-03-19 00:36:26 +01:00
} ;
# endif
return " [ " + _get_obj ( ) . obj - > get_type ( ) + " : " + 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 : {
2017-03-19 00:36:26 +01:00
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 {
2017-03-19 00:36:26 +01:00
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 ) ;
2014-02-10 02:10:30 +01:00
else
return Vector2 ( ) ;
}
Variant : : operator Rect2 ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = RECT2 )
return * reinterpret_cast < const Rect2 * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return Rect2 ( ) ;
}
Variant : : operator Vector3 ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = VECTOR3 )
return * reinterpret_cast < const Vector3 * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return Vector3 ( ) ;
}
Variant : : operator Plane ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = PLANE )
return * reinterpret_cast < const Plane * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return Plane ( ) ;
}
Variant : : operator AABB ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = _AABB )
2014-02-10 02:10:30 +01:00
return * _data . _aabb ;
else
return AABB ( ) ;
}
Variant : : operator Matrix3 ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = MATRIX3 )
2014-02-10 02:10:30 +01:00
return * _data . _matrix3 ;
2017-03-19 00:36:26 +01:00
else if ( type = = QUAT )
return * reinterpret_cast < const Quat * > ( _data . _mem ) ;
else if ( type = = TRANSFORM )
2014-05-14 06:22:15 +02:00
return _data . _transform - > basis ;
2014-02-10 02:10:30 +01:00
else
return Matrix3 ( ) ;
}
Variant : : operator Quat ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = QUAT )
return * reinterpret_cast < const Quat * > ( _data . _mem ) ;
else if ( type = = MATRIX3 )
return * _data . _matrix3 ;
else if ( type = = TRANSFORM )
return _data . _transform - > basis ;
2014-02-10 02:10:30 +01:00
else
return Quat ( ) ;
}
Variant : : operator Transform ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = TRANSFORM )
2014-02-10 02:10:30 +01:00
return * _data . _transform ;
2017-03-19 00:36:26 +01:00
else if ( type = = MATRIX3 )
return Transform ( * _data . _matrix3 , Vector3 ( ) ) ;
else if ( type = = QUAT )
return Transform ( Matrix3 ( * reinterpret_cast < const Quat * > ( _data . _mem ) ) , Vector3 ( ) ) ;
2014-02-10 02:10:30 +01:00
else
return Transform ( ) ;
}
2017-03-19 00:36:26 +01:00
Variant : : operator Matrix32 ( ) const {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( type = = MATRIX32 ) {
return * _data . _matrix32 ;
} else if ( type = = TRANSFORM ) {
const Transform & t = * _data . _transform ;
Matrix32 m ;
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 Matrix32 ( ) ;
2014-02-10 02:10:30 +01:00
}
Variant : : operator Color ( ) const {
2017-03-19 00:36:26 +01:00
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 {
2017-03-19 00:36:26 +01:00
if ( type = = IMAGE )
2014-02-10 02:10:30 +01:00
return * _data . _image ;
else
return Image ( ) ;
}
Variant : : operator NodePath ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = NODE_PATH )
return * reinterpret_cast < const NodePath * > ( _data . _mem ) ;
else if ( type = = STRING )
2014-02-10 02:10:30 +01:00
return NodePath ( operator String ( ) ) ;
else
return NodePath ( ) ;
}
Variant : : operator RefPtr ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = OBJECT )
2014-02-10 02:10:30 +01:00
return _get_obj ( ) . ref ;
else
return RefPtr ( ) ;
}
Variant : : operator RID ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = _RID )
return * reinterpret_cast < const RID * > ( _data . _mem ) ;
else if ( type = = OBJECT & & ! _get_obj ( ) . ref . is_null ( ) ) {
2014-02-10 02:10:30 +01:00
return _get_obj ( ) . ref . get_rid ( ) ;
2017-03-19 00:36:26 +01:00
} else if ( type = = OBJECT & & _get_obj ( ) . obj ) {
2015-04-25 01:45:07 +02:00
Variant : : CallError ce ;
2017-03-19 00:36:26 +01:00
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 ) {
2015-04-25 01:45:07 +02:00
return ret ;
}
2014-02-10 02:10:30 +01:00
return RID ( ) ;
2015-04-25 01:45:07 +02:00
} else {
return RID ( ) ;
}
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : operator Object * ( ) const {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( type = = OBJECT )
2014-02-10 02:10:30 +01:00
return _get_obj ( ) . obj ;
else
return NULL ;
}
2017-03-19 00:36:26 +01:00
Variant : : operator Node * ( ) const {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( type = = OBJECT )
return _get_obj ( ) . obj ? _get_obj ( ) . obj - > cast_to < Node > ( ) : NULL ;
2014-02-10 02:10:30 +01:00
else
return NULL ;
}
2017-03-19 00:36:26 +01:00
Variant : : operator Control * ( ) const {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
if ( type = = OBJECT )
return _get_obj ( ) . obj ? _get_obj ( ) . obj - > cast_to < Control > ( ) : NULL ;
2014-02-10 02:10:30 +01:00
else
return NULL ;
}
Variant : : operator InputEvent ( ) const {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
if ( type = = INPUT_EVENT )
return * reinterpret_cast < const InputEvent * > ( _data . _input_event ) ;
2014-02-10 02:10:30 +01:00
else
return InputEvent ( ) ;
}
Variant : : operator Dictionary ( ) const {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
if ( type = = DICTIONARY )
return * reinterpret_cast < const Dictionary * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return Dictionary ( ) ;
}
2017-03-19 00:36:26 +01:00
template < class DA , class SA >
inline DA _convert_array ( const SA & p_array ) {
2014-02-10 02:10:30 +01:00
DA da ;
da . resize ( p_array . size ( ) ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < p_array . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
da . set ( i , Variant ( p_array . get ( i ) ) ) ;
2014-02-10 02:10:30 +01:00
}
return da ;
}
2017-03-19 00:36:26 +01:00
template < class DA >
inline DA _convert_array_from_variant ( const Variant & p_variant ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
switch ( p_variant . get_type ( ) ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
case Variant : : ARRAY : {
return _convert_array < DA , Array > ( p_variant . operator Array ( ) ) ;
}
case Variant : : RAW_ARRAY : {
return _convert_array < DA , DVector < uint8_t > > ( p_variant . operator DVector < uint8_t > ( ) ) ;
}
case Variant : : INT_ARRAY : {
return _convert_array < DA , DVector < int > > ( p_variant . operator DVector < int > ( ) ) ;
}
case Variant : : REAL_ARRAY : {
return _convert_array < DA , DVector < real_t > > ( p_variant . operator DVector < real_t > ( ) ) ;
}
case Variant : : STRING_ARRAY : {
return _convert_array < DA , DVector < String > > ( p_variant . operator DVector < String > ( ) ) ;
}
case Variant : : VECTOR2_ARRAY : {
return _convert_array < DA , DVector < Vector2 > > ( p_variant . operator DVector < Vector2 > ( ) ) ;
}
case Variant : : VECTOR3_ARRAY : {
return _convert_array < DA , DVector < Vector3 > > ( p_variant . operator DVector < Vector3 > ( ) ) ;
}
case Variant : : COLOR_ARRAY : {
return _convert_array < DA , DVector < Color > > ( p_variant . operator DVector < 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
2017-03-19 00:36:26 +01:00
if ( type = = ARRAY )
return * reinterpret_cast < const Array * > ( _data . _mem ) ;
2016-03-09 00:00:52 +01:00
else
2017-03-19 00:36:26 +01:00
return _convert_array_from_variant < Array > ( * this ) ;
2014-02-10 02:10:30 +01:00
}
Variant : : operator DVector < uint8_t > ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = RAW_ARRAY )
return * reinterpret_cast < const DVector < uint8_t > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant < DVector < uint8_t > > ( * this ) ;
}
Variant : : operator DVector < int > ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = INT_ARRAY )
return * reinterpret_cast < const DVector < int > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant < DVector < int > > ( * this ) ;
}
Variant : : operator DVector < real_t > ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = REAL_ARRAY )
return * reinterpret_cast < const DVector < real_t > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant < DVector < real_t > > ( * this ) ;
}
Variant : : operator DVector < String > ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = STRING_ARRAY )
return * reinterpret_cast < const DVector < String > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant < DVector < String > > ( * this ) ;
}
Variant : : operator DVector < Vector3 > ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = VECTOR3_ARRAY )
return * reinterpret_cast < const DVector < Vector3 > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant < DVector < Vector3 > > ( * this ) ;
}
Variant : : operator DVector < Vector2 > ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = VECTOR2_ARRAY )
return * reinterpret_cast < const DVector < Vector2 > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant < DVector < Vector2 > > ( * this ) ;
}
Variant : : operator DVector < Color > ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = COLOR_ARRAY )
return * reinterpret_cast < const DVector < Color > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
else
return _convert_array_from_variant < DVector < Color > > ( * this ) ;
}
/* helpers */
Variant : : operator Vector < RID > ( ) const {
2017-03-19 00:36:26 +01:00
Array va = operator Array ( ) ;
2014-02-10 02:10:30 +01:00
Vector < RID > rids ;
rids . resize ( va . size ( ) ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < rids . size ( ) ; i + + )
rids [ i ] = va [ i ] ;
2014-02-10 02:10:30 +01:00
return rids ;
}
Variant : : operator Vector < Vector2 > ( ) const {
2017-03-19 00:36:26 +01:00
DVector < Vector2 > from = operator DVector < Vector2 > ( ) ;
2014-02-10 02:10:30 +01:00
Vector < Vector2 > to ;
2017-03-19 00:36:26 +01:00
int len = from . size ( ) ;
if ( len = = 0 )
2014-02-10 02:10:30 +01:00
return Vector < Vector2 > ( ) ;
to . resize ( len ) ;
DVector < Vector2 > : : Read r = from . read ( ) ;
Vector2 * w = & to [ 0 ] ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
w [ i ] = r [ i ] ;
2014-02-10 02:10:30 +01:00
}
return to ;
}
Variant : : operator DVector < Plane > ( ) const {
2017-03-19 00:36:26 +01:00
Array va = operator Array ( ) ;
2014-02-10 02:10:30 +01:00
DVector < Plane > planes ;
2017-03-19 00:36:26 +01:00
int va_size = va . size ( ) ;
if ( va_size = = 0 )
2014-02-10 02:10:30 +01:00
return planes ;
planes . resize ( va_size ) ;
DVector < Plane > : : Write w = planes . write ( ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < va_size ; i + + )
w [ i ] = va [ i ] ;
2014-02-10 02:10:30 +01:00
return planes ;
}
Variant : : operator DVector < Face3 > ( ) const {
2017-03-19 00:36:26 +01:00
DVector < Vector3 > va = operator DVector < Vector3 > ( ) ;
2014-02-10 02:10:30 +01:00
DVector < Face3 > faces ;
2017-03-19 00:36:26 +01:00
int va_size = va . size ( ) ;
if ( va_size = = 0 )
2014-02-10 02:10:30 +01:00
return faces ;
2017-03-19 00:36:26 +01:00
faces . resize ( va_size / 3 ) ;
2014-02-10 02:10:30 +01:00
DVector < Face3 > : : Write w = faces . write ( ) ;
DVector < Vector3 > : : Read r = va . read ( ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < va_size ; i + + )
w [ i / 3 ] . vertex [ i % 3 ] = r [ i ] ;
2014-02-10 02:10:30 +01:00
return faces ;
}
Variant : : operator Vector < Plane > ( ) const {
2017-03-19 00:36:26 +01:00
Array va = operator Array ( ) ;
2014-02-10 02:10:30 +01:00
Vector < Plane > planes ;
2017-03-19 00:36:26 +01:00
int va_size = va . size ( ) ;
if ( va_size = = 0 )
2014-02-10 02:10:30 +01:00
return planes ;
planes . resize ( va_size ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < va_size ; i + + )
planes [ i ] = va [ i ] ;
2014-02-10 02:10:30 +01:00
return planes ;
}
Variant : : operator Vector < Variant > ( ) const {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
Array from = operator Array ( ) ;
2014-02-10 02:10:30 +01:00
Vector < Variant > to ;
2017-03-19 00:36:26 +01:00
int len = from . size ( ) ;
2014-02-10 02:10:30 +01:00
to . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
to [ i ] = from [ i ] ;
2014-02-10 02:10:30 +01:00
}
return to ;
}
Variant : : operator Vector < uint8_t > ( ) const {
2017-03-19 00:36:26 +01:00
DVector < uint8_t > from = operator DVector < uint8_t > ( ) ;
2014-02-10 02:10:30 +01:00
Vector < uint8_t > to ;
2017-03-19 00:36:26 +01:00
int len = from . size ( ) ;
2014-02-10 02:10:30 +01:00
to . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
to [ i ] = from [ i ] ;
2014-02-10 02:10:30 +01:00
}
return to ;
}
Variant : : operator Vector < int > ( ) const {
2017-03-19 00:36:26 +01:00
DVector < int > from = operator DVector < int > ( ) ;
2014-02-10 02:10:30 +01:00
Vector < int > to ;
2017-03-19 00:36:26 +01:00
int len = from . size ( ) ;
2014-02-10 02:10:30 +01:00
to . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
to [ i ] = from [ i ] ;
2014-02-10 02:10:30 +01:00
}
return to ;
}
Variant : : operator Vector < real_t > ( ) const {
2017-03-19 00:36:26 +01:00
DVector < real_t > from = operator DVector < real_t > ( ) ;
2014-02-10 02:10:30 +01:00
Vector < real_t > to ;
2017-03-19 00:36:26 +01:00
int len = from . size ( ) ;
2014-02-10 02:10:30 +01:00
to . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
to [ i ] = from [ i ] ;
2014-02-10 02:10:30 +01:00
}
return to ;
}
Variant : : operator Vector < String > ( ) const {
2017-03-19 00:36:26 +01:00
DVector < String > from = operator DVector < String > ( ) ;
2014-02-10 02:10:30 +01:00
Vector < String > to ;
2017-03-19 00:36:26 +01:00
int len = from . size ( ) ;
2014-02-10 02:10:30 +01:00
to . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
to [ i ] = from [ i ] ;
2014-02-10 02:10:30 +01:00
}
return to ;
}
Variant : : operator Vector < Vector3 > ( ) const {
2017-03-19 00:36:26 +01:00
DVector < Vector3 > from = operator DVector < Vector3 > ( ) ;
2014-02-10 02:10:30 +01:00
Vector < Vector3 > to ;
2017-03-19 00:36:26 +01:00
int len = from . size ( ) ;
if ( len = = 0 )
2014-02-10 02:10:30 +01:00
return Vector < Vector3 > ( ) ;
to . resize ( len ) ;
DVector < Vector3 > : : Read r = from . read ( ) ;
Vector3 * w = & to [ 0 ] ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
w [ i ] = r [ i ] ;
2014-02-10 02:10:30 +01:00
}
return to ;
}
Variant : : operator Vector < Color > ( ) const {
2017-03-19 00:36:26 +01:00
DVector < Color > from = operator DVector < Color > ( ) ;
2014-02-10 02:10:30 +01:00
Vector < Color > to ;
2017-03-19 00:36:26 +01:00
int len = from . size ( ) ;
if ( len = = 0 )
2014-02-10 02:10:30 +01:00
return Vector < Color > ( ) ;
to . resize ( len ) ;
DVector < Color > : : Read r = from . read ( ) ;
Color * w = & to [ 0 ] ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
w [ i ] = r [ i ] ;
2014-02-10 02:10:30 +01:00
}
return to ;
}
Variant : : operator Margin ( ) const {
2017-03-19 00:36:26 +01:00
return ( Margin ) operator int ( ) ;
2014-02-10 02:10:30 +01:00
}
Variant : : operator Orientation ( ) const {
2017-03-19 00:36:26 +01:00
return ( Orientation ) operator int ( ) ;
2014-02-10 02:10:30 +01:00
}
Variant : : operator IP_Address ( ) const {
2017-03-19 00:36:26 +01:00
if ( type = = REAL_ARRAY | | type = = INT_ARRAY | | type = = RAW_ARRAY ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
DVector < int > addr = operator DVector < int > ( ) ;
if ( addr . size ( ) = = 4 ) {
return IP_Address ( addr . get ( 0 ) , addr . get ( 1 ) , addr . get ( 2 ) , addr . get ( 3 ) ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-19 00:36:26 +01:00
return IP_Address ( operator String ( ) ) ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( bool p_bool ) {
2017-03-19 00:36:26 +01:00
type = BOOL ;
_data . _bool = p_bool ;
2014-02-10 02:10:30 +01:00
}
/*
Variant : : Variant ( long unsigned int p_long ) {
type = INT ;
_data . _int = p_long ;
} ;
*/
Variant : : Variant ( signed int p_int ) {
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_int ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( unsigned int p_int ) {
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_int ;
2014-02-10 02:10:30 +01:00
}
# ifdef NEED_LONG_INT
Variant : : Variant ( signed long p_int ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_int ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( unsigned long p_int ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_int ;
2014-02-10 02:10:30 +01:00
}
# endif
Variant : : Variant ( int64_t p_int ) {
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_int ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( uint64_t p_int ) {
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_int ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( signed short p_short ) {
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_short ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( unsigned short p_short ) {
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_short ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( signed char p_char ) {
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_char ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( unsigned char p_char ) {
2017-03-19 00:36:26 +01:00
type = INT ;
_data . _int = p_char ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( float p_float ) {
2017-03-19 00:36:26 +01:00
type = REAL ;
_data . _real = p_float ;
2014-02-10 02:10:30 +01:00
}
Variant : : Variant ( double p_double ) {
2017-03-19 00:36:26 +01:00
type = REAL ;
_data . _real = p_double ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const StringName & p_string ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
type = STRING ;
memnew_placement ( _data . _mem , String ( p_string . operator String ( ) ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const String & p_string ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = STRING ;
memnew_placement ( _data . _mem , String ( p_string ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const char * const p_cstring ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = STRING ;
memnew_placement ( _data . _mem , String ( ( const char * ) p_cstring ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const CharType * p_wstring ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = STRING ;
memnew_placement ( _data . _mem , String ( p_wstring ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector3 & p_vector3 ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = VECTOR3 ;
memnew_placement ( _data . _mem , Vector3 ( p_vector3 ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector2 & p_vector2 ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = VECTOR2 ;
memnew_placement ( _data . _mem , Vector2 ( p_vector2 ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Rect2 & p_rect2 ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = RECT2 ;
memnew_placement ( _data . _mem , Rect2 ( p_rect2 ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Plane & p_plane ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = PLANE ;
memnew_placement ( _data . _mem , Plane ( p_plane ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const AABB & p_aabb ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = _AABB ;
_data . _aabb = memnew ( AABB ( p_aabb ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Matrix3 & p_matrix ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = MATRIX3 ;
_data . _matrix3 = memnew ( Matrix3 ( p_matrix ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Quat & p_quat ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = QUAT ;
memnew_placement ( _data . _mem , Quat ( p_quat ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Transform & p_transform ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = TRANSFORM ;
_data . _transform = memnew ( Transform ( p_transform ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Matrix32 & p_transform ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = MATRIX32 ;
_data . _matrix32 = memnew ( Matrix32 ( p_transform ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Color & p_color ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = COLOR ;
memnew_placement ( _data . _mem , Color ( p_color ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Image & p_image ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = IMAGE ;
_data . _image = memnew ( Image ( p_image ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const NodePath & p_node_path ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NODE_PATH ;
memnew_placement ( _data . _mem , NodePath ( p_node_path ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const InputEvent & p_input_event ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = INPUT_EVENT ;
_data . _input_event = memnew ( InputEvent ( p_input_event ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const RefPtr & p_resource ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = OBJECT ;
memnew_placement ( _data . _mem , ObjData ) ;
2014-02-10 02:10:30 +01:00
REF ref = p_resource ;
2017-03-19 00:36:26 +01:00
_get_obj ( ) . obj = ref . ptr ( ) ;
_get_obj ( ) . ref = p_resource ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const RID & p_rid ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = _RID ;
memnew_placement ( _data . _mem , RID ( p_rid ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Object * p_object ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = OBJECT ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
memnew_placement ( _data . _mem , ObjData ) ;
_get_obj ( ) . obj = const_cast < Object * > ( p_object ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Dictionary & p_dictionary ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
type = DICTIONARY ;
memnew_placement ( _data . _mem , ( Dictionary ) ( p_dictionary ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Array & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = ARRAY ;
memnew_placement ( _data . _mem , Array ( p_array ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const DVector < Plane > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = ARRAY ;
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
Array * plane_array = memnew_placement ( _data . _mem , Array ) ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
plane_array - > resize ( p_array . size ( ) ) ;
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < p_array . size ( ) ; i + + ) {
2016-03-09 00:00:52 +01:00
2017-03-19 00:36:26 +01:00
plane_array - > operator [ ] ( i ) = Variant ( p_array [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < Plane > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = ARRAY ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
Array * plane_array = memnew_placement ( _data . _mem , Array ) ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
plane_array - > resize ( p_array . size ( ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < p_array . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
plane_array - > operator [ ] ( i ) = Variant ( p_array [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < RID > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = ARRAY ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
Array * rid_array = memnew_placement ( _data . _mem , Array ) ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
rid_array - > resize ( p_array . size ( ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < p_array . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
rid_array - > set ( i , Variant ( p_array [ i ] ) ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < Vector2 > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
DVector < Vector2 > v ;
2017-03-19 00:36:26 +01:00
int len = p_array . size ( ) ;
if ( len > 0 ) {
2014-02-10 02:10:30 +01:00
v . resize ( len ) ;
DVector < Vector2 > : : Write w = v . write ( ) ;
const Vector2 * r = p_array . ptr ( ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + )
w [ i ] = r [ i ] ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
* this = v ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const DVector < uint8_t > & p_raw_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = RAW_ARRAY ;
memnew_placement ( _data . _mem , DVector < uint8_t > ( p_raw_array ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const DVector < int > & p_int_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = INT_ARRAY ;
memnew_placement ( _data . _mem , DVector < int > ( p_int_array ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const DVector < real_t > & p_real_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = REAL_ARRAY ;
memnew_placement ( _data . _mem , DVector < real_t > ( p_real_array ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const DVector < String > & p_string_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = STRING_ARRAY ;
memnew_placement ( _data . _mem , DVector < String > ( p_string_array ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const DVector < Vector3 > & p_vector3_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = VECTOR3_ARRAY ;
memnew_placement ( _data . _mem , DVector < Vector3 > ( p_vector3_array ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const DVector < Vector2 > & p_vector2_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = VECTOR2_ARRAY ;
memnew_placement ( _data . _mem , DVector < Vector2 > ( p_vector2_array ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const DVector < Color > & p_color_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = COLOR_ARRAY ;
memnew_placement ( _data . _mem , DVector < Color > ( p_color_array ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const DVector < Face3 > & p_face_array ) {
2014-02-10 02:10:30 +01:00
DVector < Vector3 > vertices ;
2017-03-19 00:36:26 +01:00
int face_count = p_face_array . size ( ) ;
vertices . resize ( face_count * 3 ) ;
2014-02-10 02:10:30 +01:00
if ( face_count ) {
DVector < Face3 > : : Read r = p_face_array . read ( ) ;
DVector < Vector3 > : : Write w = vertices . write ( ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < face_count ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
for ( int j = 0 ; j < 3 ; j + + )
w [ i * 3 + j ] = r [ i ] . vertex [ j ] ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
r = DVector < Face3 > : : Read ( ) ;
w = DVector < Vector3 > : : Write ( ) ;
2014-02-10 02:10:30 +01:00
}
type = NIL ;
* this = vertices ;
}
/* helpers */
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < Variant > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
Array v ;
2017-03-19 00:36:26 +01:00
int len = p_array . size ( ) ;
2014-02-10 02:10:30 +01:00
v . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + )
v . set ( i , p_array [ i ] ) ;
* this = v ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < uint8_t > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
DVector < uint8_t > v ;
2017-03-19 00:36:26 +01:00
int len = p_array . size ( ) ;
2014-02-10 02:10:30 +01:00
v . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + )
v . set ( i , p_array [ i ] ) ;
* this = v ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < int > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
DVector < int > v ;
2017-03-19 00:36:26 +01:00
int len = p_array . size ( ) ;
2014-02-10 02:10:30 +01:00
v . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + )
v . set ( i , p_array [ i ] ) ;
* this = v ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < real_t > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
DVector < real_t > v ;
2017-03-19 00:36:26 +01:00
int len = p_array . size ( ) ;
2014-02-10 02:10:30 +01:00
v . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + )
v . set ( i , p_array [ i ] ) ;
* this = v ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < String > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
DVector < String > v ;
2017-03-19 00:36:26 +01:00
int len = p_array . size ( ) ;
2014-02-10 02:10:30 +01:00
v . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + )
v . set ( i , p_array [ i ] ) ;
* this = v ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < Vector3 > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
DVector < Vector3 > v ;
2017-03-19 00:36:26 +01:00
int len = p_array . size ( ) ;
if ( len > 0 ) {
2014-02-10 02:10:30 +01:00
v . resize ( len ) ;
DVector < Vector3 > : : Write w = v . write ( ) ;
const Vector3 * r = p_array . ptr ( ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + )
w [ i ] = r [ i ] ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
* this = v ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Vector < Color > & p_array ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
DVector < Color > v ;
2017-03-19 00:36:26 +01:00
int len = p_array . size ( ) ;
2014-02-10 02:10:30 +01:00
v . resize ( len ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + )
v . set ( i , p_array [ i ] ) ;
* this = v ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
void Variant : : operator = ( const Variant & p_variant ) {
2014-02-10 02:10:30 +01:00
reference ( p_variant ) ;
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const IP_Address & p_address ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = STRING ;
memnew_placement ( _data . _mem , String ( p_address ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant : : Variant ( const Variant & p_variant ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
type = NIL ;
2014-02-10 02:10:30 +01:00
reference ( p_variant ) ;
}
/*
Variant : : ~ Variant ( ) {
clear ( ) ;
} */
uint32_t Variant : : hash ( ) const {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2014-02-10 02:10:30 +01:00
case NIL : {
return 0 ;
} break ;
case BOOL : {
2017-03-19 00:36:26 +01:00
return _data . _bool ? 1 : 0 ;
2014-02-10 02:10:30 +01:00
} break ;
case INT : {
return _data . _int ;
} break ;
case REAL : {
2017-02-15 14:41:16 +01:00
return hash_djb2_one_float ( _data . _real ) ;
2014-02-10 02:10:30 +01:00
} break ;
case STRING : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const String * > ( _data . _mem ) - > hash ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
2017-12-11 15:31:49 +01:00
// math types
2014-02-10 02:10:30 +01:00
case VECTOR2 : {
2017-03-19 00:36:26 +01:00
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 ) ;
2014-02-10 02:10:30 +01:00
} break ;
case RECT2 : {
2017-03-19 00:36:26 +01:00
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 ) ;
2014-02-10 02:10:30 +01:00
} break ;
case MATRIX32 : {
uint32_t hash = 5831 ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < 3 ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
for ( int j = 0 ; j < 2 ; j + + ) {
hash = hash_djb2_one_float ( _data . _matrix32 - > elements [ i ] [ j ] , hash ) ;
2014-02-10 02:10:30 +01:00
}
}
return hash ;
} break ;
case VECTOR3 : {
2017-03-19 00:36:26 +01:00
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 ) ;
2014-02-10 02:10:30 +01:00
} break ;
case PLANE : {
2017-03-19 00:36:26 +01:00
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 ) ;
2014-02-10 02:10:30 +01:00
} break ;
2017-03-19 00:36:26 +01:00
/*
2014-02-10 02:10:30 +01:00
case QUAT : {
} break ; */
case _AABB : {
uint32_t hash = 5831 ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < 3 ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
hash = hash_djb2_one_float ( _data . _aabb - > pos [ i ] , hash ) ;
hash = hash_djb2_one_float ( _data . _aabb - > size [ i ] , hash ) ;
2014-02-10 02:10:30 +01:00
}
return hash ;
} break ;
case QUAT : {
2017-03-19 00:36:26 +01:00
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 ) ;
2014-02-10 02:10:30 +01:00
} break ;
case MATRIX3 : {
uint32_t hash = 5831 ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < 3 ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
for ( int j = 0 ; j < 3 ; j + + ) {
hash = hash_djb2_one_float ( _data . _matrix3 - > elements [ i ] [ j ] , hash ) ;
2014-02-10 02:10:30 +01:00
}
}
return hash ;
} break ;
case TRANSFORM : {
uint32_t hash = 5831 ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < 3 ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
for ( int j = 0 ; j < 3 ; j + + ) {
hash = hash_djb2_one_float ( _data . _transform - > basis . elements [ i ] [ j ] , hash ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
hash = hash_djb2_one_float ( _data . _transform - > origin [ i ] , hash ) ;
2014-02-10 02:10:30 +01:00
}
return hash ;
} break ;
2017-03-19 00:36:26 +01:00
// misc types
2014-02-10 02:10:30 +01:00
case COLOR : {
2017-03-19 00:36:26 +01:00
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 ) ;
2014-02-10 02:10:30 +01:00
} break ;
case IMAGE : {
return 0 ;
} break ;
case _RID : {
2017-03-19 00:36:26 +01:00
return hash_djb2_one_64 ( reinterpret_cast < const RID * > ( _data . _mem ) - > get_id ( ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case OBJECT : {
return hash_djb2_one_64 ( make_uint64_t ( _get_obj ( ) . obj ) ) ;
} break ;
case NODE_PATH : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const NodePath * > ( _data . _mem ) - > hash ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case INPUT_EVENT : {
2017-03-19 00:36:26 +01:00
return hash_djb2_buffer ( ( uint8_t * ) _data . _input_event , sizeof ( InputEvent ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case DICTIONARY : {
2017-03-19 00:36:26 +01:00
return reinterpret_cast < const Dictionary * > ( _data . _mem ) - > hash ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case ARRAY : {
2017-03-19 00:36:26 +01:00
const Array & arr = * reinterpret_cast < const Array * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
return arr . hash ( ) ;
} break ;
case RAW_ARRAY : {
2017-03-19 00:36:26 +01:00
const DVector < uint8_t > & arr = * reinterpret_cast < const DVector < uint8_t > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
int len = arr . size ( ) ;
DVector < uint8_t > : : Read r = arr . read ( ) ;
2017-03-19 00:36:26 +01:00
return hash_djb2_buffer ( ( uint8_t * ) & r [ 0 ] , len ) ;
2014-02-10 02:10:30 +01:00
} break ;
case INT_ARRAY : {
2017-03-19 00:36:26 +01:00
const DVector < int > & arr = * reinterpret_cast < const DVector < int > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
int len = arr . size ( ) ;
DVector < int > : : Read r = arr . read ( ) ;
2017-03-19 00:36:26 +01:00
return hash_djb2_buffer ( ( uint8_t * ) & r [ 0 ] , len * sizeof ( int ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case REAL_ARRAY : {
2017-03-19 00:36:26 +01:00
const DVector < real_t > & arr = * reinterpret_cast < const DVector < real_t > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
int len = arr . size ( ) ;
DVector < real_t > : : Read r = arr . read ( ) ;
2017-03-19 00:36:26 +01:00
return hash_djb2_buffer ( ( uint8_t * ) & r [ 0 ] , len * sizeof ( real_t ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case STRING_ARRAY : {
2017-03-19 00:36:26 +01:00
uint32_t hash = 5831 ;
const DVector < String > & arr = * reinterpret_cast < const DVector < String > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
int len = arr . size ( ) ;
DVector < String > : : Read r = arr . read ( ) ;
2017-03-19 00:36:26 +01:00
for ( int i = 0 ; i < len ; i + + ) {
hash = hash_djb2_one_32 ( r [ i ] . hash ( ) , hash ) ;
2014-02-10 02:10:30 +01:00
}
return hash ;
} break ;
case VECTOR2_ARRAY : {
2017-03-19 00:36:26 +01:00
uint32_t hash = 5831 ;
const DVector < Vector2 > & arr = * reinterpret_cast < const DVector < Vector2 > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
int len = arr . size ( ) ;
DVector < Vector2 > : : Read r = arr . read ( ) ;
2017-03-19 00:36:26 +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 ) ;
2014-02-10 02:10:30 +01:00
}
return hash ;
} break ;
case VECTOR3_ARRAY : {
2017-03-19 00:36:26 +01:00
uint32_t hash = 5831 ;
const DVector < Vector3 > & arr = * reinterpret_cast < const DVector < Vector3 > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
int len = arr . size ( ) ;
DVector < Vector3 > : : Read r = arr . read ( ) ;
2017-03-19 00:36:26 +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 ) ;
2014-02-10 02:10:30 +01:00
}
return hash ;
} break ;
case COLOR_ARRAY : {
2017-03-19 00:36:26 +01:00
uint32_t hash = 5831 ;
const DVector < Color > & arr = * reinterpret_cast < const DVector < Color > * > ( _data . _mem ) ;
2014-02-10 02:10:30 +01:00
int len = arr . size ( ) ;
DVector < Color > : : Read r = arr . read ( ) ;
2017-03-19 00:36:26 +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 ) ;
2014-02-10 02:10:30 +01:00
}
return hash ;
} break ;
default : { }
2017-03-19 00:36:26 +01:00
}
2014-02-10 02:10:30 +01:00
return 0 ;
}
2017-02-15 14:41:16 +01:00
# define hash_compare_scalar(p_lhs, p_rhs) \
( ( p_lhs ) = = ( p_rhs ) ) | | ( Math : : is_nan ( p_lhs ) & & Math : : is_nan ( p_rhs ) )
2017-08-27 14:11:11 +02:00
# define hash_compare_vector2(p_lhs, p_rhs) \
2017-02-15 14:41:16 +01:00
( hash_compare_scalar ( ( p_lhs ) . x , ( p_rhs ) . x ) ) & & \
2017-08-27 14:11:11 +02:00
( hash_compare_scalar ( ( p_lhs ) . y , ( p_rhs ) . y ) )
# define hash_compare_vector3(p_lhs, p_rhs) \
( hash_compare_scalar ( ( p_lhs ) . x , ( p_rhs ) . x ) ) & & \
( hash_compare_scalar ( ( p_lhs ) . y , ( p_rhs ) . y ) ) & & \
( hash_compare_scalar ( ( p_lhs ) . z , ( p_rhs ) . z ) )
# define hash_compare_quat(p_lhs, p_rhs) \
( hash_compare_scalar ( ( p_lhs ) . x , ( p_rhs ) . x ) ) & & \
( hash_compare_scalar ( ( p_lhs ) . y , ( p_rhs ) . y ) ) & & \
( hash_compare_scalar ( ( p_lhs ) . z , ( p_rhs ) . z ) ) & & \
( hash_compare_scalar ( ( p_lhs ) . w , ( p_rhs ) . w ) )
# define hash_compare_color(p_lhs, p_rhs) \
( hash_compare_scalar ( ( p_lhs ) . r , ( p_rhs ) . r ) ) & & \
( hash_compare_scalar ( ( p_lhs ) . g , ( p_rhs ) . g ) ) & & \
( hash_compare_scalar ( ( p_lhs ) . b , ( p_rhs ) . b ) ) & & \
( hash_compare_scalar ( ( p_lhs ) . a , ( p_rhs ) . a ) )
# define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
const DVector < p_type > & l = * reinterpret_cast < const DVector < p_type > * > ( p_lhs ) ; \
const DVector < p_type > & r = * reinterpret_cast < const DVector < p_type > * > ( p_rhs ) ; \
\
if ( l . size ( ) ! = r . size ( ) ) \
return false ; \
\
DVector < p_type > : : Read lr = l . read ( ) ; \
DVector < p_type > : : Read rr = r . read ( ) ; \
\
for ( int i = 0 ; i < l . size ( ) ; + + i ) { \
if ( ! p_compare_func ( ( lr [ 0 ] ) , ( rr [ 0 ] ) ) ) \
return false ; \
} \
\
return true
bool Variant : : hash_compare ( const Variant & p_variant ) const {
2017-02-15 14:41:16 +01:00
if ( type ! = p_variant . type )
return false ;
2017-08-27 14:11:11 +02:00
switch ( type ) {
2017-02-15 14:41:16 +01:00
case REAL : {
return hash_compare_scalar ( _data . _real , p_variant . _data . _real ) ;
} break ;
case VECTOR2 : {
2017-08-27 14:11:11 +02:00
const Vector2 * l = reinterpret_cast < const Vector2 * > ( _data . _mem ) ;
const Vector2 * r = reinterpret_cast < const Vector2 * > ( p_variant . _data . _mem ) ;
2017-02-15 14:41:16 +01:00
return hash_compare_vector2 ( * l , * r ) ;
} break ;
case RECT2 : {
2017-08-27 14:11:11 +02:00
const Rect2 * l = reinterpret_cast < const Rect2 * > ( _data . _mem ) ;
const Rect2 * r = reinterpret_cast < const Rect2 * > ( p_variant . _data . _mem ) ;
2017-02-15 14:41:16 +01:00
return ( hash_compare_vector2 ( l - > pos , r - > pos ) ) & &
2017-08-27 14:11:11 +02:00
( hash_compare_vector2 ( l - > size , r - > size ) ) ;
2017-02-15 14:41:16 +01:00
} break ;
case MATRIX32 : {
2017-08-27 14:11:11 +02:00
Matrix32 * l = _data . _matrix32 ;
Matrix32 * r = p_variant . _data . _matrix32 ;
2017-02-15 14:41:16 +01:00
2017-08-27 14:11:11 +02:00
for ( int i = 0 ; i < 3 ; i + + ) {
if ( ! ( hash_compare_vector2 ( l - > elements [ i ] , r - > elements [ i ] ) ) )
2017-02-15 14:41:16 +01:00
return false ;
}
return true ;
} break ;
case VECTOR3 : {
2017-08-27 14:11:11 +02:00
const Vector3 * l = reinterpret_cast < const Vector3 * > ( _data . _mem ) ;
const Vector3 * r = reinterpret_cast < const Vector3 * > ( p_variant . _data . _mem ) ;
2017-02-15 14:41:16 +01:00
return hash_compare_vector3 ( * l , * r ) ;
} break ;
case PLANE : {
2017-08-27 14:11:11 +02:00
const Plane * l = reinterpret_cast < const Plane * > ( _data . _mem ) ;
const Plane * r = reinterpret_cast < const Plane * > ( p_variant . _data . _mem ) ;
2017-02-15 14:41:16 +01:00
return ( hash_compare_vector3 ( l - > normal , r - > normal ) ) & &
2017-08-27 14:11:11 +02:00
( hash_compare_scalar ( l - > d , r - > d ) ) ;
2017-02-15 14:41:16 +01:00
} break ;
case _AABB : {
2017-08-27 14:11:11 +02:00
const AABB * l = _data . _aabb ;
const AABB * r = p_variant . _data . _aabb ;
2017-02-15 14:41:16 +01:00
return ( hash_compare_vector3 ( l - > pos , r - > pos ) & &
( hash_compare_vector3 ( l - > size , r - > size ) ) ) ;
} break ;
case QUAT : {
2017-08-27 14:11:11 +02:00
const Quat * l = reinterpret_cast < const Quat * > ( _data . _mem ) ;
const Quat * r = reinterpret_cast < const Quat * > ( p_variant . _data . _mem ) ;
2017-02-15 14:41:16 +01:00
return hash_compare_quat ( * l , * r ) ;
} break ;
case MATRIX3 : {
2017-08-27 14:11:11 +02:00
const Matrix3 * l = _data . _matrix3 ;
const Matrix3 * r = p_variant . _data . _matrix3 ;
2017-02-15 14:41:16 +01:00
2017-08-27 14:11:11 +02:00
for ( int i = 0 ; i < 3 ; i + + ) {
if ( ! ( hash_compare_vector3 ( l - > elements [ i ] , r - > elements [ i ] ) ) )
2017-02-15 14:41:16 +01:00
return false ;
}
return true ;
} break ;
case TRANSFORM : {
2017-08-27 14:11:11 +02:00
const Transform * l = _data . _transform ;
const Transform * r = p_variant . _data . _transform ;
2017-02-15 14:41:16 +01:00
2017-08-27 14:11:11 +02:00
for ( int i = 0 ; i < 3 ; i + + ) {
if ( ! ( hash_compare_vector3 ( l - > basis . elements [ i ] , r - > basis . elements [ i ] ) ) )
2017-02-15 14:41:16 +01:00
return false ;
}
return hash_compare_vector3 ( l - > origin , r - > origin ) ;
} break ;
case COLOR : {
2017-08-27 14:11:11 +02:00
const Color * l = reinterpret_cast < const Color * > ( _data . _mem ) ;
const Color * r = reinterpret_cast < const Color * > ( p_variant . _data . _mem ) ;
2017-02-15 14:41:16 +01:00
return hash_compare_color ( * l , * r ) ;
} break ;
case ARRAY : {
2017-08-27 14:11:11 +02:00
const Array & l = * ( reinterpret_cast < const Array * > ( _data . _mem ) ) ;
const Array & r = * ( reinterpret_cast < const Array * > ( p_variant . _data . _mem ) ) ;
2017-02-15 14:41:16 +01:00
2017-08-27 14:11:11 +02:00
if ( l . size ( ) ! = r . size ( ) )
2017-02-15 14:41:16 +01:00
return false ;
2017-08-27 14:11:11 +02:00
for ( int i = 0 ; i < l . size ( ) ; + + i ) {
if ( ! l [ 0 ] . hash_compare ( r [ 0 ] ) )
2017-02-15 14:41:16 +01:00
return false ;
}
return true ;
} break ;
case REAL_ARRAY : {
hash_compare_pool_array ( _data . _mem , p_variant . _data . _mem , real_t , hash_compare_scalar ) ;
} break ;
case VECTOR2_ARRAY : {
hash_compare_pool_array ( _data . _mem , p_variant . _data . _mem , Vector2 , hash_compare_vector2 ) ;
} break ;
case VECTOR3_ARRAY : {
hash_compare_pool_array ( _data . _mem , p_variant . _data . _mem , Vector3 , hash_compare_vector3 ) ;
} break ;
case COLOR_ARRAY : {
hash_compare_pool_array ( _data . _mem , p_variant . _data . _mem , Color , hash_compare_color ) ;
} break ;
default :
bool v ;
Variant r ;
2017-08-27 14:11:11 +02:00
evaluate ( OP_EQUAL , * this , p_variant , r , v ) ;
2017-02-15 14:41:16 +01:00
return r ;
2017-08-27 14:11:11 +02:00
}
2017-02-15 14:41:16 +01:00
return false ;
}
2014-02-10 02:10:30 +01:00
bool Variant : : is_ref ( ) const {
2017-03-19 00:36:26 +01:00
return type = = OBJECT & & ! _get_obj ( ) . ref . is_null ( ) ;
2014-02-10 02:10:30 +01:00
}
Vector < Variant > varray ( ) {
return Vector < Variant > ( ) ;
}
2017-03-19 00:36:26 +01:00
Vector < Variant > varray ( const Variant & p_arg1 ) {
2014-02-10 02:10:30 +01:00
Vector < Variant > v ;
v . push_back ( p_arg1 ) ;
return v ;
}
2017-03-19 00:36:26 +01:00
Vector < Variant > varray ( const Variant & p_arg1 , const Variant & p_arg2 ) {
2014-02-10 02:10:30 +01:00
Vector < Variant > v ;
v . push_back ( p_arg1 ) ;
v . push_back ( p_arg2 ) ;
return v ;
}
2017-03-19 00:36:26 +01:00
Vector < Variant > varray ( const Variant & p_arg1 , const Variant & p_arg2 , const Variant & p_arg3 ) {
2014-02-10 02:10:30 +01:00
Vector < Variant > v ;
v . push_back ( p_arg1 ) ;
v . push_back ( p_arg2 ) ;
v . push_back ( p_arg3 ) ;
return v ;
}
2017-03-19 00:36:26 +01:00
Vector < Variant > varray ( const Variant & p_arg1 , const Variant & p_arg2 , const Variant & p_arg3 , const Variant & p_arg4 ) {
2014-02-10 02:10:30 +01:00
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 ;
}
2017-03-19 00:36:26 +01:00
Vector < Variant > varray ( const Variant & p_arg1 , const Variant & p_arg2 , const Variant & p_arg3 , const Variant & p_arg4 , const Variant & p_arg5 ) {
2014-02-10 02:10:30 +01:00
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 ;
}
2017-03-19 00:36:26 +01:00
void Variant : : static_assign ( const Variant & p_variant ) {
2014-02-10 02:10:30 +01:00
}
bool Variant : : is_shared ( ) const {
2017-03-19 00:36:26 +01:00
switch ( type ) {
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
case OBJECT : return true ;
case ARRAY : return reinterpret_cast < const Array * > ( _data . _mem ) - > is_shared ( ) ;
case DICTIONARY : return reinterpret_cast < const Dictionary * > ( _data . _mem ) - > is_shared ( ) ;
default : { }
}
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
return false ;
2014-02-10 02:10:30 +01:00
}
2017-03-19 00:36:26 +01:00
Variant Variant : : call ( const StringName & p_method , VARIANT_ARG_DECLARE ) {
2014-02-10 02:10:30 +01:00
VARIANT_ARGPTRS ;
2017-03-19 00:36:26 +01:00
int argc = 0 ;
for ( int i = 0 ; i < VARIANT_ARG_MAX ; i + + ) {
if ( argptr [ i ] - > get_type ( ) = = Variant : : NIL )
2014-02-10 02:10:30 +01:00
break ;
argc + + ;
}
CallError error ;
2017-03-19 00:36:26 +01:00
Variant ret = call ( p_method , argptr , argc , error ) ;
2014-02-10 02:10:30 +01:00
2017-03-19 00:36:26 +01:00
switch ( error . error ) {
2014-02-10 02:10:30 +01:00
case CallError : : CALL_ERROR_INVALID_ARGUMENT : {
2017-03-19 00:36:26 +01:00
String err = " Invalid type for argument # " + itos ( error . argument ) + " , expected ' " + Variant : : get_type_name ( error . expected ) + " '. " ;
2014-02-10 02:10:30 +01:00
ERR_PRINT ( err . utf8 ( ) . get_data ( ) ) ;
} break ;
case CallError : : CALL_ERROR_INVALID_METHOD : {
2017-03-19 00:36:26 +01:00
String err = " Invalid method ' " + p_method + " ' for type ' " + Variant : : get_type_name ( type ) + " '. " ;
2014-02-10 02:10:30 +01:00
ERR_PRINT ( err . utf8 ( ) . get_data ( ) ) ;
} break ;
case CallError : : CALL_ERROR_TOO_MANY_ARGUMENTS : {
2017-03-19 00:36:26 +01:00
String err = " Too many arguments for method ' " + p_method + " ' " ;
2014-02-10 02:10:30 +01:00
ERR_PRINT ( err . utf8 ( ) . get_data ( ) ) ;
} break ;
default : { }
}
return ret ;
}
2017-03-19 00:36:26 +01:00
void Variant : : construct_from_string ( const String & p_string , Variant & r_value , ObjectConstruct p_obj_construct , void * p_construct_ud ) {
2015-02-15 05:19:46 +01:00
2017-03-19 00:36:26 +01:00
r_value = Variant ( ) ;
2015-02-15 05:19:46 +01:00
}
2014-12-17 02:31:57 +01:00
2015-12-31 04:54:00 +01:00
String Variant : : get_construct_string ( ) const {
2014-12-17 02:31:57 +01:00
2015-12-31 04:54:00 +01:00
String vars ;
2017-03-19 00:36:26 +01:00
VariantWriter : : write_to_string ( * this , vars ) ;
2014-12-17 02:31:57 +01:00
2015-12-31 04:54:00 +01:00
return vars ;
2014-12-17 02:31:57 +01:00
}
2016-01-04 13:35:21 +01:00
2017-03-19 00:36:26 +01:00
String Variant : : get_call_error_text ( Object * p_base , const StringName & p_method , const Variant * * p_argptrs , int p_argcount , const Variant : : CallError & ce ) {
2016-01-04 13:35:21 +01:00
String err_text ;
2017-03-19 00:36:26 +01:00
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 ) {
2016-01-04 13:35:21 +01:00
return " Call OK " ;
}
String class_name = p_base - > get_type ( ) ;
Ref < Script > script = p_base - > get_script ( ) ;
if ( script . is_valid ( ) & & script - > get_path ( ) . is_resource_file ( ) ) {
2017-03-19 00:36:26 +01:00
class_name + = " ( " + script - > get_path ( ) . get_file ( ) + " ) " ;
2016-01-04 13:35:21 +01:00
}
2017-03-19 00:36:26 +01:00
return " ' " + class_name + " :: " + String ( p_method ) + " ': " + err_text ;
2016-01-04 13:35:21 +01:00
}
2016-05-17 23:27:15 +02:00
2017-03-19 00:36:26 +01:00
String vformat ( const String & p_text , const Variant & p1 , const Variant & p2 , const Variant & p3 , const Variant & p4 , const Variant & p5 ) {
2016-05-17 23:27:15 +02:00
Array args ;
2017-03-19 00:36:26 +01:00
if ( p1 . get_type ( ) ! = Variant : : NIL ) {
2016-05-17 23:27:15 +02:00
args . push_back ( p1 ) ;
2017-03-19 00:36:26 +01:00
if ( p2 . get_type ( ) ! = Variant : : NIL ) {
2016-05-17 23:27:15 +02:00
args . push_back ( p2 ) ;
2017-03-19 00:36:26 +01:00
if ( p3 . get_type ( ) ! = Variant : : NIL ) {
2016-05-17 23:27:15 +02:00
args . push_back ( p3 ) ;
2017-03-19 00:36:26 +01:00
if ( p4 . get_type ( ) ! = Variant : : NIL ) {
2016-05-17 23:27:15 +02:00
args . push_back ( p4 ) ;
2017-03-19 00:36:26 +01:00
if ( p5 . get_type ( ) ! = Variant : : NIL ) {
2016-05-17 23:27:15 +02:00
args . push_back ( p5 ) ;
}
}
}
}
}
2017-03-19 00:36:26 +01:00
bool error = false ;
String fmt = p_text . sprintf ( args , & error ) ;
2016-05-17 23:27:15 +02:00
2017-03-19 00:36:26 +01:00
ERR_FAIL_COND_V ( error , String ( ) ) ;
2016-05-17 23:27:15 +02:00
return fmt ;
}