variant_op.cpp 113 KB

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