tile_set.cpp 213 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568
  1. /*************************************************************************/
  2. /* tile_set.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "tile_set.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/math/geometry_2d.h"
  34. #include "core/templates/local_vector.h"
  35. #include "scene/2d/navigation_region_2d.h"
  36. #include "scene/gui/control.h"
  37. #include "scene/resources/convex_polygon_shape_2d.h"
  38. #include "servers/navigation_server_2d.h"
  39. /////////////////////////////// TileMapPattern //////////////////////////////////////
  40. void TileMapPattern::_set_tile_data(const Vector<int> &p_data) {
  41. int c = p_data.size();
  42. const int *r = p_data.ptr();
  43. int offset = 3;
  44. ERR_FAIL_COND_MSG(c % offset != 0, "Corrupted tile data.");
  45. clear();
  46. for (int i = 0; i < c; i += offset) {
  47. const uint8_t *ptr = (const uint8_t *)&r[i];
  48. uint8_t local[12];
  49. for (int j = 0; j < 12; j++) {
  50. local[j] = ptr[j];
  51. }
  52. #ifdef BIG_ENDIAN_ENABLED
  53. SWAP(local[0], local[3]);
  54. SWAP(local[1], local[2]);
  55. SWAP(local[4], local[7]);
  56. SWAP(local[5], local[6]);
  57. SWAP(local[8], local[11]);
  58. SWAP(local[9], local[10]);
  59. #endif
  60. int16_t x = decode_uint16(&local[0]);
  61. int16_t y = decode_uint16(&local[2]);
  62. uint16_t source_id = decode_uint16(&local[4]);
  63. uint16_t atlas_coords_x = decode_uint16(&local[6]);
  64. uint16_t atlas_coords_y = decode_uint16(&local[8]);
  65. uint16_t alternative_tile = decode_uint16(&local[10]);
  66. set_cell(Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile);
  67. }
  68. emit_signal(SNAME("changed"));
  69. }
  70. Vector<int> TileMapPattern::_get_tile_data() const {
  71. // Export tile data to raw format
  72. Vector<int> data;
  73. data.resize(pattern.size() * 3);
  74. int *w = data.ptrw();
  75. // Save in highest format
  76. int idx = 0;
  77. for (const KeyValue<Vector2i, TileMapCell> &E : pattern) {
  78. uint8_t *ptr = (uint8_t *)&w[idx];
  79. encode_uint16((int16_t)(E.key.x), &ptr[0]);
  80. encode_uint16((int16_t)(E.key.y), &ptr[2]);
  81. encode_uint16(E.value.source_id, &ptr[4]);
  82. encode_uint16(E.value.coord_x, &ptr[6]);
  83. encode_uint16(E.value.coord_y, &ptr[8]);
  84. encode_uint16(E.value.alternative_tile, &ptr[10]);
  85. idx += 3;
  86. }
  87. return data;
  88. }
  89. void TileMapPattern::set_cell(const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) {
  90. ERR_FAIL_COND_MSG(p_coords.x < 0 || p_coords.y < 0, vformat("Cannot set cell with negative coords in a TileMapPattern. Wrong coords: %s", p_coords));
  91. size = size.max(p_coords + Vector2i(1, 1));
  92. pattern[p_coords] = TileMapCell(p_source_id, p_atlas_coords, p_alternative_tile);
  93. emit_changed();
  94. }
  95. bool TileMapPattern::has_cell(const Vector2i &p_coords) const {
  96. return pattern.has(p_coords);
  97. }
  98. void TileMapPattern::remove_cell(const Vector2i &p_coords, bool p_update_size) {
  99. ERR_FAIL_COND(!pattern.has(p_coords));
  100. pattern.erase(p_coords);
  101. if (p_update_size) {
  102. size = Vector2i();
  103. for (const KeyValue<Vector2i, TileMapCell> &E : pattern) {
  104. size = size.max(E.key + Vector2i(1, 1));
  105. }
  106. }
  107. emit_changed();
  108. }
  109. int TileMapPattern::get_cell_source_id(const Vector2i &p_coords) const {
  110. ERR_FAIL_COND_V(!pattern.has(p_coords), TileSet::INVALID_SOURCE);
  111. return pattern[p_coords].source_id;
  112. }
  113. Vector2i TileMapPattern::get_cell_atlas_coords(const Vector2i &p_coords) const {
  114. ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetSource::INVALID_ATLAS_COORDS);
  115. return pattern[p_coords].get_atlas_coords();
  116. }
  117. int TileMapPattern::get_cell_alternative_tile(const Vector2i &p_coords) const {
  118. ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetSource::INVALID_TILE_ALTERNATIVE);
  119. return pattern[p_coords].alternative_tile;
  120. }
  121. TypedArray<Vector2i> TileMapPattern::get_used_cells() const {
  122. // Returns the cells used in the tilemap.
  123. TypedArray<Vector2i> a;
  124. a.resize(pattern.size());
  125. int i = 0;
  126. for (const KeyValue<Vector2i, TileMapCell> &E : pattern) {
  127. Vector2i p(E.key.x, E.key.y);
  128. a[i++] = p;
  129. }
  130. return a;
  131. }
  132. Vector2i TileMapPattern::get_size() const {
  133. return size;
  134. }
  135. void TileMapPattern::set_size(const Vector2i &p_size) {
  136. for (const KeyValue<Vector2i, TileMapCell> &E : pattern) {
  137. Vector2i coords = E.key;
  138. if (p_size.x <= coords.x || p_size.y <= coords.y) {
  139. ERR_FAIL_MSG(vformat("Cannot set pattern size to %s, it contains a tile at %s. Size can only be increased.", p_size, coords));
  140. };
  141. }
  142. size = p_size;
  143. emit_changed();
  144. }
  145. bool TileMapPattern::is_empty() const {
  146. return pattern.is_empty();
  147. };
  148. void TileMapPattern::clear() {
  149. size = Vector2i();
  150. pattern.clear();
  151. emit_changed();
  152. };
  153. bool TileMapPattern::_set(const StringName &p_name, const Variant &p_value) {
  154. if (p_name == "tile_data") {
  155. if (p_value.is_array()) {
  156. _set_tile_data(p_value);
  157. return true;
  158. }
  159. return false;
  160. }
  161. return false;
  162. }
  163. bool TileMapPattern::_get(const StringName &p_name, Variant &r_ret) const {
  164. if (p_name == "tile_data") {
  165. r_ret = _get_tile_data();
  166. return true;
  167. }
  168. return false;
  169. }
  170. void TileMapPattern::_get_property_list(List<PropertyInfo> *p_list) const {
  171. p_list->push_back(PropertyInfo(Variant::OBJECT, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  172. }
  173. void TileMapPattern::_bind_methods() {
  174. ClassDB::bind_method(D_METHOD("set_cell", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMapPattern::set_cell, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(TileSetSource::INVALID_TILE_ALTERNATIVE));
  175. ClassDB::bind_method(D_METHOD("has_cell", "coords"), &TileMapPattern::has_cell);
  176. ClassDB::bind_method(D_METHOD("remove_cell", "coords", "update_size"), &TileMapPattern::remove_cell);
  177. ClassDB::bind_method(D_METHOD("get_cell_source_id", "coords"), &TileMapPattern::get_cell_source_id);
  178. ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "coords"), &TileMapPattern::get_cell_atlas_coords);
  179. ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "coords"), &TileMapPattern::get_cell_alternative_tile);
  180. ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMapPattern::get_used_cells);
  181. ClassDB::bind_method(D_METHOD("get_size"), &TileMapPattern::get_size);
  182. ClassDB::bind_method(D_METHOD("set_size", "size"), &TileMapPattern::set_size);
  183. ClassDB::bind_method(D_METHOD("is_empty"), &TileMapPattern::is_empty);
  184. }
  185. /////////////////////////////// TileSet //////////////////////////////////////
  186. bool TileSet::TerrainsPattern::is_valid() const {
  187. return valid;
  188. }
  189. bool TileSet::TerrainsPattern::is_erase_pattern() const {
  190. return not_empty_terrains_count == 0;
  191. }
  192. bool TileSet::TerrainsPattern::operator<(const TerrainsPattern &p_terrains_pattern) const {
  193. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  194. if (is_valid_bit[i] != p_terrains_pattern.is_valid_bit[i]) {
  195. return is_valid_bit[i] < p_terrains_pattern.is_valid_bit[i];
  196. }
  197. }
  198. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  199. if (is_valid_bit[i] && bits[i] != p_terrains_pattern.bits[i]) {
  200. return bits[i] < p_terrains_pattern.bits[i];
  201. }
  202. }
  203. return false;
  204. }
  205. bool TileSet::TerrainsPattern::operator==(const TerrainsPattern &p_terrains_pattern) const {
  206. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  207. if (is_valid_bit[i] != p_terrains_pattern.is_valid_bit[i]) {
  208. return false;
  209. }
  210. if (is_valid_bit[i] && bits[i] != p_terrains_pattern.bits[i]) {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }
  216. void TileSet::TerrainsPattern::set_terrain(TileSet::CellNeighbor p_peering_bit, int p_terrain) {
  217. ERR_FAIL_COND(p_peering_bit == TileSet::CELL_NEIGHBOR_MAX);
  218. ERR_FAIL_COND(!is_valid_bit[p_peering_bit]);
  219. ERR_FAIL_COND(p_terrain < -1);
  220. // Update the "is_erase_pattern" status.
  221. if (p_terrain >= 0 && bits[p_peering_bit] < 0) {
  222. not_empty_terrains_count++;
  223. } else if (p_terrain < 0 && bits[p_peering_bit] >= 0) {
  224. not_empty_terrains_count--;
  225. }
  226. bits[p_peering_bit] = p_terrain;
  227. }
  228. int TileSet::TerrainsPattern::get_terrain(TileSet::CellNeighbor p_peering_bit) const {
  229. ERR_FAIL_COND_V(p_peering_bit == TileSet::CELL_NEIGHBOR_MAX, -1);
  230. ERR_FAIL_COND_V(!is_valid_bit[p_peering_bit], -1);
  231. return bits[p_peering_bit];
  232. }
  233. void TileSet::TerrainsPattern::set_terrains_from_array(Array p_terrains) {
  234. int in_array_index = 0;
  235. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  236. if (is_valid_bit[i]) {
  237. ERR_FAIL_COND(in_array_index >= p_terrains.size());
  238. set_terrain(TileSet::CellNeighbor(i), p_terrains[in_array_index]);
  239. in_array_index++;
  240. }
  241. }
  242. }
  243. Array TileSet::TerrainsPattern::get_terrains_as_array() const {
  244. Array output;
  245. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  246. if (is_valid_bit[i]) {
  247. output.push_back(bits[i]);
  248. }
  249. }
  250. return output;
  251. }
  252. TileSet::TerrainsPattern::TerrainsPattern(const TileSet *p_tile_set, int p_terrain_set) {
  253. ERR_FAIL_COND(p_terrain_set < 0);
  254. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  255. is_valid_bit[i] = (p_tile_set->is_valid_peering_bit_terrain(p_terrain_set, TileSet::CellNeighbor(i)));
  256. bits[i] = -1;
  257. }
  258. valid = true;
  259. }
  260. const int TileSet::INVALID_SOURCE = -1;
  261. const char *TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[] = {
  262. "right_side",
  263. "right_corner",
  264. "bottom_right_side",
  265. "bottom_right_corner",
  266. "bottom_side",
  267. "bottom_corner",
  268. "bottom_left_side",
  269. "bottom_left_corner",
  270. "left_side",
  271. "left_corner",
  272. "top_left_side",
  273. "top_left_corner",
  274. "top_side",
  275. "top_corner",
  276. "top_right_side",
  277. "top_right_corner"
  278. };
  279. // -- Shape and layout --
  280. void TileSet::set_tile_shape(TileSet::TileShape p_shape) {
  281. tile_shape = p_shape;
  282. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  283. E_source.value->notify_tile_data_properties_should_change();
  284. }
  285. terrain_bits_meshes_dirty = true;
  286. tile_meshes_dirty = true;
  287. notify_property_list_changed();
  288. emit_changed();
  289. }
  290. TileSet::TileShape TileSet::get_tile_shape() const {
  291. return tile_shape;
  292. }
  293. void TileSet::set_tile_layout(TileSet::TileLayout p_layout) {
  294. tile_layout = p_layout;
  295. emit_changed();
  296. }
  297. TileSet::TileLayout TileSet::get_tile_layout() const {
  298. return tile_layout;
  299. }
  300. void TileSet::set_tile_offset_axis(TileSet::TileOffsetAxis p_alignment) {
  301. tile_offset_axis = p_alignment;
  302. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  303. E_source.value->notify_tile_data_properties_should_change();
  304. }
  305. terrain_bits_meshes_dirty = true;
  306. tile_meshes_dirty = true;
  307. emit_changed();
  308. }
  309. TileSet::TileOffsetAxis TileSet::get_tile_offset_axis() const {
  310. return tile_offset_axis;
  311. }
  312. void TileSet::set_tile_size(Size2i p_size) {
  313. ERR_FAIL_COND(p_size.x < 1 || p_size.y < 1);
  314. tile_size = p_size;
  315. terrain_bits_meshes_dirty = true;
  316. tile_meshes_dirty = true;
  317. emit_changed();
  318. }
  319. Size2i TileSet::get_tile_size() const {
  320. return tile_size;
  321. }
  322. int TileSet::get_next_source_id() const {
  323. return next_source_id;
  324. }
  325. void TileSet::_update_terrains_cache() {
  326. if (terrains_cache_dirty) {
  327. // Organizes tiles into structures.
  328. per_terrain_pattern_tiles.resize(terrain_sets.size());
  329. for (int i = 0; i < (int)per_terrain_pattern_tiles.size(); i++) {
  330. per_terrain_pattern_tiles[i].clear();
  331. }
  332. for (const KeyValue<int, Ref<TileSetSource>> &kv : sources) {
  333. Ref<TileSetSource> source = kv.value;
  334. Ref<TileSetAtlasSource> atlas_source = source;
  335. if (atlas_source.is_valid()) {
  336. for (int tile_index = 0; tile_index < source->get_tiles_count(); tile_index++) {
  337. Vector2i tile_id = source->get_tile_id(tile_index);
  338. for (int alternative_index = 0; alternative_index < source->get_alternative_tiles_count(tile_id); alternative_index++) {
  339. int alternative_id = source->get_alternative_tile_id(tile_id, alternative_index);
  340. // Executed for each tile_data.
  341. TileData *tile_data = atlas_source->get_tile_data(tile_id, alternative_id);
  342. int terrain_set = tile_data->get_terrain_set();
  343. if (terrain_set >= 0) {
  344. TileMapCell cell;
  345. cell.source_id = kv.key;
  346. cell.set_atlas_coords(tile_id);
  347. cell.alternative_tile = alternative_id;
  348. TileSet::TerrainsPattern terrains_pattern = tile_data->get_terrains_pattern();
  349. // Terrain bits.
  350. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  351. CellNeighbor bit = CellNeighbor(i);
  352. if (is_valid_peering_bit_terrain(terrain_set, bit)) {
  353. int terrain = terrains_pattern.get_terrain(bit);
  354. if (terrain >= 0) {
  355. per_terrain_pattern_tiles[terrain_set][terrains_pattern].insert(cell);
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. }
  363. }
  364. // Add the empty cell in the possible patterns and cells.
  365. for (int i = 0; i < terrain_sets.size(); i++) {
  366. TileSet::TerrainsPattern empty_pattern(this, i);
  367. TileMapCell empty_cell;
  368. empty_cell.source_id = TileSet::INVALID_SOURCE;
  369. empty_cell.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  370. empty_cell.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  371. per_terrain_pattern_tiles[i][empty_pattern].insert(empty_cell);
  372. }
  373. terrains_cache_dirty = false;
  374. }
  375. }
  376. void TileSet::_compute_next_source_id() {
  377. while (sources.has(next_source_id)) {
  378. next_source_id = (next_source_id + 1) % 1073741824; // 2 ** 30
  379. };
  380. }
  381. // Sources management
  382. int TileSet::add_source(Ref<TileSetSource> p_tile_set_source, int p_atlas_source_id_override) {
  383. ERR_FAIL_COND_V(!p_tile_set_source.is_valid(), TileSet::INVALID_SOURCE);
  384. ERR_FAIL_COND_V_MSG(p_atlas_source_id_override >= 0 && (sources.has(p_atlas_source_id_override)), TileSet::INVALID_SOURCE, vformat("Cannot create TileSet atlas source. Another atlas source exists with id %d.", p_atlas_source_id_override));
  385. int new_source_id = p_atlas_source_id_override >= 0 ? p_atlas_source_id_override : next_source_id;
  386. sources[new_source_id] = p_tile_set_source;
  387. source_ids.append(new_source_id);
  388. source_ids.sort();
  389. p_tile_set_source->set_tile_set(this);
  390. _compute_next_source_id();
  391. sources[new_source_id]->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TileSet::_source_changed));
  392. terrains_cache_dirty = true;
  393. emit_changed();
  394. return new_source_id;
  395. }
  396. void TileSet::remove_source(int p_source_id) {
  397. ERR_FAIL_COND_MSG(!sources.has(p_source_id), vformat("Cannot remove TileSet atlas source. No tileset atlas source with id %d.", p_source_id));
  398. sources[p_source_id]->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TileSet::_source_changed));
  399. sources[p_source_id]->set_tile_set(nullptr);
  400. sources.erase(p_source_id);
  401. source_ids.erase(p_source_id);
  402. source_ids.sort();
  403. terrains_cache_dirty = true;
  404. emit_changed();
  405. }
  406. void TileSet::set_source_id(int p_source_id, int p_new_source_id) {
  407. ERR_FAIL_COND(p_new_source_id < 0);
  408. ERR_FAIL_COND_MSG(!sources.has(p_source_id), vformat("Cannot change TileSet atlas source ID. No tileset atlas source with id %d.", p_source_id));
  409. if (p_source_id == p_new_source_id) {
  410. return;
  411. }
  412. ERR_FAIL_COND_MSG(sources.has(p_new_source_id), vformat("Cannot change TileSet atlas source ID. Another atlas source exists with id %d.", p_new_source_id));
  413. sources[p_new_source_id] = sources[p_source_id];
  414. sources.erase(p_source_id);
  415. source_ids.erase(p_source_id);
  416. source_ids.append(p_new_source_id);
  417. source_ids.sort();
  418. _compute_next_source_id();
  419. terrains_cache_dirty = true;
  420. emit_changed();
  421. }
  422. bool TileSet::has_source(int p_source_id) const {
  423. return sources.has(p_source_id);
  424. }
  425. Ref<TileSetSource> TileSet::get_source(int p_source_id) const {
  426. ERR_FAIL_COND_V_MSG(!sources.has(p_source_id), nullptr, vformat("No TileSet atlas source with id %d.", p_source_id));
  427. return sources[p_source_id];
  428. }
  429. int TileSet::get_source_count() const {
  430. return source_ids.size();
  431. }
  432. int TileSet::get_source_id(int p_index) const {
  433. ERR_FAIL_INDEX_V(p_index, source_ids.size(), TileSet::INVALID_SOURCE);
  434. return source_ids[p_index];
  435. }
  436. // Rendering
  437. void TileSet::set_uv_clipping(bool p_uv_clipping) {
  438. if (uv_clipping == p_uv_clipping) {
  439. return;
  440. }
  441. uv_clipping = p_uv_clipping;
  442. emit_changed();
  443. }
  444. bool TileSet::is_uv_clipping() const {
  445. return uv_clipping;
  446. };
  447. int TileSet::get_occlusion_layers_count() const {
  448. return occlusion_layers.size();
  449. };
  450. void TileSet::add_occlusion_layer(int p_index) {
  451. if (p_index < 0) {
  452. p_index = occlusion_layers.size();
  453. }
  454. ERR_FAIL_INDEX(p_index, occlusion_layers.size() + 1);
  455. occlusion_layers.insert(p_index, OcclusionLayer());
  456. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  457. source.value->add_occlusion_layer(p_index);
  458. }
  459. notify_property_list_changed();
  460. emit_changed();
  461. }
  462. void TileSet::move_occlusion_layer(int p_from_index, int p_to_pos) {
  463. ERR_FAIL_INDEX(p_from_index, occlusion_layers.size());
  464. ERR_FAIL_INDEX(p_to_pos, occlusion_layers.size() + 1);
  465. occlusion_layers.insert(p_to_pos, occlusion_layers[p_from_index]);
  466. occlusion_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  467. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  468. source.value->move_occlusion_layer(p_from_index, p_to_pos);
  469. }
  470. notify_property_list_changed();
  471. emit_changed();
  472. }
  473. void TileSet::remove_occlusion_layer(int p_index) {
  474. ERR_FAIL_INDEX(p_index, occlusion_layers.size());
  475. occlusion_layers.remove_at(p_index);
  476. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  477. source.value->remove_occlusion_layer(p_index);
  478. }
  479. notify_property_list_changed();
  480. emit_changed();
  481. }
  482. void TileSet::set_occlusion_layer_light_mask(int p_layer_index, int p_light_mask) {
  483. ERR_FAIL_INDEX(p_layer_index, occlusion_layers.size());
  484. occlusion_layers.write[p_layer_index].light_mask = p_light_mask;
  485. emit_changed();
  486. }
  487. int TileSet::get_occlusion_layer_light_mask(int p_layer_index) const {
  488. ERR_FAIL_INDEX_V(p_layer_index, occlusion_layers.size(), 0);
  489. return occlusion_layers[p_layer_index].light_mask;
  490. }
  491. void TileSet::set_occlusion_layer_sdf_collision(int p_layer_index, bool p_sdf_collision) {
  492. ERR_FAIL_INDEX(p_layer_index, occlusion_layers.size());
  493. occlusion_layers.write[p_layer_index].sdf_collision = p_sdf_collision;
  494. emit_changed();
  495. }
  496. bool TileSet::get_occlusion_layer_sdf_collision(int p_layer_index) const {
  497. ERR_FAIL_INDEX_V(p_layer_index, occlusion_layers.size(), false);
  498. return occlusion_layers[p_layer_index].sdf_collision;
  499. }
  500. int TileSet::get_physics_layers_count() const {
  501. return physics_layers.size();
  502. }
  503. void TileSet::add_physics_layer(int p_index) {
  504. if (p_index < 0) {
  505. p_index = physics_layers.size();
  506. }
  507. ERR_FAIL_INDEX(p_index, physics_layers.size() + 1);
  508. physics_layers.insert(p_index, PhysicsLayer());
  509. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  510. source.value->add_physics_layer(p_index);
  511. }
  512. notify_property_list_changed();
  513. emit_changed();
  514. }
  515. void TileSet::move_physics_layer(int p_from_index, int p_to_pos) {
  516. ERR_FAIL_INDEX(p_from_index, physics_layers.size());
  517. ERR_FAIL_INDEX(p_to_pos, physics_layers.size() + 1);
  518. physics_layers.insert(p_to_pos, physics_layers[p_from_index]);
  519. physics_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  520. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  521. source.value->move_physics_layer(p_from_index, p_to_pos);
  522. }
  523. notify_property_list_changed();
  524. emit_changed();
  525. }
  526. void TileSet::remove_physics_layer(int p_index) {
  527. ERR_FAIL_INDEX(p_index, physics_layers.size());
  528. physics_layers.remove_at(p_index);
  529. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  530. source.value->remove_physics_layer(p_index);
  531. }
  532. notify_property_list_changed();
  533. emit_changed();
  534. }
  535. void TileSet::set_physics_layer_collision_layer(int p_layer_index, uint32_t p_layer) {
  536. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  537. physics_layers.write[p_layer_index].collision_layer = p_layer;
  538. emit_changed();
  539. }
  540. uint32_t TileSet::get_physics_layer_collision_layer(int p_layer_index) const {
  541. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), 0);
  542. return physics_layers[p_layer_index].collision_layer;
  543. }
  544. void TileSet::set_physics_layer_collision_mask(int p_layer_index, uint32_t p_mask) {
  545. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  546. physics_layers.write[p_layer_index].collision_mask = p_mask;
  547. emit_changed();
  548. }
  549. uint32_t TileSet::get_physics_layer_collision_mask(int p_layer_index) const {
  550. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), 0);
  551. return physics_layers[p_layer_index].collision_mask;
  552. }
  553. void TileSet::set_physics_layer_physics_material(int p_layer_index, Ref<PhysicsMaterial> p_physics_material) {
  554. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  555. physics_layers.write[p_layer_index].physics_material = p_physics_material;
  556. }
  557. Ref<PhysicsMaterial> TileSet::get_physics_layer_physics_material(int p_layer_index) const {
  558. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), Ref<PhysicsMaterial>());
  559. return physics_layers[p_layer_index].physics_material;
  560. }
  561. // Terrains
  562. int TileSet::get_terrain_sets_count() const {
  563. return terrain_sets.size();
  564. }
  565. void TileSet::add_terrain_set(int p_index) {
  566. if (p_index < 0) {
  567. p_index = terrain_sets.size();
  568. }
  569. ERR_FAIL_INDEX(p_index, terrain_sets.size() + 1);
  570. terrain_sets.insert(p_index, TerrainSet());
  571. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  572. source.value->add_terrain_set(p_index);
  573. }
  574. notify_property_list_changed();
  575. terrains_cache_dirty = true;
  576. emit_changed();
  577. }
  578. void TileSet::move_terrain_set(int p_from_index, int p_to_pos) {
  579. ERR_FAIL_INDEX(p_from_index, terrain_sets.size());
  580. ERR_FAIL_INDEX(p_to_pos, terrain_sets.size() + 1);
  581. terrain_sets.insert(p_to_pos, terrain_sets[p_from_index]);
  582. terrain_sets.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  583. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  584. source.value->move_terrain_set(p_from_index, p_to_pos);
  585. }
  586. notify_property_list_changed();
  587. terrains_cache_dirty = true;
  588. emit_changed();
  589. }
  590. void TileSet::remove_terrain_set(int p_index) {
  591. ERR_FAIL_INDEX(p_index, terrain_sets.size());
  592. terrain_sets.remove_at(p_index);
  593. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  594. source.value->remove_terrain_set(p_index);
  595. }
  596. notify_property_list_changed();
  597. terrains_cache_dirty = true;
  598. emit_changed();
  599. }
  600. void TileSet::set_terrain_set_mode(int p_terrain_set, TerrainMode p_terrain_mode) {
  601. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  602. terrain_sets.write[p_terrain_set].mode = p_terrain_mode;
  603. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  604. E_source.value->notify_tile_data_properties_should_change();
  605. }
  606. notify_property_list_changed();
  607. terrains_cache_dirty = true;
  608. emit_changed();
  609. }
  610. TileSet::TerrainMode TileSet::get_terrain_set_mode(int p_terrain_set) const {
  611. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES);
  612. return terrain_sets[p_terrain_set].mode;
  613. }
  614. int TileSet::get_terrains_count(int p_terrain_set) const {
  615. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), -1);
  616. return terrain_sets[p_terrain_set].terrains.size();
  617. }
  618. void TileSet::add_terrain(int p_terrain_set, int p_index) {
  619. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  620. Vector<Terrain> &terrains = terrain_sets.write[p_terrain_set].terrains;
  621. if (p_index < 0) {
  622. p_index = terrains.size();
  623. }
  624. ERR_FAIL_INDEX(p_index, terrains.size() + 1);
  625. terrains.insert(p_index, Terrain());
  626. // Default name and color
  627. float hue_rotate = (terrains.size() % 16) / 16.0;
  628. Color c;
  629. c.set_hsv(Math::fmod(float(hue_rotate), float(1.0)), 0.5, 0.5);
  630. terrains.write[p_index].color = c;
  631. terrains.write[p_index].name = String(vformat("Terrain %d", p_index));
  632. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  633. source.value->add_terrain(p_terrain_set, p_index);
  634. }
  635. notify_property_list_changed();
  636. terrains_cache_dirty = true;
  637. emit_changed();
  638. }
  639. void TileSet::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  640. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  641. Vector<Terrain> &terrains = terrain_sets.write[p_terrain_set].terrains;
  642. ERR_FAIL_INDEX(p_from_index, terrains.size());
  643. ERR_FAIL_INDEX(p_to_pos, terrains.size() + 1);
  644. terrains.insert(p_to_pos, terrains[p_from_index]);
  645. terrains.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  646. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  647. source.value->move_terrain(p_terrain_set, p_from_index, p_to_pos);
  648. }
  649. notify_property_list_changed();
  650. terrains_cache_dirty = true;
  651. emit_changed();
  652. }
  653. void TileSet::remove_terrain(int p_terrain_set, int p_index) {
  654. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  655. Vector<Terrain> &terrains = terrain_sets.write[p_terrain_set].terrains;
  656. ERR_FAIL_INDEX(p_index, terrains.size());
  657. terrains.remove_at(p_index);
  658. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  659. source.value->remove_terrain(p_terrain_set, p_index);
  660. }
  661. notify_property_list_changed();
  662. terrains_cache_dirty = true;
  663. emit_changed();
  664. }
  665. void TileSet::set_terrain_name(int p_terrain_set, int p_terrain_index, String p_name) {
  666. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  667. ERR_FAIL_INDEX(p_terrain_index, terrain_sets[p_terrain_set].terrains.size());
  668. terrain_sets.write[p_terrain_set].terrains.write[p_terrain_index].name = p_name;
  669. emit_changed();
  670. }
  671. String TileSet::get_terrain_name(int p_terrain_set, int p_terrain_index) const {
  672. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), String());
  673. ERR_FAIL_INDEX_V(p_terrain_index, terrain_sets[p_terrain_set].terrains.size(), String());
  674. return terrain_sets[p_terrain_set].terrains[p_terrain_index].name;
  675. }
  676. void TileSet::set_terrain_color(int p_terrain_set, int p_terrain_index, Color p_color) {
  677. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  678. ERR_FAIL_INDEX(p_terrain_index, terrain_sets[p_terrain_set].terrains.size());
  679. if (p_color.a != 1.0) {
  680. WARN_PRINT("Terrain color should have alpha == 1.0");
  681. p_color.a = 1.0;
  682. }
  683. terrain_sets.write[p_terrain_set].terrains.write[p_terrain_index].color = p_color;
  684. emit_changed();
  685. }
  686. Color TileSet::get_terrain_color(int p_terrain_set, int p_terrain_index) const {
  687. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), Color());
  688. ERR_FAIL_INDEX_V(p_terrain_index, terrain_sets[p_terrain_set].terrains.size(), Color());
  689. return terrain_sets[p_terrain_set].terrains[p_terrain_index].color;
  690. }
  691. bool TileSet::is_valid_peering_bit_for_mode(TileSet::TerrainMode p_terrain_mode, TileSet::CellNeighbor p_peering_bit) const {
  692. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  693. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  694. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  695. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  696. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  697. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_SIDE) {
  698. return true;
  699. }
  700. }
  701. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  702. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  703. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  704. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  705. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER) {
  706. return true;
  707. }
  708. }
  709. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  710. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  711. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  712. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  713. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  714. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  715. return true;
  716. }
  717. }
  718. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  719. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  720. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  721. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  722. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  723. return true;
  724. }
  725. }
  726. } else {
  727. if (get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  728. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  729. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  730. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  731. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  732. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  733. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  734. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  735. return true;
  736. }
  737. }
  738. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  739. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  740. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  741. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  742. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  743. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_CORNER ||
  744. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER) {
  745. return true;
  746. }
  747. }
  748. } else {
  749. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  750. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  751. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  752. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  753. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  754. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  755. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  756. return true;
  757. }
  758. }
  759. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  760. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  761. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  762. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  763. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  764. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  765. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER) {
  766. return true;
  767. }
  768. }
  769. }
  770. }
  771. return false;
  772. }
  773. bool TileSet::is_valid_peering_bit_terrain(int p_terrain_set, TileSet::CellNeighbor p_peering_bit) const {
  774. if (p_terrain_set < 0 || p_terrain_set >= get_terrain_sets_count()) {
  775. return false;
  776. }
  777. TileSet::TerrainMode terrain_mode = get_terrain_set_mode(p_terrain_set);
  778. return is_valid_peering_bit_for_mode(terrain_mode, p_peering_bit);
  779. }
  780. // Navigation
  781. int TileSet::get_navigation_layers_count() const {
  782. return navigation_layers.size();
  783. }
  784. void TileSet::add_navigation_layer(int p_index) {
  785. if (p_index < 0) {
  786. p_index = navigation_layers.size();
  787. }
  788. ERR_FAIL_INDEX(p_index, navigation_layers.size() + 1);
  789. navigation_layers.insert(p_index, NavigationLayer());
  790. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  791. source.value->add_navigation_layer(p_index);
  792. }
  793. notify_property_list_changed();
  794. emit_changed();
  795. }
  796. void TileSet::move_navigation_layer(int p_from_index, int p_to_pos) {
  797. ERR_FAIL_INDEX(p_from_index, navigation_layers.size());
  798. ERR_FAIL_INDEX(p_to_pos, navigation_layers.size() + 1);
  799. navigation_layers.insert(p_to_pos, navigation_layers[p_from_index]);
  800. navigation_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  801. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  802. source.value->move_navigation_layer(p_from_index, p_to_pos);
  803. }
  804. notify_property_list_changed();
  805. emit_changed();
  806. }
  807. void TileSet::remove_navigation_layer(int p_index) {
  808. ERR_FAIL_INDEX(p_index, navigation_layers.size());
  809. navigation_layers.remove_at(p_index);
  810. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  811. source.value->remove_navigation_layer(p_index);
  812. }
  813. notify_property_list_changed();
  814. emit_changed();
  815. }
  816. void TileSet::set_navigation_layer_layers(int p_layer_index, uint32_t p_layers) {
  817. ERR_FAIL_INDEX(p_layer_index, navigation_layers.size());
  818. navigation_layers.write[p_layer_index].layers = p_layers;
  819. emit_changed();
  820. }
  821. uint32_t TileSet::get_navigation_layer_layers(int p_layer_index) const {
  822. ERR_FAIL_INDEX_V(p_layer_index, navigation_layers.size(), 0);
  823. return navigation_layers[p_layer_index].layers;
  824. }
  825. // Custom data.
  826. int TileSet::get_custom_data_layers_count() const {
  827. return custom_data_layers.size();
  828. }
  829. void TileSet::add_custom_data_layer(int p_index) {
  830. if (p_index < 0) {
  831. p_index = custom_data_layers.size();
  832. }
  833. ERR_FAIL_INDEX(p_index, custom_data_layers.size() + 1);
  834. custom_data_layers.insert(p_index, CustomDataLayer());
  835. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  836. source.value->add_custom_data_layer(p_index);
  837. }
  838. notify_property_list_changed();
  839. emit_changed();
  840. }
  841. void TileSet::move_custom_data_layer(int p_from_index, int p_to_pos) {
  842. ERR_FAIL_INDEX(p_from_index, custom_data_layers.size());
  843. ERR_FAIL_INDEX(p_to_pos, custom_data_layers.size() + 1);
  844. custom_data_layers.insert(p_to_pos, custom_data_layers[p_from_index]);
  845. custom_data_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  846. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  847. source.value->move_custom_data_layer(p_from_index, p_to_pos);
  848. }
  849. notify_property_list_changed();
  850. emit_changed();
  851. }
  852. void TileSet::remove_custom_data_layer(int p_index) {
  853. ERR_FAIL_INDEX(p_index, custom_data_layers.size());
  854. custom_data_layers.remove_at(p_index);
  855. for (KeyValue<String, int> E : custom_data_layers_by_name) {
  856. if (E.value == p_index) {
  857. custom_data_layers_by_name.erase(E.key);
  858. break;
  859. }
  860. }
  861. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  862. source.value->remove_custom_data_layer(p_index);
  863. }
  864. notify_property_list_changed();
  865. emit_changed();
  866. }
  867. int TileSet::get_custom_data_layer_by_name(String p_value) const {
  868. if (custom_data_layers_by_name.has(p_value)) {
  869. return custom_data_layers_by_name[p_value];
  870. } else {
  871. return -1;
  872. }
  873. }
  874. void TileSet::set_custom_data_name(int p_layer_id, String p_value) {
  875. ERR_FAIL_INDEX(p_layer_id, custom_data_layers.size());
  876. // Exit if another property has the same name.
  877. if (!p_value.is_empty()) {
  878. for (int other_layer_id = 0; other_layer_id < get_custom_data_layers_count(); other_layer_id++) {
  879. if (other_layer_id != p_layer_id && get_custom_data_name(other_layer_id) == p_value) {
  880. ERR_FAIL_MSG(vformat("There is already a custom property named %s", p_value));
  881. }
  882. }
  883. }
  884. if (p_value.is_empty() && custom_data_layers_by_name.has(p_value)) {
  885. custom_data_layers_by_name.erase(p_value);
  886. } else {
  887. custom_data_layers_by_name[p_value] = p_layer_id;
  888. }
  889. custom_data_layers.write[p_layer_id].name = p_value;
  890. emit_changed();
  891. }
  892. String TileSet::get_custom_data_name(int p_layer_id) const {
  893. ERR_FAIL_INDEX_V(p_layer_id, custom_data_layers.size(), "");
  894. return custom_data_layers[p_layer_id].name;
  895. }
  896. void TileSet::set_custom_data_type(int p_layer_id, Variant::Type p_value) {
  897. ERR_FAIL_INDEX(p_layer_id, custom_data_layers.size());
  898. custom_data_layers.write[p_layer_id].type = p_value;
  899. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  900. E_source.value->notify_tile_data_properties_should_change();
  901. }
  902. emit_changed();
  903. }
  904. Variant::Type TileSet::get_custom_data_type(int p_layer_id) const {
  905. ERR_FAIL_INDEX_V(p_layer_id, custom_data_layers.size(), Variant::NIL);
  906. return custom_data_layers[p_layer_id].type;
  907. }
  908. void TileSet::set_source_level_tile_proxy(int p_source_from, int p_source_to) {
  909. ERR_FAIL_COND(p_source_from == TileSet::INVALID_SOURCE || p_source_to == TileSet::INVALID_SOURCE);
  910. source_level_proxies[p_source_from] = p_source_to;
  911. emit_changed();
  912. }
  913. int TileSet::get_source_level_tile_proxy(int p_source_from) {
  914. ERR_FAIL_COND_V(!source_level_proxies.has(p_source_from), TileSet::INVALID_SOURCE);
  915. return source_level_proxies[p_source_from];
  916. }
  917. bool TileSet::has_source_level_tile_proxy(int p_source_from) {
  918. return source_level_proxies.has(p_source_from);
  919. }
  920. void TileSet::remove_source_level_tile_proxy(int p_source_from) {
  921. ERR_FAIL_COND(!source_level_proxies.has(p_source_from));
  922. source_level_proxies.erase(p_source_from);
  923. emit_changed();
  924. }
  925. void TileSet::set_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_source_to, Vector2i p_coords_to) {
  926. ERR_FAIL_COND(p_source_from == TileSet::INVALID_SOURCE || p_source_to == TileSet::INVALID_SOURCE);
  927. ERR_FAIL_COND(p_coords_from == TileSetSource::INVALID_ATLAS_COORDS || p_coords_to == TileSetSource::INVALID_ATLAS_COORDS);
  928. Array from;
  929. from.push_back(p_source_from);
  930. from.push_back(p_coords_from);
  931. Array to;
  932. to.push_back(p_source_to);
  933. to.push_back(p_coords_to);
  934. coords_level_proxies[from] = to;
  935. emit_changed();
  936. }
  937. Array TileSet::get_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from) {
  938. Array from;
  939. from.push_back(p_source_from);
  940. from.push_back(p_coords_from);
  941. ERR_FAIL_COND_V(!coords_level_proxies.has(from), Array());
  942. return coords_level_proxies[from];
  943. }
  944. bool TileSet::has_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from) {
  945. Array from;
  946. from.push_back(p_source_from);
  947. from.push_back(p_coords_from);
  948. return coords_level_proxies.has(from);
  949. }
  950. void TileSet::remove_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from) {
  951. Array from;
  952. from.push_back(p_source_from);
  953. from.push_back(p_coords_from);
  954. ERR_FAIL_COND(!coords_level_proxies.has(from));
  955. coords_level_proxies.erase(from);
  956. emit_changed();
  957. }
  958. void TileSet::set_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from, int p_source_to, Vector2i p_coords_to, int p_alternative_to) {
  959. ERR_FAIL_COND(p_source_from == TileSet::INVALID_SOURCE || p_source_to == TileSet::INVALID_SOURCE);
  960. ERR_FAIL_COND(p_coords_from == TileSetSource::INVALID_ATLAS_COORDS || p_coords_to == TileSetSource::INVALID_ATLAS_COORDS);
  961. Array from;
  962. from.push_back(p_source_from);
  963. from.push_back(p_coords_from);
  964. from.push_back(p_alternative_from);
  965. Array to;
  966. to.push_back(p_source_to);
  967. to.push_back(p_coords_to);
  968. to.push_back(p_alternative_to);
  969. alternative_level_proxies[from] = to;
  970. emit_changed();
  971. }
  972. Array TileSet::get_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) {
  973. Array from;
  974. from.push_back(p_source_from);
  975. from.push_back(p_coords_from);
  976. from.push_back(p_alternative_from);
  977. ERR_FAIL_COND_V(!alternative_level_proxies.has(from), Array());
  978. return alternative_level_proxies[from];
  979. }
  980. bool TileSet::has_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) {
  981. Array from;
  982. from.push_back(p_source_from);
  983. from.push_back(p_coords_from);
  984. from.push_back(p_alternative_from);
  985. return alternative_level_proxies.has(from);
  986. }
  987. void TileSet::remove_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) {
  988. Array from;
  989. from.push_back(p_source_from);
  990. from.push_back(p_coords_from);
  991. from.push_back(p_alternative_from);
  992. ERR_FAIL_COND(!alternative_level_proxies.has(from));
  993. alternative_level_proxies.erase(from);
  994. emit_changed();
  995. }
  996. Array TileSet::get_source_level_tile_proxies() const {
  997. Array output;
  998. for (const KeyValue<int, int> &E : source_level_proxies) {
  999. Array proxy;
  1000. proxy.push_back(E.key);
  1001. proxy.push_back(E.value);
  1002. output.push_back(proxy);
  1003. }
  1004. return output;
  1005. }
  1006. Array TileSet::get_coords_level_tile_proxies() const {
  1007. Array output;
  1008. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  1009. Array proxy;
  1010. proxy.append_array(E.key);
  1011. proxy.append_array(E.value);
  1012. output.push_back(proxy);
  1013. }
  1014. return output;
  1015. }
  1016. Array TileSet::get_alternative_level_tile_proxies() const {
  1017. Array output;
  1018. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  1019. Array proxy;
  1020. proxy.append_array(E.key);
  1021. proxy.append_array(E.value);
  1022. output.push_back(proxy);
  1023. }
  1024. return output;
  1025. }
  1026. Array TileSet::map_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) const {
  1027. Array from;
  1028. from.push_back(p_source_from);
  1029. from.push_back(p_coords_from);
  1030. from.push_back(p_alternative_from);
  1031. // Check if the tile is valid, and if so, don't map the tile and return the input.
  1032. if (has_source(p_source_from)) {
  1033. Ref<TileSetSource> source = get_source(p_source_from);
  1034. if (source->has_tile(p_coords_from) && source->has_alternative_tile(p_coords_from, p_alternative_from)) {
  1035. return from;
  1036. }
  1037. }
  1038. // Source, coords and alternative match.
  1039. if (alternative_level_proxies.has(from)) {
  1040. return alternative_level_proxies[from].duplicate();
  1041. }
  1042. // Source and coords match.
  1043. from.pop_back();
  1044. if (coords_level_proxies.has(from)) {
  1045. Array output = coords_level_proxies[from].duplicate();
  1046. output.push_back(p_alternative_from);
  1047. return output;
  1048. }
  1049. // Source matches.
  1050. if (source_level_proxies.has(p_source_from)) {
  1051. Array output;
  1052. output.push_back(source_level_proxies[p_source_from]);
  1053. output.push_back(p_coords_from);
  1054. output.push_back(p_alternative_from);
  1055. return output;
  1056. }
  1057. Array output;
  1058. output.push_back(p_source_from);
  1059. output.push_back(p_coords_from);
  1060. output.push_back(p_alternative_from);
  1061. return output;
  1062. }
  1063. void TileSet::cleanup_invalid_tile_proxies() {
  1064. // Source level.
  1065. Vector<int> source_to_remove;
  1066. for (const KeyValue<int, int> &E : source_level_proxies) {
  1067. if (has_source(E.key)) {
  1068. source_to_remove.append(E.key);
  1069. }
  1070. }
  1071. for (int i = 0; i < source_to_remove.size(); i++) {
  1072. remove_source_level_tile_proxy(source_to_remove[i]);
  1073. }
  1074. // Coords level.
  1075. Vector<Array> coords_to_remove;
  1076. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  1077. Array a = E.key;
  1078. if (has_source(a[0]) && get_source(a[0])->has_tile(a[1])) {
  1079. coords_to_remove.append(a);
  1080. }
  1081. }
  1082. for (int i = 0; i < coords_to_remove.size(); i++) {
  1083. Array a = coords_to_remove[i];
  1084. remove_coords_level_tile_proxy(a[0], a[1]);
  1085. }
  1086. // Alternative level.
  1087. Vector<Array> alternative_to_remove;
  1088. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  1089. Array a = E.key;
  1090. if (has_source(a[0]) && get_source(a[0])->has_tile(a[1]) && get_source(a[0])->has_alternative_tile(a[1], a[2])) {
  1091. alternative_to_remove.append(a);
  1092. }
  1093. }
  1094. for (int i = 0; i < alternative_to_remove.size(); i++) {
  1095. Array a = alternative_to_remove[i];
  1096. remove_alternative_level_tile_proxy(a[0], a[1], a[2]);
  1097. }
  1098. }
  1099. void TileSet::clear_tile_proxies() {
  1100. source_level_proxies.clear();
  1101. coords_level_proxies.clear();
  1102. alternative_level_proxies.clear();
  1103. emit_changed();
  1104. }
  1105. int TileSet::add_pattern(Ref<TileMapPattern> p_pattern, int p_index) {
  1106. ERR_FAIL_COND_V(!p_pattern.is_valid(), -1);
  1107. ERR_FAIL_COND_V_MSG(p_pattern->is_empty(), -1, "Cannot add an empty pattern to the TileSet.");
  1108. for (unsigned int i = 0; i < patterns.size(); i++) {
  1109. ERR_FAIL_COND_V_MSG(patterns[i] == p_pattern, -1, "TileSet has already this pattern.");
  1110. }
  1111. ERR_FAIL_COND_V(p_index > (int)patterns.size(), -1);
  1112. if (p_index < 0) {
  1113. p_index = patterns.size();
  1114. }
  1115. patterns.insert(p_index, p_pattern);
  1116. emit_changed();
  1117. return p_index;
  1118. }
  1119. Ref<TileMapPattern> TileSet::get_pattern(int p_index) {
  1120. ERR_FAIL_INDEX_V(p_index, (int)patterns.size(), Ref<TileMapPattern>());
  1121. return patterns[p_index];
  1122. }
  1123. void TileSet::remove_pattern(int p_index) {
  1124. ERR_FAIL_INDEX(p_index, (int)patterns.size());
  1125. patterns.remove_at(p_index);
  1126. emit_changed();
  1127. }
  1128. int TileSet::get_patterns_count() {
  1129. return patterns.size();
  1130. }
  1131. RBSet<TileSet::TerrainsPattern> TileSet::get_terrains_pattern_set(int p_terrain_set) {
  1132. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), RBSet<TileSet::TerrainsPattern>());
  1133. _update_terrains_cache();
  1134. RBSet<TileSet::TerrainsPattern> output;
  1135. for (KeyValue<TileSet::TerrainsPattern, RBSet<TileMapCell>> kv : per_terrain_pattern_tiles[p_terrain_set]) {
  1136. output.insert(kv.key);
  1137. }
  1138. return output;
  1139. }
  1140. RBSet<TileMapCell> TileSet::get_tiles_for_terrains_pattern(int p_terrain_set, TerrainsPattern p_terrain_tile_pattern) {
  1141. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), RBSet<TileMapCell>());
  1142. _update_terrains_cache();
  1143. return per_terrain_pattern_tiles[p_terrain_set][p_terrain_tile_pattern];
  1144. }
  1145. TileMapCell TileSet::get_random_tile_from_terrains_pattern(int p_terrain_set, TileSet::TerrainsPattern p_terrain_tile_pattern) {
  1146. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), TileMapCell());
  1147. _update_terrains_cache();
  1148. // Count the sum of probabilities.
  1149. double sum = 0.0;
  1150. RBSet<TileMapCell> set = per_terrain_pattern_tiles[p_terrain_set][p_terrain_tile_pattern];
  1151. for (const TileMapCell &E : set) {
  1152. if (E.source_id >= 0) {
  1153. Ref<TileSetSource> source = sources[E.source_id];
  1154. Ref<TileSetAtlasSource> atlas_source = source;
  1155. if (atlas_source.is_valid()) {
  1156. TileData *tile_data = atlas_source->get_tile_data(E.get_atlas_coords(), E.alternative_tile);
  1157. sum += tile_data->get_probability();
  1158. } else {
  1159. sum += 1.0;
  1160. }
  1161. } else {
  1162. sum += 1.0;
  1163. }
  1164. }
  1165. // Generate a random number.
  1166. double count = 0.0;
  1167. double picked = Math::random(0.0, sum);
  1168. // Pick the tile.
  1169. for (const TileMapCell &E : set) {
  1170. if (E.source_id >= 0) {
  1171. Ref<TileSetSource> source = sources[E.source_id];
  1172. Ref<TileSetAtlasSource> atlas_source = source;
  1173. if (atlas_source.is_valid()) {
  1174. TileData *tile_data = atlas_source->get_tile_data(E.get_atlas_coords(), E.alternative_tile);
  1175. count += tile_data->get_probability();
  1176. } else {
  1177. count += 1.0;
  1178. }
  1179. } else {
  1180. count += 1.0;
  1181. }
  1182. if (count >= picked) {
  1183. return E;
  1184. }
  1185. }
  1186. ERR_FAIL_V(TileMapCell());
  1187. }
  1188. Vector<Vector2> TileSet::get_tile_shape_polygon() {
  1189. Vector<Vector2> points;
  1190. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1191. points.append(Vector2(-0.5, -0.5));
  1192. points.append(Vector2(0.5, -0.5));
  1193. points.append(Vector2(0.5, 0.5));
  1194. points.append(Vector2(-0.5, 0.5));
  1195. } else {
  1196. float overlap = 0.0;
  1197. switch (tile_shape) {
  1198. case TileSet::TILE_SHAPE_ISOMETRIC:
  1199. overlap = 0.5;
  1200. break;
  1201. case TileSet::TILE_SHAPE_HEXAGON:
  1202. overlap = 0.25;
  1203. break;
  1204. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1205. overlap = 0.0;
  1206. break;
  1207. default:
  1208. break;
  1209. }
  1210. points.append(Vector2(0.0, -0.5));
  1211. points.append(Vector2(-0.5, overlap - 0.5));
  1212. points.append(Vector2(-0.5, 0.5 - overlap));
  1213. points.append(Vector2(0.0, 0.5));
  1214. points.append(Vector2(0.5, 0.5 - overlap));
  1215. points.append(Vector2(0.5, overlap - 0.5));
  1216. if (get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  1217. for (int i = 0; i < points.size(); i++) {
  1218. points.write[i] = Vector2(points[i].y, points[i].x);
  1219. }
  1220. }
  1221. }
  1222. return points;
  1223. }
  1224. void TileSet::draw_tile_shape(CanvasItem *p_canvas_item, Transform2D p_transform, Color p_color, bool p_filled, Ref<Texture2D> p_texture) {
  1225. if (tile_meshes_dirty) {
  1226. Vector<Vector2> shape = get_tile_shape_polygon();
  1227. Vector<Vector2> uvs;
  1228. uvs.resize(shape.size());
  1229. for (int i = 0; i < shape.size(); i++) {
  1230. uvs.write[i] = shape[i] + Vector2(0.5, 0.5);
  1231. }
  1232. Vector<Color> colors;
  1233. colors.resize(shape.size());
  1234. colors.fill(Color(1.0, 1.0, 1.0, 1.0));
  1235. // Filled mesh.
  1236. tile_filled_mesh->clear_surfaces();
  1237. Array a;
  1238. a.resize(Mesh::ARRAY_MAX);
  1239. a[Mesh::ARRAY_VERTEX] = shape;
  1240. a[Mesh::ARRAY_TEX_UV] = uvs;
  1241. a[Mesh::ARRAY_COLOR] = colors;
  1242. a[Mesh::ARRAY_INDEX] = Geometry2D::triangulate_polygon(shape);
  1243. tile_filled_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  1244. // Lines mesh.
  1245. tile_lines_mesh->clear_surfaces();
  1246. a.clear();
  1247. a.resize(Mesh::ARRAY_MAX);
  1248. // Add the first point again when drawing lines.
  1249. shape.push_back(shape[0]);
  1250. colors.push_back(colors[0]);
  1251. a[Mesh::ARRAY_VERTEX] = shape;
  1252. a[Mesh::ARRAY_COLOR] = colors;
  1253. tile_lines_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINE_STRIP, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  1254. tile_meshes_dirty = false;
  1255. }
  1256. if (p_filled) {
  1257. p_canvas_item->draw_mesh(tile_filled_mesh, p_texture, p_transform, p_color);
  1258. } else {
  1259. p_canvas_item->draw_mesh(tile_lines_mesh, Ref<Texture2D>(), p_transform, p_color);
  1260. }
  1261. }
  1262. Vector<Point2> TileSet::get_terrain_bit_polygon(int p_terrain_set, TileSet::CellNeighbor p_bit) {
  1263. ERR_FAIL_COND_V(p_terrain_set < 0 || p_terrain_set >= get_terrain_sets_count(), Vector<Point2>());
  1264. TileSet::TerrainMode terrain_mode = get_terrain_set_mode(p_terrain_set);
  1265. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1266. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1267. return _get_square_corner_or_side_terrain_bit_polygon(tile_size, p_bit);
  1268. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1269. return _get_square_corner_terrain_bit_polygon(tile_size, p_bit);
  1270. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1271. return _get_square_side_terrain_bit_polygon(tile_size, p_bit);
  1272. }
  1273. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1274. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1275. return _get_isometric_corner_or_side_terrain_bit_polygon(tile_size, p_bit);
  1276. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1277. return _get_isometric_corner_terrain_bit_polygon(tile_size, p_bit);
  1278. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1279. return _get_isometric_side_terrain_bit_polygon(tile_size, p_bit);
  1280. }
  1281. } else {
  1282. float overlap = 0.0;
  1283. switch (tile_shape) {
  1284. case TileSet::TILE_SHAPE_HEXAGON:
  1285. overlap = 0.25;
  1286. break;
  1287. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1288. overlap = 0.0;
  1289. break;
  1290. default:
  1291. break;
  1292. }
  1293. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1294. return _get_half_offset_corner_or_side_terrain_bit_polygon(tile_size, p_bit, overlap, tile_offset_axis);
  1295. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1296. return _get_half_offset_corner_terrain_bit_polygon(tile_size, p_bit, overlap, tile_offset_axis);
  1297. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1298. return _get_half_offset_side_terrain_bit_polygon(tile_size, p_bit, overlap, tile_offset_axis);
  1299. }
  1300. }
  1301. }
  1302. #define TERRAIN_ALPHA 0.6
  1303. void TileSet::draw_terrains(CanvasItem *p_canvas_item, Transform2D p_transform, const TileData *p_tile_data) {
  1304. ERR_FAIL_COND(!p_tile_data);
  1305. if (terrain_bits_meshes_dirty) {
  1306. // Recompute the meshes.
  1307. terrain_bits_meshes.clear();
  1308. for (int terrain_mode_index = 0; terrain_mode_index < 3; terrain_mode_index++) {
  1309. TerrainMode terrain_mode = TerrainMode(terrain_mode_index);
  1310. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1311. CellNeighbor bit = CellNeighbor(i);
  1312. if (is_valid_peering_bit_for_mode(terrain_mode, bit)) {
  1313. Vector<Vector2> polygon;
  1314. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1315. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1316. polygon = _get_square_corner_or_side_terrain_bit_polygon(tile_size, bit);
  1317. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1318. polygon = _get_square_corner_terrain_bit_polygon(tile_size, bit);
  1319. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1320. polygon = _get_square_side_terrain_bit_polygon(tile_size, bit);
  1321. }
  1322. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1323. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1324. polygon = _get_isometric_corner_or_side_terrain_bit_polygon(tile_size, bit);
  1325. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1326. polygon = _get_isometric_corner_terrain_bit_polygon(tile_size, bit);
  1327. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1328. polygon = _get_isometric_side_terrain_bit_polygon(tile_size, bit);
  1329. }
  1330. } else {
  1331. float overlap = 0.0;
  1332. switch (tile_shape) {
  1333. case TileSet::TILE_SHAPE_HEXAGON:
  1334. overlap = 0.25;
  1335. break;
  1336. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1337. overlap = 0.0;
  1338. break;
  1339. default:
  1340. break;
  1341. }
  1342. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1343. polygon = _get_half_offset_corner_or_side_terrain_bit_polygon(tile_size, bit, overlap, tile_offset_axis);
  1344. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1345. polygon = _get_half_offset_corner_terrain_bit_polygon(tile_size, bit, overlap, tile_offset_axis);
  1346. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1347. polygon = _get_half_offset_side_terrain_bit_polygon(tile_size, bit, overlap, tile_offset_axis);
  1348. }
  1349. }
  1350. Ref<ArrayMesh> mesh;
  1351. mesh.instantiate();
  1352. Vector<Vector2> uvs;
  1353. uvs.resize(polygon.size());
  1354. Vector<Color> colors;
  1355. colors.resize(polygon.size());
  1356. colors.fill(Color(1.0, 1.0, 1.0, 1.0));
  1357. Array a;
  1358. a.resize(Mesh::ARRAY_MAX);
  1359. a[Mesh::ARRAY_VERTEX] = polygon;
  1360. a[Mesh::ARRAY_TEX_UV] = uvs;
  1361. a[Mesh::ARRAY_COLOR] = colors;
  1362. a[Mesh::ARRAY_INDEX] = Geometry2D::triangulate_polygon(polygon);
  1363. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  1364. terrain_bits_meshes[terrain_mode][bit] = mesh;
  1365. }
  1366. }
  1367. }
  1368. terrain_bits_meshes_dirty = false;
  1369. }
  1370. int terrain_set = p_tile_data->get_terrain_set();
  1371. if (terrain_set < 0) {
  1372. return;
  1373. }
  1374. TileSet::TerrainMode terrain_mode = get_terrain_set_mode(terrain_set);
  1375. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), p_transform);
  1376. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1377. CellNeighbor bit = CellNeighbor(i);
  1378. if (is_valid_peering_bit_terrain(terrain_set, bit)) {
  1379. int terrain_id = p_tile_data->get_peering_bit_terrain(bit);
  1380. if (terrain_id >= 0) {
  1381. Color color = get_terrain_color(terrain_set, terrain_id);
  1382. color.a = TERRAIN_ALPHA;
  1383. p_canvas_item->draw_mesh(terrain_bits_meshes[terrain_mode][bit], Ref<Texture2D>(), Transform2D(), color);
  1384. }
  1385. }
  1386. }
  1387. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), Transform2D());
  1388. }
  1389. Vector<Vector<Ref<Texture2D>>> TileSet::generate_terrains_icons(Size2i p_size) {
  1390. // Counts the number of matching terrain tiles and find the best matching icon.
  1391. struct Count {
  1392. int count = 0;
  1393. float probability = 0.0;
  1394. Ref<Texture2D> texture;
  1395. Rect2i region;
  1396. };
  1397. Vector<Vector<Ref<Texture2D>>> output;
  1398. LocalVector<LocalVector<Count>> counts;
  1399. output.resize(get_terrain_sets_count());
  1400. counts.resize(get_terrain_sets_count());
  1401. for (int terrain_set = 0; terrain_set < get_terrain_sets_count(); terrain_set++) {
  1402. output.write[terrain_set].resize(get_terrains_count(terrain_set));
  1403. counts[terrain_set].resize(get_terrains_count(terrain_set));
  1404. }
  1405. for (int source_index = 0; source_index < get_source_count(); source_index++) {
  1406. int source_id = get_source_id(source_index);
  1407. Ref<TileSetSource> source = get_source(source_id);
  1408. Ref<TileSetAtlasSource> atlas_source = source;
  1409. if (atlas_source.is_valid()) {
  1410. for (int tile_index = 0; tile_index < source->get_tiles_count(); tile_index++) {
  1411. Vector2i tile_id = source->get_tile_id(tile_index);
  1412. for (int alternative_index = 0; alternative_index < source->get_alternative_tiles_count(tile_id); alternative_index++) {
  1413. int alternative_id = source->get_alternative_tile_id(tile_id, alternative_index);
  1414. TileData *tile_data = atlas_source->get_tile_data(tile_id, alternative_id);
  1415. int terrain_set = tile_data->get_terrain_set();
  1416. if (terrain_set >= 0) {
  1417. ERR_FAIL_INDEX_V(terrain_set, get_terrain_sets_count(), Vector<Vector<Ref<Texture2D>>>());
  1418. LocalVector<int> bit_counts;
  1419. bit_counts.resize(get_terrains_count(terrain_set));
  1420. for (int terrain = 0; terrain < get_terrains_count(terrain_set); terrain++) {
  1421. bit_counts[terrain] = 0;
  1422. }
  1423. for (int terrain_bit = 0; terrain_bit < TileSet::CELL_NEIGHBOR_MAX; terrain_bit++) {
  1424. TileSet::CellNeighbor cell_neighbor = TileSet::CellNeighbor(terrain_bit);
  1425. if (is_valid_peering_bit_terrain(terrain_set, cell_neighbor)) {
  1426. int terrain = tile_data->get_peering_bit_terrain(cell_neighbor);
  1427. if (terrain >= 0) {
  1428. if (terrain >= (int)bit_counts.size()) {
  1429. WARN_PRINT(vformat("Invalid peering bit terrain: %d", terrain));
  1430. } else {
  1431. bit_counts[terrain] += 1;
  1432. }
  1433. }
  1434. }
  1435. }
  1436. for (int terrain = 0; terrain < get_terrains_count(terrain_set); terrain++) {
  1437. if ((bit_counts[terrain] > counts[terrain_set][terrain].count) || (bit_counts[terrain] == counts[terrain_set][terrain].count && tile_data->get_probability() > counts[terrain_set][terrain].probability)) {
  1438. counts[terrain_set][terrain].count = bit_counts[terrain];
  1439. counts[terrain_set][terrain].probability = tile_data->get_probability();
  1440. counts[terrain_set][terrain].texture = atlas_source->get_texture();
  1441. counts[terrain_set][terrain].region = atlas_source->get_tile_texture_region(tile_id);
  1442. }
  1443. }
  1444. }
  1445. }
  1446. }
  1447. }
  1448. }
  1449. // Generate the icons.
  1450. for (int terrain_set = 0; terrain_set < get_terrain_sets_count(); terrain_set++) {
  1451. for (int terrain = 0; terrain < get_terrains_count(terrain_set); terrain++) {
  1452. Ref<Image> image;
  1453. image.instantiate();
  1454. if (counts[terrain_set][terrain].count > 0) {
  1455. // Get the best tile.
  1456. Ref<Texture2D> texture = counts[terrain_set][terrain].texture;
  1457. Rect2 region = counts[terrain_set][terrain].region;
  1458. image->create(region.size.x, region.size.y, false, Image::FORMAT_RGBA8);
  1459. image->blit_rect(texture->get_image(), region, Point2());
  1460. image->resize(p_size.x, p_size.y, Image::INTERPOLATE_NEAREST);
  1461. } else {
  1462. image->create(1, 1, false, Image::FORMAT_RGBA8);
  1463. image->set_pixel(0, 0, get_terrain_color(terrain_set, terrain));
  1464. }
  1465. Ref<ImageTexture> icon;
  1466. icon.instantiate();
  1467. icon->create_from_image(image);
  1468. icon->set_size_override(p_size);
  1469. output.write[terrain_set].write[terrain] = icon;
  1470. }
  1471. }
  1472. return output;
  1473. }
  1474. void TileSet::_source_changed() {
  1475. terrains_cache_dirty = true;
  1476. emit_changed();
  1477. }
  1478. Vector<Point2> TileSet::_get_square_corner_or_side_terrain_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1479. Rect2 bit_rect;
  1480. bit_rect.size = Vector2(p_size) / 3;
  1481. switch (p_bit) {
  1482. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  1483. bit_rect.position = Vector2(1, -1);
  1484. break;
  1485. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1486. bit_rect.position = Vector2(1, 1);
  1487. break;
  1488. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  1489. bit_rect.position = Vector2(-1, 1);
  1490. break;
  1491. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1492. bit_rect.position = Vector2(-3, 1);
  1493. break;
  1494. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  1495. bit_rect.position = Vector2(-3, -1);
  1496. break;
  1497. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1498. bit_rect.position = Vector2(-3, -3);
  1499. break;
  1500. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  1501. bit_rect.position = Vector2(-1, -3);
  1502. break;
  1503. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1504. bit_rect.position = Vector2(1, -3);
  1505. break;
  1506. default:
  1507. break;
  1508. }
  1509. bit_rect.position *= Vector2(p_size) / 6.0;
  1510. Vector<Vector2> polygon = {
  1511. bit_rect.position,
  1512. Vector2(bit_rect.get_end().x, bit_rect.position.y),
  1513. bit_rect.get_end(),
  1514. Vector2(bit_rect.position.x, bit_rect.get_end().y)
  1515. };
  1516. return polygon;
  1517. }
  1518. Vector<Point2> TileSet::_get_square_corner_terrain_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1519. Vector2 unit = Vector2(p_size) / 6.0;
  1520. Vector<Vector2> polygon;
  1521. switch (p_bit) {
  1522. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1523. polygon.push_back(Vector2(0, 3) * unit);
  1524. polygon.push_back(Vector2(3, 3) * unit);
  1525. polygon.push_back(Vector2(3, 0) * unit);
  1526. polygon.push_back(Vector2(1, 0) * unit);
  1527. polygon.push_back(Vector2(1, 1) * unit);
  1528. polygon.push_back(Vector2(0, 1) * unit);
  1529. break;
  1530. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1531. polygon.push_back(Vector2(0, 3) * unit);
  1532. polygon.push_back(Vector2(-3, 3) * unit);
  1533. polygon.push_back(Vector2(-3, 0) * unit);
  1534. polygon.push_back(Vector2(-1, 0) * unit);
  1535. polygon.push_back(Vector2(-1, 1) * unit);
  1536. polygon.push_back(Vector2(0, 1) * unit);
  1537. break;
  1538. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1539. polygon.push_back(Vector2(0, -3) * unit);
  1540. polygon.push_back(Vector2(-3, -3) * unit);
  1541. polygon.push_back(Vector2(-3, 0) * unit);
  1542. polygon.push_back(Vector2(-1, 0) * unit);
  1543. polygon.push_back(Vector2(-1, -1) * unit);
  1544. polygon.push_back(Vector2(0, -1) * unit);
  1545. break;
  1546. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1547. polygon.push_back(Vector2(0, -3) * unit);
  1548. polygon.push_back(Vector2(3, -3) * unit);
  1549. polygon.push_back(Vector2(3, 0) * unit);
  1550. polygon.push_back(Vector2(1, 0) * unit);
  1551. polygon.push_back(Vector2(1, -1) * unit);
  1552. polygon.push_back(Vector2(0, -1) * unit);
  1553. break;
  1554. default:
  1555. break;
  1556. }
  1557. return polygon;
  1558. }
  1559. Vector<Point2> TileSet::_get_square_side_terrain_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1560. Vector2 unit = Vector2(p_size) / 6.0;
  1561. Vector<Vector2> polygon;
  1562. switch (p_bit) {
  1563. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  1564. polygon.push_back(Vector2(1, -1) * unit);
  1565. polygon.push_back(Vector2(3, -3) * unit);
  1566. polygon.push_back(Vector2(3, 3) * unit);
  1567. polygon.push_back(Vector2(1, 1) * unit);
  1568. break;
  1569. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  1570. polygon.push_back(Vector2(-1, 1) * unit);
  1571. polygon.push_back(Vector2(-3, 3) * unit);
  1572. polygon.push_back(Vector2(3, 3) * unit);
  1573. polygon.push_back(Vector2(1, 1) * unit);
  1574. break;
  1575. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  1576. polygon.push_back(Vector2(-1, -1) * unit);
  1577. polygon.push_back(Vector2(-3, -3) * unit);
  1578. polygon.push_back(Vector2(-3, 3) * unit);
  1579. polygon.push_back(Vector2(-1, 1) * unit);
  1580. break;
  1581. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  1582. polygon.push_back(Vector2(-1, -1) * unit);
  1583. polygon.push_back(Vector2(-3, -3) * unit);
  1584. polygon.push_back(Vector2(3, -3) * unit);
  1585. polygon.push_back(Vector2(1, -1) * unit);
  1586. break;
  1587. default:
  1588. break;
  1589. }
  1590. return polygon;
  1591. }
  1592. Vector<Point2> TileSet::_get_isometric_corner_or_side_terrain_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1593. Vector2 unit = Vector2(p_size) / 6.0;
  1594. Vector<Vector2> polygon;
  1595. switch (p_bit) {
  1596. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  1597. polygon.push_back(Vector2(1, 0) * unit);
  1598. polygon.push_back(Vector2(2, -1) * unit);
  1599. polygon.push_back(Vector2(3, 0) * unit);
  1600. polygon.push_back(Vector2(2, 1) * unit);
  1601. break;
  1602. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  1603. polygon.push_back(Vector2(0, 1) * unit);
  1604. polygon.push_back(Vector2(1, 2) * unit);
  1605. polygon.push_back(Vector2(2, 1) * unit);
  1606. polygon.push_back(Vector2(1, 0) * unit);
  1607. break;
  1608. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  1609. polygon.push_back(Vector2(0, 1) * unit);
  1610. polygon.push_back(Vector2(-1, 2) * unit);
  1611. polygon.push_back(Vector2(0, 3) * unit);
  1612. polygon.push_back(Vector2(1, 2) * unit);
  1613. break;
  1614. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  1615. polygon.push_back(Vector2(0, 1) * unit);
  1616. polygon.push_back(Vector2(-1, 2) * unit);
  1617. polygon.push_back(Vector2(-2, 1) * unit);
  1618. polygon.push_back(Vector2(-1, 0) * unit);
  1619. break;
  1620. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  1621. polygon.push_back(Vector2(-1, 0) * unit);
  1622. polygon.push_back(Vector2(-2, -1) * unit);
  1623. polygon.push_back(Vector2(-3, 0) * unit);
  1624. polygon.push_back(Vector2(-2, 1) * unit);
  1625. break;
  1626. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  1627. polygon.push_back(Vector2(0, -1) * unit);
  1628. polygon.push_back(Vector2(-1, -2) * unit);
  1629. polygon.push_back(Vector2(-2, -1) * unit);
  1630. polygon.push_back(Vector2(-1, 0) * unit);
  1631. break;
  1632. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  1633. polygon.push_back(Vector2(0, -1) * unit);
  1634. polygon.push_back(Vector2(-1, -2) * unit);
  1635. polygon.push_back(Vector2(0, -3) * unit);
  1636. polygon.push_back(Vector2(1, -2) * unit);
  1637. break;
  1638. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  1639. polygon.push_back(Vector2(0, -1) * unit);
  1640. polygon.push_back(Vector2(1, -2) * unit);
  1641. polygon.push_back(Vector2(2, -1) * unit);
  1642. polygon.push_back(Vector2(1, 0) * unit);
  1643. break;
  1644. default:
  1645. break;
  1646. }
  1647. return polygon;
  1648. }
  1649. Vector<Point2> TileSet::_get_isometric_corner_terrain_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1650. Vector2 unit = Vector2(p_size) / 6.0;
  1651. Vector<Vector2> polygon;
  1652. switch (p_bit) {
  1653. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  1654. polygon.push_back(Vector2(0.5, -0.5) * unit);
  1655. polygon.push_back(Vector2(1.5, -1.5) * unit);
  1656. polygon.push_back(Vector2(3, 0) * unit);
  1657. polygon.push_back(Vector2(1.5, 1.5) * unit);
  1658. polygon.push_back(Vector2(0.5, 0.5) * unit);
  1659. polygon.push_back(Vector2(1, 0) * unit);
  1660. break;
  1661. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  1662. polygon.push_back(Vector2(-0.5, 0.5) * unit);
  1663. polygon.push_back(Vector2(-1.5, 1.5) * unit);
  1664. polygon.push_back(Vector2(0, 3) * unit);
  1665. polygon.push_back(Vector2(1.5, 1.5) * unit);
  1666. polygon.push_back(Vector2(0.5, 0.5) * unit);
  1667. polygon.push_back(Vector2(0, 1) * unit);
  1668. break;
  1669. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  1670. polygon.push_back(Vector2(-0.5, -0.5) * unit);
  1671. polygon.push_back(Vector2(-1.5, -1.5) * unit);
  1672. polygon.push_back(Vector2(-3, 0) * unit);
  1673. polygon.push_back(Vector2(-1.5, 1.5) * unit);
  1674. polygon.push_back(Vector2(-0.5, 0.5) * unit);
  1675. polygon.push_back(Vector2(-1, 0) * unit);
  1676. break;
  1677. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  1678. polygon.push_back(Vector2(-0.5, -0.5) * unit);
  1679. polygon.push_back(Vector2(-1.5, -1.5) * unit);
  1680. polygon.push_back(Vector2(0, -3) * unit);
  1681. polygon.push_back(Vector2(1.5, -1.5) * unit);
  1682. polygon.push_back(Vector2(0.5, -0.5) * unit);
  1683. polygon.push_back(Vector2(0, -1) * unit);
  1684. break;
  1685. default:
  1686. break;
  1687. }
  1688. return polygon;
  1689. }
  1690. Vector<Point2> TileSet::_get_isometric_side_terrain_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1691. Vector2 unit = Vector2(p_size) / 6.0;
  1692. Vector<Vector2> polygon;
  1693. switch (p_bit) {
  1694. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  1695. polygon.push_back(Vector2(1, 0) * unit);
  1696. polygon.push_back(Vector2(3, 0) * unit);
  1697. polygon.push_back(Vector2(0, 3) * unit);
  1698. polygon.push_back(Vector2(0, 1) * unit);
  1699. break;
  1700. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  1701. polygon.push_back(Vector2(-1, 0) * unit);
  1702. polygon.push_back(Vector2(-3, 0) * unit);
  1703. polygon.push_back(Vector2(0, 3) * unit);
  1704. polygon.push_back(Vector2(0, 1) * unit);
  1705. break;
  1706. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  1707. polygon.push_back(Vector2(-1, 0) * unit);
  1708. polygon.push_back(Vector2(-3, 0) * unit);
  1709. polygon.push_back(Vector2(0, -3) * unit);
  1710. polygon.push_back(Vector2(0, -1) * unit);
  1711. break;
  1712. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  1713. polygon.push_back(Vector2(1, 0) * unit);
  1714. polygon.push_back(Vector2(3, 0) * unit);
  1715. polygon.push_back(Vector2(0, -3) * unit);
  1716. polygon.push_back(Vector2(0, -1) * unit);
  1717. break;
  1718. default:
  1719. break;
  1720. }
  1721. return polygon;
  1722. }
  1723. Vector<Point2> TileSet::_get_half_offset_corner_or_side_terrain_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit, float p_overlap, TileSet::TileOffsetAxis p_offset_axis) {
  1724. Vector<Vector2> point_list = {
  1725. Vector2(3, (3.0 * (1.0 - p_overlap * 2.0)) / 2.0),
  1726. Vector2(3, 3.0 * (1.0 - p_overlap * 2.0)),
  1727. Vector2(2, 3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  1728. Vector2(1, 3.0 - p_overlap * 2.0),
  1729. Vector2(0, 3),
  1730. Vector2(-1, 3.0 - p_overlap * 2.0),
  1731. Vector2(-2, 3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  1732. Vector2(-3, 3.0 * (1.0 - p_overlap * 2.0)),
  1733. Vector2(-3, (3.0 * (1.0 - p_overlap * 2.0)) / 2.0),
  1734. Vector2(-3, -(3.0 * (1.0 - p_overlap * 2.0)) / 2.0),
  1735. Vector2(-3, -3.0 * (1.0 - p_overlap * 2.0)),
  1736. Vector2(-2, -3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  1737. Vector2(-1, -(3.0 - p_overlap * 2.0)),
  1738. Vector2(0, -3),
  1739. Vector2(1, -(3.0 - p_overlap * 2.0)),
  1740. Vector2(2, -3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  1741. Vector2(3, -3.0 * (1.0 - p_overlap * 2.0)),
  1742. Vector2(3, -(3.0 * (1.0 - p_overlap * 2.0)) / 2.0)
  1743. };
  1744. Vector2 unit = Vector2(p_size) / 6.0;
  1745. for (int i = 0; i < point_list.size(); i++) {
  1746. point_list.write[i] = point_list[i] * unit;
  1747. }
  1748. Vector<Vector2> polygon;
  1749. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1750. switch (p_bit) {
  1751. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  1752. polygon.push_back(point_list[17]);
  1753. polygon.push_back(point_list[0]);
  1754. break;
  1755. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1756. polygon.push_back(point_list[0]);
  1757. polygon.push_back(point_list[1]);
  1758. polygon.push_back(point_list[2]);
  1759. break;
  1760. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  1761. polygon.push_back(point_list[2]);
  1762. polygon.push_back(point_list[3]);
  1763. break;
  1764. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  1765. polygon.push_back(point_list[3]);
  1766. polygon.push_back(point_list[4]);
  1767. polygon.push_back(point_list[5]);
  1768. break;
  1769. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  1770. polygon.push_back(point_list[5]);
  1771. polygon.push_back(point_list[6]);
  1772. break;
  1773. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1774. polygon.push_back(point_list[6]);
  1775. polygon.push_back(point_list[7]);
  1776. polygon.push_back(point_list[8]);
  1777. break;
  1778. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  1779. polygon.push_back(point_list[8]);
  1780. polygon.push_back(point_list[9]);
  1781. break;
  1782. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1783. polygon.push_back(point_list[9]);
  1784. polygon.push_back(point_list[10]);
  1785. polygon.push_back(point_list[11]);
  1786. break;
  1787. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  1788. polygon.push_back(point_list[11]);
  1789. polygon.push_back(point_list[12]);
  1790. break;
  1791. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  1792. polygon.push_back(point_list[12]);
  1793. polygon.push_back(point_list[13]);
  1794. polygon.push_back(point_list[14]);
  1795. break;
  1796. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  1797. polygon.push_back(point_list[14]);
  1798. polygon.push_back(point_list[15]);
  1799. break;
  1800. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1801. polygon.push_back(point_list[15]);
  1802. polygon.push_back(point_list[16]);
  1803. polygon.push_back(point_list[17]);
  1804. break;
  1805. default:
  1806. break;
  1807. }
  1808. } else {
  1809. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  1810. for (int i = 0; i < point_list.size(); i++) {
  1811. point_list.write[i] = Vector2(point_list[i].y, point_list[i].x);
  1812. }
  1813. }
  1814. switch (p_bit) {
  1815. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  1816. polygon.push_back(point_list[3]);
  1817. polygon.push_back(point_list[4]);
  1818. polygon.push_back(point_list[5]);
  1819. break;
  1820. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  1821. polygon.push_back(point_list[2]);
  1822. polygon.push_back(point_list[3]);
  1823. break;
  1824. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1825. polygon.push_back(point_list[0]);
  1826. polygon.push_back(point_list[1]);
  1827. polygon.push_back(point_list[2]);
  1828. break;
  1829. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  1830. polygon.push_back(point_list[17]);
  1831. polygon.push_back(point_list[0]);
  1832. break;
  1833. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1834. polygon.push_back(point_list[15]);
  1835. polygon.push_back(point_list[16]);
  1836. polygon.push_back(point_list[17]);
  1837. break;
  1838. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  1839. polygon.push_back(point_list[14]);
  1840. polygon.push_back(point_list[15]);
  1841. break;
  1842. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  1843. polygon.push_back(point_list[12]);
  1844. polygon.push_back(point_list[13]);
  1845. polygon.push_back(point_list[14]);
  1846. break;
  1847. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  1848. polygon.push_back(point_list[11]);
  1849. polygon.push_back(point_list[12]);
  1850. break;
  1851. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1852. polygon.push_back(point_list[9]);
  1853. polygon.push_back(point_list[10]);
  1854. polygon.push_back(point_list[11]);
  1855. break;
  1856. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  1857. polygon.push_back(point_list[8]);
  1858. polygon.push_back(point_list[9]);
  1859. break;
  1860. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1861. polygon.push_back(point_list[6]);
  1862. polygon.push_back(point_list[7]);
  1863. polygon.push_back(point_list[8]);
  1864. break;
  1865. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  1866. polygon.push_back(point_list[5]);
  1867. polygon.push_back(point_list[6]);
  1868. break;
  1869. default:
  1870. break;
  1871. }
  1872. }
  1873. int half_polygon_size = polygon.size();
  1874. for (int i = 0; i < half_polygon_size; i++) {
  1875. polygon.push_back(polygon[half_polygon_size - 1 - i] / 3.0);
  1876. }
  1877. return polygon;
  1878. }
  1879. Vector<Point2> TileSet::_get_half_offset_corner_terrain_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit, float p_overlap, TileSet::TileOffsetAxis p_offset_axis) {
  1880. Vector<Vector2> point_list = {
  1881. Vector2(3, 0),
  1882. Vector2(3, 3.0 * (1.0 - p_overlap * 2.0)),
  1883. Vector2(1.5, (3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  1884. Vector2(0, 3),
  1885. Vector2(-1.5, (3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  1886. Vector2(-3, 3.0 * (1.0 - p_overlap * 2.0)),
  1887. Vector2(-3, 0),
  1888. Vector2(-3, -3.0 * (1.0 - p_overlap * 2.0)),
  1889. Vector2(-1.5, -(3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  1890. Vector2(0, -3),
  1891. Vector2(1.5, -(3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  1892. Vector2(3, -3.0 * (1.0 - p_overlap * 2.0))
  1893. };
  1894. Vector2 unit = Vector2(p_size) / 6.0;
  1895. for (int i = 0; i < point_list.size(); i++) {
  1896. point_list.write[i] = point_list[i] * unit;
  1897. }
  1898. Vector<Vector2> polygon;
  1899. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1900. switch (p_bit) {
  1901. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1902. polygon.push_back(point_list[0]);
  1903. polygon.push_back(point_list[1]);
  1904. polygon.push_back(point_list[2]);
  1905. break;
  1906. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  1907. polygon.push_back(point_list[2]);
  1908. polygon.push_back(point_list[3]);
  1909. polygon.push_back(point_list[4]);
  1910. break;
  1911. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1912. polygon.push_back(point_list[4]);
  1913. polygon.push_back(point_list[5]);
  1914. polygon.push_back(point_list[6]);
  1915. break;
  1916. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1917. polygon.push_back(point_list[6]);
  1918. polygon.push_back(point_list[7]);
  1919. polygon.push_back(point_list[8]);
  1920. break;
  1921. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  1922. polygon.push_back(point_list[8]);
  1923. polygon.push_back(point_list[9]);
  1924. polygon.push_back(point_list[10]);
  1925. break;
  1926. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1927. polygon.push_back(point_list[10]);
  1928. polygon.push_back(point_list[11]);
  1929. polygon.push_back(point_list[0]);
  1930. break;
  1931. default:
  1932. break;
  1933. }
  1934. } else {
  1935. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  1936. for (int i = 0; i < point_list.size(); i++) {
  1937. point_list.write[i] = Vector2(point_list[i].y, point_list[i].x);
  1938. }
  1939. }
  1940. switch (p_bit) {
  1941. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  1942. polygon.push_back(point_list[2]);
  1943. polygon.push_back(point_list[3]);
  1944. polygon.push_back(point_list[4]);
  1945. break;
  1946. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1947. polygon.push_back(point_list[0]);
  1948. polygon.push_back(point_list[1]);
  1949. polygon.push_back(point_list[2]);
  1950. break;
  1951. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1952. polygon.push_back(point_list[10]);
  1953. polygon.push_back(point_list[11]);
  1954. polygon.push_back(point_list[0]);
  1955. break;
  1956. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  1957. polygon.push_back(point_list[8]);
  1958. polygon.push_back(point_list[9]);
  1959. polygon.push_back(point_list[10]);
  1960. break;
  1961. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1962. polygon.push_back(point_list[6]);
  1963. polygon.push_back(point_list[7]);
  1964. polygon.push_back(point_list[8]);
  1965. break;
  1966. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1967. polygon.push_back(point_list[4]);
  1968. polygon.push_back(point_list[5]);
  1969. polygon.push_back(point_list[6]);
  1970. break;
  1971. default:
  1972. break;
  1973. }
  1974. }
  1975. int half_polygon_size = polygon.size();
  1976. for (int i = 0; i < half_polygon_size; i++) {
  1977. polygon.push_back(polygon[half_polygon_size - 1 - i] / 3.0);
  1978. }
  1979. return polygon;
  1980. }
  1981. Vector<Point2> TileSet::_get_half_offset_side_terrain_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit, float p_overlap, TileSet::TileOffsetAxis p_offset_axis) {
  1982. Vector<Vector2> point_list = {
  1983. Vector2(3, 3.0 * (1.0 - p_overlap * 2.0)),
  1984. Vector2(0, 3),
  1985. Vector2(-3, 3.0 * (1.0 - p_overlap * 2.0)),
  1986. Vector2(-3, -3.0 * (1.0 - p_overlap * 2.0)),
  1987. Vector2(0, -3),
  1988. Vector2(3, -3.0 * (1.0 - p_overlap * 2.0))
  1989. };
  1990. Vector2 unit = Vector2(p_size) / 6.0;
  1991. for (int i = 0; i < point_list.size(); i++) {
  1992. point_list.write[i] = point_list[i] * unit;
  1993. }
  1994. Vector<Vector2> polygon;
  1995. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1996. switch (p_bit) {
  1997. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  1998. polygon.push_back(point_list[5]);
  1999. polygon.push_back(point_list[0]);
  2000. break;
  2001. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2002. polygon.push_back(point_list[0]);
  2003. polygon.push_back(point_list[1]);
  2004. break;
  2005. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2006. polygon.push_back(point_list[1]);
  2007. polygon.push_back(point_list[2]);
  2008. break;
  2009. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2010. polygon.push_back(point_list[2]);
  2011. polygon.push_back(point_list[3]);
  2012. break;
  2013. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2014. polygon.push_back(point_list[3]);
  2015. polygon.push_back(point_list[4]);
  2016. break;
  2017. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2018. polygon.push_back(point_list[4]);
  2019. polygon.push_back(point_list[5]);
  2020. break;
  2021. default:
  2022. break;
  2023. }
  2024. } else {
  2025. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  2026. for (int i = 0; i < point_list.size(); i++) {
  2027. point_list.write[i] = Vector2(point_list[i].y, point_list[i].x);
  2028. }
  2029. }
  2030. switch (p_bit) {
  2031. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2032. polygon.push_back(point_list[0]);
  2033. polygon.push_back(point_list[1]);
  2034. break;
  2035. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2036. polygon.push_back(point_list[5]);
  2037. polygon.push_back(point_list[0]);
  2038. break;
  2039. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2040. polygon.push_back(point_list[4]);
  2041. polygon.push_back(point_list[5]);
  2042. break;
  2043. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2044. polygon.push_back(point_list[3]);
  2045. polygon.push_back(point_list[4]);
  2046. break;
  2047. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2048. polygon.push_back(point_list[2]);
  2049. polygon.push_back(point_list[3]);
  2050. break;
  2051. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2052. polygon.push_back(point_list[1]);
  2053. polygon.push_back(point_list[2]);
  2054. break;
  2055. default:
  2056. break;
  2057. }
  2058. }
  2059. int half_polygon_size = polygon.size();
  2060. for (int i = 0; i < half_polygon_size; i++) {
  2061. polygon.push_back(polygon[half_polygon_size - 1 - i] / 3.0);
  2062. }
  2063. return polygon;
  2064. }
  2065. void TileSet::reset_state() {
  2066. occlusion_layers.clear();
  2067. physics_layers.clear();
  2068. custom_data_layers.clear();
  2069. }
  2070. const Vector2i TileSetSource::INVALID_ATLAS_COORDS = Vector2i(-1, -1);
  2071. const int TileSetSource::INVALID_TILE_ALTERNATIVE = -1;
  2072. #ifndef DISABLE_DEPRECATED
  2073. void TileSet::_compatibility_conversion() {
  2074. for (KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  2075. CompatibilityTileData *ctd = E.value;
  2076. // Add the texture
  2077. TileSetAtlasSource *atlas_source = memnew(TileSetAtlasSource);
  2078. int source_id = add_source(Ref<TileSetSource>(atlas_source));
  2079. atlas_source->set_texture(ctd->texture);
  2080. // Handle each tile as a new source. Not optimal but at least it should stay compatible.
  2081. switch (ctd->tile_mode) {
  2082. case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
  2083. atlas_source->set_margins(ctd->region.get_position());
  2084. atlas_source->set_texture_region_size(ctd->region.get_size());
  2085. Vector2i coords;
  2086. for (int flags = 0; flags < 8; flags++) {
  2087. bool flip_h = flags & 1;
  2088. bool flip_v = flags & 2;
  2089. bool transpose = flags & 4;
  2090. int alternative_tile = 0;
  2091. if (!atlas_source->has_tile(coords)) {
  2092. atlas_source->create_tile(coords);
  2093. } else {
  2094. alternative_tile = atlas_source->create_alternative_tile(coords);
  2095. }
  2096. // Add to the mapping.
  2097. Array key_array;
  2098. key_array.push_back(flip_h);
  2099. key_array.push_back(flip_v);
  2100. key_array.push_back(transpose);
  2101. Array value_array;
  2102. value_array.push_back(source_id);
  2103. value_array.push_back(coords);
  2104. value_array.push_back(alternative_tile);
  2105. if (!compatibility_tilemap_mapping.has(E.key)) {
  2106. compatibility_tilemap_mapping[E.key] = RBMap<Array, Array>();
  2107. }
  2108. compatibility_tilemap_mapping[E.key][key_array] = value_array;
  2109. compatibility_tilemap_mapping_tile_modes[E.key] = COMPATIBILITY_TILE_MODE_SINGLE_TILE;
  2110. TileData *tile_data = atlas_source->get_tile_data(coords, alternative_tile);
  2111. tile_data->set_flip_h(flip_h);
  2112. tile_data->set_flip_v(flip_v);
  2113. tile_data->set_transpose(transpose);
  2114. tile_data->set_material(ctd->material);
  2115. tile_data->set_modulate(ctd->modulate);
  2116. tile_data->set_z_index(ctd->z_index);
  2117. if (ctd->occluder.is_valid()) {
  2118. if (get_occlusion_layers_count() < 1) {
  2119. add_occlusion_layer();
  2120. }
  2121. tile_data->set_occluder(0, ctd->occluder);
  2122. }
  2123. if (ctd->navigation.is_valid()) {
  2124. if (get_navigation_layers_count() < 1) {
  2125. add_navigation_layer();
  2126. }
  2127. tile_data->set_navigation_polygon(0, ctd->autotile_navpoly_map[coords]);
  2128. }
  2129. tile_data->set_z_index(ctd->z_index);
  2130. // Add the shapes.
  2131. if (ctd->shapes.size() > 0) {
  2132. if (get_physics_layers_count() < 1) {
  2133. add_physics_layer();
  2134. }
  2135. }
  2136. for (int k = 0; k < ctd->shapes.size(); k++) {
  2137. CompatibilityShapeData csd = ctd->shapes[k];
  2138. if (csd.autotile_coords == coords) {
  2139. Ref<ConvexPolygonShape2D> convex_shape = csd.shape; // Only ConvexPolygonShape2D are supported, which is the default type used by the 3.x editor
  2140. if (convex_shape.is_valid()) {
  2141. Vector<Vector2> polygon = convex_shape->get_points();
  2142. for (int point_index = 0; point_index < polygon.size(); point_index++) {
  2143. polygon.write[point_index] = csd.transform.xform(polygon[point_index]);
  2144. }
  2145. tile_data->set_collision_polygons_count(0, tile_data->get_collision_polygons_count(0) + 1);
  2146. int index = tile_data->get_collision_polygons_count(0) - 1;
  2147. tile_data->set_collision_polygon_one_way(0, index, csd.one_way);
  2148. tile_data->set_collision_polygon_one_way_margin(0, index, csd.one_way_margin);
  2149. tile_data->set_collision_polygon_points(0, index, polygon);
  2150. }
  2151. }
  2152. }
  2153. }
  2154. } break;
  2155. case COMPATIBILITY_TILE_MODE_AUTO_TILE: {
  2156. // Not supported. It would need manual conversion.
  2157. } break;
  2158. case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
  2159. atlas_source->set_margins(ctd->region.get_position());
  2160. atlas_source->set_separation(Vector2i(ctd->autotile_spacing, ctd->autotile_spacing));
  2161. atlas_source->set_texture_region_size(ctd->autotile_tile_size);
  2162. Size2i atlas_size = ctd->region.get_size() / (ctd->autotile_tile_size + atlas_source->get_separation());
  2163. for (int i = 0; i < atlas_size.x; i++) {
  2164. for (int j = 0; j < atlas_size.y; j++) {
  2165. Vector2i coords = Vector2i(i, j);
  2166. for (int flags = 0; flags < 8; flags++) {
  2167. bool flip_h = flags & 1;
  2168. bool flip_v = flags & 2;
  2169. bool transpose = flags & 4;
  2170. int alternative_tile = 0;
  2171. if (!atlas_source->has_tile(coords)) {
  2172. atlas_source->create_tile(coords);
  2173. } else {
  2174. alternative_tile = atlas_source->create_alternative_tile(coords);
  2175. }
  2176. // Add to the mapping.
  2177. Array key_array;
  2178. key_array.push_back(coords);
  2179. key_array.push_back(flip_h);
  2180. key_array.push_back(flip_v);
  2181. key_array.push_back(transpose);
  2182. Array value_array;
  2183. value_array.push_back(source_id);
  2184. value_array.push_back(coords);
  2185. value_array.push_back(alternative_tile);
  2186. if (!compatibility_tilemap_mapping.has(E.key)) {
  2187. compatibility_tilemap_mapping[E.key] = RBMap<Array, Array>();
  2188. }
  2189. compatibility_tilemap_mapping[E.key][key_array] = value_array;
  2190. compatibility_tilemap_mapping_tile_modes[E.key] = COMPATIBILITY_TILE_MODE_ATLAS_TILE;
  2191. TileData *tile_data = atlas_source->get_tile_data(coords, alternative_tile);
  2192. tile_data->set_flip_h(flip_h);
  2193. tile_data->set_flip_v(flip_v);
  2194. tile_data->set_transpose(transpose);
  2195. tile_data->set_material(ctd->material);
  2196. tile_data->set_modulate(ctd->modulate);
  2197. tile_data->set_z_index(ctd->z_index);
  2198. if (ctd->autotile_occluder_map.has(coords)) {
  2199. if (get_occlusion_layers_count() < 1) {
  2200. add_occlusion_layer();
  2201. }
  2202. tile_data->set_occluder(0, ctd->autotile_occluder_map[coords]);
  2203. }
  2204. if (ctd->autotile_navpoly_map.has(coords)) {
  2205. if (get_navigation_layers_count() < 1) {
  2206. add_navigation_layer();
  2207. }
  2208. tile_data->set_navigation_polygon(0, ctd->autotile_navpoly_map[coords]);
  2209. }
  2210. if (ctd->autotile_priority_map.has(coords)) {
  2211. tile_data->set_probability(ctd->autotile_priority_map[coords]);
  2212. }
  2213. if (ctd->autotile_z_index_map.has(coords)) {
  2214. tile_data->set_z_index(ctd->autotile_z_index_map[coords]);
  2215. }
  2216. // Add the shapes.
  2217. if (ctd->shapes.size() > 0) {
  2218. if (get_physics_layers_count() < 1) {
  2219. add_physics_layer();
  2220. }
  2221. }
  2222. for (int k = 0; k < ctd->shapes.size(); k++) {
  2223. CompatibilityShapeData csd = ctd->shapes[k];
  2224. if (csd.autotile_coords == coords) {
  2225. Ref<ConvexPolygonShape2D> convex_shape = csd.shape; // Only ConvexPolygonShape2D are supported, which is the default type used by the 3.x editor
  2226. if (convex_shape.is_valid()) {
  2227. Vector<Vector2> polygon = convex_shape->get_points();
  2228. for (int point_index = 0; point_index < polygon.size(); point_index++) {
  2229. polygon.write[point_index] = csd.transform.xform(polygon[point_index]);
  2230. }
  2231. tile_data->set_collision_polygons_count(0, tile_data->get_collision_polygons_count(0) + 1);
  2232. int index = tile_data->get_collision_polygons_count(0) - 1;
  2233. tile_data->set_collision_polygon_one_way(0, index, csd.one_way);
  2234. tile_data->set_collision_polygon_one_way_margin(0, index, csd.one_way_margin);
  2235. tile_data->set_collision_polygon_points(0, index, polygon);
  2236. }
  2237. }
  2238. }
  2239. // -- TODO: handle --
  2240. // Those are offset for the whole atlas, they are likely useless for the atlases, but might make sense for single tiles.
  2241. // texture offset
  2242. // occluder_offset
  2243. // navigation_offset
  2244. // For terrains, ignored for now?
  2245. // bitmask_mode
  2246. // bitmask_flags
  2247. }
  2248. }
  2249. }
  2250. } break;
  2251. }
  2252. // Offset all shapes
  2253. for (int k = 0; k < ctd->shapes.size(); k++) {
  2254. Ref<ConvexPolygonShape2D> convex = ctd->shapes[k].shape;
  2255. if (convex.is_valid()) {
  2256. Vector<Vector2> points = convex->get_points();
  2257. for (int i_point = 0; i_point < points.size(); i_point++) {
  2258. points.write[i_point] = points[i_point] - get_tile_size() / 2;
  2259. }
  2260. convex->set_points(points);
  2261. }
  2262. }
  2263. }
  2264. // Reset compatibility data
  2265. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  2266. memdelete(E.value);
  2267. }
  2268. compatibility_data = HashMap<int, CompatibilityTileData *>();
  2269. }
  2270. Array TileSet::compatibility_tilemap_map(int p_tile_id, Vector2i p_coords, bool p_flip_h, bool p_flip_v, bool p_transpose) {
  2271. Array cannot_convert_array;
  2272. cannot_convert_array.push_back(TileSet::INVALID_SOURCE);
  2273. cannot_convert_array.push_back(TileSetAtlasSource::INVALID_ATLAS_COORDS);
  2274. cannot_convert_array.push_back(TileSetAtlasSource::INVALID_TILE_ALTERNATIVE);
  2275. if (!compatibility_tilemap_mapping.has(p_tile_id)) {
  2276. return cannot_convert_array;
  2277. }
  2278. int tile_mode = compatibility_tilemap_mapping_tile_modes[p_tile_id];
  2279. switch (tile_mode) {
  2280. case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
  2281. Array a;
  2282. a.push_back(p_flip_h);
  2283. a.push_back(p_flip_v);
  2284. a.push_back(p_transpose);
  2285. return compatibility_tilemap_mapping[p_tile_id][a];
  2286. }
  2287. case COMPATIBILITY_TILE_MODE_AUTO_TILE:
  2288. return cannot_convert_array;
  2289. break;
  2290. case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
  2291. Array a;
  2292. a.push_back(p_coords);
  2293. a.push_back(p_flip_h);
  2294. a.push_back(p_flip_v);
  2295. a.push_back(p_transpose);
  2296. return compatibility_tilemap_mapping[p_tile_id][a];
  2297. }
  2298. default:
  2299. return cannot_convert_array;
  2300. break;
  2301. }
  2302. };
  2303. #endif // DISABLE_DEPRECATED
  2304. bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
  2305. Vector<String> components = String(p_name).split("/", true, 2);
  2306. #ifndef DISABLE_DEPRECATED
  2307. // TODO: This should be moved to a dedicated conversion system (see #50691)
  2308. if (components.size() >= 1 && components[0].is_valid_int()) {
  2309. int id = components[0].to_int();
  2310. // Get or create the compatibility object
  2311. CompatibilityTileData *ctd;
  2312. HashMap<int, CompatibilityTileData *>::Iterator E = compatibility_data.find(id);
  2313. if (!E) {
  2314. ctd = memnew(CompatibilityTileData);
  2315. compatibility_data.insert(id, ctd);
  2316. } else {
  2317. ctd = E->value;
  2318. }
  2319. if (components.size() < 2) {
  2320. return false;
  2321. }
  2322. String what = components[1];
  2323. if (what == "name") {
  2324. ctd->name = p_value;
  2325. } else if (what == "texture") {
  2326. ctd->texture = p_value;
  2327. } else if (what == "tex_offset") {
  2328. ctd->tex_offset = p_value;
  2329. } else if (what == "material") {
  2330. ctd->material = p_value;
  2331. } else if (what == "modulate") {
  2332. ctd->modulate = p_value;
  2333. } else if (what == "region") {
  2334. ctd->region = p_value;
  2335. } else if (what == "tile_mode") {
  2336. ctd->tile_mode = p_value;
  2337. } else if (what.left(9) == "autotile") {
  2338. what = what.substr(9);
  2339. if (what == "bitmask_mode") {
  2340. ctd->autotile_bitmask_mode = p_value;
  2341. } else if (what == "icon_coordinate") {
  2342. ctd->autotile_icon_coordinate = p_value;
  2343. } else if (what == "tile_size") {
  2344. ctd->autotile_tile_size = p_value;
  2345. } else if (what == "spacing") {
  2346. ctd->autotile_spacing = p_value;
  2347. } else if (what == "bitmask_flags") {
  2348. if (p_value.is_array()) {
  2349. Array p = p_value;
  2350. Vector2i last_coord;
  2351. while (p.size() > 0) {
  2352. if (p[0].get_type() == Variant::VECTOR2) {
  2353. last_coord = p[0];
  2354. } else if (p[0].get_type() == Variant::INT) {
  2355. ctd->autotile_bitmask_flags.insert(last_coord, p[0]);
  2356. }
  2357. p.pop_front();
  2358. }
  2359. }
  2360. } else if (what == "occluder_map") {
  2361. Array p = p_value;
  2362. Vector2 last_coord;
  2363. while (p.size() > 0) {
  2364. if (p[0].get_type() == Variant::VECTOR2) {
  2365. last_coord = p[0];
  2366. } else if (p[0].get_type() == Variant::OBJECT) {
  2367. ctd->autotile_occluder_map.insert(last_coord, p[0]);
  2368. }
  2369. p.pop_front();
  2370. }
  2371. } else if (what == "navpoly_map") {
  2372. Array p = p_value;
  2373. Vector2 last_coord;
  2374. while (p.size() > 0) {
  2375. if (p[0].get_type() == Variant::VECTOR2) {
  2376. last_coord = p[0];
  2377. } else if (p[0].get_type() == Variant::OBJECT) {
  2378. ctd->autotile_navpoly_map.insert(last_coord, p[0]);
  2379. }
  2380. p.pop_front();
  2381. }
  2382. } else if (what == "priority_map") {
  2383. Array p = p_value;
  2384. Vector3 val;
  2385. Vector2 v;
  2386. int priority;
  2387. while (p.size() > 0) {
  2388. val = p[0];
  2389. if (val.z > 1) {
  2390. v.x = val.x;
  2391. v.y = val.y;
  2392. priority = (int)val.z;
  2393. ctd->autotile_priority_map.insert(v, priority);
  2394. }
  2395. p.pop_front();
  2396. }
  2397. } else if (what == "z_index_map") {
  2398. Array p = p_value;
  2399. Vector3 val;
  2400. Vector2 v;
  2401. int z_index;
  2402. while (p.size() > 0) {
  2403. val = p[0];
  2404. if (val.z != 0) {
  2405. v.x = val.x;
  2406. v.y = val.y;
  2407. z_index = (int)val.z;
  2408. ctd->autotile_z_index_map.insert(v, z_index);
  2409. }
  2410. p.pop_front();
  2411. }
  2412. }
  2413. } else if (what == "shapes") {
  2414. Array p = p_value;
  2415. for (int i = 0; i < p.size(); i++) {
  2416. CompatibilityShapeData csd;
  2417. Dictionary d = p[i];
  2418. for (int j = 0; j < d.size(); j++) {
  2419. String key = d.get_key_at_index(j);
  2420. if (key == "autotile_coord") {
  2421. csd.autotile_coords = d[key];
  2422. } else if (key == "one_way") {
  2423. csd.one_way = d[key];
  2424. } else if (key == "one_way_margin") {
  2425. csd.one_way_margin = d[key];
  2426. } else if (key == "shape") {
  2427. csd.shape = d[key];
  2428. } else if (key == "shape_transform") {
  2429. csd.transform = d[key];
  2430. }
  2431. }
  2432. ctd->shapes.push_back(csd);
  2433. }
  2434. /*
  2435. // IGNORED FOR NOW, they seem duplicated data compared to the shapes array
  2436. } else if (what == "shape") {
  2437. } else if (what == "shape_offset") {
  2438. } else if (what == "shape_transform") {
  2439. } else if (what == "shape_one_way") {
  2440. } else if (what == "shape_one_way_margin") {
  2441. }
  2442. // IGNORED FOR NOW, maybe useless ?
  2443. else if (what == "occluder_offset") {
  2444. // Not
  2445. } else if (what == "navigation_offset") {
  2446. }
  2447. */
  2448. } else if (what == "z_index") {
  2449. ctd->z_index = p_value;
  2450. // TODO: remove the conversion from here, it's not where it should be done (see #50691)
  2451. _compatibility_conversion();
  2452. } else {
  2453. return false;
  2454. }
  2455. } else {
  2456. #endif // DISABLE_DEPRECATED
  2457. // This is now a new property.
  2458. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  2459. // Occlusion layers.
  2460. int index = components[0].trim_prefix("occlusion_layer_").to_int();
  2461. ERR_FAIL_COND_V(index < 0, false);
  2462. if (components[1] == "light_mask") {
  2463. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2464. while (index >= occlusion_layers.size()) {
  2465. add_occlusion_layer();
  2466. }
  2467. set_occlusion_layer_light_mask(index, p_value);
  2468. return true;
  2469. } else if (components[1] == "sdf_collision") {
  2470. ERR_FAIL_COND_V(p_value.get_type() != Variant::BOOL, false);
  2471. while (index >= occlusion_layers.size()) {
  2472. add_occlusion_layer();
  2473. }
  2474. set_occlusion_layer_sdf_collision(index, p_value);
  2475. return true;
  2476. }
  2477. } else if (components.size() == 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  2478. // Physics layers.
  2479. int index = components[0].trim_prefix("physics_layer_").to_int();
  2480. ERR_FAIL_COND_V(index < 0, false);
  2481. if (components[1] == "collision_layer") {
  2482. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2483. while (index >= physics_layers.size()) {
  2484. add_physics_layer();
  2485. }
  2486. set_physics_layer_collision_layer(index, p_value);
  2487. return true;
  2488. } else if (components[1] == "collision_mask") {
  2489. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2490. while (index >= physics_layers.size()) {
  2491. add_physics_layer();
  2492. }
  2493. set_physics_layer_collision_mask(index, p_value);
  2494. return true;
  2495. } else if (components[1] == "physics_material") {
  2496. Ref<PhysicsMaterial> physics_material = p_value;
  2497. while (index >= physics_layers.size()) {
  2498. add_physics_layer();
  2499. }
  2500. set_physics_layer_physics_material(index, physics_material);
  2501. return true;
  2502. }
  2503. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int()) {
  2504. // Terrains.
  2505. int terrain_set_index = components[0].trim_prefix("terrain_set_").to_int();
  2506. ERR_FAIL_COND_V(terrain_set_index < 0, false);
  2507. if (components[1] == "mode") {
  2508. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2509. while (terrain_set_index >= terrain_sets.size()) {
  2510. add_terrain_set();
  2511. }
  2512. set_terrain_set_mode(terrain_set_index, TerrainMode(int(p_value)));
  2513. } else if (components.size() >= 3 && components[1].begins_with("terrain_") && components[1].trim_prefix("terrain_").is_valid_int()) {
  2514. int terrain_index = components[1].trim_prefix("terrain_").to_int();
  2515. ERR_FAIL_COND_V(terrain_index < 0, false);
  2516. if (components[2] == "name") {
  2517. ERR_FAIL_COND_V(p_value.get_type() != Variant::STRING, false);
  2518. while (terrain_set_index >= terrain_sets.size()) {
  2519. add_terrain_set();
  2520. }
  2521. while (terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  2522. add_terrain(terrain_set_index);
  2523. }
  2524. set_terrain_name(terrain_set_index, terrain_index, p_value);
  2525. return true;
  2526. } else if (components[2] == "color") {
  2527. ERR_FAIL_COND_V(p_value.get_type() != Variant::COLOR, false);
  2528. while (terrain_set_index >= terrain_sets.size()) {
  2529. add_terrain_set();
  2530. }
  2531. while (terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  2532. add_terrain(terrain_set_index);
  2533. }
  2534. set_terrain_color(terrain_set_index, terrain_index, p_value);
  2535. return true;
  2536. }
  2537. }
  2538. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  2539. // Navigation layers.
  2540. int index = components[0].trim_prefix("navigation_layer_").to_int();
  2541. ERR_FAIL_COND_V(index < 0, false);
  2542. if (components[1] == "layers") {
  2543. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2544. while (index >= navigation_layers.size()) {
  2545. add_navigation_layer();
  2546. }
  2547. set_navigation_layer_layers(index, p_value);
  2548. return true;
  2549. }
  2550. } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int()) {
  2551. // Custom data layers.
  2552. int index = components[0].trim_prefix("custom_data_layer_").to_int();
  2553. ERR_FAIL_COND_V(index < 0, false);
  2554. if (components[1] == "name") {
  2555. ERR_FAIL_COND_V(p_value.get_type() != Variant::STRING, false);
  2556. while (index >= custom_data_layers.size()) {
  2557. add_custom_data_layer();
  2558. }
  2559. set_custom_data_name(index, p_value);
  2560. return true;
  2561. } else if (components[1] == "type") {
  2562. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2563. while (index >= custom_data_layers.size()) {
  2564. add_custom_data_layer();
  2565. }
  2566. set_custom_data_type(index, Variant::Type(int(p_value)));
  2567. return true;
  2568. }
  2569. } else if (components.size() == 2 && components[0] == "sources" && components[1].is_valid_int()) {
  2570. // Create source only if it does not exists.
  2571. int source_id = components[1].to_int();
  2572. if (!has_source(source_id)) {
  2573. add_source(p_value, source_id);
  2574. }
  2575. return true;
  2576. } else if (components.size() == 2 && components[0] == "tile_proxies") {
  2577. ERR_FAIL_COND_V(p_value.get_type() != Variant::ARRAY, false);
  2578. Array a = p_value;
  2579. ERR_FAIL_COND_V(a.size() % 2 != 0, false);
  2580. if (components[1] == "source_level") {
  2581. for (int i = 0; i < a.size(); i += 2) {
  2582. set_source_level_tile_proxy(a[i], a[i + 1]);
  2583. }
  2584. return true;
  2585. } else if (components[1] == "coords_level") {
  2586. for (int i = 0; i < a.size(); i += 2) {
  2587. Array key = a[i];
  2588. Array value = a[i + 1];
  2589. set_coords_level_tile_proxy(key[0], key[1], value[0], value[1]);
  2590. }
  2591. return true;
  2592. } else if (components[1] == "alternative_level") {
  2593. for (int i = 0; i < a.size(); i += 2) {
  2594. Array key = a[i];
  2595. Array value = a[i + 1];
  2596. set_alternative_level_tile_proxy(key[0], key[1], key[2], value[0], value[1], value[2]);
  2597. }
  2598. return true;
  2599. }
  2600. return false;
  2601. } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) {
  2602. int pattern_index = components[0].trim_prefix("pattern_").to_int();
  2603. for (int i = patterns.size(); i <= pattern_index; i++) {
  2604. add_pattern(p_value);
  2605. }
  2606. return true;
  2607. }
  2608. #ifndef DISABLE_DEPRECATED
  2609. }
  2610. #endif // DISABLE_DEPRECATED
  2611. return false;
  2612. }
  2613. bool TileSet::_get(const StringName &p_name, Variant &r_ret) const {
  2614. Vector<String> components = String(p_name).split("/", true, 2);
  2615. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  2616. // Occlusion layers.
  2617. int index = components[0].trim_prefix("occlusion_layer_").to_int();
  2618. if (index < 0 || index >= occlusion_layers.size()) {
  2619. return false;
  2620. }
  2621. if (components[1] == "light_mask") {
  2622. r_ret = get_occlusion_layer_light_mask(index);
  2623. return true;
  2624. } else if (components[1] == "sdf_collision") {
  2625. r_ret = get_occlusion_layer_sdf_collision(index);
  2626. return true;
  2627. }
  2628. } else if (components.size() == 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  2629. // Physics layers.
  2630. int index = components[0].trim_prefix("physics_layer_").to_int();
  2631. if (index < 0 || index >= physics_layers.size()) {
  2632. return false;
  2633. }
  2634. if (components[1] == "collision_layer") {
  2635. r_ret = get_physics_layer_collision_layer(index);
  2636. return true;
  2637. } else if (components[1] == "collision_mask") {
  2638. r_ret = get_physics_layer_collision_mask(index);
  2639. return true;
  2640. } else if (components[1] == "physics_material") {
  2641. r_ret = get_physics_layer_physics_material(index);
  2642. return true;
  2643. }
  2644. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int()) {
  2645. // Terrains.
  2646. int terrain_set_index = components[0].trim_prefix("terrain_set_").to_int();
  2647. if (terrain_set_index < 0 || terrain_set_index >= terrain_sets.size()) {
  2648. return false;
  2649. }
  2650. if (components[1] == "mode") {
  2651. r_ret = get_terrain_set_mode(terrain_set_index);
  2652. return true;
  2653. } else if (components.size() >= 3 && components[1].begins_with("terrain_") && components[1].trim_prefix("terrain_").is_valid_int()) {
  2654. int terrain_index = components[1].trim_prefix("terrain_").to_int();
  2655. if (terrain_index < 0 || terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  2656. return false;
  2657. }
  2658. if (components[2] == "name") {
  2659. r_ret = get_terrain_name(terrain_set_index, terrain_index);
  2660. return true;
  2661. } else if (components[2] == "color") {
  2662. r_ret = get_terrain_color(terrain_set_index, terrain_index);
  2663. return true;
  2664. }
  2665. }
  2666. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  2667. // navigation layers.
  2668. int index = components[0].trim_prefix("navigation_layer_").to_int();
  2669. if (index < 0 || index >= navigation_layers.size()) {
  2670. return false;
  2671. }
  2672. if (components[1] == "layers") {
  2673. r_ret = get_navigation_layer_layers(index);
  2674. return true;
  2675. }
  2676. } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int()) {
  2677. // Custom data layers.
  2678. int index = components[0].trim_prefix("custom_data_layer_").to_int();
  2679. if (index < 0 || index >= custom_data_layers.size()) {
  2680. return false;
  2681. }
  2682. if (components[1] == "name") {
  2683. r_ret = get_custom_data_name(index);
  2684. return true;
  2685. } else if (components[1] == "type") {
  2686. r_ret = get_custom_data_type(index);
  2687. return true;
  2688. }
  2689. } else if (components.size() == 2 && components[0] == "sources" && components[1].is_valid_int()) {
  2690. // Atlases data.
  2691. int source_id = components[1].to_int();
  2692. if (has_source(source_id)) {
  2693. r_ret = get_source(source_id);
  2694. return true;
  2695. } else {
  2696. return false;
  2697. }
  2698. } else if (components.size() == 2 && components[0] == "tile_proxies") {
  2699. if (components[1] == "source_level") {
  2700. Array a;
  2701. for (const KeyValue<int, int> &E : source_level_proxies) {
  2702. a.push_back(E.key);
  2703. a.push_back(E.value);
  2704. }
  2705. r_ret = a;
  2706. return true;
  2707. } else if (components[1] == "coords_level") {
  2708. Array a;
  2709. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  2710. a.push_back(E.key);
  2711. a.push_back(E.value);
  2712. }
  2713. r_ret = a;
  2714. return true;
  2715. } else if (components[1] == "alternative_level") {
  2716. Array a;
  2717. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  2718. a.push_back(E.key);
  2719. a.push_back(E.value);
  2720. }
  2721. r_ret = a;
  2722. return true;
  2723. }
  2724. return false;
  2725. } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) {
  2726. int pattern_index = components[0].trim_prefix("pattern_").to_int();
  2727. if (pattern_index < 0 || pattern_index >= (int)patterns.size()) {
  2728. return false;
  2729. }
  2730. r_ret = patterns[pattern_index];
  2731. return true;
  2732. }
  2733. return false;
  2734. }
  2735. void TileSet::_get_property_list(List<PropertyInfo> *p_list) const {
  2736. PropertyInfo property_info;
  2737. // Rendering.
  2738. p_list->push_back(PropertyInfo(Variant::NIL, "Rendering", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2739. for (int i = 0; i < occlusion_layers.size(); i++) {
  2740. p_list->push_back(PropertyInfo(Variant::INT, vformat("occlusion_layer_%d/light_mask", i), PROPERTY_HINT_LAYERS_2D_RENDER));
  2741. // occlusion_layer_%d/sdf_collision
  2742. property_info = PropertyInfo(Variant::BOOL, vformat("occlusion_layer_%d/sdf_collision", i));
  2743. if (occlusion_layers[i].sdf_collision == false) {
  2744. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  2745. }
  2746. p_list->push_back(property_info);
  2747. }
  2748. // Physics.
  2749. p_list->push_back(PropertyInfo(Variant::NIL, "Physics", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2750. for (int i = 0; i < physics_layers.size(); i++) {
  2751. p_list->push_back(PropertyInfo(Variant::INT, vformat("physics_layer_%d/collision_layer", i), PROPERTY_HINT_LAYERS_2D_PHYSICS));
  2752. // physics_layer_%d/collision_mask
  2753. property_info = PropertyInfo(Variant::INT, vformat("physics_layer_%d/collision_mask", i), PROPERTY_HINT_LAYERS_2D_PHYSICS);
  2754. if (physics_layers[i].collision_mask == 1) {
  2755. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  2756. }
  2757. p_list->push_back(property_info);
  2758. // physics_layer_%d/physics_material
  2759. property_info = PropertyInfo(Variant::OBJECT, vformat("physics_layer_%d/physics_material", i), PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial");
  2760. if (!physics_layers[i].physics_material.is_valid()) {
  2761. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  2762. }
  2763. p_list->push_back(property_info);
  2764. }
  2765. // Terrains.
  2766. p_list->push_back(PropertyInfo(Variant::NIL, "Terrains", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2767. for (int terrain_set_index = 0; terrain_set_index < terrain_sets.size(); terrain_set_index++) {
  2768. p_list->push_back(PropertyInfo(Variant::INT, vformat("terrain_set_%d/mode", terrain_set_index), PROPERTY_HINT_ENUM, "Match Corners and Sides,Match Corners,Match Sides"));
  2769. p_list->push_back(PropertyInfo(Variant::NIL, vformat("terrain_set_%d/terrains", terrain_set_index), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, vformat("terrain_set_%d/terrain_", terrain_set_index)));
  2770. for (int terrain_index = 0; terrain_index < terrain_sets[terrain_set_index].terrains.size(); terrain_index++) {
  2771. p_list->push_back(PropertyInfo(Variant::STRING, vformat("terrain_set_%d/terrain_%d/name", terrain_set_index, terrain_index)));
  2772. p_list->push_back(PropertyInfo(Variant::COLOR, vformat("terrain_set_%d/terrain_%d/color", terrain_set_index, terrain_index)));
  2773. }
  2774. }
  2775. // Navigation.
  2776. p_list->push_back(PropertyInfo(Variant::NIL, "Navigation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2777. for (int i = 0; i < navigation_layers.size(); i++) {
  2778. p_list->push_back(PropertyInfo(Variant::INT, vformat("navigation_layer_%d/layers", i), PROPERTY_HINT_LAYERS_2D_NAVIGATION));
  2779. }
  2780. // Custom data.
  2781. String argt = "Any";
  2782. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  2783. argt += "," + Variant::get_type_name(Variant::Type(i));
  2784. }
  2785. p_list->push_back(PropertyInfo(Variant::NIL, "Custom data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2786. for (int i = 0; i < custom_data_layers.size(); i++) {
  2787. p_list->push_back(PropertyInfo(Variant::STRING, vformat("custom_data_layer_%d/name", i)));
  2788. p_list->push_back(PropertyInfo(Variant::INT, vformat("custom_data_layer_%d/type", i), PROPERTY_HINT_ENUM, argt));
  2789. }
  2790. // Sources.
  2791. // Note: sources have to be listed in at the end as some TileData rely on the TileSet properties being initialized first.
  2792. for (const KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  2793. p_list->push_back(PropertyInfo(Variant::INT, vformat("sources/%d", E_source.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2794. }
  2795. // Tile Proxies.
  2796. // Note: proxies need to be set after sources are set.
  2797. p_list->push_back(PropertyInfo(Variant::NIL, "Tile Proxies", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2798. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/source_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2799. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/coords_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2800. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/alternative_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2801. // Patterns.
  2802. for (unsigned int pattern_index = 0; pattern_index < patterns.size(); pattern_index++) {
  2803. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("pattern_%d", pattern_index), PROPERTY_HINT_RESOURCE_TYPE, "TileMapPattern", PROPERTY_USAGE_NO_EDITOR));
  2804. }
  2805. }
  2806. void TileSet::_validate_property(PropertyInfo &property) const {
  2807. if (property.name == "tile_layout" && tile_shape == TILE_SHAPE_SQUARE) {
  2808. property.usage ^= PROPERTY_USAGE_READ_ONLY;
  2809. } else if (property.name == "tile_offset_axis" && tile_shape == TILE_SHAPE_SQUARE) {
  2810. property.usage ^= PROPERTY_USAGE_READ_ONLY;
  2811. }
  2812. }
  2813. void TileSet::_bind_methods() {
  2814. // Sources management.
  2815. ClassDB::bind_method(D_METHOD("get_next_source_id"), &TileSet::get_next_source_id);
  2816. ClassDB::bind_method(D_METHOD("add_source", "source", "atlas_source_id_override"), &TileSet::add_source, DEFVAL(TileSet::INVALID_SOURCE));
  2817. ClassDB::bind_method(D_METHOD("remove_source", "source_id"), &TileSet::remove_source);
  2818. ClassDB::bind_method(D_METHOD("set_source_id", "source_id", "new_source_id"), &TileSet::set_source_id);
  2819. ClassDB::bind_method(D_METHOD("get_source_count"), &TileSet::get_source_count);
  2820. ClassDB::bind_method(D_METHOD("get_source_id", "index"), &TileSet::get_source_id);
  2821. ClassDB::bind_method(D_METHOD("has_source", "source_id"), &TileSet::has_source);
  2822. ClassDB::bind_method(D_METHOD("get_source", "source_id"), &TileSet::get_source);
  2823. // Shape and layout.
  2824. ClassDB::bind_method(D_METHOD("set_tile_shape", "shape"), &TileSet::set_tile_shape);
  2825. ClassDB::bind_method(D_METHOD("get_tile_shape"), &TileSet::get_tile_shape);
  2826. ClassDB::bind_method(D_METHOD("set_tile_layout", "layout"), &TileSet::set_tile_layout);
  2827. ClassDB::bind_method(D_METHOD("get_tile_layout"), &TileSet::get_tile_layout);
  2828. ClassDB::bind_method(D_METHOD("set_tile_offset_axis", "alignment"), &TileSet::set_tile_offset_axis);
  2829. ClassDB::bind_method(D_METHOD("get_tile_offset_axis"), &TileSet::get_tile_offset_axis);
  2830. ClassDB::bind_method(D_METHOD("set_tile_size", "size"), &TileSet::set_tile_size);
  2831. ClassDB::bind_method(D_METHOD("get_tile_size"), &TileSet::get_tile_size);
  2832. ADD_PROPERTY(PropertyInfo(Variant::INT, "tile_shape", PROPERTY_HINT_ENUM, "Square,Isometric,Half-Offset Square,Hexagon"), "set_tile_shape", "get_tile_shape");
  2833. ADD_PROPERTY(PropertyInfo(Variant::INT, "tile_layout", PROPERTY_HINT_ENUM, "Stacked,Stacked Offset,Stairs Right,Stairs Down,Diamond Right,Diamond Down"), "set_tile_layout", "get_tile_layout");
  2834. ADD_PROPERTY(PropertyInfo(Variant::INT, "tile_offset_axis", PROPERTY_HINT_ENUM, "Horizontal Offset,Vertical Offset"), "set_tile_offset_axis", "get_tile_offset_axis");
  2835. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "tile_size"), "set_tile_size", "get_tile_size");
  2836. // Rendering.
  2837. ClassDB::bind_method(D_METHOD("set_uv_clipping", "uv_clipping"), &TileSet::set_uv_clipping);
  2838. ClassDB::bind_method(D_METHOD("is_uv_clipping"), &TileSet::is_uv_clipping);
  2839. ClassDB::bind_method(D_METHOD("get_occlusion_layers_count"), &TileSet::get_occlusion_layers_count);
  2840. ClassDB::bind_method(D_METHOD("add_occlusion_layer", "to_position"), &TileSet::add_occlusion_layer, DEFVAL(-1));
  2841. ClassDB::bind_method(D_METHOD("move_occlusion_layer", "layer_index", "to_position"), &TileSet::move_occlusion_layer);
  2842. ClassDB::bind_method(D_METHOD("remove_occlusion_layer", "layer_index"), &TileSet::remove_occlusion_layer);
  2843. ClassDB::bind_method(D_METHOD("set_occlusion_layer_light_mask", "layer_index", "light_mask"), &TileSet::set_occlusion_layer_light_mask);
  2844. ClassDB::bind_method(D_METHOD("get_occlusion_layer_light_mask", "layer_index"), &TileSet::get_occlusion_layer_light_mask);
  2845. ClassDB::bind_method(D_METHOD("set_occlusion_layer_sdf_collision", "layer_index", "sdf_collision"), &TileSet::set_occlusion_layer_sdf_collision);
  2846. ClassDB::bind_method(D_METHOD("get_occlusion_layer_sdf_collision", "layer_index"), &TileSet::get_occlusion_layer_sdf_collision);
  2847. // Physics
  2848. ClassDB::bind_method(D_METHOD("get_physics_layers_count"), &TileSet::get_physics_layers_count);
  2849. ClassDB::bind_method(D_METHOD("add_physics_layer", "to_position"), &TileSet::add_physics_layer, DEFVAL(-1));
  2850. ClassDB::bind_method(D_METHOD("move_physics_layer", "layer_index", "to_position"), &TileSet::move_physics_layer);
  2851. ClassDB::bind_method(D_METHOD("remove_physics_layer", "layer_index"), &TileSet::remove_physics_layer);
  2852. ClassDB::bind_method(D_METHOD("set_physics_layer_collision_layer", "layer_index", "layer"), &TileSet::set_physics_layer_collision_layer);
  2853. ClassDB::bind_method(D_METHOD("get_physics_layer_collision_layer", "layer_index"), &TileSet::get_physics_layer_collision_layer);
  2854. ClassDB::bind_method(D_METHOD("set_physics_layer_collision_mask", "layer_index", "mask"), &TileSet::set_physics_layer_collision_mask);
  2855. ClassDB::bind_method(D_METHOD("get_physics_layer_collision_mask", "layer_index"), &TileSet::get_physics_layer_collision_mask);
  2856. ClassDB::bind_method(D_METHOD("set_physics_layer_physics_material", "layer_index", "physics_material"), &TileSet::set_physics_layer_physics_material);
  2857. ClassDB::bind_method(D_METHOD("get_physics_layer_physics_material", "layer_index"), &TileSet::get_physics_layer_physics_material);
  2858. // Terrains
  2859. ClassDB::bind_method(D_METHOD("get_terrain_sets_count"), &TileSet::get_terrain_sets_count);
  2860. ClassDB::bind_method(D_METHOD("add_terrain_set", "to_position"), &TileSet::add_terrain_set, DEFVAL(-1));
  2861. ClassDB::bind_method(D_METHOD("move_terrain_set", "terrain_set", "to_position"), &TileSet::move_terrain_set);
  2862. ClassDB::bind_method(D_METHOD("remove_terrain_set", "terrain_set"), &TileSet::remove_terrain_set);
  2863. ClassDB::bind_method(D_METHOD("set_terrain_set_mode", "terrain_set", "mode"), &TileSet::set_terrain_set_mode);
  2864. ClassDB::bind_method(D_METHOD("get_terrain_set_mode", "terrain_set"), &TileSet::get_terrain_set_mode);
  2865. ClassDB::bind_method(D_METHOD("get_terrains_count", "terrain_set"), &TileSet::get_terrains_count);
  2866. ClassDB::bind_method(D_METHOD("add_terrain", "terrain_set", "to_position"), &TileSet::add_terrain, DEFVAL(-1));
  2867. ClassDB::bind_method(D_METHOD("move_terrain", "terrain_set", "terrain_index", "to_position"), &TileSet::move_terrain);
  2868. ClassDB::bind_method(D_METHOD("remove_terrain", "terrain_set", "terrain_index"), &TileSet::remove_terrain);
  2869. ClassDB::bind_method(D_METHOD("set_terrain_name", "terrain_set", "terrain_index", "name"), &TileSet::set_terrain_name);
  2870. ClassDB::bind_method(D_METHOD("get_terrain_name", "terrain_set", "terrain_index"), &TileSet::get_terrain_name);
  2871. ClassDB::bind_method(D_METHOD("set_terrain_color", "terrain_set", "terrain_index", "color"), &TileSet::set_terrain_color);
  2872. ClassDB::bind_method(D_METHOD("get_terrain_color", "terrain_set", "terrain_index"), &TileSet::get_terrain_color);
  2873. // Navigation
  2874. ClassDB::bind_method(D_METHOD("get_navigation_layers_count"), &TileSet::get_navigation_layers_count);
  2875. ClassDB::bind_method(D_METHOD("add_navigation_layer", "to_position"), &TileSet::add_navigation_layer, DEFVAL(-1));
  2876. ClassDB::bind_method(D_METHOD("move_navigation_layer", "layer_index", "to_position"), &TileSet::move_navigation_layer);
  2877. ClassDB::bind_method(D_METHOD("remove_navigation_layer", "layer_index"), &TileSet::remove_navigation_layer);
  2878. ClassDB::bind_method(D_METHOD("set_navigation_layer_layers", "layer_index", "layers"), &TileSet::set_navigation_layer_layers);
  2879. ClassDB::bind_method(D_METHOD("get_navigation_layer_layers", "layer_index"), &TileSet::get_navigation_layer_layers);
  2880. // Custom data
  2881. ClassDB::bind_method(D_METHOD("get_custom_data_layers_count"), &TileSet::get_custom_data_layers_count);
  2882. ClassDB::bind_method(D_METHOD("add_custom_data_layer", "to_position"), &TileSet::add_custom_data_layer, DEFVAL(-1));
  2883. ClassDB::bind_method(D_METHOD("move_custom_data_layer", "layer_index", "to_position"), &TileSet::move_custom_data_layer);
  2884. ClassDB::bind_method(D_METHOD("remove_custom_data_layer", "layer_index"), &TileSet::remove_custom_data_layer);
  2885. // Tile proxies
  2886. ClassDB::bind_method(D_METHOD("set_source_level_tile_proxy", "source_from", "source_to"), &TileSet::set_source_level_tile_proxy);
  2887. ClassDB::bind_method(D_METHOD("get_source_level_tile_proxy", "source_from"), &TileSet::get_source_level_tile_proxy);
  2888. ClassDB::bind_method(D_METHOD("has_source_level_tile_proxy", "source_from"), &TileSet::has_source_level_tile_proxy);
  2889. ClassDB::bind_method(D_METHOD("remove_source_level_tile_proxy", "source_from"), &TileSet::remove_source_level_tile_proxy);
  2890. ClassDB::bind_method(D_METHOD("set_coords_level_tile_proxy", "p_source_from", "coords_from", "source_to", "coords_to"), &TileSet::set_coords_level_tile_proxy);
  2891. ClassDB::bind_method(D_METHOD("get_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::get_coords_level_tile_proxy);
  2892. ClassDB::bind_method(D_METHOD("has_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::has_coords_level_tile_proxy);
  2893. ClassDB::bind_method(D_METHOD("remove_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::remove_coords_level_tile_proxy);
  2894. ClassDB::bind_method(D_METHOD("set_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from", "source_to", "coords_to", "alternative_to"), &TileSet::set_alternative_level_tile_proxy);
  2895. ClassDB::bind_method(D_METHOD("get_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::get_alternative_level_tile_proxy);
  2896. ClassDB::bind_method(D_METHOD("has_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::has_alternative_level_tile_proxy);
  2897. ClassDB::bind_method(D_METHOD("remove_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::remove_alternative_level_tile_proxy);
  2898. ClassDB::bind_method(D_METHOD("map_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::map_tile_proxy);
  2899. ClassDB::bind_method(D_METHOD("cleanup_invalid_tile_proxies"), &TileSet::cleanup_invalid_tile_proxies);
  2900. ClassDB::bind_method(D_METHOD("clear_tile_proxies"), &TileSet::clear_tile_proxies);
  2901. // Patterns
  2902. ClassDB::bind_method(D_METHOD("add_pattern", "pattern", "index"), &TileSet::add_pattern, DEFVAL(-1));
  2903. ClassDB::bind_method(D_METHOD("get_pattern", "index"), &TileSet::get_pattern, DEFVAL(-1));
  2904. ClassDB::bind_method(D_METHOD("remove_pattern", "index"), &TileSet::remove_pattern);
  2905. ClassDB::bind_method(D_METHOD("get_patterns_count"), &TileSet::get_patterns_count);
  2906. ADD_GROUP("Rendering", "");
  2907. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uv_clipping"), "set_uv_clipping", "is_uv_clipping");
  2908. ADD_ARRAY("occlusion_layers", "occlusion_layer_");
  2909. ADD_GROUP("", "");
  2910. ADD_ARRAY("physics_layers", "physics_layer_");
  2911. ADD_ARRAY("terrain_sets", "terrain_set_");
  2912. ADD_ARRAY("navigation_layers", "navigation_layer_");
  2913. ADD_ARRAY("custom_data_layers", "custom_data_layer_");
  2914. // -- Enum binding --
  2915. BIND_ENUM_CONSTANT(TILE_SHAPE_SQUARE);
  2916. BIND_ENUM_CONSTANT(TILE_SHAPE_ISOMETRIC);
  2917. BIND_ENUM_CONSTANT(TILE_SHAPE_HALF_OFFSET_SQUARE);
  2918. BIND_ENUM_CONSTANT(TILE_SHAPE_HEXAGON);
  2919. BIND_ENUM_CONSTANT(TILE_LAYOUT_STACKED);
  2920. BIND_ENUM_CONSTANT(TILE_LAYOUT_STACKED_OFFSET);
  2921. BIND_ENUM_CONSTANT(TILE_LAYOUT_STAIRS_RIGHT);
  2922. BIND_ENUM_CONSTANT(TILE_LAYOUT_STAIRS_DOWN);
  2923. BIND_ENUM_CONSTANT(TILE_LAYOUT_DIAMOND_RIGHT);
  2924. BIND_ENUM_CONSTANT(TILE_LAYOUT_DIAMOND_DOWN);
  2925. BIND_ENUM_CONSTANT(TILE_OFFSET_AXIS_HORIZONTAL);
  2926. BIND_ENUM_CONSTANT(TILE_OFFSET_AXIS_VERTICAL);
  2927. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_RIGHT_SIDE);
  2928. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_RIGHT_CORNER);
  2929. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE);
  2930. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER);
  2931. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_SIDE);
  2932. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_CORNER);
  2933. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_LEFT_SIDE);
  2934. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_LEFT_CORNER);
  2935. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_LEFT_SIDE);
  2936. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_LEFT_CORNER);
  2937. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_LEFT_SIDE);
  2938. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_LEFT_CORNER);
  2939. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_SIDE);
  2940. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_CORNER);
  2941. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  2942. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_RIGHT_CORNER);
  2943. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_CORNERS_AND_SIDES);
  2944. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_CORNERS);
  2945. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_SIDES);
  2946. }
  2947. TileSet::TileSet() {
  2948. // Instantiate the tile meshes.
  2949. tile_lines_mesh.instantiate();
  2950. tile_filled_mesh.instantiate();
  2951. }
  2952. TileSet::~TileSet() {
  2953. #ifndef DISABLE_DEPRECATED
  2954. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  2955. memdelete(E.value);
  2956. }
  2957. #endif // DISABLE_DEPRECATED
  2958. while (!source_ids.is_empty()) {
  2959. remove_source(source_ids[0]);
  2960. }
  2961. }
  2962. /////////////////////////////// TileSetSource //////////////////////////////////////
  2963. void TileSetSource::set_tile_set(const TileSet *p_tile_set) {
  2964. tile_set = p_tile_set;
  2965. }
  2966. void TileSetSource::_bind_methods() {
  2967. // Base tiles
  2968. ClassDB::bind_method(D_METHOD("get_tiles_count"), &TileSetSource::get_tiles_count);
  2969. ClassDB::bind_method(D_METHOD("get_tile_id", "index"), &TileSetSource::get_tile_id);
  2970. ClassDB::bind_method(D_METHOD("has_tile", "atlas_coords"), &TileSetSource::has_tile);
  2971. // Alternative tiles
  2972. ClassDB::bind_method(D_METHOD("get_alternative_tiles_count", "atlas_coords"), &TileSetSource::get_alternative_tiles_count);
  2973. ClassDB::bind_method(D_METHOD("get_alternative_tile_id", "atlas_coords", "index"), &TileSetSource::get_alternative_tile_id);
  2974. ClassDB::bind_method(D_METHOD("has_alternative_tile", "atlas_coords", "alternative_tile"), &TileSetSource::has_alternative_tile);
  2975. }
  2976. /////////////////////////////// TileSetAtlasSource //////////////////////////////////////
  2977. void TileSetAtlasSource::set_tile_set(const TileSet *p_tile_set) {
  2978. tile_set = p_tile_set;
  2979. // Set the TileSet on all TileData.
  2980. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  2981. for (KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  2982. E_alternative.value->set_tile_set(tile_set);
  2983. }
  2984. }
  2985. }
  2986. const TileSet *TileSetAtlasSource::get_tile_set() const {
  2987. return tile_set;
  2988. }
  2989. void TileSetAtlasSource::notify_tile_data_properties_should_change() {
  2990. // Set the TileSet on all TileData.
  2991. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  2992. for (KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  2993. E_alternative.value->notify_tile_data_properties_should_change();
  2994. }
  2995. }
  2996. }
  2997. void TileSetAtlasSource::add_occlusion_layer(int p_to_pos) {
  2998. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  2999. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3000. E_alternative.value->add_occlusion_layer(p_to_pos);
  3001. }
  3002. }
  3003. }
  3004. void TileSetAtlasSource::move_occlusion_layer(int p_from_index, int p_to_pos) {
  3005. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3006. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3007. E_alternative.value->move_occlusion_layer(p_from_index, p_to_pos);
  3008. }
  3009. }
  3010. }
  3011. void TileSetAtlasSource::remove_occlusion_layer(int p_index) {
  3012. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3013. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3014. E_alternative.value->remove_occlusion_layer(p_index);
  3015. }
  3016. }
  3017. }
  3018. void TileSetAtlasSource::add_physics_layer(int p_to_pos) {
  3019. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3020. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3021. E_alternative.value->add_physics_layer(p_to_pos);
  3022. }
  3023. }
  3024. }
  3025. void TileSetAtlasSource::move_physics_layer(int p_from_index, int p_to_pos) {
  3026. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3027. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3028. E_alternative.value->move_physics_layer(p_from_index, p_to_pos);
  3029. }
  3030. }
  3031. }
  3032. void TileSetAtlasSource::remove_physics_layer(int p_index) {
  3033. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3034. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3035. E_alternative.value->remove_physics_layer(p_index);
  3036. }
  3037. }
  3038. }
  3039. void TileSetAtlasSource::add_terrain_set(int p_to_pos) {
  3040. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3041. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3042. E_alternative.value->add_terrain_set(p_to_pos);
  3043. }
  3044. }
  3045. }
  3046. void TileSetAtlasSource::move_terrain_set(int p_from_index, int p_to_pos) {
  3047. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3048. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3049. E_alternative.value->move_terrain_set(p_from_index, p_to_pos);
  3050. }
  3051. }
  3052. }
  3053. void TileSetAtlasSource::remove_terrain_set(int p_index) {
  3054. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3055. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3056. E_alternative.value->remove_terrain_set(p_index);
  3057. }
  3058. }
  3059. }
  3060. void TileSetAtlasSource::add_terrain(int p_terrain_set, int p_to_pos) {
  3061. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3062. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3063. E_alternative.value->add_terrain(p_terrain_set, p_to_pos);
  3064. }
  3065. }
  3066. }
  3067. void TileSetAtlasSource::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  3068. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3069. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3070. E_alternative.value->move_terrain(p_terrain_set, p_from_index, p_to_pos);
  3071. }
  3072. }
  3073. }
  3074. void TileSetAtlasSource::remove_terrain(int p_terrain_set, int p_index) {
  3075. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3076. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3077. E_alternative.value->remove_terrain(p_terrain_set, p_index);
  3078. }
  3079. }
  3080. }
  3081. void TileSetAtlasSource::add_navigation_layer(int p_to_pos) {
  3082. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3083. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3084. E_alternative.value->add_navigation_layer(p_to_pos);
  3085. }
  3086. }
  3087. }
  3088. void TileSetAtlasSource::move_navigation_layer(int p_from_index, int p_to_pos) {
  3089. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3090. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3091. E_alternative.value->move_navigation_layer(p_from_index, p_to_pos);
  3092. }
  3093. }
  3094. }
  3095. void TileSetAtlasSource::remove_navigation_layer(int p_index) {
  3096. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3097. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3098. E_alternative.value->remove_navigation_layer(p_index);
  3099. }
  3100. }
  3101. }
  3102. void TileSetAtlasSource::add_custom_data_layer(int p_to_pos) {
  3103. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3104. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3105. E_alternative.value->add_custom_data_layer(p_to_pos);
  3106. }
  3107. }
  3108. }
  3109. void TileSetAtlasSource::move_custom_data_layer(int p_from_index, int p_to_pos) {
  3110. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3111. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3112. E_alternative.value->move_custom_data_layer(p_from_index, p_to_pos);
  3113. }
  3114. }
  3115. }
  3116. void TileSetAtlasSource::remove_custom_data_layer(int p_index) {
  3117. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3118. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3119. E_alternative.value->remove_custom_data_layer(p_index);
  3120. }
  3121. }
  3122. }
  3123. void TileSetAtlasSource::reset_state() {
  3124. // Reset all TileData.
  3125. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3126. for (KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  3127. E_alternative.value->reset_state();
  3128. }
  3129. }
  3130. }
  3131. void TileSetAtlasSource::set_texture(Ref<Texture2D> p_texture) {
  3132. if (texture.is_valid()) {
  3133. texture->disconnect(SNAME("changed"), callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  3134. }
  3135. texture = p_texture;
  3136. if (texture.is_valid()) {
  3137. texture->connect(SNAME("changed"), callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  3138. }
  3139. _clear_tiles_outside_texture();
  3140. _queue_update_padded_texture();
  3141. emit_changed();
  3142. }
  3143. Ref<Texture2D> TileSetAtlasSource::get_texture() const {
  3144. return texture;
  3145. }
  3146. void TileSetAtlasSource::set_margins(Vector2i p_margins) {
  3147. if (p_margins.x < 0 || p_margins.y < 0) {
  3148. WARN_PRINT("Atlas source margins should be positive.");
  3149. margins = Vector2i(MAX(0, p_margins.x), MAX(0, p_margins.y));
  3150. } else {
  3151. margins = p_margins;
  3152. }
  3153. _clear_tiles_outside_texture();
  3154. _queue_update_padded_texture();
  3155. emit_changed();
  3156. }
  3157. Vector2i TileSetAtlasSource::get_margins() const {
  3158. return margins;
  3159. }
  3160. void TileSetAtlasSource::set_separation(Vector2i p_separation) {
  3161. if (p_separation.x < 0 || p_separation.y < 0) {
  3162. WARN_PRINT("Atlas source separation should be positive.");
  3163. separation = Vector2i(MAX(0, p_separation.x), MAX(0, p_separation.y));
  3164. } else {
  3165. separation = p_separation;
  3166. }
  3167. _clear_tiles_outside_texture();
  3168. _queue_update_padded_texture();
  3169. emit_changed();
  3170. }
  3171. Vector2i TileSetAtlasSource::get_separation() const {
  3172. return separation;
  3173. }
  3174. void TileSetAtlasSource::set_texture_region_size(Vector2i p_tile_size) {
  3175. if (p_tile_size.x <= 0 || p_tile_size.y <= 0) {
  3176. WARN_PRINT("Atlas source tile_size should be strictly positive.");
  3177. texture_region_size = Vector2i(MAX(1, p_tile_size.x), MAX(1, p_tile_size.y));
  3178. } else {
  3179. texture_region_size = p_tile_size;
  3180. }
  3181. _clear_tiles_outside_texture();
  3182. _queue_update_padded_texture();
  3183. emit_changed();
  3184. }
  3185. Vector2i TileSetAtlasSource::get_texture_region_size() const {
  3186. return texture_region_size;
  3187. }
  3188. void TileSetAtlasSource::set_use_texture_padding(bool p_use_padding) {
  3189. if (use_texture_padding == p_use_padding) {
  3190. return;
  3191. }
  3192. use_texture_padding = p_use_padding;
  3193. _queue_update_padded_texture();
  3194. emit_changed();
  3195. }
  3196. bool TileSetAtlasSource::get_use_texture_padding() const {
  3197. return use_texture_padding;
  3198. }
  3199. Vector2i TileSetAtlasSource::get_atlas_grid_size() const {
  3200. Ref<Texture2D> texture = get_texture();
  3201. if (!texture.is_valid()) {
  3202. return Vector2i();
  3203. }
  3204. ERR_FAIL_COND_V(texture_region_size.x <= 0 || texture_region_size.y <= 0, Vector2i());
  3205. Size2i valid_area = texture->get_size() - margins;
  3206. // Compute the number of valid tiles in the tiles atlas
  3207. Size2i grid_size = Size2i();
  3208. if (valid_area.x >= texture_region_size.x && valid_area.y >= texture_region_size.y) {
  3209. valid_area -= texture_region_size;
  3210. grid_size = Size2i(1, 1) + valid_area / (texture_region_size + separation);
  3211. }
  3212. return grid_size;
  3213. }
  3214. bool TileSetAtlasSource::_set(const StringName &p_name, const Variant &p_value) {
  3215. Vector<String> components = String(p_name).split("/", true, 2);
  3216. // Compute the vector2i if we have coordinates.
  3217. Vector<String> coords_split = components[0].split(":");
  3218. Vector2i coords = TileSetSource::INVALID_ATLAS_COORDS;
  3219. if (coords_split.size() == 2 && coords_split[0].is_valid_int() && coords_split[1].is_valid_int()) {
  3220. coords = Vector2i(coords_split[0].to_int(), coords_split[1].to_int());
  3221. }
  3222. // Properties.
  3223. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  3224. // Create the tile if needed.
  3225. if (!has_tile(coords)) {
  3226. create_tile(coords);
  3227. }
  3228. if (components.size() >= 2) {
  3229. // Properties.
  3230. if (components[1] == "size_in_atlas") {
  3231. move_tile_in_atlas(coords, coords, p_value);
  3232. return true;
  3233. } else if (components[1] == "next_alternative_id") {
  3234. tiles[coords].next_alternative_id = p_value;
  3235. return true;
  3236. } else if (components[1] == "animation_columns") {
  3237. set_tile_animation_columns(coords, p_value);
  3238. return true;
  3239. } else if (components[1] == "animation_separation") {
  3240. set_tile_animation_separation(coords, p_value);
  3241. return true;
  3242. } else if (components[1] == "animation_speed") {
  3243. set_tile_animation_speed(coords, p_value);
  3244. return true;
  3245. } else if (components[1] == "animation_frames_count") {
  3246. set_tile_animation_frames_count(coords, p_value);
  3247. return true;
  3248. } else if (components.size() >= 3 && components[1].begins_with("animation_frame_") && components[1].trim_prefix("animation_frame_").is_valid_int()) {
  3249. int frame = components[1].trim_prefix("animation_frame_").to_int();
  3250. if (components[2] == "duration") {
  3251. if (frame >= get_tile_animation_frames_count(coords)) {
  3252. set_tile_animation_frames_count(coords, frame + 1);
  3253. }
  3254. set_tile_animation_frame_duration(coords, frame, p_value);
  3255. return true;
  3256. }
  3257. return false;
  3258. } else if (components[1].is_valid_int()) {
  3259. int alternative_id = components[1].to_int();
  3260. if (alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE) {
  3261. // Create the alternative if needed ?
  3262. if (!has_alternative_tile(coords, alternative_id)) {
  3263. create_alternative_tile(coords, alternative_id);
  3264. }
  3265. if (!tiles[coords].alternatives.has(alternative_id)) {
  3266. tiles[coords].alternatives[alternative_id] = memnew(TileData);
  3267. tiles[coords].alternatives[alternative_id]->set_tile_set(tile_set);
  3268. tiles[coords].alternatives[alternative_id]->set_allow_transform(alternative_id > 0);
  3269. tiles[coords].alternatives_ids.append(alternative_id);
  3270. }
  3271. if (components.size() >= 3) {
  3272. bool valid;
  3273. tiles[coords].alternatives[alternative_id]->set(components[2], p_value, &valid);
  3274. return valid;
  3275. } else {
  3276. // Only create the alternative if it did not exist yet.
  3277. return true;
  3278. }
  3279. }
  3280. }
  3281. }
  3282. }
  3283. return false;
  3284. }
  3285. bool TileSetAtlasSource::_get(const StringName &p_name, Variant &r_ret) const {
  3286. Vector<String> components = String(p_name).split("/", true, 2);
  3287. // Properties.
  3288. Vector<String> coords_split = components[0].split(":");
  3289. if (coords_split.size() == 2 && coords_split[0].is_valid_int() && coords_split[1].is_valid_int()) {
  3290. Vector2i coords = Vector2i(coords_split[0].to_int(), coords_split[1].to_int());
  3291. if (tiles.has(coords)) {
  3292. if (components.size() >= 2) {
  3293. // Properties.
  3294. if (components[1] == "size_in_atlas") {
  3295. r_ret = tiles[coords].size_in_atlas;
  3296. return true;
  3297. } else if (components[1] == "next_alternative_id") {
  3298. r_ret = tiles[coords].next_alternative_id;
  3299. return true;
  3300. } else if (components[1] == "animation_columns") {
  3301. r_ret = get_tile_animation_columns(coords);
  3302. return true;
  3303. } else if (components[1] == "animation_separation") {
  3304. r_ret = get_tile_animation_separation(coords);
  3305. return true;
  3306. } else if (components[1] == "animation_speed") {
  3307. r_ret = get_tile_animation_speed(coords);
  3308. return true;
  3309. } else if (components[1] == "animation_frames_count") {
  3310. r_ret = get_tile_animation_frames_count(coords);
  3311. return true;
  3312. } else if (components.size() >= 3 && components[1].begins_with("animation_frame_") && components[1].trim_prefix("animation_frame_").is_valid_int()) {
  3313. int frame = components[1].trim_prefix("animation_frame_").to_int();
  3314. if (frame < 0 || frame >= get_tile_animation_frames_count(coords)) {
  3315. return false;
  3316. }
  3317. if (components[2] == "duration") {
  3318. r_ret = get_tile_animation_frame_duration(coords, frame);
  3319. return true;
  3320. }
  3321. return false;
  3322. } else if (components[1].is_valid_int()) {
  3323. int alternative_id = components[1].to_int();
  3324. if (alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE && tiles[coords].alternatives.has(alternative_id)) {
  3325. if (components.size() >= 3) {
  3326. bool valid;
  3327. r_ret = tiles[coords].alternatives[alternative_id]->get(components[2], &valid);
  3328. return valid;
  3329. } else {
  3330. // Only to notify the tile alternative exists.
  3331. r_ret = alternative_id;
  3332. return true;
  3333. }
  3334. }
  3335. }
  3336. }
  3337. }
  3338. }
  3339. return false;
  3340. }
  3341. void TileSetAtlasSource::_get_property_list(List<PropertyInfo> *p_list) const {
  3342. // Atlases data.
  3343. PropertyInfo property_info;
  3344. for (const KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3345. List<PropertyInfo> tile_property_list;
  3346. // size_in_atlas
  3347. property_info = PropertyInfo(Variant::VECTOR2I, "size_in_atlas", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3348. if (E_tile.value.size_in_atlas == Vector2i(1, 1)) {
  3349. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3350. }
  3351. tile_property_list.push_back(property_info);
  3352. // next_alternative_id
  3353. property_info = PropertyInfo(Variant::INT, "next_alternative_id", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3354. if (E_tile.value.next_alternative_id == 1) {
  3355. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3356. }
  3357. tile_property_list.push_back(property_info);
  3358. // animation_columns.
  3359. property_info = PropertyInfo(Variant::INT, "animation_columns", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3360. if (E_tile.value.animation_columns == 0) {
  3361. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3362. }
  3363. tile_property_list.push_back(property_info);
  3364. // animation_separation.
  3365. property_info = PropertyInfo(Variant::INT, "animation_separation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3366. if (E_tile.value.animation_separation == Vector2i()) {
  3367. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3368. }
  3369. tile_property_list.push_back(property_info);
  3370. // animation_speed.
  3371. property_info = PropertyInfo(Variant::FLOAT, "animation_speed", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3372. if (E_tile.value.animation_speed == 1.0) {
  3373. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3374. }
  3375. tile_property_list.push_back(property_info);
  3376. // animation_frames_count.
  3377. tile_property_list.push_back(PropertyInfo(Variant::INT, "animation_frames_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NETWORK));
  3378. // animation_frame_*.
  3379. bool store_durations = tiles[E_tile.key].animation_frames_durations.size() >= 2;
  3380. for (int i = 0; i < (int)tiles[E_tile.key].animation_frames_durations.size(); i++) {
  3381. property_info = PropertyInfo(Variant::FLOAT, vformat("animation_frame_%d/duration", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3382. if (!store_durations) {
  3383. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3384. }
  3385. tile_property_list.push_back(property_info);
  3386. }
  3387. for (const KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  3388. // Add a dummy property to show the alternative exists.
  3389. tile_property_list.push_back(PropertyInfo(Variant::INT, vformat("%d", E_alternative.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  3390. // Get the alternative tile's properties and append them to the list of properties.
  3391. List<PropertyInfo> alternative_property_list;
  3392. E_alternative.value->get_property_list(&alternative_property_list);
  3393. for (PropertyInfo &alternative_property_info : alternative_property_list) {
  3394. Variant default_value = ClassDB::class_get_default_property_value("TileData", alternative_property_info.name);
  3395. Variant value = E_alternative.value->get(alternative_property_info.name);
  3396. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {
  3397. alternative_property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3398. }
  3399. alternative_property_info.name = vformat("%s/%s", vformat("%d", E_alternative.key), alternative_property_info.name);
  3400. tile_property_list.push_back(alternative_property_info);
  3401. }
  3402. }
  3403. // Add all alternative.
  3404. for (PropertyInfo &tile_property_info : tile_property_list) {
  3405. tile_property_info.name = vformat("%s/%s", vformat("%d:%d", E_tile.key.x, E_tile.key.y), tile_property_info.name);
  3406. p_list->push_back(tile_property_info);
  3407. }
  3408. }
  3409. }
  3410. void TileSetAtlasSource::create_tile(const Vector2i p_atlas_coords, const Vector2i p_size) {
  3411. // Create a tile if it does not exists.
  3412. ERR_FAIL_COND(p_atlas_coords.x < 0 || p_atlas_coords.y < 0);
  3413. ERR_FAIL_COND(p_size.x <= 0 || p_size.y <= 0);
  3414. bool room_for_tile = has_room_for_tile(p_atlas_coords, p_size, 1, Vector2i(), 1);
  3415. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot create tile. The tile is outside the texture or tiles are already present in the space the tile would cover.");
  3416. // Initialize the tile data.
  3417. TileAlternativesData tad;
  3418. tad.size_in_atlas = p_size;
  3419. tad.animation_frames_durations.push_back(1.0);
  3420. tad.alternatives[0] = memnew(TileData);
  3421. tad.alternatives[0]->set_tile_set(tile_set);
  3422. tad.alternatives[0]->set_allow_transform(false);
  3423. tad.alternatives[0]->connect("changed", callable_mp((Resource *)this, &TileSetAtlasSource::emit_changed));
  3424. tad.alternatives[0]->notify_property_list_changed();
  3425. tad.alternatives_ids.append(0);
  3426. // Create and resize the tile.
  3427. tiles.insert(p_atlas_coords, tad);
  3428. tiles_ids.append(p_atlas_coords);
  3429. tiles_ids.sort();
  3430. _create_coords_mapping_cache(p_atlas_coords);
  3431. _queue_update_padded_texture();
  3432. emit_signal(SNAME("changed"));
  3433. }
  3434. void TileSetAtlasSource::remove_tile(Vector2i p_atlas_coords) {
  3435. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3436. // Remove all covered positions from the mapping cache
  3437. _clear_coords_mapping_cache(p_atlas_coords);
  3438. // Free tile data.
  3439. for (const KeyValue<int, TileData *> &E_tile_data : tiles[p_atlas_coords].alternatives) {
  3440. memdelete(E_tile_data.value);
  3441. }
  3442. // Delete the tile
  3443. tiles.erase(p_atlas_coords);
  3444. tiles_ids.erase(p_atlas_coords);
  3445. tiles_ids.sort();
  3446. _queue_update_padded_texture();
  3447. emit_signal(SNAME("changed"));
  3448. }
  3449. bool TileSetAtlasSource::has_tile(Vector2i p_atlas_coords) const {
  3450. return tiles.has(p_atlas_coords);
  3451. }
  3452. Vector2i TileSetAtlasSource::get_tile_at_coords(Vector2i p_atlas_coords) const {
  3453. if (!_coords_mapping_cache.has(p_atlas_coords)) {
  3454. return INVALID_ATLAS_COORDS;
  3455. }
  3456. return _coords_mapping_cache[p_atlas_coords];
  3457. }
  3458. void TileSetAtlasSource::set_tile_animation_columns(const Vector2i p_atlas_coords, int p_frame_columns) {
  3459. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3460. ERR_FAIL_COND(p_frame_columns < 0);
  3461. TileAlternativesData &tad = tiles[p_atlas_coords];
  3462. bool room_for_tile = has_room_for_tile(p_atlas_coords, tad.size_in_atlas, p_frame_columns, tad.animation_separation, tad.animation_frames_durations.size(), p_atlas_coords);
  3463. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  3464. _clear_coords_mapping_cache(p_atlas_coords);
  3465. tiles[p_atlas_coords].animation_columns = p_frame_columns;
  3466. _create_coords_mapping_cache(p_atlas_coords);
  3467. _queue_update_padded_texture();
  3468. emit_signal(SNAME("changed"));
  3469. }
  3470. int TileSetAtlasSource::get_tile_animation_columns(const Vector2i p_atlas_coords) const {
  3471. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3472. return tiles[p_atlas_coords].animation_columns;
  3473. }
  3474. void TileSetAtlasSource::set_tile_animation_separation(const Vector2i p_atlas_coords, const Vector2i p_separation) {
  3475. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3476. ERR_FAIL_COND(p_separation.x < 0 || p_separation.y < 0);
  3477. TileAlternativesData &tad = tiles[p_atlas_coords];
  3478. bool room_for_tile = has_room_for_tile(p_atlas_coords, tad.size_in_atlas, tad.animation_columns, p_separation, tad.animation_frames_durations.size(), p_atlas_coords);
  3479. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  3480. _clear_coords_mapping_cache(p_atlas_coords);
  3481. tiles[p_atlas_coords].animation_separation = p_separation;
  3482. _create_coords_mapping_cache(p_atlas_coords);
  3483. _queue_update_padded_texture();
  3484. emit_signal(SNAME("changed"));
  3485. }
  3486. Vector2i TileSetAtlasSource::get_tile_animation_separation(const Vector2i p_atlas_coords) const {
  3487. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3488. return tiles[p_atlas_coords].animation_separation;
  3489. }
  3490. void TileSetAtlasSource::set_tile_animation_speed(const Vector2i p_atlas_coords, real_t p_speed) {
  3491. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3492. ERR_FAIL_COND(p_speed <= 0);
  3493. tiles[p_atlas_coords].animation_speed = p_speed;
  3494. emit_signal(SNAME("changed"));
  3495. }
  3496. real_t TileSetAtlasSource::get_tile_animation_speed(const Vector2i p_atlas_coords) const {
  3497. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1.0, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3498. return tiles[p_atlas_coords].animation_speed;
  3499. }
  3500. void TileSetAtlasSource::set_tile_animation_frames_count(const Vector2i p_atlas_coords, int p_frames_count) {
  3501. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3502. ERR_FAIL_COND(p_frames_count < 1);
  3503. int old_size = tiles[p_atlas_coords].animation_frames_durations.size();
  3504. if (p_frames_count == old_size) {
  3505. return;
  3506. }
  3507. TileAlternativesData &tad = tiles[p_atlas_coords];
  3508. bool room_for_tile = has_room_for_tile(p_atlas_coords, tad.size_in_atlas, tad.animation_columns, tad.animation_separation, p_frames_count, p_atlas_coords);
  3509. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  3510. _clear_coords_mapping_cache(p_atlas_coords);
  3511. tiles[p_atlas_coords].animation_frames_durations.resize(p_frames_count);
  3512. for (int i = old_size; i < p_frames_count; i++) {
  3513. tiles[p_atlas_coords].animation_frames_durations[i] = 1.0;
  3514. }
  3515. _create_coords_mapping_cache(p_atlas_coords);
  3516. _queue_update_padded_texture();
  3517. notify_property_list_changed();
  3518. emit_signal(SNAME("changed"));
  3519. }
  3520. int TileSetAtlasSource::get_tile_animation_frames_count(const Vector2i p_atlas_coords) const {
  3521. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3522. return tiles[p_atlas_coords].animation_frames_durations.size();
  3523. }
  3524. void TileSetAtlasSource::set_tile_animation_frame_duration(const Vector2i p_atlas_coords, int p_frame_index, real_t p_duration) {
  3525. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3526. ERR_FAIL_INDEX(p_frame_index, (int)tiles[p_atlas_coords].animation_frames_durations.size());
  3527. ERR_FAIL_COND(p_duration <= 0.0);
  3528. tiles[p_atlas_coords].animation_frames_durations[p_frame_index] = p_duration;
  3529. emit_signal(SNAME("changed"));
  3530. }
  3531. real_t TileSetAtlasSource::get_tile_animation_frame_duration(const Vector2i p_atlas_coords, int p_frame_index) const {
  3532. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3533. ERR_FAIL_INDEX_V(p_frame_index, (int)tiles[p_atlas_coords].animation_frames_durations.size(), 0.0);
  3534. return tiles[p_atlas_coords].animation_frames_durations[p_frame_index];
  3535. }
  3536. real_t TileSetAtlasSource::get_tile_animation_total_duration(const Vector2i p_atlas_coords) const {
  3537. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3538. real_t sum = 0.0;
  3539. for (int frame = 0; frame < (int)tiles[p_atlas_coords].animation_frames_durations.size(); frame++) {
  3540. sum += tiles[p_atlas_coords].animation_frames_durations[frame];
  3541. }
  3542. return sum;
  3543. }
  3544. Vector2i TileSetAtlasSource::get_tile_size_in_atlas(Vector2i p_atlas_coords) const {
  3545. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(-1, -1), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3546. return tiles[p_atlas_coords].size_in_atlas;
  3547. }
  3548. int TileSetAtlasSource::get_tiles_count() const {
  3549. return tiles_ids.size();
  3550. }
  3551. Vector2i TileSetAtlasSource::get_tile_id(int p_index) const {
  3552. ERR_FAIL_INDEX_V(p_index, tiles_ids.size(), TileSetSource::INVALID_ATLAS_COORDS);
  3553. return tiles_ids[p_index];
  3554. }
  3555. bool TileSetAtlasSource::has_room_for_tile(Vector2i p_atlas_coords, Vector2i p_size, int p_animation_columns, Vector2i p_animation_separation, int p_frames_count, Vector2i p_ignored_tile) const {
  3556. if (p_atlas_coords.x < 0 || p_atlas_coords.y < 0) {
  3557. return false;
  3558. }
  3559. if (p_size.x <= 0 || p_size.y <= 0) {
  3560. return false;
  3561. }
  3562. Size2i atlas_grid_size = get_atlas_grid_size();
  3563. for (int frame = 0; frame < p_frames_count; frame++) {
  3564. Vector2i frame_coords = p_atlas_coords + (p_size + p_animation_separation) * ((p_animation_columns > 0) ? Vector2i(frame % p_animation_columns, frame / p_animation_columns) : Vector2i(frame, 0));
  3565. for (int x = 0; x < p_size.x; x++) {
  3566. for (int y = 0; y < p_size.y; y++) {
  3567. Vector2i coords = frame_coords + Vector2i(x, y);
  3568. if (_coords_mapping_cache.has(coords) && _coords_mapping_cache[coords] != p_ignored_tile) {
  3569. return false;
  3570. }
  3571. if (coords.x >= atlas_grid_size.x || coords.y >= atlas_grid_size.y) {
  3572. return false;
  3573. }
  3574. }
  3575. }
  3576. }
  3577. return true;
  3578. }
  3579. PackedVector2Array TileSetAtlasSource::get_tiles_to_be_removed_on_change(Ref<Texture2D> p_texture, Vector2i p_margins, Vector2i p_separation, Vector2i p_texture_region_size) {
  3580. ERR_FAIL_COND_V(p_margins.x < 0 || p_margins.y < 0, PackedVector2Array());
  3581. ERR_FAIL_COND_V(p_separation.x < 0 || p_separation.y < 0, PackedVector2Array());
  3582. ERR_FAIL_COND_V(p_texture_region_size.x <= 0 || p_texture_region_size.y <= 0, PackedVector2Array());
  3583. // Compute the new atlas grid size.
  3584. Size2 new_grid_size;
  3585. if (p_texture.is_valid()) {
  3586. Size2i valid_area = p_texture->get_size() - p_margins;
  3587. // Compute the number of valid tiles in the tiles atlas
  3588. if (valid_area.x >= p_texture_region_size.x && valid_area.y >= p_texture_region_size.y) {
  3589. valid_area -= p_texture_region_size;
  3590. new_grid_size = Size2i(1, 1) + valid_area / (p_texture_region_size + p_separation);
  3591. }
  3592. }
  3593. Vector<Vector2> output;
  3594. for (KeyValue<Vector2i, TileAlternativesData> &E : tiles) {
  3595. for (unsigned int frame = 0; frame < E.value.animation_frames_durations.size(); frame++) {
  3596. Vector2i frame_coords = E.key + (E.value.size_in_atlas + E.value.animation_separation) * ((E.value.animation_columns > 0) ? Vector2i(frame % E.value.animation_columns, frame / E.value.animation_columns) : Vector2i(frame, 0));
  3597. frame_coords += E.value.size_in_atlas;
  3598. if (frame_coords.x > new_grid_size.x || frame_coords.y > new_grid_size.y) {
  3599. output.push_back(E.key);
  3600. break;
  3601. }
  3602. }
  3603. }
  3604. return output;
  3605. }
  3606. Rect2i TileSetAtlasSource::get_tile_texture_region(Vector2i p_atlas_coords, int p_frame) const {
  3607. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Rect2i(), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3608. ERR_FAIL_INDEX_V(p_frame, (int)tiles[p_atlas_coords].animation_frames_durations.size(), Rect2i());
  3609. const TileAlternativesData &tad = tiles[p_atlas_coords];
  3610. Vector2i size_in_atlas = tad.size_in_atlas;
  3611. Vector2 region_size = texture_region_size * size_in_atlas + separation * (size_in_atlas - Vector2i(1, 1));
  3612. Vector2i frame_coords = p_atlas_coords + (size_in_atlas + tad.animation_separation) * ((tad.animation_columns > 0) ? Vector2i(p_frame % tad.animation_columns, p_frame / tad.animation_columns) : Vector2i(p_frame, 0));
  3613. Vector2 origin = margins + (frame_coords * (texture_region_size + separation));
  3614. return Rect2(origin, region_size);
  3615. }
  3616. Vector2i TileSetAtlasSource::get_tile_effective_texture_offset(Vector2i p_atlas_coords, int p_alternative_tile) const {
  3617. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3618. ERR_FAIL_COND_V_MSG(!has_alternative_tile(p_atlas_coords, p_alternative_tile), Vector2i(), vformat("TileSetAtlasSource has no alternative tile with id %d at %s.", p_alternative_tile, String(p_atlas_coords)));
  3619. ERR_FAIL_COND_V(!tile_set, Vector2i());
  3620. Vector2 margin = (get_tile_texture_region(p_atlas_coords).size - tile_set->get_tile_size()) / 2;
  3621. margin = Vector2i(MAX(0, margin.x), MAX(0, margin.y));
  3622. Vector2i effective_texture_offset = get_tile_data(p_atlas_coords, p_alternative_tile)->get_texture_offset();
  3623. if (ABS(effective_texture_offset.x) > margin.x || ABS(effective_texture_offset.y) > margin.y) {
  3624. effective_texture_offset = effective_texture_offset.clamp(-margin, margin);
  3625. }
  3626. return effective_texture_offset;
  3627. }
  3628. // Getters for texture and tile region (padded or not)
  3629. Ref<Texture2D> TileSetAtlasSource::get_runtime_texture() const {
  3630. if (use_texture_padding) {
  3631. return padded_texture;
  3632. } else {
  3633. return texture;
  3634. }
  3635. }
  3636. Rect2i TileSetAtlasSource::get_runtime_tile_texture_region(Vector2i p_atlas_coords, int p_frame) const {
  3637. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Rect2i(), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3638. ERR_FAIL_INDEX_V(p_frame, (int)tiles[p_atlas_coords].animation_frames_durations.size(), Rect2i());
  3639. Rect2i src_rect = get_tile_texture_region(p_atlas_coords, p_frame);
  3640. if (use_texture_padding) {
  3641. const TileAlternativesData &tad = tiles[p_atlas_coords];
  3642. Vector2i frame_coords = p_atlas_coords + (tad.size_in_atlas + tad.animation_separation) * ((tad.animation_columns > 0) ? Vector2i(p_frame % tad.animation_columns, p_frame / tad.animation_columns) : Vector2i(p_frame, 0));
  3643. Vector2i base_pos = frame_coords * (texture_region_size + Vector2i(2, 2)) + Vector2i(1, 1);
  3644. return Rect2i(base_pos, src_rect.size);
  3645. } else {
  3646. return src_rect;
  3647. }
  3648. }
  3649. void TileSetAtlasSource::move_tile_in_atlas(Vector2i p_atlas_coords, Vector2i p_new_atlas_coords, Vector2i p_new_size) {
  3650. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3651. TileAlternativesData &tad = tiles[p_atlas_coords];
  3652. // Compute the actual new rect from arguments.
  3653. Vector2i new_atlas_coords = (p_new_atlas_coords != INVALID_ATLAS_COORDS) ? p_new_atlas_coords : p_atlas_coords;
  3654. Vector2i new_size = (p_new_size != Vector2i(-1, -1)) ? p_new_size : tad.size_in_atlas;
  3655. if (new_atlas_coords == p_atlas_coords && new_size == tad.size_in_atlas) {
  3656. return;
  3657. }
  3658. bool room_for_tile = has_room_for_tile(new_atlas_coords, new_size, tad.animation_columns, tad.animation_separation, tad.animation_frames_durations.size(), p_atlas_coords);
  3659. ERR_FAIL_COND_MSG(!room_for_tile, vformat("Cannot move tile at position %s with size %s. Tile already present.", new_atlas_coords, new_size));
  3660. _clear_coords_mapping_cache(p_atlas_coords);
  3661. // Move the tile and update its size.
  3662. if (new_atlas_coords != p_atlas_coords) {
  3663. tiles[new_atlas_coords] = tiles[p_atlas_coords];
  3664. tiles.erase(p_atlas_coords);
  3665. tiles_ids.erase(p_atlas_coords);
  3666. tiles_ids.append(new_atlas_coords);
  3667. tiles_ids.sort();
  3668. }
  3669. tiles[new_atlas_coords].size_in_atlas = new_size;
  3670. _create_coords_mapping_cache(new_atlas_coords);
  3671. _queue_update_padded_texture();
  3672. emit_signal(SNAME("changed"));
  3673. }
  3674. int TileSetAtlasSource::create_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_id_override) {
  3675. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), TileSetSource::INVALID_TILE_ALTERNATIVE, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3676. ERR_FAIL_COND_V_MSG(p_alternative_id_override >= 0 && tiles[p_atlas_coords].alternatives.has(p_alternative_id_override), TileSetSource::INVALID_TILE_ALTERNATIVE, vformat("Cannot create alternative tile. Another alternative exists with id %d.", p_alternative_id_override));
  3677. int new_alternative_id = p_alternative_id_override >= 0 ? p_alternative_id_override : tiles[p_atlas_coords].next_alternative_id;
  3678. tiles[p_atlas_coords].alternatives[new_alternative_id] = memnew(TileData);
  3679. tiles[p_atlas_coords].alternatives[new_alternative_id]->set_tile_set(tile_set);
  3680. tiles[p_atlas_coords].alternatives[new_alternative_id]->set_allow_transform(true);
  3681. tiles[p_atlas_coords].alternatives[new_alternative_id]->notify_property_list_changed();
  3682. tiles[p_atlas_coords].alternatives_ids.append(new_alternative_id);
  3683. tiles[p_atlas_coords].alternatives_ids.sort();
  3684. _compute_next_alternative_id(p_atlas_coords);
  3685. emit_signal(SNAME("changed"));
  3686. return new_alternative_id;
  3687. }
  3688. void TileSetAtlasSource::remove_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) {
  3689. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3690. ERR_FAIL_COND_MSG(!tiles[p_atlas_coords].alternatives.has(p_alternative_tile), vformat("TileSetAtlasSource has no alternative with id %d for tile coords %s.", p_alternative_tile, String(p_atlas_coords)));
  3691. ERR_FAIL_COND_MSG(p_alternative_tile == 0, "Cannot remove the alternative with id 0, the base tile alternative cannot be removed.");
  3692. memdelete(tiles[p_atlas_coords].alternatives[p_alternative_tile]);
  3693. tiles[p_atlas_coords].alternatives.erase(p_alternative_tile);
  3694. tiles[p_atlas_coords].alternatives_ids.erase(p_alternative_tile);
  3695. tiles[p_atlas_coords].alternatives_ids.sort();
  3696. emit_signal(SNAME("changed"));
  3697. }
  3698. void TileSetAtlasSource::set_alternative_tile_id(const Vector2i p_atlas_coords, int p_alternative_tile, int p_new_id) {
  3699. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3700. ERR_FAIL_COND_MSG(!tiles[p_atlas_coords].alternatives.has(p_alternative_tile), vformat("TileSetAtlasSource has no alternative with id %d for tile coords %s.", p_alternative_tile, String(p_atlas_coords)));
  3701. ERR_FAIL_COND_MSG(p_alternative_tile == 0, "Cannot change the alternative with id 0, the base tile alternative cannot be modified.");
  3702. ERR_FAIL_COND_MSG(tiles[p_atlas_coords].alternatives.has(p_new_id), vformat("TileSetAtlasSource has already an alternative with id %d at %s.", p_new_id, String(p_atlas_coords)));
  3703. tiles[p_atlas_coords].alternatives[p_new_id] = tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3704. tiles[p_atlas_coords].alternatives_ids.append(p_new_id);
  3705. tiles[p_atlas_coords].alternatives.erase(p_alternative_tile);
  3706. tiles[p_atlas_coords].alternatives_ids.erase(p_alternative_tile);
  3707. tiles[p_atlas_coords].alternatives_ids.sort();
  3708. emit_signal(SNAME("changed"));
  3709. }
  3710. bool TileSetAtlasSource::has_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  3711. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), false, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3712. return tiles[p_atlas_coords].alternatives.has(p_alternative_tile);
  3713. }
  3714. int TileSetAtlasSource::get_next_alternative_tile_id(const Vector2i p_atlas_coords) const {
  3715. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), TileSetSource::INVALID_TILE_ALTERNATIVE, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3716. return tiles[p_atlas_coords].next_alternative_id;
  3717. }
  3718. int TileSetAtlasSource::get_alternative_tiles_count(const Vector2i p_atlas_coords) const {
  3719. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), -1, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3720. return tiles[p_atlas_coords].alternatives_ids.size();
  3721. }
  3722. int TileSetAtlasSource::get_alternative_tile_id(const Vector2i p_atlas_coords, int p_index) const {
  3723. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), TileSetSource::INVALID_TILE_ALTERNATIVE, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3724. ERR_FAIL_INDEX_V(p_index, tiles[p_atlas_coords].alternatives_ids.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  3725. return tiles[p_atlas_coords].alternatives_ids[p_index];
  3726. }
  3727. TileData *TileSetAtlasSource::get_tile_data(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  3728. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3729. ERR_FAIL_COND_V_MSG(!tiles[p_atlas_coords].alternatives.has(p_alternative_tile), nullptr, vformat("TileSetAtlasSource has no alternative with id %d for tile coords %s.", p_alternative_tile, String(p_atlas_coords)));
  3730. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3731. }
  3732. void TileSetAtlasSource::_bind_methods() {
  3733. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TileSetAtlasSource::set_texture);
  3734. ClassDB::bind_method(D_METHOD("get_texture"), &TileSetAtlasSource::get_texture);
  3735. ClassDB::bind_method(D_METHOD("set_margins", "margins"), &TileSetAtlasSource::set_margins);
  3736. ClassDB::bind_method(D_METHOD("get_margins"), &TileSetAtlasSource::get_margins);
  3737. ClassDB::bind_method(D_METHOD("set_separation", "separation"), &TileSetAtlasSource::set_separation);
  3738. ClassDB::bind_method(D_METHOD("get_separation"), &TileSetAtlasSource::get_separation);
  3739. ClassDB::bind_method(D_METHOD("set_texture_region_size", "texture_region_size"), &TileSetAtlasSource::set_texture_region_size);
  3740. ClassDB::bind_method(D_METHOD("get_texture_region_size"), &TileSetAtlasSource::get_texture_region_size);
  3741. ClassDB::bind_method(D_METHOD("set_use_texture_padding", "use_texture_padding"), &TileSetAtlasSource::set_use_texture_padding);
  3742. ClassDB::bind_method(D_METHOD("get_use_texture_padding"), &TileSetAtlasSource::get_use_texture_padding);
  3743. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_NO_EDITOR), "set_texture", "get_texture");
  3744. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "margins", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_margins", "get_margins");
  3745. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "separation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_separation", "get_separation");
  3746. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "texture_region_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_texture_region_size", "get_texture_region_size");
  3747. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_texture_padding", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_use_texture_padding", "get_use_texture_padding");
  3748. // Base tiles
  3749. ClassDB::bind_method(D_METHOD("create_tile", "atlas_coords", "size"), &TileSetAtlasSource::create_tile, DEFVAL(Vector2i(1, 1)));
  3750. ClassDB::bind_method(D_METHOD("remove_tile", "atlas_coords"), &TileSetAtlasSource::remove_tile); // Remove a tile. If p_tile_key.alternative_tile if different from 0, remove the alternative
  3751. ClassDB::bind_method(D_METHOD("move_tile_in_atlas", "atlas_coords", "new_atlas_coords", "new_size"), &TileSetAtlasSource::move_tile_in_atlas, DEFVAL(INVALID_ATLAS_COORDS), DEFVAL(Vector2i(-1, -1)));
  3752. ClassDB::bind_method(D_METHOD("get_tile_size_in_atlas", "atlas_coords"), &TileSetAtlasSource::get_tile_size_in_atlas);
  3753. ClassDB::bind_method(D_METHOD("has_room_for_tile", "atlas_coords", "size", "animation_columns", "animation_separation", "frames_count", "ignored_tile"), &TileSetAtlasSource::has_room_for_tile, DEFVAL(INVALID_ATLAS_COORDS));
  3754. ClassDB::bind_method(D_METHOD("get_tiles_to_be_removed_on_change", "texture", "margins", "separation", "texture_region_size"), &TileSetAtlasSource::get_tiles_to_be_removed_on_change);
  3755. ClassDB::bind_method(D_METHOD("get_tile_at_coords", "atlas_coords"), &TileSetAtlasSource::get_tile_at_coords);
  3756. ClassDB::bind_method(D_METHOD("set_tile_animation_columns", "atlas_coords", "frame_columns"), &TileSetAtlasSource::set_tile_animation_columns);
  3757. ClassDB::bind_method(D_METHOD("get_tile_animation_columns", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_columns);
  3758. ClassDB::bind_method(D_METHOD("set_tile_animation_separation", "atlas_coords", "separation"), &TileSetAtlasSource::set_tile_animation_separation);
  3759. ClassDB::bind_method(D_METHOD("get_tile_animation_separation", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_separation);
  3760. ClassDB::bind_method(D_METHOD("set_tile_animation_speed", "atlas_coords", "speed"), &TileSetAtlasSource::set_tile_animation_speed);
  3761. ClassDB::bind_method(D_METHOD("get_tile_animation_speed", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_speed);
  3762. ClassDB::bind_method(D_METHOD("set_tile_animation_frames_count", "atlas_coords", "frames_count"), &TileSetAtlasSource::set_tile_animation_frames_count);
  3763. ClassDB::bind_method(D_METHOD("get_tile_animation_frames_count", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_frames_count);
  3764. ClassDB::bind_method(D_METHOD("set_tile_animation_frame_duration", "atlas_coords", "frame_index", "duration"), &TileSetAtlasSource::set_tile_animation_frame_duration);
  3765. ClassDB::bind_method(D_METHOD("get_tile_animation_frame_duration", "atlas_coords", "frame_index"), &TileSetAtlasSource::get_tile_animation_frame_duration);
  3766. ClassDB::bind_method(D_METHOD("get_tile_animation_total_duration", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_total_duration);
  3767. // Alternative tiles
  3768. ClassDB::bind_method(D_METHOD("create_alternative_tile", "atlas_coords", "alternative_id_override"), &TileSetAtlasSource::create_alternative_tile, DEFVAL(INVALID_TILE_ALTERNATIVE));
  3769. ClassDB::bind_method(D_METHOD("remove_alternative_tile", "atlas_coords", "alternative_tile"), &TileSetAtlasSource::remove_alternative_tile);
  3770. ClassDB::bind_method(D_METHOD("set_alternative_tile_id", "atlas_coords", "alternative_tile", "new_id"), &TileSetAtlasSource::set_alternative_tile_id);
  3771. ClassDB::bind_method(D_METHOD("get_next_alternative_tile_id", "atlas_coords"), &TileSetAtlasSource::get_next_alternative_tile_id);
  3772. ClassDB::bind_method(D_METHOD("get_tile_data", "atlas_coords", "alternative_tile"), &TileSetAtlasSource::get_tile_data);
  3773. // Helpers.
  3774. ClassDB::bind_method(D_METHOD("get_atlas_grid_size"), &TileSetAtlasSource::get_atlas_grid_size);
  3775. ClassDB::bind_method(D_METHOD("get_tile_texture_region", "atlas_coords", "frame"), &TileSetAtlasSource::get_tile_texture_region, DEFVAL(0));
  3776. // Getters for texture and tile region (padded or not)
  3777. ClassDB::bind_method(D_METHOD("_update_padded_texture"), &TileSetAtlasSource::_update_padded_texture);
  3778. ClassDB::bind_method(D_METHOD("get_runtime_texture"), &TileSetAtlasSource::get_runtime_texture);
  3779. ClassDB::bind_method(D_METHOD("get_runtime_tile_texture_region", "atlas_coords", "frame"), &TileSetAtlasSource::get_runtime_tile_texture_region);
  3780. }
  3781. TileSetAtlasSource::~TileSetAtlasSource() {
  3782. // Free everything needed.
  3783. for (KeyValue<Vector2i, TileAlternativesData> &E_alternatives : tiles) {
  3784. for (KeyValue<int, TileData *> &E_tile_data : E_alternatives.value.alternatives) {
  3785. memdelete(E_tile_data.value);
  3786. }
  3787. }
  3788. }
  3789. TileData *TileSetAtlasSource::_get_atlas_tile_data(Vector2i p_atlas_coords, int p_alternative_tile) {
  3790. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3791. ERR_FAIL_COND_V_MSG(!tiles[p_atlas_coords].alternatives.has(p_alternative_tile), nullptr, vformat("TileSetAtlasSource has no alternative with id %d for tile coords %s.", p_alternative_tile, String(p_atlas_coords)));
  3792. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3793. }
  3794. const TileData *TileSetAtlasSource::_get_atlas_tile_data(Vector2i p_atlas_coords, int p_alternative_tile) const {
  3795. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3796. ERR_FAIL_COND_V_MSG(!tiles[p_atlas_coords].alternatives.has(p_alternative_tile), nullptr, vformat("TileSetAtlasSource has no alternative with id %d for tile coords %s.", p_alternative_tile, String(p_atlas_coords)));
  3797. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3798. }
  3799. void TileSetAtlasSource::_compute_next_alternative_id(const Vector2i p_atlas_coords) {
  3800. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3801. while (tiles[p_atlas_coords].alternatives.has(tiles[p_atlas_coords].next_alternative_id)) {
  3802. tiles[p_atlas_coords].next_alternative_id = (tiles[p_atlas_coords].next_alternative_id % 1073741823) + 1; // 2 ** 30
  3803. };
  3804. }
  3805. void TileSetAtlasSource::_clear_coords_mapping_cache(Vector2i p_atlas_coords) {
  3806. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3807. TileAlternativesData &tad = tiles[p_atlas_coords];
  3808. for (int frame = 0; frame < (int)tad.animation_frames_durations.size(); frame++) {
  3809. Vector2i frame_coords = p_atlas_coords + (tad.size_in_atlas + tad.animation_separation) * ((tad.animation_columns > 0) ? Vector2i(frame % tad.animation_columns, frame / tad.animation_columns) : Vector2i(frame, 0));
  3810. for (int x = 0; x < tad.size_in_atlas.x; x++) {
  3811. for (int y = 0; y < tad.size_in_atlas.y; y++) {
  3812. Vector2i coords = frame_coords + Vector2i(x, y);
  3813. if (!_coords_mapping_cache.has(coords)) {
  3814. WARN_PRINT(vformat("TileSetAtlasSource has no cached tile at position %s, the position cache might be corrupted.", coords));
  3815. } else {
  3816. if (_coords_mapping_cache[coords] != p_atlas_coords) {
  3817. WARN_PRINT(vformat("The position cache at position %s is pointing to a wrong tile, the position cache might be corrupted.", coords));
  3818. }
  3819. _coords_mapping_cache.erase(coords);
  3820. }
  3821. }
  3822. }
  3823. }
  3824. }
  3825. void TileSetAtlasSource::_create_coords_mapping_cache(Vector2i p_atlas_coords) {
  3826. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3827. TileAlternativesData &tad = tiles[p_atlas_coords];
  3828. for (int frame = 0; frame < (int)tad.animation_frames_durations.size(); frame++) {
  3829. Vector2i frame_coords = p_atlas_coords + (tad.size_in_atlas + tad.animation_separation) * ((tad.animation_columns > 0) ? Vector2i(frame % tad.animation_columns, frame / tad.animation_columns) : Vector2i(frame, 0));
  3830. for (int x = 0; x < tad.size_in_atlas.x; x++) {
  3831. for (int y = 0; y < tad.size_in_atlas.y; y++) {
  3832. Vector2i coords = frame_coords + Vector2i(x, y);
  3833. if (_coords_mapping_cache.has(coords)) {
  3834. WARN_PRINT(vformat("The cache already has a tile for position %s, the position cache might be corrupted.", coords));
  3835. }
  3836. _coords_mapping_cache[coords] = p_atlas_coords;
  3837. }
  3838. }
  3839. }
  3840. }
  3841. void TileSetAtlasSource::_clear_tiles_outside_texture() {
  3842. LocalVector<Vector2i> to_remove;
  3843. for (const KeyValue<Vector2i, TileSetAtlasSource::TileAlternativesData> &E : tiles) {
  3844. if (!has_room_for_tile(E.key, E.value.size_in_atlas, E.value.animation_columns, E.value.animation_separation, E.value.animation_frames_durations.size(), E.key)) {
  3845. to_remove.push_back(E.key);
  3846. }
  3847. }
  3848. for (unsigned int i = 0; i < to_remove.size(); i++) {
  3849. remove_tile(to_remove[i]);
  3850. }
  3851. }
  3852. void TileSetAtlasSource::_queue_update_padded_texture() {
  3853. padded_texture_needs_update = true;
  3854. call_deferred(SNAME("_update_padded_texture"));
  3855. }
  3856. void TileSetAtlasSource::_update_padded_texture() {
  3857. if (!padded_texture_needs_update) {
  3858. return;
  3859. }
  3860. padded_texture_needs_update = false;
  3861. padded_texture = Ref<ImageTexture>();
  3862. if (!texture.is_valid()) {
  3863. return;
  3864. }
  3865. if (!use_texture_padding) {
  3866. return;
  3867. }
  3868. Size2 size = get_atlas_grid_size() * (texture_region_size + Vector2i(2, 2));
  3869. Ref<Image> src = texture->get_image();
  3870. if (!src.is_valid()) {
  3871. return;
  3872. }
  3873. Ref<Image> image;
  3874. image.instantiate();
  3875. image->create(size.x, size.y, false, src->get_format());
  3876. for (KeyValue<Vector2i, TileAlternativesData> kv : tiles) {
  3877. for (int frame = 0; frame < (int)kv.value.animation_frames_durations.size(); frame++) {
  3878. // Compute the source rects.
  3879. Rect2i src_rect = get_tile_texture_region(kv.key, frame);
  3880. Rect2i top_src_rect = Rect2i(src_rect.position, Vector2i(src_rect.size.x, 1));
  3881. Rect2i bottom_src_rect = Rect2i(src_rect.position + Vector2i(0, src_rect.size.y - 1), Vector2i(src_rect.size.x, 1));
  3882. Rect2i left_src_rect = Rect2i(src_rect.position, Vector2i(1, src_rect.size.y));
  3883. Rect2i right_src_rect = Rect2i(src_rect.position + Vector2i(src_rect.size.x - 1, 0), Vector2i(1, src_rect.size.y));
  3884. // Copy the tile and the paddings.
  3885. Vector2i frame_coords = kv.key + (kv.value.size_in_atlas + kv.value.animation_separation) * ((kv.value.animation_columns > 0) ? Vector2i(frame % kv.value.animation_columns, frame / kv.value.animation_columns) : Vector2i(frame, 0));
  3886. Vector2i base_pos = frame_coords * (texture_region_size + Vector2i(2, 2)) + Vector2i(1, 1);
  3887. image->blit_rect(*src, src_rect, base_pos);
  3888. image->blit_rect(*src, top_src_rect, base_pos + Vector2i(0, -1));
  3889. image->blit_rect(*src, bottom_src_rect, base_pos + Vector2i(0, src_rect.size.y));
  3890. image->blit_rect(*src, left_src_rect, base_pos + Vector2i(-1, 0));
  3891. image->blit_rect(*src, right_src_rect, base_pos + Vector2i(src_rect.size.x, 0));
  3892. image->set_pixelv(base_pos + Vector2i(-1, -1), src->get_pixelv(src_rect.position));
  3893. image->set_pixelv(base_pos + Vector2i(src_rect.size.x, -1), src->get_pixelv(src_rect.position + Vector2i(src_rect.size.x - 1, 0)));
  3894. image->set_pixelv(base_pos + Vector2i(-1, src_rect.size.y), src->get_pixelv(src_rect.position + Vector2i(0, src_rect.size.y - 1)));
  3895. image->set_pixelv(base_pos + Vector2i(src_rect.size.x, src_rect.size.y), src->get_pixelv(src_rect.position + Vector2i(src_rect.size.x - 1, src_rect.size.y - 1)));
  3896. }
  3897. }
  3898. if (!padded_texture.is_valid()) {
  3899. padded_texture.instantiate();
  3900. }
  3901. padded_texture->create_from_image(image);
  3902. emit_changed();
  3903. }
  3904. /////////////////////////////// TileSetScenesCollectionSource //////////////////////////////////////
  3905. void TileSetScenesCollectionSource::_compute_next_alternative_id() {
  3906. while (scenes.has(next_scene_id)) {
  3907. next_scene_id = (next_scene_id % 1073741823) + 1; // 2 ** 30
  3908. };
  3909. }
  3910. int TileSetScenesCollectionSource::get_tiles_count() const {
  3911. return 1;
  3912. }
  3913. Vector2i TileSetScenesCollectionSource::get_tile_id(int p_tile_index) const {
  3914. ERR_FAIL_COND_V(p_tile_index != 0, TileSetSource::INVALID_ATLAS_COORDS);
  3915. return Vector2i();
  3916. }
  3917. bool TileSetScenesCollectionSource::has_tile(Vector2i p_atlas_coords) const {
  3918. return p_atlas_coords == Vector2i();
  3919. }
  3920. int TileSetScenesCollectionSource::get_alternative_tiles_count(const Vector2i p_atlas_coords) const {
  3921. return scenes_ids.size();
  3922. }
  3923. int TileSetScenesCollectionSource::get_alternative_tile_id(const Vector2i p_atlas_coords, int p_index) const {
  3924. ERR_FAIL_COND_V(p_atlas_coords != Vector2i(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  3925. ERR_FAIL_INDEX_V(p_index, scenes_ids.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  3926. return scenes_ids[p_index];
  3927. }
  3928. bool TileSetScenesCollectionSource::has_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  3929. ERR_FAIL_COND_V(p_atlas_coords != Vector2i(), false);
  3930. return scenes.has(p_alternative_tile);
  3931. }
  3932. int TileSetScenesCollectionSource::create_scene_tile(Ref<PackedScene> p_packed_scene, int p_id_override) {
  3933. ERR_FAIL_COND_V_MSG(p_id_override >= 0 && scenes.has(p_id_override), INVALID_TILE_ALTERNATIVE, vformat("Cannot create scene tile. Another scene tile exists with id %d.", p_id_override));
  3934. int new_scene_id = p_id_override >= 0 ? p_id_override : next_scene_id;
  3935. scenes[new_scene_id] = SceneData();
  3936. scenes_ids.append(new_scene_id);
  3937. scenes_ids.sort();
  3938. set_scene_tile_scene(new_scene_id, p_packed_scene);
  3939. _compute_next_alternative_id();
  3940. emit_signal(SNAME("changed"));
  3941. return new_scene_id;
  3942. }
  3943. void TileSetScenesCollectionSource::set_scene_tile_id(int p_id, int p_new_id) {
  3944. ERR_FAIL_COND(p_new_id < 0);
  3945. ERR_FAIL_COND(!has_scene_tile_id(p_id));
  3946. ERR_FAIL_COND(has_scene_tile_id(p_new_id));
  3947. scenes[p_new_id] = SceneData();
  3948. scenes[p_new_id] = scenes[p_id];
  3949. scenes_ids.append(p_new_id);
  3950. scenes_ids.sort();
  3951. _compute_next_alternative_id();
  3952. scenes.erase(p_id);
  3953. scenes_ids.erase(p_id);
  3954. emit_signal(SNAME("changed"));
  3955. }
  3956. void TileSetScenesCollectionSource::set_scene_tile_scene(int p_id, Ref<PackedScene> p_packed_scene) {
  3957. ERR_FAIL_COND(!scenes.has(p_id));
  3958. if (p_packed_scene.is_valid()) {
  3959. // Make sure we have a root node. Supposed to be at 0 index because find_node_by_path() does not seem to work.
  3960. ERR_FAIL_COND(!p_packed_scene->get_state().is_valid());
  3961. ERR_FAIL_COND(p_packed_scene->get_state()->get_node_count() < 1);
  3962. // Check if it extends CanvasItem.
  3963. String type = p_packed_scene->get_state()->get_node_type(0);
  3964. bool extends_correct_class = ClassDB::is_parent_class(type, "Control") || ClassDB::is_parent_class(type, "Node2D");
  3965. ERR_FAIL_COND_MSG(!extends_correct_class, vformat("Invalid PackedScene for TileSetScenesCollectionSource: %s. Root node should extend Control or Node2D.", p_packed_scene->get_path()));
  3966. scenes[p_id].scene = p_packed_scene;
  3967. } else {
  3968. scenes[p_id].scene = Ref<PackedScene>();
  3969. }
  3970. emit_signal(SNAME("changed"));
  3971. }
  3972. Ref<PackedScene> TileSetScenesCollectionSource::get_scene_tile_scene(int p_id) const {
  3973. ERR_FAIL_COND_V(!scenes.has(p_id), Ref<PackedScene>());
  3974. return scenes[p_id].scene;
  3975. }
  3976. void TileSetScenesCollectionSource::set_scene_tile_display_placeholder(int p_id, bool p_display_placeholder) {
  3977. ERR_FAIL_COND(!scenes.has(p_id));
  3978. scenes[p_id].display_placeholder = p_display_placeholder;
  3979. emit_signal(SNAME("changed"));
  3980. }
  3981. bool TileSetScenesCollectionSource::get_scene_tile_display_placeholder(int p_id) const {
  3982. ERR_FAIL_COND_V(!scenes.has(p_id), false);
  3983. return scenes[p_id].display_placeholder;
  3984. }
  3985. void TileSetScenesCollectionSource::remove_scene_tile(int p_id) {
  3986. ERR_FAIL_COND(!scenes.has(p_id));
  3987. scenes.erase(p_id);
  3988. scenes_ids.erase(p_id);
  3989. emit_signal(SNAME("changed"));
  3990. }
  3991. int TileSetScenesCollectionSource::get_next_scene_tile_id() const {
  3992. return next_scene_id;
  3993. }
  3994. bool TileSetScenesCollectionSource::_set(const StringName &p_name, const Variant &p_value) {
  3995. Vector<String> components = String(p_name).split("/", true, 2);
  3996. if (components.size() >= 2 && components[0] == "scenes" && components[1].is_valid_int()) {
  3997. int scene_id = components[1].to_int();
  3998. if (components.size() >= 3 && components[2] == "scene") {
  3999. if (has_scene_tile_id(scene_id)) {
  4000. set_scene_tile_scene(scene_id, p_value);
  4001. } else {
  4002. create_scene_tile(p_value, scene_id);
  4003. }
  4004. return true;
  4005. } else if (components.size() >= 3 && components[2] == "display_placeholder") {
  4006. if (!has_scene_tile_id(scene_id)) {
  4007. create_scene_tile(p_value, scene_id);
  4008. }
  4009. return true;
  4010. }
  4011. }
  4012. return false;
  4013. }
  4014. bool TileSetScenesCollectionSource::_get(const StringName &p_name, Variant &r_ret) const {
  4015. Vector<String> components = String(p_name).split("/", true, 2);
  4016. if (components.size() >= 2 && components[0] == "scenes" && components[1].is_valid_int() && scenes.has(components[1].to_int())) {
  4017. if (components.size() >= 3 && components[2] == "scene") {
  4018. r_ret = scenes[components[1].to_int()].scene;
  4019. return true;
  4020. } else if (components.size() >= 3 && components[2] == "display_placeholder") {
  4021. r_ret = scenes[components[1].to_int()].scene;
  4022. return true;
  4023. }
  4024. }
  4025. return false;
  4026. }
  4027. void TileSetScenesCollectionSource::_get_property_list(List<PropertyInfo> *p_list) const {
  4028. for (int i = 0; i < scenes_ids.size(); i++) {
  4029. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("scenes/%d/scene", scenes_ids[i]), PROPERTY_HINT_RESOURCE_TYPE, "TileSetScenesCollectionSource"));
  4030. PropertyInfo property_info = PropertyInfo(Variant::BOOL, vformat("scenes/%d/display_placeholder", scenes_ids[i]));
  4031. if (scenes[scenes_ids[i]].display_placeholder == false) {
  4032. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4033. }
  4034. p_list->push_back(property_info);
  4035. }
  4036. }
  4037. void TileSetScenesCollectionSource::_bind_methods() {
  4038. ClassDB::bind_method(D_METHOD("get_scene_tiles_count"), &TileSetScenesCollectionSource::get_scene_tiles_count);
  4039. ClassDB::bind_method(D_METHOD("get_scene_tile_id", "index"), &TileSetScenesCollectionSource::get_scene_tile_id);
  4040. ClassDB::bind_method(D_METHOD("has_scene_tile_id", "id"), &TileSetScenesCollectionSource::has_scene_tile_id);
  4041. ClassDB::bind_method(D_METHOD("create_scene_tile", "packed_scene", "id_override"), &TileSetScenesCollectionSource::create_scene_tile, DEFVAL(INVALID_TILE_ALTERNATIVE));
  4042. ClassDB::bind_method(D_METHOD("set_scene_tile_id", "id", "new_id"), &TileSetScenesCollectionSource::set_scene_tile_id);
  4043. ClassDB::bind_method(D_METHOD("set_scene_tile_scene", "id", "packed_scene"), &TileSetScenesCollectionSource::set_scene_tile_scene);
  4044. ClassDB::bind_method(D_METHOD("get_scene_tile_scene", "id"), &TileSetScenesCollectionSource::get_scene_tile_scene);
  4045. ClassDB::bind_method(D_METHOD("set_scene_tile_display_placeholder", "id", "display_placeholder"), &TileSetScenesCollectionSource::set_scene_tile_display_placeholder);
  4046. ClassDB::bind_method(D_METHOD("get_scene_tile_display_placeholder", "id"), &TileSetScenesCollectionSource::get_scene_tile_display_placeholder);
  4047. ClassDB::bind_method(D_METHOD("remove_scene_tile", "id"), &TileSetScenesCollectionSource::remove_scene_tile);
  4048. ClassDB::bind_method(D_METHOD("get_next_scene_tile_id"), &TileSetScenesCollectionSource::get_next_scene_tile_id);
  4049. }
  4050. /////////////////////////////// TileData //////////////////////////////////////
  4051. void TileData::set_tile_set(const TileSet *p_tile_set) {
  4052. tile_set = p_tile_set;
  4053. notify_tile_data_properties_should_change();
  4054. }
  4055. void TileData::notify_tile_data_properties_should_change() {
  4056. if (!tile_set) {
  4057. return;
  4058. }
  4059. occluders.resize(tile_set->get_occlusion_layers_count());
  4060. physics.resize(tile_set->get_physics_layers_count());
  4061. for (int bit_index = 0; bit_index < 16; bit_index++) {
  4062. if (terrain_set < 0 || terrain_peering_bits[bit_index] >= tile_set->get_terrains_count(terrain_set)) {
  4063. terrain_peering_bits[bit_index] = -1;
  4064. }
  4065. }
  4066. navigation.resize(tile_set->get_navigation_layers_count());
  4067. // Convert custom data to the new type.
  4068. custom_data.resize(tile_set->get_custom_data_layers_count());
  4069. for (int i = 0; i < custom_data.size(); i++) {
  4070. if (custom_data[i].get_type() != tile_set->get_custom_data_type(i)) {
  4071. Variant new_val;
  4072. Callable::CallError error;
  4073. if (Variant::can_convert(custom_data[i].get_type(), tile_set->get_custom_data_type(i))) {
  4074. const Variant *args[] = { &custom_data[i] };
  4075. Variant::construct(tile_set->get_custom_data_type(i), new_val, args, 1, error);
  4076. } else {
  4077. Variant::construct(tile_set->get_custom_data_type(i), new_val, nullptr, 0, error);
  4078. }
  4079. custom_data.write[i] = new_val;
  4080. }
  4081. }
  4082. notify_property_list_changed();
  4083. emit_signal(SNAME("changed"));
  4084. }
  4085. void TileData::add_occlusion_layer(int p_to_pos) {
  4086. if (p_to_pos < 0) {
  4087. p_to_pos = occluders.size();
  4088. }
  4089. ERR_FAIL_INDEX(p_to_pos, occluders.size() + 1);
  4090. occluders.insert(p_to_pos, Ref<OccluderPolygon2D>());
  4091. }
  4092. void TileData::move_occlusion_layer(int p_from_index, int p_to_pos) {
  4093. ERR_FAIL_INDEX(p_from_index, occluders.size());
  4094. ERR_FAIL_INDEX(p_to_pos, occluders.size() + 1);
  4095. occluders.insert(p_to_pos, occluders[p_from_index]);
  4096. occluders.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4097. }
  4098. void TileData::remove_occlusion_layer(int p_index) {
  4099. ERR_FAIL_INDEX(p_index, occluders.size());
  4100. occluders.remove_at(p_index);
  4101. }
  4102. void TileData::add_physics_layer(int p_to_pos) {
  4103. if (p_to_pos < 0) {
  4104. p_to_pos = physics.size();
  4105. }
  4106. ERR_FAIL_INDEX(p_to_pos, physics.size() + 1);
  4107. physics.insert(p_to_pos, PhysicsLayerTileData());
  4108. }
  4109. void TileData::move_physics_layer(int p_from_index, int p_to_pos) {
  4110. ERR_FAIL_INDEX(p_from_index, physics.size());
  4111. ERR_FAIL_INDEX(p_to_pos, physics.size() + 1);
  4112. physics.insert(p_to_pos, physics[p_from_index]);
  4113. physics.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4114. }
  4115. void TileData::remove_physics_layer(int p_index) {
  4116. ERR_FAIL_INDEX(p_index, physics.size());
  4117. physics.remove_at(p_index);
  4118. }
  4119. void TileData::add_terrain_set(int p_to_pos) {
  4120. if (p_to_pos >= 0 && p_to_pos <= terrain_set) {
  4121. terrain_set += 1;
  4122. }
  4123. }
  4124. void TileData::move_terrain_set(int p_from_index, int p_to_pos) {
  4125. if (p_from_index == terrain_set) {
  4126. terrain_set = (p_from_index < p_to_pos) ? p_to_pos - 1 : p_to_pos;
  4127. } else {
  4128. if (p_from_index < terrain_set) {
  4129. terrain_set -= 1;
  4130. }
  4131. if (p_to_pos <= terrain_set) {
  4132. terrain_set += 1;
  4133. }
  4134. }
  4135. }
  4136. void TileData::remove_terrain_set(int p_index) {
  4137. if (p_index == terrain_set) {
  4138. terrain_set = -1;
  4139. for (int i = 0; i < 16; i++) {
  4140. terrain_peering_bits[i] = -1;
  4141. }
  4142. } else if (terrain_set > p_index) {
  4143. terrain_set -= 1;
  4144. }
  4145. }
  4146. void TileData::add_terrain(int p_terrain_set, int p_to_pos) {
  4147. if (terrain_set == p_terrain_set) {
  4148. for (int i = 0; i < 16; i++) {
  4149. if (p_to_pos >= 0 && p_to_pos <= terrain_peering_bits[i]) {
  4150. terrain_peering_bits[i] += 1;
  4151. }
  4152. }
  4153. }
  4154. }
  4155. void TileData::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  4156. if (terrain_set == p_terrain_set) {
  4157. for (int i = 0; i < 16; i++) {
  4158. if (p_from_index == terrain_peering_bits[i]) {
  4159. terrain_peering_bits[i] = (p_from_index < p_to_pos) ? p_to_pos - 1 : p_to_pos;
  4160. } else {
  4161. if (p_from_index < terrain_peering_bits[i]) {
  4162. terrain_peering_bits[i] -= 1;
  4163. }
  4164. if (p_to_pos <= terrain_peering_bits[i]) {
  4165. terrain_peering_bits[i] += 1;
  4166. }
  4167. }
  4168. }
  4169. }
  4170. }
  4171. void TileData::remove_terrain(int p_terrain_set, int p_index) {
  4172. if (terrain_set == p_terrain_set) {
  4173. for (int i = 0; i < 16; i++) {
  4174. if (terrain_peering_bits[i] == p_index) {
  4175. terrain_peering_bits[i] = -1;
  4176. } else if (terrain_peering_bits[i] > p_index) {
  4177. terrain_peering_bits[i] -= 1;
  4178. }
  4179. }
  4180. }
  4181. }
  4182. void TileData::add_navigation_layer(int p_to_pos) {
  4183. if (p_to_pos < 0) {
  4184. p_to_pos = navigation.size();
  4185. }
  4186. ERR_FAIL_INDEX(p_to_pos, navigation.size() + 1);
  4187. navigation.insert(p_to_pos, Ref<NavigationPolygon>());
  4188. }
  4189. void TileData::move_navigation_layer(int p_from_index, int p_to_pos) {
  4190. ERR_FAIL_INDEX(p_from_index, navigation.size());
  4191. ERR_FAIL_INDEX(p_to_pos, navigation.size() + 1);
  4192. navigation.insert(p_to_pos, navigation[p_from_index]);
  4193. navigation.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4194. }
  4195. void TileData::remove_navigation_layer(int p_index) {
  4196. ERR_FAIL_INDEX(p_index, navigation.size());
  4197. navigation.remove_at(p_index);
  4198. }
  4199. void TileData::add_custom_data_layer(int p_to_pos) {
  4200. if (p_to_pos < 0) {
  4201. p_to_pos = custom_data.size();
  4202. }
  4203. ERR_FAIL_INDEX(p_to_pos, custom_data.size() + 1);
  4204. custom_data.insert(p_to_pos, Variant());
  4205. }
  4206. void TileData::move_custom_data_layer(int p_from_index, int p_to_pos) {
  4207. ERR_FAIL_INDEX(p_from_index, custom_data.size());
  4208. ERR_FAIL_INDEX(p_to_pos, custom_data.size() + 1);
  4209. custom_data.insert(p_to_pos, navigation[p_from_index]);
  4210. custom_data.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4211. }
  4212. void TileData::remove_custom_data_layer(int p_index) {
  4213. ERR_FAIL_INDEX(p_index, custom_data.size());
  4214. custom_data.remove_at(p_index);
  4215. }
  4216. void TileData::reset_state() {
  4217. occluders.clear();
  4218. physics.clear();
  4219. navigation.clear();
  4220. custom_data.clear();
  4221. }
  4222. void TileData::set_allow_transform(bool p_allow_transform) {
  4223. allow_transform = p_allow_transform;
  4224. }
  4225. bool TileData::is_allowing_transform() const {
  4226. return allow_transform;
  4227. }
  4228. TileData *TileData::duplicate() {
  4229. TileData *output = memnew(TileData);
  4230. output->tile_set = tile_set;
  4231. output->allow_transform = allow_transform;
  4232. // Rendering
  4233. output->flip_h = flip_h;
  4234. output->flip_v = flip_v;
  4235. output->transpose = transpose;
  4236. output->tex_offset = tex_offset;
  4237. output->material = material;
  4238. output->modulate = modulate;
  4239. output->z_index = z_index;
  4240. output->y_sort_origin = y_sort_origin;
  4241. output->occluders = occluders;
  4242. // Physics
  4243. output->physics = physics;
  4244. // Terrain
  4245. output->terrain_set = -1;
  4246. memcpy(output->terrain_peering_bits, terrain_peering_bits, 16 * sizeof(int));
  4247. // Navigation
  4248. output->navigation = navigation;
  4249. // Misc
  4250. output->probability = probability;
  4251. // Custom data
  4252. output->custom_data = custom_data;
  4253. return output;
  4254. }
  4255. // Rendering
  4256. void TileData::set_flip_h(bool p_flip_h) {
  4257. ERR_FAIL_COND_MSG(!allow_transform && p_flip_h, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  4258. flip_h = p_flip_h;
  4259. emit_signal(SNAME("changed"));
  4260. }
  4261. bool TileData::get_flip_h() const {
  4262. return flip_h;
  4263. }
  4264. void TileData::set_flip_v(bool p_flip_v) {
  4265. ERR_FAIL_COND_MSG(!allow_transform && p_flip_v, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  4266. flip_v = p_flip_v;
  4267. emit_signal(SNAME("changed"));
  4268. }
  4269. bool TileData::get_flip_v() const {
  4270. return flip_v;
  4271. }
  4272. void TileData::set_transpose(bool p_transpose) {
  4273. ERR_FAIL_COND_MSG(!allow_transform && p_transpose, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  4274. transpose = p_transpose;
  4275. emit_signal(SNAME("changed"));
  4276. }
  4277. bool TileData::get_transpose() const {
  4278. return transpose;
  4279. }
  4280. void TileData::set_texture_offset(Vector2i p_texture_offset) {
  4281. tex_offset = p_texture_offset;
  4282. emit_signal(SNAME("changed"));
  4283. }
  4284. Vector2i TileData::get_texture_offset() const {
  4285. return tex_offset;
  4286. }
  4287. void TileData::set_material(Ref<ShaderMaterial> p_material) {
  4288. material = p_material;
  4289. emit_signal(SNAME("changed"));
  4290. }
  4291. Ref<ShaderMaterial> TileData::get_material() const {
  4292. return material;
  4293. }
  4294. void TileData::set_modulate(Color p_modulate) {
  4295. modulate = p_modulate;
  4296. emit_signal(SNAME("changed"));
  4297. }
  4298. Color TileData::get_modulate() const {
  4299. return modulate;
  4300. }
  4301. void TileData::set_z_index(int p_z_index) {
  4302. z_index = p_z_index;
  4303. emit_signal(SNAME("changed"));
  4304. }
  4305. int TileData::get_z_index() const {
  4306. return z_index;
  4307. }
  4308. void TileData::set_y_sort_origin(int p_y_sort_origin) {
  4309. y_sort_origin = p_y_sort_origin;
  4310. emit_signal(SNAME("changed"));
  4311. }
  4312. int TileData::get_y_sort_origin() const {
  4313. return y_sort_origin;
  4314. }
  4315. void TileData::set_occluder(int p_layer_id, Ref<OccluderPolygon2D> p_occluder_polygon) {
  4316. ERR_FAIL_INDEX(p_layer_id, occluders.size());
  4317. occluders.write[p_layer_id] = p_occluder_polygon;
  4318. emit_signal(SNAME("changed"));
  4319. }
  4320. Ref<OccluderPolygon2D> TileData::get_occluder(int p_layer_id) const {
  4321. ERR_FAIL_INDEX_V(p_layer_id, occluders.size(), Ref<OccluderPolygon2D>());
  4322. return occluders[p_layer_id];
  4323. }
  4324. // Physics
  4325. void TileData::set_constant_linear_velocity(int p_layer_id, const Vector2 &p_velocity) {
  4326. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4327. physics.write[p_layer_id].linear_velocity = p_velocity;
  4328. emit_signal(SNAME("changed"));
  4329. }
  4330. Vector2 TileData::get_constant_linear_velocity(int p_layer_id) const {
  4331. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Vector2());
  4332. return physics[p_layer_id].linear_velocity;
  4333. }
  4334. void TileData::set_constant_angular_velocity(int p_layer_id, real_t p_velocity) {
  4335. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4336. physics.write[p_layer_id].angular_velocity = p_velocity;
  4337. emit_signal(SNAME("changed"));
  4338. }
  4339. real_t TileData::get_constant_angular_velocity(int p_layer_id) const {
  4340. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0.0);
  4341. return physics[p_layer_id].angular_velocity;
  4342. }
  4343. void TileData::set_collision_polygons_count(int p_layer_id, int p_polygons_count) {
  4344. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4345. ERR_FAIL_COND(p_polygons_count < 0);
  4346. if (p_polygons_count == physics.write[p_layer_id].polygons.size()) {
  4347. return;
  4348. }
  4349. physics.write[p_layer_id].polygons.resize(p_polygons_count);
  4350. notify_property_list_changed();
  4351. emit_signal(SNAME("changed"));
  4352. }
  4353. int TileData::get_collision_polygons_count(int p_layer_id) const {
  4354. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0);
  4355. return physics[p_layer_id].polygons.size();
  4356. }
  4357. void TileData::add_collision_polygon(int p_layer_id) {
  4358. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4359. physics.write[p_layer_id].polygons.push_back(PhysicsLayerTileData::PolygonShapeTileData());
  4360. emit_signal(SNAME("changed"));
  4361. }
  4362. void TileData::remove_collision_polygon(int p_layer_id, int p_polygon_index) {
  4363. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4364. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4365. physics.write[p_layer_id].polygons.remove_at(p_polygon_index);
  4366. emit_signal(SNAME("changed"));
  4367. }
  4368. void TileData::set_collision_polygon_points(int p_layer_id, int p_polygon_index, Vector<Vector2> p_polygon) {
  4369. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4370. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4371. ERR_FAIL_COND_MSG(p_polygon.size() != 0 && p_polygon.size() < 3, "Invalid polygon. Needs either 0 or more than 3 points.");
  4372. if (p_polygon.is_empty()) {
  4373. physics.write[p_layer_id].polygons.write[p_polygon_index].shapes.clear();
  4374. } else {
  4375. // Decompose into convex shapes.
  4376. Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(p_polygon);
  4377. ERR_FAIL_COND_MSG(decomp.is_empty(), "Could not decompose the polygon into convex shapes.");
  4378. physics.write[p_layer_id].polygons.write[p_polygon_index].shapes.resize(decomp.size());
  4379. for (int i = 0; i < decomp.size(); i++) {
  4380. Ref<ConvexPolygonShape2D> shape;
  4381. shape.instantiate();
  4382. shape->set_points(decomp[i]);
  4383. physics.write[p_layer_id].polygons.write[p_polygon_index].shapes[i] = shape;
  4384. }
  4385. }
  4386. physics.write[p_layer_id].polygons.write[p_polygon_index].polygon = p_polygon;
  4387. emit_signal(SNAME("changed"));
  4388. }
  4389. Vector<Vector2> TileData::get_collision_polygon_points(int p_layer_id, int p_polygon_index) const {
  4390. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Vector<Vector2>());
  4391. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), Vector<Vector2>());
  4392. return physics[p_layer_id].polygons[p_polygon_index].polygon;
  4393. }
  4394. void TileData::set_collision_polygon_one_way(int p_layer_id, int p_polygon_index, bool p_one_way) {
  4395. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4396. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4397. physics.write[p_layer_id].polygons.write[p_polygon_index].one_way = p_one_way;
  4398. emit_signal(SNAME("changed"));
  4399. }
  4400. bool TileData::is_collision_polygon_one_way(int p_layer_id, int p_polygon_index) const {
  4401. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), false);
  4402. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), false);
  4403. return physics[p_layer_id].polygons[p_polygon_index].one_way;
  4404. }
  4405. void TileData::set_collision_polygon_one_way_margin(int p_layer_id, int p_polygon_index, float p_one_way_margin) {
  4406. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4407. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4408. physics.write[p_layer_id].polygons.write[p_polygon_index].one_way_margin = p_one_way_margin;
  4409. emit_signal(SNAME("changed"));
  4410. }
  4411. float TileData::get_collision_polygon_one_way_margin(int p_layer_id, int p_polygon_index) const {
  4412. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0.0);
  4413. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), 0.0);
  4414. return physics[p_layer_id].polygons[p_polygon_index].one_way_margin;
  4415. }
  4416. int TileData::get_collision_polygon_shapes_count(int p_layer_id, int p_polygon_index) const {
  4417. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0);
  4418. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), 0);
  4419. return physics[p_layer_id].polygons[p_polygon_index].shapes.size();
  4420. }
  4421. Ref<ConvexPolygonShape2D> TileData::get_collision_polygon_shape(int p_layer_id, int p_polygon_index, int shape_index) const {
  4422. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Ref<ConvexPolygonShape2D>());
  4423. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), Ref<ConvexPolygonShape2D>());
  4424. ERR_FAIL_INDEX_V(shape_index, (int)physics[p_layer_id].polygons[p_polygon_index].shapes.size(), Ref<ConvexPolygonShape2D>());
  4425. return physics[p_layer_id].polygons[p_polygon_index].shapes[shape_index];
  4426. }
  4427. // Terrain
  4428. void TileData::set_terrain_set(int p_terrain_set) {
  4429. ERR_FAIL_COND(p_terrain_set < -1);
  4430. if (p_terrain_set == terrain_set) {
  4431. return;
  4432. }
  4433. if (tile_set) {
  4434. ERR_FAIL_COND(p_terrain_set >= tile_set->get_terrain_sets_count());
  4435. for (int i = 0; i < 16; i++) {
  4436. terrain_peering_bits[i] = -1;
  4437. }
  4438. }
  4439. terrain_set = p_terrain_set;
  4440. notify_property_list_changed();
  4441. emit_signal(SNAME("changed"));
  4442. }
  4443. int TileData::get_terrain_set() const {
  4444. return terrain_set;
  4445. }
  4446. void TileData::set_peering_bit_terrain(TileSet::CellNeighbor p_peering_bit, int p_terrain_index) {
  4447. ERR_FAIL_INDEX(p_peering_bit, TileSet::CellNeighbor::CELL_NEIGHBOR_MAX);
  4448. ERR_FAIL_COND(terrain_set < 0);
  4449. ERR_FAIL_COND(p_terrain_index < -1);
  4450. if (tile_set) {
  4451. ERR_FAIL_COND(p_terrain_index >= tile_set->get_terrains_count(terrain_set));
  4452. ERR_FAIL_COND(!is_valid_peering_bit_terrain(p_peering_bit));
  4453. }
  4454. terrain_peering_bits[p_peering_bit] = p_terrain_index;
  4455. emit_signal(SNAME("changed"));
  4456. }
  4457. int TileData::get_peering_bit_terrain(TileSet::CellNeighbor p_peering_bit) const {
  4458. ERR_FAIL_COND_V(!is_valid_peering_bit_terrain(p_peering_bit), -1);
  4459. return terrain_peering_bits[p_peering_bit];
  4460. }
  4461. bool TileData::is_valid_peering_bit_terrain(TileSet::CellNeighbor p_peering_bit) const {
  4462. ERR_FAIL_COND_V(!tile_set, false);
  4463. return tile_set->is_valid_peering_bit_terrain(terrain_set, p_peering_bit);
  4464. }
  4465. TileSet::TerrainsPattern TileData::get_terrains_pattern() const {
  4466. ERR_FAIL_COND_V(!tile_set, TileSet::TerrainsPattern());
  4467. TileSet::TerrainsPattern output(tile_set, terrain_set);
  4468. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4469. if (tile_set->is_valid_peering_bit_terrain(terrain_set, TileSet::CellNeighbor(i))) {
  4470. output.set_terrain(TileSet::CellNeighbor(i), get_peering_bit_terrain(TileSet::CellNeighbor(i)));
  4471. }
  4472. }
  4473. return output;
  4474. }
  4475. // Navigation
  4476. void TileData::set_navigation_polygon(int p_layer_id, Ref<NavigationPolygon> p_navigation_polygon) {
  4477. ERR_FAIL_INDEX(p_layer_id, navigation.size());
  4478. navigation.write[p_layer_id] = p_navigation_polygon;
  4479. emit_signal(SNAME("changed"));
  4480. }
  4481. Ref<NavigationPolygon> TileData::get_navigation_polygon(int p_layer_id) const {
  4482. ERR_FAIL_INDEX_V(p_layer_id, navigation.size(), Ref<NavigationPolygon>());
  4483. return navigation[p_layer_id];
  4484. }
  4485. // Misc
  4486. void TileData::set_probability(float p_probability) {
  4487. ERR_FAIL_COND(p_probability < 0.0);
  4488. probability = p_probability;
  4489. emit_signal(SNAME("changed"));
  4490. }
  4491. float TileData::get_probability() const {
  4492. return probability;
  4493. }
  4494. // Custom data
  4495. void TileData::set_custom_data(String p_layer_name, Variant p_value) {
  4496. ERR_FAIL_COND(!tile_set);
  4497. int p_layer_id = tile_set->get_custom_data_layer_by_name(p_layer_name);
  4498. ERR_FAIL_COND_MSG(p_layer_id < 0, vformat("TileSet has no layer with name: %s", p_layer_name));
  4499. set_custom_data_by_layer_id(p_layer_id, p_value);
  4500. }
  4501. Variant TileData::get_custom_data(String p_layer_name) const {
  4502. ERR_FAIL_COND_V(!tile_set, Variant());
  4503. int p_layer_id = tile_set->get_custom_data_layer_by_name(p_layer_name);
  4504. ERR_FAIL_COND_V_MSG(p_layer_id < 0, Variant(), vformat("TileSet has no layer with name: %s", p_layer_name));
  4505. return get_custom_data_by_layer_id(p_layer_id);
  4506. }
  4507. void TileData::set_custom_data_by_layer_id(int p_layer_id, Variant p_value) {
  4508. ERR_FAIL_INDEX(p_layer_id, custom_data.size());
  4509. custom_data.write[p_layer_id] = p_value;
  4510. emit_signal(SNAME("changed"));
  4511. }
  4512. Variant TileData::get_custom_data_by_layer_id(int p_layer_id) const {
  4513. ERR_FAIL_INDEX_V(p_layer_id, custom_data.size(), Variant());
  4514. return custom_data[p_layer_id];
  4515. }
  4516. bool TileData::_set(const StringName &p_name, const Variant &p_value) {
  4517. Vector<String> components = String(p_name).split("/", true, 2);
  4518. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  4519. // Occlusion layers.
  4520. int layer_index = components[0].trim_prefix("occlusion_layer_").to_int();
  4521. ERR_FAIL_COND_V(layer_index < 0, false);
  4522. if (components[1] == "polygon") {
  4523. Ref<OccluderPolygon2D> polygon = p_value;
  4524. if (layer_index >= occluders.size()) {
  4525. if (tile_set) {
  4526. return false;
  4527. } else {
  4528. occluders.resize(layer_index + 1);
  4529. }
  4530. }
  4531. set_occluder(layer_index, polygon);
  4532. return true;
  4533. }
  4534. } else if (components.size() >= 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  4535. // Physics layers.
  4536. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  4537. ERR_FAIL_COND_V(layer_index < 0, false);
  4538. if (components.size() == 2) {
  4539. if (layer_index >= physics.size()) {
  4540. if (tile_set) {
  4541. return false;
  4542. } else {
  4543. physics.resize(layer_index + 1);
  4544. }
  4545. }
  4546. if (components[1] == "linear_velocity") {
  4547. set_constant_linear_velocity(layer_index, p_value);
  4548. return true;
  4549. } else if (components[1] == "angular_velocity") {
  4550. set_constant_angular_velocity(layer_index, p_value);
  4551. return true;
  4552. } else if (components[1] == "polygons_count") {
  4553. if (p_value.get_type() != Variant::INT) {
  4554. return false;
  4555. }
  4556. set_collision_polygons_count(layer_index, p_value);
  4557. return true;
  4558. }
  4559. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  4560. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  4561. ERR_FAIL_COND_V(polygon_index < 0, false);
  4562. if (components[2] == "points" || components[2] == "one_way" || components[2] == "one_way_margin") {
  4563. if (layer_index >= physics.size()) {
  4564. if (tile_set) {
  4565. return false;
  4566. } else {
  4567. physics.resize(layer_index + 1);
  4568. }
  4569. }
  4570. if (polygon_index >= physics[layer_index].polygons.size()) {
  4571. physics.write[layer_index].polygons.resize(polygon_index + 1);
  4572. }
  4573. }
  4574. if (components[2] == "points") {
  4575. Vector<Vector2> polygon = p_value;
  4576. set_collision_polygon_points(layer_index, polygon_index, polygon);
  4577. return true;
  4578. } else if (components[2] == "one_way") {
  4579. set_collision_polygon_one_way(layer_index, polygon_index, p_value);
  4580. return true;
  4581. } else if (components[2] == "one_way_margin") {
  4582. set_collision_polygon_one_way_margin(layer_index, polygon_index, p_value);
  4583. return true;
  4584. }
  4585. }
  4586. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  4587. // Navigation layers.
  4588. int layer_index = components[0].trim_prefix("navigation_layer_").to_int();
  4589. ERR_FAIL_COND_V(layer_index < 0, false);
  4590. if (components[1] == "polygon") {
  4591. Ref<NavigationPolygon> polygon = p_value;
  4592. if (layer_index >= navigation.size()) {
  4593. if (tile_set) {
  4594. return false;
  4595. } else {
  4596. navigation.resize(layer_index + 1);
  4597. }
  4598. }
  4599. set_navigation_polygon(layer_index, polygon);
  4600. return true;
  4601. }
  4602. } else if (components.size() == 2 && components[0] == "terrains_peering_bit") {
  4603. // Terrains.
  4604. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4605. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  4606. if (components[1] == TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]) {
  4607. set_peering_bit_terrain(bit, p_value);
  4608. return true;
  4609. }
  4610. }
  4611. return false;
  4612. } else if (components.size() == 1 && components[0].begins_with("custom_data_") && components[0].trim_prefix("custom_data_").is_valid_int()) {
  4613. // Custom data layers.
  4614. int layer_index = components[0].trim_prefix("custom_data_").to_int();
  4615. ERR_FAIL_COND_V(layer_index < 0, false);
  4616. if (layer_index >= custom_data.size()) {
  4617. if (tile_set) {
  4618. return false;
  4619. } else {
  4620. custom_data.resize(layer_index + 1);
  4621. }
  4622. }
  4623. set_custom_data_by_layer_id(layer_index, p_value);
  4624. return true;
  4625. }
  4626. return false;
  4627. }
  4628. bool TileData::_get(const StringName &p_name, Variant &r_ret) const {
  4629. Vector<String> components = String(p_name).split("/", true, 2);
  4630. if (tile_set) {
  4631. if (components.size() == 2 && components[0].begins_with("occlusion_layer") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  4632. // Occlusion layers.
  4633. int layer_index = components[0].trim_prefix("occlusion_layer_").to_int();
  4634. ERR_FAIL_COND_V(layer_index < 0, false);
  4635. if (layer_index >= occluders.size()) {
  4636. return false;
  4637. }
  4638. if (components[1] == "polygon") {
  4639. r_ret = get_occluder(layer_index);
  4640. return true;
  4641. }
  4642. } else if (components.size() >= 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  4643. // Physics layers.
  4644. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  4645. ERR_FAIL_COND_V(layer_index < 0, false);
  4646. if (layer_index >= physics.size()) {
  4647. return false;
  4648. }
  4649. if (components.size() == 2) {
  4650. if (components[1] == "linear_velocity") {
  4651. r_ret = get_constant_linear_velocity(layer_index);
  4652. return true;
  4653. } else if (components[1] == "angular_velocity") {
  4654. r_ret = get_constant_angular_velocity(layer_index);
  4655. return true;
  4656. } else if (components[1] == "polygons_count") {
  4657. r_ret = get_collision_polygons_count(layer_index);
  4658. return true;
  4659. }
  4660. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  4661. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  4662. ERR_FAIL_COND_V(polygon_index < 0, false);
  4663. if (polygon_index >= physics[layer_index].polygons.size()) {
  4664. return false;
  4665. }
  4666. if (components[2] == "points") {
  4667. r_ret = get_collision_polygon_points(layer_index, polygon_index);
  4668. return true;
  4669. } else if (components[2] == "one_way") {
  4670. r_ret = is_collision_polygon_one_way(layer_index, polygon_index);
  4671. return true;
  4672. } else if (components[2] == "one_way_margin") {
  4673. r_ret = get_collision_polygon_one_way_margin(layer_index, polygon_index);
  4674. return true;
  4675. }
  4676. }
  4677. } else if (components.size() == 2 && components[0] == "terrains_peering_bit") {
  4678. // Terrains.
  4679. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4680. if (components[1] == TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]) {
  4681. r_ret = terrain_peering_bits[i];
  4682. return true;
  4683. }
  4684. }
  4685. return false;
  4686. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  4687. // Occlusion layers.
  4688. int layer_index = components[0].trim_prefix("navigation_layer_").to_int();
  4689. ERR_FAIL_COND_V(layer_index < 0, false);
  4690. if (layer_index >= navigation.size()) {
  4691. return false;
  4692. }
  4693. if (components[1] == "polygon") {
  4694. r_ret = get_navigation_polygon(layer_index);
  4695. return true;
  4696. }
  4697. } else if (components.size() == 1 && components[0].begins_with("custom_data_") && components[0].trim_prefix("custom_data_").is_valid_int()) {
  4698. // Custom data layers.
  4699. int layer_index = components[0].trim_prefix("custom_data_").to_int();
  4700. ERR_FAIL_COND_V(layer_index < 0, false);
  4701. if (layer_index >= custom_data.size()) {
  4702. return false;
  4703. }
  4704. r_ret = get_custom_data_by_layer_id(layer_index);
  4705. return true;
  4706. }
  4707. }
  4708. return false;
  4709. }
  4710. void TileData::_get_property_list(List<PropertyInfo> *p_list) const {
  4711. PropertyInfo property_info;
  4712. // Add the groups manually.
  4713. if (tile_set) {
  4714. // Occlusion layers.
  4715. p_list->push_back(PropertyInfo(Variant::NIL, "Rendering", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4716. for (int i = 0; i < occluders.size(); i++) {
  4717. // occlusion_layer_%d/polygon
  4718. property_info = PropertyInfo(Variant::OBJECT, vformat("occlusion_layer_%d/polygon", i), PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D", PROPERTY_USAGE_DEFAULT);
  4719. if (!occluders[i].is_valid()) {
  4720. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4721. }
  4722. p_list->push_back(property_info);
  4723. }
  4724. // Physics layers.
  4725. p_list->push_back(PropertyInfo(Variant::NIL, "Physics", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4726. for (int i = 0; i < physics.size(); i++) {
  4727. p_list->push_back(PropertyInfo(Variant::VECTOR2, vformat("physics_layer_%d/linear_velocity", i), PROPERTY_HINT_NONE));
  4728. p_list->push_back(PropertyInfo(Variant::FLOAT, vformat("physics_layer_%d/angular_velocity", i), PROPERTY_HINT_NONE));
  4729. p_list->push_back(PropertyInfo(Variant::INT, vformat("physics_layer_%d/polygons_count", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  4730. for (int j = 0; j < physics[i].polygons.size(); j++) {
  4731. // physics_layer_%d/points
  4732. property_info = PropertyInfo(Variant::ARRAY, vformat("physics_layer_%d/polygon_%d/points", i, j), PROPERTY_HINT_ARRAY_TYPE, "Vector2", PROPERTY_USAGE_DEFAULT);
  4733. if (physics[i].polygons[j].polygon.is_empty()) {
  4734. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4735. }
  4736. p_list->push_back(property_info);
  4737. // physics_layer_%d/polygon_%d/one_way
  4738. property_info = PropertyInfo(Variant::BOOL, vformat("physics_layer_%d/polygon_%d/one_way", i, j));
  4739. if (physics[i].polygons[j].one_way == false) {
  4740. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4741. }
  4742. p_list->push_back(property_info);
  4743. // physics_layer_%d/polygon_%d/one_way_margin
  4744. property_info = PropertyInfo(Variant::FLOAT, vformat("physics_layer_%d/polygon_%d/one_way_margin", i, j));
  4745. if (physics[i].polygons[j].one_way_margin == 1.0) {
  4746. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4747. }
  4748. p_list->push_back(property_info);
  4749. }
  4750. }
  4751. // Terrain data
  4752. if (terrain_set >= 0) {
  4753. p_list->push_back(PropertyInfo(Variant::NIL, "Terrains", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4754. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4755. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  4756. if (is_valid_peering_bit_terrain(bit)) {
  4757. property_info = PropertyInfo(Variant::INT, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]));
  4758. if (get_peering_bit_terrain(bit) == -1) {
  4759. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4760. }
  4761. p_list->push_back(property_info);
  4762. }
  4763. }
  4764. }
  4765. // Navigation layers.
  4766. p_list->push_back(PropertyInfo(Variant::NIL, "Navigation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4767. for (int i = 0; i < navigation.size(); i++) {
  4768. property_info = PropertyInfo(Variant::OBJECT, vformat("navigation_layer_%d/polygon", i), PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon", PROPERTY_USAGE_DEFAULT);
  4769. if (!navigation[i].is_valid()) {
  4770. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4771. }
  4772. p_list->push_back(property_info);
  4773. }
  4774. // Custom data layers.
  4775. p_list->push_back(PropertyInfo(Variant::NIL, "Custom data", PROPERTY_HINT_NONE, "custom_data_", PROPERTY_USAGE_GROUP));
  4776. for (int i = 0; i < custom_data.size(); i++) {
  4777. Variant default_val;
  4778. Callable::CallError error;
  4779. Variant::construct(custom_data[i].get_type(), default_val, nullptr, 0, error);
  4780. property_info = PropertyInfo(tile_set->get_custom_data_type(i), vformat("custom_data_%d", i), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT);
  4781. if (custom_data[i] == default_val) {
  4782. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4783. }
  4784. p_list->push_back(property_info);
  4785. }
  4786. }
  4787. }
  4788. void TileData::_bind_methods() {
  4789. // Rendering.
  4790. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &TileData::set_flip_h);
  4791. ClassDB::bind_method(D_METHOD("get_flip_h"), &TileData::get_flip_h);
  4792. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &TileData::set_flip_v);
  4793. ClassDB::bind_method(D_METHOD("get_flip_v"), &TileData::get_flip_v);
  4794. ClassDB::bind_method(D_METHOD("set_transpose", "transpose"), &TileData::set_transpose);
  4795. ClassDB::bind_method(D_METHOD("get_transpose"), &TileData::get_transpose);
  4796. ClassDB::bind_method(D_METHOD("set_material", "material"), &TileData::set_material);
  4797. ClassDB::bind_method(D_METHOD("get_material"), &TileData::get_material);
  4798. ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &TileData::set_texture_offset);
  4799. ClassDB::bind_method(D_METHOD("get_texture_offset"), &TileData::get_texture_offset);
  4800. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &TileData::set_modulate);
  4801. ClassDB::bind_method(D_METHOD("get_modulate"), &TileData::get_modulate);
  4802. ClassDB::bind_method(D_METHOD("set_z_index", "z_index"), &TileData::set_z_index);
  4803. ClassDB::bind_method(D_METHOD("get_z_index"), &TileData::get_z_index);
  4804. ClassDB::bind_method(D_METHOD("set_y_sort_origin", "y_sort_origin"), &TileData::set_y_sort_origin);
  4805. ClassDB::bind_method(D_METHOD("get_y_sort_origin"), &TileData::get_y_sort_origin);
  4806. ClassDB::bind_method(D_METHOD("set_occluder", "layer_id", "occluder_polygon"), &TileData::set_occluder);
  4807. ClassDB::bind_method(D_METHOD("get_occluder", "layer_id"), &TileData::get_occluder);
  4808. // Physics.
  4809. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "layer_id", "velocity"), &TileData::set_constant_linear_velocity);
  4810. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity", "layer_id"), &TileData::get_constant_linear_velocity);
  4811. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "layer_id", "velocity"), &TileData::set_constant_angular_velocity);
  4812. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity", "layer_id"), &TileData::get_constant_angular_velocity);
  4813. ClassDB::bind_method(D_METHOD("set_collision_polygons_count", "layer_id", "polygons_count"), &TileData::set_collision_polygons_count);
  4814. ClassDB::bind_method(D_METHOD("get_collision_polygons_count", "layer_id"), &TileData::get_collision_polygons_count);
  4815. ClassDB::bind_method(D_METHOD("add_collision_polygon", "layer_id"), &TileData::add_collision_polygon);
  4816. ClassDB::bind_method(D_METHOD("remove_collision_polygon", "layer_id", "polygon_index"), &TileData::remove_collision_polygon);
  4817. ClassDB::bind_method(D_METHOD("set_collision_polygon_points", "layer_id", "polygon_index", "polygon"), &TileData::set_collision_polygon_points);
  4818. ClassDB::bind_method(D_METHOD("get_collision_polygon_points", "layer_id", "polygon_index"), &TileData::get_collision_polygon_points);
  4819. ClassDB::bind_method(D_METHOD("set_collision_polygon_one_way", "layer_id", "polygon_index", "one_way"), &TileData::set_collision_polygon_one_way);
  4820. ClassDB::bind_method(D_METHOD("is_collision_polygon_one_way", "layer_id", "polygon_index"), &TileData::is_collision_polygon_one_way);
  4821. ClassDB::bind_method(D_METHOD("set_collision_polygon_one_way_margin", "layer_id", "polygon_index", "one_way_margin"), &TileData::set_collision_polygon_one_way_margin);
  4822. ClassDB::bind_method(D_METHOD("get_collision_polygon_one_way_margin", "layer_id", "polygon_index"), &TileData::get_collision_polygon_one_way_margin);
  4823. // Terrain
  4824. ClassDB::bind_method(D_METHOD("set_terrain_set", "terrain_set"), &TileData::set_terrain_set);
  4825. ClassDB::bind_method(D_METHOD("get_terrain_set"), &TileData::get_terrain_set);
  4826. ClassDB::bind_method(D_METHOD("set_peering_bit_terrain", "peering_bit", "terrain"), &TileData::set_peering_bit_terrain);
  4827. ClassDB::bind_method(D_METHOD("get_peering_bit_terrain", "peering_bit"), &TileData::get_peering_bit_terrain);
  4828. // Navigation
  4829. ClassDB::bind_method(D_METHOD("set_navigation_polygon", "layer_id", "navigation_polygon"), &TileData::set_navigation_polygon);
  4830. ClassDB::bind_method(D_METHOD("get_navigation_polygon", "layer_id"), &TileData::get_navigation_polygon);
  4831. // Misc.
  4832. ClassDB::bind_method(D_METHOD("set_probability", "probability"), &TileData::set_probability);
  4833. ClassDB::bind_method(D_METHOD("get_probability"), &TileData::get_probability);
  4834. // Custom data.
  4835. ClassDB::bind_method(D_METHOD("set_custom_data", "layer_name", "value"), &TileData::set_custom_data);
  4836. ClassDB::bind_method(D_METHOD("get_custom_data", "layer_name"), &TileData::get_custom_data);
  4837. ClassDB::bind_method(D_METHOD("set_custom_data_by_layer_id", "layer_id", "value"), &TileData::set_custom_data_by_layer_id);
  4838. ClassDB::bind_method(D_METHOD("get_custom_data_by_layer_id", "layer_id"), &TileData::get_custom_data_by_layer_id);
  4839. ADD_GROUP("Rendering", "");
  4840. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "get_flip_h");
  4841. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "get_flip_v");
  4842. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transpose"), "set_transpose", "get_transpose");
  4843. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "texture_offset"), "set_texture_offset", "get_texture_offset");
  4844. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  4845. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial"), "set_material", "get_material");
  4846. ADD_PROPERTY(PropertyInfo(Variant::INT, "z_index"), "set_z_index", "get_z_index");
  4847. ADD_PROPERTY(PropertyInfo(Variant::INT, "y_sort_origin"), "set_y_sort_origin", "get_y_sort_origin");
  4848. ADD_GROUP("Terrains", "");
  4849. ADD_PROPERTY(PropertyInfo(Variant::INT, "terrain_set"), "set_terrain_set", "get_terrain_set");
  4850. ADD_GROUP("Miscellaneous", "");
  4851. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "probability"), "set_probability", "get_probability");
  4852. ADD_SIGNAL(MethodInfo("changed"));
  4853. }