variant_op.cpp 81 KB

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