variant_op.cpp 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766
  1. /**************************************************************************/
  2. /* variant_op.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/object.h"
  33. #include "core/object_rc.h"
  34. #include "core/script_language.h"
  35. #define CASE_TYPE_ALL(PREFIX, OP) \
  36. CASE_TYPE(PREFIX, OP, INT) \
  37. CASE_TYPE_ALL_BUT_INT(PREFIX, OP)
  38. #define CASE_TYPE_ALL_BUT_INT(PREFIX, OP) \
  39. CASE_TYPE(PREFIX, OP, NIL) \
  40. CASE_TYPE(PREFIX, OP, BOOL) \
  41. CASE_TYPE(PREFIX, OP, REAL) \
  42. CASE_TYPE(PREFIX, OP, STRING) \
  43. CASE_TYPE(PREFIX, OP, VECTOR2) \
  44. CASE_TYPE(PREFIX, OP, RECT2) \
  45. CASE_TYPE(PREFIX, OP, VECTOR3) \
  46. CASE_TYPE(PREFIX, OP, TRANSFORM2D) \
  47. CASE_TYPE(PREFIX, OP, PLANE) \
  48. CASE_TYPE(PREFIX, OP, QUAT) \
  49. CASE_TYPE(PREFIX, OP, AABB) \
  50. CASE_TYPE(PREFIX, OP, BASIS) \
  51. CASE_TYPE(PREFIX, OP, TRANSFORM) \
  52. CASE_TYPE(PREFIX, OP, COLOR) \
  53. CASE_TYPE(PREFIX, OP, NODE_PATH) \
  54. CASE_TYPE(PREFIX, OP, _RID) \
  55. CASE_TYPE(PREFIX, OP, OBJECT) \
  56. CASE_TYPE(PREFIX, OP, DICTIONARY) \
  57. CASE_TYPE(PREFIX, OP, ARRAY) \
  58. CASE_TYPE(PREFIX, OP, POOL_BYTE_ARRAY) \
  59. CASE_TYPE(PREFIX, OP, POOL_INT_ARRAY) \
  60. CASE_TYPE(PREFIX, OP, POOL_REAL_ARRAY) \
  61. CASE_TYPE(PREFIX, OP, POOL_STRING_ARRAY) \
  62. CASE_TYPE(PREFIX, OP, POOL_VECTOR2_ARRAY) \
  63. CASE_TYPE(PREFIX, OP, POOL_VECTOR3_ARRAY) \
  64. CASE_TYPE(PREFIX, OP, POOL_COLOR_ARRAY)
  65. #ifdef __GNUC__
  66. #define TYPE(PREFIX, OP, TYPE) &&PREFIX##_##OP##_##TYPE
  67. /* clang-format off */
  68. #define TYPES(PREFIX, OP) { \
  69. TYPE(PREFIX, OP, NIL), \
  70. TYPE(PREFIX, OP, BOOL), \
  71. TYPE(PREFIX, OP, INT), \
  72. TYPE(PREFIX, OP, REAL), \
  73. TYPE(PREFIX, OP, STRING), \
  74. TYPE(PREFIX, OP, VECTOR2), \
  75. TYPE(PREFIX, OP, RECT2), \
  76. TYPE(PREFIX, OP, VECTOR3), \
  77. TYPE(PREFIX, OP, TRANSFORM2D), \
  78. TYPE(PREFIX, OP, PLANE), \
  79. TYPE(PREFIX, OP, QUAT), \
  80. TYPE(PREFIX, OP, AABB), \
  81. TYPE(PREFIX, OP, BASIS), \
  82. TYPE(PREFIX, OP, TRANSFORM), \
  83. TYPE(PREFIX, OP, COLOR), \
  84. TYPE(PREFIX, OP, NODE_PATH), \
  85. TYPE(PREFIX, OP, _RID), \
  86. TYPE(PREFIX, OP, OBJECT), \
  87. TYPE(PREFIX, OP, DICTIONARY), \
  88. TYPE(PREFIX, OP, ARRAY), \
  89. TYPE(PREFIX, OP, POOL_BYTE_ARRAY), \
  90. TYPE(PREFIX, OP, POOL_INT_ARRAY), \
  91. TYPE(PREFIX, OP, POOL_REAL_ARRAY), \
  92. TYPE(PREFIX, OP, POOL_STRING_ARRAY), \
  93. TYPE(PREFIX, OP, POOL_VECTOR2_ARRAY), \
  94. TYPE(PREFIX, OP, POOL_VECTOR3_ARRAY), \
  95. TYPE(PREFIX, OP, POOL_COLOR_ARRAY), \
  96. }
  97. /* clang-format on */
  98. #define CASES(PREFIX) static const void *switch_table_##PREFIX[25][27] = { \
  99. TYPES(PREFIX, OP_EQUAL), \
  100. TYPES(PREFIX, OP_NOT_EQUAL), \
  101. TYPES(PREFIX, OP_LESS), \
  102. TYPES(PREFIX, OP_LESS_EQUAL), \
  103. TYPES(PREFIX, OP_GREATER), \
  104. TYPES(PREFIX, OP_GREATER_EQUAL), \
  105. TYPES(PREFIX, OP_ADD), \
  106. TYPES(PREFIX, OP_SUBTRACT), \
  107. TYPES(PREFIX, OP_MULTIPLY), \
  108. TYPES(PREFIX, OP_DIVIDE), \
  109. TYPES(PREFIX, OP_NEGATE), \
  110. TYPES(PREFIX, OP_POSITIVE), \
  111. TYPES(PREFIX, OP_MODULE), \
  112. TYPES(PREFIX, OP_STRING_CONCAT), \
  113. TYPES(PREFIX, OP_SHIFT_LEFT), \
  114. TYPES(PREFIX, OP_SHIFT_RIGHT), \
  115. TYPES(PREFIX, OP_BIT_AND), \
  116. TYPES(PREFIX, OP_BIT_OR), \
  117. TYPES(PREFIX, OP_BIT_XOR), \
  118. TYPES(PREFIX, OP_BIT_NEGATE), \
  119. TYPES(PREFIX, OP_AND), \
  120. TYPES(PREFIX, OP_OR), \
  121. TYPES(PREFIX, OP_XOR), \
  122. TYPES(PREFIX, OP_NOT), \
  123. TYPES(PREFIX, OP_IN), \
  124. }
  125. #define SWITCH(PREFIX, op, val) goto *switch_table_##PREFIX[op][val];
  126. #define SWITCH_OP(PREFIX, OP, val)
  127. #define CASE_TYPE(PREFIX, OP, TYPE) PREFIX##_##OP##_##TYPE:
  128. #else
  129. #define CASES(PREFIX)
  130. #define SWITCH(PREFIX, op, val) switch (op)
  131. #define SWITCH_OP(PREFIX, OP, val) \
  132. case OP: \
  133. switch (val)
  134. #define CASE_TYPE(PREFIX, OP, TYPE) case TYPE:
  135. #endif
  136. Variant::operator bool() const {
  137. return booleanize();
  138. }
  139. // We consider all uninitialized or empty types to be false based on the type's
  140. // zeroiness.
  141. bool Variant::booleanize() const {
  142. return !is_zero();
  143. }
  144. #define _RETURN(m_what) \
  145. { \
  146. r_ret = m_what; \
  147. return; \
  148. }
  149. #define _RETURN_FAIL \
  150. { \
  151. r_valid = false; \
  152. return; \
  153. }
  154. #define DEFAULT_OP_NUM(m_prefix, m_op_name, m_name, m_op, m_type) \
  155. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  156. if (p_b.type == INT) \
  157. _RETURN(p_a._data.m_type m_op p_b._data._int); \
  158. if (p_b.type == REAL) \
  159. _RETURN(p_a._data.m_type m_op p_b._data._real); \
  160. \
  161. _RETURN_FAIL \
  162. };
  163. #define DEFAULT_OP_NUM_NULL(m_prefix, m_op_name, m_name, m_op, m_type) \
  164. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  165. if (p_b.type == INT) \
  166. _RETURN(p_a._data.m_type m_op p_b._data._int); \
  167. if (p_b.type == REAL) \
  168. _RETURN(p_a._data.m_type m_op p_b._data._real); \
  169. if (p_b.type == NIL) \
  170. _RETURN(!(p_b.type m_op NIL)); \
  171. \
  172. _RETURN_FAIL \
  173. };
  174. #ifdef DEBUG_ENABLED
  175. #define DEFAULT_OP_NUM_DIV(m_prefix, m_op_name, m_name, m_type) \
  176. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  177. if (p_b.type == INT) { \
  178. if (p_b._data._int == 0) { \
  179. r_valid = false; \
  180. _RETURN("Division By Zero"); \
  181. } \
  182. _RETURN(p_a._data.m_type / p_b._data._int); \
  183. } \
  184. if (p_b.type == REAL) { \
  185. if (p_b._data._real == 0) { \
  186. r_valid = false; \
  187. _RETURN("Division By Zero"); \
  188. } \
  189. _RETURN(p_a._data.m_type / p_b._data._real); \
  190. } \
  191. \
  192. _RETURN_FAIL \
  193. };
  194. #else
  195. #define DEFAULT_OP_NUM_DIV(m_prefix, m_op_name, m_name, m_type) \
  196. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  197. if (p_b.type == INT) \
  198. _RETURN(p_a._data.m_type / p_b._data._int); \
  199. if (p_b.type == REAL) \
  200. _RETURN(p_a._data.m_type / p_b._data._real); \
  201. \
  202. _RETURN_FAIL \
  203. };
  204. #endif
  205. #define DEFAULT_OP_NUM_NEG(m_prefix, m_op_name, m_name, m_type) \
  206. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  207. _RETURN(-p_a._data.m_type); \
  208. };
  209. #define DEFAULT_OP_NUM_POS(m_prefix, m_op_name, m_name, m_type) \
  210. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  211. _RETURN(p_a._data.m_type); \
  212. };
  213. #define DEFAULT_OP_NUM_VEC(m_prefix, m_op_name, m_name, m_op, m_type) \
  214. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  215. if (p_b.type == INT) \
  216. _RETURN(p_a._data.m_type m_op p_b._data._int); \
  217. if (p_b.type == REAL) \
  218. _RETURN(p_a._data.m_type m_op p_b._data._real); \
  219. if (p_b.type == VECTOR2) \
  220. _RETURN(p_a._data.m_type m_op *reinterpret_cast<const Vector2 *>(p_b._data._mem)); \
  221. if (p_b.type == VECTOR3) \
  222. _RETURN(p_a._data.m_type m_op *reinterpret_cast<const Vector3 *>(p_b._data._mem)); \
  223. \
  224. _RETURN_FAIL \
  225. };
  226. #define DEFAULT_OP_STR_REV(m_prefix, m_op_name, m_name, m_op, m_type) \
  227. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  228. if (p_b.type == STRING) \
  229. _RETURN(*reinterpret_cast<const m_type *>(p_b._data._mem) m_op *reinterpret_cast<const String *>(p_a._data._mem)); \
  230. if (p_b.type == NODE_PATH) \
  231. _RETURN(*reinterpret_cast<const m_type *>(p_b._data._mem) m_op *reinterpret_cast<const NodePath *>(p_a._data._mem)); \
  232. \
  233. _RETURN_FAIL \
  234. };
  235. #define DEFAULT_OP_STR(m_prefix, m_op_name, m_name, m_op, m_type) \
  236. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  237. if (p_b.type == STRING) \
  238. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const String *>(p_b._data._mem)); \
  239. if (p_b.type == NODE_PATH) \
  240. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const NodePath *>(p_b._data._mem)); \
  241. \
  242. _RETURN_FAIL \
  243. };
  244. #define DEFAULT_OP_STR_NULL(m_prefix, m_op_name, m_name, m_op, m_type) \
  245. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  246. if (p_b.type == STRING) \
  247. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const String *>(p_b._data._mem)); \
  248. if (p_b.type == NODE_PATH) \
  249. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const NodePath *>(p_b._data._mem)); \
  250. if (p_b.type == NIL) \
  251. _RETURN(!(p_b.type m_op NIL)); \
  252. \
  253. _RETURN_FAIL \
  254. };
  255. #define DEFAULT_OP_LOCALMEM_REV(m_prefix, m_op_name, m_name, m_op, m_type) \
  256. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  257. if (p_b.type == m_name) \
  258. _RETURN(*reinterpret_cast<const m_type *>(p_b._data._mem) m_op *reinterpret_cast<const m_type *>(p_a._data._mem)); \
  259. \
  260. _RETURN_FAIL \
  261. };
  262. #define DEFAULT_OP_LOCALMEM(m_prefix, m_op_name, m_name, m_op, m_type) \
  263. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  264. if (p_b.type == m_name) \
  265. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const m_type *>(p_b._data._mem)); \
  266. \
  267. _RETURN_FAIL \
  268. };
  269. #define DEFAULT_OP_LOCALMEM_NULL(m_prefix, m_op_name, m_name, m_op, m_type) \
  270. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  271. if (p_b.type == m_name) \
  272. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const m_type *>(p_b._data._mem)); \
  273. if (p_b.type == NIL) \
  274. _RETURN(!(p_b.type m_op NIL)); \
  275. \
  276. _RETURN_FAIL \
  277. };
  278. #define DEFAULT_OP_LOCALMEM_NEG(m_prefix, m_op_name, m_name, m_type) \
  279. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  280. _RETURN(-*reinterpret_cast<const m_type *>(p_a._data._mem)); \
  281. }
  282. #define DEFAULT_OP_LOCALMEM_POS(m_prefix, m_op_name, m_name, m_type) \
  283. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  284. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem)); \
  285. }
  286. #define DEFAULT_OP_LOCALMEM_NUM(m_prefix, m_op_name, m_name, m_op, m_type) \
  287. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  288. if (p_b.type == m_name) \
  289. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const m_type *>(p_b._data._mem)); \
  290. if (p_b.type == INT) \
  291. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op p_b._data._int); \
  292. if (p_b.type == REAL) \
  293. _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op p_b._data._real); \
  294. \
  295. _RETURN_FAIL \
  296. }
  297. #define DEFAULT_OP_PTR(m_op, m_name, m_sub) \
  298. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  299. if (p_b.type == m_name) \
  300. _RETURN(p_a._data.m_sub m_op p_b._data.m_sub); \
  301. \
  302. _RETURN_FAIL \
  303. }
  304. #define DEFAULT_OP_PTRREF(m_prefix, m_op_name, m_name, m_op, m_sub) \
  305. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  306. if (p_b.type == m_name) \
  307. _RETURN(*p_a._data.m_sub m_op *p_b._data.m_sub); \
  308. \
  309. _RETURN_FAIL \
  310. }
  311. #define DEFAULT_OP_PTRREF_NULL(m_prefix, m_op_name, m_name, m_op, m_sub) \
  312. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  313. if (p_b.type == m_name) \
  314. _RETURN(*p_a._data.m_sub m_op *p_b._data.m_sub); \
  315. if (p_b.type == NIL) \
  316. _RETURN(!(p_b.type m_op NIL)); \
  317. \
  318. _RETURN_FAIL \
  319. }
  320. #define DEFAULT_OP_ARRAY_EQ(m_prefix, m_op_name, m_name, m_type) \
  321. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  322. if (p_b.type == NIL) \
  323. _RETURN(false) \
  324. DEFAULT_OP_ARRAY_OP_BODY(m_prefix, m_op_name, m_name, m_type, !=, !=, true, false, false) \
  325. }
  326. #define DEFAULT_OP_ARRAY_NEQ(m_prefix, m_op_name, m_name, m_type) \
  327. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  328. if (p_b.type == NIL) \
  329. _RETURN(true) \
  330. DEFAULT_OP_ARRAY_OP_BODY(m_prefix, m_op_name, m_name, m_type, !=, !=, false, true, true) \
  331. }
  332. #define DEFAULT_OP_ARRAY_LT(m_prefix, m_op_name, m_name, m_type) \
  333. DEFAULT_OP_ARRAY_OP(m_prefix, m_op_name, m_name, m_type, <, !=, false, a_len < array_b.size(), true)
  334. #define DEFAULT_OP_ARRAY_GT(m_prefix, m_op_name, m_name, m_type) \
  335. DEFAULT_OP_ARRAY_OP(m_prefix, m_op_name, m_name, m_type, >, !=, false, a_len < array_b.size(), true)
  336. #define DEFAULT_OP_ARRAY_OP(m_prefix, m_op_name, m_name, m_type, m_opa, m_opb, m_ret_def, m_ret_s, m_ret_f) \
  337. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  338. DEFAULT_OP_ARRAY_OP_BODY(m_prefix, m_op_name, m_name, m_type, m_opa, m_opb, m_ret_def, m_ret_s, m_ret_f) \
  339. }
  340. #define DEFAULT_OP_ARRAY_OP_BODY(m_prefix, m_op_name, m_name, m_type, m_opa, m_opb, m_ret_def, m_ret_s, m_ret_f) \
  341. if (p_a.type != p_b.type) \
  342. _RETURN_FAIL \
  343. \
  344. const PoolVector<m_type> &array_a = *reinterpret_cast<const PoolVector<m_type> *>(p_a._data._mem); \
  345. const PoolVector<m_type> &array_b = *reinterpret_cast<const PoolVector<m_type> *>(p_b._data._mem); \
  346. \
  347. int a_len = array_a.size(); \
  348. if (a_len m_opa array_b.size()) { \
  349. _RETURN(m_ret_s); \
  350. } else { \
  351. PoolVector<m_type>::Read ra = array_a.read(); \
  352. PoolVector<m_type>::Read rb = array_b.read(); \
  353. \
  354. for (int i = 0; i < a_len; i++) { \
  355. if (ra[i] m_opb rb[i]) \
  356. _RETURN(m_ret_f); \
  357. } \
  358. \
  359. _RETURN(m_ret_def); \
  360. }
  361. #define DEFAULT_OP_ARRAY_ADD(m_prefix, m_op_name, m_name, m_type) \
  362. CASE_TYPE(m_prefix, m_op_name, m_name) { \
  363. if (p_a.type != p_b.type) \
  364. _RETURN_FAIL; \
  365. \
  366. const PoolVector<m_type> &array_a = *reinterpret_cast<const PoolVector<m_type> *>(p_a._data._mem); \
  367. const PoolVector<m_type> &array_b = *reinterpret_cast<const PoolVector<m_type> *>(p_b._data._mem); \
  368. PoolVector<m_type> sum = array_a; \
  369. sum.append_array(array_b); \
  370. _RETURN(sum); \
  371. }
  372. void Variant::evaluate(const Operator &p_op, const Variant &p_a,
  373. const Variant &p_b, Variant &r_ret, bool &r_valid) {
  374. CASES(math);
  375. r_valid = true;
  376. SWITCH(math, p_op, p_a.type) {
  377. SWITCH_OP(math, OP_EQUAL, p_a.type) {
  378. CASE_TYPE(math, OP_EQUAL, NIL) {
  379. if (p_b.type == NIL)
  380. _RETURN(true);
  381. if (p_b.type == OBJECT)
  382. _RETURN(_UNSAFE_OBJ_PROXY_PTR(p_b) == nullptr);
  383. _RETURN(false);
  384. }
  385. CASE_TYPE(math, OP_EQUAL, BOOL) {
  386. if (p_b.type != BOOL) {
  387. if (p_b.type == NIL)
  388. _RETURN(false);
  389. _RETURN_FAIL;
  390. }
  391. _RETURN(p_a._data._bool == p_b._data._bool);
  392. }
  393. CASE_TYPE(math, OP_EQUAL, OBJECT) {
  394. if (p_b.type == OBJECT)
  395. _RETURN(_UNSAFE_OBJ_PROXY_PTR(p_a) == _UNSAFE_OBJ_PROXY_PTR(p_b));
  396. if (p_b.type == NIL)
  397. _RETURN(_UNSAFE_OBJ_PROXY_PTR(p_a) == nullptr);
  398. _RETURN_FAIL;
  399. }
  400. CASE_TYPE(math, OP_EQUAL, DICTIONARY) {
  401. if (p_b.type != DICTIONARY) {
  402. if (p_b.type == NIL)
  403. _RETURN(false);
  404. _RETURN_FAIL;
  405. }
  406. const Dictionary *arr_a = reinterpret_cast<const Dictionary *>(p_a._data._mem);
  407. const Dictionary *arr_b = reinterpret_cast<const Dictionary *>(p_b._data._mem);
  408. _RETURN(*arr_a == *arr_b);
  409. }
  410. CASE_TYPE(math, OP_EQUAL, ARRAY) {
  411. if (p_b.type != ARRAY) {
  412. if (p_b.type == NIL)
  413. _RETURN(false);
  414. _RETURN_FAIL;
  415. }
  416. const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem);
  417. const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem);
  418. int l = arr_a->size();
  419. if (arr_b->size() != l)
  420. _RETURN(false);
  421. for (int i = 0; i < l; i++) {
  422. if (!((*arr_a)[i] == (*arr_b)[i])) {
  423. _RETURN(false);
  424. }
  425. }
  426. _RETURN(true);
  427. }
  428. DEFAULT_OP_NUM_NULL(math, OP_EQUAL, INT, ==, _int);
  429. DEFAULT_OP_NUM_NULL(math, OP_EQUAL, REAL, ==, _real);
  430. DEFAULT_OP_STR_NULL(math, OP_EQUAL, STRING, ==, String);
  431. DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, VECTOR2, ==, Vector2);
  432. DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, RECT2, ==, Rect2);
  433. DEFAULT_OP_PTRREF_NULL(math, OP_EQUAL, TRANSFORM2D, ==, _transform2d);
  434. DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, VECTOR3, ==, Vector3);
  435. DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, PLANE, ==, Plane);
  436. DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, QUAT, ==, Quat);
  437. DEFAULT_OP_PTRREF_NULL(math, OP_EQUAL, AABB, ==, _aabb);
  438. DEFAULT_OP_PTRREF_NULL(math, OP_EQUAL, BASIS, ==, _basis);
  439. DEFAULT_OP_PTRREF_NULL(math, OP_EQUAL, TRANSFORM, ==, _transform);
  440. DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, COLOR, ==, Color);
  441. DEFAULT_OP_STR_NULL(math, OP_EQUAL, NODE_PATH, ==, NodePath);
  442. DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, _RID, ==, RID);
  443. DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_BYTE_ARRAY, uint8_t);
  444. DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_INT_ARRAY, int);
  445. DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_REAL_ARRAY, real_t);
  446. DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_STRING_ARRAY, String);
  447. DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_VECTOR2_ARRAY, Vector2);
  448. DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_VECTOR3_ARRAY, Vector3);
  449. DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_COLOR_ARRAY, Color);
  450. }
  451. SWITCH_OP(math, OP_NOT_EQUAL, p_a.type) {
  452. CASE_TYPE(math, OP_NOT_EQUAL, NIL) {
  453. if (p_b.type == NIL)
  454. _RETURN(false);
  455. if (p_b.type == OBJECT)
  456. _RETURN(_UNSAFE_OBJ_PROXY_PTR(p_b) != nullptr);
  457. _RETURN(true);
  458. }
  459. CASE_TYPE(math, OP_NOT_EQUAL, BOOL) {
  460. if (p_b.type != BOOL) {
  461. if (p_b.type == NIL)
  462. _RETURN(true);
  463. _RETURN_FAIL;
  464. }
  465. _RETURN(p_a._data._bool != p_b._data._bool);
  466. }
  467. CASE_TYPE(math, OP_NOT_EQUAL, OBJECT) {
  468. if (p_b.type == OBJECT)
  469. _RETURN((_UNSAFE_OBJ_PROXY_PTR(p_a) != _UNSAFE_OBJ_PROXY_PTR(p_b)));
  470. if (p_b.type == NIL)
  471. _RETURN(_UNSAFE_OBJ_PROXY_PTR(p_a) != nullptr);
  472. _RETURN_FAIL;
  473. }
  474. CASE_TYPE(math, OP_NOT_EQUAL, DICTIONARY) {
  475. if (p_b.type != DICTIONARY) {
  476. if (p_b.type == NIL)
  477. _RETURN(true);
  478. _RETURN_FAIL;
  479. }
  480. const Dictionary *arr_a = reinterpret_cast<const Dictionary *>(p_a._data._mem);
  481. const Dictionary *arr_b = reinterpret_cast<const Dictionary *>(p_b._data._mem);
  482. _RETURN(*arr_a != *arr_b);
  483. }
  484. CASE_TYPE(math, OP_NOT_EQUAL, ARRAY) {
  485. if (p_b.type != ARRAY) {
  486. if (p_b.type == NIL)
  487. _RETURN(true);
  488. _RETURN_FAIL;
  489. }
  490. const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem);
  491. const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem);
  492. int l = arr_a->size();
  493. if (arr_b->size() != l)
  494. _RETURN(true);
  495. for (int i = 0; i < l; i++) {
  496. if (((*arr_a)[i] != (*arr_b)[i])) {
  497. _RETURN(true);
  498. }
  499. }
  500. _RETURN(false);
  501. }
  502. DEFAULT_OP_NUM_NULL(math, OP_NOT_EQUAL, INT, !=, _int);
  503. DEFAULT_OP_NUM_NULL(math, OP_NOT_EQUAL, REAL, !=, _real);
  504. DEFAULT_OP_STR_NULL(math, OP_NOT_EQUAL, STRING, !=, String);
  505. DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, VECTOR2, !=, Vector2);
  506. DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, RECT2, !=, Rect2);
  507. DEFAULT_OP_PTRREF_NULL(math, OP_NOT_EQUAL, TRANSFORM2D, !=, _transform2d);
  508. DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, VECTOR3, !=, Vector3);
  509. DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, PLANE, !=, Plane);
  510. DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, QUAT, !=, Quat);
  511. DEFAULT_OP_PTRREF_NULL(math, OP_NOT_EQUAL, AABB, !=, _aabb);
  512. DEFAULT_OP_PTRREF_NULL(math, OP_NOT_EQUAL, BASIS, !=, _basis);
  513. DEFAULT_OP_PTRREF_NULL(math, OP_NOT_EQUAL, TRANSFORM, !=, _transform);
  514. DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, COLOR, !=, Color);
  515. DEFAULT_OP_STR_NULL(math, OP_NOT_EQUAL, NODE_PATH, !=, NodePath);
  516. DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, _RID, !=, RID);
  517. DEFAULT_OP_ARRAY_NEQ(math, OP_NOT_EQUAL, POOL_BYTE_ARRAY, uint8_t);
  518. DEFAULT_OP_ARRAY_NEQ(math, OP_NOT_EQUAL, POOL_INT_ARRAY, int);
  519. DEFAULT_OP_ARRAY_NEQ(math, OP_NOT_EQUAL, POOL_REAL_ARRAY, real_t);
  520. DEFAULT_OP_ARRAY_NEQ(math, OP_NOT_EQUAL, POOL_STRING_ARRAY, String);
  521. DEFAULT_OP_ARRAY_NEQ(math, OP_NOT_EQUAL, POOL_VECTOR2_ARRAY, Vector2);
  522. DEFAULT_OP_ARRAY_NEQ(math, OP_NOT_EQUAL, POOL_VECTOR3_ARRAY, Vector3);
  523. DEFAULT_OP_ARRAY_NEQ(math, OP_NOT_EQUAL, POOL_COLOR_ARRAY, Color);
  524. }
  525. SWITCH_OP(math, OP_LESS, p_a.type) {
  526. CASE_TYPE(math, OP_LESS, BOOL) {
  527. if (p_b.type != BOOL)
  528. _RETURN_FAIL;
  529. if (p_a._data._bool == p_b._data._bool)
  530. _RETURN(false);
  531. if (p_a._data._bool && !p_b._data._bool)
  532. _RETURN(false);
  533. _RETURN(true);
  534. }
  535. CASE_TYPE(math, OP_LESS, OBJECT) {
  536. if (p_b.type != OBJECT)
  537. _RETURN_FAIL;
  538. _RETURN(_UNSAFE_OBJ_PROXY_PTR(p_a) < _UNSAFE_OBJ_PROXY_PTR(p_b));
  539. }
  540. CASE_TYPE(math, OP_LESS, ARRAY) {
  541. if (p_b.type != ARRAY)
  542. _RETURN_FAIL;
  543. const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem);
  544. const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem);
  545. int l = arr_a->size();
  546. if (arr_b->size() < l)
  547. _RETURN(false);
  548. for (int i = 0; i < l; i++) {
  549. if (!((*arr_a)[i] < (*arr_b)[i])) {
  550. _RETURN(true);
  551. }
  552. }
  553. _RETURN(false);
  554. }
  555. DEFAULT_OP_NUM(math, OP_LESS, INT, <, _int);
  556. DEFAULT_OP_NUM(math, OP_LESS, REAL, <, _real);
  557. DEFAULT_OP_STR(math, OP_LESS, STRING, <, String);
  558. DEFAULT_OP_LOCALMEM(math, OP_LESS, VECTOR2, <, Vector2);
  559. DEFAULT_OP_LOCALMEM(math, OP_LESS, VECTOR3, <, Vector3);
  560. DEFAULT_OP_LOCALMEM(math, OP_LESS, _RID, <, RID);
  561. DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_BYTE_ARRAY, uint8_t);
  562. DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_INT_ARRAY, int);
  563. DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_REAL_ARRAY, real_t);
  564. DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_STRING_ARRAY, String);
  565. DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_VECTOR2_ARRAY, Vector3);
  566. DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_VECTOR3_ARRAY, Vector3);
  567. DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_COLOR_ARRAY, Color);
  568. CASE_TYPE(math, OP_LESS, NIL)
  569. CASE_TYPE(math, OP_LESS, RECT2)
  570. CASE_TYPE(math, OP_LESS, TRANSFORM2D)
  571. CASE_TYPE(math, OP_LESS, PLANE)
  572. CASE_TYPE(math, OP_LESS, QUAT)
  573. CASE_TYPE(math, OP_LESS, AABB)
  574. CASE_TYPE(math, OP_LESS, BASIS)
  575. CASE_TYPE(math, OP_LESS, TRANSFORM)
  576. CASE_TYPE(math, OP_LESS, COLOR)
  577. CASE_TYPE(math, OP_LESS, NODE_PATH)
  578. CASE_TYPE(math, OP_LESS, DICTIONARY)
  579. _RETURN_FAIL;
  580. }
  581. SWITCH_OP(math, OP_LESS_EQUAL, p_a.type) {
  582. CASE_TYPE(math, OP_LESS_EQUAL, OBJECT) {
  583. if (p_b.type != OBJECT)
  584. _RETURN_FAIL;
  585. _RETURN(_UNSAFE_OBJ_PROXY_PTR(p_a) <= _UNSAFE_OBJ_PROXY_PTR(p_b));
  586. }
  587. DEFAULT_OP_NUM(math, OP_LESS_EQUAL, INT, <=, _int);
  588. DEFAULT_OP_NUM(math, OP_LESS_EQUAL, REAL, <=, _real);
  589. DEFAULT_OP_STR(math, OP_LESS_EQUAL, STRING, <=, String);
  590. DEFAULT_OP_LOCALMEM(math, OP_LESS_EQUAL, VECTOR2, <=, Vector2);
  591. DEFAULT_OP_LOCALMEM(math, OP_LESS_EQUAL, VECTOR3, <=, Vector3);
  592. DEFAULT_OP_LOCALMEM(math, OP_LESS_EQUAL, _RID, <=, RID);
  593. CASE_TYPE(math, OP_LESS_EQUAL, NIL)
  594. CASE_TYPE(math, OP_LESS_EQUAL, BOOL)
  595. CASE_TYPE(math, OP_LESS_EQUAL, RECT2)
  596. CASE_TYPE(math, OP_LESS_EQUAL, TRANSFORM2D)
  597. CASE_TYPE(math, OP_LESS_EQUAL, PLANE)
  598. CASE_TYPE(math, OP_LESS_EQUAL, QUAT)
  599. CASE_TYPE(math, OP_LESS_EQUAL, AABB)
  600. CASE_TYPE(math, OP_LESS_EQUAL, BASIS)
  601. CASE_TYPE(math, OP_LESS_EQUAL, TRANSFORM)
  602. CASE_TYPE(math, OP_LESS_EQUAL, COLOR)
  603. CASE_TYPE(math, OP_LESS_EQUAL, NODE_PATH)
  604. CASE_TYPE(math, OP_LESS_EQUAL, DICTIONARY)
  605. CASE_TYPE(math, OP_LESS_EQUAL, ARRAY)
  606. CASE_TYPE(math, OP_LESS_EQUAL, POOL_BYTE_ARRAY);
  607. CASE_TYPE(math, OP_LESS_EQUAL, POOL_INT_ARRAY);
  608. CASE_TYPE(math, OP_LESS_EQUAL, POOL_REAL_ARRAY);
  609. CASE_TYPE(math, OP_LESS_EQUAL, POOL_STRING_ARRAY);
  610. CASE_TYPE(math, OP_LESS_EQUAL, POOL_VECTOR2_ARRAY);
  611. CASE_TYPE(math, OP_LESS_EQUAL, POOL_VECTOR3_ARRAY);
  612. CASE_TYPE(math, OP_LESS_EQUAL, POOL_COLOR_ARRAY);
  613. _RETURN_FAIL;
  614. }
  615. SWITCH_OP(math, OP_GREATER, p_a.type) {
  616. CASE_TYPE(math, OP_GREATER, BOOL) {
  617. if (p_b.type != BOOL)
  618. _RETURN_FAIL;
  619. if (p_a._data._bool == p_b._data._bool)
  620. _RETURN(false);
  621. if (!p_a._data._bool && p_b._data._bool)
  622. _RETURN(false);
  623. _RETURN(true);
  624. }
  625. CASE_TYPE(math, OP_GREATER, OBJECT) {
  626. if (p_b.type != OBJECT)
  627. _RETURN_FAIL;
  628. _RETURN(_UNSAFE_OBJ_PROXY_PTR(p_a) > _UNSAFE_OBJ_PROXY_PTR(p_b));
  629. }
  630. CASE_TYPE(math, OP_GREATER, ARRAY) {
  631. if (p_b.type != ARRAY)
  632. _RETURN_FAIL;
  633. const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem);
  634. const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem);
  635. int l = arr_a->size();
  636. if (arr_b->size() > l)
  637. _RETURN(false);
  638. for (int i = 0; i < l; i++) {
  639. if (((*arr_a)[i] < (*arr_b)[i])) {
  640. _RETURN(false);
  641. }
  642. }
  643. _RETURN(true);
  644. }
  645. DEFAULT_OP_NUM(math, OP_GREATER, INT, >, _int);
  646. DEFAULT_OP_NUM(math, OP_GREATER, REAL, >, _real);
  647. DEFAULT_OP_STR_REV(math, OP_GREATER, STRING, <, String);
  648. DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER, VECTOR2, <, Vector2);
  649. DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER, VECTOR3, <, Vector3);
  650. DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER, _RID, <, RID);
  651. DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_BYTE_ARRAY, uint8_t);
  652. DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_INT_ARRAY, int);
  653. DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_REAL_ARRAY, real_t);
  654. DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_STRING_ARRAY, String);
  655. DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_VECTOR2_ARRAY, Vector3);
  656. DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_VECTOR3_ARRAY, Vector3);
  657. DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_COLOR_ARRAY, Color);
  658. CASE_TYPE(math, OP_GREATER, NIL)
  659. CASE_TYPE(math, OP_GREATER, RECT2)
  660. CASE_TYPE(math, OP_GREATER, TRANSFORM2D)
  661. CASE_TYPE(math, OP_GREATER, PLANE)
  662. CASE_TYPE(math, OP_GREATER, QUAT)
  663. CASE_TYPE(math, OP_GREATER, AABB)
  664. CASE_TYPE(math, OP_GREATER, BASIS)
  665. CASE_TYPE(math, OP_GREATER, TRANSFORM)
  666. CASE_TYPE(math, OP_GREATER, COLOR)
  667. CASE_TYPE(math, OP_GREATER, NODE_PATH)
  668. CASE_TYPE(math, OP_GREATER, DICTIONARY)
  669. _RETURN_FAIL;
  670. }
  671. SWITCH_OP(math, OP_GREATER_EQUAL, p_a.type) {
  672. CASE_TYPE(math, OP_GREATER_EQUAL, OBJECT) {
  673. if (p_b.type != OBJECT)
  674. _RETURN_FAIL;
  675. _RETURN(_UNSAFE_OBJ_PROXY_PTR(p_a) >= _UNSAFE_OBJ_PROXY_PTR(p_b));
  676. }
  677. DEFAULT_OP_NUM(math, OP_GREATER_EQUAL, INT, >=, _int);
  678. DEFAULT_OP_NUM(math, OP_GREATER_EQUAL, REAL, >=, _real);
  679. DEFAULT_OP_STR_REV(math, OP_GREATER_EQUAL, STRING, <=, String);
  680. DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER_EQUAL, VECTOR2, <=, Vector2);
  681. DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER_EQUAL, VECTOR3, <=, Vector3);
  682. DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER_EQUAL, _RID, <=, RID);
  683. CASE_TYPE(math, OP_GREATER_EQUAL, NIL)
  684. CASE_TYPE(math, OP_GREATER_EQUAL, BOOL)
  685. CASE_TYPE(math, OP_GREATER_EQUAL, RECT2)
  686. CASE_TYPE(math, OP_GREATER_EQUAL, TRANSFORM2D)
  687. CASE_TYPE(math, OP_GREATER_EQUAL, PLANE)
  688. CASE_TYPE(math, OP_GREATER_EQUAL, QUAT)
  689. CASE_TYPE(math, OP_GREATER_EQUAL, AABB)
  690. CASE_TYPE(math, OP_GREATER_EQUAL, BASIS)
  691. CASE_TYPE(math, OP_GREATER_EQUAL, TRANSFORM)
  692. CASE_TYPE(math, OP_GREATER_EQUAL, COLOR)
  693. CASE_TYPE(math, OP_GREATER_EQUAL, NODE_PATH)
  694. CASE_TYPE(math, OP_GREATER_EQUAL, DICTIONARY)
  695. CASE_TYPE(math, OP_GREATER_EQUAL, ARRAY)
  696. CASE_TYPE(math, OP_GREATER_EQUAL, POOL_BYTE_ARRAY);
  697. CASE_TYPE(math, OP_GREATER_EQUAL, POOL_INT_ARRAY);
  698. CASE_TYPE(math, OP_GREATER_EQUAL, POOL_REAL_ARRAY);
  699. CASE_TYPE(math, OP_GREATER_EQUAL, POOL_STRING_ARRAY);
  700. CASE_TYPE(math, OP_GREATER_EQUAL, POOL_VECTOR2_ARRAY);
  701. CASE_TYPE(math, OP_GREATER_EQUAL, POOL_VECTOR3_ARRAY);
  702. CASE_TYPE(math, OP_GREATER_EQUAL, POOL_COLOR_ARRAY);
  703. _RETURN_FAIL;
  704. }
  705. SWITCH_OP(math, OP_ADD, p_a.type) {
  706. CASE_TYPE(math, OP_ADD, ARRAY) {
  707. if (p_a.type != p_b.type)
  708. _RETURN_FAIL;
  709. const Array &array_a = *reinterpret_cast<const Array *>(p_a._data._mem);
  710. const Array &array_b = *reinterpret_cast<const Array *>(p_b._data._mem);
  711. Array sum;
  712. int asize = array_a.size();
  713. int bsize = array_b.size();
  714. sum.resize(asize + bsize);
  715. for (int i = 0; i < asize; i++) {
  716. sum[i] = array_a[i];
  717. }
  718. for (int i = 0; i < bsize; i++) {
  719. sum[i + asize] = array_b[i];
  720. }
  721. _RETURN(sum);
  722. }
  723. DEFAULT_OP_NUM(math, OP_ADD, INT, +, _int);
  724. DEFAULT_OP_NUM(math, OP_ADD, REAL, +, _real);
  725. DEFAULT_OP_STR(math, OP_ADD, STRING, +, String);
  726. DEFAULT_OP_LOCALMEM(math, OP_ADD, VECTOR2, +, Vector2);
  727. DEFAULT_OP_LOCALMEM(math, OP_ADD, VECTOR3, +, Vector3);
  728. DEFAULT_OP_LOCALMEM(math, OP_ADD, QUAT, +, Quat);
  729. DEFAULT_OP_LOCALMEM(math, OP_ADD, COLOR, +, Color);
  730. DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_BYTE_ARRAY, uint8_t);
  731. DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_INT_ARRAY, int);
  732. DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_REAL_ARRAY, real_t);
  733. DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_STRING_ARRAY, String);
  734. DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_VECTOR2_ARRAY, Vector2);
  735. DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_VECTOR3_ARRAY, Vector3);
  736. DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_COLOR_ARRAY, Color);
  737. CASE_TYPE(math, OP_ADD, NIL)
  738. CASE_TYPE(math, OP_ADD, BOOL)
  739. CASE_TYPE(math, OP_ADD, RECT2)
  740. CASE_TYPE(math, OP_ADD, TRANSFORM2D)
  741. CASE_TYPE(math, OP_ADD, PLANE)
  742. CASE_TYPE(math, OP_ADD, AABB)
  743. CASE_TYPE(math, OP_ADD, BASIS)
  744. CASE_TYPE(math, OP_ADD, TRANSFORM)
  745. CASE_TYPE(math, OP_ADD, NODE_PATH)
  746. CASE_TYPE(math, OP_ADD, _RID)
  747. CASE_TYPE(math, OP_ADD, OBJECT)
  748. CASE_TYPE(math, OP_ADD, DICTIONARY)
  749. _RETURN_FAIL;
  750. }
  751. SWITCH_OP(math, OP_SUBTRACT, p_a.type) {
  752. DEFAULT_OP_NUM(math, OP_SUBTRACT, INT, -, _int);
  753. DEFAULT_OP_NUM(math, OP_SUBTRACT, REAL, -, _real);
  754. DEFAULT_OP_LOCALMEM(math, OP_SUBTRACT, VECTOR2, -, Vector2);
  755. DEFAULT_OP_LOCALMEM(math, OP_SUBTRACT, VECTOR3, -, Vector3);
  756. DEFAULT_OP_LOCALMEM(math, OP_SUBTRACT, QUAT, -, Quat);
  757. DEFAULT_OP_LOCALMEM(math, OP_SUBTRACT, COLOR, -, Color);
  758. CASE_TYPE(math, OP_SUBTRACT, NIL)
  759. CASE_TYPE(math, OP_SUBTRACT, BOOL)
  760. CASE_TYPE(math, OP_SUBTRACT, STRING)
  761. CASE_TYPE(math, OP_SUBTRACT, RECT2)
  762. CASE_TYPE(math, OP_SUBTRACT, TRANSFORM2D)
  763. CASE_TYPE(math, OP_SUBTRACT, PLANE)
  764. CASE_TYPE(math, OP_SUBTRACT, AABB)
  765. CASE_TYPE(math, OP_SUBTRACT, BASIS)
  766. CASE_TYPE(math, OP_SUBTRACT, TRANSFORM)
  767. CASE_TYPE(math, OP_SUBTRACT, NODE_PATH)
  768. CASE_TYPE(math, OP_SUBTRACT, _RID)
  769. CASE_TYPE(math, OP_SUBTRACT, OBJECT)
  770. CASE_TYPE(math, OP_SUBTRACT, DICTIONARY)
  771. CASE_TYPE(math, OP_SUBTRACT, ARRAY)
  772. CASE_TYPE(math, OP_SUBTRACT, POOL_BYTE_ARRAY);
  773. CASE_TYPE(math, OP_SUBTRACT, POOL_INT_ARRAY);
  774. CASE_TYPE(math, OP_SUBTRACT, POOL_REAL_ARRAY);
  775. CASE_TYPE(math, OP_SUBTRACT, POOL_STRING_ARRAY);
  776. CASE_TYPE(math, OP_SUBTRACT, POOL_VECTOR2_ARRAY);
  777. CASE_TYPE(math, OP_SUBTRACT, POOL_VECTOR3_ARRAY);
  778. CASE_TYPE(math, OP_SUBTRACT, POOL_COLOR_ARRAY);
  779. _RETURN_FAIL;
  780. }
  781. SWITCH_OP(math, OP_MULTIPLY, p_a.type) {
  782. CASE_TYPE(math, OP_MULTIPLY, TRANSFORM2D) {
  783. switch (p_b.type) {
  784. case TRANSFORM2D: {
  785. _RETURN(*p_a._data._transform2d * *p_b._data._transform2d);
  786. }
  787. case VECTOR2: {
  788. _RETURN(p_a._data._transform2d->xform(*(const Vector2 *)p_b._data._mem));
  789. }
  790. default:
  791. _RETURN_FAIL;
  792. }
  793. }
  794. CASE_TYPE(math, OP_MULTIPLY, QUAT) {
  795. switch (p_b.type) {
  796. case VECTOR3: {
  797. _RETURN(reinterpret_cast<const Quat *>(p_a._data._mem)->xform(*(const Vector3 *)p_b._data._mem));
  798. }
  799. case QUAT: {
  800. _RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) * *reinterpret_cast<const Quat *>(p_b._data._mem));
  801. }
  802. case REAL: {
  803. _RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) * p_b._data._real);
  804. }
  805. default:
  806. _RETURN_FAIL;
  807. }
  808. }
  809. CASE_TYPE(math, OP_MULTIPLY, BASIS) {
  810. switch (p_b.type) {
  811. case VECTOR3: {
  812. _RETURN(p_a._data._basis->xform(*(const Vector3 *)p_b._data._mem));
  813. }
  814. case BASIS: {
  815. _RETURN(*p_a._data._basis * *p_b._data._basis);
  816. }
  817. default:
  818. _RETURN_FAIL;
  819. }
  820. }
  821. CASE_TYPE(math, OP_MULTIPLY, TRANSFORM) {
  822. switch (p_b.type) {
  823. case VECTOR3: {
  824. _RETURN(p_a._data._transform->xform(*(const Vector3 *)p_b._data._mem));
  825. }
  826. case TRANSFORM: {
  827. _RETURN(*p_a._data._transform * *p_b._data._transform);
  828. }
  829. default:
  830. _RETURN_FAIL;
  831. }
  832. }
  833. DEFAULT_OP_NUM_VEC(math, OP_MULTIPLY, INT, *, _int);
  834. DEFAULT_OP_NUM_VEC(math, OP_MULTIPLY, REAL, *, _real);
  835. DEFAULT_OP_LOCALMEM_NUM(math, OP_MULTIPLY, VECTOR2, *, Vector2);
  836. DEFAULT_OP_LOCALMEM_NUM(math, OP_MULTIPLY, VECTOR3, *, Vector3);
  837. DEFAULT_OP_LOCALMEM_NUM(math, OP_MULTIPLY, COLOR, *, Color);
  838. CASE_TYPE(math, OP_MULTIPLY, NIL)
  839. CASE_TYPE(math, OP_MULTIPLY, BOOL)
  840. CASE_TYPE(math, OP_MULTIPLY, STRING)
  841. CASE_TYPE(math, OP_MULTIPLY, RECT2)
  842. CASE_TYPE(math, OP_MULTIPLY, PLANE)
  843. CASE_TYPE(math, OP_MULTIPLY, AABB)
  844. CASE_TYPE(math, OP_MULTIPLY, NODE_PATH)
  845. CASE_TYPE(math, OP_MULTIPLY, _RID)
  846. CASE_TYPE(math, OP_MULTIPLY, OBJECT)
  847. CASE_TYPE(math, OP_MULTIPLY, DICTIONARY)
  848. CASE_TYPE(math, OP_MULTIPLY, ARRAY)
  849. CASE_TYPE(math, OP_MULTIPLY, POOL_BYTE_ARRAY);
  850. CASE_TYPE(math, OP_MULTIPLY, POOL_INT_ARRAY);
  851. CASE_TYPE(math, OP_MULTIPLY, POOL_REAL_ARRAY);
  852. CASE_TYPE(math, OP_MULTIPLY, POOL_STRING_ARRAY);
  853. CASE_TYPE(math, OP_MULTIPLY, POOL_VECTOR2_ARRAY);
  854. CASE_TYPE(math, OP_MULTIPLY, POOL_VECTOR3_ARRAY);
  855. CASE_TYPE(math, OP_MULTIPLY, POOL_COLOR_ARRAY);
  856. _RETURN_FAIL;
  857. }
  858. SWITCH_OP(math, OP_DIVIDE, p_a.type) {
  859. CASE_TYPE(math, OP_DIVIDE, QUAT) {
  860. if (p_b.type != REAL)
  861. _RETURN_FAIL;
  862. #ifdef DEBUG_ENABLED
  863. if (p_b._data._real == 0) {
  864. r_valid = false;
  865. _RETURN("Division By Zero");
  866. }
  867. #endif
  868. _RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) / p_b._data._real);
  869. }
  870. DEFAULT_OP_NUM_DIV(math, OP_DIVIDE, INT, _int);
  871. DEFAULT_OP_NUM_DIV(math, OP_DIVIDE, REAL, _real);
  872. DEFAULT_OP_LOCALMEM_NUM(math, OP_DIVIDE, VECTOR2, /, Vector2);
  873. DEFAULT_OP_LOCALMEM_NUM(math, OP_DIVIDE, VECTOR3, /, Vector3);
  874. DEFAULT_OP_LOCALMEM_NUM(math, OP_DIVIDE, COLOR, /, Color);
  875. CASE_TYPE(math, OP_DIVIDE, NIL)
  876. CASE_TYPE(math, OP_DIVIDE, BOOL)
  877. CASE_TYPE(math, OP_DIVIDE, STRING)
  878. CASE_TYPE(math, OP_DIVIDE, RECT2)
  879. CASE_TYPE(math, OP_DIVIDE, TRANSFORM2D)
  880. CASE_TYPE(math, OP_DIVIDE, PLANE)
  881. CASE_TYPE(math, OP_DIVIDE, AABB)
  882. CASE_TYPE(math, OP_DIVIDE, BASIS)
  883. CASE_TYPE(math, OP_DIVIDE, TRANSFORM)
  884. CASE_TYPE(math, OP_DIVIDE, NODE_PATH)
  885. CASE_TYPE(math, OP_DIVIDE, _RID)
  886. CASE_TYPE(math, OP_DIVIDE, OBJECT)
  887. CASE_TYPE(math, OP_DIVIDE, DICTIONARY)
  888. CASE_TYPE(math, OP_DIVIDE, ARRAY)
  889. CASE_TYPE(math, OP_DIVIDE, POOL_BYTE_ARRAY);
  890. CASE_TYPE(math, OP_DIVIDE, POOL_INT_ARRAY);
  891. CASE_TYPE(math, OP_DIVIDE, POOL_REAL_ARRAY);
  892. CASE_TYPE(math, OP_DIVIDE, POOL_STRING_ARRAY);
  893. CASE_TYPE(math, OP_DIVIDE, POOL_VECTOR2_ARRAY);
  894. CASE_TYPE(math, OP_DIVIDE, POOL_VECTOR3_ARRAY);
  895. CASE_TYPE(math, OP_DIVIDE, POOL_COLOR_ARRAY);
  896. _RETURN_FAIL;
  897. }
  898. SWITCH_OP(math, OP_POSITIVE, p_a.type) {
  899. DEFAULT_OP_NUM_POS(math, OP_POSITIVE, INT, _int);
  900. DEFAULT_OP_NUM_POS(math, OP_POSITIVE, REAL, _real);
  901. DEFAULT_OP_LOCALMEM_POS(math, OP_POSITIVE, VECTOR3, Vector3);
  902. DEFAULT_OP_LOCALMEM_POS(math, OP_POSITIVE, PLANE, Plane);
  903. DEFAULT_OP_LOCALMEM_POS(math, OP_POSITIVE, QUAT, Quat);
  904. DEFAULT_OP_LOCALMEM_POS(math, OP_POSITIVE, VECTOR2, Vector2);
  905. CASE_TYPE(math, OP_POSITIVE, NIL)
  906. CASE_TYPE(math, OP_POSITIVE, BOOL)
  907. CASE_TYPE(math, OP_POSITIVE, STRING)
  908. CASE_TYPE(math, OP_POSITIVE, RECT2)
  909. CASE_TYPE(math, OP_POSITIVE, TRANSFORM2D)
  910. CASE_TYPE(math, OP_POSITIVE, AABB)
  911. CASE_TYPE(math, OP_POSITIVE, BASIS)
  912. CASE_TYPE(math, OP_POSITIVE, TRANSFORM)
  913. CASE_TYPE(math, OP_POSITIVE, COLOR)
  914. CASE_TYPE(math, OP_POSITIVE, NODE_PATH)
  915. CASE_TYPE(math, OP_POSITIVE, _RID)
  916. CASE_TYPE(math, OP_POSITIVE, OBJECT)
  917. CASE_TYPE(math, OP_POSITIVE, DICTIONARY)
  918. CASE_TYPE(math, OP_POSITIVE, ARRAY)
  919. CASE_TYPE(math, OP_POSITIVE, POOL_BYTE_ARRAY)
  920. CASE_TYPE(math, OP_POSITIVE, POOL_INT_ARRAY)
  921. CASE_TYPE(math, OP_POSITIVE, POOL_REAL_ARRAY)
  922. CASE_TYPE(math, OP_POSITIVE, POOL_STRING_ARRAY)
  923. CASE_TYPE(math, OP_POSITIVE, POOL_VECTOR2_ARRAY)
  924. CASE_TYPE(math, OP_POSITIVE, POOL_VECTOR3_ARRAY)
  925. CASE_TYPE(math, OP_POSITIVE, POOL_COLOR_ARRAY)
  926. _RETURN_FAIL;
  927. }
  928. SWITCH_OP(math, OP_NEGATE, p_a.type) {
  929. DEFAULT_OP_NUM_NEG(math, OP_NEGATE, INT, _int);
  930. DEFAULT_OP_NUM_NEG(math, OP_NEGATE, REAL, _real);
  931. DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, VECTOR2, Vector2);
  932. DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, VECTOR3, Vector3);
  933. DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, PLANE, Plane);
  934. DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, QUAT, Quat);
  935. DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, COLOR, Color);
  936. CASE_TYPE(math, OP_NEGATE, NIL)
  937. CASE_TYPE(math, OP_NEGATE, BOOL)
  938. CASE_TYPE(math, OP_NEGATE, STRING)
  939. CASE_TYPE(math, OP_NEGATE, RECT2)
  940. CASE_TYPE(math, OP_NEGATE, TRANSFORM2D)
  941. CASE_TYPE(math, OP_NEGATE, AABB)
  942. CASE_TYPE(math, OP_NEGATE, BASIS)
  943. CASE_TYPE(math, OP_NEGATE, TRANSFORM)
  944. CASE_TYPE(math, OP_NEGATE, NODE_PATH)
  945. CASE_TYPE(math, OP_NEGATE, _RID)
  946. CASE_TYPE(math, OP_NEGATE, OBJECT)
  947. CASE_TYPE(math, OP_NEGATE, DICTIONARY)
  948. CASE_TYPE(math, OP_NEGATE, ARRAY)
  949. CASE_TYPE(math, OP_NEGATE, POOL_BYTE_ARRAY)
  950. CASE_TYPE(math, OP_NEGATE, POOL_INT_ARRAY)
  951. CASE_TYPE(math, OP_NEGATE, POOL_REAL_ARRAY)
  952. CASE_TYPE(math, OP_NEGATE, POOL_STRING_ARRAY)
  953. CASE_TYPE(math, OP_NEGATE, POOL_VECTOR2_ARRAY)
  954. CASE_TYPE(math, OP_NEGATE, POOL_VECTOR3_ARRAY)
  955. CASE_TYPE(math, OP_NEGATE, POOL_COLOR_ARRAY)
  956. _RETURN_FAIL;
  957. }
  958. SWITCH_OP(math, OP_MODULE, p_a.type) {
  959. CASE_TYPE(math, OP_MODULE, INT) {
  960. if (p_b.type != INT)
  961. _RETURN_FAIL;
  962. #ifdef DEBUG_ENABLED
  963. if (p_b._data._int == 0) {
  964. r_valid = false;
  965. _RETURN("Division By Zero");
  966. }
  967. #endif
  968. _RETURN(p_a._data._int % p_b._data._int);
  969. }
  970. CASE_TYPE(math, OP_MODULE, STRING) {
  971. const String *format = reinterpret_cast<const String *>(p_a._data._mem);
  972. String result;
  973. bool error;
  974. if (p_b.type == ARRAY) {
  975. // e.g. "frog %s %d" % ["fish", 12]
  976. const Array *args = reinterpret_cast<const Array *>(p_b._data._mem);
  977. result = format->sprintf(*args, &error);
  978. } else {
  979. // e.g. "frog %d" % 12
  980. Array args;
  981. args.push_back(p_b);
  982. result = format->sprintf(args, &error);
  983. }
  984. r_valid = !error;
  985. _RETURN(result);
  986. }
  987. CASE_TYPE(math, OP_MODULE, NIL)
  988. CASE_TYPE(math, OP_MODULE, BOOL)
  989. CASE_TYPE(math, OP_MODULE, REAL)
  990. CASE_TYPE(math, OP_MODULE, VECTOR2)
  991. CASE_TYPE(math, OP_MODULE, RECT2)
  992. CASE_TYPE(math, OP_MODULE, VECTOR3)
  993. CASE_TYPE(math, OP_MODULE, TRANSFORM2D)
  994. CASE_TYPE(math, OP_MODULE, PLANE)
  995. CASE_TYPE(math, OP_MODULE, QUAT)
  996. CASE_TYPE(math, OP_MODULE, AABB)
  997. CASE_TYPE(math, OP_MODULE, BASIS)
  998. CASE_TYPE(math, OP_MODULE, TRANSFORM)
  999. CASE_TYPE(math, OP_MODULE, COLOR)
  1000. CASE_TYPE(math, OP_MODULE, NODE_PATH)
  1001. CASE_TYPE(math, OP_MODULE, _RID)
  1002. CASE_TYPE(math, OP_MODULE, OBJECT)
  1003. CASE_TYPE(math, OP_MODULE, DICTIONARY)
  1004. CASE_TYPE(math, OP_MODULE, ARRAY)
  1005. CASE_TYPE(math, OP_MODULE, POOL_BYTE_ARRAY)
  1006. CASE_TYPE(math, OP_MODULE, POOL_INT_ARRAY)
  1007. CASE_TYPE(math, OP_MODULE, POOL_REAL_ARRAY)
  1008. CASE_TYPE(math, OP_MODULE, POOL_STRING_ARRAY)
  1009. CASE_TYPE(math, OP_MODULE, POOL_VECTOR2_ARRAY)
  1010. CASE_TYPE(math, OP_MODULE, POOL_VECTOR3_ARRAY)
  1011. CASE_TYPE(math, OP_MODULE, POOL_COLOR_ARRAY)
  1012. _RETURN_FAIL;
  1013. }
  1014. SWITCH_OP(math, OP_STRING_CONCAT, p_a.type) {
  1015. CASE_TYPE_ALL(math, OP_STRING_CONCAT)
  1016. _RETURN(p_a.operator String() + p_b.operator String());
  1017. }
  1018. SWITCH_OP(math, OP_SHIFT_LEFT, p_a.type) {
  1019. CASE_TYPE(math, OP_SHIFT_LEFT, INT) {
  1020. if (p_b.type != INT)
  1021. _RETURN_FAIL;
  1022. if (p_b._data._int < 0 || p_b._data._int >= 64)
  1023. _RETURN_FAIL;
  1024. _RETURN(p_a._data._int << p_b._data._int);
  1025. }
  1026. CASE_TYPE_ALL_BUT_INT(math, OP_SHIFT_LEFT)
  1027. _RETURN_FAIL;
  1028. }
  1029. SWITCH_OP(math, OP_SHIFT_RIGHT, p_a.type) {
  1030. CASE_TYPE(math, OP_SHIFT_RIGHT, INT) {
  1031. if (p_b.type != INT)
  1032. _RETURN_FAIL;
  1033. if (p_b._data._int < 0 || p_b._data._int >= 64)
  1034. _RETURN_FAIL;
  1035. _RETURN(p_a._data._int >> p_b._data._int);
  1036. }
  1037. CASE_TYPE_ALL_BUT_INT(math, OP_SHIFT_RIGHT)
  1038. _RETURN_FAIL;
  1039. }
  1040. SWITCH_OP(math, OP_BIT_AND, p_a.type) {
  1041. CASE_TYPE(math, OP_BIT_AND, INT) {
  1042. if (p_b.type != INT)
  1043. _RETURN_FAIL;
  1044. _RETURN(p_a._data._int & p_b._data._int);
  1045. }
  1046. CASE_TYPE_ALL_BUT_INT(math, OP_BIT_AND)
  1047. _RETURN_FAIL;
  1048. }
  1049. SWITCH_OP(math, OP_BIT_OR, p_a.type) {
  1050. CASE_TYPE(math, OP_BIT_OR, INT) {
  1051. if (p_b.type != INT)
  1052. _RETURN_FAIL;
  1053. _RETURN(p_a._data._int | p_b._data._int);
  1054. }
  1055. CASE_TYPE_ALL_BUT_INT(math, OP_BIT_OR)
  1056. _RETURN_FAIL;
  1057. }
  1058. SWITCH_OP(math, OP_BIT_XOR, p_a.type) {
  1059. CASE_TYPE(math, OP_BIT_XOR, INT) {
  1060. if (p_b.type != INT)
  1061. _RETURN_FAIL;
  1062. _RETURN(p_a._data._int ^ p_b._data._int);
  1063. }
  1064. CASE_TYPE_ALL_BUT_INT(math, OP_BIT_XOR)
  1065. _RETURN_FAIL;
  1066. }
  1067. SWITCH_OP(math, OP_BIT_NEGATE, p_a.type) {
  1068. CASE_TYPE(math, OP_BIT_NEGATE, INT) {
  1069. _RETURN(~p_a._data._int);
  1070. }
  1071. CASE_TYPE_ALL_BUT_INT(math, OP_BIT_NEGATE)
  1072. _RETURN_FAIL;
  1073. }
  1074. SWITCH_OP(math, OP_AND, p_a.type) {
  1075. CASE_TYPE_ALL(math, OP_AND) {
  1076. bool l = p_a.booleanize();
  1077. bool r = p_b.booleanize();
  1078. _RETURN(l && r);
  1079. }
  1080. }
  1081. SWITCH_OP(math, OP_OR, p_a.type) {
  1082. CASE_TYPE_ALL(math, OP_OR) {
  1083. bool l = p_a.booleanize();
  1084. bool r = p_b.booleanize();
  1085. _RETURN(l || r);
  1086. }
  1087. }
  1088. SWITCH_OP(math, OP_XOR, p_a.type) {
  1089. CASE_TYPE_ALL(math, OP_XOR) {
  1090. bool l = p_a.booleanize();
  1091. bool r = p_b.booleanize();
  1092. _RETURN((l || r) && !(l && r));
  1093. }
  1094. }
  1095. SWITCH_OP(math, OP_NOT, p_a.type) {
  1096. CASE_TYPE_ALL(math, OP_NOT) {
  1097. bool l = p_a.booleanize();
  1098. _RETURN(!l);
  1099. }
  1100. }
  1101. SWITCH_OP(math, OP_IN, p_a.type) {
  1102. CASE_TYPE_ALL(math, OP_IN)
  1103. _RETURN(p_b.in(p_a, &r_valid));
  1104. }
  1105. }
  1106. }
  1107. void Variant::set_named(const StringName &p_index, const Variant &p_value, bool *r_valid) {
  1108. bool valid = false;
  1109. switch (type) {
  1110. case VECTOR2: {
  1111. if (p_value.type == Variant::INT) {
  1112. Vector2 *v = reinterpret_cast<Vector2 *>(_data._mem);
  1113. if (p_index == CoreStringNames::singleton->x) {
  1114. v->x = p_value._data._int;
  1115. valid = true;
  1116. } else if (p_index == CoreStringNames::singleton->y) {
  1117. v->y = p_value._data._int;
  1118. valid = true;
  1119. }
  1120. } else if (p_value.type == Variant::REAL) {
  1121. Vector2 *v = reinterpret_cast<Vector2 *>(_data._mem);
  1122. if (p_index == CoreStringNames::singleton->x) {
  1123. v->x = p_value._data._real;
  1124. valid = true;
  1125. } else if (p_index == CoreStringNames::singleton->y) {
  1126. v->y = p_value._data._real;
  1127. valid = true;
  1128. }
  1129. }
  1130. } break;
  1131. case RECT2: {
  1132. if (p_value.type == Variant::VECTOR2) {
  1133. Rect2 *v = reinterpret_cast<Rect2 *>(_data._mem);
  1134. //scalar name
  1135. if (p_index == CoreStringNames::singleton->position) {
  1136. v->position = *reinterpret_cast<const Vector2 *>(p_value._data._mem);
  1137. valid = true;
  1138. } else if (p_index == CoreStringNames::singleton->size) {
  1139. v->size = *reinterpret_cast<const Vector2 *>(p_value._data._mem);
  1140. valid = true;
  1141. } else if (p_index == CoreStringNames::singleton->end) {
  1142. v->size = *reinterpret_cast<const Vector2 *>(p_value._data._mem) - v->position;
  1143. valid = true;
  1144. }
  1145. }
  1146. } break;
  1147. case TRANSFORM2D: {
  1148. if (p_value.type == Variant::VECTOR2) {
  1149. Transform2D *v = _data._transform2d;
  1150. if (p_index == CoreStringNames::singleton->x) {
  1151. v->elements[0] = *reinterpret_cast<const Vector2 *>(p_value._data._mem);
  1152. valid = true;
  1153. } else if (p_index == CoreStringNames::singleton->y) {
  1154. v->elements[1] = *reinterpret_cast<const Vector2 *>(p_value._data._mem);
  1155. valid = true;
  1156. } else if (p_index == CoreStringNames::singleton->origin) {
  1157. v->elements[2] = *reinterpret_cast<const Vector2 *>(p_value._data._mem);
  1158. valid = true;
  1159. }
  1160. }
  1161. } break;
  1162. case VECTOR3: {
  1163. if (p_value.type == Variant::INT) {
  1164. Vector3 *v = reinterpret_cast<Vector3 *>(_data._mem);
  1165. if (p_index == CoreStringNames::singleton->x) {
  1166. v->x = p_value._data._int;
  1167. valid = true;
  1168. } else if (p_index == CoreStringNames::singleton->y) {
  1169. v->y = p_value._data._int;
  1170. valid = true;
  1171. } else if (p_index == CoreStringNames::singleton->z) {
  1172. v->z = p_value._data._int;
  1173. valid = true;
  1174. }
  1175. } else if (p_value.type == Variant::REAL) {
  1176. Vector3 *v = reinterpret_cast<Vector3 *>(_data._mem);
  1177. if (p_index == CoreStringNames::singleton->x) {
  1178. v->x = p_value._data._real;
  1179. valid = true;
  1180. } else if (p_index == CoreStringNames::singleton->y) {
  1181. v->y = p_value._data._real;
  1182. valid = true;
  1183. } else if (p_index == CoreStringNames::singleton->z) {
  1184. v->z = p_value._data._real;
  1185. valid = true;
  1186. }
  1187. }
  1188. } break;
  1189. case PLANE: {
  1190. if (p_value.type == Variant::INT) {
  1191. Plane *v = reinterpret_cast<Plane *>(_data._mem);
  1192. if (p_index == CoreStringNames::singleton->x) {
  1193. v->normal.x = p_value._data._int;
  1194. valid = true;
  1195. } else if (p_index == CoreStringNames::singleton->y) {
  1196. v->normal.y = p_value._data._int;
  1197. valid = true;
  1198. } else if (p_index == CoreStringNames::singleton->z) {
  1199. v->normal.z = p_value._data._int;
  1200. valid = true;
  1201. } else if (p_index == CoreStringNames::singleton->d) {
  1202. v->d = p_value._data._int;
  1203. valid = true;
  1204. }
  1205. } else if (p_value.type == Variant::REAL) {
  1206. Plane *v = reinterpret_cast<Plane *>(_data._mem);
  1207. if (p_index == CoreStringNames::singleton->x) {
  1208. v->normal.x = p_value._data._real;
  1209. valid = true;
  1210. } else if (p_index == CoreStringNames::singleton->y) {
  1211. v->normal.y = p_value._data._real;
  1212. valid = true;
  1213. } else if (p_index == CoreStringNames::singleton->z) {
  1214. v->normal.z = p_value._data._real;
  1215. valid = true;
  1216. } else if (p_index == CoreStringNames::singleton->d) {
  1217. v->d = p_value._data._real;
  1218. valid = true;
  1219. }
  1220. } else if (p_value.type == Variant::VECTOR3) {
  1221. Plane *v = reinterpret_cast<Plane *>(_data._mem);
  1222. if (p_index == CoreStringNames::singleton->normal) {
  1223. v->normal = *reinterpret_cast<const Vector3 *>(p_value._data._mem);
  1224. valid = true;
  1225. }
  1226. }
  1227. } break;
  1228. case QUAT: {
  1229. if (p_value.type == Variant::INT) {
  1230. Quat *v = reinterpret_cast<Quat *>(_data._mem);
  1231. if (p_index == CoreStringNames::singleton->x) {
  1232. v->x = p_value._data._int;
  1233. valid = true;
  1234. } else if (p_index == CoreStringNames::singleton->y) {
  1235. v->y = p_value._data._int;
  1236. valid = true;
  1237. } else if (p_index == CoreStringNames::singleton->z) {
  1238. v->z = p_value._data._int;
  1239. valid = true;
  1240. } else if (p_index == CoreStringNames::singleton->w) {
  1241. v->w = p_value._data._int;
  1242. valid = true;
  1243. }
  1244. } else if (p_value.type == Variant::REAL) {
  1245. Quat *v = reinterpret_cast<Quat *>(_data._mem);
  1246. if (p_index == CoreStringNames::singleton->x) {
  1247. v->x = p_value._data._real;
  1248. valid = true;
  1249. } else if (p_index == CoreStringNames::singleton->y) {
  1250. v->y = p_value._data._real;
  1251. valid = true;
  1252. } else if (p_index == CoreStringNames::singleton->z) {
  1253. v->z = p_value._data._real;
  1254. valid = true;
  1255. } else if (p_index == CoreStringNames::singleton->w) {
  1256. v->w = p_value._data._real;
  1257. valid = true;
  1258. }
  1259. }
  1260. } break; // 10
  1261. case AABB: {
  1262. if (p_value.type == Variant::VECTOR3) {
  1263. ::AABB *v = _data._aabb;
  1264. //scalar name
  1265. if (p_index == CoreStringNames::singleton->position) {
  1266. v->position = *reinterpret_cast<const Vector3 *>(p_value._data._mem);
  1267. valid = true;
  1268. } else if (p_index == CoreStringNames::singleton->size) {
  1269. v->size = *reinterpret_cast<const Vector3 *>(p_value._data._mem);
  1270. valid = true;
  1271. } else if (p_index == CoreStringNames::singleton->end) {
  1272. v->size = *reinterpret_cast<const Vector3 *>(p_value._data._mem) - v->position;
  1273. valid = true;
  1274. }
  1275. }
  1276. } break;
  1277. case BASIS: {
  1278. if (p_value.type == Variant::VECTOR3) {
  1279. Basis *v = _data._basis;
  1280. //scalar name
  1281. if (p_index == CoreStringNames::singleton->x) {
  1282. v->set_axis(0, *reinterpret_cast<const Vector3 *>(p_value._data._mem));
  1283. valid = true;
  1284. } else if (p_index == CoreStringNames::singleton->y) {
  1285. v->set_axis(1, *reinterpret_cast<const Vector3 *>(p_value._data._mem));
  1286. valid = true;
  1287. } else if (p_index == CoreStringNames::singleton->z) {
  1288. v->set_axis(2, *reinterpret_cast<const Vector3 *>(p_value._data._mem));
  1289. valid = true;
  1290. }
  1291. }
  1292. } break;
  1293. case TRANSFORM: {
  1294. if (p_value.type == Variant::BASIS && p_index == CoreStringNames::singleton->basis) {
  1295. _data._transform->basis = *p_value._data._basis;
  1296. valid = true;
  1297. } else if (p_value.type == Variant::VECTOR3 && p_index == CoreStringNames::singleton->origin) {
  1298. _data._transform->origin = *reinterpret_cast<const Vector3 *>(p_value._data._mem);
  1299. valid = true;
  1300. }
  1301. } break;
  1302. case COLOR: {
  1303. if (p_value.type == Variant::INT) {
  1304. Color *v = reinterpret_cast<Color *>(_data._mem);
  1305. if (p_index == CoreStringNames::singleton->r) {
  1306. v->r = p_value._data._int;
  1307. valid = true;
  1308. } else if (p_index == CoreStringNames::singleton->g) {
  1309. v->g = p_value._data._int;
  1310. valid = true;
  1311. } else if (p_index == CoreStringNames::singleton->b) {
  1312. v->b = p_value._data._int;
  1313. valid = true;
  1314. } else if (p_index == CoreStringNames::singleton->a) {
  1315. v->a = p_value._data._int;
  1316. valid = true;
  1317. } else if (p_index == CoreStringNames::singleton->r8) {
  1318. v->r = p_value._data._int / 255.0;
  1319. valid = true;
  1320. } else if (p_index == CoreStringNames::singleton->g8) {
  1321. v->g = p_value._data._int / 255.0;
  1322. valid = true;
  1323. } else if (p_index == CoreStringNames::singleton->b8) {
  1324. v->b = p_value._data._int / 255.0;
  1325. valid = true;
  1326. } else if (p_index == CoreStringNames::singleton->a8) {
  1327. v->a = p_value._data._int / 255.0;
  1328. valid = true;
  1329. } else if (p_index == CoreStringNames::singleton->h) {
  1330. v->set_hsv(p_value._data._int, v->get_s(), v->get_v(), v->a);
  1331. valid = true;
  1332. } else if (p_index == CoreStringNames::singleton->s) {
  1333. v->set_hsv(v->get_h(), p_value._data._int, v->get_v(), v->a);
  1334. valid = true;
  1335. } else if (p_index == CoreStringNames::singleton->v) {
  1336. v->set_hsv(v->get_h(), v->get_s(), p_value._data._int, v->a);
  1337. valid = true;
  1338. }
  1339. } else if (p_value.type == Variant::REAL) {
  1340. Color *v = reinterpret_cast<Color *>(_data._mem);
  1341. if (p_index == CoreStringNames::singleton->r) {
  1342. v->r = p_value._data._real;
  1343. valid = true;
  1344. } else if (p_index == CoreStringNames::singleton->g) {
  1345. v->g = p_value._data._real;
  1346. valid = true;
  1347. } else if (p_index == CoreStringNames::singleton->b) {
  1348. v->b = p_value._data._real;
  1349. valid = true;
  1350. } else if (p_index == CoreStringNames::singleton->a) {
  1351. v->a = p_value._data._real;
  1352. valid = true;
  1353. } else if (p_index == CoreStringNames::singleton->r8) {
  1354. v->r = p_value._data._real / 255.0;
  1355. valid = true;
  1356. } else if (p_index == CoreStringNames::singleton->g8) {
  1357. v->g = p_value._data._real / 255.0;
  1358. valid = true;
  1359. } else if (p_index == CoreStringNames::singleton->b8) {
  1360. v->b = p_value._data._real / 255.0;
  1361. valid = true;
  1362. } else if (p_index == CoreStringNames::singleton->a8) {
  1363. v->a = p_value._data._real / 255.0;
  1364. valid = true;
  1365. } else if (p_index == CoreStringNames::singleton->h) {
  1366. v->set_hsv(p_value._data._real, v->get_s(), v->get_v(), v->a);
  1367. valid = true;
  1368. } else if (p_index == CoreStringNames::singleton->s) {
  1369. v->set_hsv(v->get_h(), p_value._data._real, v->get_v(), v->a);
  1370. valid = true;
  1371. } else if (p_index == CoreStringNames::singleton->v) {
  1372. v->set_hsv(v->get_h(), v->get_s(), p_value._data._real, v->a);
  1373. valid = true;
  1374. }
  1375. }
  1376. } break;
  1377. case OBJECT: {
  1378. Object *obj = _OBJ_PTR(*this);
  1379. if (unlikely(!obj)) {
  1380. #ifdef DEBUG_ENABLED
  1381. if (_get_obj().rc) {
  1382. ERR_PRINT("Attempted set on a deleted object.");
  1383. }
  1384. #endif
  1385. break;
  1386. }
  1387. obj->set(p_index, p_value, &valid);
  1388. } break;
  1389. default: {
  1390. set(p_index.operator String(), p_value, &valid);
  1391. } break;
  1392. }
  1393. if (r_valid) {
  1394. *r_valid = valid;
  1395. }
  1396. }
  1397. Variant Variant::get_named(const StringName &p_index, bool *r_valid) const {
  1398. if (r_valid) {
  1399. *r_valid = true;
  1400. }
  1401. switch (type) {
  1402. case VECTOR2: {
  1403. const Vector2 *v = reinterpret_cast<const Vector2 *>(_data._mem);
  1404. if (p_index == CoreStringNames::singleton->x) {
  1405. return v->x;
  1406. } else if (p_index == CoreStringNames::singleton->y) {
  1407. return v->y;
  1408. }
  1409. } break;
  1410. case RECT2: {
  1411. const Rect2 *v = reinterpret_cast<const Rect2 *>(_data._mem);
  1412. //scalar name
  1413. if (p_index == CoreStringNames::singleton->position) {
  1414. return v->position;
  1415. } else if (p_index == CoreStringNames::singleton->size) {
  1416. return v->size;
  1417. } else if (p_index == CoreStringNames::singleton->end) {
  1418. return v->size + v->position;
  1419. }
  1420. } break;
  1421. case TRANSFORM2D: {
  1422. const Transform2D *v = _data._transform2d;
  1423. if (p_index == CoreStringNames::singleton->x) {
  1424. return v->elements[0];
  1425. } else if (p_index == CoreStringNames::singleton->y) {
  1426. return v->elements[1];
  1427. } else if (p_index == CoreStringNames::singleton->origin) {
  1428. return v->elements[2];
  1429. }
  1430. } break;
  1431. case VECTOR3: {
  1432. const Vector3 *v = reinterpret_cast<const Vector3 *>(_data._mem);
  1433. if (p_index == CoreStringNames::singleton->x) {
  1434. return v->x;
  1435. } else if (p_index == CoreStringNames::singleton->y) {
  1436. return v->y;
  1437. } else if (p_index == CoreStringNames::singleton->z) {
  1438. return v->z;
  1439. }
  1440. } break;
  1441. case PLANE: {
  1442. const Plane *v = reinterpret_cast<const Plane *>(_data._mem);
  1443. if (p_index == CoreStringNames::singleton->x) {
  1444. return v->normal.x;
  1445. } else if (p_index == CoreStringNames::singleton->y) {
  1446. return v->normal.y;
  1447. } else if (p_index == CoreStringNames::singleton->z) {
  1448. return v->normal.z;
  1449. } else if (p_index == CoreStringNames::singleton->d) {
  1450. return v->d;
  1451. } else if (p_index == CoreStringNames::singleton->normal) {
  1452. return v->normal;
  1453. }
  1454. } break;
  1455. case QUAT: {
  1456. const Quat *v = reinterpret_cast<const Quat *>(_data._mem);
  1457. if (p_index == CoreStringNames::singleton->x) {
  1458. return v->x;
  1459. } else if (p_index == CoreStringNames::singleton->y) {
  1460. return v->y;
  1461. } else if (p_index == CoreStringNames::singleton->z) {
  1462. return v->z;
  1463. } else if (p_index == CoreStringNames::singleton->w) {
  1464. return v->w;
  1465. }
  1466. } break; // 10
  1467. case AABB: {
  1468. const ::AABB *v = _data._aabb;
  1469. //scalar name
  1470. if (p_index == CoreStringNames::singleton->position) {
  1471. return v->position;
  1472. } else if (p_index == CoreStringNames::singleton->size) {
  1473. return v->size;
  1474. } else if (p_index == CoreStringNames::singleton->end) {
  1475. return v->size + v->position;
  1476. }
  1477. } break;
  1478. case BASIS: {
  1479. const Basis *v = _data._basis;
  1480. //scalar name
  1481. if (p_index == CoreStringNames::singleton->x) {
  1482. return v->get_axis(0);
  1483. } else if (p_index == CoreStringNames::singleton->y) {
  1484. return v->get_axis(1);
  1485. } else if (p_index == CoreStringNames::singleton->z) {
  1486. return v->get_axis(2);
  1487. }
  1488. } break;
  1489. case TRANSFORM: {
  1490. if (p_index == CoreStringNames::singleton->basis) {
  1491. return _data._transform->basis;
  1492. } else if (p_index == CoreStringNames::singleton->origin) {
  1493. return _data._transform->origin;
  1494. }
  1495. } break;
  1496. case COLOR: {
  1497. const Color *v = reinterpret_cast<const Color *>(_data._mem);
  1498. if (p_index == CoreStringNames::singleton->r) {
  1499. return v->r;
  1500. } else if (p_index == CoreStringNames::singleton->g) {
  1501. return v->g;
  1502. } else if (p_index == CoreStringNames::singleton->b) {
  1503. return v->b;
  1504. } else if (p_index == CoreStringNames::singleton->a) {
  1505. return v->a;
  1506. } else if (p_index == CoreStringNames::singleton->r8) {
  1507. return int(Math::round(v->r * 255.0));
  1508. } else if (p_index == CoreStringNames::singleton->g8) {
  1509. return int(Math::round(v->g * 255.0));
  1510. } else if (p_index == CoreStringNames::singleton->b8) {
  1511. return int(Math::round(v->b * 255.0));
  1512. } else if (p_index == CoreStringNames::singleton->a8) {
  1513. return int(Math::round(v->a * 255.0));
  1514. } else if (p_index == CoreStringNames::singleton->h) {
  1515. return v->get_h();
  1516. } else if (p_index == CoreStringNames::singleton->s) {
  1517. return v->get_s();
  1518. } else if (p_index == CoreStringNames::singleton->v) {
  1519. return v->get_v();
  1520. }
  1521. } break;
  1522. case OBJECT: {
  1523. Object *obj = _OBJ_PTR(*this);
  1524. if (unlikely(!obj)) {
  1525. if (r_valid) {
  1526. *r_valid = false;
  1527. }
  1528. #ifdef DEBUG_ENABLED
  1529. if (_get_obj().rc) {
  1530. ERR_PRINT("Attempted get on a deleted object.");
  1531. }
  1532. #endif
  1533. return Variant();
  1534. }
  1535. return obj->get(p_index, r_valid);
  1536. } break;
  1537. default: {
  1538. return get(p_index.operator String(), r_valid);
  1539. }
  1540. }
  1541. if (r_valid) {
  1542. *r_valid = false;
  1543. }
  1544. return Variant();
  1545. }
  1546. #define DEFAULT_OP_ARRAY_CMD(m_name, m_type, skip_test, cmd) \
  1547. case m_name: { \
  1548. skip_test; \
  1549. \
  1550. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) { \
  1551. int index = p_index; \
  1552. m_type *arr = reinterpret_cast<m_type *>(_data._mem); \
  1553. \
  1554. if (index < 0) \
  1555. index += arr->size(); \
  1556. if (index >= 0 && index < arr->size()) { \
  1557. valid = true; \
  1558. cmd; \
  1559. } \
  1560. } \
  1561. } break;
  1562. // clang-format 14 wants to add a space after the last return,
  1563. // and clang-format 15 removes it...
  1564. /* clang-format off */
  1565. #define DEFAULT_OP_DVECTOR_SET(m_name, dv_type, skip_cond) \
  1566. DEFAULT_OP_ARRAY_CMD(m_name, PoolVector<dv_type>, if (skip_cond) return;, arr->set(index, p_value); return)
  1567. /* clang-format on */
  1568. #define DEFAULT_OP_DVECTOR_GET(m_name, dv_type) \
  1569. DEFAULT_OP_ARRAY_CMD(m_name, const PoolVector<dv_type>, ;, return arr->get(index))
  1570. void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid) {
  1571. static bool _dummy = false;
  1572. bool &valid = r_valid ? *r_valid : _dummy;
  1573. valid = false;
  1574. switch (type) {
  1575. case NIL: {
  1576. return;
  1577. } break;
  1578. case BOOL: {
  1579. return;
  1580. } break;
  1581. case INT: {
  1582. return;
  1583. } break;
  1584. case REAL: {
  1585. return;
  1586. } break;
  1587. case STRING: {
  1588. if (p_index.type != Variant::INT && p_index.type != Variant::REAL) {
  1589. return;
  1590. }
  1591. int idx = p_index;
  1592. String *str = reinterpret_cast<String *>(_data._mem);
  1593. int len = str->length();
  1594. if (idx < 0) {
  1595. idx += len;
  1596. }
  1597. if (idx < 0 || idx >= len) {
  1598. return;
  1599. }
  1600. String chr;
  1601. if (p_value.type == Variant::INT || p_value.type == Variant::REAL) {
  1602. chr = String::chr(p_value);
  1603. } else if (p_value.type == Variant::STRING) {
  1604. chr = p_value;
  1605. } else {
  1606. return;
  1607. }
  1608. *str = str->substr(0, idx) + chr + str->substr(idx + 1, len);
  1609. valid = true;
  1610. return;
  1611. } break;
  1612. case VECTOR2: {
  1613. if (p_value.type != Variant::INT && p_value.type != Variant::REAL) {
  1614. return;
  1615. }
  1616. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  1617. // scalar index
  1618. int idx = p_index;
  1619. if (idx < 0) {
  1620. idx += 2;
  1621. }
  1622. if (idx >= 0 && idx < 2) {
  1623. Vector2 *v = reinterpret_cast<Vector2 *>(_data._mem);
  1624. valid = true;
  1625. (*v)[idx] = p_value;
  1626. return;
  1627. }
  1628. } else if (p_index.get_type() == Variant::STRING) {
  1629. //scalar name
  1630. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1631. Vector2 *v = reinterpret_cast<Vector2 *>(_data._mem);
  1632. if (*str == "x") {
  1633. valid = true;
  1634. v->x = p_value;
  1635. return;
  1636. } else if (*str == "y") {
  1637. valid = true;
  1638. v->y = p_value;
  1639. return;
  1640. }
  1641. }
  1642. } break; // 5
  1643. case RECT2: {
  1644. if (p_value.type != Variant::VECTOR2) {
  1645. return;
  1646. }
  1647. if (p_index.get_type() == Variant::STRING) {
  1648. //scalar name
  1649. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1650. Rect2 *v = reinterpret_cast<Rect2 *>(_data._mem);
  1651. if (*str == "position") {
  1652. valid = true;
  1653. v->position = p_value;
  1654. return;
  1655. } else if (*str == "size") {
  1656. valid = true;
  1657. v->size = p_value;
  1658. return;
  1659. } else if (*str == "end") {
  1660. valid = true;
  1661. v->size = Vector2(p_value) - v->position;
  1662. return;
  1663. }
  1664. }
  1665. } break;
  1666. case TRANSFORM2D: {
  1667. if (p_value.type != Variant::VECTOR2) {
  1668. return;
  1669. }
  1670. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  1671. int index = p_index;
  1672. if (index < 0) {
  1673. index += 3;
  1674. }
  1675. if (index >= 0 && index < 3) {
  1676. Transform2D *v = _data._transform2d;
  1677. valid = true;
  1678. v->elements[index] = p_value;
  1679. return;
  1680. }
  1681. } else if (p_index.get_type() == Variant::STRING && p_value.get_type() == Variant::VECTOR2) {
  1682. //scalar name
  1683. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1684. Transform2D *v = _data._transform2d;
  1685. if (*str == "x") {
  1686. valid = true;
  1687. v->elements[0] = p_value;
  1688. return;
  1689. } else if (*str == "y") {
  1690. valid = true;
  1691. v->elements[1] = p_value;
  1692. return;
  1693. } else if (*str == "origin") {
  1694. valid = true;
  1695. v->elements[2] = p_value;
  1696. return;
  1697. }
  1698. }
  1699. } break;
  1700. case VECTOR3: {
  1701. if (p_value.type != Variant::INT && p_value.type != Variant::REAL) {
  1702. return;
  1703. }
  1704. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  1705. //scalar index
  1706. int idx = p_index;
  1707. if (idx < 0) {
  1708. idx += 3;
  1709. }
  1710. if (idx >= 0 && idx < 3) {
  1711. Vector3 *v = reinterpret_cast<Vector3 *>(_data._mem);
  1712. valid = true;
  1713. (*v)[idx] = p_value;
  1714. return;
  1715. }
  1716. } else if (p_index.get_type() == Variant::STRING) {
  1717. //scalar name
  1718. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1719. Vector3 *v = reinterpret_cast<Vector3 *>(_data._mem);
  1720. if (*str == "x") {
  1721. valid = true;
  1722. v->x = p_value;
  1723. return;
  1724. } else if (*str == "y") {
  1725. valid = true;
  1726. v->y = p_value;
  1727. return;
  1728. } else if (*str == "z") {
  1729. valid = true;
  1730. v->z = p_value;
  1731. return;
  1732. }
  1733. }
  1734. } break;
  1735. case PLANE: {
  1736. if (p_index.get_type() == Variant::STRING) {
  1737. //scalar name
  1738. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1739. Plane *v = reinterpret_cast<Plane *>(_data._mem);
  1740. if (*str == "x") {
  1741. if (p_value.type != Variant::INT && p_value.type != Variant::REAL) {
  1742. return;
  1743. }
  1744. valid = true;
  1745. v->normal.x = p_value;
  1746. return;
  1747. } else if (*str == "y") {
  1748. if (p_value.type != Variant::INT && p_value.type != Variant::REAL) {
  1749. return;
  1750. }
  1751. valid = true;
  1752. v->normal.y = p_value;
  1753. return;
  1754. } else if (*str == "z") {
  1755. if (p_value.type != Variant::INT && p_value.type != Variant::REAL) {
  1756. return;
  1757. }
  1758. valid = true;
  1759. v->normal.z = p_value;
  1760. return;
  1761. } else if (*str == "normal") {
  1762. if (p_value.type != Variant::VECTOR3) {
  1763. return;
  1764. }
  1765. valid = true;
  1766. v->normal = p_value;
  1767. return;
  1768. } else if (*str == "d") {
  1769. valid = true;
  1770. v->d = p_value;
  1771. return;
  1772. }
  1773. }
  1774. } break;
  1775. case QUAT: {
  1776. if (p_value.type != Variant::INT && p_value.type != Variant::REAL) {
  1777. return;
  1778. }
  1779. if (p_index.get_type() == Variant::STRING) {
  1780. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1781. Quat *v = reinterpret_cast<Quat *>(_data._mem);
  1782. if (*str == "x") {
  1783. valid = true;
  1784. v->x = p_value;
  1785. return;
  1786. } else if (*str == "y") {
  1787. valid = true;
  1788. v->y = p_value;
  1789. return;
  1790. } else if (*str == "z") {
  1791. valid = true;
  1792. v->z = p_value;
  1793. return;
  1794. } else if (*str == "w") {
  1795. valid = true;
  1796. v->w = p_value;
  1797. return;
  1798. }
  1799. }
  1800. } break; // 10
  1801. case AABB: {
  1802. if (p_value.type != Variant::VECTOR3) {
  1803. return;
  1804. }
  1805. if (p_index.get_type() == Variant::STRING) {
  1806. //scalar name
  1807. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1808. ::AABB *v = _data._aabb;
  1809. if (*str == "position") {
  1810. valid = true;
  1811. v->position = p_value;
  1812. return;
  1813. } else if (*str == "size") {
  1814. valid = true;
  1815. v->size = p_value;
  1816. return;
  1817. } else if (*str == "end") {
  1818. valid = true;
  1819. v->size = Vector3(p_value) - v->position;
  1820. return;
  1821. }
  1822. }
  1823. } break;
  1824. case BASIS: {
  1825. if (p_value.type != Variant::VECTOR3) {
  1826. return;
  1827. }
  1828. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  1829. int index = p_index;
  1830. if (index < 0) {
  1831. index += 3;
  1832. }
  1833. if (index >= 0 && index < 3) {
  1834. Basis *v = _data._basis;
  1835. valid = true;
  1836. v->set_axis(index, p_value);
  1837. return;
  1838. }
  1839. } else if (p_index.get_type() == Variant::STRING) {
  1840. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1841. Basis *v = _data._basis;
  1842. if (*str == "x") {
  1843. valid = true;
  1844. v->set_axis(0, p_value);
  1845. return;
  1846. } else if (*str == "y") {
  1847. valid = true;
  1848. v->set_axis(1, p_value);
  1849. return;
  1850. } else if (*str == "z") {
  1851. valid = true;
  1852. v->set_axis(2, p_value);
  1853. return;
  1854. }
  1855. }
  1856. } break;
  1857. case TRANSFORM: {
  1858. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  1859. if (p_value.type != Variant::VECTOR3) {
  1860. return;
  1861. }
  1862. int index = p_index;
  1863. if (index < 0) {
  1864. index += 4;
  1865. }
  1866. if (index >= 0 && index < 4) {
  1867. Transform *v = _data._transform;
  1868. valid = true;
  1869. if (index == 3) {
  1870. v->origin = p_value;
  1871. } else {
  1872. v->basis.set_axis(index, p_value);
  1873. }
  1874. return;
  1875. }
  1876. } else if (p_index.get_type() == Variant::STRING) {
  1877. Transform *v = _data._transform;
  1878. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1879. if (*str == "basis") {
  1880. if (p_value.type != Variant::BASIS) {
  1881. return;
  1882. }
  1883. valid = true;
  1884. v->basis = p_value;
  1885. return;
  1886. }
  1887. if (*str == "origin") {
  1888. if (p_value.type != Variant::VECTOR3) {
  1889. return;
  1890. }
  1891. valid = true;
  1892. v->origin = p_value;
  1893. return;
  1894. }
  1895. }
  1896. } break;
  1897. case COLOR: {
  1898. if (p_value.type != Variant::INT && p_value.type != Variant::REAL) {
  1899. return;
  1900. }
  1901. if (p_index.get_type() == Variant::STRING) {
  1902. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  1903. Color *v = reinterpret_cast<Color *>(_data._mem);
  1904. if (*str == "r") {
  1905. valid = true;
  1906. v->r = p_value;
  1907. return;
  1908. } else if (*str == "g") {
  1909. valid = true;
  1910. v->g = p_value;
  1911. return;
  1912. } else if (*str == "b") {
  1913. valid = true;
  1914. v->b = p_value;
  1915. return;
  1916. } else if (*str == "a") {
  1917. valid = true;
  1918. v->a = p_value;
  1919. return;
  1920. } else if (*str == "h") {
  1921. valid = true;
  1922. v->set_hsv(p_value, v->get_s(), v->get_v(), v->a);
  1923. return;
  1924. } else if (*str == "s") {
  1925. valid = true;
  1926. v->set_hsv(v->get_h(), p_value, v->get_v(), v->a);
  1927. return;
  1928. } else if (*str == "v") {
  1929. valid = true;
  1930. v->set_hsv(v->get_h(), v->get_s(), p_value, v->a);
  1931. return;
  1932. } else if (*str == "r8") {
  1933. valid = true;
  1934. v->r = float(p_value) / 255.0;
  1935. return;
  1936. } else if (*str == "g8") {
  1937. valid = true;
  1938. v->g = float(p_value) / 255.0;
  1939. return;
  1940. } else if (*str == "b8") {
  1941. valid = true;
  1942. v->b = float(p_value) / 255.0;
  1943. return;
  1944. } else if (*str == "a8") {
  1945. valid = true;
  1946. v->a = float(p_value) / 255.0;
  1947. return;
  1948. }
  1949. } else if (p_index.get_type() == Variant::INT) {
  1950. int idx = p_index;
  1951. if (idx < 0) {
  1952. idx += 4;
  1953. }
  1954. if (idx >= 0 && idx < 4) {
  1955. Color *v = reinterpret_cast<Color *>(_data._mem);
  1956. (*v)[idx] = p_value;
  1957. valid = true;
  1958. }
  1959. }
  1960. } break;
  1961. case NODE_PATH: {
  1962. } break; // 15
  1963. case _RID: {
  1964. } break;
  1965. case OBJECT: {
  1966. Object *obj = _OBJ_PTR(*this);
  1967. if (unlikely(!obj)) {
  1968. valid = false;
  1969. #ifdef DEBUG_ENABLED
  1970. if (_get_obj().rc) {
  1971. ERR_PRINT("Attempted set on a deleted object.");
  1972. }
  1973. #endif
  1974. return;
  1975. }
  1976. if (p_index.get_type() != Variant::STRING) {
  1977. obj->setvar(p_index, p_value, r_valid);
  1978. return;
  1979. }
  1980. obj->set(p_index, p_value, r_valid);
  1981. return;
  1982. } break;
  1983. case DICTIONARY: {
  1984. Dictionary *dic = reinterpret_cast<Dictionary *>(_data._mem);
  1985. dic->operator[](p_index) = p_value;
  1986. valid = true; //always valid, i guess? should this really be ok?
  1987. return;
  1988. } break;
  1989. // clang-format 14 wants to add a space after the last return,
  1990. // and clang-format 15 removes it...
  1991. /* clang-format off */
  1992. DEFAULT_OP_ARRAY_CMD(ARRAY, Array, ;, (*arr)[index] = p_value; return) // 20
  1993. /* clang-format on */
  1994. DEFAULT_OP_DVECTOR_SET(POOL_BYTE_ARRAY, uint8_t, p_value.type != Variant::REAL && p_value.type != Variant::INT)
  1995. DEFAULT_OP_DVECTOR_SET(POOL_INT_ARRAY, int, p_value.type != Variant::REAL && p_value.type != Variant::INT)
  1996. DEFAULT_OP_DVECTOR_SET(POOL_REAL_ARRAY, real_t, p_value.type != Variant::REAL && p_value.type != Variant::INT)
  1997. DEFAULT_OP_DVECTOR_SET(POOL_STRING_ARRAY, String, p_value.type != Variant::STRING)
  1998. DEFAULT_OP_DVECTOR_SET(POOL_VECTOR2_ARRAY, Vector2, p_value.type != Variant::VECTOR2) // 25
  1999. DEFAULT_OP_DVECTOR_SET(POOL_VECTOR3_ARRAY, Vector3, p_value.type != Variant::VECTOR3)
  2000. DEFAULT_OP_DVECTOR_SET(POOL_COLOR_ARRAY, Color, p_value.type != Variant::COLOR)
  2001. default:
  2002. return;
  2003. }
  2004. }
  2005. Variant Variant::get(const Variant &p_index, bool *r_valid) const {
  2006. static bool _dummy = false;
  2007. bool &valid = r_valid ? *r_valid : _dummy;
  2008. valid = false;
  2009. switch (type) {
  2010. case NIL: {
  2011. return Variant();
  2012. } break;
  2013. case BOOL: {
  2014. return Variant();
  2015. } break;
  2016. case INT: {
  2017. return Variant();
  2018. } break;
  2019. case REAL: {
  2020. return Variant();
  2021. } break;
  2022. case STRING: {
  2023. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  2024. //string index
  2025. int idx = p_index;
  2026. const String *str = reinterpret_cast<const String *>(_data._mem);
  2027. if (idx < 0) {
  2028. idx += str->length();
  2029. }
  2030. if (idx >= 0 && idx < str->length()) {
  2031. valid = true;
  2032. return str->substr(idx, 1);
  2033. }
  2034. }
  2035. } break;
  2036. case VECTOR2: {
  2037. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  2038. // scalar index
  2039. int idx = p_index;
  2040. if (idx < 0) {
  2041. idx += 2;
  2042. }
  2043. if (idx >= 0 && idx < 2) {
  2044. const Vector2 *v = reinterpret_cast<const Vector2 *>(_data._mem);
  2045. valid = true;
  2046. return (*v)[idx];
  2047. }
  2048. } else if (p_index.get_type() == Variant::STRING) {
  2049. //scalar name
  2050. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2051. const Vector2 *v = reinterpret_cast<const Vector2 *>(_data._mem);
  2052. if (*str == "x") {
  2053. valid = true;
  2054. return v->x;
  2055. } else if (*str == "y") {
  2056. valid = true;
  2057. return v->y;
  2058. }
  2059. }
  2060. } break; // 5
  2061. case RECT2: {
  2062. if (p_index.get_type() == Variant::STRING) {
  2063. //scalar name
  2064. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2065. const Rect2 *v = reinterpret_cast<const Rect2 *>(_data._mem);
  2066. if (*str == "position") {
  2067. valid = true;
  2068. return v->position;
  2069. } else if (*str == "size") {
  2070. valid = true;
  2071. return v->size;
  2072. } else if (*str == "end") {
  2073. valid = true;
  2074. return v->size + v->position;
  2075. }
  2076. }
  2077. } break;
  2078. case VECTOR3: {
  2079. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  2080. //scalar index
  2081. int idx = p_index;
  2082. if (idx < 0) {
  2083. idx += 3;
  2084. }
  2085. if (idx >= 0 && idx < 3) {
  2086. const Vector3 *v = reinterpret_cast<const Vector3 *>(_data._mem);
  2087. valid = true;
  2088. return (*v)[idx];
  2089. }
  2090. } else if (p_index.get_type() == Variant::STRING) {
  2091. //scalar name
  2092. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2093. const Vector3 *v = reinterpret_cast<const Vector3 *>(_data._mem);
  2094. if (*str == "x") {
  2095. valid = true;
  2096. return v->x;
  2097. } else if (*str == "y") {
  2098. valid = true;
  2099. return v->y;
  2100. } else if (*str == "z") {
  2101. valid = true;
  2102. return v->z;
  2103. }
  2104. }
  2105. } break;
  2106. case TRANSFORM2D: {
  2107. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  2108. int index = p_index;
  2109. if (index < 0) {
  2110. index += 3;
  2111. }
  2112. if (index >= 0 && index < 3) {
  2113. const Transform2D *v = _data._transform2d;
  2114. valid = true;
  2115. return v->elements[index];
  2116. }
  2117. } else if (p_index.get_type() == Variant::STRING) {
  2118. //scalar name
  2119. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2120. const Transform2D *v = _data._transform2d;
  2121. if (*str == "x") {
  2122. valid = true;
  2123. return v->elements[0];
  2124. } else if (*str == "y") {
  2125. valid = true;
  2126. return v->elements[1];
  2127. } else if (*str == "origin") {
  2128. valid = true;
  2129. return v->elements[2];
  2130. }
  2131. }
  2132. } break;
  2133. case PLANE: {
  2134. if (p_index.get_type() == Variant::STRING) {
  2135. //scalar name
  2136. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2137. const Plane *v = reinterpret_cast<const Plane *>(_data._mem);
  2138. if (*str == "x") {
  2139. valid = true;
  2140. return v->normal.x;
  2141. } else if (*str == "y") {
  2142. valid = true;
  2143. return v->normal.y;
  2144. } else if (*str == "z") {
  2145. valid = true;
  2146. return v->normal.z;
  2147. } else if (*str == "normal") {
  2148. valid = true;
  2149. return v->normal;
  2150. } else if (*str == "d") {
  2151. valid = true;
  2152. return v->d;
  2153. }
  2154. }
  2155. } break;
  2156. case QUAT: {
  2157. if (p_index.get_type() == Variant::STRING) {
  2158. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2159. const Quat *v = reinterpret_cast<const Quat *>(_data._mem);
  2160. if (*str == "x") {
  2161. valid = true;
  2162. return v->x;
  2163. } else if (*str == "y") {
  2164. valid = true;
  2165. return v->y;
  2166. } else if (*str == "z") {
  2167. valid = true;
  2168. return v->z;
  2169. } else if (*str == "w") {
  2170. valid = true;
  2171. return v->w;
  2172. }
  2173. }
  2174. } break; // 10
  2175. case AABB: {
  2176. if (p_index.get_type() == Variant::STRING) {
  2177. //scalar name
  2178. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2179. const ::AABB *v = _data._aabb;
  2180. if (*str == "position") {
  2181. valid = true;
  2182. return v->position;
  2183. } else if (*str == "size") {
  2184. valid = true;
  2185. return v->size;
  2186. } else if (*str == "end") {
  2187. valid = true;
  2188. return v->size + v->position;
  2189. }
  2190. }
  2191. } break;
  2192. case BASIS: {
  2193. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  2194. int index = p_index;
  2195. if (index < 0) {
  2196. index += 3;
  2197. }
  2198. if (index >= 0 && index < 3) {
  2199. const Basis *v = _data._basis;
  2200. valid = true;
  2201. return v->get_axis(index);
  2202. }
  2203. } else if (p_index.get_type() == Variant::STRING) {
  2204. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2205. const Basis *v = _data._basis;
  2206. if (*str == "x") {
  2207. valid = true;
  2208. return v->get_axis(0);
  2209. } else if (*str == "y") {
  2210. valid = true;
  2211. return v->get_axis(1);
  2212. } else if (*str == "z") {
  2213. valid = true;
  2214. return v->get_axis(2);
  2215. }
  2216. }
  2217. } break;
  2218. case TRANSFORM: {
  2219. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  2220. int index = p_index;
  2221. if (index < 0) {
  2222. index += 4;
  2223. }
  2224. if (index >= 0 && index < 4) {
  2225. const Transform *v = _data._transform;
  2226. valid = true;
  2227. return index == 3 ? v->origin : v->basis.get_axis(index);
  2228. }
  2229. } else if (p_index.get_type() == Variant::STRING) {
  2230. const Transform *v = _data._transform;
  2231. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2232. if (*str == "basis") {
  2233. valid = true;
  2234. return v->basis;
  2235. }
  2236. if (*str == "origin") {
  2237. valid = true;
  2238. return v->origin;
  2239. }
  2240. }
  2241. } break;
  2242. case COLOR: {
  2243. if (p_index.get_type() == Variant::STRING) {
  2244. const String *str = reinterpret_cast<const String *>(p_index._data._mem);
  2245. const Color *v = reinterpret_cast<const Color *>(_data._mem);
  2246. if (*str == "r") {
  2247. valid = true;
  2248. return v->r;
  2249. } else if (*str == "g") {
  2250. valid = true;
  2251. return v->g;
  2252. } else if (*str == "b") {
  2253. valid = true;
  2254. return v->b;
  2255. } else if (*str == "a") {
  2256. valid = true;
  2257. return v->a;
  2258. } else if (*str == "h") {
  2259. valid = true;
  2260. return v->get_h();
  2261. } else if (*str == "s") {
  2262. valid = true;
  2263. return v->get_s();
  2264. } else if (*str == "v") {
  2265. valid = true;
  2266. return v->get_v();
  2267. } else if (*str == "r8") {
  2268. valid = true;
  2269. return (int)Math::round(v->r * 255.0);
  2270. } else if (*str == "g8") {
  2271. valid = true;
  2272. return (int)Math::round(v->g * 255.0);
  2273. } else if (*str == "b8") {
  2274. valid = true;
  2275. return (int)Math::round(v->b * 255.0);
  2276. } else if (*str == "a8") {
  2277. valid = true;
  2278. return (int)Math::round(v->a * 255.0);
  2279. }
  2280. } else if (p_index.get_type() == Variant::INT) {
  2281. int idx = p_index;
  2282. if (idx < 0) {
  2283. idx += 4;
  2284. }
  2285. if (idx >= 0 && idx < 4) {
  2286. const Color *v = reinterpret_cast<const Color *>(_data._mem);
  2287. valid = true;
  2288. return (*v)[idx];
  2289. }
  2290. }
  2291. } break;
  2292. case NODE_PATH: {
  2293. } break; // 15
  2294. case _RID: {
  2295. } break;
  2296. case OBJECT: {
  2297. Object *obj = _OBJ_PTR(*this);
  2298. if (unlikely(!obj)) {
  2299. valid = false;
  2300. #ifdef DEBUG_ENABLED
  2301. if (_get_obj().rc) {
  2302. ERR_PRINT("Attempted get on a deleted object.");
  2303. }
  2304. #endif
  2305. return Variant();
  2306. }
  2307. if (p_index.get_type() != Variant::STRING) {
  2308. return obj->getvar(p_index, r_valid);
  2309. } else {
  2310. return obj->get(p_index, r_valid);
  2311. }
  2312. } break;
  2313. case DICTIONARY: {
  2314. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  2315. const Variant *res = dic->getptr(p_index);
  2316. if (res) {
  2317. valid = true;
  2318. return *res;
  2319. }
  2320. } break;
  2321. DEFAULT_OP_ARRAY_CMD(ARRAY, const Array, ;, return (*arr)[index]) // 20
  2322. DEFAULT_OP_DVECTOR_GET(POOL_BYTE_ARRAY, uint8_t)
  2323. DEFAULT_OP_DVECTOR_GET(POOL_INT_ARRAY, int)
  2324. DEFAULT_OP_DVECTOR_GET(POOL_REAL_ARRAY, real_t)
  2325. DEFAULT_OP_DVECTOR_GET(POOL_STRING_ARRAY, String)
  2326. DEFAULT_OP_DVECTOR_GET(POOL_VECTOR2_ARRAY, Vector2) // 25
  2327. DEFAULT_OP_DVECTOR_GET(POOL_VECTOR3_ARRAY, Vector3)
  2328. DEFAULT_OP_DVECTOR_GET(POOL_COLOR_ARRAY, Color)
  2329. default:
  2330. return Variant();
  2331. }
  2332. return Variant();
  2333. }
  2334. bool Variant::in(const Variant &p_index, bool *r_valid) const {
  2335. if (r_valid) {
  2336. *r_valid = true;
  2337. }
  2338. switch (type) {
  2339. case STRING: {
  2340. if (p_index.get_type() == Variant::STRING) {
  2341. //string index
  2342. String idx = p_index;
  2343. const String *str = reinterpret_cast<const String *>(_data._mem);
  2344. return str->find(idx) != -1;
  2345. }
  2346. } break;
  2347. case OBJECT: {
  2348. Object *obj = _OBJ_PTR(*this);
  2349. if (unlikely(!obj)) {
  2350. if (r_valid) {
  2351. *r_valid = false;
  2352. }
  2353. #ifdef DEBUG_ENABLED
  2354. if (_get_obj().rc) {
  2355. ERR_PRINT("Attempted 'in' on a deleted object.");
  2356. }
  2357. #endif
  2358. return false;
  2359. }
  2360. bool result;
  2361. if (p_index.get_type() != Variant::STRING) {
  2362. obj->getvar(p_index, &result);
  2363. } else {
  2364. obj->get(p_index, &result);
  2365. }
  2366. return result;
  2367. } break;
  2368. case DICTIONARY: {
  2369. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  2370. return dic->has(p_index);
  2371. } break; // 20
  2372. case ARRAY: {
  2373. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  2374. int l = arr->size();
  2375. if (l) {
  2376. for (int i = 0; i < l; i++) {
  2377. if (evaluate(OP_EQUAL, (*arr)[i], p_index)) {
  2378. return true;
  2379. }
  2380. }
  2381. }
  2382. return false;
  2383. } break;
  2384. case POOL_BYTE_ARRAY: {
  2385. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  2386. int index = p_index;
  2387. const PoolVector<uint8_t> *arr = reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2388. int l = arr->size();
  2389. if (l) {
  2390. PoolVector<uint8_t>::Read r = arr->read();
  2391. for (int i = 0; i < l; i++) {
  2392. if (r[i] == index) {
  2393. return true;
  2394. }
  2395. }
  2396. }
  2397. return false;
  2398. }
  2399. } break;
  2400. case POOL_INT_ARRAY: {
  2401. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  2402. int index = p_index;
  2403. const PoolVector<int> *arr = reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2404. int l = arr->size();
  2405. if (l) {
  2406. PoolVector<int>::Read r = arr->read();
  2407. for (int i = 0; i < l; i++) {
  2408. if (r[i] == index) {
  2409. return true;
  2410. }
  2411. }
  2412. }
  2413. return false;
  2414. }
  2415. } break;
  2416. case POOL_REAL_ARRAY: {
  2417. if (p_index.get_type() == Variant::INT || p_index.get_type() == Variant::REAL) {
  2418. real_t index = p_index;
  2419. const PoolVector<real_t> *arr = reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2420. int l = arr->size();
  2421. if (l) {
  2422. PoolVector<real_t>::Read r = arr->read();
  2423. for (int i = 0; i < l; i++) {
  2424. if (r[i] == index) {
  2425. return true;
  2426. }
  2427. }
  2428. }
  2429. return false;
  2430. }
  2431. } break;
  2432. case POOL_STRING_ARRAY: {
  2433. if (p_index.get_type() == Variant::STRING) {
  2434. String index = p_index;
  2435. const PoolVector<String> *arr = reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2436. int l = arr->size();
  2437. if (l) {
  2438. PoolVector<String>::Read r = arr->read();
  2439. for (int i = 0; i < l; i++) {
  2440. if (r[i] == index) {
  2441. return true;
  2442. }
  2443. }
  2444. }
  2445. return false;
  2446. }
  2447. } break; //25
  2448. case POOL_VECTOR2_ARRAY: {
  2449. if (p_index.get_type() == Variant::VECTOR2) {
  2450. Vector2 index = p_index;
  2451. const PoolVector<Vector2> *arr = reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2452. int l = arr->size();
  2453. if (l) {
  2454. PoolVector<Vector2>::Read r = arr->read();
  2455. for (int i = 0; i < l; i++) {
  2456. if (r[i] == index) {
  2457. return true;
  2458. }
  2459. }
  2460. }
  2461. return false;
  2462. }
  2463. } break;
  2464. case POOL_VECTOR3_ARRAY: {
  2465. if (p_index.get_type() == Variant::VECTOR3) {
  2466. Vector3 index = p_index;
  2467. const PoolVector<Vector3> *arr = reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2468. int l = arr->size();
  2469. if (l) {
  2470. PoolVector<Vector3>::Read r = arr->read();
  2471. for (int i = 0; i < l; i++) {
  2472. if (r[i] == index) {
  2473. return true;
  2474. }
  2475. }
  2476. }
  2477. return false;
  2478. }
  2479. } break;
  2480. case POOL_COLOR_ARRAY: {
  2481. if (p_index.get_type() == Variant::COLOR) {
  2482. Color index = p_index;
  2483. const PoolVector<Color> *arr = reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2484. int l = arr->size();
  2485. if (l) {
  2486. PoolVector<Color>::Read r = arr->read();
  2487. for (int i = 0; i < l; i++) {
  2488. if (r[i] == index) {
  2489. return true;
  2490. }
  2491. }
  2492. }
  2493. return false;
  2494. }
  2495. } break;
  2496. default: {
  2497. }
  2498. }
  2499. if (r_valid) {
  2500. *r_valid = false;
  2501. }
  2502. return false;
  2503. }
  2504. void Variant::get_property_list(List<PropertyInfo> *p_list) const {
  2505. switch (type) {
  2506. case VECTOR2: {
  2507. p_list->push_back(PropertyInfo(Variant::REAL, "x"));
  2508. p_list->push_back(PropertyInfo(Variant::REAL, "y"));
  2509. } break; // 5
  2510. case RECT2: {
  2511. p_list->push_back(PropertyInfo(Variant::VECTOR2, "position"));
  2512. p_list->push_back(PropertyInfo(Variant::VECTOR2, "size"));
  2513. p_list->push_back(PropertyInfo(Variant::VECTOR2, "end"));
  2514. } break;
  2515. case VECTOR3: {
  2516. p_list->push_back(PropertyInfo(Variant::REAL, "x"));
  2517. p_list->push_back(PropertyInfo(Variant::REAL, "y"));
  2518. p_list->push_back(PropertyInfo(Variant::REAL, "z"));
  2519. } break;
  2520. case TRANSFORM2D: {
  2521. p_list->push_back(PropertyInfo(Variant::VECTOR2, "x"));
  2522. p_list->push_back(PropertyInfo(Variant::VECTOR2, "y"));
  2523. p_list->push_back(PropertyInfo(Variant::VECTOR2, "origin"));
  2524. } break;
  2525. case PLANE: {
  2526. p_list->push_back(PropertyInfo(Variant::VECTOR3, "normal"));
  2527. p_list->push_back(PropertyInfo(Variant::REAL, "x"));
  2528. p_list->push_back(PropertyInfo(Variant::REAL, "y"));
  2529. p_list->push_back(PropertyInfo(Variant::REAL, "z"));
  2530. p_list->push_back(PropertyInfo(Variant::REAL, "d"));
  2531. } break;
  2532. case QUAT: {
  2533. p_list->push_back(PropertyInfo(Variant::REAL, "x"));
  2534. p_list->push_back(PropertyInfo(Variant::REAL, "y"));
  2535. p_list->push_back(PropertyInfo(Variant::REAL, "z"));
  2536. p_list->push_back(PropertyInfo(Variant::REAL, "w"));
  2537. } break; // 10
  2538. case AABB: {
  2539. p_list->push_back(PropertyInfo(Variant::VECTOR3, "position"));
  2540. p_list->push_back(PropertyInfo(Variant::VECTOR3, "size"));
  2541. p_list->push_back(PropertyInfo(Variant::VECTOR3, "end"));
  2542. } break;
  2543. case BASIS: {
  2544. p_list->push_back(PropertyInfo(Variant::VECTOR3, "x"));
  2545. p_list->push_back(PropertyInfo(Variant::VECTOR3, "y"));
  2546. p_list->push_back(PropertyInfo(Variant::VECTOR3, "z"));
  2547. } break;
  2548. case TRANSFORM: {
  2549. p_list->push_back(PropertyInfo(Variant::BASIS, "basis"));
  2550. p_list->push_back(PropertyInfo(Variant::VECTOR3, "origin"));
  2551. } break;
  2552. case COLOR: {
  2553. p_list->push_back(PropertyInfo(Variant::REAL, "r"));
  2554. p_list->push_back(PropertyInfo(Variant::REAL, "g"));
  2555. p_list->push_back(PropertyInfo(Variant::REAL, "b"));
  2556. p_list->push_back(PropertyInfo(Variant::REAL, "a"));
  2557. p_list->push_back(PropertyInfo(Variant::REAL, "h"));
  2558. p_list->push_back(PropertyInfo(Variant::REAL, "s"));
  2559. p_list->push_back(PropertyInfo(Variant::REAL, "v"));
  2560. p_list->push_back(PropertyInfo(Variant::INT, "r8"));
  2561. p_list->push_back(PropertyInfo(Variant::INT, "g8"));
  2562. p_list->push_back(PropertyInfo(Variant::INT, "b8"));
  2563. p_list->push_back(PropertyInfo(Variant::INT, "a8"));
  2564. } break;
  2565. case NODE_PATH: {
  2566. } break; // 15
  2567. case _RID: {
  2568. } break;
  2569. case OBJECT: {
  2570. Object *obj = _OBJ_PTR(*this);
  2571. if (unlikely(!obj)) {
  2572. #ifdef DEBUG_ENABLED
  2573. if (_get_obj().rc) {
  2574. ERR_PRINT("Attempted get property list on a deleted object.");
  2575. }
  2576. #endif
  2577. return;
  2578. }
  2579. obj->get_property_list(p_list);
  2580. } break;
  2581. case DICTIONARY: {
  2582. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  2583. List<Variant> keys;
  2584. dic->get_key_list(&keys);
  2585. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  2586. if (E->get().get_type() == Variant::STRING) {
  2587. p_list->push_back(PropertyInfo(Variant::STRING, E->get()));
  2588. }
  2589. }
  2590. } break;
  2591. case ARRAY: // 20
  2592. case POOL_BYTE_ARRAY:
  2593. case POOL_INT_ARRAY:
  2594. case POOL_REAL_ARRAY:
  2595. case POOL_STRING_ARRAY:
  2596. case POOL_VECTOR2_ARRAY: // 25
  2597. case POOL_VECTOR3_ARRAY:
  2598. case POOL_COLOR_ARRAY: {
  2599. //nothing
  2600. } break;
  2601. default: {
  2602. }
  2603. }
  2604. }
  2605. bool Variant::iter_init(Variant &r_iter, bool &valid) const {
  2606. valid = true;
  2607. switch (type) {
  2608. case INT: {
  2609. r_iter = 0;
  2610. return _data._int > 0;
  2611. } break;
  2612. case REAL: {
  2613. r_iter = 0;
  2614. return _data._real > 0.0;
  2615. } break;
  2616. case VECTOR2: {
  2617. int64_t from = reinterpret_cast<const Vector2 *>(_data._mem)->x;
  2618. int64_t to = reinterpret_cast<const Vector2 *>(_data._mem)->y;
  2619. r_iter = from;
  2620. return from < to;
  2621. } break;
  2622. case VECTOR3: {
  2623. int64_t from = reinterpret_cast<const Vector3 *>(_data._mem)->x;
  2624. int64_t to = reinterpret_cast<const Vector3 *>(_data._mem)->y;
  2625. int64_t step = reinterpret_cast<const Vector3 *>(_data._mem)->z;
  2626. r_iter = from;
  2627. if (from == to) {
  2628. return false;
  2629. } else if (from < to) {
  2630. return step > 0;
  2631. } else {
  2632. return step < 0;
  2633. }
  2634. //return true;
  2635. } break;
  2636. case OBJECT: {
  2637. Object *obj = _OBJ_PTR(*this);
  2638. if (unlikely(!obj)) {
  2639. valid = false;
  2640. #ifdef DEBUG_ENABLED
  2641. if (_get_obj().rc) {
  2642. ERR_PRINT("Attempted iteration start on a deleted object.");
  2643. }
  2644. #endif
  2645. return false;
  2646. }
  2647. Variant::CallError ce;
  2648. ce.error = Variant::CallError::CALL_OK;
  2649. Array ref;
  2650. ref.push_back(r_iter);
  2651. Variant vref = ref;
  2652. const Variant *refp[] = { &vref };
  2653. Variant ret = obj->call(CoreStringNames::get_singleton()->_iter_init, refp, 1, ce);
  2654. if (ref.size() != 1 || ce.error != Variant::CallError::CALL_OK) {
  2655. valid = false;
  2656. return false;
  2657. }
  2658. r_iter = ref[0];
  2659. return ret;
  2660. } break;
  2661. case STRING: {
  2662. const String *str = reinterpret_cast<const String *>(_data._mem);
  2663. if (str->empty()) {
  2664. return false;
  2665. }
  2666. r_iter = 0;
  2667. return true;
  2668. } break;
  2669. case DICTIONARY: {
  2670. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  2671. if (dic->empty()) {
  2672. return false;
  2673. }
  2674. const Variant *next = dic->next(nullptr);
  2675. r_iter = *next;
  2676. return true;
  2677. } break;
  2678. case ARRAY: {
  2679. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  2680. if (arr->empty()) {
  2681. return false;
  2682. }
  2683. r_iter = 0;
  2684. return true;
  2685. } break;
  2686. case POOL_BYTE_ARRAY: {
  2687. const PoolVector<uint8_t> *arr = reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2688. if (arr->size() == 0) {
  2689. return false;
  2690. }
  2691. r_iter = 0;
  2692. return true;
  2693. } break;
  2694. case POOL_INT_ARRAY: {
  2695. const PoolVector<int> *arr = reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2696. if (arr->size() == 0) {
  2697. return false;
  2698. }
  2699. r_iter = 0;
  2700. return true;
  2701. } break;
  2702. case POOL_REAL_ARRAY: {
  2703. const PoolVector<real_t> *arr = reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2704. if (arr->size() == 0) {
  2705. return false;
  2706. }
  2707. r_iter = 0;
  2708. return true;
  2709. } break;
  2710. case POOL_STRING_ARRAY: {
  2711. const PoolVector<String> *arr = reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2712. if (arr->size() == 0) {
  2713. return false;
  2714. }
  2715. r_iter = 0;
  2716. return true;
  2717. } break;
  2718. case POOL_VECTOR2_ARRAY: {
  2719. const PoolVector<Vector2> *arr = reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2720. if (arr->size() == 0) {
  2721. return false;
  2722. }
  2723. r_iter = 0;
  2724. return true;
  2725. } break;
  2726. case POOL_VECTOR3_ARRAY: {
  2727. const PoolVector<Vector3> *arr = reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2728. if (arr->size() == 0) {
  2729. return false;
  2730. }
  2731. r_iter = 0;
  2732. return true;
  2733. } break;
  2734. case POOL_COLOR_ARRAY: {
  2735. const PoolVector<Color> *arr = reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2736. if (arr->size() == 0) {
  2737. return false;
  2738. }
  2739. r_iter = 0;
  2740. return true;
  2741. } break;
  2742. default: {
  2743. }
  2744. }
  2745. valid = false;
  2746. return false;
  2747. }
  2748. bool Variant::iter_next(Variant &r_iter, bool &valid) const {
  2749. valid = true;
  2750. switch (type) {
  2751. case INT: {
  2752. int64_t idx = r_iter;
  2753. idx++;
  2754. if (idx >= _data._int) {
  2755. return false;
  2756. }
  2757. r_iter = idx;
  2758. return true;
  2759. } break;
  2760. case REAL: {
  2761. int64_t idx = r_iter;
  2762. idx++;
  2763. if (idx >= _data._real) {
  2764. return false;
  2765. }
  2766. r_iter = idx;
  2767. return true;
  2768. } break;
  2769. case VECTOR2: {
  2770. int64_t to = reinterpret_cast<const Vector2 *>(_data._mem)->y;
  2771. int64_t idx = r_iter;
  2772. idx++;
  2773. if (idx >= to) {
  2774. return false;
  2775. }
  2776. r_iter = idx;
  2777. return true;
  2778. } break;
  2779. case VECTOR3: {
  2780. int64_t to = reinterpret_cast<const Vector3 *>(_data._mem)->y;
  2781. int64_t step = reinterpret_cast<const Vector3 *>(_data._mem)->z;
  2782. int64_t idx = r_iter;
  2783. idx += step;
  2784. if (step < 0 && idx <= to) {
  2785. return false;
  2786. }
  2787. if (step > 0 && idx >= to) {
  2788. return false;
  2789. }
  2790. r_iter = idx;
  2791. return true;
  2792. } break;
  2793. case OBJECT: {
  2794. Object *obj = _OBJ_PTR(*this);
  2795. if (unlikely(!obj)) {
  2796. valid = false;
  2797. #ifdef DEBUG_ENABLED
  2798. if (_get_obj().rc) {
  2799. ERR_PRINT("Attempted iteration check next on a deleted object.");
  2800. }
  2801. #endif
  2802. return false;
  2803. }
  2804. Variant::CallError ce;
  2805. ce.error = Variant::CallError::CALL_OK;
  2806. Array ref;
  2807. ref.push_back(r_iter);
  2808. Variant vref = ref;
  2809. const Variant *refp[] = { &vref };
  2810. Variant ret = obj->call(CoreStringNames::get_singleton()->_iter_next, refp, 1, ce);
  2811. if (ref.size() != 1 || ce.error != Variant::CallError::CALL_OK) {
  2812. valid = false;
  2813. return false;
  2814. }
  2815. r_iter = ref[0];
  2816. return ret;
  2817. } break;
  2818. case STRING: {
  2819. const String *str = reinterpret_cast<const String *>(_data._mem);
  2820. int idx = r_iter;
  2821. idx++;
  2822. if (idx >= str->length()) {
  2823. return false;
  2824. }
  2825. r_iter = idx;
  2826. return true;
  2827. } break;
  2828. case DICTIONARY: {
  2829. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  2830. const Variant *next = dic->next(&r_iter);
  2831. if (!next) {
  2832. return false;
  2833. }
  2834. r_iter = *next;
  2835. return true;
  2836. } break;
  2837. case ARRAY: {
  2838. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  2839. int idx = r_iter;
  2840. idx++;
  2841. if (idx >= arr->size()) {
  2842. return false;
  2843. }
  2844. r_iter = idx;
  2845. return true;
  2846. } break;
  2847. case POOL_BYTE_ARRAY: {
  2848. const PoolVector<uint8_t> *arr = reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2849. int idx = r_iter;
  2850. idx++;
  2851. if (idx >= arr->size()) {
  2852. return false;
  2853. }
  2854. r_iter = idx;
  2855. return true;
  2856. } break;
  2857. case POOL_INT_ARRAY: {
  2858. const PoolVector<int> *arr = reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2859. int idx = r_iter;
  2860. idx++;
  2861. if (idx >= arr->size()) {
  2862. return false;
  2863. }
  2864. r_iter = idx;
  2865. return true;
  2866. } break;
  2867. case POOL_REAL_ARRAY: {
  2868. const PoolVector<real_t> *arr = reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2869. int idx = r_iter;
  2870. idx++;
  2871. if (idx >= arr->size()) {
  2872. return false;
  2873. }
  2874. r_iter = idx;
  2875. return true;
  2876. } break;
  2877. case POOL_STRING_ARRAY: {
  2878. const PoolVector<String> *arr = reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2879. int idx = r_iter;
  2880. idx++;
  2881. if (idx >= arr->size()) {
  2882. return false;
  2883. }
  2884. r_iter = idx;
  2885. return true;
  2886. } break;
  2887. case POOL_VECTOR2_ARRAY: {
  2888. const PoolVector<Vector2> *arr = reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2889. int idx = r_iter;
  2890. idx++;
  2891. if (idx >= arr->size()) {
  2892. return false;
  2893. }
  2894. r_iter = idx;
  2895. return true;
  2896. } break;
  2897. case POOL_VECTOR3_ARRAY: {
  2898. const PoolVector<Vector3> *arr = reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2899. int idx = r_iter;
  2900. idx++;
  2901. if (idx >= arr->size()) {
  2902. return false;
  2903. }
  2904. r_iter = idx;
  2905. return true;
  2906. } break;
  2907. case POOL_COLOR_ARRAY: {
  2908. const PoolVector<Color> *arr = reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2909. int idx = r_iter;
  2910. idx++;
  2911. if (idx >= arr->size()) {
  2912. return false;
  2913. }
  2914. r_iter = idx;
  2915. return true;
  2916. } break;
  2917. default: {
  2918. }
  2919. }
  2920. valid = false;
  2921. return false;
  2922. }
  2923. Variant Variant::iter_get(const Variant &r_iter, bool &r_valid) const {
  2924. r_valid = true;
  2925. switch (type) {
  2926. case INT: {
  2927. return r_iter;
  2928. } break;
  2929. case REAL: {
  2930. return r_iter;
  2931. } break;
  2932. case VECTOR2: {
  2933. return r_iter;
  2934. } break;
  2935. case VECTOR3: {
  2936. return r_iter;
  2937. } break;
  2938. case OBJECT: {
  2939. Object *obj = _OBJ_PTR(*this);
  2940. if (unlikely(!obj)) {
  2941. r_valid = false;
  2942. #ifdef DEBUG_ENABLED
  2943. if (_get_obj().rc) {
  2944. ERR_PRINT("Attempted iteration get next on a deleted object.");
  2945. }
  2946. #endif
  2947. return Variant();
  2948. }
  2949. Variant::CallError ce;
  2950. ce.error = Variant::CallError::CALL_OK;
  2951. const Variant *refp[] = { &r_iter };
  2952. Variant ret = obj->call(CoreStringNames::get_singleton()->_iter_get, refp, 1, ce);
  2953. if (ce.error != Variant::CallError::CALL_OK) {
  2954. r_valid = false;
  2955. return Variant();
  2956. }
  2957. //r_iter=ref[0];
  2958. return ret;
  2959. } break;
  2960. case STRING: {
  2961. const String *str = reinterpret_cast<const String *>(_data._mem);
  2962. return str->substr(r_iter, 1);
  2963. } break;
  2964. case DICTIONARY: {
  2965. return r_iter; //iterator is the same as the key
  2966. } break;
  2967. case ARRAY: {
  2968. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  2969. int idx = r_iter;
  2970. #ifdef DEBUG_ENABLED
  2971. if (idx < 0 || idx >= arr->size()) {
  2972. r_valid = false;
  2973. return Variant();
  2974. }
  2975. #endif
  2976. return arr->get(idx);
  2977. } break;
  2978. case POOL_BYTE_ARRAY: {
  2979. const PoolVector<uint8_t> *arr = reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2980. int idx = r_iter;
  2981. #ifdef DEBUG_ENABLED
  2982. if (idx < 0 || idx >= arr->size()) {
  2983. r_valid = false;
  2984. return Variant();
  2985. }
  2986. #endif
  2987. return arr->get(idx);
  2988. } break;
  2989. case POOL_INT_ARRAY: {
  2990. const PoolVector<int> *arr = reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2991. int idx = r_iter;
  2992. #ifdef DEBUG_ENABLED
  2993. if (idx < 0 || idx >= arr->size()) {
  2994. r_valid = false;
  2995. return Variant();
  2996. }
  2997. #endif
  2998. return arr->get(idx);
  2999. } break;
  3000. case POOL_REAL_ARRAY: {
  3001. const PoolVector<real_t> *arr = reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  3002. int idx = r_iter;
  3003. #ifdef DEBUG_ENABLED
  3004. if (idx < 0 || idx >= arr->size()) {
  3005. r_valid = false;
  3006. return Variant();
  3007. }
  3008. #endif
  3009. return arr->get(idx);
  3010. } break;
  3011. case POOL_STRING_ARRAY: {
  3012. const PoolVector<String> *arr = reinterpret_cast<const PoolVector<String> *>(_data._mem);
  3013. int idx = r_iter;
  3014. #ifdef DEBUG_ENABLED
  3015. if (idx < 0 || idx >= arr->size()) {
  3016. r_valid = false;
  3017. return Variant();
  3018. }
  3019. #endif
  3020. return arr->get(idx);
  3021. } break;
  3022. case POOL_VECTOR2_ARRAY: {
  3023. const PoolVector<Vector2> *arr = reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  3024. int idx = r_iter;
  3025. #ifdef DEBUG_ENABLED
  3026. if (idx < 0 || idx >= arr->size()) {
  3027. r_valid = false;
  3028. return Variant();
  3029. }
  3030. #endif
  3031. return arr->get(idx);
  3032. } break;
  3033. case POOL_VECTOR3_ARRAY: {
  3034. const PoolVector<Vector3> *arr = reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  3035. int idx = r_iter;
  3036. #ifdef DEBUG_ENABLED
  3037. if (idx < 0 || idx >= arr->size()) {
  3038. r_valid = false;
  3039. return Variant();
  3040. }
  3041. #endif
  3042. return arr->get(idx);
  3043. } break;
  3044. case POOL_COLOR_ARRAY: {
  3045. const PoolVector<Color> *arr = reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  3046. int idx = r_iter;
  3047. #ifdef DEBUG_ENABLED
  3048. if (idx < 0 || idx >= arr->size()) {
  3049. r_valid = false;
  3050. return Variant();
  3051. }
  3052. #endif
  3053. return arr->get(idx);
  3054. } break;
  3055. default: {
  3056. }
  3057. }
  3058. r_valid = false;
  3059. return Variant();
  3060. }
  3061. Variant Variant::duplicate(bool deep) const {
  3062. switch (type) {
  3063. case OBJECT: {
  3064. /* breaks stuff :(
  3065. if (deep && !_get_obj().ref.is_null()) {
  3066. Ref<Resource> resource = _get_obj().ref;
  3067. if (resource.is_valid()) {
  3068. return resource->duplicate(true);
  3069. }
  3070. }
  3071. */
  3072. return *this;
  3073. } break;
  3074. case DICTIONARY:
  3075. return operator Dictionary().duplicate(deep);
  3076. case ARRAY:
  3077. return operator Array().duplicate(deep);
  3078. default:
  3079. return *this;
  3080. }
  3081. }
  3082. void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst) {
  3083. if (a.type != b.type) {
  3084. if (a.is_num() && b.is_num()) {
  3085. real_t va = a;
  3086. real_t vb = b;
  3087. r_dst = va + vb * c;
  3088. } else {
  3089. r_dst = a;
  3090. }
  3091. return;
  3092. }
  3093. switch (a.type) {
  3094. case NIL: {
  3095. r_dst = Variant();
  3096. }
  3097. return;
  3098. case INT: {
  3099. int64_t va = a._data._int;
  3100. int64_t vb = b._data._int;
  3101. r_dst = int(va + vb * c + 0.5);
  3102. }
  3103. return;
  3104. case REAL: {
  3105. double ra = a._data._real;
  3106. double rb = b._data._real;
  3107. r_dst = ra + rb * c;
  3108. }
  3109. return;
  3110. case VECTOR2: {
  3111. r_dst = *reinterpret_cast<const Vector2 *>(a._data._mem) + *reinterpret_cast<const Vector2 *>(b._data._mem) * c;
  3112. }
  3113. return;
  3114. case RECT2: {
  3115. const Rect2 *ra = reinterpret_cast<const Rect2 *>(a._data._mem);
  3116. const Rect2 *rb = reinterpret_cast<const Rect2 *>(b._data._mem);
  3117. r_dst = Rect2(ra->position + rb->position * c, ra->size + rb->size * c);
  3118. }
  3119. return;
  3120. case VECTOR3: {
  3121. r_dst = *reinterpret_cast<const Vector3 *>(a._data._mem) + *reinterpret_cast<const Vector3 *>(b._data._mem) * c;
  3122. }
  3123. return;
  3124. case AABB: {
  3125. const ::AABB *ra = reinterpret_cast<const ::AABB *>(a._data._mem);
  3126. const ::AABB *rb = reinterpret_cast<const ::AABB *>(b._data._mem);
  3127. r_dst = ::AABB(ra->position + rb->position * c, ra->size + rb->size * c);
  3128. }
  3129. return;
  3130. case QUAT: {
  3131. Quat empty_rot;
  3132. const Quat *qa = reinterpret_cast<const Quat *>(a._data._mem);
  3133. const Quat *qb = reinterpret_cast<const Quat *>(b._data._mem);
  3134. r_dst = *qa * empty_rot.slerp(*qb, c);
  3135. }
  3136. return;
  3137. case COLOR: {
  3138. const Color *ca = reinterpret_cast<const Color *>(a._data._mem);
  3139. const Color *cb = reinterpret_cast<const Color *>(b._data._mem);
  3140. float new_r = ca->r + cb->r * c;
  3141. float new_g = ca->g + cb->g * c;
  3142. float new_b = ca->b + cb->b * c;
  3143. float new_a = ca->a + cb->a * c;
  3144. new_r = new_r > 1.0 ? 1.0 : new_r;
  3145. new_g = new_g > 1.0 ? 1.0 : new_g;
  3146. new_b = new_b > 1.0 ? 1.0 : new_b;
  3147. new_a = new_a > 1.0 ? 1.0 : new_a;
  3148. r_dst = Color(new_r, new_g, new_b, new_a);
  3149. }
  3150. return;
  3151. default: {
  3152. r_dst = c < 0.5 ? a : b;
  3153. }
  3154. return;
  3155. }
  3156. }
  3157. void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst) {
  3158. if (a.type != b.type) {
  3159. if (a.is_num() && b.is_num()) {
  3160. //not as efficient but..
  3161. real_t va = a;
  3162. real_t vb = b;
  3163. r_dst = va + (vb - va) * c;
  3164. } else {
  3165. r_dst = a;
  3166. }
  3167. return;
  3168. }
  3169. switch (a.type) {
  3170. case NIL: {
  3171. r_dst = Variant();
  3172. }
  3173. return;
  3174. case BOOL: {
  3175. r_dst = a;
  3176. }
  3177. return;
  3178. case INT: {
  3179. int64_t va = a._data._int;
  3180. int64_t vb = b._data._int;
  3181. r_dst = int(va + (vb - va) * c);
  3182. }
  3183. return;
  3184. case REAL: {
  3185. real_t va = a._data._real;
  3186. real_t vb = b._data._real;
  3187. r_dst = va + (vb - va) * c;
  3188. }
  3189. return;
  3190. case STRING: {
  3191. //this is pretty funny and bizarre, but artists like to use it for typewritter effects
  3192. String sa = *reinterpret_cast<const String *>(a._data._mem);
  3193. String sb = *reinterpret_cast<const String *>(b._data._mem);
  3194. String dst;
  3195. int sa_len = sa.length();
  3196. int sb_len = sb.length();
  3197. int csize = sa_len + (sb_len - sa_len) * c;
  3198. if (csize == 0) {
  3199. r_dst = "";
  3200. return;
  3201. }
  3202. dst.resize(csize + 1);
  3203. dst[csize] = 0;
  3204. int split = csize / 2;
  3205. for (int i = 0; i < csize; i++) {
  3206. CharType chr = ' ';
  3207. if (i < split) {
  3208. if (i < sa.length()) {
  3209. chr = sa[i];
  3210. } else if (i < sb.length()) {
  3211. chr = sb[i];
  3212. }
  3213. } else {
  3214. if (i < sb.length()) {
  3215. chr = sb[i];
  3216. } else if (i < sa.length()) {
  3217. chr = sa[i];
  3218. }
  3219. }
  3220. dst[i] = chr;
  3221. }
  3222. r_dst = dst;
  3223. }
  3224. return;
  3225. case VECTOR2: {
  3226. r_dst = reinterpret_cast<const Vector2 *>(a._data._mem)->linear_interpolate(*reinterpret_cast<const Vector2 *>(b._data._mem), c);
  3227. }
  3228. return;
  3229. case RECT2: {
  3230. r_dst = Rect2(reinterpret_cast<const Rect2 *>(a._data._mem)->position.linear_interpolate(reinterpret_cast<const Rect2 *>(b._data._mem)->position, c), reinterpret_cast<const Rect2 *>(a._data._mem)->size.linear_interpolate(reinterpret_cast<const Rect2 *>(b._data._mem)->size, c));
  3231. }
  3232. return;
  3233. case VECTOR3: {
  3234. r_dst = reinterpret_cast<const Vector3 *>(a._data._mem)->linear_interpolate(*reinterpret_cast<const Vector3 *>(b._data._mem), c);
  3235. }
  3236. return;
  3237. case TRANSFORM2D: {
  3238. r_dst = a._data._transform2d->interpolate_with(*b._data._transform2d, c);
  3239. }
  3240. return;
  3241. case PLANE: {
  3242. r_dst = a;
  3243. }
  3244. return;
  3245. case QUAT: {
  3246. r_dst = reinterpret_cast<const Quat *>(a._data._mem)->slerp(*reinterpret_cast<const Quat *>(b._data._mem), c);
  3247. }
  3248. return;
  3249. case AABB: {
  3250. r_dst = ::AABB(a._data._aabb->position.linear_interpolate(b._data._aabb->position, c), a._data._aabb->size.linear_interpolate(b._data._aabb->size, c));
  3251. }
  3252. return;
  3253. case BASIS: {
  3254. r_dst = Transform(*a._data._basis).interpolate_with(Transform(*b._data._basis), c).basis;
  3255. }
  3256. return;
  3257. case TRANSFORM: {
  3258. r_dst = a._data._transform->interpolate_with(*b._data._transform, c);
  3259. }
  3260. return;
  3261. case COLOR: {
  3262. r_dst = reinterpret_cast<const Color *>(a._data._mem)->linear_interpolate(*reinterpret_cast<const Color *>(b._data._mem), c);
  3263. }
  3264. return;
  3265. case NODE_PATH: {
  3266. r_dst = a;
  3267. }
  3268. return;
  3269. case _RID: {
  3270. r_dst = a;
  3271. }
  3272. return;
  3273. case OBJECT: {
  3274. r_dst = a;
  3275. }
  3276. return;
  3277. case DICTIONARY: {
  3278. }
  3279. return;
  3280. case ARRAY: {
  3281. r_dst = a;
  3282. }
  3283. return;
  3284. case POOL_BYTE_ARRAY: {
  3285. r_dst = a;
  3286. }
  3287. return;
  3288. case POOL_INT_ARRAY: {
  3289. const PoolVector<int> *arr_a = reinterpret_cast<const PoolVector<int> *>(a._data._mem);
  3290. const PoolVector<int> *arr_b = reinterpret_cast<const PoolVector<int> *>(b._data._mem);
  3291. int sz = arr_a->size();
  3292. if (sz == 0 || arr_b->size() != sz) {
  3293. r_dst = a;
  3294. } else {
  3295. PoolVector<int> v;
  3296. v.resize(sz);
  3297. {
  3298. PoolVector<int>::Write vw = v.write();
  3299. PoolVector<int>::Read ar = arr_a->read();
  3300. PoolVector<int>::Read br = arr_b->read();
  3301. Variant va;
  3302. for (int i = 0; i < sz; i++) {
  3303. Variant::interpolate(ar[i], br[i], c, va);
  3304. vw[i] = va;
  3305. }
  3306. }
  3307. r_dst = v;
  3308. }
  3309. }
  3310. return;
  3311. case POOL_REAL_ARRAY: {
  3312. const PoolVector<real_t> *arr_a = reinterpret_cast<const PoolVector<real_t> *>(a._data._mem);
  3313. const PoolVector<real_t> *arr_b = reinterpret_cast<const PoolVector<real_t> *>(b._data._mem);
  3314. int sz = arr_a->size();
  3315. if (sz == 0 || arr_b->size() != sz) {
  3316. r_dst = a;
  3317. } else {
  3318. PoolVector<real_t> v;
  3319. v.resize(sz);
  3320. {
  3321. PoolVector<real_t>::Write vw = v.write();
  3322. PoolVector<real_t>::Read ar = arr_a->read();
  3323. PoolVector<real_t>::Read br = arr_b->read();
  3324. Variant va;
  3325. for (int i = 0; i < sz; i++) {
  3326. Variant::interpolate(ar[i], br[i], c, va);
  3327. vw[i] = va;
  3328. }
  3329. }
  3330. r_dst = v;
  3331. }
  3332. }
  3333. return;
  3334. case POOL_STRING_ARRAY: {
  3335. r_dst = a;
  3336. }
  3337. return;
  3338. case POOL_VECTOR2_ARRAY: {
  3339. const PoolVector<Vector2> *arr_a = reinterpret_cast<const PoolVector<Vector2> *>(a._data._mem);
  3340. const PoolVector<Vector2> *arr_b = reinterpret_cast<const PoolVector<Vector2> *>(b._data._mem);
  3341. int sz = arr_a->size();
  3342. if (sz == 0 || arr_b->size() != sz) {
  3343. r_dst = a;
  3344. } else {
  3345. PoolVector<Vector2> v;
  3346. v.resize(sz);
  3347. {
  3348. PoolVector<Vector2>::Write vw = v.write();
  3349. PoolVector<Vector2>::Read ar = arr_a->read();
  3350. PoolVector<Vector2>::Read br = arr_b->read();
  3351. for (int i = 0; i < sz; i++) {
  3352. vw[i] = ar[i].linear_interpolate(br[i], c);
  3353. }
  3354. }
  3355. r_dst = v;
  3356. }
  3357. }
  3358. return;
  3359. case POOL_VECTOR3_ARRAY: {
  3360. const PoolVector<Vector3> *arr_a = reinterpret_cast<const PoolVector<Vector3> *>(a._data._mem);
  3361. const PoolVector<Vector3> *arr_b = reinterpret_cast<const PoolVector<Vector3> *>(b._data._mem);
  3362. int sz = arr_a->size();
  3363. if (sz == 0 || arr_b->size() != sz) {
  3364. r_dst = a;
  3365. } else {
  3366. PoolVector<Vector3> v;
  3367. v.resize(sz);
  3368. {
  3369. PoolVector<Vector3>::Write vw = v.write();
  3370. PoolVector<Vector3>::Read ar = arr_a->read();
  3371. PoolVector<Vector3>::Read br = arr_b->read();
  3372. for (int i = 0; i < sz; i++) {
  3373. vw[i] = ar[i].linear_interpolate(br[i], c);
  3374. }
  3375. }
  3376. r_dst = v;
  3377. }
  3378. }
  3379. return;
  3380. case POOL_COLOR_ARRAY: {
  3381. const PoolVector<Color> *arr_a = reinterpret_cast<const PoolVector<Color> *>(a._data._mem);
  3382. const PoolVector<Color> *arr_b = reinterpret_cast<const PoolVector<Color> *>(b._data._mem);
  3383. int sz = arr_a->size();
  3384. if (sz == 0 || arr_b->size() != sz) {
  3385. r_dst = a;
  3386. } else {
  3387. PoolVector<Color> v;
  3388. v.resize(sz);
  3389. {
  3390. PoolVector<Color>::Write vw = v.write();
  3391. PoolVector<Color>::Read ar = arr_a->read();
  3392. PoolVector<Color>::Read br = arr_b->read();
  3393. for (int i = 0; i < sz; i++) {
  3394. vw[i] = ar[i].linear_interpolate(br[i], c);
  3395. }
  3396. }
  3397. r_dst = v;
  3398. }
  3399. }
  3400. return;
  3401. default: {
  3402. r_dst = a;
  3403. }
  3404. }
  3405. }
  3406. static const char *_op_names[Variant::OP_MAX] = {
  3407. "==",
  3408. "!=",
  3409. "<",
  3410. "<=",
  3411. ">",
  3412. ">=",
  3413. "+",
  3414. "-",
  3415. "*",
  3416. "/",
  3417. "- (negation)",
  3418. "+ (positive)",
  3419. "%",
  3420. "+ (concatenation)",
  3421. "<<",
  3422. ">>",
  3423. "&",
  3424. "|",
  3425. "^",
  3426. "~",
  3427. "and",
  3428. "or",
  3429. "xor",
  3430. "not",
  3431. "in"
  3432. };
  3433. String Variant::get_operator_name(Operator p_op) {
  3434. ERR_FAIL_INDEX_V(p_op, OP_MAX, "");
  3435. return _op_names[p_op];
  3436. }