variant_setget.cpp 85 KB

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