variant.cpp 54 KB

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