variant.cpp 52 KB

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