variant_op.cpp 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764
  1. /*************************************************************************/
  2. /* variant_op.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "variant.h"
  31. #include "core/core_string_names.h"
  32. #include "core/object.h"
  33. #include "core/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, AABB) \
  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, AABB), \
  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 uninitialized 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, !=, !=, false, true, true) \
  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, AABB, ==, _aabb);
  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);
  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(true);
  475. }
  476. }
  477. _RETURN(false);
  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, AABB, !=, _aabb);
  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, AABB)
  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, AABB)
  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, AABB)
  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, AABB)
  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, AABB)
  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, AABB)
  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, AABB)
  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, AABB)
  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, AABB)
  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, AABB)
  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, AABB)
  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 AABB: {
  1229. if (p_value.type == Variant::VECTOR3) {
  1230. ::AABB *v = _data._aabb;
  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(), v->a);
  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(), v->a);
  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, v->a);
  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(), v->a);
  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(), v->a);
  1337. valid = true;
  1338. } else if (p_index == CoreStringNames::singleton->v) {
  1339. v->set_hsv(v->get_h(), v->get_s(), p_value._data._real, v->a);
  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 AABB: {
  1433. const ::AABB *v = _data._aabb;
  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(Math::round(v->r * 255.0));
  1473. } else if (p_index == CoreStringNames::singleton->g8) {
  1474. return int(Math::round(v->g * 255.0));
  1475. } else if (p_index == CoreStringNames::singleton->b8) {
  1476. return int(Math::round(v->b * 255.0));
  1477. } else if (p_index == CoreStringNames::singleton->a8) {
  1478. return int(Math::round(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 AABB: {
  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. ::AABB *v = _data._aabb;
  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(), v->a);
  1861. return;
  1862. } else if (*str == "s") {
  1863. valid = true;
  1864. v->set_hsv(v->get_h(), p_value, v->get_v(), v->a);
  1865. return;
  1866. } else if (*str == "v") {
  1867. valid = true;
  1868. v->set_hsv(v->get_h(), v->get_s(), p_value, v->a);
  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 AABB: {
  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 ::AABB *v = _data._aabb;
  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 AABB: {
  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. Variant Variant::duplicate(bool deep) const {
  2961. switch (type) {
  2962. case OBJECT: {
  2963. /* breaks stuff :(
  2964. if (deep && !_get_obj().ref.is_null()) {
  2965. Ref<Resource> resource = _get_obj().ref;
  2966. if (resource.is_valid()) {
  2967. return resource->duplicate(true);
  2968. }
  2969. }
  2970. */
  2971. return *this;
  2972. } break;
  2973. case DICTIONARY:
  2974. return operator Dictionary().duplicate(deep);
  2975. case ARRAY:
  2976. return operator Array().duplicate(deep);
  2977. default:
  2978. return *this;
  2979. }
  2980. }
  2981. void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst) {
  2982. if (a.type != b.type) {
  2983. if (a.is_num() && b.is_num()) {
  2984. real_t va = a;
  2985. real_t vb = b;
  2986. r_dst = va + vb * c;
  2987. } else {
  2988. r_dst = a;
  2989. }
  2990. return;
  2991. }
  2992. switch (a.type) {
  2993. case NIL: {
  2994. r_dst = Variant();
  2995. }
  2996. return;
  2997. case INT: {
  2998. int64_t va = a._data._int;
  2999. int64_t vb = b._data._int;
  3000. r_dst = int(va + vb * c + 0.5);
  3001. }
  3002. return;
  3003. case REAL: {
  3004. double ra = a._data._real;
  3005. double rb = b._data._real;
  3006. r_dst = ra + rb * c;
  3007. }
  3008. return;
  3009. case VECTOR2: {
  3010. r_dst = *reinterpret_cast<const Vector2 *>(a._data._mem) + *reinterpret_cast<const Vector2 *>(b._data._mem) * c;
  3011. }
  3012. return;
  3013. case RECT2: {
  3014. const Rect2 *ra = reinterpret_cast<const Rect2 *>(a._data._mem);
  3015. const Rect2 *rb = reinterpret_cast<const Rect2 *>(b._data._mem);
  3016. r_dst = Rect2(ra->position + rb->position * c, ra->size + rb->size * c);
  3017. }
  3018. return;
  3019. case VECTOR3: {
  3020. r_dst = *reinterpret_cast<const Vector3 *>(a._data._mem) + *reinterpret_cast<const Vector3 *>(b._data._mem) * c;
  3021. }
  3022. return;
  3023. case AABB: {
  3024. const ::AABB *ra = reinterpret_cast<const ::AABB *>(a._data._mem);
  3025. const ::AABB *rb = reinterpret_cast<const ::AABB *>(b._data._mem);
  3026. r_dst = ::AABB(ra->position + rb->position * c, ra->size + rb->size * c);
  3027. }
  3028. return;
  3029. case QUAT: {
  3030. Quat empty_rot;
  3031. const Quat *qa = reinterpret_cast<const Quat *>(a._data._mem);
  3032. const Quat *qb = reinterpret_cast<const Quat *>(b._data._mem);
  3033. r_dst = *qa * empty_rot.slerp(*qb, c);
  3034. }
  3035. return;
  3036. case COLOR: {
  3037. const Color *ca = reinterpret_cast<const Color *>(a._data._mem);
  3038. const Color *cb = reinterpret_cast<const Color *>(b._data._mem);
  3039. float r = ca->r + cb->r * c;
  3040. float g = ca->g + cb->g * c;
  3041. float b = ca->b + cb->b * c;
  3042. float a = ca->a + cb->a * c;
  3043. r = r > 1.0 ? 1.0 : r;
  3044. g = g > 1.0 ? 1.0 : g;
  3045. b = b > 1.0 ? 1.0 : b;
  3046. a = a > 1.0 ? 1.0 : a;
  3047. r_dst = Color(r, g, b, a);
  3048. }
  3049. return;
  3050. default: {
  3051. r_dst = c < 0.5 ? a : b;
  3052. }
  3053. return;
  3054. }
  3055. }
  3056. void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst) {
  3057. if (a.type != b.type) {
  3058. if (a.is_num() && b.is_num()) {
  3059. //not as efficient but..
  3060. real_t va = a;
  3061. real_t vb = b;
  3062. r_dst = va + (vb - va) * c;
  3063. } else {
  3064. r_dst = a;
  3065. }
  3066. return;
  3067. }
  3068. switch (a.type) {
  3069. case NIL: {
  3070. r_dst = Variant();
  3071. }
  3072. return;
  3073. case BOOL: {
  3074. r_dst = a;
  3075. }
  3076. return;
  3077. case INT: {
  3078. int64_t va = a._data._int;
  3079. int64_t vb = b._data._int;
  3080. r_dst = int(va + (vb - va) * c);
  3081. }
  3082. return;
  3083. case REAL: {
  3084. real_t va = a._data._real;
  3085. real_t vb = b._data._real;
  3086. r_dst = va + (vb - va) * c;
  3087. }
  3088. return;
  3089. case STRING: {
  3090. //this is pretty funny and bizarre, but artists like to use it for typewritter effects
  3091. String sa = *reinterpret_cast<const String *>(a._data._mem);
  3092. String sb = *reinterpret_cast<const String *>(b._data._mem);
  3093. String dst;
  3094. int sa_len = sa.length();
  3095. int sb_len = sb.length();
  3096. int csize = sa_len + (sb_len - sa_len) * c;
  3097. if (csize == 0) {
  3098. r_dst = "";
  3099. return;
  3100. }
  3101. dst.resize(csize + 1);
  3102. dst[csize] = 0;
  3103. int split = csize / 2;
  3104. for (int i = 0; i < csize; i++) {
  3105. CharType chr = ' ';
  3106. if (i < split) {
  3107. if (i < sa.length())
  3108. chr = sa[i];
  3109. else if (i < sb.length())
  3110. chr = sb[i];
  3111. } else {
  3112. if (i < sb.length())
  3113. chr = sb[i];
  3114. else if (i < sa.length())
  3115. chr = sa[i];
  3116. }
  3117. dst[i] = chr;
  3118. }
  3119. r_dst = dst;
  3120. }
  3121. return;
  3122. case VECTOR2: {
  3123. r_dst = reinterpret_cast<const Vector2 *>(a._data._mem)->linear_interpolate(*reinterpret_cast<const Vector2 *>(b._data._mem), c);
  3124. }
  3125. return;
  3126. case RECT2: {
  3127. 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));
  3128. }
  3129. return;
  3130. case VECTOR3: {
  3131. r_dst = reinterpret_cast<const Vector3 *>(a._data._mem)->linear_interpolate(*reinterpret_cast<const Vector3 *>(b._data._mem), c);
  3132. }
  3133. return;
  3134. case TRANSFORM2D: {
  3135. r_dst = a._data._transform2d->interpolate_with(*b._data._transform2d, c);
  3136. }
  3137. return;
  3138. case PLANE: {
  3139. r_dst = a;
  3140. }
  3141. return;
  3142. case QUAT: {
  3143. r_dst = reinterpret_cast<const Quat *>(a._data._mem)->slerp(*reinterpret_cast<const Quat *>(b._data._mem), c);
  3144. }
  3145. return;
  3146. case AABB: {
  3147. r_dst = ::AABB(a._data._aabb->position.linear_interpolate(b._data._aabb->position, c), a._data._aabb->size.linear_interpolate(b._data._aabb->size, c));
  3148. }
  3149. return;
  3150. case BASIS: {
  3151. r_dst = Transform(*a._data._basis).interpolate_with(Transform(*b._data._basis), c).basis;
  3152. }
  3153. return;
  3154. case TRANSFORM: {
  3155. r_dst = a._data._transform->interpolate_with(*b._data._transform, c);
  3156. }
  3157. return;
  3158. case COLOR: {
  3159. r_dst = reinterpret_cast<const Color *>(a._data._mem)->linear_interpolate(*reinterpret_cast<const Color *>(b._data._mem), c);
  3160. }
  3161. return;
  3162. case NODE_PATH: {
  3163. r_dst = a;
  3164. }
  3165. return;
  3166. case _RID: {
  3167. r_dst = a;
  3168. }
  3169. return;
  3170. case OBJECT: {
  3171. r_dst = a;
  3172. }
  3173. return;
  3174. case DICTIONARY: {
  3175. }
  3176. return;
  3177. case ARRAY: {
  3178. r_dst = a;
  3179. }
  3180. return;
  3181. case POOL_BYTE_ARRAY: {
  3182. r_dst = a;
  3183. }
  3184. return;
  3185. case POOL_INT_ARRAY: {
  3186. r_dst = a;
  3187. }
  3188. return;
  3189. case POOL_REAL_ARRAY: {
  3190. r_dst = a;
  3191. }
  3192. return;
  3193. case POOL_STRING_ARRAY: {
  3194. r_dst = a;
  3195. }
  3196. return;
  3197. case POOL_VECTOR2_ARRAY: {
  3198. const PoolVector<Vector2> *arr_a = reinterpret_cast<const PoolVector<Vector2> *>(a._data._mem);
  3199. const PoolVector<Vector2> *arr_b = reinterpret_cast<const PoolVector<Vector2> *>(b._data._mem);
  3200. int sz = arr_a->size();
  3201. if (sz == 0 || arr_b->size() != sz) {
  3202. r_dst = a;
  3203. } else {
  3204. PoolVector<Vector2> v;
  3205. v.resize(sz);
  3206. {
  3207. PoolVector<Vector2>::Write vw = v.write();
  3208. PoolVector<Vector2>::Read ar = arr_a->read();
  3209. PoolVector<Vector2>::Read br = arr_b->read();
  3210. for (int i = 0; i < sz; i++) {
  3211. vw[i] = ar[i].linear_interpolate(br[i], c);
  3212. }
  3213. }
  3214. r_dst = v;
  3215. }
  3216. }
  3217. return;
  3218. case POOL_VECTOR3_ARRAY: {
  3219. const PoolVector<Vector3> *arr_a = reinterpret_cast<const PoolVector<Vector3> *>(a._data._mem);
  3220. const PoolVector<Vector3> *arr_b = reinterpret_cast<const PoolVector<Vector3> *>(b._data._mem);
  3221. int sz = arr_a->size();
  3222. if (sz == 0 || arr_b->size() != sz) {
  3223. r_dst = a;
  3224. } else {
  3225. PoolVector<Vector3> v;
  3226. v.resize(sz);
  3227. {
  3228. PoolVector<Vector3>::Write vw = v.write();
  3229. PoolVector<Vector3>::Read ar = arr_a->read();
  3230. PoolVector<Vector3>::Read br = arr_b->read();
  3231. for (int i = 0; i < sz; i++) {
  3232. vw[i] = ar[i].linear_interpolate(br[i], c);
  3233. }
  3234. }
  3235. r_dst = v;
  3236. }
  3237. }
  3238. return;
  3239. case POOL_COLOR_ARRAY: {
  3240. r_dst = a;
  3241. }
  3242. return;
  3243. default: {
  3244. r_dst = a;
  3245. }
  3246. }
  3247. }
  3248. static const char *_op_names[Variant::OP_MAX] = {
  3249. "==",
  3250. "!=",
  3251. "<",
  3252. "<=",
  3253. ">",
  3254. ">=",
  3255. "+",
  3256. "-",
  3257. "*",
  3258. "/",
  3259. "- (negation)",
  3260. "+ (positive)",
  3261. "%",
  3262. "+ (concatenation)",
  3263. "<<",
  3264. ">>",
  3265. "&",
  3266. "|",
  3267. "^",
  3268. "~",
  3269. "and",
  3270. "or",
  3271. "xor",
  3272. "not",
  3273. "in"
  3274. };
  3275. String Variant::get_operator_name(Operator p_op) {
  3276. ERR_FAIL_INDEX_V(p_op, OP_MAX, "");
  3277. return _op_names[p_op];
  3278. }