2
0

variant.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255
  1. /*************************************************************************/
  2. /* variant.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "variant.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/print_string.h"
  35. #include "core/resource.h"
  36. #include "core/variant_parser.h"
  37. #include "scene/gui/control.h"
  38. #include "scene/main/node.h"
  39. String Variant::get_type_name(Variant::Type p_type) {
  40. switch (p_type) {
  41. case NIL: {
  42. return "Nil";
  43. } break;
  44. // atomic types
  45. case BOOL: {
  46. return "bool";
  47. } break;
  48. case INT: {
  49. return "int";
  50. } break;
  51. case REAL: {
  52. return "float";
  53. } break;
  54. case STRING: {
  55. return "String";
  56. } break;
  57. // math types
  58. case VECTOR2: {
  59. return "Vector2";
  60. } break;
  61. case RECT2: {
  62. return "Rect2";
  63. } break;
  64. case TRANSFORM2D: {
  65. return "Transform2D";
  66. } break;
  67. case VECTOR3: {
  68. return "Vector3";
  69. } break;
  70. case PLANE: {
  71. return "Plane";
  72. } break;
  73. /*
  74. case QUAT: {
  75. } break;*/
  76. case AABB: {
  77. return "AABB";
  78. } break;
  79. case QUAT: {
  80. return "Quat";
  81. } break;
  82. case BASIS: {
  83. return "Basis";
  84. } break;
  85. case TRANSFORM: {
  86. return "Transform";
  87. } break;
  88. // misc types
  89. case COLOR: {
  90. return "Color";
  91. } break;
  92. case _RID: {
  93. return "RID";
  94. } break;
  95. case OBJECT: {
  96. return "Object";
  97. } break;
  98. case NODE_PATH: {
  99. return "NodePath";
  100. } break;
  101. case DICTIONARY: {
  102. return "Dictionary";
  103. } break;
  104. case ARRAY: {
  105. return "Array";
  106. } break;
  107. // arrays
  108. case POOL_BYTE_ARRAY: {
  109. return "PoolByteArray";
  110. } break;
  111. case POOL_INT_ARRAY: {
  112. return "PoolIntArray";
  113. } break;
  114. case POOL_REAL_ARRAY: {
  115. return "PoolRealArray";
  116. } break;
  117. case POOL_STRING_ARRAY: {
  118. return "PoolStringArray";
  119. } break;
  120. case POOL_VECTOR2_ARRAY: {
  121. return "PoolVector2Array";
  122. } break;
  123. case POOL_VECTOR3_ARRAY: {
  124. return "PoolVector3Array";
  125. } break;
  126. case POOL_COLOR_ARRAY: {
  127. return "PoolColorArray";
  128. } break;
  129. default: {}
  130. }
  131. return "";
  132. }
  133. bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
  134. if (p_type_from == p_type_to)
  135. return true;
  136. if (p_type_to == NIL && p_type_from != NIL) //nil can convert to anything
  137. return true;
  138. if (p_type_from == NIL) {
  139. return (p_type_to == OBJECT);
  140. };
  141. const Type *valid_types = NULL;
  142. const Type *invalid_types = NULL;
  143. switch (p_type_to) {
  144. case BOOL: {
  145. static const Type valid[] = {
  146. INT,
  147. REAL,
  148. STRING,
  149. NIL,
  150. };
  151. valid_types = valid;
  152. } break;
  153. case INT: {
  154. static const Type valid[] = {
  155. BOOL,
  156. REAL,
  157. STRING,
  158. NIL,
  159. };
  160. valid_types = valid;
  161. } break;
  162. case REAL: {
  163. static const Type valid[] = {
  164. BOOL,
  165. INT,
  166. STRING,
  167. NIL,
  168. };
  169. valid_types = valid;
  170. } break;
  171. case STRING: {
  172. static const Type invalid[] = {
  173. OBJECT,
  174. NIL
  175. };
  176. invalid_types = invalid;
  177. } break;
  178. case TRANSFORM2D: {
  179. static const Type valid[] = {
  180. TRANSFORM,
  181. NIL
  182. };
  183. valid_types = valid;
  184. } break;
  185. case QUAT: {
  186. static const Type valid[] = {
  187. BASIS,
  188. NIL
  189. };
  190. valid_types = valid;
  191. } break;
  192. case BASIS: {
  193. static const Type valid[] = {
  194. QUAT,
  195. VECTOR3,
  196. NIL
  197. };
  198. valid_types = valid;
  199. } break;
  200. case TRANSFORM: {
  201. static const Type valid[] = {
  202. TRANSFORM2D,
  203. QUAT,
  204. BASIS,
  205. NIL
  206. };
  207. valid_types = valid;
  208. } break;
  209. case COLOR: {
  210. static const Type valid[] = {
  211. STRING,
  212. INT,
  213. NIL,
  214. };
  215. valid_types = valid;
  216. } break;
  217. case _RID: {
  218. static const Type valid[] = {
  219. OBJECT,
  220. NIL
  221. };
  222. valid_types = valid;
  223. } break;
  224. case OBJECT: {
  225. static const Type valid[] = {
  226. NIL
  227. };
  228. valid_types = valid;
  229. } break;
  230. case NODE_PATH: {
  231. static const Type valid[] = {
  232. STRING,
  233. NIL
  234. };
  235. valid_types = valid;
  236. } break;
  237. case ARRAY: {
  238. static const Type valid[] = {
  239. POOL_BYTE_ARRAY,
  240. POOL_INT_ARRAY,
  241. POOL_STRING_ARRAY,
  242. POOL_REAL_ARRAY,
  243. POOL_COLOR_ARRAY,
  244. POOL_VECTOR2_ARRAY,
  245. POOL_VECTOR3_ARRAY,
  246. NIL
  247. };
  248. valid_types = valid;
  249. } break;
  250. // arrays
  251. case POOL_BYTE_ARRAY: {
  252. static const Type valid[] = {
  253. ARRAY,
  254. NIL
  255. };
  256. valid_types = valid;
  257. } break;
  258. case POOL_INT_ARRAY: {
  259. static const Type valid[] = {
  260. ARRAY,
  261. NIL
  262. };
  263. valid_types = valid;
  264. } break;
  265. case POOL_REAL_ARRAY: {
  266. static const Type valid[] = {
  267. ARRAY,
  268. NIL
  269. };
  270. valid_types = valid;
  271. } break;
  272. case POOL_STRING_ARRAY: {
  273. static const Type valid[] = {
  274. ARRAY,
  275. NIL
  276. };
  277. valid_types = valid;
  278. } break;
  279. case POOL_VECTOR2_ARRAY: {
  280. static const Type valid[] = {
  281. ARRAY,
  282. NIL
  283. };
  284. valid_types = valid;
  285. } break;
  286. case POOL_VECTOR3_ARRAY: {
  287. static const Type valid[] = {
  288. ARRAY,
  289. NIL
  290. };
  291. valid_types = valid;
  292. } break;
  293. case POOL_COLOR_ARRAY: {
  294. static const Type valid[] = {
  295. ARRAY,
  296. NIL
  297. };
  298. valid_types = valid;
  299. } break;
  300. default: {}
  301. }
  302. if (valid_types) {
  303. int i = 0;
  304. while (valid_types[i] != NIL) {
  305. if (p_type_from == valid_types[i])
  306. return true;
  307. i++;
  308. }
  309. } else if (invalid_types) {
  310. int i = 0;
  311. while (invalid_types[i] != NIL) {
  312. if (p_type_from == invalid_types[i])
  313. return false;
  314. i++;
  315. }
  316. return true;
  317. }
  318. return false;
  319. }
  320. bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
  321. if (p_type_from == p_type_to)
  322. return true;
  323. if (p_type_to == NIL && p_type_from != NIL) //nil can convert to anything
  324. return true;
  325. if (p_type_from == NIL) {
  326. return (p_type_to == OBJECT);
  327. };
  328. const Type *valid_types = NULL;
  329. switch (p_type_to) {
  330. case BOOL: {
  331. static const Type valid[] = {
  332. INT,
  333. REAL,
  334. //STRING,
  335. NIL,
  336. };
  337. valid_types = valid;
  338. } break;
  339. case INT: {
  340. static const Type valid[] = {
  341. BOOL,
  342. REAL,
  343. //STRING,
  344. NIL,
  345. };
  346. valid_types = valid;
  347. } break;
  348. case REAL: {
  349. static const Type valid[] = {
  350. BOOL,
  351. INT,
  352. //STRING,
  353. NIL,
  354. };
  355. valid_types = valid;
  356. } break;
  357. case STRING: {
  358. static const Type valid[] = {
  359. NODE_PATH,
  360. NIL
  361. };
  362. valid_types = valid;
  363. } break;
  364. case TRANSFORM2D: {
  365. static const Type valid[] = {
  366. TRANSFORM,
  367. NIL
  368. };
  369. valid_types = valid;
  370. } break;
  371. case QUAT: {
  372. static const Type valid[] = {
  373. BASIS,
  374. NIL
  375. };
  376. valid_types = valid;
  377. } break;
  378. case BASIS: {
  379. static const Type valid[] = {
  380. QUAT,
  381. VECTOR3,
  382. NIL
  383. };
  384. valid_types = valid;
  385. } break;
  386. case TRANSFORM: {
  387. static const Type valid[] = {
  388. TRANSFORM2D,
  389. QUAT,
  390. BASIS,
  391. NIL
  392. };
  393. valid_types = valid;
  394. } break;
  395. case COLOR: {
  396. static const Type valid[] = {
  397. STRING,
  398. INT,
  399. NIL,
  400. };
  401. valid_types = valid;
  402. } break;
  403. case _RID: {
  404. static const Type valid[] = {
  405. OBJECT,
  406. NIL
  407. };
  408. valid_types = valid;
  409. } break;
  410. case OBJECT: {
  411. static const Type valid[] = {
  412. NIL
  413. };
  414. valid_types = valid;
  415. } break;
  416. case NODE_PATH: {
  417. static const Type valid[] = {
  418. STRING,
  419. NIL
  420. };
  421. valid_types = valid;
  422. } break;
  423. case ARRAY: {
  424. static const Type valid[] = {
  425. POOL_BYTE_ARRAY,
  426. POOL_INT_ARRAY,
  427. POOL_STRING_ARRAY,
  428. POOL_REAL_ARRAY,
  429. POOL_COLOR_ARRAY,
  430. POOL_VECTOR2_ARRAY,
  431. POOL_VECTOR3_ARRAY,
  432. NIL
  433. };
  434. valid_types = valid;
  435. } break;
  436. // arrays
  437. case POOL_BYTE_ARRAY: {
  438. static const Type valid[] = {
  439. ARRAY,
  440. NIL
  441. };
  442. valid_types = valid;
  443. } break;
  444. case POOL_INT_ARRAY: {
  445. static const Type valid[] = {
  446. ARRAY,
  447. NIL
  448. };
  449. valid_types = valid;
  450. } break;
  451. case POOL_REAL_ARRAY: {
  452. static const Type valid[] = {
  453. ARRAY,
  454. NIL
  455. };
  456. valid_types = valid;
  457. } break;
  458. case POOL_STRING_ARRAY: {
  459. static const Type valid[] = {
  460. ARRAY,
  461. NIL
  462. };
  463. valid_types = valid;
  464. } break;
  465. case POOL_VECTOR2_ARRAY: {
  466. static const Type valid[] = {
  467. ARRAY,
  468. NIL
  469. };
  470. valid_types = valid;
  471. } break;
  472. case POOL_VECTOR3_ARRAY: {
  473. static const Type valid[] = {
  474. ARRAY,
  475. NIL
  476. };
  477. valid_types = valid;
  478. } break;
  479. case POOL_COLOR_ARRAY: {
  480. static const Type valid[] = {
  481. ARRAY,
  482. NIL
  483. };
  484. valid_types = valid;
  485. } break;
  486. default: {}
  487. }
  488. if (valid_types) {
  489. int i = 0;
  490. while (valid_types[i] != NIL) {
  491. if (p_type_from == valid_types[i])
  492. return true;
  493. i++;
  494. }
  495. }
  496. return false;
  497. }
  498. bool Variant::operator==(const Variant &p_variant) const {
  499. if (type != p_variant.type) //evaluation of operator== needs to be more strict
  500. return false;
  501. bool v;
  502. Variant r;
  503. evaluate(OP_EQUAL, *this, p_variant, r, v);
  504. return r;
  505. }
  506. bool Variant::operator!=(const Variant &p_variant) const {
  507. if (type != p_variant.type) //evaluation of operator== needs to be more strict
  508. return true;
  509. bool v;
  510. Variant r;
  511. evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
  512. return r;
  513. }
  514. bool Variant::operator<(const Variant &p_variant) const {
  515. if (type != p_variant.type) //if types differ, then order by type first
  516. return type < p_variant.type;
  517. bool v;
  518. Variant r;
  519. evaluate(OP_LESS, *this, p_variant, r, v);
  520. return r;
  521. }
  522. bool Variant::is_zero() const {
  523. switch (type) {
  524. case NIL: {
  525. return true;
  526. } break;
  527. // atomic types
  528. case BOOL: {
  529. return _data._bool == false;
  530. } break;
  531. case INT: {
  532. return _data._int == 0;
  533. } break;
  534. case REAL: {
  535. return _data._real == 0;
  536. } break;
  537. case STRING: {
  538. return *reinterpret_cast<const String *>(_data._mem) == String();
  539. } break;
  540. // math types
  541. case VECTOR2: {
  542. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
  543. } break;
  544. case RECT2: {
  545. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
  546. } break;
  547. case TRANSFORM2D: {
  548. return *_data._transform2d == Transform2D();
  549. } break;
  550. case VECTOR3: {
  551. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
  552. } break;
  553. case PLANE: {
  554. return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
  555. } break;
  556. /*
  557. case QUAT: {
  558. } break;*/
  559. case AABB: {
  560. return *_data._aabb == ::AABB();
  561. } break;
  562. case QUAT: {
  563. return *reinterpret_cast<const Quat *>(_data._mem) == Quat();
  564. } break;
  565. case BASIS: {
  566. return *_data._basis == Basis();
  567. } break;
  568. case TRANSFORM: {
  569. return *_data._transform == Transform();
  570. } break;
  571. // misc types
  572. case COLOR: {
  573. return *reinterpret_cast<const Color *>(_data._mem) == Color();
  574. } break;
  575. case _RID: {
  576. return *reinterpret_cast<const RID *>(_data._mem) == RID();
  577. } break;
  578. case OBJECT: {
  579. return _get_obj().obj == NULL;
  580. } break;
  581. case NODE_PATH: {
  582. return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
  583. } break;
  584. case DICTIONARY: {
  585. return reinterpret_cast<const Dictionary *>(_data._mem)->empty();
  586. } break;
  587. case ARRAY: {
  588. return reinterpret_cast<const Array *>(_data._mem)->empty();
  589. } break;
  590. // arrays
  591. case POOL_BYTE_ARRAY: {
  592. return reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem)->size() == 0;
  593. } break;
  594. case POOL_INT_ARRAY: {
  595. return reinterpret_cast<const PoolVector<int> *>(_data._mem)->size() == 0;
  596. } break;
  597. case POOL_REAL_ARRAY: {
  598. return reinterpret_cast<const PoolVector<real_t> *>(_data._mem)->size() == 0;
  599. } break;
  600. case POOL_STRING_ARRAY: {
  601. return reinterpret_cast<const PoolVector<String> *>(_data._mem)->size() == 0;
  602. } break;
  603. case POOL_VECTOR2_ARRAY: {
  604. return reinterpret_cast<const PoolVector<Vector2> *>(_data._mem)->size() == 0;
  605. } break;
  606. case POOL_VECTOR3_ARRAY: {
  607. return reinterpret_cast<const PoolVector<Vector3> *>(_data._mem)->size() == 0;
  608. } break;
  609. case POOL_COLOR_ARRAY: {
  610. return reinterpret_cast<const PoolVector<Color> *>(_data._mem)->size() == 0;
  611. } break;
  612. default: {}
  613. }
  614. return false;
  615. }
  616. bool Variant::is_one() const {
  617. switch (type) {
  618. case NIL: {
  619. return true;
  620. } break;
  621. // atomic types
  622. case BOOL: {
  623. return _data._bool;
  624. } break;
  625. case INT: {
  626. return _data._int == 1;
  627. } break;
  628. case REAL: {
  629. return _data._real == 1;
  630. } break;
  631. case VECTOR2: {
  632. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
  633. } break;
  634. case RECT2: {
  635. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
  636. } break;
  637. case VECTOR3: {
  638. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
  639. } break;
  640. case PLANE: {
  641. return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
  642. } break;
  643. case COLOR: {
  644. return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
  645. } break;
  646. default: { return !is_zero(); }
  647. }
  648. return false;
  649. }
  650. void Variant::reference(const Variant &p_variant) {
  651. clear();
  652. type = p_variant.type;
  653. switch (p_variant.type) {
  654. case NIL: {
  655. // none
  656. } break;
  657. // atomic types
  658. case BOOL: {
  659. _data._bool = p_variant._data._bool;
  660. } break;
  661. case INT: {
  662. _data._int = p_variant._data._int;
  663. } break;
  664. case REAL: {
  665. _data._real = p_variant._data._real;
  666. } break;
  667. case STRING: {
  668. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  669. } break;
  670. // math types
  671. case VECTOR2: {
  672. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  673. } break;
  674. case RECT2: {
  675. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  676. } break;
  677. case TRANSFORM2D: {
  678. _data._transform2d = memnew(Transform2D(*p_variant._data._transform2d));
  679. } break;
  680. case VECTOR3: {
  681. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  682. } break;
  683. case PLANE: {
  684. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  685. } break;
  686. case AABB: {
  687. _data._aabb = memnew(::AABB(*p_variant._data._aabb));
  688. } break;
  689. case QUAT: {
  690. memnew_placement(_data._mem, Quat(*reinterpret_cast<const Quat *>(p_variant._data._mem)));
  691. } break;
  692. case BASIS: {
  693. _data._basis = memnew(Basis(*p_variant._data._basis));
  694. } break;
  695. case TRANSFORM: {
  696. _data._transform = memnew(Transform(*p_variant._data._transform));
  697. } break;
  698. // misc types
  699. case COLOR: {
  700. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  701. } break;
  702. case _RID: {
  703. memnew_placement(_data._mem, RID(*reinterpret_cast<const RID *>(p_variant._data._mem)));
  704. } break;
  705. case OBJECT: {
  706. memnew_placement(_data._mem, ObjData(p_variant._get_obj()));
  707. } break;
  708. case NODE_PATH: {
  709. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  710. } break;
  711. case DICTIONARY: {
  712. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  713. } break;
  714. case ARRAY: {
  715. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  716. } break;
  717. // arrays
  718. case POOL_BYTE_ARRAY: {
  719. memnew_placement(_data._mem, PoolVector<uint8_t>(*reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem)));
  720. } break;
  721. case POOL_INT_ARRAY: {
  722. memnew_placement(_data._mem, PoolVector<int>(*reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem)));
  723. } break;
  724. case POOL_REAL_ARRAY: {
  725. memnew_placement(_data._mem, PoolVector<real_t>(*reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem)));
  726. } break;
  727. case POOL_STRING_ARRAY: {
  728. memnew_placement(_data._mem, PoolVector<String>(*reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem)));
  729. } break;
  730. case POOL_VECTOR2_ARRAY: {
  731. memnew_placement(_data._mem, PoolVector<Vector2>(*reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem)));
  732. } break;
  733. case POOL_VECTOR3_ARRAY: {
  734. memnew_placement(_data._mem, PoolVector<Vector3>(*reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem)));
  735. } break;
  736. case POOL_COLOR_ARRAY: {
  737. memnew_placement(_data._mem, PoolVector<Color>(*reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem)));
  738. } break;
  739. default: {}
  740. }
  741. }
  742. void Variant::zero() {
  743. switch (type) {
  744. case NIL: break;
  745. case BOOL: this->_data._bool = false; break;
  746. case INT: this->_data._int = 0; break;
  747. case REAL: this->_data._real = 0; break;
  748. case VECTOR2: *reinterpret_cast<Vector2 *>(this->_data._mem) = Vector2(); break;
  749. case RECT2: *reinterpret_cast<Rect2 *>(this->_data._mem) = Rect2(); break;
  750. case VECTOR3: *reinterpret_cast<Vector3 *>(this->_data._mem) = Vector3(); break;
  751. case PLANE: *reinterpret_cast<Plane *>(this->_data._mem) = Plane(); break;
  752. case QUAT: *reinterpret_cast<Quat *>(this->_data._mem) = Quat(); break;
  753. case COLOR: *reinterpret_cast<Color *>(this->_data._mem) = Color(); break;
  754. default: this->clear(); break;
  755. }
  756. }
  757. void Variant::clear() {
  758. switch (type) {
  759. case STRING: {
  760. reinterpret_cast<String *>(_data._mem)->~String();
  761. } break;
  762. /*
  763. // no point, they don't allocate memory
  764. VECTOR3,
  765. PLANE,
  766. QUAT,
  767. COLOR,
  768. VECTOR2,
  769. RECT2
  770. */
  771. case TRANSFORM2D: {
  772. memdelete(_data._transform2d);
  773. } break;
  774. case AABB: {
  775. memdelete(_data._aabb);
  776. } break;
  777. case BASIS: {
  778. memdelete(_data._basis);
  779. } break;
  780. case TRANSFORM: {
  781. memdelete(_data._transform);
  782. } break;
  783. // misc types
  784. case NODE_PATH: {
  785. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  786. } break;
  787. case OBJECT: {
  788. _get_obj().obj = NULL;
  789. _get_obj().ref.unref();
  790. } break;
  791. case _RID: {
  792. // not much need probably
  793. reinterpret_cast<RID *>(_data._mem)->~RID();
  794. } break;
  795. case DICTIONARY: {
  796. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  797. } break;
  798. case ARRAY: {
  799. reinterpret_cast<Array *>(_data._mem)->~Array();
  800. } break;
  801. // arrays
  802. case POOL_BYTE_ARRAY: {
  803. reinterpret_cast<PoolVector<uint8_t> *>(_data._mem)->~PoolVector<uint8_t>();
  804. } break;
  805. case POOL_INT_ARRAY: {
  806. reinterpret_cast<PoolVector<int> *>(_data._mem)->~PoolVector<int>();
  807. } break;
  808. case POOL_REAL_ARRAY: {
  809. reinterpret_cast<PoolVector<real_t> *>(_data._mem)->~PoolVector<real_t>();
  810. } break;
  811. case POOL_STRING_ARRAY: {
  812. reinterpret_cast<PoolVector<String> *>(_data._mem)->~PoolVector<String>();
  813. } break;
  814. case POOL_VECTOR2_ARRAY: {
  815. reinterpret_cast<PoolVector<Vector2> *>(_data._mem)->~PoolVector<Vector2>();
  816. } break;
  817. case POOL_VECTOR3_ARRAY: {
  818. reinterpret_cast<PoolVector<Vector3> *>(_data._mem)->~PoolVector<Vector3>();
  819. } break;
  820. case POOL_COLOR_ARRAY: {
  821. reinterpret_cast<PoolVector<Color> *>(_data._mem)->~PoolVector<Color>();
  822. } break;
  823. default: {} /* not needed */
  824. }
  825. type = NIL;
  826. }
  827. Variant::operator signed int() const {
  828. switch (type) {
  829. case NIL: return 0;
  830. case BOOL: return _data._bool ? 1 : 0;
  831. case INT: return _data._int;
  832. case REAL: return _data._real;
  833. case STRING: return operator String().to_int();
  834. default: {
  835. return 0;
  836. }
  837. }
  838. return 0;
  839. }
  840. Variant::operator unsigned int() const {
  841. switch (type) {
  842. case NIL: return 0;
  843. case BOOL: return _data._bool ? 1 : 0;
  844. case INT: return _data._int;
  845. case REAL: return _data._real;
  846. case STRING: return operator String().to_int();
  847. default: {
  848. return 0;
  849. }
  850. }
  851. return 0;
  852. }
  853. Variant::operator int64_t() 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_int64();
  860. default: {
  861. return 0;
  862. }
  863. }
  864. return 0;
  865. }
  866. /*
  867. Variant::operator long unsigned int() const {
  868. switch( type ) {
  869. case NIL: return 0;
  870. case BOOL: return _data._bool ? 1 : 0;
  871. case INT: return _data._int;
  872. case REAL: return _data._real;
  873. case STRING: return operator String().to_int();
  874. default: {
  875. return 0;
  876. }
  877. }
  878. return 0;
  879. };
  880. */
  881. Variant::operator uint64_t() const {
  882. switch (type) {
  883. case NIL: return 0;
  884. case BOOL: return _data._bool ? 1 : 0;
  885. case INT: return _data._int;
  886. case REAL: return _data._real;
  887. case STRING: return operator String().to_int();
  888. default: {
  889. return 0;
  890. }
  891. }
  892. return 0;
  893. }
  894. #ifdef NEED_LONG_INT
  895. Variant::operator signed long() const {
  896. switch (type) {
  897. case NIL: return 0;
  898. case BOOL: return _data._bool ? 1 : 0;
  899. case INT: return _data._int;
  900. case REAL: return _data._real;
  901. case STRING: return operator String().to_int();
  902. default: {
  903. return 0;
  904. }
  905. }
  906. return 0;
  907. };
  908. Variant::operator unsigned long() const {
  909. switch (type) {
  910. case NIL: return 0;
  911. case BOOL: return _data._bool ? 1 : 0;
  912. case INT: return _data._int;
  913. case REAL: return _data._real;
  914. case STRING: return operator String().to_int();
  915. default: {
  916. return 0;
  917. }
  918. }
  919. return 0;
  920. };
  921. #endif
  922. Variant::operator signed short() const {
  923. switch (type) {
  924. case NIL: return 0;
  925. case BOOL: return _data._bool ? 1 : 0;
  926. case INT: return _data._int;
  927. case REAL: return _data._real;
  928. case STRING: return operator String().to_int();
  929. default: {
  930. return 0;
  931. }
  932. }
  933. return 0;
  934. }
  935. Variant::operator unsigned short() const {
  936. switch (type) {
  937. case NIL: return 0;
  938. case BOOL: return _data._bool ? 1 : 0;
  939. case INT: return _data._int;
  940. case REAL: return _data._real;
  941. case STRING: return operator String().to_int();
  942. default: {
  943. return 0;
  944. }
  945. }
  946. return 0;
  947. }
  948. Variant::operator signed char() 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 char() 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 CharType() const {
  975. return operator unsigned int();
  976. }
  977. Variant::operator float() const {
  978. switch (type) {
  979. case NIL: return 0;
  980. case BOOL: return _data._bool ? 1.0 : 0.0;
  981. case INT: return (float)_data._int;
  982. case REAL: return _data._real;
  983. case STRING: return operator String().to_double();
  984. default: {
  985. return 0;
  986. }
  987. }
  988. return 0;
  989. }
  990. Variant::operator double() const {
  991. switch (type) {
  992. case NIL: return 0;
  993. case BOOL: return _data._bool ? 1.0 : 0.0;
  994. case INT: return (double)_data._int;
  995. case REAL: return _data._real;
  996. case STRING: return operator String().to_double();
  997. default: {
  998. return 0;
  999. }
  1000. }
  1001. return true;
  1002. }
  1003. Variant::operator StringName() const {
  1004. if (type == NODE_PATH) {
  1005. return reinterpret_cast<const NodePath *>(_data._mem)->get_sname();
  1006. }
  1007. return StringName(operator String());
  1008. }
  1009. struct _VariantStrPair {
  1010. String key;
  1011. String value;
  1012. bool operator<(const _VariantStrPair &p) const {
  1013. return key < p.key;
  1014. }
  1015. };
  1016. Variant::operator String() const {
  1017. switch (type) {
  1018. case NIL: return "Null";
  1019. case BOOL: return _data._bool ? "True" : "False";
  1020. case INT: return itos(_data._int);
  1021. case REAL: return rtos(_data._real);
  1022. case STRING: return *reinterpret_cast<const String *>(_data._mem);
  1023. case VECTOR2: return "(" + operator Vector2() + ")";
  1024. case RECT2: return "(" + operator Rect2() + ")";
  1025. case TRANSFORM2D: {
  1026. Transform2D mat32 = operator Transform2D();
  1027. return "(" + Variant(mat32.elements[0]).operator String() + ", " + Variant(mat32.elements[1]).operator String() + ", " + Variant(mat32.elements[2]).operator String() + ")";
  1028. } break;
  1029. case VECTOR3: return "(" + operator Vector3() + ")";
  1030. case PLANE:
  1031. return operator Plane();
  1032. //case QUAT:
  1033. case AABB: return operator ::AABB();
  1034. case QUAT: return "(" + operator Quat() + ")";
  1035. case BASIS: {
  1036. Basis mat3 = operator Basis();
  1037. String mtx("(");
  1038. for (int i = 0; i < 3; i++) {
  1039. if (i != 0)
  1040. mtx += ", ";
  1041. mtx += "(";
  1042. for (int j = 0; j < 3; j++) {
  1043. if (j != 0)
  1044. mtx += ", ";
  1045. mtx += Variant(mat3.elements[i][j]).operator String();
  1046. }
  1047. mtx += ")";
  1048. }
  1049. return mtx + ")";
  1050. } break;
  1051. case TRANSFORM: return operator Transform();
  1052. case NODE_PATH: return operator NodePath();
  1053. case COLOR: return String::num(operator Color().r) + "," + String::num(operator Color().g) + "," + String::num(operator Color().b) + "," + String::num(operator Color().a);
  1054. case DICTIONARY: {
  1055. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1056. //const String *K=NULL;
  1057. String str("{");
  1058. List<Variant> keys;
  1059. d.get_key_list(&keys);
  1060. Vector<_VariantStrPair> pairs;
  1061. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1062. _VariantStrPair sp;
  1063. sp.key = String(E->get());
  1064. sp.value = d[E->get()];
  1065. pairs.push_back(sp);
  1066. }
  1067. pairs.sort();
  1068. for (int i = 0; i < pairs.size(); i++) {
  1069. if (i > 0)
  1070. str += ", ";
  1071. str += pairs[i].key + ":" + pairs[i].value;
  1072. }
  1073. str += "}";
  1074. return str;
  1075. } break;
  1076. case POOL_VECTOR2_ARRAY: {
  1077. PoolVector<Vector2> vec = operator PoolVector<Vector2>();
  1078. String str("[");
  1079. for (int i = 0; i < vec.size(); i++) {
  1080. if (i > 0)
  1081. str += ", ";
  1082. str = str + Variant(vec[i]);
  1083. }
  1084. str += "]";
  1085. return str;
  1086. } break;
  1087. case POOL_VECTOR3_ARRAY: {
  1088. PoolVector<Vector3> vec = operator PoolVector<Vector3>();
  1089. String str("[");
  1090. for (int i = 0; i < vec.size(); i++) {
  1091. if (i > 0)
  1092. str += ", ";
  1093. str = str + Variant(vec[i]);
  1094. }
  1095. str += "]";
  1096. return str;
  1097. } break;
  1098. case POOL_STRING_ARRAY: {
  1099. PoolVector<String> vec = operator PoolVector<String>();
  1100. String str("[");
  1101. for (int i = 0; i < vec.size(); i++) {
  1102. if (i > 0)
  1103. str += ", ";
  1104. str = str + vec[i];
  1105. }
  1106. str += "]";
  1107. return str;
  1108. } break;
  1109. case POOL_INT_ARRAY: {
  1110. PoolVector<int> vec = operator PoolVector<int>();
  1111. String str("[");
  1112. for (int i = 0; i < vec.size(); i++) {
  1113. if (i > 0)
  1114. str += ", ";
  1115. str = str + itos(vec[i]);
  1116. }
  1117. str += "]";
  1118. return str;
  1119. } break;
  1120. case POOL_REAL_ARRAY: {
  1121. PoolVector<real_t> vec = operator PoolVector<real_t>();
  1122. String str("[");
  1123. for (int i = 0; i < vec.size(); i++) {
  1124. if (i > 0)
  1125. str += ", ";
  1126. str = str + rtos(vec[i]);
  1127. }
  1128. str += "]";
  1129. return str;
  1130. } break;
  1131. case ARRAY: {
  1132. Array arr = operator Array();
  1133. String str("[");
  1134. for (int i = 0; i < arr.size(); i++) {
  1135. if (i)
  1136. str += ", ";
  1137. str += String(arr[i]);
  1138. };
  1139. str += "]";
  1140. return str;
  1141. } break;
  1142. case OBJECT: {
  1143. if (_get_obj().obj) {
  1144. #ifdef DEBUG_ENABLED
  1145. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null()) {
  1146. //only if debugging!
  1147. if (!ObjectDB::instance_validate(_get_obj().obj)) {
  1148. return "[Deleted Object]";
  1149. };
  1150. };
  1151. #endif
  1152. return "[" + _get_obj().obj->get_class() + ":" + itos(_get_obj().obj->get_instance_id()) + "]";
  1153. } else
  1154. return "[Object:null]";
  1155. } break;
  1156. default: {
  1157. return "[" + get_type_name(type) + "]";
  1158. }
  1159. }
  1160. return "";
  1161. }
  1162. Variant::operator Vector2() const {
  1163. if (type == VECTOR2)
  1164. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1165. else if (type == VECTOR3)
  1166. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1167. else
  1168. return Vector2();
  1169. }
  1170. Variant::operator Rect2() const {
  1171. if (type == RECT2)
  1172. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1173. else
  1174. return Rect2();
  1175. }
  1176. Variant::operator Vector3() const {
  1177. if (type == VECTOR3)
  1178. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1179. else if (type == VECTOR2)
  1180. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1181. else
  1182. return Vector3();
  1183. }
  1184. Variant::operator Plane() const {
  1185. if (type == PLANE)
  1186. return *reinterpret_cast<const Plane *>(_data._mem);
  1187. else
  1188. return Plane();
  1189. }
  1190. Variant::operator ::AABB() const {
  1191. if (type == AABB)
  1192. return *_data._aabb;
  1193. else
  1194. return ::AABB();
  1195. }
  1196. Variant::operator Basis() const {
  1197. if (type == BASIS)
  1198. return *_data._basis;
  1199. else if (type == QUAT)
  1200. return *reinterpret_cast<const Quat *>(_data._mem);
  1201. else if (type == VECTOR3) {
  1202. return Basis(*reinterpret_cast<const Vector3 *>(_data._mem));
  1203. } else if (type == TRANSFORM) // unexposed in Variant::can_convert?
  1204. return _data._transform->basis;
  1205. else
  1206. return Basis();
  1207. }
  1208. Variant::operator Quat() const {
  1209. if (type == QUAT)
  1210. return *reinterpret_cast<const Quat *>(_data._mem);
  1211. else if (type == BASIS)
  1212. return *_data._basis;
  1213. else if (type == TRANSFORM)
  1214. return _data._transform->basis;
  1215. else
  1216. return Quat();
  1217. }
  1218. Variant::operator Transform() const {
  1219. if (type == TRANSFORM)
  1220. return *_data._transform;
  1221. else if (type == BASIS)
  1222. return Transform(*_data._basis, Vector3());
  1223. else if (type == QUAT)
  1224. return Transform(Basis(*reinterpret_cast<const Quat *>(_data._mem)), Vector3());
  1225. else
  1226. return Transform();
  1227. }
  1228. Variant::operator Transform2D() const {
  1229. if (type == TRANSFORM2D) {
  1230. return *_data._transform2d;
  1231. } else if (type == TRANSFORM) {
  1232. const Transform &t = *_data._transform;
  1233. Transform2D m;
  1234. m.elements[0][0] = t.basis.elements[0][0];
  1235. m.elements[0][1] = t.basis.elements[1][0];
  1236. m.elements[1][0] = t.basis.elements[0][1];
  1237. m.elements[1][1] = t.basis.elements[1][1];
  1238. m.elements[2][0] = t.origin[0];
  1239. m.elements[2][1] = t.origin[1];
  1240. return m;
  1241. } else
  1242. return Transform2D();
  1243. }
  1244. Variant::operator Color() const {
  1245. if (type == COLOR)
  1246. return *reinterpret_cast<const Color *>(_data._mem);
  1247. else if (type == STRING)
  1248. return Color::html(operator String());
  1249. else if (type == INT)
  1250. return Color::hex(operator int());
  1251. else
  1252. return Color();
  1253. }
  1254. Variant::operator NodePath() const {
  1255. if (type == NODE_PATH)
  1256. return *reinterpret_cast<const NodePath *>(_data._mem);
  1257. else if (type == STRING)
  1258. return NodePath(operator String());
  1259. else
  1260. return NodePath();
  1261. }
  1262. Variant::operator RefPtr() const {
  1263. if (type == OBJECT)
  1264. return _get_obj().ref;
  1265. else
  1266. return RefPtr();
  1267. }
  1268. Variant::operator RID() const {
  1269. if (type == _RID)
  1270. return *reinterpret_cast<const RID *>(_data._mem);
  1271. else if (type == OBJECT && !_get_obj().ref.is_null()) {
  1272. return _get_obj().ref.get_rid();
  1273. } else if (type == OBJECT && _get_obj().obj) {
  1274. Variant::CallError ce;
  1275. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid, NULL, 0, ce);
  1276. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() == Variant::_RID) {
  1277. return ret;
  1278. }
  1279. return RID();
  1280. } else {
  1281. return RID();
  1282. }
  1283. }
  1284. Variant::operator Object *() const {
  1285. if (type == OBJECT)
  1286. return _get_obj().obj;
  1287. else
  1288. return NULL;
  1289. }
  1290. Variant::operator Node *() const {
  1291. if (type == OBJECT)
  1292. return Object::cast_to<Node>(_get_obj().obj);
  1293. else
  1294. return NULL;
  1295. }
  1296. Variant::operator Control *() const {
  1297. if (type == OBJECT)
  1298. return Object::cast_to<Control>(_get_obj().obj);
  1299. else
  1300. return NULL;
  1301. }
  1302. Variant::operator Dictionary() const {
  1303. if (type == DICTIONARY)
  1304. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1305. else
  1306. return Dictionary();
  1307. }
  1308. template <class DA, class SA>
  1309. inline DA _convert_array(const SA &p_array) {
  1310. DA da;
  1311. da.resize(p_array.size());
  1312. for (int i = 0; i < p_array.size(); i++) {
  1313. da.set(i, Variant(p_array.get(i)));
  1314. }
  1315. return da;
  1316. }
  1317. template <class DA>
  1318. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1319. switch (p_variant.get_type()) {
  1320. case Variant::ARRAY: {
  1321. return _convert_array<DA, Array>(p_variant.operator Array());
  1322. }
  1323. case Variant::POOL_BYTE_ARRAY: {
  1324. return _convert_array<DA, PoolVector<uint8_t> >(p_variant.operator PoolVector<uint8_t>());
  1325. }
  1326. case Variant::POOL_INT_ARRAY: {
  1327. return _convert_array<DA, PoolVector<int> >(p_variant.operator PoolVector<int>());
  1328. }
  1329. case Variant::POOL_REAL_ARRAY: {
  1330. return _convert_array<DA, PoolVector<real_t> >(p_variant.operator PoolVector<real_t>());
  1331. }
  1332. case Variant::POOL_STRING_ARRAY: {
  1333. return _convert_array<DA, PoolVector<String> >(p_variant.operator PoolVector<String>());
  1334. }
  1335. case Variant::POOL_VECTOR2_ARRAY: {
  1336. return _convert_array<DA, PoolVector<Vector2> >(p_variant.operator PoolVector<Vector2>());
  1337. }
  1338. case Variant::POOL_VECTOR3_ARRAY: {
  1339. return _convert_array<DA, PoolVector<Vector3> >(p_variant.operator PoolVector<Vector3>());
  1340. }
  1341. case Variant::POOL_COLOR_ARRAY: {
  1342. return _convert_array<DA, PoolVector<Color> >(p_variant.operator PoolVector<Color>());
  1343. }
  1344. default: { return DA(); }
  1345. }
  1346. return DA();
  1347. }
  1348. Variant::operator Array() const {
  1349. if (type == ARRAY)
  1350. return *reinterpret_cast<const Array *>(_data._mem);
  1351. else
  1352. return _convert_array_from_variant<Array>(*this);
  1353. }
  1354. Variant::operator PoolVector<uint8_t>() const {
  1355. if (type == POOL_BYTE_ARRAY)
  1356. return *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  1357. else
  1358. return _convert_array_from_variant<PoolVector<uint8_t> >(*this);
  1359. }
  1360. Variant::operator PoolVector<int>() const {
  1361. if (type == POOL_INT_ARRAY)
  1362. return *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  1363. else
  1364. return _convert_array_from_variant<PoolVector<int> >(*this);
  1365. }
  1366. Variant::operator PoolVector<real_t>() const {
  1367. if (type == POOL_REAL_ARRAY)
  1368. return *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  1369. else
  1370. return _convert_array_from_variant<PoolVector<real_t> >(*this);
  1371. }
  1372. Variant::operator PoolVector<String>() const {
  1373. if (type == POOL_STRING_ARRAY)
  1374. return *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  1375. else
  1376. return _convert_array_from_variant<PoolVector<String> >(*this);
  1377. }
  1378. Variant::operator PoolVector<Vector3>() const {
  1379. if (type == POOL_VECTOR3_ARRAY)
  1380. return *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  1381. else
  1382. return _convert_array_from_variant<PoolVector<Vector3> >(*this);
  1383. }
  1384. Variant::operator PoolVector<Vector2>() const {
  1385. if (type == POOL_VECTOR2_ARRAY)
  1386. return *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  1387. else
  1388. return _convert_array_from_variant<PoolVector<Vector2> >(*this);
  1389. }
  1390. Variant::operator PoolVector<Color>() const {
  1391. if (type == POOL_COLOR_ARRAY)
  1392. return *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  1393. else
  1394. return _convert_array_from_variant<PoolVector<Color> >(*this);
  1395. }
  1396. /* helpers */
  1397. Variant::operator Vector<RID>() const {
  1398. Array va = operator Array();
  1399. Vector<RID> rids;
  1400. rids.resize(va.size());
  1401. for (int i = 0; i < rids.size(); i++)
  1402. rids.write[i] = va[i];
  1403. return rids;
  1404. }
  1405. Variant::operator Vector<Vector2>() const {
  1406. PoolVector<Vector2> from = operator PoolVector<Vector2>();
  1407. Vector<Vector2> to;
  1408. int len = from.size();
  1409. if (len == 0)
  1410. return Vector<Vector2>();
  1411. to.resize(len);
  1412. PoolVector<Vector2>::Read r = from.read();
  1413. Vector2 *w = to.ptrw();
  1414. for (int i = 0; i < len; i++) {
  1415. w[i] = r[i];
  1416. }
  1417. return to;
  1418. }
  1419. Variant::operator PoolVector<Plane>() const {
  1420. Array va = operator Array();
  1421. PoolVector<Plane> planes;
  1422. int va_size = va.size();
  1423. if (va_size == 0)
  1424. return planes;
  1425. planes.resize(va_size);
  1426. PoolVector<Plane>::Write w = planes.write();
  1427. for (int i = 0; i < va_size; i++)
  1428. w[i] = va[i];
  1429. return planes;
  1430. }
  1431. Variant::operator PoolVector<Face3>() const {
  1432. PoolVector<Vector3> va = operator PoolVector<Vector3>();
  1433. PoolVector<Face3> faces;
  1434. int va_size = va.size();
  1435. if (va_size == 0)
  1436. return faces;
  1437. faces.resize(va_size / 3);
  1438. PoolVector<Face3>::Write w = faces.write();
  1439. PoolVector<Vector3>::Read r = va.read();
  1440. for (int i = 0; i < va_size; i++)
  1441. w[i / 3].vertex[i % 3] = r[i];
  1442. return faces;
  1443. }
  1444. Variant::operator Vector<Plane>() const {
  1445. Array va = operator Array();
  1446. Vector<Plane> planes;
  1447. int va_size = va.size();
  1448. if (va_size == 0)
  1449. return planes;
  1450. planes.resize(va_size);
  1451. for (int i = 0; i < va_size; i++)
  1452. planes.write[i] = va[i];
  1453. return planes;
  1454. }
  1455. Variant::operator Vector<Variant>() const {
  1456. Array from = operator Array();
  1457. Vector<Variant> to;
  1458. int len = from.size();
  1459. to.resize(len);
  1460. for (int i = 0; i < len; i++) {
  1461. to.write[i] = from[i];
  1462. }
  1463. return to;
  1464. }
  1465. Variant::operator Vector<uint8_t>() const {
  1466. PoolVector<uint8_t> from = operator PoolVector<uint8_t>();
  1467. Vector<uint8_t> to;
  1468. int len = from.size();
  1469. to.resize(len);
  1470. for (int i = 0; i < len; i++) {
  1471. to.write[i] = from[i];
  1472. }
  1473. return to;
  1474. }
  1475. Variant::operator Vector<int>() const {
  1476. PoolVector<int> from = operator PoolVector<int>();
  1477. Vector<int> to;
  1478. int len = from.size();
  1479. to.resize(len);
  1480. for (int i = 0; i < len; i++) {
  1481. to.write[i] = from[i];
  1482. }
  1483. return to;
  1484. }
  1485. Variant::operator Vector<real_t>() const {
  1486. PoolVector<real_t> from = operator PoolVector<real_t>();
  1487. Vector<real_t> to;
  1488. int len = from.size();
  1489. to.resize(len);
  1490. for (int i = 0; i < len; i++) {
  1491. to.write[i] = from[i];
  1492. }
  1493. return to;
  1494. }
  1495. Variant::operator Vector<String>() const {
  1496. PoolVector<String> from = operator PoolVector<String>();
  1497. Vector<String> to;
  1498. int len = from.size();
  1499. to.resize(len);
  1500. for (int i = 0; i < len; i++) {
  1501. to.write[i] = from[i];
  1502. }
  1503. return to;
  1504. }
  1505. Variant::operator Vector<StringName>() const {
  1506. PoolVector<String> from = operator PoolVector<String>();
  1507. Vector<StringName> to;
  1508. int len = from.size();
  1509. to.resize(len);
  1510. for (int i = 0; i < len; i++) {
  1511. to.write[i] = from[i];
  1512. }
  1513. return to;
  1514. }
  1515. Variant::operator Vector<Vector3>() const {
  1516. PoolVector<Vector3> from = operator PoolVector<Vector3>();
  1517. Vector<Vector3> to;
  1518. int len = from.size();
  1519. if (len == 0)
  1520. return Vector<Vector3>();
  1521. to.resize(len);
  1522. PoolVector<Vector3>::Read r = from.read();
  1523. Vector3 *w = to.ptrw();
  1524. for (int i = 0; i < len; i++) {
  1525. w[i] = r[i];
  1526. }
  1527. return to;
  1528. }
  1529. Variant::operator Vector<Color>() const {
  1530. PoolVector<Color> from = operator PoolVector<Color>();
  1531. Vector<Color> to;
  1532. int len = from.size();
  1533. if (len == 0)
  1534. return Vector<Color>();
  1535. to.resize(len);
  1536. PoolVector<Color>::Read r = from.read();
  1537. Color *w = to.ptrw();
  1538. for (int i = 0; i < len; i++) {
  1539. w[i] = r[i];
  1540. }
  1541. return to;
  1542. }
  1543. Variant::operator Margin() const {
  1544. return (Margin) operator int();
  1545. }
  1546. Variant::operator Orientation() const {
  1547. return (Orientation) operator int();
  1548. }
  1549. Variant::operator IP_Address() const {
  1550. if (type == POOL_REAL_ARRAY || type == POOL_INT_ARRAY || type == POOL_BYTE_ARRAY) {
  1551. PoolVector<int> addr = operator PoolVector<int>();
  1552. if (addr.size() == 4) {
  1553. return IP_Address(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  1554. }
  1555. }
  1556. return IP_Address(operator String());
  1557. }
  1558. Variant::Variant(bool p_bool) {
  1559. type = BOOL;
  1560. _data._bool = p_bool;
  1561. }
  1562. /*
  1563. Variant::Variant(long unsigned int p_long) {
  1564. type=INT;
  1565. _data._int=p_long;
  1566. };
  1567. */
  1568. Variant::Variant(signed int p_int) {
  1569. type = INT;
  1570. _data._int = p_int;
  1571. }
  1572. Variant::Variant(unsigned int p_int) {
  1573. type = INT;
  1574. _data._int = p_int;
  1575. }
  1576. #ifdef NEED_LONG_INT
  1577. Variant::Variant(signed long p_int) {
  1578. type = INT;
  1579. _data._int = p_int;
  1580. }
  1581. Variant::Variant(unsigned long p_int) {
  1582. type = INT;
  1583. _data._int = p_int;
  1584. }
  1585. #endif
  1586. Variant::Variant(int64_t p_int) {
  1587. type = INT;
  1588. _data._int = p_int;
  1589. }
  1590. Variant::Variant(uint64_t p_int) {
  1591. type = INT;
  1592. _data._int = p_int;
  1593. }
  1594. Variant::Variant(signed short p_short) {
  1595. type = INT;
  1596. _data._int = p_short;
  1597. }
  1598. Variant::Variant(unsigned short p_short) {
  1599. type = INT;
  1600. _data._int = p_short;
  1601. }
  1602. Variant::Variant(signed char p_char) {
  1603. type = INT;
  1604. _data._int = p_char;
  1605. }
  1606. Variant::Variant(unsigned char p_char) {
  1607. type = INT;
  1608. _data._int = p_char;
  1609. }
  1610. Variant::Variant(float p_float) {
  1611. type = REAL;
  1612. _data._real = p_float;
  1613. }
  1614. Variant::Variant(double p_double) {
  1615. type = REAL;
  1616. _data._real = p_double;
  1617. }
  1618. Variant::Variant(const StringName &p_string) {
  1619. type = STRING;
  1620. memnew_placement(_data._mem, String(p_string.operator String()));
  1621. }
  1622. Variant::Variant(const String &p_string) {
  1623. type = STRING;
  1624. memnew_placement(_data._mem, String(p_string));
  1625. }
  1626. Variant::Variant(const char *const p_cstring) {
  1627. type = STRING;
  1628. memnew_placement(_data._mem, String((const char *)p_cstring));
  1629. }
  1630. Variant::Variant(const CharType *p_wstring) {
  1631. type = STRING;
  1632. memnew_placement(_data._mem, String(p_wstring));
  1633. }
  1634. Variant::Variant(const Vector3 &p_vector3) {
  1635. type = VECTOR3;
  1636. memnew_placement(_data._mem, Vector3(p_vector3));
  1637. }
  1638. Variant::Variant(const Vector2 &p_vector2) {
  1639. type = VECTOR2;
  1640. memnew_placement(_data._mem, Vector2(p_vector2));
  1641. }
  1642. Variant::Variant(const Rect2 &p_rect2) {
  1643. type = RECT2;
  1644. memnew_placement(_data._mem, Rect2(p_rect2));
  1645. }
  1646. Variant::Variant(const Plane &p_plane) {
  1647. type = PLANE;
  1648. memnew_placement(_data._mem, Plane(p_plane));
  1649. }
  1650. Variant::Variant(const ::AABB &p_aabb) {
  1651. type = AABB;
  1652. _data._aabb = memnew(::AABB(p_aabb));
  1653. }
  1654. Variant::Variant(const Basis &p_matrix) {
  1655. type = BASIS;
  1656. _data._basis = memnew(Basis(p_matrix));
  1657. }
  1658. Variant::Variant(const Quat &p_quat) {
  1659. type = QUAT;
  1660. memnew_placement(_data._mem, Quat(p_quat));
  1661. }
  1662. Variant::Variant(const Transform &p_transform) {
  1663. type = TRANSFORM;
  1664. _data._transform = memnew(Transform(p_transform));
  1665. }
  1666. Variant::Variant(const Transform2D &p_transform) {
  1667. type = TRANSFORM2D;
  1668. _data._transform2d = memnew(Transform2D(p_transform));
  1669. }
  1670. Variant::Variant(const Color &p_color) {
  1671. type = COLOR;
  1672. memnew_placement(_data._mem, Color(p_color));
  1673. }
  1674. Variant::Variant(const NodePath &p_node_path) {
  1675. type = NODE_PATH;
  1676. memnew_placement(_data._mem, NodePath(p_node_path));
  1677. }
  1678. Variant::Variant(const RefPtr &p_resource) {
  1679. type = OBJECT;
  1680. memnew_placement(_data._mem, ObjData);
  1681. REF *ref = reinterpret_cast<REF *>(p_resource.get_data());
  1682. _get_obj().obj = ref->ptr();
  1683. _get_obj().ref = p_resource;
  1684. }
  1685. Variant::Variant(const RID &p_rid) {
  1686. type = _RID;
  1687. memnew_placement(_data._mem, RID(p_rid));
  1688. }
  1689. Variant::Variant(const Object *p_object) {
  1690. type = OBJECT;
  1691. memnew_placement(_data._mem, ObjData);
  1692. _get_obj().obj = const_cast<Object *>(p_object);
  1693. }
  1694. Variant::Variant(const Dictionary &p_dictionary) {
  1695. type = DICTIONARY;
  1696. memnew_placement(_data._mem, (Dictionary)(p_dictionary));
  1697. }
  1698. Variant::Variant(const Array &p_array) {
  1699. type = ARRAY;
  1700. memnew_placement(_data._mem, Array(p_array));
  1701. }
  1702. Variant::Variant(const PoolVector<Plane> &p_array) {
  1703. type = ARRAY;
  1704. Array *plane_array = memnew_placement(_data._mem, Array);
  1705. plane_array->resize(p_array.size());
  1706. for (int i = 0; i < p_array.size(); i++) {
  1707. plane_array->operator[](i) = Variant(p_array[i]);
  1708. }
  1709. }
  1710. Variant::Variant(const Vector<Plane> &p_array) {
  1711. type = ARRAY;
  1712. Array *plane_array = memnew_placement(_data._mem, Array);
  1713. plane_array->resize(p_array.size());
  1714. for (int i = 0; i < p_array.size(); i++) {
  1715. plane_array->operator[](i) = Variant(p_array[i]);
  1716. }
  1717. }
  1718. Variant::Variant(const Vector<RID> &p_array) {
  1719. type = ARRAY;
  1720. Array *rid_array = memnew_placement(_data._mem, Array);
  1721. rid_array->resize(p_array.size());
  1722. for (int i = 0; i < p_array.size(); i++) {
  1723. rid_array->set(i, Variant(p_array[i]));
  1724. }
  1725. }
  1726. Variant::Variant(const Vector<Vector2> &p_array) {
  1727. type = NIL;
  1728. PoolVector<Vector2> v;
  1729. int len = p_array.size();
  1730. if (len > 0) {
  1731. v.resize(len);
  1732. PoolVector<Vector2>::Write w = v.write();
  1733. const Vector2 *r = p_array.ptr();
  1734. for (int i = 0; i < len; i++)
  1735. w[i] = r[i];
  1736. }
  1737. *this = v;
  1738. }
  1739. Variant::Variant(const PoolVector<uint8_t> &p_raw_array) {
  1740. type = POOL_BYTE_ARRAY;
  1741. memnew_placement(_data._mem, PoolVector<uint8_t>(p_raw_array));
  1742. }
  1743. Variant::Variant(const PoolVector<int> &p_int_array) {
  1744. type = POOL_INT_ARRAY;
  1745. memnew_placement(_data._mem, PoolVector<int>(p_int_array));
  1746. }
  1747. Variant::Variant(const PoolVector<real_t> &p_real_array) {
  1748. type = POOL_REAL_ARRAY;
  1749. memnew_placement(_data._mem, PoolVector<real_t>(p_real_array));
  1750. }
  1751. Variant::Variant(const PoolVector<String> &p_string_array) {
  1752. type = POOL_STRING_ARRAY;
  1753. memnew_placement(_data._mem, PoolVector<String>(p_string_array));
  1754. }
  1755. Variant::Variant(const PoolVector<Vector3> &p_vector3_array) {
  1756. type = POOL_VECTOR3_ARRAY;
  1757. memnew_placement(_data._mem, PoolVector<Vector3>(p_vector3_array));
  1758. }
  1759. Variant::Variant(const PoolVector<Vector2> &p_vector2_array) {
  1760. type = POOL_VECTOR2_ARRAY;
  1761. memnew_placement(_data._mem, PoolVector<Vector2>(p_vector2_array));
  1762. }
  1763. Variant::Variant(const PoolVector<Color> &p_color_array) {
  1764. type = POOL_COLOR_ARRAY;
  1765. memnew_placement(_data._mem, PoolVector<Color>(p_color_array));
  1766. }
  1767. Variant::Variant(const PoolVector<Face3> &p_face_array) {
  1768. PoolVector<Vector3> vertices;
  1769. int face_count = p_face_array.size();
  1770. vertices.resize(face_count * 3);
  1771. if (face_count) {
  1772. PoolVector<Face3>::Read r = p_face_array.read();
  1773. PoolVector<Vector3>::Write w = vertices.write();
  1774. for (int i = 0; i < face_count; i++) {
  1775. for (int j = 0; j < 3; j++)
  1776. w[i * 3 + j] = r[i].vertex[j];
  1777. }
  1778. r = PoolVector<Face3>::Read();
  1779. w = PoolVector<Vector3>::Write();
  1780. }
  1781. type = NIL;
  1782. *this = vertices;
  1783. }
  1784. /* helpers */
  1785. Variant::Variant(const Vector<Variant> &p_array) {
  1786. type = NIL;
  1787. Array v;
  1788. int len = p_array.size();
  1789. v.resize(len);
  1790. for (int i = 0; i < len; i++)
  1791. v.set(i, p_array[i]);
  1792. *this = v;
  1793. }
  1794. Variant::Variant(const Vector<uint8_t> &p_array) {
  1795. type = NIL;
  1796. PoolVector<uint8_t> v;
  1797. int len = p_array.size();
  1798. v.resize(len);
  1799. for (int i = 0; i < len; i++)
  1800. v.set(i, p_array[i]);
  1801. *this = v;
  1802. }
  1803. Variant::Variant(const Vector<int> &p_array) {
  1804. type = NIL;
  1805. PoolVector<int> v;
  1806. int len = p_array.size();
  1807. v.resize(len);
  1808. for (int i = 0; i < len; i++)
  1809. v.set(i, p_array[i]);
  1810. *this = v;
  1811. }
  1812. Variant::Variant(const Vector<real_t> &p_array) {
  1813. type = NIL;
  1814. PoolVector<real_t> v;
  1815. int len = p_array.size();
  1816. v.resize(len);
  1817. for (int i = 0; i < len; i++)
  1818. v.set(i, p_array[i]);
  1819. *this = v;
  1820. }
  1821. Variant::Variant(const Vector<String> &p_array) {
  1822. type = NIL;
  1823. PoolVector<String> v;
  1824. int len = p_array.size();
  1825. v.resize(len);
  1826. for (int i = 0; i < len; i++)
  1827. v.set(i, p_array[i]);
  1828. *this = v;
  1829. }
  1830. Variant::Variant(const Vector<StringName> &p_array) {
  1831. type = NIL;
  1832. PoolVector<String> v;
  1833. int len = p_array.size();
  1834. v.resize(len);
  1835. for (int i = 0; i < len; i++)
  1836. v.set(i, p_array[i]);
  1837. *this = v;
  1838. }
  1839. Variant::Variant(const Vector<Vector3> &p_array) {
  1840. type = NIL;
  1841. PoolVector<Vector3> v;
  1842. int len = p_array.size();
  1843. if (len > 0) {
  1844. v.resize(len);
  1845. PoolVector<Vector3>::Write w = v.write();
  1846. const Vector3 *r = p_array.ptr();
  1847. for (int i = 0; i < len; i++)
  1848. w[i] = r[i];
  1849. }
  1850. *this = v;
  1851. }
  1852. Variant::Variant(const Vector<Color> &p_array) {
  1853. type = NIL;
  1854. PoolVector<Color> v;
  1855. int len = p_array.size();
  1856. v.resize(len);
  1857. for (int i = 0; i < len; i++)
  1858. v.set(i, p_array[i]);
  1859. *this = v;
  1860. }
  1861. void Variant::operator=(const Variant &p_variant) {
  1862. if (unlikely(this == &p_variant))
  1863. return;
  1864. if (unlikely(type != p_variant.type)) {
  1865. reference(p_variant);
  1866. return;
  1867. }
  1868. switch (p_variant.type) {
  1869. case NIL: {
  1870. // none
  1871. } break;
  1872. // atomic types
  1873. case BOOL: {
  1874. _data._bool = p_variant._data._bool;
  1875. } break;
  1876. case INT: {
  1877. _data._int = p_variant._data._int;
  1878. } break;
  1879. case REAL: {
  1880. _data._real = p_variant._data._real;
  1881. } break;
  1882. case STRING: {
  1883. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  1884. } break;
  1885. // math types
  1886. case VECTOR2: {
  1887. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  1888. } break;
  1889. case RECT2: {
  1890. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  1891. } break;
  1892. case TRANSFORM2D: {
  1893. *_data._transform2d = *(p_variant._data._transform2d);
  1894. } break;
  1895. case VECTOR3: {
  1896. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  1897. } break;
  1898. case PLANE: {
  1899. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  1900. } break;
  1901. case AABB: {
  1902. *_data._aabb = *(p_variant._data._aabb);
  1903. } break;
  1904. case QUAT: {
  1905. *reinterpret_cast<Quat *>(_data._mem) = *reinterpret_cast<const Quat *>(p_variant._data._mem);
  1906. } break;
  1907. case BASIS: {
  1908. *_data._basis = *(p_variant._data._basis);
  1909. } break;
  1910. case TRANSFORM: {
  1911. *_data._transform = *(p_variant._data._transform);
  1912. } break;
  1913. // misc types
  1914. case COLOR: {
  1915. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  1916. } break;
  1917. case _RID: {
  1918. *reinterpret_cast<RID *>(_data._mem) = *reinterpret_cast<const RID *>(p_variant._data._mem);
  1919. } break;
  1920. case OBJECT: {
  1921. *reinterpret_cast<ObjData *>(_data._mem) = p_variant._get_obj();
  1922. } break;
  1923. case NODE_PATH: {
  1924. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  1925. } break;
  1926. case DICTIONARY: {
  1927. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  1928. } break;
  1929. case ARRAY: {
  1930. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  1931. } break;
  1932. // arrays
  1933. case POOL_BYTE_ARRAY: {
  1934. *reinterpret_cast<PoolVector<uint8_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem);
  1935. } break;
  1936. case POOL_INT_ARRAY: {
  1937. *reinterpret_cast<PoolVector<int> *>(_data._mem) = *reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem);
  1938. } break;
  1939. case POOL_REAL_ARRAY: {
  1940. *reinterpret_cast<PoolVector<real_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem);
  1941. } break;
  1942. case POOL_STRING_ARRAY: {
  1943. *reinterpret_cast<PoolVector<String> *>(_data._mem) = *reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem);
  1944. } break;
  1945. case POOL_VECTOR2_ARRAY: {
  1946. *reinterpret_cast<PoolVector<Vector2> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem);
  1947. } break;
  1948. case POOL_VECTOR3_ARRAY: {
  1949. *reinterpret_cast<PoolVector<Vector3> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem);
  1950. } break;
  1951. case POOL_COLOR_ARRAY: {
  1952. *reinterpret_cast<PoolVector<Color> *>(_data._mem) = *reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem);
  1953. } break;
  1954. default: {}
  1955. }
  1956. }
  1957. Variant::Variant(const IP_Address &p_address) {
  1958. type = STRING;
  1959. memnew_placement(_data._mem, String(p_address));
  1960. }
  1961. Variant::Variant(const Variant &p_variant) {
  1962. type = NIL;
  1963. reference(p_variant);
  1964. }
  1965. /*
  1966. Variant::~Variant() {
  1967. clear();
  1968. }*/
  1969. uint32_t Variant::hash() const {
  1970. switch (type) {
  1971. case NIL: {
  1972. return 0;
  1973. } break;
  1974. case BOOL: {
  1975. return _data._bool ? 1 : 0;
  1976. } break;
  1977. case INT: {
  1978. return _data._int;
  1979. } break;
  1980. case REAL: {
  1981. return hash_djb2_one_float(_data._real);
  1982. } break;
  1983. case STRING: {
  1984. return reinterpret_cast<const String *>(_data._mem)->hash();
  1985. } break;
  1986. // math types
  1987. case VECTOR2: {
  1988. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->x);
  1989. return hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->y, hash);
  1990. } break;
  1991. case RECT2: {
  1992. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.x);
  1993. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.y, hash);
  1994. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash);
  1995. return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash);
  1996. } break;
  1997. case TRANSFORM2D: {
  1998. uint32_t hash = 5831;
  1999. for (int i = 0; i < 3; i++) {
  2000. for (int j = 0; j < 2; j++) {
  2001. hash = hash_djb2_one_float(_data._transform2d->elements[i][j], hash);
  2002. }
  2003. }
  2004. return hash;
  2005. } break;
  2006. case VECTOR3: {
  2007. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->x);
  2008. hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->y, hash);
  2009. return hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->z, hash);
  2010. } break;
  2011. case PLANE: {
  2012. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.x);
  2013. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.y, hash);
  2014. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.z, hash);
  2015. return hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->d, hash);
  2016. } break;
  2017. /*
  2018. case QUAT: {
  2019. } break;*/
  2020. case AABB: {
  2021. uint32_t hash = 5831;
  2022. for (int i = 0; i < 3; i++) {
  2023. hash = hash_djb2_one_float(_data._aabb->position[i], hash);
  2024. hash = hash_djb2_one_float(_data._aabb->size[i], hash);
  2025. }
  2026. return hash;
  2027. } break;
  2028. case QUAT: {
  2029. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->x);
  2030. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->y, hash);
  2031. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->z, hash);
  2032. return hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->w, hash);
  2033. } break;
  2034. case BASIS: {
  2035. uint32_t hash = 5831;
  2036. for (int i = 0; i < 3; i++) {
  2037. for (int j = 0; j < 3; j++) {
  2038. hash = hash_djb2_one_float(_data._basis->elements[i][j], hash);
  2039. }
  2040. }
  2041. return hash;
  2042. } break;
  2043. case TRANSFORM: {
  2044. uint32_t hash = 5831;
  2045. for (int i = 0; i < 3; i++) {
  2046. for (int j = 0; j < 3; j++) {
  2047. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j], hash);
  2048. }
  2049. hash = hash_djb2_one_float(_data._transform->origin[i], hash);
  2050. }
  2051. return hash;
  2052. } break;
  2053. // misc types
  2054. case COLOR: {
  2055. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->r);
  2056. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->g, hash);
  2057. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->b, hash);
  2058. return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash);
  2059. } break;
  2060. case _RID: {
  2061. return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id());
  2062. } break;
  2063. case OBJECT: {
  2064. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  2065. } break;
  2066. case NODE_PATH: {
  2067. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2068. } break;
  2069. case DICTIONARY: {
  2070. return reinterpret_cast<const Dictionary *>(_data._mem)->hash();
  2071. } break;
  2072. case ARRAY: {
  2073. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2074. return arr.hash();
  2075. } break;
  2076. case POOL_BYTE_ARRAY: {
  2077. const PoolVector<uint8_t> &arr = *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2078. int len = arr.size();
  2079. PoolVector<uint8_t>::Read r = arr.read();
  2080. return hash_djb2_buffer((uint8_t *)&r[0], len);
  2081. } break;
  2082. case POOL_INT_ARRAY: {
  2083. const PoolVector<int> &arr = *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2084. int len = arr.size();
  2085. PoolVector<int>::Read r = arr.read();
  2086. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(int));
  2087. } break;
  2088. case POOL_REAL_ARRAY: {
  2089. const PoolVector<real_t> &arr = *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2090. int len = arr.size();
  2091. PoolVector<real_t>::Read r = arr.read();
  2092. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(real_t));
  2093. } break;
  2094. case POOL_STRING_ARRAY: {
  2095. uint32_t hash = 5831;
  2096. const PoolVector<String> &arr = *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2097. int len = arr.size();
  2098. PoolVector<String>::Read r = arr.read();
  2099. for (int i = 0; i < len; i++) {
  2100. hash = hash_djb2_one_32(r[i].hash(), hash);
  2101. }
  2102. return hash;
  2103. } break;
  2104. case POOL_VECTOR2_ARRAY: {
  2105. uint32_t hash = 5831;
  2106. const PoolVector<Vector2> &arr = *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2107. int len = arr.size();
  2108. PoolVector<Vector2>::Read r = arr.read();
  2109. for (int i = 0; i < len; i++) {
  2110. hash = hash_djb2_one_float(r[i].x, hash);
  2111. hash = hash_djb2_one_float(r[i].y, hash);
  2112. }
  2113. return hash;
  2114. } break;
  2115. case POOL_VECTOR3_ARRAY: {
  2116. uint32_t hash = 5831;
  2117. const PoolVector<Vector3> &arr = *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2118. int len = arr.size();
  2119. PoolVector<Vector3>::Read r = arr.read();
  2120. for (int i = 0; i < len; i++) {
  2121. hash = hash_djb2_one_float(r[i].x, hash);
  2122. hash = hash_djb2_one_float(r[i].y, hash);
  2123. hash = hash_djb2_one_float(r[i].z, hash);
  2124. }
  2125. return hash;
  2126. } break;
  2127. case POOL_COLOR_ARRAY: {
  2128. uint32_t hash = 5831;
  2129. const PoolVector<Color> &arr = *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2130. int len = arr.size();
  2131. PoolVector<Color>::Read r = arr.read();
  2132. for (int i = 0; i < len; i++) {
  2133. hash = hash_djb2_one_float(r[i].r, hash);
  2134. hash = hash_djb2_one_float(r[i].g, hash);
  2135. hash = hash_djb2_one_float(r[i].b, hash);
  2136. hash = hash_djb2_one_float(r[i].a, hash);
  2137. }
  2138. return hash;
  2139. } break;
  2140. default: {}
  2141. }
  2142. return 0;
  2143. }
  2144. #define hash_compare_scalar(p_lhs, p_rhs) \
  2145. ((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs))
  2146. #define hash_compare_vector2(p_lhs, p_rhs) \
  2147. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2148. (hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2149. #define hash_compare_vector3(p_lhs, p_rhs) \
  2150. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2151. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2152. (hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2153. #define hash_compare_quat(p_lhs, p_rhs) \
  2154. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2155. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2156. (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
  2157. (hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2158. #define hash_compare_color(p_lhs, p_rhs) \
  2159. (hash_compare_scalar((p_lhs).r, (p_rhs).r)) && \
  2160. (hash_compare_scalar((p_lhs).g, (p_rhs).g)) && \
  2161. (hash_compare_scalar((p_lhs).b, (p_rhs).b)) && \
  2162. (hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2163. #define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2164. const PoolVector<p_type> &l = *reinterpret_cast<const PoolVector<p_type> *>(p_lhs); \
  2165. const PoolVector<p_type> &r = *reinterpret_cast<const PoolVector<p_type> *>(p_rhs); \
  2166. \
  2167. if (l.size() != r.size()) \
  2168. return false; \
  2169. \
  2170. PoolVector<p_type>::Read lr = l.read(); \
  2171. PoolVector<p_type>::Read rr = r.read(); \
  2172. \
  2173. for (int i = 0; i < l.size(); ++i) { \
  2174. if (!p_compare_func((lr[i]), (rr[i]))) \
  2175. return false; \
  2176. } \
  2177. \
  2178. return true
  2179. bool Variant::hash_compare(const Variant &p_variant) const {
  2180. if (type != p_variant.type)
  2181. return false;
  2182. switch (type) {
  2183. case REAL: {
  2184. return hash_compare_scalar(_data._real, p_variant._data._real);
  2185. } break;
  2186. case VECTOR2: {
  2187. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2188. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2189. return hash_compare_vector2(*l, *r);
  2190. } break;
  2191. case RECT2: {
  2192. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2193. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2194. return (hash_compare_vector2(l->position, r->position)) &&
  2195. (hash_compare_vector2(l->size, r->size));
  2196. } break;
  2197. case TRANSFORM2D: {
  2198. Transform2D *l = _data._transform2d;
  2199. Transform2D *r = p_variant._data._transform2d;
  2200. for (int i = 0; i < 3; i++) {
  2201. if (!(hash_compare_vector2(l->elements[i], r->elements[i])))
  2202. return false;
  2203. }
  2204. return true;
  2205. } break;
  2206. case VECTOR3: {
  2207. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2208. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2209. return hash_compare_vector3(*l, *r);
  2210. } break;
  2211. case PLANE: {
  2212. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2213. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2214. return (hash_compare_vector3(l->normal, r->normal)) &&
  2215. (hash_compare_scalar(l->d, r->d));
  2216. } break;
  2217. case AABB: {
  2218. const ::AABB *l = _data._aabb;
  2219. const ::AABB *r = p_variant._data._aabb;
  2220. return (hash_compare_vector3(l->position, r->position) &&
  2221. (hash_compare_vector3(l->size, r->size)));
  2222. } break;
  2223. case QUAT: {
  2224. const Quat *l = reinterpret_cast<const Quat *>(_data._mem);
  2225. const Quat *r = reinterpret_cast<const Quat *>(p_variant._data._mem);
  2226. return hash_compare_quat(*l, *r);
  2227. } break;
  2228. case BASIS: {
  2229. const Basis *l = _data._basis;
  2230. const Basis *r = p_variant._data._basis;
  2231. for (int i = 0; i < 3; i++) {
  2232. if (!(hash_compare_vector3(l->elements[i], r->elements[i])))
  2233. return false;
  2234. }
  2235. return true;
  2236. } break;
  2237. case TRANSFORM: {
  2238. const Transform *l = _data._transform;
  2239. const Transform *r = p_variant._data._transform;
  2240. for (int i = 0; i < 3; i++) {
  2241. if (!(hash_compare_vector3(l->basis.elements[i], r->basis.elements[i])))
  2242. return false;
  2243. }
  2244. return hash_compare_vector3(l->origin, r->origin);
  2245. } break;
  2246. case COLOR: {
  2247. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2248. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2249. return hash_compare_color(*l, *r);
  2250. } break;
  2251. case ARRAY: {
  2252. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2253. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2254. if (l.size() != r.size())
  2255. return false;
  2256. for (int i = 0; i < l.size(); ++i) {
  2257. if (!l[i].hash_compare(r[i]))
  2258. return false;
  2259. }
  2260. return true;
  2261. } break;
  2262. case POOL_REAL_ARRAY: {
  2263. hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
  2264. } break;
  2265. case POOL_VECTOR2_ARRAY: {
  2266. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
  2267. } break;
  2268. case POOL_VECTOR3_ARRAY: {
  2269. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
  2270. } break;
  2271. case POOL_COLOR_ARRAY: {
  2272. hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
  2273. } break;
  2274. default:
  2275. bool v;
  2276. Variant r;
  2277. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2278. return r;
  2279. }
  2280. return false;
  2281. }
  2282. bool Variant::is_ref() const {
  2283. return type == OBJECT && !_get_obj().ref.is_null();
  2284. }
  2285. Vector<Variant> varray() {
  2286. return Vector<Variant>();
  2287. }
  2288. Vector<Variant> varray(const Variant &p_arg1) {
  2289. Vector<Variant> v;
  2290. v.push_back(p_arg1);
  2291. return v;
  2292. }
  2293. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
  2294. Vector<Variant> v;
  2295. v.push_back(p_arg1);
  2296. v.push_back(p_arg2);
  2297. return v;
  2298. }
  2299. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  2300. Vector<Variant> v;
  2301. v.push_back(p_arg1);
  2302. v.push_back(p_arg2);
  2303. v.push_back(p_arg3);
  2304. return v;
  2305. }
  2306. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  2307. Vector<Variant> v;
  2308. v.push_back(p_arg1);
  2309. v.push_back(p_arg2);
  2310. v.push_back(p_arg3);
  2311. v.push_back(p_arg4);
  2312. return v;
  2313. }
  2314. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  2315. Vector<Variant> v;
  2316. v.push_back(p_arg1);
  2317. v.push_back(p_arg2);
  2318. v.push_back(p_arg3);
  2319. v.push_back(p_arg4);
  2320. v.push_back(p_arg5);
  2321. return v;
  2322. }
  2323. void Variant::static_assign(const Variant &p_variant) {
  2324. }
  2325. bool Variant::is_shared() const {
  2326. switch (type) {
  2327. case OBJECT: return true;
  2328. case ARRAY: return true;
  2329. case DICTIONARY: return true;
  2330. default: {}
  2331. }
  2332. return false;
  2333. }
  2334. Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  2335. VARIANT_ARGPTRS;
  2336. int argc = 0;
  2337. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  2338. if (argptr[i]->get_type() == Variant::NIL)
  2339. break;
  2340. argc++;
  2341. }
  2342. CallError error;
  2343. Variant ret = call(p_method, argptr, argc, error);
  2344. switch (error.error) {
  2345. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2346. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(error.expected) + "'.";
  2347. ERR_PRINT(err.utf8().get_data());
  2348. } break;
  2349. case CallError::CALL_ERROR_INVALID_METHOD: {
  2350. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  2351. ERR_PRINT(err.utf8().get_data());
  2352. } break;
  2353. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2354. String err = "Too many arguments for method '" + p_method + "'";
  2355. ERR_PRINT(err.utf8().get_data());
  2356. } break;
  2357. default: {}
  2358. }
  2359. return ret;
  2360. }
  2361. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  2362. r_value = Variant();
  2363. }
  2364. String Variant::get_construct_string() const {
  2365. String vars;
  2366. VariantWriter::write_to_string(*this, vars);
  2367. return vars;
  2368. }
  2369. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce) {
  2370. String err_text;
  2371. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2372. int errorarg = ce.argument;
  2373. if (p_argptrs) {
  2374. 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) + ".";
  2375. } else {
  2376. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(ce.expected) + ".";
  2377. }
  2378. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2379. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2380. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2381. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2382. } else if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2383. err_text = "Method not found.";
  2384. } else if (ce.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2385. err_text = "Instance is null";
  2386. } else if (ce.error == Variant::CallError::CALL_OK) {
  2387. return "Call OK";
  2388. }
  2389. String class_name = p_base->get_class();
  2390. Ref<Script> script = p_base->get_script();
  2391. if (script.is_valid() && script->get_path().is_resource_file()) {
  2392. class_name += "(" + script->get_path().get_file() + ")";
  2393. }
  2394. return "'" + class_name + "::" + String(p_method) + "': " + err_text;
  2395. }
  2396. String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  2397. Array args;
  2398. if (p1.get_type() != Variant::NIL) {
  2399. args.push_back(p1);
  2400. if (p2.get_type() != Variant::NIL) {
  2401. args.push_back(p2);
  2402. if (p3.get_type() != Variant::NIL) {
  2403. args.push_back(p3);
  2404. if (p4.get_type() != Variant::NIL) {
  2405. args.push_back(p4);
  2406. if (p5.get_type() != Variant::NIL) {
  2407. args.push_back(p5);
  2408. }
  2409. }
  2410. }
  2411. }
  2412. }
  2413. bool error = false;
  2414. String fmt = p_text.sprintf(args, &error);
  2415. ERR_FAIL_COND_V(error, String());
  2416. return fmt;
  2417. }