variant.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050
  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::zero() {
  770. switch(type) {
  771. case NIL: break;
  772. case BOOL: this->_data._bool = false; break;
  773. case INT: this->_data._int = 0; break;
  774. case REAL: this->_data._real = 0; break;
  775. case VECTOR2: *reinterpret_cast<Vector2*>(this->_data._mem) = Vector2(); break;
  776. case RECT2: *reinterpret_cast<Rect2*>(this->_data._mem) = Rect2(); break;
  777. case VECTOR3: *reinterpret_cast<Vector3*>(this->_data._mem) = Vector3(); break;
  778. case PLANE: *reinterpret_cast<Plane*>(this->_data._mem) = Plane(); break;
  779. case QUAT: *reinterpret_cast<Quat*>(this->_data._mem) = Quat(); break;
  780. case COLOR: *reinterpret_cast<Color*>(this->_data._mem) = Color(); break;
  781. default: this->clear(); break;
  782. }
  783. }
  784. void Variant::clear() {
  785. switch(type) {
  786. case STRING: {
  787. reinterpret_cast<String*>(_data._mem)->~String();
  788. } break;
  789. /*
  790. // no point, they don't allocate memory
  791. VECTOR3,
  792. PLANE,
  793. QUAT,
  794. COLOR,
  795. VECTOR2,
  796. RECT2
  797. */
  798. case MATRIX32: {
  799. memdelete( _data._matrix32 );
  800. } break;
  801. case _AABB: {
  802. memdelete( _data._aabb );
  803. } break;
  804. case MATRIX3: {
  805. memdelete( _data._matrix3 );
  806. } break;
  807. case TRANSFORM: {
  808. memdelete( _data._transform );
  809. } break;
  810. // misc types
  811. case IMAGE: {
  812. memdelete( _data._image );
  813. } break;
  814. case NODE_PATH: {
  815. reinterpret_cast<NodePath*>(_data._mem)->~NodePath();
  816. } break;
  817. case OBJECT: {
  818. _get_obj().obj=NULL;
  819. _get_obj().ref.unref();
  820. } break;
  821. case _RID: {
  822. // not much need probably
  823. reinterpret_cast<RID*>(_data._mem)->~RID();
  824. } break;
  825. case DICTIONARY: {
  826. reinterpret_cast<Dictionary*>(_data._mem)->~Dictionary();
  827. } break;
  828. case ARRAY: {
  829. reinterpret_cast<Array*>(_data._mem)->~Array();
  830. } break;
  831. case INPUT_EVENT: {
  832. memdelete( _data._input_event );
  833. } break;
  834. // arrays
  835. case RAW_ARRAY: {
  836. reinterpret_cast< DVector<uint8_t>* >(_data._mem)->~DVector<uint8_t>();
  837. } break;
  838. case INT_ARRAY: {
  839. reinterpret_cast< DVector<int>* >(_data._mem)->~DVector<int>();
  840. } break;
  841. case REAL_ARRAY: {
  842. reinterpret_cast< DVector<real_t>* >(_data._mem)->~DVector<real_t>();
  843. } break;
  844. case STRING_ARRAY: {
  845. reinterpret_cast< DVector<String>* >(_data._mem)->~DVector<String>();
  846. } break;
  847. case VECTOR2_ARRAY: {
  848. reinterpret_cast< DVector<Vector2>* >(_data._mem)->~DVector<Vector2>();
  849. } break;
  850. case VECTOR3_ARRAY: {
  851. reinterpret_cast< DVector<Vector3>* >(_data._mem)->~DVector<Vector3>();
  852. } break;
  853. case COLOR_ARRAY: {
  854. reinterpret_cast< DVector<Color>* >(_data._mem)->~DVector<Color>();
  855. } break;
  856. default: {} /* not needed */
  857. }
  858. type=NIL;
  859. }
  860. Variant::operator signed int() const {
  861. switch( type ) {
  862. case NIL: return 0;
  863. case BOOL: return _data._bool ? 1 : 0;
  864. case INT: return _data._int;
  865. case REAL: return _data._real;
  866. case STRING: return operator String().to_int();
  867. default: {
  868. return 0;
  869. }
  870. }
  871. return 0;
  872. }
  873. Variant::operator unsigned int() const {
  874. switch( type ) {
  875. case NIL: return 0;
  876. case BOOL: return _data._bool ? 1 : 0;
  877. case INT: return _data._int;
  878. case REAL: return _data._real;
  879. case STRING: return operator String().to_int();
  880. default: {
  881. return 0;
  882. }
  883. }
  884. return 0;
  885. }
  886. Variant::operator int64_t() const {
  887. switch( type ) {
  888. case NIL: return 0;
  889. case BOOL: return _data._bool ? 1 : 0;
  890. case INT: return _data._int;
  891. case REAL: return _data._real;
  892. case STRING: return operator String().to_int();
  893. default: {
  894. return 0;
  895. }
  896. }
  897. return 0;
  898. }
  899. /*
  900. Variant::operator long unsigned int() const {
  901. switch( type ) {
  902. case NIL: return 0;
  903. case BOOL: return _data._bool ? 1 : 0;
  904. case INT: return _data._int;
  905. case REAL: return _data._real;
  906. case STRING: return operator String().to_int();
  907. default: {
  908. return 0;
  909. }
  910. }
  911. return 0;
  912. };
  913. */
  914. Variant::operator uint64_t() const {
  915. switch( type ) {
  916. case NIL: return 0;
  917. case BOOL: return _data._bool ? 1 : 0;
  918. case INT: return _data._int;
  919. case REAL: return _data._real;
  920. case STRING: return operator String().to_int();
  921. default: {
  922. return 0;
  923. }
  924. }
  925. return 0;
  926. }
  927. #ifdef NEED_LONG_INT
  928. Variant::operator signed long() const {
  929. switch( type ) {
  930. case NIL: return 0;
  931. case BOOL: return _data._bool ? 1 : 0;
  932. case INT: return _data._int;
  933. case REAL: return _data._real;
  934. case STRING: return operator String().to_int();
  935. default: {
  936. return 0;
  937. }
  938. }
  939. return 0;
  940. };
  941. Variant::operator unsigned long() const {
  942. switch( type ) {
  943. case NIL: return 0;
  944. case BOOL: return _data._bool ? 1 : 0;
  945. case INT: return _data._int;
  946. case REAL: return _data._real;
  947. case STRING: return operator String().to_int();
  948. default: {
  949. return 0;
  950. }
  951. }
  952. return 0;
  953. };
  954. #endif
  955. Variant::operator signed short() const {
  956. switch( type ) {
  957. case NIL: return 0;
  958. case BOOL: return _data._bool ? 1 : 0;
  959. case INT: return _data._int;
  960. case REAL: return _data._real;
  961. case STRING: return operator String().to_int();
  962. default: {
  963. return 0;
  964. }
  965. }
  966. return 0;
  967. }
  968. Variant::operator unsigned short() const {
  969. switch( type ) {
  970. case NIL: return 0;
  971. case BOOL: return _data._bool ? 1 : 0;
  972. case INT: return _data._int;
  973. case REAL: return _data._real;
  974. case STRING: return operator String().to_int();
  975. default: {
  976. return 0;
  977. }
  978. }
  979. return 0;
  980. }
  981. Variant::operator signed char() const {
  982. switch( type ) {
  983. case NIL: return 0;
  984. case BOOL: return _data._bool ? 1 : 0;
  985. case INT: return _data._int;
  986. case REAL: return _data._real;
  987. case STRING: return operator String().to_int();
  988. default: {
  989. return 0;
  990. }
  991. }
  992. return 0;
  993. }
  994. Variant::operator unsigned char() const {
  995. switch( type ) {
  996. case NIL: return 0;
  997. case BOOL: return _data._bool ? 1 : 0;
  998. case INT: return _data._int;
  999. case REAL: return _data._real;
  1000. case STRING: return operator String().to_int();
  1001. default: {
  1002. return 0;
  1003. }
  1004. }
  1005. return 0;
  1006. }
  1007. #ifndef CHARTYPE_16BITS
  1008. Variant::operator CharType() const {
  1009. return operator unsigned int();
  1010. }
  1011. #endif
  1012. Variant::operator float() const {
  1013. switch( type ) {
  1014. case NIL: return 0;
  1015. case BOOL: return _data._bool ? 1.0 : 0.0;
  1016. case INT: return (float)_data._int;
  1017. case REAL: return _data._real;
  1018. case STRING: return operator String().to_double();
  1019. default: {
  1020. return 0;
  1021. }
  1022. }
  1023. return 0;
  1024. }
  1025. Variant::operator double() const {
  1026. switch( type ) {
  1027. case NIL: return 0;
  1028. case BOOL: return _data._bool ? 1.0 : 0.0;
  1029. case INT: return (float)_data._int;
  1030. case REAL: return _data._real;
  1031. case STRING: return operator String().to_double();
  1032. default: {
  1033. return 0;
  1034. }
  1035. }
  1036. return true;
  1037. }
  1038. Variant::operator StringName() const {
  1039. if (type==NODE_PATH) {
  1040. return reinterpret_cast<const NodePath*>(_data._mem)->get_sname();
  1041. }
  1042. return StringName(operator String());
  1043. }
  1044. struct _VariantStrPair {
  1045. String key;
  1046. String value;
  1047. bool operator<(const _VariantStrPair& p) const {
  1048. return key < p.key;
  1049. }
  1050. };
  1051. Variant::operator String() const {
  1052. switch( type ) {
  1053. case NIL: return "";
  1054. case BOOL: return _data._bool ? "True" : "False";
  1055. case INT: return String::num(_data._int);
  1056. case REAL: return String::num(_data._real);
  1057. case STRING: return *reinterpret_cast<const String*>(_data._mem);
  1058. case VECTOR2: return operator Vector2();
  1059. case RECT2: return operator Rect2();
  1060. case MATRIX32: return operator Matrix32();
  1061. case VECTOR3: return operator Vector3();
  1062. case PLANE: return operator Plane();
  1063. //case QUAT:
  1064. case _AABB: return operator AABB();
  1065. case QUAT: return operator Quat();
  1066. case MATRIX3: return operator Matrix3();
  1067. case TRANSFORM: return operator Transform();
  1068. case NODE_PATH: return operator NodePath();
  1069. case INPUT_EVENT: return operator InputEvent();
  1070. case COLOR: return String::num( operator Color().r)+","+String::num( operator Color().g)+","+String::num( operator Color().b)+","+String::num( operator Color().a) ;
  1071. case DICTIONARY: {
  1072. const Dictionary &d =*reinterpret_cast<const Dictionary*>(_data._mem);
  1073. //const String *K=NULL;
  1074. String str;
  1075. List<Variant> keys;
  1076. d.get_key_list(&keys);
  1077. Vector<_VariantStrPair> pairs;
  1078. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1079. _VariantStrPair sp;
  1080. sp.key=String(E->get());
  1081. sp.value=d[E->get()];
  1082. pairs.push_back(sp);
  1083. }
  1084. pairs.sort();
  1085. for(int i=0;i<pairs.size();i++) {
  1086. if (i>0)
  1087. str+=", ";
  1088. str+="("+pairs[i].key+":"+pairs[i].value+")";
  1089. }
  1090. return str;
  1091. } break;
  1092. case VECTOR2_ARRAY: {
  1093. DVector<Vector2> vec = operator DVector<Vector2>();
  1094. String str;
  1095. for(int i=0;i<vec.size();i++) {
  1096. if (i>0)
  1097. str+=", ";
  1098. str=str+Variant( vec[i] );
  1099. }
  1100. return str;
  1101. } break;
  1102. case VECTOR3_ARRAY: {
  1103. DVector<Vector3> vec = operator DVector<Vector3>();
  1104. String str;
  1105. for(int i=0;i<vec.size();i++) {
  1106. if (i>0)
  1107. str+=", ";
  1108. str=str+Variant( vec[i] );
  1109. }
  1110. return str;
  1111. } break;
  1112. case STRING_ARRAY: {
  1113. DVector<String> vec = operator DVector<String>();
  1114. String str;
  1115. for(int i=0;i<vec.size();i++) {
  1116. if (i>0)
  1117. str+=", ";
  1118. str=str+vec[i];
  1119. }
  1120. return str;
  1121. } break;
  1122. case INT_ARRAY: {
  1123. DVector<int> vec = operator DVector<int>();
  1124. String str;
  1125. for(int i=0;i<vec.size();i++) {
  1126. if (i>0)
  1127. str+=", ";
  1128. str=str+itos(vec[i]);
  1129. }
  1130. return str;
  1131. } break;
  1132. case REAL_ARRAY: {
  1133. DVector<real_t> vec = operator DVector<real_t>();
  1134. String str;
  1135. for(int i=0;i<vec.size();i++) {
  1136. if (i>0)
  1137. str+=", ";
  1138. str=str+rtos(vec[i]);
  1139. }
  1140. return str;
  1141. } break;
  1142. case ARRAY: {
  1143. Array arr = operator Array();
  1144. String str;
  1145. for (int i=0; i<arr.size(); i++) {
  1146. if (i)
  1147. str+=", ";
  1148. str += String(arr[i]);
  1149. };
  1150. return str;
  1151. } break;
  1152. case OBJECT: {
  1153. if (_get_obj().obj) {
  1154. #ifdef DEBUG_ENABLED
  1155. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null()) {
  1156. //only if debugging!
  1157. if (!ObjectDB::instance_validate(_get_obj().obj)) {
  1158. return "[Deleted Object]";
  1159. };
  1160. };
  1161. #endif
  1162. return "["+_get_obj().obj->get_type()+":"+itos(_get_obj().obj->get_instance_ID())+"]";
  1163. } else
  1164. return "[Object:null]";
  1165. } break;
  1166. default: {
  1167. return "["+get_type_name(type)+"]";
  1168. }
  1169. }
  1170. return "";
  1171. }
  1172. Variant::operator Vector2() const {
  1173. if (type==VECTOR2)
  1174. return *reinterpret_cast<const Vector2*>(_data._mem);
  1175. else if (type==VECTOR3)
  1176. return Vector2(reinterpret_cast<const Vector3*>(_data._mem)->x,reinterpret_cast<const Vector3*>(_data._mem)->y);
  1177. else
  1178. return Vector2();
  1179. }
  1180. Variant::operator Rect2() const {
  1181. if (type==RECT2)
  1182. return *reinterpret_cast<const Rect2*>(_data._mem);
  1183. else
  1184. return Rect2();
  1185. }
  1186. Variant::operator Vector3() const {
  1187. if (type==VECTOR3)
  1188. return *reinterpret_cast<const Vector3*>(_data._mem);
  1189. else
  1190. return Vector3();
  1191. }
  1192. Variant::operator Plane() const {
  1193. if (type==PLANE)
  1194. return *reinterpret_cast<const Plane*>(_data._mem);
  1195. else
  1196. return Plane();
  1197. }
  1198. Variant::operator AABB() const {
  1199. if (type==_AABB)
  1200. return *_data._aabb;
  1201. else
  1202. return AABB();
  1203. }
  1204. Variant::operator Matrix3() const {
  1205. if (type==MATRIX3)
  1206. return *_data._matrix3;
  1207. else if (type==QUAT)
  1208. return *reinterpret_cast<const Quat*>(_data._mem);
  1209. else if (type==TRANSFORM)
  1210. return _data._transform->basis;
  1211. else
  1212. return Matrix3();
  1213. }
  1214. Variant::operator Quat() const {
  1215. if (type==QUAT)
  1216. return *reinterpret_cast<const Quat*>(_data._mem);
  1217. else if (type==MATRIX3)
  1218. return *_data._matrix3;
  1219. else if (type==TRANSFORM)
  1220. return _data._transform->basis;
  1221. else
  1222. return Quat();
  1223. }
  1224. Variant::operator Transform() const {
  1225. if (type==TRANSFORM)
  1226. return *_data._transform;
  1227. else if (type==MATRIX3)
  1228. return Transform(*_data._matrix3,Vector3());
  1229. else if (type==QUAT)
  1230. return Transform(Matrix3(*reinterpret_cast<const Quat*>(_data._mem)),Vector3());
  1231. else
  1232. return Transform();
  1233. }
  1234. Variant::operator Matrix32() const {
  1235. if (type==MATRIX32) {
  1236. return *_data._matrix32;
  1237. } else if (type==TRANSFORM) {
  1238. const Transform& t = *_data._transform;;
  1239. Matrix32 m;
  1240. m.elements[0][0]=t.basis.elements[0][0];
  1241. m.elements[0][1]=t.basis.elements[1][0];
  1242. m.elements[1][0]=t.basis.elements[0][1];
  1243. m.elements[1][1]=t.basis.elements[1][1];
  1244. m.elements[2][0]=t.origin[0];
  1245. m.elements[2][1]=t.origin[1];
  1246. return m;
  1247. } else
  1248. return Matrix32();
  1249. }
  1250. Variant::operator Color() const {
  1251. if (type==COLOR)
  1252. return *reinterpret_cast<const Color*>(_data._mem);
  1253. else if (type==STRING)
  1254. return Color::html( operator String() );
  1255. else if (type==INT)
  1256. return Color::hex( operator int() );
  1257. else
  1258. return Color();
  1259. }
  1260. Variant::operator Image() const {
  1261. if (type==IMAGE)
  1262. return *_data._image;
  1263. else
  1264. return Image();
  1265. }
  1266. Variant::operator NodePath() const {
  1267. if (type==NODE_PATH)
  1268. return *reinterpret_cast<const NodePath*>(_data._mem);
  1269. else if (type==STRING)
  1270. return NodePath(operator String());
  1271. else
  1272. return NodePath();
  1273. }
  1274. Variant::operator RefPtr() const {
  1275. if (type==OBJECT)
  1276. return _get_obj().ref;
  1277. else
  1278. return RefPtr();
  1279. }
  1280. Variant::operator RID() const {
  1281. if (type==_RID)
  1282. return *reinterpret_cast<const RID*>(_data._mem);
  1283. else if (type==OBJECT && !_get_obj().ref.is_null()) {
  1284. return _get_obj().ref.get_rid();
  1285. } else if (type==OBJECT && _get_obj().obj) {
  1286. Variant::CallError ce;
  1287. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid,NULL,0,ce);
  1288. if (ce.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::_RID) {
  1289. return ret;
  1290. }
  1291. return RID();
  1292. } else {
  1293. return RID();
  1294. }
  1295. }
  1296. Variant::operator Object*() const {
  1297. if (type==OBJECT)
  1298. return _get_obj().obj;
  1299. else
  1300. return NULL;
  1301. }
  1302. Variant::operator Node*() const {
  1303. if (type==OBJECT)
  1304. return _get_obj().obj?_get_obj().obj->cast_to<Node>():NULL;
  1305. else
  1306. return NULL;
  1307. }
  1308. Variant::operator Control*() const {
  1309. if (type==OBJECT)
  1310. return _get_obj().obj?_get_obj().obj->cast_to<Control>():NULL;
  1311. else
  1312. return NULL;
  1313. }
  1314. Variant::operator InputEvent() const {
  1315. if (type==INPUT_EVENT)
  1316. return *reinterpret_cast<const InputEvent*>(_data._input_event);
  1317. else
  1318. return InputEvent();
  1319. }
  1320. Variant::operator Dictionary() const {
  1321. if (type==DICTIONARY)
  1322. return *reinterpret_cast<const Dictionary*>(_data._mem);
  1323. else
  1324. return Dictionary();
  1325. }
  1326. template<class DA,class SA>
  1327. inline DA _convert_array(const SA& p_array) {
  1328. DA da;
  1329. da.resize(p_array.size());
  1330. for(int i=0;i<p_array.size();i++) {
  1331. da.set( i, Variant(p_array.get(i)) );
  1332. }
  1333. return da;
  1334. }
  1335. template<class DA>
  1336. inline DA _convert_array_from_variant(const Variant& p_variant) {
  1337. switch(p_variant.get_type()) {
  1338. case Variant::ARRAY: { return _convert_array<DA,Array >( p_variant.operator Array () ); }
  1339. case Variant::RAW_ARRAY: { return _convert_array<DA,DVector<uint8_t> >( p_variant.operator DVector<uint8_t> () ); }
  1340. case Variant::INT_ARRAY: { return _convert_array<DA,DVector<int> >( p_variant.operator DVector<int> () ); }
  1341. case Variant::REAL_ARRAY: { return _convert_array<DA,DVector<real_t> >( p_variant.operator DVector<real_t> () ); }
  1342. case Variant::STRING_ARRAY: { return _convert_array<DA,DVector<String> >( p_variant.operator DVector<String> () ); }
  1343. case Variant::VECTOR2_ARRAY: { return _convert_array<DA,DVector<Vector2> >( p_variant.operator DVector<Vector2> () ); }
  1344. case Variant::VECTOR3_ARRAY: { return _convert_array<DA,DVector<Vector3> >( p_variant.operator DVector<Vector3> () ); }
  1345. case Variant::COLOR_ARRAY: { return _convert_array<DA,DVector<Color> >( p_variant.operator DVector<Color>() ); }
  1346. default: { return DA(); }
  1347. }
  1348. return DA();
  1349. }
  1350. Variant::operator Array() const {
  1351. if (type==ARRAY)
  1352. return *reinterpret_cast<const Array*>(_data._mem);
  1353. else
  1354. return _convert_array_from_variant<Array >(*this);
  1355. }
  1356. Variant::operator DVector<uint8_t>() const {
  1357. if (type==RAW_ARRAY)
  1358. return *reinterpret_cast<const DVector<uint8_t>* >(_data._mem);
  1359. else
  1360. return _convert_array_from_variant<DVector<uint8_t> >(*this);
  1361. }
  1362. Variant::operator DVector<int>() const {
  1363. if (type==INT_ARRAY)
  1364. return *reinterpret_cast<const DVector<int>* >(_data._mem);
  1365. else
  1366. return _convert_array_from_variant<DVector<int> >(*this);
  1367. }
  1368. Variant::operator DVector<real_t>() const {
  1369. if (type==REAL_ARRAY)
  1370. return *reinterpret_cast<const DVector<real_t>* >(_data._mem);
  1371. else
  1372. return _convert_array_from_variant<DVector<real_t> >(*this);
  1373. }
  1374. Variant::operator DVector<String>() const {
  1375. if (type==STRING_ARRAY)
  1376. return *reinterpret_cast<const DVector<String>* >(_data._mem);
  1377. else
  1378. return _convert_array_from_variant<DVector<String> >(*this);
  1379. }
  1380. Variant::operator DVector<Vector3>() const {
  1381. if (type==VECTOR3_ARRAY)
  1382. return *reinterpret_cast<const DVector<Vector3>* >(_data._mem);
  1383. else
  1384. return _convert_array_from_variant<DVector<Vector3> >(*this);
  1385. }
  1386. Variant::operator DVector<Vector2>() const {
  1387. if (type==VECTOR2_ARRAY)
  1388. return *reinterpret_cast<const DVector<Vector2>* >(_data._mem);
  1389. else
  1390. return _convert_array_from_variant<DVector<Vector2> >(*this);
  1391. }
  1392. Variant::operator DVector<Color>() const {
  1393. if (type==COLOR_ARRAY)
  1394. return *reinterpret_cast<const DVector<Color>* >(_data._mem);
  1395. else
  1396. return _convert_array_from_variant<DVector<Color> >(*this);
  1397. }
  1398. /* helpers */
  1399. Variant::operator Vector<RID>() const {
  1400. Array va= operator Array();
  1401. Vector<RID> rids;
  1402. rids.resize(va.size());
  1403. for(int i=0;i<rids.size();i++)
  1404. rids[i]=va[i];
  1405. return rids;
  1406. }
  1407. Variant::operator Vector<Vector2>() const {
  1408. DVector<Vector2> from=operator DVector<Vector2>();
  1409. Vector<Vector2> to;
  1410. int len=from.size();
  1411. if (len==0)
  1412. return Vector<Vector2>();
  1413. to.resize(len);
  1414. DVector<Vector2>::Read r = from.read();
  1415. Vector2 *w = &to[0];
  1416. for (int i=0;i<len;i++) {
  1417. w[i]=r[i];
  1418. }
  1419. return to;
  1420. }
  1421. Variant::operator DVector<Plane>() const {
  1422. Array va= operator Array();
  1423. DVector<Plane> planes;
  1424. int va_size=va.size();
  1425. if (va_size==0)
  1426. return planes;
  1427. planes.resize(va_size);
  1428. DVector<Plane>::Write w = planes.write();
  1429. for(int i=0;i<va_size;i++)
  1430. w[i]=va[i];
  1431. return planes;
  1432. }
  1433. Variant::operator DVector<Face3>() const {
  1434. DVector<Vector3> va= operator DVector<Vector3>();
  1435. DVector<Face3> faces;
  1436. int va_size=va.size();
  1437. if (va_size==0)
  1438. return faces;
  1439. faces.resize(va_size/3);
  1440. DVector<Face3>::Write w = faces.write();
  1441. DVector<Vector3>::Read r = va.read();
  1442. for(int i=0;i<va_size;i++)
  1443. w[i/3].vertex[i%3]=r[i];
  1444. return faces;
  1445. }
  1446. Variant::operator Vector<Plane>() const {
  1447. Array va= operator Array();
  1448. Vector<Plane> planes;
  1449. int va_size=va.size();
  1450. if (va_size==0)
  1451. return planes;
  1452. planes.resize(va_size);
  1453. for(int i=0;i<va_size;i++)
  1454. planes[i]=va[i];
  1455. return planes;
  1456. }
  1457. Variant::operator Vector<Variant>() const {
  1458. Array from=operator Array();
  1459. Vector<Variant> to;
  1460. int len=from.size();
  1461. to.resize(len);
  1462. for (int i=0;i<len;i++) {
  1463. to[i]=from[i];
  1464. }
  1465. return to;
  1466. }
  1467. Variant::operator Vector<uint8_t>() const {
  1468. DVector<uint8_t> from=operator DVector<uint8_t>();
  1469. Vector<uint8_t> to;
  1470. int len=from.size();
  1471. to.resize(len);
  1472. for (int i=0;i<len;i++) {
  1473. to[i]=from[i];
  1474. }
  1475. return to;
  1476. }
  1477. Variant::operator Vector<int>() const {
  1478. DVector<int> from=operator DVector<int>();
  1479. Vector<int> to;
  1480. int len=from.size();
  1481. to.resize(len);
  1482. for (int i=0;i<len;i++) {
  1483. to[i]=from[i];
  1484. }
  1485. return to;
  1486. }
  1487. Variant::operator Vector<real_t>() const {
  1488. DVector<real_t> from=operator DVector<real_t>();
  1489. Vector<real_t> to;
  1490. int len=from.size();
  1491. to.resize(len);
  1492. for (int i=0;i<len;i++) {
  1493. to[i]=from[i];
  1494. }
  1495. return to;
  1496. }
  1497. Variant::operator Vector<String>() const {
  1498. DVector<String> from=operator DVector<String>();
  1499. Vector<String> to;
  1500. int len=from.size();
  1501. to.resize(len);
  1502. for (int i=0;i<len;i++) {
  1503. to[i]=from[i];
  1504. }
  1505. return to;
  1506. }
  1507. Variant::operator Vector<Vector3>() const {
  1508. DVector<Vector3> from=operator DVector<Vector3>();
  1509. Vector<Vector3> to;
  1510. int len=from.size();
  1511. if (len==0)
  1512. return Vector<Vector3>();
  1513. to.resize(len);
  1514. DVector<Vector3>::Read r = from.read();
  1515. Vector3 *w = &to[0];
  1516. for (int i=0;i<len;i++) {
  1517. w[i]=r[i];
  1518. }
  1519. return to;
  1520. }
  1521. Variant::operator Vector<Color>() const {
  1522. DVector<Color> from=operator DVector<Color>();
  1523. Vector<Color> to;
  1524. int len=from.size();
  1525. if (len==0)
  1526. return Vector<Color>();
  1527. to.resize(len);
  1528. DVector<Color>::Read r = from.read();
  1529. Color *w = &to[0];
  1530. for (int i=0;i<len;i++) {
  1531. w[i]=r[i];
  1532. }
  1533. return to;
  1534. }
  1535. Variant::operator Margin() const {
  1536. return (Margin)operator int();
  1537. }
  1538. Variant::operator Orientation() const {
  1539. return (Orientation)operator int();
  1540. }
  1541. Variant::operator IP_Address() const {
  1542. if (type==REAL_ARRAY || type==INT_ARRAY || type==RAW_ARRAY) {
  1543. DVector<int> addr=operator DVector<int>();
  1544. if (addr.size()==4) {
  1545. return IP_Address(addr.get(0),addr.get(1),addr.get(2),addr.get(3));
  1546. }
  1547. }
  1548. return IP_Address( operator String() );
  1549. }
  1550. Variant::Variant(bool p_bool) {
  1551. type=BOOL;
  1552. _data._bool=p_bool;
  1553. }
  1554. /*
  1555. Variant::Variant(long unsigned int p_long) {
  1556. type=INT;
  1557. _data._int=p_long;
  1558. };
  1559. */
  1560. Variant::Variant(signed int p_int) {
  1561. type=INT;
  1562. _data._int=p_int;
  1563. }
  1564. Variant::Variant(unsigned int p_int) {
  1565. type=INT;
  1566. _data._int=p_int;
  1567. }
  1568. #ifdef NEED_LONG_INT
  1569. Variant::Variant(signed long p_int) {
  1570. type=INT;
  1571. _data._int=p_int;
  1572. }
  1573. Variant::Variant(unsigned long p_int) {
  1574. type=INT;
  1575. _data._int=p_int;
  1576. }
  1577. #endif
  1578. Variant::Variant(int64_t p_int) {
  1579. type=INT;
  1580. _data._int=p_int;
  1581. }
  1582. Variant::Variant(uint64_t p_int) {
  1583. type=INT;
  1584. _data._int=p_int;
  1585. }
  1586. Variant::Variant(signed short p_short) {
  1587. type=INT;
  1588. _data._int=p_short;
  1589. }
  1590. Variant::Variant(unsigned short p_short) {
  1591. type=INT;
  1592. _data._int=p_short;
  1593. }
  1594. Variant::Variant(signed char p_char) {
  1595. type=INT;
  1596. _data._int=p_char;
  1597. }
  1598. Variant::Variant(unsigned char p_char) {
  1599. type=INT;
  1600. _data._int=p_char;
  1601. }
  1602. Variant::Variant(float p_float) {
  1603. type=REAL;
  1604. _data._real=p_float;
  1605. }
  1606. Variant::Variant(double p_double) {
  1607. type=REAL;
  1608. _data._real=p_double;
  1609. }
  1610. Variant::Variant(const StringName& p_string) {
  1611. type=STRING;
  1612. memnew_placement( _data._mem, String( p_string.operator String() ) );
  1613. }
  1614. Variant::Variant(const String& p_string) {
  1615. type=STRING;
  1616. memnew_placement( _data._mem, String( p_string ) );
  1617. }
  1618. Variant::Variant(const char * const p_cstring) {
  1619. type=STRING;
  1620. memnew_placement( _data._mem, String( (const char*)p_cstring ) );
  1621. }
  1622. Variant::Variant(const CharType * p_wstring) {
  1623. type=STRING;
  1624. memnew_placement( _data._mem, String( p_wstring ) );
  1625. }
  1626. Variant::Variant(const Vector3& p_vector3) {
  1627. type=VECTOR3;
  1628. memnew_placement( _data._mem, Vector3( p_vector3 ) );
  1629. }
  1630. Variant::Variant(const Vector2& p_vector2) {
  1631. type=VECTOR2;
  1632. memnew_placement( _data._mem, Vector2( p_vector2 ) );
  1633. }
  1634. Variant::Variant(const Rect2& p_rect2) {
  1635. type=RECT2;
  1636. memnew_placement( _data._mem, Rect2( p_rect2 ) );
  1637. }
  1638. Variant::Variant(const Plane& p_plane) {
  1639. type=PLANE;
  1640. memnew_placement( _data._mem, Plane( p_plane ) );
  1641. }
  1642. Variant::Variant(const AABB& p_aabb) {
  1643. type=_AABB;
  1644. _data._aabb = memnew( AABB( p_aabb ) );
  1645. }
  1646. Variant::Variant(const Matrix3& p_matrix) {
  1647. type=MATRIX3;
  1648. _data._matrix3= memnew( Matrix3( p_matrix ) );
  1649. }
  1650. Variant::Variant(const Quat& p_quat) {
  1651. type=QUAT;
  1652. memnew_placement( _data._mem, Quat( p_quat ) );
  1653. }
  1654. Variant::Variant(const Transform& p_transform) {
  1655. type=TRANSFORM;
  1656. _data._transform = memnew( Transform( p_transform ) );
  1657. }
  1658. Variant::Variant(const Matrix32& p_transform) {
  1659. type=MATRIX32;
  1660. _data._matrix32 = memnew( Matrix32( p_transform ) );
  1661. }
  1662. Variant::Variant(const Color& p_color) {
  1663. type=COLOR;
  1664. memnew_placement( _data._mem, Color(p_color) );
  1665. }
  1666. Variant::Variant(const Image& p_image) {
  1667. type=IMAGE;
  1668. _data._image=memnew( Image(p_image) );
  1669. }
  1670. Variant::Variant(const NodePath& p_node_path) {
  1671. type=NODE_PATH;
  1672. memnew_placement( _data._mem, NodePath(p_node_path) );
  1673. }
  1674. Variant::Variant(const InputEvent& p_input_event) {
  1675. type=INPUT_EVENT;
  1676. _data._input_event = memnew( InputEvent(p_input_event) );
  1677. }
  1678. Variant::Variant(const RefPtr& p_resource) {
  1679. type=OBJECT;
  1680. memnew_placement( _data._mem, ObjData );
  1681. REF ref = p_resource;
  1682. _get_obj().obj=ref.ptr();
  1683. _get_obj().ref=p_resource;
  1684. }
  1685. Variant::Variant(const RID& p_rid) {
  1686. type=_RID;
  1687. memnew_placement( _data._mem, RID(p_rid) );
  1688. }
  1689. Variant::Variant(const Object* p_object) {
  1690. type=OBJECT;
  1691. memnew_placement( _data._mem, ObjData );
  1692. _get_obj().obj=const_cast<Object*>(p_object);
  1693. }
  1694. Variant::Variant(const Dictionary& p_dictionary) {
  1695. type=DICTIONARY;
  1696. memnew_placement( _data._mem, (Dictionary)( p_dictionary) );
  1697. }
  1698. Variant::Variant(const Array& p_array) {
  1699. type=ARRAY;
  1700. memnew_placement( _data._mem, Array(p_array) );
  1701. }
  1702. Variant::Variant(const DVector<Plane>& p_array) {
  1703. type=ARRAY;
  1704. Array *plane_array=memnew_placement( _data._mem, Array );
  1705. plane_array->resize( p_array.size() );
  1706. for (int i=0;i<p_array.size();i++) {
  1707. plane_array->operator [](i)=Variant(p_array[i]);
  1708. }
  1709. }
  1710. Variant::Variant(const Vector<Plane>& p_array) {
  1711. type=ARRAY;
  1712. Array *plane_array=memnew_placement( _data._mem, Array );
  1713. plane_array->resize( p_array.size() );
  1714. for (int i=0;i<p_array.size();i++) {
  1715. plane_array->operator [](i)=Variant(p_array[i]);
  1716. }
  1717. }
  1718. Variant::Variant(const Vector<RID>& p_array) {
  1719. type=ARRAY;
  1720. Array *rid_array=memnew_placement( _data._mem, Array );
  1721. rid_array->resize( p_array.size() );
  1722. for (int i=0;i<p_array.size();i++) {
  1723. rid_array->set(i,Variant(p_array[i]));
  1724. }
  1725. }
  1726. Variant::Variant(const Vector<Vector2>& p_array) {
  1727. type=NIL;
  1728. DVector<Vector2> v;
  1729. int len=p_array.size();
  1730. if (len>0) {
  1731. v.resize(len);
  1732. DVector<Vector2>::Write w = v.write();
  1733. const Vector2 *r = p_array.ptr();
  1734. for (int i=0;i<len;i++)
  1735. w[i]=r[i];
  1736. }
  1737. *this=v;
  1738. }
  1739. Variant::Variant(const DVector<uint8_t>& p_raw_array) {
  1740. type=RAW_ARRAY;
  1741. memnew_placement( _data._mem, DVector<uint8_t>(p_raw_array) );
  1742. }
  1743. Variant::Variant(const DVector<int>& p_int_array) {
  1744. type=INT_ARRAY;
  1745. memnew_placement( _data._mem, DVector<int>(p_int_array) );
  1746. }
  1747. Variant::Variant(const DVector<real_t>& p_real_array) {
  1748. type=REAL_ARRAY;
  1749. memnew_placement( _data._mem, DVector<real_t>(p_real_array) );
  1750. }
  1751. Variant::Variant(const DVector<String>& p_string_array) {
  1752. type=STRING_ARRAY;
  1753. memnew_placement( _data._mem, DVector<String>(p_string_array) );
  1754. }
  1755. Variant::Variant(const DVector<Vector3>& p_vector3_array) {
  1756. type=VECTOR3_ARRAY;
  1757. memnew_placement( _data._mem, DVector<Vector3>(p_vector3_array) );
  1758. }
  1759. Variant::Variant(const DVector<Vector2>& p_vector2_array) {
  1760. type=VECTOR2_ARRAY;
  1761. memnew_placement( _data._mem, DVector<Vector2>(p_vector2_array) );
  1762. }
  1763. Variant::Variant(const DVector<Color>& p_color_array) {
  1764. type=COLOR_ARRAY;
  1765. memnew_placement( _data._mem, DVector<Color>(p_color_array) );
  1766. }
  1767. Variant::Variant(const DVector<Face3>& p_face_array) {
  1768. DVector<Vector3> vertices;
  1769. int face_count=p_face_array.size();
  1770. vertices.resize(face_count*3);
  1771. if (face_count) {
  1772. DVector<Face3>::Read r = p_face_array.read();
  1773. DVector<Vector3>::Write w = vertices.write();
  1774. for(int i=0;i<face_count;i++) {
  1775. for(int j=0;j<3;j++)
  1776. w[i*3+j]=r[i].vertex[j];
  1777. }
  1778. r=DVector<Face3>::Read();
  1779. w=DVector<Vector3>::Write();
  1780. }
  1781. type = NIL;
  1782. *this = vertices;
  1783. }
  1784. /* helpers */
  1785. Variant::Variant(const Vector<Variant>& p_array) {
  1786. type=NIL;
  1787. Array v;
  1788. int len=p_array.size();
  1789. v.resize(len);
  1790. for (int i=0;i<len;i++)
  1791. v.set(i,p_array[i]);
  1792. *this=v;
  1793. }
  1794. Variant::Variant(const Vector<uint8_t>& p_array) {
  1795. type=NIL;
  1796. DVector<uint8_t> v;
  1797. int len=p_array.size();
  1798. v.resize(len);
  1799. for (int i=0;i<len;i++)
  1800. v.set(i,p_array[i]);
  1801. *this=v;
  1802. }
  1803. Variant::Variant(const Vector<int>& p_array) {
  1804. type=NIL;
  1805. DVector<int> v;
  1806. int len=p_array.size();
  1807. v.resize(len);
  1808. for (int i=0;i<len;i++)
  1809. v.set(i,p_array[i]);
  1810. *this=v;
  1811. }
  1812. Variant::Variant(const Vector<real_t>& p_array) {
  1813. type=NIL;
  1814. DVector<real_t> v;
  1815. int len=p_array.size();
  1816. v.resize(len);
  1817. for (int i=0;i<len;i++)
  1818. v.set(i,p_array[i]);
  1819. *this=v;
  1820. }
  1821. Variant::Variant(const Vector<String>& p_array) {
  1822. type=NIL;
  1823. DVector<String> v;
  1824. int len=p_array.size();
  1825. v.resize(len);
  1826. for (int i=0;i<len;i++)
  1827. v.set(i,p_array[i]);
  1828. *this=v;
  1829. }
  1830. Variant::Variant(const Vector<Vector3>& p_array) {
  1831. type=NIL;
  1832. DVector<Vector3> v;
  1833. int len=p_array.size();
  1834. if (len>0) {
  1835. v.resize(len);
  1836. DVector<Vector3>::Write w = v.write();
  1837. const Vector3 *r = p_array.ptr();
  1838. for (int i=0;i<len;i++)
  1839. w[i]=r[i];
  1840. }
  1841. *this=v;
  1842. }
  1843. Variant::Variant(const Vector<Color>& p_array) {
  1844. type=NIL;
  1845. DVector<Color> v;
  1846. int len=p_array.size();
  1847. v.resize(len);
  1848. for (int i=0;i<len;i++)
  1849. v.set(i,p_array[i]);
  1850. *this=v;
  1851. }
  1852. void Variant::operator=(const Variant& p_variant) {
  1853. reference(p_variant);
  1854. }
  1855. Variant::Variant(const IP_Address& p_address) {
  1856. type=STRING;
  1857. memnew_placement( _data._mem, String( p_address ) );
  1858. }
  1859. Variant::Variant(const Variant& p_variant) {
  1860. type=NIL;
  1861. reference(p_variant);
  1862. }
  1863. /*
  1864. Variant::~Variant() {
  1865. clear();
  1866. }*/
  1867. uint32_t Variant::hash() const {
  1868. switch( type ) {
  1869. case NIL: {
  1870. return 0;
  1871. } break;
  1872. case BOOL: {
  1873. return _data._bool?1:0;
  1874. } break;
  1875. case INT: {
  1876. return _data._int;
  1877. } break;
  1878. case REAL: {
  1879. MarshallFloat mf;
  1880. mf.f=_data._real;
  1881. return mf.i;
  1882. } break;
  1883. case STRING: {
  1884. return reinterpret_cast<const String*>(_data._mem)->hash();
  1885. } break;
  1886. // math types
  1887. case VECTOR2: {
  1888. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->x);
  1889. return hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->y,hash);
  1890. } break;
  1891. case RECT2: {
  1892. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.x);
  1893. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.y,hash);
  1894. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.x,hash);
  1895. return hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.y,hash);
  1896. } break;
  1897. case MATRIX32: {
  1898. uint32_t hash = 5831;
  1899. for(int i=0;i<3;i++) {
  1900. for(int j=0;j<2;j++) {
  1901. hash = hash_djb2_one_float(_data._matrix32->elements[i][j],hash);
  1902. }
  1903. }
  1904. return hash;
  1905. } break;
  1906. case VECTOR3: {
  1907. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->x);
  1908. hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->y,hash);
  1909. return hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->z,hash);
  1910. } break;
  1911. case PLANE: {
  1912. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.x);
  1913. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.y,hash);
  1914. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.z,hash);
  1915. return hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->d,hash);
  1916. } break;
  1917. /*
  1918. case QUAT: {
  1919. } break;*/
  1920. case _AABB: {
  1921. uint32_t hash = 5831;
  1922. for(int i=0;i<3;i++) {
  1923. hash = hash_djb2_one_float(_data._aabb->pos[i],hash);
  1924. hash = hash_djb2_one_float(_data._aabb->size[i],hash);
  1925. }
  1926. return hash;
  1927. } break;
  1928. case QUAT: {
  1929. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->x);
  1930. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->y,hash);
  1931. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->z,hash);
  1932. return hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->w,hash);
  1933. } break;
  1934. case MATRIX3: {
  1935. uint32_t hash = 5831;
  1936. for(int i=0;i<3;i++) {
  1937. for(int j=0;j<3;j++) {
  1938. hash = hash_djb2_one_float(_data._matrix3->elements[i][j],hash);
  1939. }
  1940. }
  1941. return hash;
  1942. } break;
  1943. case TRANSFORM: {
  1944. uint32_t hash = 5831;
  1945. for(int i=0;i<3;i++) {
  1946. for(int j=0;j<3;j++) {
  1947. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j],hash);
  1948. }
  1949. hash = hash_djb2_one_float(_data._transform->origin[i],hash);
  1950. }
  1951. return hash;
  1952. } break;
  1953. // misc types
  1954. case COLOR: {
  1955. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->r);
  1956. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->g,hash);
  1957. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->b,hash);
  1958. return hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->a,hash);
  1959. } break;
  1960. case IMAGE: {
  1961. return 0;
  1962. } break;
  1963. case _RID: {
  1964. return hash_djb2_one_64(reinterpret_cast<const RID*>(_data._mem)->get_id());
  1965. } break;
  1966. case OBJECT: {
  1967. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  1968. } break;
  1969. case NODE_PATH: {
  1970. return reinterpret_cast<const NodePath*>(_data._mem)->hash();
  1971. } break;
  1972. case INPUT_EVENT: {
  1973. return hash_djb2_buffer((uint8_t*)_data._input_event,sizeof(InputEvent));
  1974. } break;
  1975. case DICTIONARY: {
  1976. return reinterpret_cast<const Dictionary*>(_data._mem)->hash();
  1977. } break;
  1978. case ARRAY: {
  1979. const Array& arr = *reinterpret_cast<const Array* >(_data._mem);
  1980. return arr.hash();
  1981. } break;
  1982. case RAW_ARRAY: {
  1983. const DVector<uint8_t>& arr = *reinterpret_cast<const DVector<uint8_t>* >(_data._mem);
  1984. int len = arr.size();
  1985. DVector<uint8_t>::Read r = arr.read();
  1986. return hash_djb2_buffer((uint8_t*)&r[0],len);
  1987. } break;
  1988. case INT_ARRAY: {
  1989. const DVector<int>& arr = *reinterpret_cast<const DVector<int>* >(_data._mem);
  1990. int len = arr.size();
  1991. DVector<int>::Read r = arr.read();
  1992. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(int));
  1993. } break;
  1994. case REAL_ARRAY: {
  1995. const DVector<real_t>& arr = *reinterpret_cast<const DVector<real_t>* >(_data._mem);
  1996. int len = arr.size();
  1997. DVector<real_t>::Read r = arr.read();
  1998. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(real_t));
  1999. } break;
  2000. case STRING_ARRAY: {
  2001. uint32_t hash=5831;
  2002. const DVector<String>& arr = *reinterpret_cast<const DVector<String>* >(_data._mem);
  2003. int len = arr.size();
  2004. DVector<String>::Read r = arr.read();
  2005. for(int i=0;i<len;i++) {
  2006. hash = hash_djb2_one_32(r[i].hash(),hash);
  2007. }
  2008. return hash;
  2009. } break;
  2010. case VECTOR2_ARRAY: {
  2011. uint32_t hash=5831;
  2012. const DVector<Vector2>& arr = *reinterpret_cast<const DVector<Vector2>* >(_data._mem);
  2013. int len = arr.size();
  2014. DVector<Vector2>::Read r = arr.read();
  2015. for(int i=0;i<len;i++) {
  2016. hash = hash_djb2_one_float(r[i].x,hash);
  2017. hash = hash_djb2_one_float(r[i].y,hash);
  2018. }
  2019. return hash;
  2020. } break;
  2021. case VECTOR3_ARRAY: {
  2022. uint32_t hash=5831;
  2023. const DVector<Vector3>& arr = *reinterpret_cast<const DVector<Vector3>* >(_data._mem);
  2024. int len = arr.size();
  2025. DVector<Vector3>::Read r = arr.read();
  2026. for(int i=0;i<len;i++) {
  2027. hash = hash_djb2_one_float(r[i].x,hash);
  2028. hash = hash_djb2_one_float(r[i].y,hash);
  2029. hash = hash_djb2_one_float(r[i].z,hash);
  2030. }
  2031. return hash;
  2032. } break;
  2033. case COLOR_ARRAY: {
  2034. uint32_t hash=5831;
  2035. const DVector<Color>& arr = *reinterpret_cast<const DVector<Color>* >(_data._mem);
  2036. int len = arr.size();
  2037. DVector<Color>::Read r = arr.read();
  2038. for(int i=0;i<len;i++) {
  2039. hash = hash_djb2_one_float(r[i].r,hash);
  2040. hash = hash_djb2_one_float(r[i].g,hash);
  2041. hash = hash_djb2_one_float(r[i].b,hash);
  2042. hash = hash_djb2_one_float(r[i].a,hash);
  2043. }
  2044. return hash;
  2045. } break;
  2046. default: {}
  2047. }
  2048. return 0;
  2049. }
  2050. bool Variant::is_ref() const {
  2051. return type==OBJECT && !_get_obj().ref.is_null();
  2052. }
  2053. Vector<Variant> varray() {
  2054. return Vector<Variant>();
  2055. }
  2056. Vector<Variant> varray(const Variant& p_arg1) {
  2057. Vector<Variant> v;
  2058. v.push_back(p_arg1);
  2059. return v;
  2060. }
  2061. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2) {
  2062. Vector<Variant> v;
  2063. v.push_back(p_arg1);
  2064. v.push_back(p_arg2);
  2065. return v;
  2066. }
  2067. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3) {
  2068. Vector<Variant> v;
  2069. v.push_back(p_arg1);
  2070. v.push_back(p_arg2);
  2071. v.push_back(p_arg3);
  2072. return v;
  2073. }
  2074. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4) {
  2075. Vector<Variant> v;
  2076. v.push_back(p_arg1);
  2077. v.push_back(p_arg2);
  2078. v.push_back(p_arg3);
  2079. v.push_back(p_arg4);
  2080. return v;
  2081. }
  2082. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4,const Variant& p_arg5) {
  2083. Vector<Variant> v;
  2084. v.push_back(p_arg1);
  2085. v.push_back(p_arg2);
  2086. v.push_back(p_arg3);
  2087. v.push_back(p_arg4);
  2088. v.push_back(p_arg5);
  2089. return v;
  2090. }
  2091. void Variant::static_assign(const Variant& p_variant) {
  2092. }
  2093. bool Variant::is_shared() const {
  2094. switch(type) {
  2095. case OBJECT: return true;
  2096. case ARRAY: return reinterpret_cast<const Array*>(_data._mem)->is_shared();
  2097. case DICTIONARY: return reinterpret_cast<const Dictionary*>(_data._mem)->is_shared();
  2098. default: {}
  2099. }
  2100. return false;
  2101. }
  2102. Variant Variant::call(const StringName& p_method,VARIANT_ARG_DECLARE) {
  2103. VARIANT_ARGPTRS;
  2104. int argc=0;
  2105. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  2106. if (argptr[i]->get_type()==Variant::NIL)
  2107. break;
  2108. argc++;
  2109. }
  2110. CallError error;
  2111. Variant ret = call(p_method,argptr,argc,error);
  2112. switch(error.error) {
  2113. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2114. String err = "Invalid type for argument #"+itos(error.argument)+", expected '"+Variant::get_type_name(error.expected)+"'.";
  2115. ERR_PRINT(err.utf8().get_data());
  2116. } break;
  2117. case CallError::CALL_ERROR_INVALID_METHOD: {
  2118. String err = "Invalid method '"+p_method+"' for type '"+Variant::get_type_name(type)+"'.";
  2119. ERR_PRINT(err.utf8().get_data());
  2120. } break;
  2121. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2122. String err = "Too many arguments for method '"+p_method+"'";
  2123. ERR_PRINT(err.utf8().get_data());
  2124. } break;
  2125. default: {}
  2126. }
  2127. return ret;
  2128. }
  2129. void Variant::construct_from_string(const String& p_string,Variant& r_value,ObjectConstruct p_obj_construct,void *p_construct_ud) {
  2130. r_value=Variant();
  2131. }
  2132. String Variant::get_construct_string() const {
  2133. String vars;
  2134. VariantWriter::write_to_string(*this,vars);
  2135. return vars;
  2136. }
  2137. String Variant::get_call_error_text(Object* p_base, const StringName& p_method,const Variant** p_argptrs,int p_argcount,const Variant::CallError &ce) {
  2138. String err_text;
  2139. if (ce.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2140. int errorarg=ce.argument;
  2141. err_text="Cannot convert argument "+itos(errorarg+1)+" from "+Variant::get_type_name(p_argptrs[errorarg]->get_type())+" to "+Variant::get_type_name(ce.expected)+".";
  2142. } else if (ce.error==Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2143. err_text="Expected "+itos(ce.argument)+" arguments.";
  2144. } else if (ce.error==Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2145. err_text="Expected "+itos(ce.argument)+" arguments.";
  2146. } else if (ce.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2147. err_text="Method not found.";
  2148. } else if (ce.error==Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2149. err_text="Instance is null";
  2150. } else if (ce.error==Variant::CallError::CALL_OK){
  2151. return "Call OK";
  2152. }
  2153. String class_name = p_base->get_type();
  2154. Ref<Script> script = p_base->get_script();
  2155. if (script.is_valid() && script->get_path().is_resource_file()) {
  2156. class_name+="("+script->get_path().get_file()+")";
  2157. }
  2158. return "'"+class_name+"::"+String(p_method)+"': "+err_text;
  2159. }