tile_set.cpp 224 KB

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