variant_op.cpp 83 KB

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