variant.cpp 57 KB

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