variant.cpp 57 KB

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