variant.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266
  1. /*************************************************************************/
  2. /* variant.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 if (type == TRANSFORM2D) {
  1226. const Transform2D &t = *_data._transform2d;
  1227. Transform m;
  1228. m.basis.elements[0][0] = t.elements[0][0];
  1229. m.basis.elements[1][0] = t.elements[0][1];
  1230. m.basis.elements[0][1] = t.elements[1][0];
  1231. m.basis.elements[1][1] = t.elements[1][1];
  1232. m.origin[0] = t.elements[2][0];
  1233. m.origin[1] = t.elements[2][1];
  1234. return m;
  1235. } else
  1236. return Transform();
  1237. }
  1238. Variant::operator Transform2D() const {
  1239. if (type == TRANSFORM2D) {
  1240. return *_data._transform2d;
  1241. } else if (type == TRANSFORM) {
  1242. const Transform &t = *_data._transform;
  1243. Transform2D m;
  1244. m.elements[0][0] = t.basis.elements[0][0];
  1245. m.elements[0][1] = t.basis.elements[1][0];
  1246. m.elements[1][0] = t.basis.elements[0][1];
  1247. m.elements[1][1] = t.basis.elements[1][1];
  1248. m.elements[2][0] = t.origin[0];
  1249. m.elements[2][1] = t.origin[1];
  1250. return m;
  1251. } else
  1252. return Transform2D();
  1253. }
  1254. Variant::operator Color() const {
  1255. if (type == COLOR)
  1256. return *reinterpret_cast<const Color *>(_data._mem);
  1257. else if (type == STRING)
  1258. return Color::html(operator String());
  1259. else if (type == INT)
  1260. return Color::hex(operator int());
  1261. else
  1262. return Color();
  1263. }
  1264. Variant::operator NodePath() const {
  1265. if (type == NODE_PATH)
  1266. return *reinterpret_cast<const NodePath *>(_data._mem);
  1267. else if (type == STRING)
  1268. return NodePath(operator String());
  1269. else
  1270. return NodePath();
  1271. }
  1272. Variant::operator RefPtr() const {
  1273. if (type == OBJECT)
  1274. return _get_obj().ref;
  1275. else
  1276. return RefPtr();
  1277. }
  1278. Variant::operator RID() const {
  1279. if (type == _RID)
  1280. return *reinterpret_cast<const RID *>(_data._mem);
  1281. else if (type == OBJECT && !_get_obj().ref.is_null()) {
  1282. return _get_obj().ref.get_rid();
  1283. } else if (type == OBJECT && _get_obj().obj) {
  1284. Variant::CallError ce;
  1285. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid, NULL, 0, ce);
  1286. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() == Variant::_RID) {
  1287. return ret;
  1288. }
  1289. return RID();
  1290. } else {
  1291. return RID();
  1292. }
  1293. }
  1294. Variant::operator Object *() const {
  1295. if (type == OBJECT)
  1296. return _get_obj().obj;
  1297. else
  1298. return NULL;
  1299. }
  1300. Variant::operator Node *() const {
  1301. if (type == OBJECT)
  1302. return Object::cast_to<Node>(_get_obj().obj);
  1303. else
  1304. return NULL;
  1305. }
  1306. Variant::operator Control *() const {
  1307. if (type == OBJECT)
  1308. return Object::cast_to<Control>(_get_obj().obj);
  1309. else
  1310. return NULL;
  1311. }
  1312. Variant::operator Dictionary() const {
  1313. if (type == DICTIONARY)
  1314. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1315. else
  1316. return Dictionary();
  1317. }
  1318. template <class DA, class SA>
  1319. inline DA _convert_array(const SA &p_array) {
  1320. DA da;
  1321. da.resize(p_array.size());
  1322. for (int i = 0; i < p_array.size(); i++) {
  1323. da.set(i, Variant(p_array.get(i)));
  1324. }
  1325. return da;
  1326. }
  1327. template <class DA>
  1328. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1329. switch (p_variant.get_type()) {
  1330. case Variant::ARRAY: {
  1331. return _convert_array<DA, Array>(p_variant.operator Array());
  1332. }
  1333. case Variant::POOL_BYTE_ARRAY: {
  1334. return _convert_array<DA, PoolVector<uint8_t> >(p_variant.operator PoolVector<uint8_t>());
  1335. }
  1336. case Variant::POOL_INT_ARRAY: {
  1337. return _convert_array<DA, PoolVector<int> >(p_variant.operator PoolVector<int>());
  1338. }
  1339. case Variant::POOL_REAL_ARRAY: {
  1340. return _convert_array<DA, PoolVector<real_t> >(p_variant.operator PoolVector<real_t>());
  1341. }
  1342. case Variant::POOL_STRING_ARRAY: {
  1343. return _convert_array<DA, PoolVector<String> >(p_variant.operator PoolVector<String>());
  1344. }
  1345. case Variant::POOL_VECTOR2_ARRAY: {
  1346. return _convert_array<DA, PoolVector<Vector2> >(p_variant.operator PoolVector<Vector2>());
  1347. }
  1348. case Variant::POOL_VECTOR3_ARRAY: {
  1349. return _convert_array<DA, PoolVector<Vector3> >(p_variant.operator PoolVector<Vector3>());
  1350. }
  1351. case Variant::POOL_COLOR_ARRAY: {
  1352. return _convert_array<DA, PoolVector<Color> >(p_variant.operator PoolVector<Color>());
  1353. }
  1354. default: { return DA(); }
  1355. }
  1356. return DA();
  1357. }
  1358. Variant::operator Array() const {
  1359. if (type == ARRAY)
  1360. return *reinterpret_cast<const Array *>(_data._mem);
  1361. else
  1362. return _convert_array_from_variant<Array>(*this);
  1363. }
  1364. Variant::operator PoolVector<uint8_t>() const {
  1365. if (type == POOL_BYTE_ARRAY)
  1366. return *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  1367. else
  1368. return _convert_array_from_variant<PoolVector<uint8_t> >(*this);
  1369. }
  1370. Variant::operator PoolVector<int>() const {
  1371. if (type == POOL_INT_ARRAY)
  1372. return *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  1373. else
  1374. return _convert_array_from_variant<PoolVector<int> >(*this);
  1375. }
  1376. Variant::operator PoolVector<real_t>() const {
  1377. if (type == POOL_REAL_ARRAY)
  1378. return *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  1379. else
  1380. return _convert_array_from_variant<PoolVector<real_t> >(*this);
  1381. }
  1382. Variant::operator PoolVector<String>() const {
  1383. if (type == POOL_STRING_ARRAY)
  1384. return *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  1385. else
  1386. return _convert_array_from_variant<PoolVector<String> >(*this);
  1387. }
  1388. Variant::operator PoolVector<Vector3>() const {
  1389. if (type == POOL_VECTOR3_ARRAY)
  1390. return *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  1391. else
  1392. return _convert_array_from_variant<PoolVector<Vector3> >(*this);
  1393. }
  1394. Variant::operator PoolVector<Vector2>() const {
  1395. if (type == POOL_VECTOR2_ARRAY)
  1396. return *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  1397. else
  1398. return _convert_array_from_variant<PoolVector<Vector2> >(*this);
  1399. }
  1400. Variant::operator PoolVector<Color>() const {
  1401. if (type == POOL_COLOR_ARRAY)
  1402. return *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  1403. else
  1404. return _convert_array_from_variant<PoolVector<Color> >(*this);
  1405. }
  1406. /* helpers */
  1407. Variant::operator Vector<RID>() const {
  1408. Array va = operator Array();
  1409. Vector<RID> rids;
  1410. rids.resize(va.size());
  1411. for (int i = 0; i < rids.size(); i++)
  1412. rids.write[i] = va[i];
  1413. return rids;
  1414. }
  1415. Variant::operator Vector<Vector2>() const {
  1416. PoolVector<Vector2> from = operator PoolVector<Vector2>();
  1417. Vector<Vector2> to;
  1418. int len = from.size();
  1419. if (len == 0)
  1420. return Vector<Vector2>();
  1421. to.resize(len);
  1422. PoolVector<Vector2>::Read r = from.read();
  1423. Vector2 *w = to.ptrw();
  1424. for (int i = 0; i < len; i++) {
  1425. w[i] = r[i];
  1426. }
  1427. return to;
  1428. }
  1429. Variant::operator PoolVector<Plane>() const {
  1430. Array va = operator Array();
  1431. PoolVector<Plane> planes;
  1432. int va_size = va.size();
  1433. if (va_size == 0)
  1434. return planes;
  1435. planes.resize(va_size);
  1436. PoolVector<Plane>::Write w = planes.write();
  1437. for (int i = 0; i < va_size; i++)
  1438. w[i] = va[i];
  1439. return planes;
  1440. }
  1441. Variant::operator PoolVector<Face3>() const {
  1442. PoolVector<Vector3> va = operator PoolVector<Vector3>();
  1443. PoolVector<Face3> faces;
  1444. int va_size = va.size();
  1445. if (va_size == 0)
  1446. return faces;
  1447. faces.resize(va_size / 3);
  1448. PoolVector<Face3>::Write w = faces.write();
  1449. PoolVector<Vector3>::Read r = va.read();
  1450. for (int i = 0; i < va_size; i++)
  1451. w[i / 3].vertex[i % 3] = r[i];
  1452. return faces;
  1453. }
  1454. Variant::operator Vector<Plane>() const {
  1455. Array va = operator Array();
  1456. Vector<Plane> planes;
  1457. int va_size = va.size();
  1458. if (va_size == 0)
  1459. return planes;
  1460. planes.resize(va_size);
  1461. for (int i = 0; i < va_size; i++)
  1462. planes.write[i] = va[i];
  1463. return planes;
  1464. }
  1465. Variant::operator Vector<Variant>() const {
  1466. Array from = operator Array();
  1467. Vector<Variant> 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<uint8_t>() const {
  1476. PoolVector<uint8_t> from = operator PoolVector<uint8_t>();
  1477. Vector<uint8_t> 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<int>() const {
  1486. PoolVector<int> from = operator PoolVector<int>();
  1487. Vector<int> 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<real_t>() const {
  1496. PoolVector<real_t> from = operator PoolVector<real_t>();
  1497. Vector<real_t> 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<String>() const {
  1506. PoolVector<String> from = operator PoolVector<String>();
  1507. Vector<String> 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<StringName>() const {
  1516. PoolVector<String> from = operator PoolVector<String>();
  1517. Vector<StringName> to;
  1518. int len = from.size();
  1519. to.resize(len);
  1520. for (int i = 0; i < len; i++) {
  1521. to.write[i] = from[i];
  1522. }
  1523. return to;
  1524. }
  1525. Variant::operator Vector<Vector3>() const {
  1526. PoolVector<Vector3> from = operator PoolVector<Vector3>();
  1527. Vector<Vector3> to;
  1528. int len = from.size();
  1529. if (len == 0)
  1530. return Vector<Vector3>();
  1531. to.resize(len);
  1532. PoolVector<Vector3>::Read r = from.read();
  1533. Vector3 *w = to.ptrw();
  1534. for (int i = 0; i < len; i++) {
  1535. w[i] = r[i];
  1536. }
  1537. return to;
  1538. }
  1539. Variant::operator Vector<Color>() const {
  1540. PoolVector<Color> from = operator PoolVector<Color>();
  1541. Vector<Color> to;
  1542. int len = from.size();
  1543. if (len == 0)
  1544. return Vector<Color>();
  1545. to.resize(len);
  1546. PoolVector<Color>::Read r = from.read();
  1547. Color *w = to.ptrw();
  1548. for (int i = 0; i < len; i++) {
  1549. w[i] = r[i];
  1550. }
  1551. return to;
  1552. }
  1553. Variant::operator Margin() const {
  1554. return (Margin) operator int();
  1555. }
  1556. Variant::operator Orientation() const {
  1557. return (Orientation) operator int();
  1558. }
  1559. Variant::operator IP_Address() const {
  1560. if (type == POOL_REAL_ARRAY || type == POOL_INT_ARRAY || type == POOL_BYTE_ARRAY) {
  1561. PoolVector<int> addr = operator PoolVector<int>();
  1562. if (addr.size() == 4) {
  1563. return IP_Address(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  1564. }
  1565. }
  1566. return IP_Address(operator String());
  1567. }
  1568. Variant::Variant(bool p_bool) {
  1569. type = BOOL;
  1570. _data._bool = p_bool;
  1571. }
  1572. /*
  1573. Variant::Variant(long unsigned int p_long) {
  1574. type=INT;
  1575. _data._int=p_long;
  1576. };
  1577. */
  1578. Variant::Variant(signed int p_int) {
  1579. type = INT;
  1580. _data._int = p_int;
  1581. }
  1582. Variant::Variant(unsigned int p_int) {
  1583. type = INT;
  1584. _data._int = p_int;
  1585. }
  1586. #ifdef NEED_LONG_INT
  1587. Variant::Variant(signed long p_int) {
  1588. type = INT;
  1589. _data._int = p_int;
  1590. }
  1591. Variant::Variant(unsigned long p_int) {
  1592. type = INT;
  1593. _data._int = p_int;
  1594. }
  1595. #endif
  1596. Variant::Variant(int64_t p_int) {
  1597. type = INT;
  1598. _data._int = p_int;
  1599. }
  1600. Variant::Variant(uint64_t p_int) {
  1601. type = INT;
  1602. _data._int = p_int;
  1603. }
  1604. Variant::Variant(signed short p_short) {
  1605. type = INT;
  1606. _data._int = p_short;
  1607. }
  1608. Variant::Variant(unsigned short p_short) {
  1609. type = INT;
  1610. _data._int = p_short;
  1611. }
  1612. Variant::Variant(signed char p_char) {
  1613. type = INT;
  1614. _data._int = p_char;
  1615. }
  1616. Variant::Variant(unsigned char p_char) {
  1617. type = INT;
  1618. _data._int = p_char;
  1619. }
  1620. Variant::Variant(float p_float) {
  1621. type = REAL;
  1622. _data._real = p_float;
  1623. }
  1624. Variant::Variant(double p_double) {
  1625. type = REAL;
  1626. _data._real = p_double;
  1627. }
  1628. Variant::Variant(const StringName &p_string) {
  1629. type = STRING;
  1630. memnew_placement(_data._mem, String(p_string.operator String()));
  1631. }
  1632. Variant::Variant(const String &p_string) {
  1633. type = STRING;
  1634. memnew_placement(_data._mem, String(p_string));
  1635. }
  1636. Variant::Variant(const char *const p_cstring) {
  1637. type = STRING;
  1638. memnew_placement(_data._mem, String((const char *)p_cstring));
  1639. }
  1640. Variant::Variant(const CharType *p_wstring) {
  1641. type = STRING;
  1642. memnew_placement(_data._mem, String(p_wstring));
  1643. }
  1644. Variant::Variant(const Vector3 &p_vector3) {
  1645. type = VECTOR3;
  1646. memnew_placement(_data._mem, Vector3(p_vector3));
  1647. }
  1648. Variant::Variant(const Vector2 &p_vector2) {
  1649. type = VECTOR2;
  1650. memnew_placement(_data._mem, Vector2(p_vector2));
  1651. }
  1652. Variant::Variant(const Rect2 &p_rect2) {
  1653. type = RECT2;
  1654. memnew_placement(_data._mem, Rect2(p_rect2));
  1655. }
  1656. Variant::Variant(const Plane &p_plane) {
  1657. type = PLANE;
  1658. memnew_placement(_data._mem, Plane(p_plane));
  1659. }
  1660. Variant::Variant(const ::AABB &p_aabb) {
  1661. type = AABB;
  1662. _data._aabb = memnew(::AABB(p_aabb));
  1663. }
  1664. Variant::Variant(const Basis &p_matrix) {
  1665. type = BASIS;
  1666. _data._basis = memnew(Basis(p_matrix));
  1667. }
  1668. Variant::Variant(const Quat &p_quat) {
  1669. type = QUAT;
  1670. memnew_placement(_data._mem, Quat(p_quat));
  1671. }
  1672. Variant::Variant(const Transform &p_transform) {
  1673. type = TRANSFORM;
  1674. _data._transform = memnew(Transform(p_transform));
  1675. }
  1676. Variant::Variant(const Transform2D &p_transform) {
  1677. type = TRANSFORM2D;
  1678. _data._transform2d = memnew(Transform2D(p_transform));
  1679. }
  1680. Variant::Variant(const Color &p_color) {
  1681. type = COLOR;
  1682. memnew_placement(_data._mem, Color(p_color));
  1683. }
  1684. Variant::Variant(const NodePath &p_node_path) {
  1685. type = NODE_PATH;
  1686. memnew_placement(_data._mem, NodePath(p_node_path));
  1687. }
  1688. Variant::Variant(const RefPtr &p_resource) {
  1689. type = OBJECT;
  1690. memnew_placement(_data._mem, ObjData);
  1691. REF *ref = reinterpret_cast<REF *>(p_resource.get_data());
  1692. _get_obj().obj = ref->ptr();
  1693. _get_obj().ref = p_resource;
  1694. }
  1695. Variant::Variant(const RID &p_rid) {
  1696. type = _RID;
  1697. memnew_placement(_data._mem, RID(p_rid));
  1698. }
  1699. Variant::Variant(const Object *p_object) {
  1700. type = OBJECT;
  1701. memnew_placement(_data._mem, ObjData);
  1702. _get_obj().obj = const_cast<Object *>(p_object);
  1703. }
  1704. Variant::Variant(const Dictionary &p_dictionary) {
  1705. type = DICTIONARY;
  1706. memnew_placement(_data._mem, (Dictionary)(p_dictionary));
  1707. }
  1708. Variant::Variant(const Array &p_array) {
  1709. type = ARRAY;
  1710. memnew_placement(_data._mem, Array(p_array));
  1711. }
  1712. Variant::Variant(const PoolVector<Plane> &p_array) {
  1713. type = ARRAY;
  1714. Array *plane_array = memnew_placement(_data._mem, Array);
  1715. plane_array->resize(p_array.size());
  1716. for (int i = 0; i < p_array.size(); i++) {
  1717. plane_array->operator[](i) = Variant(p_array[i]);
  1718. }
  1719. }
  1720. Variant::Variant(const Vector<Plane> &p_array) {
  1721. type = ARRAY;
  1722. Array *plane_array = memnew_placement(_data._mem, Array);
  1723. plane_array->resize(p_array.size());
  1724. for (int i = 0; i < p_array.size(); i++) {
  1725. plane_array->operator[](i) = Variant(p_array[i]);
  1726. }
  1727. }
  1728. Variant::Variant(const Vector<RID> &p_array) {
  1729. type = ARRAY;
  1730. Array *rid_array = memnew_placement(_data._mem, Array);
  1731. rid_array->resize(p_array.size());
  1732. for (int i = 0; i < p_array.size(); i++) {
  1733. rid_array->set(i, Variant(p_array[i]));
  1734. }
  1735. }
  1736. Variant::Variant(const Vector<Vector2> &p_array) {
  1737. type = NIL;
  1738. PoolVector<Vector2> v;
  1739. int len = p_array.size();
  1740. if (len > 0) {
  1741. v.resize(len);
  1742. PoolVector<Vector2>::Write w = v.write();
  1743. const Vector2 *r = p_array.ptr();
  1744. for (int i = 0; i < len; i++)
  1745. w[i] = r[i];
  1746. }
  1747. *this = v;
  1748. }
  1749. Variant::Variant(const PoolVector<uint8_t> &p_raw_array) {
  1750. type = POOL_BYTE_ARRAY;
  1751. memnew_placement(_data._mem, PoolVector<uint8_t>(p_raw_array));
  1752. }
  1753. Variant::Variant(const PoolVector<int> &p_int_array) {
  1754. type = POOL_INT_ARRAY;
  1755. memnew_placement(_data._mem, PoolVector<int>(p_int_array));
  1756. }
  1757. Variant::Variant(const PoolVector<real_t> &p_real_array) {
  1758. type = POOL_REAL_ARRAY;
  1759. memnew_placement(_data._mem, PoolVector<real_t>(p_real_array));
  1760. }
  1761. Variant::Variant(const PoolVector<String> &p_string_array) {
  1762. type = POOL_STRING_ARRAY;
  1763. memnew_placement(_data._mem, PoolVector<String>(p_string_array));
  1764. }
  1765. Variant::Variant(const PoolVector<Vector3> &p_vector3_array) {
  1766. type = POOL_VECTOR3_ARRAY;
  1767. memnew_placement(_data._mem, PoolVector<Vector3>(p_vector3_array));
  1768. }
  1769. Variant::Variant(const PoolVector<Vector2> &p_vector2_array) {
  1770. type = POOL_VECTOR2_ARRAY;
  1771. memnew_placement(_data._mem, PoolVector<Vector2>(p_vector2_array));
  1772. }
  1773. Variant::Variant(const PoolVector<Color> &p_color_array) {
  1774. type = POOL_COLOR_ARRAY;
  1775. memnew_placement(_data._mem, PoolVector<Color>(p_color_array));
  1776. }
  1777. Variant::Variant(const PoolVector<Face3> &p_face_array) {
  1778. PoolVector<Vector3> vertices;
  1779. int face_count = p_face_array.size();
  1780. vertices.resize(face_count * 3);
  1781. if (face_count) {
  1782. PoolVector<Face3>::Read r = p_face_array.read();
  1783. PoolVector<Vector3>::Write w = vertices.write();
  1784. for (int i = 0; i < face_count; i++) {
  1785. for (int j = 0; j < 3; j++)
  1786. w[i * 3 + j] = r[i].vertex[j];
  1787. }
  1788. r = PoolVector<Face3>::Read();
  1789. w = PoolVector<Vector3>::Write();
  1790. }
  1791. type = NIL;
  1792. *this = vertices;
  1793. }
  1794. /* helpers */
  1795. Variant::Variant(const Vector<Variant> &p_array) {
  1796. type = NIL;
  1797. Array v;
  1798. int len = p_array.size();
  1799. v.resize(len);
  1800. for (int i = 0; i < len; i++)
  1801. v.set(i, p_array[i]);
  1802. *this = v;
  1803. }
  1804. Variant::Variant(const Vector<uint8_t> &p_array) {
  1805. type = NIL;
  1806. PoolVector<uint8_t> v;
  1807. int len = p_array.size();
  1808. v.resize(len);
  1809. for (int i = 0; i < len; i++)
  1810. v.set(i, p_array[i]);
  1811. *this = v;
  1812. }
  1813. Variant::Variant(const Vector<int> &p_array) {
  1814. type = NIL;
  1815. PoolVector<int> v;
  1816. int len = p_array.size();
  1817. v.resize(len);
  1818. for (int i = 0; i < len; i++)
  1819. v.set(i, p_array[i]);
  1820. *this = v;
  1821. }
  1822. Variant::Variant(const Vector<real_t> &p_array) {
  1823. type = NIL;
  1824. PoolVector<real_t> v;
  1825. int len = p_array.size();
  1826. v.resize(len);
  1827. for (int i = 0; i < len; i++)
  1828. v.set(i, p_array[i]);
  1829. *this = v;
  1830. }
  1831. Variant::Variant(const Vector<String> &p_array) {
  1832. type = NIL;
  1833. PoolVector<String> v;
  1834. int len = p_array.size();
  1835. v.resize(len);
  1836. for (int i = 0; i < len; i++)
  1837. v.set(i, p_array[i]);
  1838. *this = v;
  1839. }
  1840. Variant::Variant(const Vector<StringName> &p_array) {
  1841. type = NIL;
  1842. PoolVector<String> v;
  1843. int len = p_array.size();
  1844. v.resize(len);
  1845. for (int i = 0; i < len; i++)
  1846. v.set(i, p_array[i]);
  1847. *this = v;
  1848. }
  1849. Variant::Variant(const Vector<Vector3> &p_array) {
  1850. type = NIL;
  1851. PoolVector<Vector3> v;
  1852. int len = p_array.size();
  1853. if (len > 0) {
  1854. v.resize(len);
  1855. PoolVector<Vector3>::Write w = v.write();
  1856. const Vector3 *r = p_array.ptr();
  1857. for (int i = 0; i < len; i++)
  1858. w[i] = r[i];
  1859. }
  1860. *this = v;
  1861. }
  1862. Variant::Variant(const Vector<Color> &p_array) {
  1863. type = NIL;
  1864. PoolVector<Color> v;
  1865. int len = p_array.size();
  1866. v.resize(len);
  1867. for (int i = 0; i < len; i++)
  1868. v.set(i, p_array[i]);
  1869. *this = v;
  1870. }
  1871. void Variant::operator=(const Variant &p_variant) {
  1872. if (unlikely(this == &p_variant))
  1873. return;
  1874. if (unlikely(type != p_variant.type)) {
  1875. reference(p_variant);
  1876. return;
  1877. }
  1878. switch (p_variant.type) {
  1879. case NIL: {
  1880. // none
  1881. } break;
  1882. // atomic types
  1883. case BOOL: {
  1884. _data._bool = p_variant._data._bool;
  1885. } break;
  1886. case INT: {
  1887. _data._int = p_variant._data._int;
  1888. } break;
  1889. case REAL: {
  1890. _data._real = p_variant._data._real;
  1891. } break;
  1892. case STRING: {
  1893. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  1894. } break;
  1895. // math types
  1896. case VECTOR2: {
  1897. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  1898. } break;
  1899. case RECT2: {
  1900. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  1901. } break;
  1902. case TRANSFORM2D: {
  1903. *_data._transform2d = *(p_variant._data._transform2d);
  1904. } break;
  1905. case VECTOR3: {
  1906. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  1907. } break;
  1908. case PLANE: {
  1909. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  1910. } break;
  1911. case AABB: {
  1912. *_data._aabb = *(p_variant._data._aabb);
  1913. } break;
  1914. case QUAT: {
  1915. *reinterpret_cast<Quat *>(_data._mem) = *reinterpret_cast<const Quat *>(p_variant._data._mem);
  1916. } break;
  1917. case BASIS: {
  1918. *_data._basis = *(p_variant._data._basis);
  1919. } break;
  1920. case TRANSFORM: {
  1921. *_data._transform = *(p_variant._data._transform);
  1922. } break;
  1923. // misc types
  1924. case COLOR: {
  1925. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  1926. } break;
  1927. case _RID: {
  1928. *reinterpret_cast<RID *>(_data._mem) = *reinterpret_cast<const RID *>(p_variant._data._mem);
  1929. } break;
  1930. case OBJECT: {
  1931. *reinterpret_cast<ObjData *>(_data._mem) = p_variant._get_obj();
  1932. } break;
  1933. case NODE_PATH: {
  1934. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  1935. } break;
  1936. case DICTIONARY: {
  1937. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  1938. } break;
  1939. case ARRAY: {
  1940. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  1941. } break;
  1942. // arrays
  1943. case POOL_BYTE_ARRAY: {
  1944. *reinterpret_cast<PoolVector<uint8_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem);
  1945. } break;
  1946. case POOL_INT_ARRAY: {
  1947. *reinterpret_cast<PoolVector<int> *>(_data._mem) = *reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem);
  1948. } break;
  1949. case POOL_REAL_ARRAY: {
  1950. *reinterpret_cast<PoolVector<real_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem);
  1951. } break;
  1952. case POOL_STRING_ARRAY: {
  1953. *reinterpret_cast<PoolVector<String> *>(_data._mem) = *reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem);
  1954. } break;
  1955. case POOL_VECTOR2_ARRAY: {
  1956. *reinterpret_cast<PoolVector<Vector2> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem);
  1957. } break;
  1958. case POOL_VECTOR3_ARRAY: {
  1959. *reinterpret_cast<PoolVector<Vector3> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem);
  1960. } break;
  1961. case POOL_COLOR_ARRAY: {
  1962. *reinterpret_cast<PoolVector<Color> *>(_data._mem) = *reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem);
  1963. } break;
  1964. default: {}
  1965. }
  1966. }
  1967. Variant::Variant(const IP_Address &p_address) {
  1968. type = STRING;
  1969. memnew_placement(_data._mem, String(p_address));
  1970. }
  1971. Variant::Variant(const Variant &p_variant) {
  1972. type = NIL;
  1973. reference(p_variant);
  1974. }
  1975. /*
  1976. Variant::~Variant() {
  1977. clear();
  1978. }*/
  1979. uint32_t Variant::hash() const {
  1980. switch (type) {
  1981. case NIL: {
  1982. return 0;
  1983. } break;
  1984. case BOOL: {
  1985. return _data._bool ? 1 : 0;
  1986. } break;
  1987. case INT: {
  1988. return _data._int;
  1989. } break;
  1990. case REAL: {
  1991. return hash_djb2_one_float(_data._real);
  1992. } break;
  1993. case STRING: {
  1994. return reinterpret_cast<const String *>(_data._mem)->hash();
  1995. } break;
  1996. // math types
  1997. case VECTOR2: {
  1998. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->x);
  1999. return hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->y, hash);
  2000. } break;
  2001. case RECT2: {
  2002. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.x);
  2003. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.y, hash);
  2004. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash);
  2005. return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash);
  2006. } break;
  2007. case TRANSFORM2D: {
  2008. uint32_t hash = 5831;
  2009. for (int i = 0; i < 3; i++) {
  2010. for (int j = 0; j < 2; j++) {
  2011. hash = hash_djb2_one_float(_data._transform2d->elements[i][j], hash);
  2012. }
  2013. }
  2014. return hash;
  2015. } break;
  2016. case VECTOR3: {
  2017. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->x);
  2018. hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->y, hash);
  2019. return hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->z, hash);
  2020. } break;
  2021. case PLANE: {
  2022. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.x);
  2023. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.y, hash);
  2024. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.z, hash);
  2025. return hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->d, hash);
  2026. } break;
  2027. /*
  2028. case QUAT: {
  2029. } break;*/
  2030. case AABB: {
  2031. uint32_t hash = 5831;
  2032. for (int i = 0; i < 3; i++) {
  2033. hash = hash_djb2_one_float(_data._aabb->position[i], hash);
  2034. hash = hash_djb2_one_float(_data._aabb->size[i], hash);
  2035. }
  2036. return hash;
  2037. } break;
  2038. case QUAT: {
  2039. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->x);
  2040. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->y, hash);
  2041. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->z, hash);
  2042. return hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->w, hash);
  2043. } break;
  2044. case BASIS: {
  2045. uint32_t hash = 5831;
  2046. for (int i = 0; i < 3; i++) {
  2047. for (int j = 0; j < 3; j++) {
  2048. hash = hash_djb2_one_float(_data._basis->elements[i][j], hash);
  2049. }
  2050. }
  2051. return hash;
  2052. } break;
  2053. case TRANSFORM: {
  2054. uint32_t hash = 5831;
  2055. for (int i = 0; i < 3; i++) {
  2056. for (int j = 0; j < 3; j++) {
  2057. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j], hash);
  2058. }
  2059. hash = hash_djb2_one_float(_data._transform->origin[i], hash);
  2060. }
  2061. return hash;
  2062. } break;
  2063. // misc types
  2064. case COLOR: {
  2065. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->r);
  2066. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->g, hash);
  2067. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->b, hash);
  2068. return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash);
  2069. } break;
  2070. case _RID: {
  2071. return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id());
  2072. } break;
  2073. case OBJECT: {
  2074. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  2075. } break;
  2076. case NODE_PATH: {
  2077. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2078. } break;
  2079. case DICTIONARY: {
  2080. return reinterpret_cast<const Dictionary *>(_data._mem)->hash();
  2081. } break;
  2082. case ARRAY: {
  2083. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2084. return arr.hash();
  2085. } break;
  2086. case POOL_BYTE_ARRAY: {
  2087. const PoolVector<uint8_t> &arr = *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2088. int len = arr.size();
  2089. PoolVector<uint8_t>::Read r = arr.read();
  2090. return hash_djb2_buffer((uint8_t *)&r[0], len);
  2091. } break;
  2092. case POOL_INT_ARRAY: {
  2093. const PoolVector<int> &arr = *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2094. int len = arr.size();
  2095. PoolVector<int>::Read r = arr.read();
  2096. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(int));
  2097. } break;
  2098. case POOL_REAL_ARRAY: {
  2099. const PoolVector<real_t> &arr = *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2100. int len = arr.size();
  2101. PoolVector<real_t>::Read r = arr.read();
  2102. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(real_t));
  2103. } break;
  2104. case POOL_STRING_ARRAY: {
  2105. uint32_t hash = 5831;
  2106. const PoolVector<String> &arr = *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2107. int len = arr.size();
  2108. PoolVector<String>::Read r = arr.read();
  2109. for (int i = 0; i < len; i++) {
  2110. hash = hash_djb2_one_32(r[i].hash(), hash);
  2111. }
  2112. return hash;
  2113. } break;
  2114. case POOL_VECTOR2_ARRAY: {
  2115. uint32_t hash = 5831;
  2116. const PoolVector<Vector2> &arr = *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2117. int len = arr.size();
  2118. PoolVector<Vector2>::Read r = arr.read();
  2119. for (int i = 0; i < len; i++) {
  2120. hash = hash_djb2_one_float(r[i].x, hash);
  2121. hash = hash_djb2_one_float(r[i].y, hash);
  2122. }
  2123. return hash;
  2124. } break;
  2125. case POOL_VECTOR3_ARRAY: {
  2126. uint32_t hash = 5831;
  2127. const PoolVector<Vector3> &arr = *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2128. int len = arr.size();
  2129. PoolVector<Vector3>::Read r = arr.read();
  2130. for (int i = 0; i < len; i++) {
  2131. hash = hash_djb2_one_float(r[i].x, hash);
  2132. hash = hash_djb2_one_float(r[i].y, hash);
  2133. hash = hash_djb2_one_float(r[i].z, hash);
  2134. }
  2135. return hash;
  2136. } break;
  2137. case POOL_COLOR_ARRAY: {
  2138. uint32_t hash = 5831;
  2139. const PoolVector<Color> &arr = *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2140. int len = arr.size();
  2141. PoolVector<Color>::Read r = arr.read();
  2142. for (int i = 0; i < len; i++) {
  2143. hash = hash_djb2_one_float(r[i].r, hash);
  2144. hash = hash_djb2_one_float(r[i].g, hash);
  2145. hash = hash_djb2_one_float(r[i].b, hash);
  2146. hash = hash_djb2_one_float(r[i].a, hash);
  2147. }
  2148. return hash;
  2149. } break;
  2150. default: {}
  2151. }
  2152. return 0;
  2153. }
  2154. #define hash_compare_scalar(p_lhs, p_rhs) \
  2155. ((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs))
  2156. #define hash_compare_vector2(p_lhs, p_rhs) \
  2157. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2158. (hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2159. #define hash_compare_vector3(p_lhs, p_rhs) \
  2160. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2161. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2162. (hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2163. #define hash_compare_quat(p_lhs, p_rhs) \
  2164. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2165. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2166. (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
  2167. (hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2168. #define hash_compare_color(p_lhs, p_rhs) \
  2169. (hash_compare_scalar((p_lhs).r, (p_rhs).r)) && \
  2170. (hash_compare_scalar((p_lhs).g, (p_rhs).g)) && \
  2171. (hash_compare_scalar((p_lhs).b, (p_rhs).b)) && \
  2172. (hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2173. #define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2174. const PoolVector<p_type> &l = *reinterpret_cast<const PoolVector<p_type> *>(p_lhs); \
  2175. const PoolVector<p_type> &r = *reinterpret_cast<const PoolVector<p_type> *>(p_rhs); \
  2176. \
  2177. if (l.size() != r.size()) \
  2178. return false; \
  2179. \
  2180. PoolVector<p_type>::Read lr = l.read(); \
  2181. PoolVector<p_type>::Read rr = r.read(); \
  2182. \
  2183. for (int i = 0; i < l.size(); ++i) { \
  2184. if (!p_compare_func((lr[i]), (rr[i]))) \
  2185. return false; \
  2186. } \
  2187. \
  2188. return true
  2189. bool Variant::hash_compare(const Variant &p_variant) const {
  2190. if (type != p_variant.type)
  2191. return false;
  2192. switch (type) {
  2193. case REAL: {
  2194. return hash_compare_scalar(_data._real, p_variant._data._real);
  2195. } break;
  2196. case VECTOR2: {
  2197. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2198. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2199. return hash_compare_vector2(*l, *r);
  2200. } break;
  2201. case RECT2: {
  2202. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2203. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2204. return (hash_compare_vector2(l->position, r->position)) &&
  2205. (hash_compare_vector2(l->size, r->size));
  2206. } break;
  2207. case TRANSFORM2D: {
  2208. Transform2D *l = _data._transform2d;
  2209. Transform2D *r = p_variant._data._transform2d;
  2210. for (int i = 0; i < 3; i++) {
  2211. if (!(hash_compare_vector2(l->elements[i], r->elements[i])))
  2212. return false;
  2213. }
  2214. return true;
  2215. } break;
  2216. case VECTOR3: {
  2217. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2218. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2219. return hash_compare_vector3(*l, *r);
  2220. } break;
  2221. case PLANE: {
  2222. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2223. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2224. return (hash_compare_vector3(l->normal, r->normal)) &&
  2225. (hash_compare_scalar(l->d, r->d));
  2226. } break;
  2227. case AABB: {
  2228. const ::AABB *l = _data._aabb;
  2229. const ::AABB *r = p_variant._data._aabb;
  2230. return (hash_compare_vector3(l->position, r->position) &&
  2231. (hash_compare_vector3(l->size, r->size)));
  2232. } break;
  2233. case QUAT: {
  2234. const Quat *l = reinterpret_cast<const Quat *>(_data._mem);
  2235. const Quat *r = reinterpret_cast<const Quat *>(p_variant._data._mem);
  2236. return hash_compare_quat(*l, *r);
  2237. } break;
  2238. case BASIS: {
  2239. const Basis *l = _data._basis;
  2240. const Basis *r = p_variant._data._basis;
  2241. for (int i = 0; i < 3; i++) {
  2242. if (!(hash_compare_vector3(l->elements[i], r->elements[i])))
  2243. return false;
  2244. }
  2245. return true;
  2246. } break;
  2247. case TRANSFORM: {
  2248. const Transform *l = _data._transform;
  2249. const Transform *r = p_variant._data._transform;
  2250. for (int i = 0; i < 3; i++) {
  2251. if (!(hash_compare_vector3(l->basis.elements[i], r->basis.elements[i])))
  2252. return false;
  2253. }
  2254. return hash_compare_vector3(l->origin, r->origin);
  2255. } break;
  2256. case COLOR: {
  2257. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2258. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2259. return hash_compare_color(*l, *r);
  2260. } break;
  2261. case ARRAY: {
  2262. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2263. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2264. if (l.size() != r.size())
  2265. return false;
  2266. for (int i = 0; i < l.size(); ++i) {
  2267. if (!l[i].hash_compare(r[i]))
  2268. return false;
  2269. }
  2270. return true;
  2271. } break;
  2272. case POOL_REAL_ARRAY: {
  2273. hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
  2274. } break;
  2275. case POOL_VECTOR2_ARRAY: {
  2276. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
  2277. } break;
  2278. case POOL_VECTOR3_ARRAY: {
  2279. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
  2280. } break;
  2281. case POOL_COLOR_ARRAY: {
  2282. hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
  2283. } break;
  2284. default:
  2285. bool v;
  2286. Variant r;
  2287. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2288. return r;
  2289. }
  2290. return false;
  2291. }
  2292. bool Variant::is_ref() const {
  2293. return type == OBJECT && !_get_obj().ref.is_null();
  2294. }
  2295. Vector<Variant> varray() {
  2296. return Vector<Variant>();
  2297. }
  2298. Vector<Variant> varray(const Variant &p_arg1) {
  2299. Vector<Variant> v;
  2300. v.push_back(p_arg1);
  2301. return v;
  2302. }
  2303. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
  2304. Vector<Variant> v;
  2305. v.push_back(p_arg1);
  2306. v.push_back(p_arg2);
  2307. return v;
  2308. }
  2309. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  2310. Vector<Variant> v;
  2311. v.push_back(p_arg1);
  2312. v.push_back(p_arg2);
  2313. v.push_back(p_arg3);
  2314. return v;
  2315. }
  2316. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  2317. Vector<Variant> v;
  2318. v.push_back(p_arg1);
  2319. v.push_back(p_arg2);
  2320. v.push_back(p_arg3);
  2321. v.push_back(p_arg4);
  2322. return v;
  2323. }
  2324. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  2325. Vector<Variant> v;
  2326. v.push_back(p_arg1);
  2327. v.push_back(p_arg2);
  2328. v.push_back(p_arg3);
  2329. v.push_back(p_arg4);
  2330. v.push_back(p_arg5);
  2331. return v;
  2332. }
  2333. void Variant::static_assign(const Variant &p_variant) {
  2334. }
  2335. bool Variant::is_shared() const {
  2336. switch (type) {
  2337. case OBJECT: return true;
  2338. case ARRAY: return true;
  2339. case DICTIONARY: return true;
  2340. default: {}
  2341. }
  2342. return false;
  2343. }
  2344. Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  2345. VARIANT_ARGPTRS;
  2346. int argc = 0;
  2347. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  2348. if (argptr[i]->get_type() == Variant::NIL)
  2349. break;
  2350. argc++;
  2351. }
  2352. CallError error;
  2353. Variant ret = call(p_method, argptr, argc, error);
  2354. switch (error.error) {
  2355. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2356. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(error.expected) + "'.";
  2357. ERR_PRINT(err.utf8().get_data());
  2358. } break;
  2359. case CallError::CALL_ERROR_INVALID_METHOD: {
  2360. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  2361. ERR_PRINT(err.utf8().get_data());
  2362. } break;
  2363. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2364. String err = "Too many arguments for method '" + p_method + "'";
  2365. ERR_PRINT(err.utf8().get_data());
  2366. } break;
  2367. default: {}
  2368. }
  2369. return ret;
  2370. }
  2371. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  2372. r_value = Variant();
  2373. }
  2374. String Variant::get_construct_string() const {
  2375. String vars;
  2376. VariantWriter::write_to_string(*this, vars);
  2377. return vars;
  2378. }
  2379. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce) {
  2380. String err_text;
  2381. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2382. int errorarg = ce.argument;
  2383. if (p_argptrs) {
  2384. 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) + ".";
  2385. } else {
  2386. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(ce.expected) + ".";
  2387. }
  2388. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2389. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2390. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2391. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2392. } else if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2393. err_text = "Method not found.";
  2394. } else if (ce.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2395. err_text = "Instance is null";
  2396. } else if (ce.error == Variant::CallError::CALL_OK) {
  2397. return "Call OK";
  2398. }
  2399. String class_name = p_base->get_class();
  2400. Ref<Script> script = p_base->get_script();
  2401. if (script.is_valid() && script->get_path().is_resource_file()) {
  2402. class_name += "(" + script->get_path().get_file() + ")";
  2403. }
  2404. return "'" + class_name + "::" + String(p_method) + "': " + err_text;
  2405. }
  2406. String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  2407. Array args;
  2408. if (p1.get_type() != Variant::NIL) {
  2409. args.push_back(p1);
  2410. if (p2.get_type() != Variant::NIL) {
  2411. args.push_back(p2);
  2412. if (p3.get_type() != Variant::NIL) {
  2413. args.push_back(p3);
  2414. if (p4.get_type() != Variant::NIL) {
  2415. args.push_back(p4);
  2416. if (p5.get_type() != Variant::NIL) {
  2417. args.push_back(p5);
  2418. }
  2419. }
  2420. }
  2421. }
  2422. }
  2423. bool error = false;
  2424. String fmt = p_text.sprintf(args, &error);
  2425. ERR_FAIL_COND_V(error, String());
  2426. return fmt;
  2427. }