tile_set.cpp 220 KB

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