2
0

variant_op.cpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046
  1. /*************************************************************************/
  2. /* variant_op.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-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" || *str == "width") {
  942. valid = true;
  943. v->x = p_value;
  944. return;
  945. } else if (*str == "y" || *str == "height") {
  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 == "o") {
  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" || *str == "width") {
  1337. valid = true;
  1338. return v->x;
  1339. } else if (*str == "y" || *str == "height") {
  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 == "o") {
  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. p_list->push_back(PropertyInfo(Variant::REAL, "width"));
  1784. p_list->push_back(PropertyInfo(Variant::REAL, "height"));
  1785. } break; // 5
  1786. case RECT2: {
  1787. p_list->push_back(PropertyInfo(Variant::VECTOR2, "position"));
  1788. p_list->push_back(PropertyInfo(Variant::VECTOR2, "size"));
  1789. p_list->push_back(PropertyInfo(Variant::VECTOR2, "end"));
  1790. } break;
  1791. case VECTOR3: {
  1792. p_list->push_back(PropertyInfo(Variant::REAL, "x"));
  1793. p_list->push_back(PropertyInfo(Variant::REAL, "y"));
  1794. p_list->push_back(PropertyInfo(Variant::REAL, "z"));
  1795. } break;
  1796. case TRANSFORM2D: {
  1797. p_list->push_back(PropertyInfo(Variant::VECTOR2, "x"));
  1798. p_list->push_back(PropertyInfo(Variant::VECTOR2, "y"));
  1799. p_list->push_back(PropertyInfo(Variant::VECTOR2, "o"));
  1800. } break;
  1801. case PLANE: {
  1802. p_list->push_back(PropertyInfo(Variant::VECTOR3, "normal"));
  1803. p_list->push_back(PropertyInfo(Variant::REAL, "x"));
  1804. p_list->push_back(PropertyInfo(Variant::REAL, "y"));
  1805. p_list->push_back(PropertyInfo(Variant::REAL, "z"));
  1806. p_list->push_back(PropertyInfo(Variant::REAL, "d"));
  1807. } break;
  1808. case QUAT: {
  1809. p_list->push_back(PropertyInfo(Variant::REAL, "x"));
  1810. p_list->push_back(PropertyInfo(Variant::REAL, "y"));
  1811. p_list->push_back(PropertyInfo(Variant::REAL, "z"));
  1812. p_list->push_back(PropertyInfo(Variant::REAL, "w"));
  1813. } break; // 10
  1814. case RECT3: {
  1815. p_list->push_back(PropertyInfo(Variant::VECTOR3, "position"));
  1816. p_list->push_back(PropertyInfo(Variant::VECTOR3, "size"));
  1817. p_list->push_back(PropertyInfo(Variant::VECTOR3, "end"));
  1818. } break;
  1819. case BASIS: {
  1820. p_list->push_back(PropertyInfo(Variant::VECTOR3, "x"));
  1821. p_list->push_back(PropertyInfo(Variant::VECTOR3, "y"));
  1822. p_list->push_back(PropertyInfo(Variant::VECTOR3, "z"));
  1823. } break;
  1824. case TRANSFORM: {
  1825. p_list->push_back(PropertyInfo(Variant::BASIS, "basis"));
  1826. p_list->push_back(PropertyInfo(Variant::VECTOR3, "origin"));
  1827. } break;
  1828. case COLOR: {
  1829. p_list->push_back(PropertyInfo(Variant::REAL, "r"));
  1830. p_list->push_back(PropertyInfo(Variant::REAL, "g"));
  1831. p_list->push_back(PropertyInfo(Variant::REAL, "b"));
  1832. p_list->push_back(PropertyInfo(Variant::REAL, "a"));
  1833. p_list->push_back(PropertyInfo(Variant::REAL, "h"));
  1834. p_list->push_back(PropertyInfo(Variant::REAL, "s"));
  1835. p_list->push_back(PropertyInfo(Variant::REAL, "v"));
  1836. p_list->push_back(PropertyInfo(Variant::INT, "r8"));
  1837. p_list->push_back(PropertyInfo(Variant::INT, "g8"));
  1838. p_list->push_back(PropertyInfo(Variant::INT, "b8"));
  1839. p_list->push_back(PropertyInfo(Variant::INT, "a8"));
  1840. } break;
  1841. case NODE_PATH: {
  1842. } break; // 15
  1843. case _RID: {
  1844. } break;
  1845. case OBJECT: {
  1846. Object *obj = _get_obj().obj;
  1847. if (obj) {
  1848. #ifdef DEBUG_ENABLED
  1849. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null()) {
  1850. //only if debugging!
  1851. if (!ObjectDB::instance_validate(obj)) {
  1852. WARN_PRINT("Attempted get_property list on stray pointer.");
  1853. return;
  1854. }
  1855. }
  1856. #endif
  1857. obj->get_property_list(p_list);
  1858. }
  1859. } break;
  1860. case DICTIONARY: {
  1861. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  1862. List<Variant> keys;
  1863. dic->get_key_list(&keys);
  1864. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1865. if (E->get().get_type() == Variant::STRING) {
  1866. p_list->push_back(PropertyInfo(Variant::STRING, E->get()));
  1867. }
  1868. }
  1869. } break;
  1870. case ARRAY: // 20
  1871. case POOL_BYTE_ARRAY:
  1872. case POOL_INT_ARRAY:
  1873. case POOL_REAL_ARRAY:
  1874. case POOL_STRING_ARRAY:
  1875. case POOL_VECTOR2_ARRAY: // 25
  1876. case POOL_VECTOR3_ARRAY:
  1877. case POOL_COLOR_ARRAY: {
  1878. //nothing
  1879. } break;
  1880. default: {}
  1881. }
  1882. }
  1883. bool Variant::iter_init(Variant &r_iter, bool &valid) const {
  1884. valid = true;
  1885. switch (type) {
  1886. case INT: {
  1887. r_iter = 0;
  1888. return _data._int > 0;
  1889. } break;
  1890. case REAL: {
  1891. r_iter = 0;
  1892. return _data._real > 0.0;
  1893. } break;
  1894. case VECTOR2: {
  1895. int64_t from = reinterpret_cast<const Vector2 *>(_data._mem)->x;
  1896. int64_t to = reinterpret_cast<const Vector2 *>(_data._mem)->y;
  1897. r_iter = from;
  1898. return from < to;
  1899. } break;
  1900. case VECTOR3: {
  1901. int64_t from = reinterpret_cast<const Vector3 *>(_data._mem)->x;
  1902. int64_t to = reinterpret_cast<const Vector3 *>(_data._mem)->y;
  1903. int64_t step = reinterpret_cast<const Vector3 *>(_data._mem)->z;
  1904. r_iter = from;
  1905. if (from == to) {
  1906. return false;
  1907. } else if (from < to) {
  1908. return step > 0;
  1909. } else {
  1910. return step < 0;
  1911. }
  1912. //return true;
  1913. } break;
  1914. case OBJECT: {
  1915. #ifdef DEBUG_ENABLED
  1916. if (!_get_obj().obj) {
  1917. valid = false;
  1918. return false;
  1919. }
  1920. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null() && !ObjectDB::instance_validate(_get_obj().obj)) {
  1921. valid = false;
  1922. return false;
  1923. }
  1924. #endif
  1925. Variant::CallError ce;
  1926. ce.error = Variant::CallError::CALL_OK;
  1927. Array ref;
  1928. ref.push_back(r_iter);
  1929. Variant vref = ref;
  1930. const Variant *refp[] = { &vref };
  1931. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->_iter_init, refp, 1, ce);
  1932. if (ref.size() != 1 || ce.error != Variant::CallError::CALL_OK) {
  1933. valid = false;
  1934. return false;
  1935. }
  1936. r_iter = ref[0];
  1937. return ret;
  1938. } break;
  1939. case STRING: {
  1940. const String *str = reinterpret_cast<const String *>(_data._mem);
  1941. if (str->empty())
  1942. return false;
  1943. r_iter = 0;
  1944. return true;
  1945. } break;
  1946. case DICTIONARY: {
  1947. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  1948. if (dic->empty())
  1949. return false;
  1950. const Variant *next = dic->next(NULL);
  1951. r_iter = *next;
  1952. return true;
  1953. } break;
  1954. case ARRAY: {
  1955. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  1956. if (arr->empty())
  1957. return false;
  1958. r_iter = 0;
  1959. return true;
  1960. } break;
  1961. case POOL_BYTE_ARRAY: {
  1962. const PoolVector<uint8_t> *arr = reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  1963. if (arr->size() == 0)
  1964. return false;
  1965. r_iter = 0;
  1966. return true;
  1967. } break;
  1968. case POOL_INT_ARRAY: {
  1969. const PoolVector<int> *arr = reinterpret_cast<const PoolVector<int> *>(_data._mem);
  1970. if (arr->size() == 0)
  1971. return false;
  1972. r_iter = 0;
  1973. return true;
  1974. } break;
  1975. case POOL_REAL_ARRAY: {
  1976. const PoolVector<real_t> *arr = reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  1977. if (arr->size() == 0)
  1978. return false;
  1979. r_iter = 0;
  1980. return true;
  1981. } break;
  1982. case POOL_STRING_ARRAY: {
  1983. const PoolVector<String> *arr = reinterpret_cast<const PoolVector<String> *>(_data._mem);
  1984. if (arr->size() == 0)
  1985. return false;
  1986. r_iter = 0;
  1987. return true;
  1988. } break;
  1989. case POOL_VECTOR2_ARRAY: {
  1990. const PoolVector<Vector2> *arr = reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  1991. if (arr->size() == 0)
  1992. return false;
  1993. r_iter = 0;
  1994. return true;
  1995. } break;
  1996. case POOL_VECTOR3_ARRAY: {
  1997. const PoolVector<Vector3> *arr = reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  1998. if (arr->size() == 0)
  1999. return false;
  2000. r_iter = 0;
  2001. return true;
  2002. } break;
  2003. case POOL_COLOR_ARRAY: {
  2004. const PoolVector<Color> *arr = reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2005. if (arr->size() == 0)
  2006. return false;
  2007. r_iter = 0;
  2008. return true;
  2009. } break;
  2010. default: {}
  2011. }
  2012. valid = false;
  2013. return false;
  2014. }
  2015. bool Variant::iter_next(Variant &r_iter, bool &valid) const {
  2016. valid = true;
  2017. switch (type) {
  2018. case INT: {
  2019. int64_t idx = r_iter;
  2020. idx++;
  2021. if (idx >= _data._int)
  2022. return false;
  2023. r_iter = idx;
  2024. return true;
  2025. } break;
  2026. case REAL: {
  2027. int64_t idx = r_iter;
  2028. idx++;
  2029. if (idx >= _data._real)
  2030. return false;
  2031. r_iter = idx;
  2032. return true;
  2033. } break;
  2034. case VECTOR2: {
  2035. int64_t to = reinterpret_cast<const Vector3 *>(_data._mem)->y;
  2036. int64_t idx = r_iter;
  2037. idx++;
  2038. if (idx >= to)
  2039. return false;
  2040. r_iter = idx;
  2041. return true;
  2042. } break;
  2043. case VECTOR3: {
  2044. int64_t to = reinterpret_cast<const Vector3 *>(_data._mem)->y;
  2045. int64_t step = reinterpret_cast<const Vector3 *>(_data._mem)->z;
  2046. int64_t idx = r_iter;
  2047. idx += step;
  2048. if (step < 0 && idx <= to)
  2049. return false;
  2050. if (step > 0 && idx >= to)
  2051. return false;
  2052. r_iter = idx;
  2053. return true;
  2054. } break;
  2055. case OBJECT: {
  2056. #ifdef DEBUG_ENABLED
  2057. if (!_get_obj().obj) {
  2058. valid = false;
  2059. return false;
  2060. }
  2061. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null() && !ObjectDB::instance_validate(_get_obj().obj)) {
  2062. valid = false;
  2063. return false;
  2064. }
  2065. #endif
  2066. Variant::CallError ce;
  2067. ce.error = Variant::CallError::CALL_OK;
  2068. Array ref;
  2069. ref.push_back(r_iter);
  2070. Variant vref = ref;
  2071. const Variant *refp[] = { &vref };
  2072. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->_iter_next, refp, 1, ce);
  2073. if (ref.size() != 1 || ce.error != Variant::CallError::CALL_OK) {
  2074. valid = false;
  2075. return false;
  2076. }
  2077. r_iter = ref[0];
  2078. return ret;
  2079. } break;
  2080. case STRING: {
  2081. const String *str = reinterpret_cast<const String *>(_data._mem);
  2082. int idx = r_iter;
  2083. idx++;
  2084. if (idx >= str->length())
  2085. return false;
  2086. r_iter = idx;
  2087. return true;
  2088. } break;
  2089. case DICTIONARY: {
  2090. const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem);
  2091. const Variant *next = dic->next(&r_iter);
  2092. if (!next)
  2093. return false;
  2094. r_iter = *next;
  2095. return true;
  2096. } break;
  2097. case ARRAY: {
  2098. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  2099. int idx = r_iter;
  2100. idx++;
  2101. if (idx >= arr->size())
  2102. return false;
  2103. r_iter = idx;
  2104. return true;
  2105. } break;
  2106. case POOL_BYTE_ARRAY: {
  2107. const PoolVector<uint8_t> *arr = reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2108. int idx = r_iter;
  2109. idx++;
  2110. if (idx >= arr->size())
  2111. return false;
  2112. r_iter = idx;
  2113. return true;
  2114. } break;
  2115. case POOL_INT_ARRAY: {
  2116. const PoolVector<int> *arr = reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2117. int idx = r_iter;
  2118. idx++;
  2119. if (idx >= arr->size())
  2120. return false;
  2121. r_iter = idx;
  2122. return true;
  2123. } break;
  2124. case POOL_REAL_ARRAY: {
  2125. const PoolVector<real_t> *arr = reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2126. int idx = r_iter;
  2127. idx++;
  2128. if (idx >= arr->size())
  2129. return false;
  2130. r_iter = idx;
  2131. return true;
  2132. } break;
  2133. case POOL_STRING_ARRAY: {
  2134. const PoolVector<String> *arr = reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2135. int idx = r_iter;
  2136. idx++;
  2137. if (idx >= arr->size())
  2138. return false;
  2139. r_iter = idx;
  2140. return true;
  2141. } break;
  2142. case POOL_VECTOR2_ARRAY: {
  2143. const PoolVector<Vector2> *arr = reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2144. int idx = r_iter;
  2145. idx++;
  2146. if (idx >= arr->size())
  2147. return false;
  2148. r_iter = idx;
  2149. return true;
  2150. } break;
  2151. case POOL_VECTOR3_ARRAY: {
  2152. const PoolVector<Vector3> *arr = reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2153. int idx = r_iter;
  2154. idx++;
  2155. if (idx >= arr->size())
  2156. return false;
  2157. r_iter = idx;
  2158. return true;
  2159. } break;
  2160. case POOL_COLOR_ARRAY: {
  2161. const PoolVector<Color> *arr = reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2162. int idx = r_iter;
  2163. idx++;
  2164. if (idx >= arr->size())
  2165. return false;
  2166. r_iter = idx;
  2167. return true;
  2168. } break;
  2169. default: {}
  2170. }
  2171. valid = false;
  2172. return false;
  2173. }
  2174. Variant Variant::iter_get(const Variant &r_iter, bool &r_valid) const {
  2175. r_valid = true;
  2176. switch (type) {
  2177. case INT: {
  2178. return r_iter;
  2179. } break;
  2180. case REAL: {
  2181. return r_iter;
  2182. } break;
  2183. case VECTOR2: {
  2184. return r_iter;
  2185. } break;
  2186. case VECTOR3: {
  2187. return r_iter;
  2188. } break;
  2189. case OBJECT: {
  2190. #ifdef DEBUG_ENABLED
  2191. if (!_get_obj().obj) {
  2192. r_valid = false;
  2193. return Variant();
  2194. }
  2195. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null() && !ObjectDB::instance_validate(_get_obj().obj)) {
  2196. r_valid = false;
  2197. return Variant();
  2198. }
  2199. #endif
  2200. Variant::CallError ce;
  2201. ce.error = Variant::CallError::CALL_OK;
  2202. const Variant *refp[] = { &r_iter };
  2203. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->_iter_get, refp, 1, ce);
  2204. if (ce.error != Variant::CallError::CALL_OK) {
  2205. r_valid = false;
  2206. return Variant();
  2207. }
  2208. //r_iter=ref[0];
  2209. return ret;
  2210. } break;
  2211. case STRING: {
  2212. const String *str = reinterpret_cast<const String *>(_data._mem);
  2213. return str->substr(r_iter, 1);
  2214. } break;
  2215. case DICTIONARY: {
  2216. return r_iter; //iterator is the same as the key
  2217. } break;
  2218. case ARRAY: {
  2219. const Array *arr = reinterpret_cast<const Array *>(_data._mem);
  2220. int idx = r_iter;
  2221. #ifdef DEBUG_ENABLED
  2222. if (idx < 0 || idx >= arr->size()) {
  2223. r_valid = false;
  2224. return Variant();
  2225. }
  2226. #endif
  2227. return arr->get(idx);
  2228. } break;
  2229. case POOL_BYTE_ARRAY: {
  2230. const PoolVector<uint8_t> *arr = reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2231. int idx = r_iter;
  2232. #ifdef DEBUG_ENABLED
  2233. if (idx < 0 || idx >= arr->size()) {
  2234. r_valid = false;
  2235. return Variant();
  2236. }
  2237. #endif
  2238. return arr->get(idx);
  2239. } break;
  2240. case POOL_INT_ARRAY: {
  2241. const PoolVector<int> *arr = reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2242. int idx = r_iter;
  2243. #ifdef DEBUG_ENABLED
  2244. if (idx < 0 || idx >= arr->size()) {
  2245. r_valid = false;
  2246. return Variant();
  2247. }
  2248. #endif
  2249. return arr->get(idx);
  2250. } break;
  2251. case POOL_REAL_ARRAY: {
  2252. const PoolVector<real_t> *arr = reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2253. int idx = r_iter;
  2254. #ifdef DEBUG_ENABLED
  2255. if (idx < 0 || idx >= arr->size()) {
  2256. r_valid = false;
  2257. return Variant();
  2258. }
  2259. #endif
  2260. return arr->get(idx);
  2261. } break;
  2262. case POOL_STRING_ARRAY: {
  2263. const PoolVector<String> *arr = reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2264. int idx = r_iter;
  2265. #ifdef DEBUG_ENABLED
  2266. if (idx < 0 || idx >= arr->size()) {
  2267. r_valid = false;
  2268. return Variant();
  2269. }
  2270. #endif
  2271. return arr->get(idx);
  2272. } break;
  2273. case POOL_VECTOR2_ARRAY: {
  2274. const PoolVector<Vector2> *arr = reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2275. int idx = r_iter;
  2276. #ifdef DEBUG_ENABLED
  2277. if (idx < 0 || idx >= arr->size()) {
  2278. r_valid = false;
  2279. return Variant();
  2280. }
  2281. #endif
  2282. return arr->get(idx);
  2283. } break;
  2284. case POOL_VECTOR3_ARRAY: {
  2285. const PoolVector<Vector3> *arr = reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2286. int idx = r_iter;
  2287. #ifdef DEBUG_ENABLED
  2288. if (idx < 0 || idx >= arr->size()) {
  2289. r_valid = false;
  2290. return Variant();
  2291. }
  2292. #endif
  2293. return arr->get(idx);
  2294. } break;
  2295. case POOL_COLOR_ARRAY: {
  2296. const PoolVector<Color> *arr = reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2297. int idx = r_iter;
  2298. #ifdef DEBUG_ENABLED
  2299. if (idx < 0 || idx >= arr->size()) {
  2300. r_valid = false;
  2301. return Variant();
  2302. }
  2303. #endif
  2304. return arr->get(idx);
  2305. } break;
  2306. default: {}
  2307. }
  2308. r_valid = false;
  2309. return Variant();
  2310. }
  2311. void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst) {
  2312. if (a.type != b.type) {
  2313. if (a.is_num() && b.is_num()) {
  2314. real_t va = a;
  2315. real_t vb = b;
  2316. r_dst = va + vb * c;
  2317. } else {
  2318. r_dst = a;
  2319. }
  2320. return;
  2321. }
  2322. switch (a.type) {
  2323. case NIL: {
  2324. r_dst = Variant();
  2325. }
  2326. return;
  2327. case INT: {
  2328. int64_t va = a._data._int;
  2329. int64_t vb = b._data._int;
  2330. r_dst = int(va + vb * c + 0.5);
  2331. }
  2332. return;
  2333. case REAL: {
  2334. double ra = a._data._real;
  2335. double rb = b._data._real;
  2336. r_dst = ra + rb * c;
  2337. }
  2338. return;
  2339. case VECTOR2: {
  2340. r_dst = *reinterpret_cast<const Vector2 *>(a._data._mem) + *reinterpret_cast<const Vector2 *>(b._data._mem) * c;
  2341. }
  2342. return;
  2343. case RECT2: {
  2344. const Rect2 *ra = reinterpret_cast<const Rect2 *>(a._data._mem);
  2345. const Rect2 *rb = reinterpret_cast<const Rect2 *>(b._data._mem);
  2346. r_dst = Rect2(ra->position + rb->position * c, ra->size + rb->size * c);
  2347. }
  2348. return;
  2349. case VECTOR3: {
  2350. r_dst = *reinterpret_cast<const Vector3 *>(a._data._mem) + *reinterpret_cast<const Vector3 *>(b._data._mem) * c;
  2351. }
  2352. return;
  2353. case RECT3: {
  2354. const Rect3 *ra = reinterpret_cast<const Rect3 *>(a._data._mem);
  2355. const Rect3 *rb = reinterpret_cast<const Rect3 *>(b._data._mem);
  2356. r_dst = Rect3(ra->position + rb->position * c, ra->size + rb->size * c);
  2357. }
  2358. return;
  2359. case QUAT: {
  2360. Quat empty_rot;
  2361. const Quat *qa = reinterpret_cast<const Quat *>(a._data._mem);
  2362. const Quat *qb = reinterpret_cast<const Quat *>(b._data._mem);
  2363. r_dst = *qa * empty_rot.slerp(*qb, c);
  2364. }
  2365. return;
  2366. case COLOR: {
  2367. const Color *ca = reinterpret_cast<const Color *>(a._data._mem);
  2368. const Color *cb = reinterpret_cast<const Color *>(b._data._mem);
  2369. float r = ca->r + cb->r * c;
  2370. float g = ca->g + cb->g * c;
  2371. float b = ca->b + cb->b * c;
  2372. float a = ca->a + cb->a * c;
  2373. r = r > 1.0 ? 1.0 : r;
  2374. g = g > 1.0 ? 1.0 : g;
  2375. b = b > 1.0 ? 1.0 : b;
  2376. a = a > 1.0 ? 1.0 : a;
  2377. r_dst = Color(r, g, b, a);
  2378. }
  2379. return;
  2380. default: { r_dst = c < 0.5 ? a : b; }
  2381. return;
  2382. }
  2383. }
  2384. void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst) {
  2385. if (a.type != b.type) {
  2386. if (a.is_num() && b.is_num()) {
  2387. //not as efficient but..
  2388. real_t va = a;
  2389. real_t vb = b;
  2390. r_dst = (1.0 - c) * va + vb * c;
  2391. } else {
  2392. r_dst = a;
  2393. }
  2394. return;
  2395. }
  2396. switch (a.type) {
  2397. case NIL: {
  2398. r_dst = Variant();
  2399. }
  2400. return;
  2401. case BOOL: {
  2402. r_dst = a;
  2403. }
  2404. return;
  2405. case INT: {
  2406. int64_t va = a._data._int;
  2407. int64_t vb = b._data._int;
  2408. r_dst = int((1.0 - c) * va + vb * c);
  2409. }
  2410. return;
  2411. case REAL: {
  2412. real_t va = a._data._real;
  2413. real_t vb = b._data._real;
  2414. r_dst = (1.0 - c) * va + vb * c;
  2415. }
  2416. return;
  2417. case STRING: {
  2418. //this is pretty funny and bizarre, but artists like to use it for typewritter effects
  2419. String sa = *reinterpret_cast<const String *>(a._data._mem);
  2420. String sb = *reinterpret_cast<const String *>(b._data._mem);
  2421. String dst;
  2422. int csize = sb.length() * c + sa.length() * (1.0 - c);
  2423. if (csize == 0) {
  2424. r_dst = "";
  2425. return;
  2426. }
  2427. dst.resize(csize + 1);
  2428. dst[csize] = 0;
  2429. int split = csize / 2;
  2430. for (int i = 0; i < csize; i++) {
  2431. CharType chr = ' ';
  2432. if (i < split) {
  2433. if (i < sa.length())
  2434. chr = sa[i];
  2435. else if (i < sb.length())
  2436. chr = sb[i];
  2437. } else {
  2438. if (i < sb.length())
  2439. chr = sb[i];
  2440. else if (i < sa.length())
  2441. chr = sa[i];
  2442. }
  2443. dst[i] = chr;
  2444. }
  2445. r_dst = dst;
  2446. }
  2447. return;
  2448. case VECTOR2: {
  2449. r_dst = reinterpret_cast<const Vector2 *>(a._data._mem)->linear_interpolate(*reinterpret_cast<const Vector2 *>(b._data._mem), c);
  2450. }
  2451. return;
  2452. case RECT2: {
  2453. 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));
  2454. }
  2455. return;
  2456. case VECTOR3: {
  2457. r_dst = reinterpret_cast<const Vector3 *>(a._data._mem)->linear_interpolate(*reinterpret_cast<const Vector3 *>(b._data._mem), c);
  2458. }
  2459. return;
  2460. case TRANSFORM2D: {
  2461. r_dst = a._data._transform2d->interpolate_with(*b._data._transform2d, c);
  2462. }
  2463. return;
  2464. case PLANE: {
  2465. r_dst = a;
  2466. }
  2467. return;
  2468. case QUAT: {
  2469. r_dst = reinterpret_cast<const Quat *>(a._data._mem)->slerp(*reinterpret_cast<const Quat *>(b._data._mem), c);
  2470. }
  2471. return;
  2472. case RECT3: {
  2473. 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));
  2474. }
  2475. return;
  2476. case BASIS: {
  2477. r_dst = Transform(*a._data._basis).interpolate_with(Transform(*b._data._basis), c).basis;
  2478. }
  2479. return;
  2480. case TRANSFORM: {
  2481. r_dst = a._data._transform->interpolate_with(*b._data._transform, c);
  2482. }
  2483. return;
  2484. case COLOR: {
  2485. r_dst = reinterpret_cast<const Color *>(a._data._mem)->linear_interpolate(*reinterpret_cast<const Color *>(b._data._mem), c);
  2486. }
  2487. return;
  2488. case NODE_PATH: {
  2489. r_dst = a;
  2490. }
  2491. return;
  2492. case _RID: {
  2493. r_dst = a;
  2494. }
  2495. return;
  2496. case OBJECT: {
  2497. r_dst = a;
  2498. }
  2499. return;
  2500. case DICTIONARY: {
  2501. }
  2502. return;
  2503. case ARRAY: {
  2504. r_dst = a;
  2505. }
  2506. return;
  2507. case POOL_BYTE_ARRAY: {
  2508. r_dst = a;
  2509. }
  2510. return;
  2511. case POOL_INT_ARRAY: {
  2512. r_dst = a;
  2513. }
  2514. return;
  2515. case POOL_REAL_ARRAY: {
  2516. r_dst = a;
  2517. }
  2518. return;
  2519. case POOL_STRING_ARRAY: {
  2520. r_dst = a;
  2521. }
  2522. return;
  2523. case POOL_VECTOR2_ARRAY: {
  2524. const PoolVector<Vector2> *arr_a = reinterpret_cast<const PoolVector<Vector2> *>(a._data._mem);
  2525. const PoolVector<Vector2> *arr_b = reinterpret_cast<const PoolVector<Vector2> *>(b._data._mem);
  2526. int sz = arr_a->size();
  2527. if (sz == 0 || arr_b->size() != sz) {
  2528. r_dst = a;
  2529. } else {
  2530. PoolVector<Vector2> v;
  2531. v.resize(sz);
  2532. {
  2533. PoolVector<Vector2>::Write vw = v.write();
  2534. PoolVector<Vector2>::Read ar = arr_a->read();
  2535. PoolVector<Vector2>::Read br = arr_b->read();
  2536. for (int i = 0; i < sz; i++) {
  2537. vw[i] = ar[i].linear_interpolate(br[i], c);
  2538. }
  2539. }
  2540. r_dst = v;
  2541. }
  2542. }
  2543. return;
  2544. case POOL_VECTOR3_ARRAY: {
  2545. const PoolVector<Vector3> *arr_a = reinterpret_cast<const PoolVector<Vector3> *>(a._data._mem);
  2546. const PoolVector<Vector3> *arr_b = reinterpret_cast<const PoolVector<Vector3> *>(b._data._mem);
  2547. int sz = arr_a->size();
  2548. if (sz == 0 || arr_b->size() != sz) {
  2549. r_dst = a;
  2550. } else {
  2551. PoolVector<Vector3> v;
  2552. v.resize(sz);
  2553. {
  2554. PoolVector<Vector3>::Write vw = v.write();
  2555. PoolVector<Vector3>::Read ar = arr_a->read();
  2556. PoolVector<Vector3>::Read br = arr_b->read();
  2557. for (int i = 0; i < sz; i++) {
  2558. vw[i] = ar[i].linear_interpolate(br[i], c);
  2559. }
  2560. }
  2561. r_dst = v;
  2562. }
  2563. }
  2564. return;
  2565. case POOL_COLOR_ARRAY: {
  2566. r_dst = a;
  2567. }
  2568. return;
  2569. default: {
  2570. r_dst = a;
  2571. }
  2572. }
  2573. }
  2574. static const char *_op_names[Variant::OP_MAX] = {
  2575. "==",
  2576. "!=",
  2577. "<",
  2578. "<=",
  2579. ">",
  2580. ">=",
  2581. "+",
  2582. "-",
  2583. "*",
  2584. "/",
  2585. "- (negation)",
  2586. "%",
  2587. "..",
  2588. "<<",
  2589. ">>",
  2590. "&",
  2591. "|",
  2592. "^",
  2593. "~",
  2594. "and",
  2595. "or",
  2596. "xor",
  2597. "not",
  2598. "in"
  2599. };
  2600. String Variant::get_operator_name(Operator p_op) {
  2601. ERR_FAIL_INDEX_V(p_op, OP_MAX, "");
  2602. return _op_names[p_op];
  2603. }