diff options
Diffstat (limited to 'core/variant.cpp')
-rw-r--r-- | core/variant.cpp | 340 |
1 files changed, 334 insertions, 6 deletions
diff --git a/core/variant.cpp b/core/variant.cpp index 667a7d8648..e0bceb4dd8 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -5,7 +5,7 @@ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,7 +32,7 @@ #include "scene/main/node.h" #include "scene/gui/control.h" #include "io/marshalls.h" - +#include "core_string_names.h" @@ -258,12 +258,12 @@ bool Variant::can_convert(Variant::Type p_type_from,Variant::Type p_type_to) { case MATRIX32: { - static const Type invalid[]={ + static const Type valid[]={ TRANSFORM, NIL }; - invalid_types=invalid; + valid_types=valid; } break; case QUAT: { @@ -445,6 +445,256 @@ bool Variant::can_convert(Variant::Type p_type_from,Variant::Type p_type_to) { } +bool Variant::can_convert_strict(Variant::Type p_type_from,Variant::Type p_type_to) { + + if (p_type_from==p_type_to) + return true; + if (p_type_to==NIL && p_type_from!=NIL) //nil can convert to anything + return true; + + if (p_type_from == NIL) { + return (p_type_to == OBJECT); + }; + + const Type *valid_types=NULL; + const Type *invalid_types=NULL; + + switch(p_type_to) { + case BOOL: { + + static const Type valid[]={ + INT, + REAL, + //STRING, + NIL, + }; + + valid_types=valid; + } break; + case INT: { + + static const Type valid[]={ + BOOL, + REAL, + //STRING, + NIL, + }; + + valid_types=valid; + + } break; + case REAL: { + + static const Type valid[]={ + BOOL, + INT, + //STRING, + NIL, + }; + + valid_types=valid; + + } break; + case STRING: { + + + static const Type valid[]={ + NODE_PATH, + NIL + }; + + valid_types=valid; + } break; + case MATRIX32: { + + + static const Type valid[]={ + TRANSFORM, + NIL + }; + + valid_types=valid; + } break; + case QUAT: { + + static const Type valid[]={ + MATRIX3, + NIL + }; + + valid_types=valid; + + } break; + case MATRIX3: { + + static const Type valid[]={ + QUAT, + NIL + }; + + valid_types=valid; + + + } break; + case TRANSFORM: { + + static const Type valid[]={ + MATRIX32, + QUAT, + MATRIX3, + NIL + }; + + valid_types=valid; + + } break; + + case COLOR: { + + static const Type valid[] = { + STRING, + INT, + NIL, + }; + + valid_types = valid; + + } break; + + case _RID: { + + static const Type valid[]={ + OBJECT, + NIL + }; + + valid_types=valid; + } break; + case OBJECT: { + + static const Type valid[]={ + NIL + }; + + valid_types=valid; + } break; + case NODE_PATH: { + + static const Type valid[]={ + STRING, + NIL + }; + + valid_types=valid; + } break; + case ARRAY: { + + + static const Type valid[]={ + RAW_ARRAY, + INT_ARRAY, + STRING_ARRAY, + REAL_ARRAY, + COLOR_ARRAY, + VECTOR2_ARRAY, + VECTOR3_ARRAY, + NIL + }; + + valid_types=valid; + } break; + // arrays + case RAW_ARRAY: { + + static const Type valid[]={ + ARRAY, + NIL + }; + + valid_types=valid; + } break; + case INT_ARRAY: { + + static const Type valid[]={ + ARRAY, + NIL + }; + valid_types=valid; + } break; + case REAL_ARRAY: { + + static const Type valid[]={ + ARRAY, + NIL + }; + + valid_types=valid; + } break; + case STRING_ARRAY: { + + static const Type valid[]={ + ARRAY, + NIL + }; + valid_types=valid; + } break; + case VECTOR2_ARRAY: { + + static const Type valid[]={ + ARRAY, + NIL + }; + valid_types=valid; + + } break; + case VECTOR3_ARRAY: { + + static const Type valid[]={ + ARRAY, + NIL + }; + valid_types=valid; + + } break; + case COLOR_ARRAY: { + + static const Type valid[]={ + ARRAY, + NIL + }; + + valid_types=valid; + + } break; + default: {} + } + + + if (valid_types) { + + int i=0; + while(valid_types[i]!=NIL) { + + if (p_type_from==valid_types[i]) + return true; + i++; + } + } else if (invalid_types) { + + + int i=0; + while(invalid_types[i]!=NIL) { + + if (p_type_from==invalid_types[i]) + return false; + i++; + } + } + + return false; + +} + bool Variant::operator==(const Variant& p_variant) const { if (type!=p_variant.type) //evaluation of operator== needs to be more strict @@ -456,6 +706,15 @@ bool Variant::operator==(const Variant& p_variant) const { } +bool Variant::operator<(const Variant& p_variant) const { + if (type!=p_variant.type) //if types differ, then order by type first + return type<p_variant.type; + bool v; + Variant r; + evaluate(OP_LESS,*this,p_variant,r,v); + return r; +} + bool Variant::is_zero() const { switch( type ) { @@ -523,7 +782,7 @@ bool Variant::is_zero() const { } break; case QUAT: { - *reinterpret_cast<const Quat*>(_data._mem)==Quat(); + return *reinterpret_cast<const Quat*>(_data._mem)==Quat(); } break; case MATRIX3: { @@ -619,6 +878,63 @@ bool Variant::is_zero() const { return false; } + +bool Variant::is_one() const { + + switch( type ) { + case NIL: { + + return true; + } break; + + // atomic types + case BOOL: { + + return _data._bool==true; + } break; + case INT: { + + return _data._int==1; + + } break; + case REAL: { + + return _data._real==1; + + } break; + case VECTOR2: { + + return *reinterpret_cast<const Vector2*>(_data._mem)==Vector2(1,1); + + } break; + case RECT2: { + + return *reinterpret_cast<const Rect2*>(_data._mem)==Rect2(1,1,1,1); + + } break; + case VECTOR3: { + + return *reinterpret_cast<const Vector3*>(_data._mem)==Vector3(1,1,1); + + } break; + case PLANE: { + + return *reinterpret_cast<const Plane*>(_data._mem)==Plane(1,1,1,1); + + } break; + case COLOR: { + + return *reinterpret_cast<const Color*>(_data._mem)==Color(1,1,1,1); + + } break; + + default: { return !is_zero(); } + } + + return false; +} + + void Variant::reference(const Variant& p_variant) { @@ -1394,6 +1710,10 @@ Variant::operator Color() const { if (type==COLOR) return *reinterpret_cast<const Color*>(_data._mem); + else if (type==STRING) + return Color::html( operator String() ); + else if (type==INT) + return Color::hex( operator int() ); else return Color(); } @@ -1430,8 +1750,16 @@ Variant::operator RID() const { return *reinterpret_cast<const RID*>(_data._mem); else if (type==OBJECT && !_get_obj().ref.is_null()) { return _get_obj().ref.get_rid(); - } else + } else if (type==OBJECT && _get_obj().obj) { + Variant::CallError ce; + Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid,NULL,0,ce); + if (ce.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::_RID) { + return ret; + } return RID(); + } else { + return RID(); + } } Variant::operator Object*() const { |