variant_op.cpp 96 KB

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