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