variant.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768
  1. /*************************************************************************/
  2. /* variant.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "variant.h"
  30. #include "resource.h"
  31. #include "print_string.h"
  32. #include "scene/main/node.h"
  33. #include "scene/gui/control.h"
  34. #include "io/marshalls.h"
  35. String Variant::get_type_name(Variant::Type p_type) {
  36. switch( p_type ) {
  37. case NIL: {
  38. return "Nil";
  39. } break;
  40. // atomic types
  41. case BOOL: {
  42. return "bool";
  43. } break;
  44. case INT: {
  45. return "int";
  46. } break;
  47. case REAL: {
  48. return "float";
  49. } break;
  50. case STRING: {
  51. return "String";
  52. } break;
  53. // math types
  54. case VECTOR2: {
  55. return "Vector2";
  56. } break;
  57. case RECT2: {
  58. return "Rect2";
  59. } break;
  60. case MATRIX32: {
  61. return "Matrix32";
  62. } break;
  63. case VECTOR3: {
  64. return "Vector3";
  65. } break;
  66. case PLANE: {
  67. return "Plane";
  68. } break;
  69. /*
  70. case QUAT: {
  71. } break;*/
  72. case _AABB: {
  73. return "AABB";
  74. } break;
  75. case QUAT: {
  76. return "Quat";
  77. } break;
  78. case MATRIX3: {
  79. return "Matrix3";
  80. } break;
  81. case TRANSFORM: {
  82. return "Transform";
  83. } break;
  84. // misc types
  85. case COLOR: {
  86. return "Color";
  87. } break;
  88. case IMAGE: {
  89. return "Image";
  90. } break;
  91. case _RID: {
  92. return "RID";
  93. } break;
  94. case OBJECT: {
  95. return "Object";
  96. } break;
  97. case NODE_PATH: {
  98. return "NodePath";
  99. } break;
  100. case INPUT_EVENT: {
  101. return "InputEvent";
  102. } break;
  103. case DICTIONARY: {
  104. return "Dictionary";
  105. } break;
  106. case ARRAY: {
  107. return "Array";
  108. } break;
  109. // arrays
  110. case RAW_ARRAY: {
  111. return "RawArray";
  112. } break;
  113. case INT_ARRAY: {
  114. return "IntArray";
  115. } break;
  116. case REAL_ARRAY: {
  117. return "RealArray";
  118. } break;
  119. case STRING_ARRAY: {
  120. return "StringArray";
  121. } break;
  122. case VECTOR2_ARRAY: {
  123. return "Vector2Array";
  124. } break;
  125. case VECTOR3_ARRAY: {
  126. return "Vector3Array";
  127. } break;
  128. case COLOR_ARRAY: {
  129. return "ColorArray";
  130. } break;
  131. default: {}
  132. }
  133. return "";
  134. }
  135. bool Variant::can_convert(Variant::Type p_type_from,Variant::Type p_type_to) {
  136. if (p_type_from==p_type_to)
  137. return true;
  138. if (p_type_to==NIL && p_type_from!=NIL) //nil can convert to anything
  139. return true;
  140. if (p_type_from == NIL) {
  141. return (p_type_to == OBJECT);
  142. };
  143. const Type *valid_types=NULL;
  144. const Type *invalid_types=NULL;
  145. switch(p_type_to) {
  146. case BOOL: {
  147. static const Type valid[]={
  148. INT,
  149. REAL,
  150. STRING,
  151. NIL,
  152. };
  153. valid_types=valid;
  154. } break;
  155. case INT: {
  156. static const Type valid[]={
  157. BOOL,
  158. REAL,
  159. STRING,
  160. NIL,
  161. };
  162. valid_types=valid;
  163. } break;
  164. case REAL: {
  165. static const Type valid[]={
  166. BOOL,
  167. INT,
  168. STRING,
  169. NIL,
  170. };
  171. valid_types=valid;
  172. } break;
  173. case STRING: {
  174. static const Type invalid[]={
  175. OBJECT,
  176. IMAGE,
  177. NIL
  178. };
  179. invalid_types=invalid;
  180. } break;
  181. case MATRIX32: {
  182. static const Type invalid[]={
  183. TRANSFORM,
  184. NIL
  185. };
  186. invalid_types=invalid;
  187. } break;
  188. case QUAT: {
  189. static const Type valid[]={
  190. MATRIX3,
  191. NIL
  192. };
  193. valid_types=valid;
  194. } break;
  195. case MATRIX3: {
  196. static const Type valid[]={
  197. QUAT,
  198. NIL
  199. };
  200. valid_types=valid;
  201. } break;
  202. case TRANSFORM: {
  203. static const Type valid[]={
  204. MATRIX32,
  205. QUAT,
  206. MATRIX3,
  207. NIL
  208. };
  209. valid_types=valid;
  210. } break;
  211. case COLOR: {
  212. static const Type valid[] = {
  213. STRING,
  214. INT,
  215. NIL,
  216. };
  217. valid_types = valid;
  218. } break;
  219. case _RID: {
  220. static const Type valid[]={
  221. OBJECT,
  222. NIL
  223. };
  224. valid_types=valid;
  225. } break;
  226. case OBJECT: {
  227. static const Type valid[]={
  228. NIL
  229. };
  230. valid_types=valid;
  231. } break;
  232. case NODE_PATH: {
  233. static const Type valid[]={
  234. STRING,
  235. NIL
  236. };
  237. valid_types=valid;
  238. } break;
  239. case ARRAY: {
  240. static const Type valid[]={
  241. RAW_ARRAY,
  242. INT_ARRAY,
  243. STRING_ARRAY,
  244. REAL_ARRAY,
  245. COLOR_ARRAY,
  246. VECTOR2_ARRAY,
  247. VECTOR3_ARRAY,
  248. NIL
  249. };
  250. valid_types=valid;
  251. } break;
  252. // arrays
  253. case RAW_ARRAY: {
  254. static const Type valid[]={
  255. ARRAY,
  256. NIL
  257. };
  258. valid_types=valid;
  259. } break;
  260. case INT_ARRAY: {
  261. static const Type valid[]={
  262. ARRAY,
  263. NIL
  264. };
  265. valid_types=valid;
  266. } break;
  267. case REAL_ARRAY: {
  268. static const Type valid[]={
  269. ARRAY,
  270. NIL
  271. };
  272. valid_types=valid;
  273. } break;
  274. case STRING_ARRAY: {
  275. static const Type valid[]={
  276. ARRAY,
  277. NIL
  278. };
  279. valid_types=valid;
  280. } break;
  281. case VECTOR2_ARRAY: {
  282. static const Type valid[]={
  283. ARRAY,
  284. NIL
  285. };
  286. valid_types=valid;
  287. } break;
  288. case VECTOR3_ARRAY: {
  289. static const Type valid[]={
  290. ARRAY,
  291. NIL
  292. };
  293. valid_types=valid;
  294. } break;
  295. case COLOR_ARRAY: {
  296. static const Type valid[]={
  297. ARRAY,
  298. NIL
  299. };
  300. valid_types=valid;
  301. } break;
  302. default: {}
  303. }
  304. if (valid_types) {
  305. int i=0;
  306. while(valid_types[i]!=NIL) {
  307. if (p_type_from==valid_types[i])
  308. return true;
  309. i++;
  310. }
  311. } else if (invalid_types) {
  312. int i=0;
  313. while(invalid_types[i]!=NIL) {
  314. if (p_type_from==invalid_types[i])
  315. return false;
  316. i++;
  317. }
  318. }
  319. return false;
  320. }
  321. bool Variant::operator==(const Variant& p_variant) const {
  322. if (type!=p_variant.type) //evaluation of operator== needs to be more strict
  323. return false;
  324. bool v;
  325. Variant r;
  326. evaluate(OP_EQUAL,*this,p_variant,r,v);
  327. return r;
  328. }
  329. bool Variant::is_zero() const {
  330. switch( type ) {
  331. case NIL: {
  332. return true;
  333. } break;
  334. // atomic types
  335. case BOOL: {
  336. return _data._bool==false;
  337. } break;
  338. case INT: {
  339. return _data._int==0;
  340. } break;
  341. case REAL: {
  342. return _data._real==0;
  343. } break;
  344. case STRING: {
  345. return *reinterpret_cast<const String*>(_data._mem)==String();
  346. } break;
  347. // math types
  348. case VECTOR2: {
  349. return *reinterpret_cast<const Vector2*>(_data._mem)==Vector2();
  350. } break;
  351. case RECT2: {
  352. return *reinterpret_cast<const Rect2*>(_data._mem)==Rect2();
  353. } break;
  354. case MATRIX32: {
  355. return *_data._matrix32==Matrix32();
  356. } break;
  357. case VECTOR3: {
  358. return *reinterpret_cast<const Vector3*>(_data._mem)==Vector3();
  359. } break;
  360. case PLANE: {
  361. return *reinterpret_cast<const Plane*>(_data._mem)==Plane();
  362. } break;
  363. /*
  364. case QUAT: {
  365. } break;*/
  366. case _AABB: {
  367. return *_data._aabb==AABB();
  368. } break;
  369. case QUAT: {
  370. *reinterpret_cast<const Quat*>(_data._mem)==Quat();
  371. } break;
  372. case MATRIX3: {
  373. return *_data._matrix3==Matrix3();
  374. } break;
  375. case TRANSFORM: {
  376. return *_data._transform == Transform();
  377. } break;
  378. // misc types
  379. case COLOR: {
  380. return *reinterpret_cast<const Color*>(_data._mem)==Color();
  381. } break;
  382. case IMAGE: {
  383. return _data._image->empty();
  384. } break;
  385. case _RID: {
  386. return *reinterpret_cast<const RID*>(_data._mem)==RID();
  387. } break;
  388. case OBJECT: {
  389. return _get_obj().obj==NULL;
  390. } break;
  391. case NODE_PATH: {
  392. return reinterpret_cast<const NodePath*>(_data._mem)->is_empty();
  393. } break;
  394. case INPUT_EVENT: {
  395. return _data._input_event->type==InputEvent::NONE;
  396. } break;
  397. case DICTIONARY: {
  398. return reinterpret_cast<const Dictionary*>(_data._mem)->empty();
  399. } break;
  400. case ARRAY: {
  401. return reinterpret_cast<const Array*>(_data._mem)->empty();
  402. } break;
  403. // arrays
  404. case RAW_ARRAY: {
  405. return reinterpret_cast<const DVector<uint8_t>*>(_data._mem)->size()==0;
  406. } break;
  407. case INT_ARRAY: {
  408. return reinterpret_cast<const DVector<int>*>(_data._mem)->size()==0;
  409. } break;
  410. case REAL_ARRAY: {
  411. return reinterpret_cast<const DVector<real_t>*>(_data._mem)->size()==0;
  412. } break;
  413. case STRING_ARRAY: {
  414. return reinterpret_cast<const DVector<String>*>(_data._mem)->size()==0;
  415. } break;
  416. case VECTOR2_ARRAY: {
  417. return reinterpret_cast<const DVector<Vector2>*>(_data._mem)->size()==0;
  418. } break;
  419. case VECTOR3_ARRAY: {
  420. return reinterpret_cast<const DVector<Vector3>*>(_data._mem)->size()==0;
  421. } break;
  422. case COLOR_ARRAY: {
  423. return reinterpret_cast<const DVector<Color>*>(_data._mem)->size()==0;
  424. } break;
  425. default: {}
  426. }
  427. return false;
  428. }
  429. void Variant::reference(const Variant& p_variant) {
  430. if (this == &p_variant)
  431. return;
  432. clear();
  433. type=p_variant.type;
  434. switch( p_variant.type ) {
  435. case NIL: {
  436. // none
  437. } break;
  438. // atomic types
  439. case BOOL: {
  440. _data._bool=p_variant._data._bool;
  441. } break;
  442. case INT: {
  443. _data._int=p_variant._data._int;
  444. } break;
  445. case REAL: {
  446. _data._real=p_variant._data._real;
  447. } break;
  448. case STRING: {
  449. memnew_placement( _data._mem, String( *reinterpret_cast<const String*>(p_variant._data._mem) ) );
  450. } break;
  451. // math types
  452. case VECTOR2: {
  453. memnew_placement( _data._mem, Vector2( *reinterpret_cast<const Vector2*>(p_variant._data._mem) ) );
  454. } break;
  455. case RECT2: {
  456. memnew_placement( _data._mem, Rect2( *reinterpret_cast<const Rect2*>(p_variant._data._mem) ) );
  457. } break;
  458. case MATRIX32: {
  459. _data._matrix32 = memnew( Matrix32( *p_variant._data._matrix32 ) );
  460. } break;
  461. case VECTOR3: {
  462. memnew_placement( _data._mem, Vector3( *reinterpret_cast<const Vector3*>(p_variant._data._mem) ) );
  463. } break;
  464. case PLANE: {
  465. memnew_placement( _data._mem, Plane( *reinterpret_cast<const Plane*>(p_variant._data._mem) ) );
  466. } break;
  467. /*
  468. case QUAT: {
  469. } break;*/
  470. case _AABB: {
  471. _data._aabb = memnew( AABB( *p_variant._data._aabb ) );
  472. } break;
  473. case QUAT: {
  474. memnew_placement( _data._mem, Quat( *reinterpret_cast<const Quat*>(p_variant._data._mem) ) );
  475. } break;
  476. case MATRIX3: {
  477. _data._matrix3 = memnew( Matrix3( *p_variant._data._matrix3 ) );
  478. } break;
  479. case TRANSFORM: {
  480. _data._transform = memnew( Transform( *p_variant._data._transform ) );
  481. } break;
  482. // misc types
  483. case COLOR: {
  484. memnew_placement( _data._mem, Color( *reinterpret_cast<const Color*>(p_variant._data._mem) ) );
  485. } break;
  486. case IMAGE: {
  487. _data._image = memnew( Image( *p_variant._data._image ) );
  488. } break;
  489. case _RID: {
  490. memnew_placement( _data._mem, RID( *reinterpret_cast<const RID*>(p_variant._data._mem) ) );
  491. } break;
  492. case OBJECT: {
  493. memnew_placement( _data._mem, ObjData( p_variant._get_obj() ) );
  494. } break;
  495. case NODE_PATH: {
  496. memnew_placement( _data._mem, NodePath( *reinterpret_cast<const NodePath*>(p_variant._data._mem) ) );
  497. } break;
  498. case INPUT_EVENT: {
  499. _data._input_event= memnew( InputEvent( *p_variant._data._input_event ) );
  500. } break;
  501. case DICTIONARY: {
  502. memnew_placement( _data._mem, Dictionary( *reinterpret_cast<const Dictionary*>(p_variant._data._mem) ) );
  503. } break;
  504. case ARRAY: {
  505. memnew_placement( _data._mem, Array ( *reinterpret_cast<const Array*>(p_variant._data._mem) ) );
  506. } break;
  507. // arrays
  508. case RAW_ARRAY: {
  509. memnew_placement( _data._mem, DVector<uint8_t> ( *reinterpret_cast<const DVector<uint8_t>*>(p_variant._data._mem) ) );
  510. } break;
  511. case INT_ARRAY: {
  512. memnew_placement( _data._mem, DVector<int> ( *reinterpret_cast<const DVector<int>*>(p_variant._data._mem) ) );
  513. } break;
  514. case REAL_ARRAY: {
  515. memnew_placement( _data._mem, DVector<real_t> ( *reinterpret_cast<const DVector<real_t>*>(p_variant._data._mem) ) );
  516. } break;
  517. case STRING_ARRAY: {
  518. memnew_placement( _data._mem, DVector<String> ( *reinterpret_cast<const DVector<String>*>(p_variant._data._mem) ) );
  519. } break;
  520. case VECTOR2_ARRAY: {
  521. memnew_placement( _data._mem, DVector<Vector2> ( *reinterpret_cast<const DVector<Vector2>*>(p_variant._data._mem) ) );
  522. } break;
  523. case VECTOR3_ARRAY: {
  524. memnew_placement( _data._mem, DVector<Vector3> ( *reinterpret_cast<const DVector<Vector3>*>(p_variant._data._mem) ) );
  525. } break;
  526. case COLOR_ARRAY: {
  527. memnew_placement( _data._mem, DVector<Color> ( *reinterpret_cast<const DVector<Color>*>(p_variant._data._mem) ) );
  528. } break;
  529. default: {}
  530. }
  531. }
  532. void Variant::clear() {
  533. switch(type) {
  534. case STRING: {
  535. reinterpret_cast<String*>(_data._mem)->~String();
  536. } break;
  537. /*
  538. // no point, they don't allocate memory
  539. VECTOR3,
  540. PLANE,
  541. QUAT,
  542. COLOR,
  543. VECTOR2,
  544. RECT2
  545. */
  546. case MATRIX32: {
  547. memdelete( _data._matrix32 );
  548. } break;
  549. case _AABB: {
  550. memdelete( _data._aabb );
  551. } break;
  552. case MATRIX3: {
  553. memdelete( _data._matrix3 );
  554. } break;
  555. case TRANSFORM: {
  556. memdelete( _data._transform );
  557. } break;
  558. // misc types
  559. case IMAGE: {
  560. memdelete( _data._image );
  561. } break;
  562. case NODE_PATH: {
  563. reinterpret_cast<NodePath*>(_data._mem)->~NodePath();
  564. } break;
  565. case OBJECT: {
  566. _get_obj().obj=NULL;
  567. _get_obj().ref.unref();
  568. } break;
  569. case _RID: {
  570. // not much need probably
  571. reinterpret_cast<RID*>(_data._mem)->~RID();
  572. } break;
  573. case DICTIONARY: {
  574. reinterpret_cast<Dictionary*>(_data._mem)->~Dictionary();
  575. } break;
  576. case ARRAY: {
  577. reinterpret_cast<Array*>(_data._mem)->~Array();
  578. } break;
  579. case INPUT_EVENT: {
  580. memdelete( _data._input_event );
  581. } break;
  582. // arrays
  583. case RAW_ARRAY: {
  584. reinterpret_cast< DVector<uint8_t>* >(_data._mem)->~DVector<uint8_t>();
  585. } break;
  586. case INT_ARRAY: {
  587. reinterpret_cast< DVector<int>* >(_data._mem)->~DVector<int>();
  588. } break;
  589. case REAL_ARRAY: {
  590. reinterpret_cast< DVector<real_t>* >(_data._mem)->~DVector<real_t>();
  591. } break;
  592. case STRING_ARRAY: {
  593. reinterpret_cast< DVector<String>* >(_data._mem)->~DVector<String>();
  594. } break;
  595. case VECTOR2_ARRAY: {
  596. reinterpret_cast< DVector<Vector2>* >(_data._mem)->~DVector<Vector2>();
  597. } break;
  598. case VECTOR3_ARRAY: {
  599. reinterpret_cast< DVector<Vector3>* >(_data._mem)->~DVector<Vector3>();
  600. } break;
  601. case COLOR_ARRAY: {
  602. reinterpret_cast< DVector<Color>* >(_data._mem)->~DVector<Color>();
  603. } break;
  604. default: {} /* not needed */
  605. }
  606. type=NIL;
  607. }
  608. Variant::operator signed int() const {
  609. switch( type ) {
  610. case NIL: return 0;
  611. case BOOL: return _data._bool ? 1 : 0;
  612. case INT: return _data._int;
  613. case REAL: return _data._real;
  614. case STRING: return operator String().to_int();
  615. default: {
  616. return 0;
  617. }
  618. }
  619. return 0;
  620. }
  621. Variant::operator unsigned int() const {
  622. switch( type ) {
  623. case NIL: return 0;
  624. case BOOL: return _data._bool ? 1 : 0;
  625. case INT: return _data._int;
  626. case REAL: return _data._real;
  627. case STRING: return operator String().to_int();
  628. default: {
  629. return 0;
  630. }
  631. }
  632. return 0;
  633. }
  634. Variant::operator int64_t() const {
  635. switch( type ) {
  636. case NIL: return 0;
  637. case BOOL: return _data._bool ? 1 : 0;
  638. case INT: return _data._int;
  639. case REAL: return _data._real;
  640. case STRING: return operator String().to_int();
  641. default: {
  642. return 0;
  643. }
  644. }
  645. return 0;
  646. }
  647. /*
  648. Variant::operator long unsigned int() const {
  649. switch( type ) {
  650. case NIL: return 0;
  651. case BOOL: return _data._bool ? 1 : 0;
  652. case INT: return _data._int;
  653. case REAL: return _data._real;
  654. case STRING: return operator String().to_int();
  655. default: {
  656. return 0;
  657. }
  658. }
  659. return 0;
  660. };
  661. */
  662. Variant::operator uint64_t() const {
  663. switch( type ) {
  664. case NIL: return 0;
  665. case BOOL: return _data._bool ? 1 : 0;
  666. case INT: return _data._int;
  667. case REAL: return _data._real;
  668. case STRING: return operator String().to_int();
  669. default: {
  670. return 0;
  671. }
  672. }
  673. return 0;
  674. }
  675. #ifdef NEED_LONG_INT
  676. Variant::operator signed long() const {
  677. switch( type ) {
  678. case NIL: return 0;
  679. case BOOL: return _data._bool ? 1 : 0;
  680. case INT: return _data._int;
  681. case REAL: return _data._real;
  682. case STRING: return operator String().to_int();
  683. default: {
  684. return 0;
  685. }
  686. }
  687. return 0;
  688. };
  689. Variant::operator unsigned long() const {
  690. switch( type ) {
  691. case NIL: return 0;
  692. case BOOL: return _data._bool ? 1 : 0;
  693. case INT: return _data._int;
  694. case REAL: return _data._real;
  695. case STRING: return operator String().to_int();
  696. default: {
  697. return 0;
  698. }
  699. }
  700. return 0;
  701. };
  702. #endif
  703. Variant::operator signed short() const {
  704. switch( type ) {
  705. case NIL: return 0;
  706. case BOOL: return _data._bool ? 1 : 0;
  707. case INT: return _data._int;
  708. case REAL: return _data._real;
  709. case STRING: return operator String().to_int();
  710. default: {
  711. return 0;
  712. }
  713. }
  714. return 0;
  715. }
  716. Variant::operator unsigned short() const {
  717. switch( type ) {
  718. case NIL: return 0;
  719. case BOOL: return _data._bool ? 1 : 0;
  720. case INT: return _data._int;
  721. case REAL: return _data._real;
  722. case STRING: return operator String().to_int();
  723. default: {
  724. return 0;
  725. }
  726. }
  727. return 0;
  728. }
  729. Variant::operator signed char() const {
  730. switch( type ) {
  731. case NIL: return 0;
  732. case BOOL: return _data._bool ? 1 : 0;
  733. case INT: return _data._int;
  734. case REAL: return _data._real;
  735. case STRING: return operator String().to_int();
  736. default: {
  737. return 0;
  738. }
  739. }
  740. return 0;
  741. }
  742. Variant::operator unsigned char() const {
  743. switch( type ) {
  744. case NIL: return 0;
  745. case BOOL: return _data._bool ? 1 : 0;
  746. case INT: return _data._int;
  747. case REAL: return _data._real;
  748. case STRING: return operator String().to_int();
  749. default: {
  750. return 0;
  751. }
  752. }
  753. return 0;
  754. }
  755. #ifndef CHARTYPE_16BITS
  756. Variant::operator CharType() const {
  757. return operator unsigned int();
  758. }
  759. #endif
  760. Variant::operator float() const {
  761. switch( type ) {
  762. case NIL: return 0;
  763. case BOOL: return _data._bool ? 1.0 : 0.0;
  764. case INT: return (float)_data._int;
  765. case REAL: return _data._real;
  766. case STRING: return operator String().to_double();
  767. default: {
  768. return 0;
  769. }
  770. }
  771. return 0;
  772. }
  773. Variant::operator double() const {
  774. switch( type ) {
  775. case NIL: return 0;
  776. case BOOL: return _data._bool ? 1.0 : 0.0;
  777. case INT: return (float)_data._int;
  778. case REAL: return _data._real;
  779. case STRING: return operator String().to_double();
  780. default: {
  781. return 0;
  782. }
  783. }
  784. return true;
  785. }
  786. Variant::operator StringName() const {
  787. if (type==NODE_PATH) {
  788. return reinterpret_cast<const NodePath*>(_data._mem)->get_sname();
  789. }
  790. return StringName(operator String());
  791. }
  792. struct _VariantStrPair {
  793. String key;
  794. String value;
  795. bool operator<(const _VariantStrPair& p) const {
  796. return key < p.key;
  797. }
  798. };
  799. Variant::operator String() const {
  800. switch( type ) {
  801. case NIL: return "";
  802. case BOOL: return _data._bool ? "True" : "False";
  803. case INT: return String::num(_data._int);
  804. case REAL: return String::num(_data._real);
  805. case STRING: return *reinterpret_cast<const String*>(_data._mem);
  806. case VECTOR2: return operator Vector2();
  807. case RECT2: return operator Rect2();
  808. case MATRIX32: return operator Matrix32();
  809. case VECTOR3: return operator Vector3();
  810. case PLANE: return operator Plane();
  811. //case QUAT:
  812. case _AABB: return operator AABB();
  813. case QUAT: return operator Quat();
  814. case MATRIX3: return operator Matrix3();
  815. case TRANSFORM: return operator Transform();
  816. case NODE_PATH: return operator NodePath();
  817. case INPUT_EVENT: return operator InputEvent();
  818. case COLOR: return String::num( operator Color().r)+","+String::num( operator Color().g)+","+String::num( operator Color().b)+","+String::num( operator Color().a) ;
  819. case DICTIONARY: {
  820. const Dictionary &d =*reinterpret_cast<const Dictionary*>(_data._mem);
  821. //const String *K=NULL;
  822. String str;
  823. List<Variant> keys;
  824. d.get_key_list(&keys);
  825. Vector<_VariantStrPair> pairs;
  826. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  827. _VariantStrPair sp;
  828. sp.key=String(E->get());
  829. sp.value=d[E->get()];
  830. pairs.push_back(sp);
  831. }
  832. pairs.sort();
  833. for(int i=0;i<pairs.size();i++) {
  834. if (i>0)
  835. str+=", ";
  836. str+="("+pairs[i].key+":"+pairs[i].value+")";
  837. }
  838. return str;
  839. } break;
  840. case VECTOR3_ARRAY: {
  841. DVector<Vector3> vec = operator DVector<Vector3>();
  842. String str;
  843. for(int i=0;i<vec.size();i++) {
  844. if (i>0)
  845. str+=", ";
  846. str=str+Variant( vec[i] );
  847. }
  848. return str;
  849. } break;
  850. case STRING_ARRAY: {
  851. DVector<String> vec = operator DVector<String>();
  852. String str;
  853. for(int i=0;i<vec.size();i++) {
  854. if (i>0)
  855. str+=", ";
  856. str=str+vec[i];
  857. }
  858. return str;
  859. } break;
  860. case INT_ARRAY: {
  861. DVector<int> vec = operator DVector<int>();
  862. String str;
  863. for(int i=0;i<vec.size();i++) {
  864. if (i>0)
  865. str+=", ";
  866. str=str+itos(vec[i]);
  867. }
  868. return str;
  869. } break;
  870. case REAL_ARRAY: {
  871. DVector<real_t> vec = operator DVector<real_t>();
  872. String str;
  873. for(int i=0;i<vec.size();i++) {
  874. if (i>0)
  875. str+=", ";
  876. str=str+rtos(vec[i]);
  877. }
  878. return str;
  879. } break;
  880. case ARRAY: {
  881. Array arr = operator Array();
  882. String str;
  883. for (int i=0; i<arr.size(); i++) {
  884. if (i)
  885. str+=", ";
  886. str += String(arr[i]);
  887. };
  888. return str;
  889. } break;
  890. case OBJECT: {
  891. if (_get_obj().obj)
  892. return "["+_get_obj().obj->get_type()+":"+itos(_get_obj().obj->get_instance_ID())+"]";
  893. else
  894. return "[Object:null]";
  895. } break;
  896. default: {
  897. return "["+get_type_name(type)+"]";
  898. }
  899. }
  900. return "";
  901. }
  902. Variant::operator Vector2() const {
  903. if (type==VECTOR2)
  904. return *reinterpret_cast<const Vector2*>(_data._mem);
  905. else if (type==VECTOR3)
  906. return Vector2(reinterpret_cast<const Vector3*>(_data._mem)->x,reinterpret_cast<const Vector3*>(_data._mem)->y);
  907. else
  908. return Vector2();
  909. }
  910. Variant::operator Rect2() const {
  911. if (type==RECT2)
  912. return *reinterpret_cast<const Rect2*>(_data._mem);
  913. else
  914. return Rect2();
  915. }
  916. Variant::operator Vector3() const {
  917. if (type==VECTOR3)
  918. return *reinterpret_cast<const Vector3*>(_data._mem);
  919. else
  920. return Vector3();
  921. }
  922. Variant::operator Plane() const {
  923. if (type==PLANE)
  924. return *reinterpret_cast<const Plane*>(_data._mem);
  925. else
  926. return Plane();
  927. }
  928. Variant::operator AABB() const {
  929. if (type==_AABB)
  930. return *_data._aabb;
  931. else
  932. return AABB();
  933. }
  934. Variant::operator Matrix3() const {
  935. if (type==MATRIX3)
  936. return *_data._matrix3;
  937. else if (type==QUAT)
  938. return *reinterpret_cast<const Quat*>(_data._mem);
  939. else if (type==TRANSFORM)
  940. return _data._transform->basis;
  941. else
  942. return Matrix3();
  943. }
  944. Variant::operator Quat() const {
  945. if (type==QUAT)
  946. return *reinterpret_cast<const Quat*>(_data._mem);
  947. else if (type==MATRIX3)
  948. return *_data._matrix3;
  949. else if (type==TRANSFORM)
  950. return _data._transform->basis;
  951. else
  952. return Quat();
  953. }
  954. Variant::operator Transform() const {
  955. if (type==TRANSFORM)
  956. return *_data._transform;
  957. else if (type==MATRIX3)
  958. return Transform(*_data._matrix3,Vector3());
  959. else if (type==QUAT)
  960. return Transform(Matrix3(*reinterpret_cast<const Quat*>(_data._mem)),Vector3());
  961. else
  962. return Transform();
  963. }
  964. Variant::operator Matrix32() const {
  965. if (type==MATRIX32) {
  966. return *_data._matrix32;
  967. } else if (type==TRANSFORM) {
  968. const Transform& t = *_data._transform;;
  969. Matrix32 m;
  970. m.elements[0][0]=t.basis.elements[0][0];
  971. m.elements[0][1]=t.basis.elements[1][0];
  972. m.elements[1][0]=t.basis.elements[0][1];
  973. m.elements[1][1]=t.basis.elements[1][1];
  974. m.elements[2][0]=t.origin[0];
  975. m.elements[2][1]=t.origin[1];
  976. return m;
  977. } else
  978. return Matrix32();
  979. }
  980. Variant::operator Color() const {
  981. if (type==COLOR)
  982. return *reinterpret_cast<const Color*>(_data._mem);
  983. else
  984. return Color();
  985. }
  986. Variant::operator Image() const {
  987. if (type==IMAGE)
  988. return *_data._image;
  989. else
  990. return Image();
  991. }
  992. Variant::operator NodePath() const {
  993. if (type==NODE_PATH)
  994. return *reinterpret_cast<const NodePath*>(_data._mem);
  995. else if (type==STRING)
  996. return NodePath(operator String());
  997. else
  998. return NodePath();
  999. }
  1000. Variant::operator RefPtr() const {
  1001. if (type==OBJECT)
  1002. return _get_obj().ref;
  1003. else
  1004. return RefPtr();
  1005. }
  1006. Variant::operator RID() const {
  1007. if (type==_RID)
  1008. return *reinterpret_cast<const RID*>(_data._mem);
  1009. else if (type==OBJECT && !_get_obj().ref.is_null()) {
  1010. return _get_obj().ref.get_rid();
  1011. } else
  1012. return RID();
  1013. }
  1014. Variant::operator Object*() const {
  1015. if (type==OBJECT)
  1016. return _get_obj().obj;
  1017. else
  1018. return NULL;
  1019. }
  1020. Variant::operator Node*() const {
  1021. if (type==OBJECT)
  1022. return _get_obj().obj?_get_obj().obj->cast_to<Node>():NULL;
  1023. else
  1024. return NULL;
  1025. }
  1026. Variant::operator Control*() const {
  1027. if (type==OBJECT)
  1028. return _get_obj().obj?_get_obj().obj->cast_to<Control>():NULL;
  1029. else
  1030. return NULL;
  1031. }
  1032. Variant::operator InputEvent() const {
  1033. if (type==INPUT_EVENT)
  1034. return *reinterpret_cast<const InputEvent*>(_data._input_event);
  1035. else
  1036. return InputEvent();
  1037. }
  1038. Variant::operator Dictionary() const {
  1039. if (type==DICTIONARY)
  1040. return *reinterpret_cast<const Dictionary*>(_data._mem);
  1041. else
  1042. return Dictionary();
  1043. }
  1044. template<class DA,class SA>
  1045. inline DA _convert_array(const SA& p_array) {
  1046. DA da;
  1047. da.resize(p_array.size());
  1048. for(int i=0;i<p_array.size();i++) {
  1049. da.set( i, Variant(p_array.get(i)) );
  1050. }
  1051. return da;
  1052. }
  1053. template<class DA>
  1054. inline DA _convert_array_from_variant(const Variant& p_variant) {
  1055. switch(p_variant.get_type()) {
  1056. case Variant::ARRAY: { return _convert_array<DA,Array >( p_variant.operator Array () ); }
  1057. case Variant::RAW_ARRAY: { return _convert_array<DA,DVector<uint8_t> >( p_variant.operator DVector<uint8_t> () ); }
  1058. case Variant::INT_ARRAY: { return _convert_array<DA,DVector<int> >( p_variant.operator DVector<int> () ); }
  1059. case Variant::REAL_ARRAY: { return _convert_array<DA,DVector<real_t> >( p_variant.operator DVector<real_t> () ); }
  1060. case Variant::STRING_ARRAY: { return _convert_array<DA,DVector<String> >( p_variant.operator DVector<String> () ); }
  1061. case Variant::VECTOR2_ARRAY: { return _convert_array<DA,DVector<Vector2> >( p_variant.operator DVector<Vector2> () ); }
  1062. case Variant::VECTOR3_ARRAY: { return _convert_array<DA,DVector<Vector3> >( p_variant.operator DVector<Vector3> () ); }
  1063. case Variant::COLOR_ARRAY: { return _convert_array<DA,DVector<Color> >( p_variant.operator DVector<Color>() ); }
  1064. default: { return DA(); }
  1065. }
  1066. return DA();
  1067. }
  1068. Variant::operator Array() const {
  1069. if (type==ARRAY)
  1070. return *reinterpret_cast<const Array*>(_data._mem);
  1071. else
  1072. return _convert_array_from_variant<Array >(*this);
  1073. }
  1074. Variant::operator DVector<uint8_t>() const {
  1075. if (type==RAW_ARRAY)
  1076. return *reinterpret_cast<const DVector<uint8_t>* >(_data._mem);
  1077. else
  1078. return _convert_array_from_variant<DVector<uint8_t> >(*this);
  1079. }
  1080. Variant::operator DVector<int>() const {
  1081. if (type==INT_ARRAY)
  1082. return *reinterpret_cast<const DVector<int>* >(_data._mem);
  1083. else
  1084. return _convert_array_from_variant<DVector<int> >(*this);
  1085. }
  1086. Variant::operator DVector<real_t>() const {
  1087. if (type==REAL_ARRAY)
  1088. return *reinterpret_cast<const DVector<real_t>* >(_data._mem);
  1089. else
  1090. return _convert_array_from_variant<DVector<real_t> >(*this);
  1091. }
  1092. Variant::operator DVector<String>() const {
  1093. if (type==STRING_ARRAY)
  1094. return *reinterpret_cast<const DVector<String>* >(_data._mem);
  1095. else
  1096. return _convert_array_from_variant<DVector<String> >(*this);
  1097. }
  1098. Variant::operator DVector<Vector3>() const {
  1099. if (type==VECTOR3_ARRAY)
  1100. return *reinterpret_cast<const DVector<Vector3>* >(_data._mem);
  1101. else
  1102. return _convert_array_from_variant<DVector<Vector3> >(*this);
  1103. }
  1104. Variant::operator DVector<Vector2>() const {
  1105. if (type==VECTOR2_ARRAY)
  1106. return *reinterpret_cast<const DVector<Vector2>* >(_data._mem);
  1107. else
  1108. return _convert_array_from_variant<DVector<Vector2> >(*this);
  1109. }
  1110. Variant::operator DVector<Color>() const {
  1111. if (type==COLOR_ARRAY)
  1112. return *reinterpret_cast<const DVector<Color>* >(_data._mem);
  1113. else
  1114. return _convert_array_from_variant<DVector<Color> >(*this);
  1115. }
  1116. /* helpers */
  1117. Variant::operator Vector<RID>() const {
  1118. Array va= operator Array();
  1119. Vector<RID> rids;
  1120. rids.resize(va.size());
  1121. for(int i=0;i<rids.size();i++)
  1122. rids[i]=va[i];
  1123. return rids;
  1124. }
  1125. Variant::operator Vector<Vector2>() const {
  1126. DVector<Vector2> from=operator DVector<Vector2>();
  1127. Vector<Vector2> to;
  1128. int len=from.size();
  1129. if (len==0)
  1130. return Vector<Vector2>();
  1131. to.resize(len);
  1132. DVector<Vector2>::Read r = from.read();
  1133. Vector2 *w = &to[0];
  1134. for (int i=0;i<len;i++) {
  1135. w[i]=r[i];
  1136. }
  1137. return to;
  1138. }
  1139. Variant::operator DVector<Plane>() const {
  1140. Array va= operator Array();
  1141. DVector<Plane> planes;
  1142. int va_size=va.size();
  1143. if (va_size==0)
  1144. return planes;
  1145. planes.resize(va_size);
  1146. DVector<Plane>::Write w = planes.write();
  1147. for(int i=0;i<va_size;i++)
  1148. w[i]=va[i];
  1149. return planes;
  1150. }
  1151. Variant::operator DVector<Face3>() const {
  1152. DVector<Vector3> va= operator DVector<Vector3>();
  1153. DVector<Face3> faces;
  1154. int va_size=va.size();
  1155. if (va_size==0)
  1156. return faces;
  1157. faces.resize(va_size/3);
  1158. DVector<Face3>::Write w = faces.write();
  1159. DVector<Vector3>::Read r = va.read();
  1160. for(int i=0;i<va_size;i++)
  1161. w[i/3].vertex[i%3]=r[i];
  1162. return faces;
  1163. }
  1164. Variant::operator Vector<Plane>() const {
  1165. Array va= operator Array();
  1166. Vector<Plane> planes;
  1167. int va_size=va.size();
  1168. if (va_size==0)
  1169. return planes;
  1170. planes.resize(va_size);
  1171. for(int i=0;i<va_size;i++)
  1172. planes[i]=va[i];
  1173. return planes;
  1174. }
  1175. Variant::operator Vector<Variant>() const {
  1176. Array from=operator Array();
  1177. Vector<Variant> to;
  1178. int len=from.size();
  1179. to.resize(len);
  1180. for (int i=0;i<len;i++) {
  1181. to[i]=from[i];
  1182. }
  1183. return to;
  1184. }
  1185. Variant::operator Vector<uint8_t>() const {
  1186. DVector<uint8_t> from=operator DVector<uint8_t>();
  1187. Vector<uint8_t> to;
  1188. int len=from.size();
  1189. to.resize(len);
  1190. for (int i=0;i<len;i++) {
  1191. to[i]=from[i];
  1192. }
  1193. return to;
  1194. }
  1195. Variant::operator Vector<int>() const {
  1196. DVector<int> from=operator DVector<int>();
  1197. Vector<int> to;
  1198. int len=from.size();
  1199. to.resize(len);
  1200. for (int i=0;i<len;i++) {
  1201. to[i]=from[i];
  1202. }
  1203. return to;
  1204. }
  1205. Variant::operator Vector<real_t>() const {
  1206. DVector<real_t> from=operator DVector<real_t>();
  1207. Vector<real_t> to;
  1208. int len=from.size();
  1209. to.resize(len);
  1210. for (int i=0;i<len;i++) {
  1211. to[i]=from[i];
  1212. }
  1213. return to;
  1214. }
  1215. Variant::operator Vector<String>() const {
  1216. DVector<String> from=operator DVector<String>();
  1217. Vector<String> to;
  1218. int len=from.size();
  1219. to.resize(len);
  1220. for (int i=0;i<len;i++) {
  1221. to[i]=from[i];
  1222. }
  1223. return to;
  1224. }
  1225. Variant::operator Vector<Vector3>() const {
  1226. DVector<Vector3> from=operator DVector<Vector3>();
  1227. Vector<Vector3> to;
  1228. int len=from.size();
  1229. if (len==0)
  1230. return Vector<Vector3>();
  1231. to.resize(len);
  1232. DVector<Vector3>::Read r = from.read();
  1233. Vector3 *w = &to[0];
  1234. for (int i=0;i<len;i++) {
  1235. w[i]=r[i];
  1236. }
  1237. return to;
  1238. }
  1239. Variant::operator Vector<Color>() const {
  1240. DVector<Color> from=operator DVector<Color>();
  1241. Vector<Color> to;
  1242. int len=from.size();
  1243. if (len==0)
  1244. return Vector<Color>();
  1245. to.resize(len);
  1246. DVector<Color>::Read r = from.read();
  1247. Color *w = &to[0];
  1248. for (int i=0;i<len;i++) {
  1249. w[i]=r[i];
  1250. }
  1251. return to;
  1252. }
  1253. Variant::operator Margin() const {
  1254. return (Margin)operator int();
  1255. }
  1256. Variant::operator Orientation() const {
  1257. return (Orientation)operator int();
  1258. }
  1259. Variant::operator IP_Address() const {
  1260. if (type==REAL_ARRAY || type==INT_ARRAY || type==RAW_ARRAY) {
  1261. DVector<int> addr=operator DVector<int>();
  1262. if (addr.size()==4) {
  1263. return IP_Address(addr.get(0),addr.get(1),addr.get(2),addr.get(3));
  1264. }
  1265. }
  1266. return IP_Address( operator String() );
  1267. }
  1268. Variant::Variant(bool p_bool) {
  1269. type=BOOL;
  1270. _data._bool=p_bool;
  1271. }
  1272. /*
  1273. Variant::Variant(long unsigned int p_long) {
  1274. type=INT;
  1275. _data._int=p_long;
  1276. };
  1277. */
  1278. Variant::Variant(signed int p_int) {
  1279. type=INT;
  1280. _data._int=p_int;
  1281. }
  1282. Variant::Variant(unsigned int p_int) {
  1283. type=INT;
  1284. _data._int=p_int;
  1285. }
  1286. #ifdef NEED_LONG_INT
  1287. Variant::Variant(signed long p_int) {
  1288. type=INT;
  1289. _data._int=p_int;
  1290. }
  1291. Variant::Variant(unsigned long p_int) {
  1292. type=INT;
  1293. _data._int=p_int;
  1294. }
  1295. #endif
  1296. Variant::Variant(int64_t p_int) {
  1297. type=INT;
  1298. _data._int=p_int;
  1299. }
  1300. Variant::Variant(uint64_t p_int) {
  1301. type=INT;
  1302. _data._int=p_int;
  1303. }
  1304. Variant::Variant(signed short p_short) {
  1305. type=INT;
  1306. _data._int=p_short;
  1307. }
  1308. Variant::Variant(unsigned short p_short) {
  1309. type=INT;
  1310. _data._int=p_short;
  1311. }
  1312. Variant::Variant(signed char p_char) {
  1313. type=INT;
  1314. _data._int=p_char;
  1315. }
  1316. Variant::Variant(unsigned char p_char) {
  1317. type=INT;
  1318. _data._int=p_char;
  1319. }
  1320. Variant::Variant(float p_float) {
  1321. type=REAL;
  1322. _data._real=p_float;
  1323. }
  1324. Variant::Variant(double p_double) {
  1325. type=REAL;
  1326. _data._real=p_double;
  1327. }
  1328. Variant::Variant(const StringName& p_string) {
  1329. type=STRING;
  1330. memnew_placement( _data._mem, String( p_string.operator String() ) );
  1331. }
  1332. Variant::Variant(const String& p_string) {
  1333. type=STRING;
  1334. memnew_placement( _data._mem, String( p_string ) );
  1335. }
  1336. Variant::Variant(const char * const p_cstring) {
  1337. type=STRING;
  1338. memnew_placement( _data._mem, String( (const char*)p_cstring ) );
  1339. }
  1340. Variant::Variant(const CharType * p_wstring) {
  1341. type=STRING;
  1342. memnew_placement( _data._mem, String( p_wstring ) );
  1343. }
  1344. Variant::Variant(const Vector3& p_vector3) {
  1345. type=VECTOR3;
  1346. memnew_placement( _data._mem, Vector3( p_vector3 ) );
  1347. }
  1348. Variant::Variant(const Vector2& p_vector2) {
  1349. type=VECTOR2;
  1350. memnew_placement( _data._mem, Vector2( p_vector2 ) );
  1351. }
  1352. Variant::Variant(const Rect2& p_rect2) {
  1353. type=RECT2;
  1354. memnew_placement( _data._mem, Rect2( p_rect2 ) );
  1355. }
  1356. Variant::Variant(const Plane& p_plane) {
  1357. type=PLANE;
  1358. memnew_placement( _data._mem, Plane( p_plane ) );
  1359. }
  1360. Variant::Variant(const AABB& p_aabb) {
  1361. type=_AABB;
  1362. _data._aabb = memnew( AABB( p_aabb ) );
  1363. }
  1364. Variant::Variant(const Matrix3& p_matrix) {
  1365. type=MATRIX3;
  1366. _data._matrix3= memnew( Matrix3( p_matrix ) );
  1367. }
  1368. Variant::Variant(const Quat& p_quat) {
  1369. type=QUAT;
  1370. memnew_placement( _data._mem, Quat( p_quat ) );
  1371. }
  1372. Variant::Variant(const Transform& p_transform) {
  1373. type=TRANSFORM;
  1374. _data._transform = memnew( Transform( p_transform ) );
  1375. }
  1376. Variant::Variant(const Matrix32& p_transform) {
  1377. type=MATRIX32;
  1378. _data._matrix32 = memnew( Matrix32( p_transform ) );
  1379. }
  1380. Variant::Variant(const Color& p_color) {
  1381. type=COLOR;
  1382. memnew_placement( _data._mem, Color(p_color) );
  1383. }
  1384. Variant::Variant(const Image& p_image) {
  1385. type=IMAGE;
  1386. _data._image=memnew( Image(p_image) );
  1387. }
  1388. Variant::Variant(const NodePath& p_node_path) {
  1389. type=NODE_PATH;
  1390. memnew_placement( _data._mem, NodePath(p_node_path) );
  1391. }
  1392. Variant::Variant(const InputEvent& p_input_event) {
  1393. type=INPUT_EVENT;
  1394. _data._input_event = memnew( InputEvent(p_input_event) );
  1395. }
  1396. Variant::Variant(const RefPtr& p_resource) {
  1397. type=OBJECT;
  1398. memnew_placement( _data._mem, ObjData );
  1399. REF ref = p_resource;
  1400. _get_obj().obj=ref.ptr();
  1401. _get_obj().ref=p_resource;
  1402. }
  1403. Variant::Variant(const RID& p_rid) {
  1404. type=_RID;
  1405. memnew_placement( _data._mem, RID(p_rid) );
  1406. }
  1407. Variant::Variant(const Object* p_object) {
  1408. type=OBJECT;
  1409. memnew_placement( _data._mem, ObjData );
  1410. _get_obj().obj=const_cast<Object*>(p_object);
  1411. }
  1412. Variant::Variant(const Dictionary& p_dictionary) {
  1413. type=DICTIONARY;
  1414. memnew_placement( _data._mem, (Dictionary)( p_dictionary) );
  1415. }
  1416. Variant::Variant(const Array& p_array) {
  1417. type=ARRAY;
  1418. memnew_placement( _data._mem, Array(p_array) );
  1419. }
  1420. Variant::Variant(const DVector<Plane>& p_array) {
  1421. type=ARRAY;
  1422. Array *plane_array=memnew_placement( _data._mem, Array );
  1423. plane_array->resize( p_array.size() );
  1424. for (int i=0;i<p_array.size();i++) {
  1425. plane_array->operator [](i)=Variant(p_array[i]);
  1426. }
  1427. }
  1428. Variant::Variant(const Vector<Plane>& p_array) {
  1429. type=ARRAY;
  1430. Array *plane_array=memnew_placement( _data._mem, Array );
  1431. plane_array->resize( p_array.size() );
  1432. for (int i=0;i<p_array.size();i++) {
  1433. plane_array->operator [](i)=Variant(p_array[i]);
  1434. }
  1435. }
  1436. Variant::Variant(const Vector<RID>& p_array) {
  1437. type=ARRAY;
  1438. Array *rid_array=memnew_placement( _data._mem, Array );
  1439. rid_array->resize( p_array.size() );
  1440. for (int i=0;i<p_array.size();i++) {
  1441. rid_array->set(i,Variant(p_array[i]));
  1442. }
  1443. }
  1444. Variant::Variant(const Vector<Vector2>& p_array) {
  1445. type=NIL;
  1446. DVector<Vector2> v;
  1447. int len=p_array.size();
  1448. if (len>0) {
  1449. v.resize(len);
  1450. DVector<Vector2>::Write w = v.write();
  1451. const Vector2 *r = p_array.ptr();
  1452. for (int i=0;i<len;i++)
  1453. w[i]=r[i];
  1454. }
  1455. *this=v;
  1456. }
  1457. Variant::Variant(const DVector<uint8_t>& p_raw_array) {
  1458. type=RAW_ARRAY;
  1459. memnew_placement( _data._mem, DVector<uint8_t>(p_raw_array) );
  1460. }
  1461. Variant::Variant(const DVector<int>& p_int_array) {
  1462. type=INT_ARRAY;
  1463. memnew_placement( _data._mem, DVector<int>(p_int_array) );
  1464. }
  1465. Variant::Variant(const DVector<real_t>& p_real_array) {
  1466. type=REAL_ARRAY;
  1467. memnew_placement( _data._mem, DVector<real_t>(p_real_array) );
  1468. }
  1469. Variant::Variant(const DVector<String>& p_string_array) {
  1470. type=STRING_ARRAY;
  1471. memnew_placement( _data._mem, DVector<String>(p_string_array) );
  1472. }
  1473. Variant::Variant(const DVector<Vector3>& p_vector3_array) {
  1474. type=VECTOR3_ARRAY;
  1475. memnew_placement( _data._mem, DVector<Vector3>(p_vector3_array) );
  1476. }
  1477. Variant::Variant(const DVector<Vector2>& p_vector2_array) {
  1478. type=VECTOR2_ARRAY;
  1479. memnew_placement( _data._mem, DVector<Vector2>(p_vector2_array) );
  1480. }
  1481. Variant::Variant(const DVector<Color>& p_color_array) {
  1482. type=COLOR_ARRAY;
  1483. memnew_placement( _data._mem, DVector<Color>(p_color_array) );
  1484. }
  1485. Variant::Variant(const DVector<Face3>& p_face_array) {
  1486. DVector<Vector3> vertices;
  1487. int face_count=p_face_array.size();
  1488. vertices.resize(face_count*3);
  1489. if (face_count) {
  1490. DVector<Face3>::Read r = p_face_array.read();
  1491. DVector<Vector3>::Write w = vertices.write();
  1492. for(int i=0;i<face_count;i++) {
  1493. for(int j=0;j<3;j++)
  1494. w[i*3+j]=r[i].vertex[j];
  1495. }
  1496. r=DVector<Face3>::Read();
  1497. w=DVector<Vector3>::Write();
  1498. }
  1499. type = NIL;
  1500. *this = vertices;
  1501. }
  1502. /* helpers */
  1503. Variant::Variant(const Vector<Variant>& p_array) {
  1504. type=NIL;
  1505. Array v;
  1506. int len=p_array.size();
  1507. v.resize(len);
  1508. for (int i=0;i<len;i++)
  1509. v.set(i,p_array[i]);
  1510. *this=v;
  1511. }
  1512. Variant::Variant(const Vector<uint8_t>& p_array) {
  1513. type=NIL;
  1514. DVector<uint8_t> v;
  1515. int len=p_array.size();
  1516. v.resize(len);
  1517. for (int i=0;i<len;i++)
  1518. v.set(i,p_array[i]);
  1519. *this=v;
  1520. }
  1521. Variant::Variant(const Vector<int>& p_array) {
  1522. type=NIL;
  1523. DVector<int> v;
  1524. int len=p_array.size();
  1525. v.resize(len);
  1526. for (int i=0;i<len;i++)
  1527. v.set(i,p_array[i]);
  1528. *this=v;
  1529. }
  1530. Variant::Variant(const Vector<real_t>& p_array) {
  1531. type=NIL;
  1532. DVector<real_t> v;
  1533. int len=p_array.size();
  1534. v.resize(len);
  1535. for (int i=0;i<len;i++)
  1536. v.set(i,p_array[i]);
  1537. *this=v;
  1538. }
  1539. Variant::Variant(const Vector<String>& p_array) {
  1540. type=NIL;
  1541. DVector<String> v;
  1542. int len=p_array.size();
  1543. v.resize(len);
  1544. for (int i=0;i<len;i++)
  1545. v.set(i,p_array[i]);
  1546. *this=v;
  1547. }
  1548. Variant::Variant(const Vector<Vector3>& p_array) {
  1549. type=NIL;
  1550. DVector<Vector3> v;
  1551. int len=p_array.size();
  1552. if (len>0) {
  1553. v.resize(len);
  1554. DVector<Vector3>::Write w = v.write();
  1555. const Vector3 *r = p_array.ptr();
  1556. for (int i=0;i<len;i++)
  1557. w[i]=r[i];
  1558. }
  1559. *this=v;
  1560. }
  1561. Variant::Variant(const Vector<Color>& p_array) {
  1562. type=NIL;
  1563. DVector<Color> v;
  1564. int len=p_array.size();
  1565. v.resize(len);
  1566. for (int i=0;i<len;i++)
  1567. v.set(i,p_array[i]);
  1568. *this=v;
  1569. }
  1570. void Variant::operator=(const Variant& p_variant) {
  1571. reference(p_variant);
  1572. }
  1573. Variant::Variant(const IP_Address& p_address) {
  1574. type=STRING;
  1575. memnew_placement( _data._mem, String( p_address ) );
  1576. }
  1577. Variant::Variant(const Variant& p_variant) {
  1578. type=NIL;
  1579. reference(p_variant);
  1580. }
  1581. /*
  1582. Variant::~Variant() {
  1583. clear();
  1584. }*/
  1585. uint32_t Variant::hash() const {
  1586. switch( type ) {
  1587. case NIL: {
  1588. return 0;
  1589. } break;
  1590. case BOOL: {
  1591. return _data._bool?1:0;
  1592. } break;
  1593. case INT: {
  1594. return _data._int;
  1595. } break;
  1596. case REAL: {
  1597. MarshallFloat mf;
  1598. mf.f=_data._real;
  1599. return mf.i;
  1600. } break;
  1601. case STRING: {
  1602. return reinterpret_cast<const String*>(_data._mem)->hash();
  1603. } break;
  1604. // math types
  1605. case VECTOR2: {
  1606. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->x);
  1607. return hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->y,hash);
  1608. } break;
  1609. case RECT2: {
  1610. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.x);
  1611. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.y,hash);
  1612. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.x,hash);
  1613. return hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.y,hash);
  1614. } break;
  1615. case MATRIX32: {
  1616. uint32_t hash = 5831;
  1617. for(int i=0;i<3;i++) {
  1618. for(int j=0;j<2;j++) {
  1619. hash = hash_djb2_one_float(_data._matrix32->elements[i][j],hash);
  1620. }
  1621. }
  1622. return hash;
  1623. } break;
  1624. case VECTOR3: {
  1625. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->x);
  1626. hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->y,hash);
  1627. return hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->z,hash);
  1628. } break;
  1629. case PLANE: {
  1630. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.x);
  1631. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.y,hash);
  1632. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.z,hash);
  1633. return hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->d,hash);
  1634. } break;
  1635. /*
  1636. case QUAT: {
  1637. } break;*/
  1638. case _AABB: {
  1639. uint32_t hash = 5831;
  1640. for(int i=0;i<3;i++) {
  1641. hash = hash_djb2_one_float(_data._aabb->pos[i],hash);
  1642. hash = hash_djb2_one_float(_data._aabb->size[i],hash);
  1643. }
  1644. return hash;
  1645. } break;
  1646. case QUAT: {
  1647. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->x);
  1648. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->y,hash);
  1649. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->z,hash);
  1650. return hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->w,hash);
  1651. } break;
  1652. case MATRIX3: {
  1653. uint32_t hash = 5831;
  1654. for(int i=0;i<3;i++) {
  1655. for(int j=0;j<3;j++) {
  1656. hash = hash_djb2_one_float(_data._matrix3->elements[i][j],hash);
  1657. }
  1658. }
  1659. return hash;
  1660. } break;
  1661. case TRANSFORM: {
  1662. uint32_t hash = 5831;
  1663. for(int i=0;i<3;i++) {
  1664. for(int j=0;j<3;j++) {
  1665. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j],hash);
  1666. }
  1667. hash = hash_djb2_one_float(_data._transform->origin[i],hash);
  1668. }
  1669. return hash;
  1670. } break;
  1671. // misc types
  1672. case COLOR: {
  1673. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->r);
  1674. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->g,hash);
  1675. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->b,hash);
  1676. return hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->a,hash);
  1677. } break;
  1678. case IMAGE: {
  1679. return 0;
  1680. } break;
  1681. case _RID: {
  1682. return hash_djb2_one_64(reinterpret_cast<const RID*>(_data._mem)->get_id());
  1683. } break;
  1684. case OBJECT: {
  1685. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  1686. } break;
  1687. case NODE_PATH: {
  1688. return reinterpret_cast<const NodePath*>(_data._mem)->hash();
  1689. } break;
  1690. case INPUT_EVENT: {
  1691. return hash_djb2_buffer((uint8_t*)_data._input_event,sizeof(InputEvent));
  1692. } break;
  1693. case DICTIONARY: {
  1694. return reinterpret_cast<const Dictionary*>(_data._mem)->hash();
  1695. } break;
  1696. case ARRAY: {
  1697. const Array& arr = *reinterpret_cast<const Array* >(_data._mem);
  1698. return arr.hash();
  1699. } break;
  1700. case RAW_ARRAY: {
  1701. const DVector<uint8_t>& arr = *reinterpret_cast<const DVector<uint8_t>* >(_data._mem);
  1702. int len = arr.size();
  1703. DVector<uint8_t>::Read r = arr.read();
  1704. return hash_djb2_buffer((uint8_t*)&r[0],len);
  1705. } break;
  1706. case INT_ARRAY: {
  1707. const DVector<int>& arr = *reinterpret_cast<const DVector<int>* >(_data._mem);
  1708. int len = arr.size();
  1709. DVector<int>::Read r = arr.read();
  1710. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(int));
  1711. } break;
  1712. case REAL_ARRAY: {
  1713. const DVector<real_t>& arr = *reinterpret_cast<const DVector<real_t>* >(_data._mem);
  1714. int len = arr.size();
  1715. DVector<real_t>::Read r = arr.read();
  1716. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(real_t));
  1717. } break;
  1718. case STRING_ARRAY: {
  1719. uint32_t hash=5831;
  1720. const DVector<String>& arr = *reinterpret_cast<const DVector<String>* >(_data._mem);
  1721. int len = arr.size();
  1722. DVector<String>::Read r = arr.read();
  1723. for(int i=0;i<len;i++) {
  1724. hash = hash_djb2_one_32(r[i].hash(),hash);
  1725. }
  1726. return hash;
  1727. } break;
  1728. case VECTOR2_ARRAY: {
  1729. uint32_t hash=5831;
  1730. const DVector<Vector2>& arr = *reinterpret_cast<const DVector<Vector2>* >(_data._mem);
  1731. int len = arr.size();
  1732. DVector<Vector2>::Read r = arr.read();
  1733. for(int i=0;i<len;i++) {
  1734. hash = hash_djb2_one_float(r[i].x,hash);
  1735. hash = hash_djb2_one_float(r[i].y,hash);
  1736. }
  1737. return hash;
  1738. } break;
  1739. case VECTOR3_ARRAY: {
  1740. uint32_t hash=5831;
  1741. const DVector<Vector3>& arr = *reinterpret_cast<const DVector<Vector3>* >(_data._mem);
  1742. int len = arr.size();
  1743. DVector<Vector3>::Read r = arr.read();
  1744. for(int i=0;i<len;i++) {
  1745. hash = hash_djb2_one_float(r[i].x,hash);
  1746. hash = hash_djb2_one_float(r[i].y,hash);
  1747. hash = hash_djb2_one_float(r[i].z,hash);
  1748. }
  1749. return hash;
  1750. } break;
  1751. case COLOR_ARRAY: {
  1752. uint32_t hash=5831;
  1753. const DVector<Color>& arr = *reinterpret_cast<const DVector<Color>* >(_data._mem);
  1754. int len = arr.size();
  1755. DVector<Color>::Read r = arr.read();
  1756. for(int i=0;i<len;i++) {
  1757. hash = hash_djb2_one_float(r[i].r,hash);
  1758. hash = hash_djb2_one_float(r[i].g,hash);
  1759. hash = hash_djb2_one_float(r[i].b,hash);
  1760. hash = hash_djb2_one_float(r[i].a,hash);
  1761. }
  1762. return hash;
  1763. } break;
  1764. default: {}
  1765. }
  1766. return 0;
  1767. }
  1768. bool Variant::is_ref() const {
  1769. return type==OBJECT && !_get_obj().ref.is_null();
  1770. }
  1771. Vector<Variant> varray() {
  1772. return Vector<Variant>();
  1773. }
  1774. Vector<Variant> varray(const Variant& p_arg1) {
  1775. Vector<Variant> v;
  1776. v.push_back(p_arg1);
  1777. return v;
  1778. }
  1779. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2) {
  1780. Vector<Variant> v;
  1781. v.push_back(p_arg1);
  1782. v.push_back(p_arg2);
  1783. return v;
  1784. }
  1785. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3) {
  1786. Vector<Variant> v;
  1787. v.push_back(p_arg1);
  1788. v.push_back(p_arg2);
  1789. v.push_back(p_arg3);
  1790. return v;
  1791. }
  1792. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4) {
  1793. Vector<Variant> v;
  1794. v.push_back(p_arg1);
  1795. v.push_back(p_arg2);
  1796. v.push_back(p_arg3);
  1797. v.push_back(p_arg4);
  1798. return v;
  1799. }
  1800. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4,const Variant& p_arg5) {
  1801. Vector<Variant> v;
  1802. v.push_back(p_arg1);
  1803. v.push_back(p_arg2);
  1804. v.push_back(p_arg3);
  1805. v.push_back(p_arg4);
  1806. v.push_back(p_arg5);
  1807. return v;
  1808. }
  1809. void Variant::static_assign(const Variant& p_variant) {
  1810. }
  1811. bool Variant::is_shared() const {
  1812. switch(type) {
  1813. case OBJECT: return true;
  1814. case ARRAY: return reinterpret_cast<const Array*>(_data._mem)->is_shared();
  1815. case DICTIONARY: return reinterpret_cast<const Dictionary*>(_data._mem)->is_shared();
  1816. default: {}
  1817. }
  1818. return false;
  1819. }
  1820. Variant Variant::call(const StringName& p_method,VARIANT_ARG_DECLARE) {
  1821. VARIANT_ARGPTRS;
  1822. int argc=0;
  1823. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  1824. if (argptr[i]->get_type()==Variant::NIL)
  1825. break;
  1826. argc++;
  1827. }
  1828. CallError error;
  1829. Variant ret = call(p_method,argptr,argc,error);
  1830. switch(error.error) {
  1831. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1832. String err = "Invalid type for argument #"+itos(error.argument)+", expected '"+Variant::get_type_name(error.expected)+"'.";
  1833. ERR_PRINT(err.utf8().get_data());
  1834. } break;
  1835. case CallError::CALL_ERROR_INVALID_METHOD: {
  1836. String err = "Invalid method '"+p_method+"' for type '"+Variant::get_type_name(type)+"'.";
  1837. ERR_PRINT(err.utf8().get_data());
  1838. } break;
  1839. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1840. String err = "Too many arguments for method '"+p_method+"'";
  1841. ERR_PRINT(err.utf8().get_data());
  1842. } break;
  1843. default: {}
  1844. }
  1845. return ret;
  1846. }
  1847. void Variant::construct_from_string(const String& p_string,Variant& r_value,ObjectConstruct p_obj_construct,void *p_construct_ud) {
  1848. r_value=Variant();
  1849. }
  1850. String Variant::get_construct_string(ObjectDeConstruct p_obj_deconstruct,void *p_deconstruct_ud) const {
  1851. switch( type ) {
  1852. case NIL: return "null";
  1853. case BOOL: return _data._bool ? "true" : "false";
  1854. case INT: return String::num(_data._int);
  1855. case REAL: return String::num(_data._real);
  1856. case STRING: return "\""+reinterpret_cast<const String*>(_data._mem)->c_escape()+"\"";
  1857. case VECTOR2: return "Vector2("+operator Vector2()+")";
  1858. case RECT2: return "Rect2("+operator Rect2()+")";
  1859. case MATRIX32: return "Matrix32("+operator Matrix32()+")";
  1860. case VECTOR3: return "Vector3("+operator Vector3()+")";
  1861. case PLANE: return "Plane("+operator Plane()+")";
  1862. //case QUAT:
  1863. case _AABB: return "AABB("+operator AABB()+")";
  1864. case QUAT: return "Quat("+operator Quat()+")";
  1865. case MATRIX3: return "Matrix3("+operator Matrix3()+")";
  1866. case TRANSFORM: return "Transform("+operator Transform()+")";
  1867. case NODE_PATH: return "@\""+String(operator NodePath()).c_escape()+"\"";
  1868. case INPUT_EVENT: return "InputEvent()";
  1869. case COLOR: return "Color("+String::num( operator Color().r)+","+String::num( operator Color().g)+","+String::num( operator Color().b)+","+String::num( operator Color().a)+")" ;
  1870. case DICTIONARY: {
  1871. const Dictionary &d =*reinterpret_cast<const Dictionary*>(_data._mem);
  1872. //const String *K=NULL;
  1873. String str="{";
  1874. List<Variant> keys;
  1875. d.get_key_list(&keys);
  1876. Vector<_VariantStrPair> pairs;
  1877. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1878. _VariantStrPair sp;
  1879. sp.key=E->get().get_construct_string(p_obj_deconstruct,p_deconstruct_ud);
  1880. sp.value=d[E->get()].get_construct_string(p_obj_deconstruct,p_deconstruct_ud);
  1881. pairs.push_back(sp);
  1882. }
  1883. pairs.sort();
  1884. for(int i=0;i<pairs.size();i++) {
  1885. if (i>0)
  1886. str+=", ";
  1887. str+="("+pairs[i].key+":"+pairs[i].value+")";
  1888. }
  1889. str+="}";
  1890. return str;
  1891. } break;
  1892. case VECTOR3_ARRAY: {
  1893. DVector<Vector3> vec = operator DVector<Vector3>();
  1894. String str="Vector3Array([";
  1895. for(int i=0;i<vec.size();i++) {
  1896. if (i>0)
  1897. str+=", ";
  1898. str+=Variant( vec[i] ).get_construct_string();
  1899. }
  1900. return str+"])";
  1901. } break;
  1902. case STRING_ARRAY: {
  1903. DVector<String> vec = operator DVector<String>();
  1904. String str="StringArray([";
  1905. for(int i=0;i<vec.size();i++) {
  1906. if (i>0)
  1907. str+=", ";
  1908. str=str+=Variant( vec[i] ).get_construct_string();
  1909. }
  1910. return str+"])";
  1911. } break;
  1912. case INT_ARRAY: {
  1913. DVector<int> vec = operator DVector<int>();
  1914. String str="IntArray([";
  1915. for(int i=0;i<vec.size();i++) {
  1916. if (i>0)
  1917. str+=", ";
  1918. str=str+itos(vec[i]);
  1919. }
  1920. return str+"])";
  1921. } break;
  1922. case REAL_ARRAY: {
  1923. DVector<real_t> vec = operator DVector<real_t>();
  1924. String str="FloatArray([";
  1925. for(int i=0;i<vec.size();i++) {
  1926. if (i>0)
  1927. str+=", ";
  1928. str=str+rtos(vec[i]);
  1929. }
  1930. return str+"])";
  1931. } break;
  1932. case ARRAY: {
  1933. Array arr = operator Array();
  1934. String str="[";
  1935. for (int i=0; i<arr.size(); i++) {
  1936. if (i)
  1937. str+=", ";
  1938. str += arr[i].get_construct_string(p_obj_deconstruct,p_deconstruct_ud);
  1939. };
  1940. return str+"]";
  1941. } break;
  1942. case OBJECT: {
  1943. if (_get_obj().obj) {
  1944. if (p_obj_deconstruct) {
  1945. return "Object(\""+p_obj_deconstruct(Variant(*this),p_deconstruct_ud).c_escape()+")";
  1946. } else {
  1947. return _get_obj().obj->get_type()+".new()";
  1948. }
  1949. } else
  1950. return "null";
  1951. } break;
  1952. default: {
  1953. return "["+get_type_name(type)+"]";
  1954. }
  1955. }
  1956. }