variant.cpp 57 KB

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