tile_set.cpp 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793
  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. }
  2235. const Vector2i TileSetSource::INVALID_ATLAS_COORDS = Vector2i(-1, -1);
  2236. const int TileSetSource::INVALID_TILE_ALTERNATIVE = -1;
  2237. #ifndef DISABLE_DEPRECATED
  2238. void TileSet::_compatibility_conversion() {
  2239. for (KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  2240. CompatibilityTileData *ctd = E.value;
  2241. // Add the texture
  2242. TileSetAtlasSource *atlas_source = memnew(TileSetAtlasSource);
  2243. int source_id = add_source(Ref<TileSetSource>(atlas_source));
  2244. atlas_source->set_texture(ctd->texture);
  2245. // Handle each tile as a new source. Not optimal but at least it should stay compatible.
  2246. switch (ctd->tile_mode) {
  2247. case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
  2248. atlas_source->set_margins(ctd->region.get_position());
  2249. atlas_source->set_texture_region_size(ctd->region.get_size());
  2250. Vector2i coords;
  2251. for (int flags = 0; flags < 8; flags++) {
  2252. bool flip_h = flags & 1;
  2253. bool flip_v = flags & 2;
  2254. bool transpose = flags & 4;
  2255. int alternative_tile = 0;
  2256. if (!atlas_source->has_tile(coords)) {
  2257. atlas_source->create_tile(coords);
  2258. } else {
  2259. alternative_tile = atlas_source->create_alternative_tile(coords);
  2260. }
  2261. // Add to the mapping.
  2262. Array key_array;
  2263. key_array.push_back(flip_h);
  2264. key_array.push_back(flip_v);
  2265. key_array.push_back(transpose);
  2266. Array value_array;
  2267. value_array.push_back(source_id);
  2268. value_array.push_back(coords);
  2269. value_array.push_back(alternative_tile);
  2270. if (!compatibility_tilemap_mapping.has(E.key)) {
  2271. compatibility_tilemap_mapping[E.key] = RBMap<Array, Array>();
  2272. }
  2273. compatibility_tilemap_mapping[E.key][key_array] = value_array;
  2274. compatibility_tilemap_mapping_tile_modes[E.key] = COMPATIBILITY_TILE_MODE_SINGLE_TILE;
  2275. TileData *tile_data = atlas_source->get_tile_data(coords, alternative_tile);
  2276. tile_data->set_flip_h(flip_h);
  2277. tile_data->set_flip_v(flip_v);
  2278. tile_data->set_transpose(transpose);
  2279. tile_data->set_material(ctd->material);
  2280. tile_data->set_modulate(ctd->modulate);
  2281. tile_data->set_z_index(ctd->z_index);
  2282. if (ctd->occluder.is_valid()) {
  2283. if (get_occlusion_layers_count() < 1) {
  2284. add_occlusion_layer();
  2285. }
  2286. tile_data->set_occluder(0, ctd->occluder);
  2287. }
  2288. if (ctd->navigation.is_valid()) {
  2289. if (get_navigation_layers_count() < 1) {
  2290. add_navigation_layer();
  2291. }
  2292. tile_data->set_navigation_polygon(0, ctd->autotile_navpoly_map[coords]);
  2293. }
  2294. tile_data->set_z_index(ctd->z_index);
  2295. // Add the shapes.
  2296. if (ctd->shapes.size() > 0) {
  2297. if (get_physics_layers_count() < 1) {
  2298. add_physics_layer();
  2299. }
  2300. }
  2301. for (int k = 0; k < ctd->shapes.size(); k++) {
  2302. CompatibilityShapeData csd = ctd->shapes[k];
  2303. if (csd.autotile_coords == coords) {
  2304. Ref<ConvexPolygonShape2D> convex_shape = csd.shape; // Only ConvexPolygonShape2D are supported, which is the default type used by the 3.x editor
  2305. if (convex_shape.is_valid()) {
  2306. Vector<Vector2> polygon = convex_shape->get_points();
  2307. for (int point_index = 0; point_index < polygon.size(); point_index++) {
  2308. polygon.write[point_index] = csd.transform.xform(polygon[point_index]);
  2309. }
  2310. tile_data->set_collision_polygons_count(0, tile_data->get_collision_polygons_count(0) + 1);
  2311. int index = tile_data->get_collision_polygons_count(0) - 1;
  2312. tile_data->set_collision_polygon_one_way(0, index, csd.one_way);
  2313. tile_data->set_collision_polygon_one_way_margin(0, index, csd.one_way_margin);
  2314. tile_data->set_collision_polygon_points(0, index, polygon);
  2315. }
  2316. }
  2317. }
  2318. }
  2319. } break;
  2320. case COMPATIBILITY_TILE_MODE_AUTO_TILE: {
  2321. // Not supported. It would need manual conversion.
  2322. } break;
  2323. case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
  2324. atlas_source->set_margins(ctd->region.get_position());
  2325. atlas_source->set_separation(Vector2i(ctd->autotile_spacing, ctd->autotile_spacing));
  2326. atlas_source->set_texture_region_size(ctd->autotile_tile_size);
  2327. Size2i atlas_size = ctd->region.get_size() / (ctd->autotile_tile_size + atlas_source->get_separation());
  2328. for (int i = 0; i < atlas_size.x; i++) {
  2329. for (int j = 0; j < atlas_size.y; j++) {
  2330. Vector2i coords = Vector2i(i, j);
  2331. for (int flags = 0; flags < 8; flags++) {
  2332. bool flip_h = flags & 1;
  2333. bool flip_v = flags & 2;
  2334. bool transpose = flags & 4;
  2335. int alternative_tile = 0;
  2336. if (!atlas_source->has_tile(coords)) {
  2337. atlas_source->create_tile(coords);
  2338. } else {
  2339. alternative_tile = atlas_source->create_alternative_tile(coords);
  2340. }
  2341. // Add to the mapping.
  2342. Array key_array;
  2343. key_array.push_back(coords);
  2344. key_array.push_back(flip_h);
  2345. key_array.push_back(flip_v);
  2346. key_array.push_back(transpose);
  2347. Array value_array;
  2348. value_array.push_back(source_id);
  2349. value_array.push_back(coords);
  2350. value_array.push_back(alternative_tile);
  2351. if (!compatibility_tilemap_mapping.has(E.key)) {
  2352. compatibility_tilemap_mapping[E.key] = RBMap<Array, Array>();
  2353. }
  2354. compatibility_tilemap_mapping[E.key][key_array] = value_array;
  2355. compatibility_tilemap_mapping_tile_modes[E.key] = COMPATIBILITY_TILE_MODE_ATLAS_TILE;
  2356. TileData *tile_data = atlas_source->get_tile_data(coords, alternative_tile);
  2357. tile_data->set_flip_h(flip_h);
  2358. tile_data->set_flip_v(flip_v);
  2359. tile_data->set_transpose(transpose);
  2360. tile_data->set_material(ctd->material);
  2361. tile_data->set_modulate(ctd->modulate);
  2362. tile_data->set_z_index(ctd->z_index);
  2363. if (ctd->autotile_occluder_map.has(coords)) {
  2364. if (get_occlusion_layers_count() < 1) {
  2365. add_occlusion_layer();
  2366. }
  2367. tile_data->set_occluder(0, ctd->autotile_occluder_map[coords]);
  2368. }
  2369. if (ctd->autotile_navpoly_map.has(coords)) {
  2370. if (get_navigation_layers_count() < 1) {
  2371. add_navigation_layer();
  2372. }
  2373. tile_data->set_navigation_polygon(0, ctd->autotile_navpoly_map[coords]);
  2374. }
  2375. if (ctd->autotile_priority_map.has(coords)) {
  2376. tile_data->set_probability(ctd->autotile_priority_map[coords]);
  2377. }
  2378. if (ctd->autotile_z_index_map.has(coords)) {
  2379. tile_data->set_z_index(ctd->autotile_z_index_map[coords]);
  2380. }
  2381. // Add the shapes.
  2382. if (ctd->shapes.size() > 0) {
  2383. if (get_physics_layers_count() < 1) {
  2384. add_physics_layer();
  2385. }
  2386. }
  2387. for (int k = 0; k < ctd->shapes.size(); k++) {
  2388. CompatibilityShapeData csd = ctd->shapes[k];
  2389. if (csd.autotile_coords == coords) {
  2390. Ref<ConvexPolygonShape2D> convex_shape = csd.shape; // Only ConvexPolygonShape2D are supported, which is the default type used by the 3.x editor
  2391. if (convex_shape.is_valid()) {
  2392. Vector<Vector2> polygon = convex_shape->get_points();
  2393. for (int point_index = 0; point_index < polygon.size(); point_index++) {
  2394. polygon.write[point_index] = csd.transform.xform(polygon[point_index]);
  2395. }
  2396. tile_data->set_collision_polygons_count(0, tile_data->get_collision_polygons_count(0) + 1);
  2397. int index = tile_data->get_collision_polygons_count(0) - 1;
  2398. tile_data->set_collision_polygon_one_way(0, index, csd.one_way);
  2399. tile_data->set_collision_polygon_one_way_margin(0, index, csd.one_way_margin);
  2400. tile_data->set_collision_polygon_points(0, index, polygon);
  2401. }
  2402. }
  2403. }
  2404. // -- TODO: handle --
  2405. // Those are offset for the whole atlas, they are likely useless for the atlases, but might make sense for single tiles.
  2406. // texture offset
  2407. // occluder_offset
  2408. // navigation_offset
  2409. // For terrains, ignored for now?
  2410. // bitmask_mode
  2411. // bitmask_flags
  2412. }
  2413. }
  2414. }
  2415. } break;
  2416. }
  2417. // Offset all shapes
  2418. for (int k = 0; k < ctd->shapes.size(); k++) {
  2419. Ref<ConvexPolygonShape2D> convex = ctd->shapes[k].shape;
  2420. if (convex.is_valid()) {
  2421. Vector<Vector2> points = convex->get_points();
  2422. for (int i_point = 0; i_point < points.size(); i_point++) {
  2423. points.write[i_point] = points[i_point] - get_tile_size() / 2;
  2424. }
  2425. convex->set_points(points);
  2426. }
  2427. }
  2428. }
  2429. // Reset compatibility data
  2430. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  2431. memdelete(E.value);
  2432. }
  2433. compatibility_data = HashMap<int, CompatibilityTileData *>();
  2434. }
  2435. Array TileSet::compatibility_tilemap_map(int p_tile_id, Vector2i p_coords, bool p_flip_h, bool p_flip_v, bool p_transpose) {
  2436. Array cannot_convert_array;
  2437. cannot_convert_array.push_back(TileSet::INVALID_SOURCE);
  2438. cannot_convert_array.push_back(TileSetAtlasSource::INVALID_ATLAS_COORDS);
  2439. cannot_convert_array.push_back(TileSetAtlasSource::INVALID_TILE_ALTERNATIVE);
  2440. if (!compatibility_tilemap_mapping.has(p_tile_id)) {
  2441. return cannot_convert_array;
  2442. }
  2443. int tile_mode = compatibility_tilemap_mapping_tile_modes[p_tile_id];
  2444. switch (tile_mode) {
  2445. case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
  2446. Array a;
  2447. a.push_back(p_flip_h);
  2448. a.push_back(p_flip_v);
  2449. a.push_back(p_transpose);
  2450. return compatibility_tilemap_mapping[p_tile_id][a];
  2451. }
  2452. case COMPATIBILITY_TILE_MODE_AUTO_TILE:
  2453. return cannot_convert_array;
  2454. break;
  2455. case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
  2456. Array a;
  2457. a.push_back(p_coords);
  2458. a.push_back(p_flip_h);
  2459. a.push_back(p_flip_v);
  2460. a.push_back(p_transpose);
  2461. return compatibility_tilemap_mapping[p_tile_id][a];
  2462. }
  2463. default:
  2464. return cannot_convert_array;
  2465. break;
  2466. }
  2467. };
  2468. #endif // DISABLE_DEPRECATED
  2469. bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
  2470. Vector<String> components = String(p_name).split("/", true, 2);
  2471. #ifndef DISABLE_DEPRECATED
  2472. // TODO: This should be moved to a dedicated conversion system (see #50691)
  2473. if (components.size() >= 1 && components[0].is_valid_int()) {
  2474. int id = components[0].to_int();
  2475. // Get or create the compatibility object
  2476. CompatibilityTileData *ctd;
  2477. HashMap<int, CompatibilityTileData *>::Iterator E = compatibility_data.find(id);
  2478. if (!E) {
  2479. ctd = memnew(CompatibilityTileData);
  2480. compatibility_data.insert(id, ctd);
  2481. } else {
  2482. ctd = E->value;
  2483. }
  2484. if (components.size() < 2) {
  2485. return false;
  2486. }
  2487. String what = components[1];
  2488. if (what == "name") {
  2489. ctd->name = p_value;
  2490. } else if (what == "texture") {
  2491. ctd->texture = p_value;
  2492. } else if (what == "tex_offset") {
  2493. ctd->tex_offset = p_value;
  2494. } else if (what == "material") {
  2495. ctd->material = p_value;
  2496. } else if (what == "modulate") {
  2497. ctd->modulate = p_value;
  2498. } else if (what == "region") {
  2499. ctd->region = p_value;
  2500. } else if (what == "tile_mode") {
  2501. ctd->tile_mode = p_value;
  2502. } else if (what.left(9) == "autotile") {
  2503. what = what.substr(9);
  2504. if (what == "bitmask_mode") {
  2505. ctd->autotile_bitmask_mode = p_value;
  2506. } else if (what == "icon_coordinate") {
  2507. ctd->autotile_icon_coordinate = p_value;
  2508. } else if (what == "tile_size") {
  2509. ctd->autotile_tile_size = p_value;
  2510. } else if (what == "spacing") {
  2511. ctd->autotile_spacing = p_value;
  2512. } else if (what == "bitmask_flags") {
  2513. if (p_value.is_array()) {
  2514. Array p = p_value;
  2515. Vector2i last_coord;
  2516. while (p.size() > 0) {
  2517. if (p[0].get_type() == Variant::VECTOR2) {
  2518. last_coord = p[0];
  2519. } else if (p[0].get_type() == Variant::INT) {
  2520. ctd->autotile_bitmask_flags.insert(last_coord, p[0]);
  2521. }
  2522. p.pop_front();
  2523. }
  2524. }
  2525. } else if (what == "occluder_map") {
  2526. Array p = p_value;
  2527. Vector2 last_coord;
  2528. while (p.size() > 0) {
  2529. if (p[0].get_type() == Variant::VECTOR2) {
  2530. last_coord = p[0];
  2531. } else if (p[0].get_type() == Variant::OBJECT) {
  2532. ctd->autotile_occluder_map.insert(last_coord, p[0]);
  2533. }
  2534. p.pop_front();
  2535. }
  2536. } else if (what == "navpoly_map") {
  2537. Array p = p_value;
  2538. Vector2 last_coord;
  2539. while (p.size() > 0) {
  2540. if (p[0].get_type() == Variant::VECTOR2) {
  2541. last_coord = p[0];
  2542. } else if (p[0].get_type() == Variant::OBJECT) {
  2543. ctd->autotile_navpoly_map.insert(last_coord, p[0]);
  2544. }
  2545. p.pop_front();
  2546. }
  2547. } else if (what == "priority_map") {
  2548. Array p = p_value;
  2549. Vector3 val;
  2550. Vector2 v;
  2551. int priority;
  2552. while (p.size() > 0) {
  2553. val = p[0];
  2554. if (val.z > 1) {
  2555. v.x = val.x;
  2556. v.y = val.y;
  2557. priority = (int)val.z;
  2558. ctd->autotile_priority_map.insert(v, priority);
  2559. }
  2560. p.pop_front();
  2561. }
  2562. } else if (what == "z_index_map") {
  2563. Array p = p_value;
  2564. Vector3 val;
  2565. Vector2 v;
  2566. int z_index;
  2567. while (p.size() > 0) {
  2568. val = p[0];
  2569. if (val.z != 0) {
  2570. v.x = val.x;
  2571. v.y = val.y;
  2572. z_index = (int)val.z;
  2573. ctd->autotile_z_index_map.insert(v, z_index);
  2574. }
  2575. p.pop_front();
  2576. }
  2577. }
  2578. } else if (what == "shapes") {
  2579. Array p = p_value;
  2580. for (int i = 0; i < p.size(); i++) {
  2581. CompatibilityShapeData csd;
  2582. Dictionary d = p[i];
  2583. for (int j = 0; j < d.size(); j++) {
  2584. String key = d.get_key_at_index(j);
  2585. if (key == "autotile_coord") {
  2586. csd.autotile_coords = d[key];
  2587. } else if (key == "one_way") {
  2588. csd.one_way = d[key];
  2589. } else if (key == "one_way_margin") {
  2590. csd.one_way_margin = d[key];
  2591. } else if (key == "shape") {
  2592. csd.shape = d[key];
  2593. } else if (key == "shape_transform") {
  2594. csd.transform = d[key];
  2595. }
  2596. }
  2597. ctd->shapes.push_back(csd);
  2598. }
  2599. /*
  2600. // IGNORED FOR NOW, they seem duplicated data compared to the shapes array
  2601. } else if (what == "shape") {
  2602. } else if (what == "shape_offset") {
  2603. } else if (what == "shape_transform") {
  2604. } else if (what == "shape_one_way") {
  2605. } else if (what == "shape_one_way_margin") {
  2606. }
  2607. // IGNORED FOR NOW, maybe useless ?
  2608. else if (what == "occluder_offset") {
  2609. // Not
  2610. } else if (what == "navigation_offset") {
  2611. }
  2612. */
  2613. } else if (what == "z_index") {
  2614. ctd->z_index = p_value;
  2615. // TODO: remove the conversion from here, it's not where it should be done (see #50691)
  2616. _compatibility_conversion();
  2617. } else {
  2618. return false;
  2619. }
  2620. } else {
  2621. #endif // DISABLE_DEPRECATED
  2622. // This is now a new property.
  2623. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  2624. // Occlusion layers.
  2625. int index = components[0].trim_prefix("occlusion_layer_").to_int();
  2626. ERR_FAIL_COND_V(index < 0, false);
  2627. if (components[1] == "light_mask") {
  2628. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2629. while (index >= occlusion_layers.size()) {
  2630. add_occlusion_layer();
  2631. }
  2632. set_occlusion_layer_light_mask(index, p_value);
  2633. return true;
  2634. } else if (components[1] == "sdf_collision") {
  2635. ERR_FAIL_COND_V(p_value.get_type() != Variant::BOOL, false);
  2636. while (index >= occlusion_layers.size()) {
  2637. add_occlusion_layer();
  2638. }
  2639. set_occlusion_layer_sdf_collision(index, p_value);
  2640. return true;
  2641. }
  2642. } else if (components.size() == 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  2643. // Physics layers.
  2644. int index = components[0].trim_prefix("physics_layer_").to_int();
  2645. ERR_FAIL_COND_V(index < 0, false);
  2646. if (components[1] == "collision_layer") {
  2647. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2648. while (index >= physics_layers.size()) {
  2649. add_physics_layer();
  2650. }
  2651. set_physics_layer_collision_layer(index, p_value);
  2652. return true;
  2653. } else if (components[1] == "collision_mask") {
  2654. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2655. while (index >= physics_layers.size()) {
  2656. add_physics_layer();
  2657. }
  2658. set_physics_layer_collision_mask(index, p_value);
  2659. return true;
  2660. } else if (components[1] == "physics_material") {
  2661. Ref<PhysicsMaterial> physics_material = p_value;
  2662. while (index >= physics_layers.size()) {
  2663. add_physics_layer();
  2664. }
  2665. set_physics_layer_physics_material(index, physics_material);
  2666. return true;
  2667. }
  2668. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int()) {
  2669. // Terrains.
  2670. int terrain_set_index = components[0].trim_prefix("terrain_set_").to_int();
  2671. ERR_FAIL_COND_V(terrain_set_index < 0, false);
  2672. if (components[1] == "mode") {
  2673. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2674. while (terrain_set_index >= terrain_sets.size()) {
  2675. add_terrain_set();
  2676. }
  2677. set_terrain_set_mode(terrain_set_index, TerrainMode(int(p_value)));
  2678. } else if (components.size() >= 3 && components[1].begins_with("terrain_") && components[1].trim_prefix("terrain_").is_valid_int()) {
  2679. int terrain_index = components[1].trim_prefix("terrain_").to_int();
  2680. ERR_FAIL_COND_V(terrain_index < 0, false);
  2681. if (components[2] == "name") {
  2682. ERR_FAIL_COND_V(p_value.get_type() != Variant::STRING, false);
  2683. while (terrain_set_index >= terrain_sets.size()) {
  2684. add_terrain_set();
  2685. }
  2686. while (terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  2687. add_terrain(terrain_set_index);
  2688. }
  2689. set_terrain_name(terrain_set_index, terrain_index, p_value);
  2690. return true;
  2691. } else if (components[2] == "color") {
  2692. ERR_FAIL_COND_V(p_value.get_type() != Variant::COLOR, false);
  2693. while (terrain_set_index >= terrain_sets.size()) {
  2694. add_terrain_set();
  2695. }
  2696. while (terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  2697. add_terrain(terrain_set_index);
  2698. }
  2699. set_terrain_color(terrain_set_index, terrain_index, p_value);
  2700. return true;
  2701. }
  2702. }
  2703. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  2704. // Navigation layers.
  2705. int index = components[0].trim_prefix("navigation_layer_").to_int();
  2706. ERR_FAIL_COND_V(index < 0, false);
  2707. if (components[1] == "layers") {
  2708. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2709. while (index >= navigation_layers.size()) {
  2710. add_navigation_layer();
  2711. }
  2712. set_navigation_layer_layers(index, p_value);
  2713. return true;
  2714. }
  2715. } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int()) {
  2716. // Custom data layers.
  2717. int index = components[0].trim_prefix("custom_data_layer_").to_int();
  2718. ERR_FAIL_COND_V(index < 0, false);
  2719. if (components[1] == "name") {
  2720. ERR_FAIL_COND_V(p_value.get_type() != Variant::STRING, false);
  2721. while (index >= custom_data_layers.size()) {
  2722. add_custom_data_layer();
  2723. }
  2724. set_custom_data_layer_name(index, p_value);
  2725. return true;
  2726. } else if (components[1] == "type") {
  2727. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2728. while (index >= custom_data_layers.size()) {
  2729. add_custom_data_layer();
  2730. }
  2731. set_custom_data_layer_type(index, Variant::Type(int(p_value)));
  2732. return true;
  2733. }
  2734. } else if (components.size() == 2 && components[0] == "sources" && components[1].is_valid_int()) {
  2735. // Create source only if it does not exists.
  2736. int source_id = components[1].to_int();
  2737. if (!has_source(source_id)) {
  2738. add_source(p_value, source_id);
  2739. }
  2740. return true;
  2741. } else if (components.size() == 2 && components[0] == "tile_proxies") {
  2742. ERR_FAIL_COND_V(p_value.get_type() != Variant::ARRAY, false);
  2743. Array a = p_value;
  2744. ERR_FAIL_COND_V(a.size() % 2 != 0, false);
  2745. if (components[1] == "source_level") {
  2746. for (int i = 0; i < a.size(); i += 2) {
  2747. set_source_level_tile_proxy(a[i], a[i + 1]);
  2748. }
  2749. return true;
  2750. } else if (components[1] == "coords_level") {
  2751. for (int i = 0; i < a.size(); i += 2) {
  2752. Array key = a[i];
  2753. Array value = a[i + 1];
  2754. set_coords_level_tile_proxy(key[0], key[1], value[0], value[1]);
  2755. }
  2756. return true;
  2757. } else if (components[1] == "alternative_level") {
  2758. for (int i = 0; i < a.size(); i += 2) {
  2759. Array key = a[i];
  2760. Array value = a[i + 1];
  2761. set_alternative_level_tile_proxy(key[0], key[1], key[2], value[0], value[1], value[2]);
  2762. }
  2763. return true;
  2764. }
  2765. return false;
  2766. } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) {
  2767. int pattern_index = components[0].trim_prefix("pattern_").to_int();
  2768. for (int i = patterns.size(); i <= pattern_index; i++) {
  2769. add_pattern(p_value);
  2770. }
  2771. return true;
  2772. }
  2773. #ifndef DISABLE_DEPRECATED
  2774. }
  2775. #endif // DISABLE_DEPRECATED
  2776. return false;
  2777. }
  2778. bool TileSet::_get(const StringName &p_name, Variant &r_ret) const {
  2779. Vector<String> components = String(p_name).split("/", true, 2);
  2780. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  2781. // Occlusion layers.
  2782. int index = components[0].trim_prefix("occlusion_layer_").to_int();
  2783. if (index < 0 || index >= occlusion_layers.size()) {
  2784. return false;
  2785. }
  2786. if (components[1] == "light_mask") {
  2787. r_ret = get_occlusion_layer_light_mask(index);
  2788. return true;
  2789. } else if (components[1] == "sdf_collision") {
  2790. r_ret = get_occlusion_layer_sdf_collision(index);
  2791. return true;
  2792. }
  2793. } else if (components.size() == 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  2794. // Physics layers.
  2795. int index = components[0].trim_prefix("physics_layer_").to_int();
  2796. if (index < 0 || index >= physics_layers.size()) {
  2797. return false;
  2798. }
  2799. if (components[1] == "collision_layer") {
  2800. r_ret = get_physics_layer_collision_layer(index);
  2801. return true;
  2802. } else if (components[1] == "collision_mask") {
  2803. r_ret = get_physics_layer_collision_mask(index);
  2804. return true;
  2805. } else if (components[1] == "physics_material") {
  2806. r_ret = get_physics_layer_physics_material(index);
  2807. return true;
  2808. }
  2809. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int()) {
  2810. // Terrains.
  2811. int terrain_set_index = components[0].trim_prefix("terrain_set_").to_int();
  2812. if (terrain_set_index < 0 || terrain_set_index >= terrain_sets.size()) {
  2813. return false;
  2814. }
  2815. if (components[1] == "mode") {
  2816. r_ret = get_terrain_set_mode(terrain_set_index);
  2817. return true;
  2818. } else if (components.size() >= 3 && components[1].begins_with("terrain_") && components[1].trim_prefix("terrain_").is_valid_int()) {
  2819. int terrain_index = components[1].trim_prefix("terrain_").to_int();
  2820. if (terrain_index < 0 || terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  2821. return false;
  2822. }
  2823. if (components[2] == "name") {
  2824. r_ret = get_terrain_name(terrain_set_index, terrain_index);
  2825. return true;
  2826. } else if (components[2] == "color") {
  2827. r_ret = get_terrain_color(terrain_set_index, terrain_index);
  2828. return true;
  2829. }
  2830. }
  2831. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  2832. // navigation layers.
  2833. int index = components[0].trim_prefix("navigation_layer_").to_int();
  2834. if (index < 0 || index >= navigation_layers.size()) {
  2835. return false;
  2836. }
  2837. if (components[1] == "layers") {
  2838. r_ret = get_navigation_layer_layers(index);
  2839. return true;
  2840. }
  2841. } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int()) {
  2842. // Custom data layers.
  2843. int index = components[0].trim_prefix("custom_data_layer_").to_int();
  2844. if (index < 0 || index >= custom_data_layers.size()) {
  2845. return false;
  2846. }
  2847. if (components[1] == "name") {
  2848. r_ret = get_custom_data_layer_name(index);
  2849. return true;
  2850. } else if (components[1] == "type") {
  2851. r_ret = get_custom_data_layer_type(index);
  2852. return true;
  2853. }
  2854. } else if (components.size() == 2 && components[0] == "sources" && components[1].is_valid_int()) {
  2855. // Atlases data.
  2856. int source_id = components[1].to_int();
  2857. if (has_source(source_id)) {
  2858. r_ret = get_source(source_id);
  2859. return true;
  2860. } else {
  2861. return false;
  2862. }
  2863. } else if (components.size() == 2 && components[0] == "tile_proxies") {
  2864. if (components[1] == "source_level") {
  2865. Array a;
  2866. for (const KeyValue<int, int> &E : source_level_proxies) {
  2867. a.push_back(E.key);
  2868. a.push_back(E.value);
  2869. }
  2870. r_ret = a;
  2871. return true;
  2872. } else if (components[1] == "coords_level") {
  2873. Array a;
  2874. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  2875. a.push_back(E.key);
  2876. a.push_back(E.value);
  2877. }
  2878. r_ret = a;
  2879. return true;
  2880. } else if (components[1] == "alternative_level") {
  2881. Array a;
  2882. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  2883. a.push_back(E.key);
  2884. a.push_back(E.value);
  2885. }
  2886. r_ret = a;
  2887. return true;
  2888. }
  2889. return false;
  2890. } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) {
  2891. int pattern_index = components[0].trim_prefix("pattern_").to_int();
  2892. if (pattern_index < 0 || pattern_index >= (int)patterns.size()) {
  2893. return false;
  2894. }
  2895. r_ret = patterns[pattern_index];
  2896. return true;
  2897. }
  2898. return false;
  2899. }
  2900. void TileSet::_get_property_list(List<PropertyInfo> *p_list) const {
  2901. PropertyInfo property_info;
  2902. // Rendering.
  2903. p_list->push_back(PropertyInfo(Variant::NIL, "Rendering", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2904. for (int i = 0; i < occlusion_layers.size(); i++) {
  2905. p_list->push_back(PropertyInfo(Variant::INT, vformat("occlusion_layer_%d/light_mask", i), PROPERTY_HINT_LAYERS_2D_RENDER));
  2906. // occlusion_layer_%d/sdf_collision
  2907. property_info = PropertyInfo(Variant::BOOL, vformat("occlusion_layer_%d/sdf_collision", i));
  2908. if (occlusion_layers[i].sdf_collision == false) {
  2909. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  2910. }
  2911. p_list->push_back(property_info);
  2912. }
  2913. // Physics.
  2914. p_list->push_back(PropertyInfo(Variant::NIL, "Physics", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2915. for (int i = 0; i < physics_layers.size(); i++) {
  2916. p_list->push_back(PropertyInfo(Variant::INT, vformat("physics_layer_%d/collision_layer", i), PROPERTY_HINT_LAYERS_2D_PHYSICS));
  2917. // physics_layer_%d/collision_mask
  2918. property_info = PropertyInfo(Variant::INT, vformat("physics_layer_%d/collision_mask", i), PROPERTY_HINT_LAYERS_2D_PHYSICS);
  2919. if (physics_layers[i].collision_mask == 1) {
  2920. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  2921. }
  2922. p_list->push_back(property_info);
  2923. // physics_layer_%d/physics_material
  2924. property_info = PropertyInfo(Variant::OBJECT, vformat("physics_layer_%d/physics_material", i), PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial");
  2925. if (!physics_layers[i].physics_material.is_valid()) {
  2926. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  2927. }
  2928. p_list->push_back(property_info);
  2929. }
  2930. // Terrains.
  2931. p_list->push_back(PropertyInfo(Variant::NIL, "Terrains", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2932. for (int terrain_set_index = 0; terrain_set_index < terrain_sets.size(); terrain_set_index++) {
  2933. 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"));
  2934. 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)));
  2935. for (int terrain_index = 0; terrain_index < terrain_sets[terrain_set_index].terrains.size(); terrain_index++) {
  2936. p_list->push_back(PropertyInfo(Variant::STRING, vformat("terrain_set_%d/terrain_%d/name", terrain_set_index, terrain_index)));
  2937. p_list->push_back(PropertyInfo(Variant::COLOR, vformat("terrain_set_%d/terrain_%d/color", terrain_set_index, terrain_index)));
  2938. }
  2939. }
  2940. // Navigation.
  2941. p_list->push_back(PropertyInfo(Variant::NIL, "Navigation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2942. for (int i = 0; i < navigation_layers.size(); i++) {
  2943. p_list->push_back(PropertyInfo(Variant::INT, vformat("navigation_layer_%d/layers", i), PROPERTY_HINT_LAYERS_2D_NAVIGATION));
  2944. }
  2945. // Custom data.
  2946. String argt = "Any";
  2947. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  2948. argt += "," + Variant::get_type_name(Variant::Type(i));
  2949. }
  2950. p_list->push_back(PropertyInfo(Variant::NIL, "Custom data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2951. for (int i = 0; i < custom_data_layers.size(); i++) {
  2952. p_list->push_back(PropertyInfo(Variant::STRING, vformat("custom_data_layer_%d/name", i)));
  2953. p_list->push_back(PropertyInfo(Variant::INT, vformat("custom_data_layer_%d/type", i), PROPERTY_HINT_ENUM, argt));
  2954. }
  2955. // Sources.
  2956. // Note: sources have to be listed in at the end as some TileData rely on the TileSet properties being initialized first.
  2957. for (const KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  2958. p_list->push_back(PropertyInfo(Variant::INT, vformat("sources/%d", E_source.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2959. }
  2960. // Tile Proxies.
  2961. // Note: proxies need to be set after sources are set.
  2962. p_list->push_back(PropertyInfo(Variant::NIL, "Tile Proxies", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2963. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/source_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2964. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/coords_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2965. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/alternative_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2966. // Patterns.
  2967. for (unsigned int pattern_index = 0; pattern_index < patterns.size(); pattern_index++) {
  2968. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("pattern_%d", pattern_index), PROPERTY_HINT_RESOURCE_TYPE, "TileMapPattern", PROPERTY_USAGE_NO_EDITOR));
  2969. }
  2970. }
  2971. void TileSet::_validate_property(PropertyInfo &p_property) const {
  2972. if (p_property.name == "tile_layout" && tile_shape == TILE_SHAPE_SQUARE) {
  2973. p_property.usage ^= PROPERTY_USAGE_READ_ONLY;
  2974. } else if (p_property.name == "tile_offset_axis" && tile_shape == TILE_SHAPE_SQUARE) {
  2975. p_property.usage ^= PROPERTY_USAGE_READ_ONLY;
  2976. }
  2977. }
  2978. void TileSet::_bind_methods() {
  2979. // Sources management.
  2980. ClassDB::bind_method(D_METHOD("get_next_source_id"), &TileSet::get_next_source_id);
  2981. ClassDB::bind_method(D_METHOD("add_source", "source", "atlas_source_id_override"), &TileSet::add_source, DEFVAL(TileSet::INVALID_SOURCE));
  2982. ClassDB::bind_method(D_METHOD("remove_source", "source_id"), &TileSet::remove_source);
  2983. ClassDB::bind_method(D_METHOD("set_source_id", "source_id", "new_source_id"), &TileSet::set_source_id);
  2984. ClassDB::bind_method(D_METHOD("get_source_count"), &TileSet::get_source_count);
  2985. ClassDB::bind_method(D_METHOD("get_source_id", "index"), &TileSet::get_source_id);
  2986. ClassDB::bind_method(D_METHOD("has_source", "source_id"), &TileSet::has_source);
  2987. ClassDB::bind_method(D_METHOD("get_source", "source_id"), &TileSet::get_source);
  2988. // Shape and layout.
  2989. ClassDB::bind_method(D_METHOD("set_tile_shape", "shape"), &TileSet::set_tile_shape);
  2990. ClassDB::bind_method(D_METHOD("get_tile_shape"), &TileSet::get_tile_shape);
  2991. ClassDB::bind_method(D_METHOD("set_tile_layout", "layout"), &TileSet::set_tile_layout);
  2992. ClassDB::bind_method(D_METHOD("get_tile_layout"), &TileSet::get_tile_layout);
  2993. ClassDB::bind_method(D_METHOD("set_tile_offset_axis", "alignment"), &TileSet::set_tile_offset_axis);
  2994. ClassDB::bind_method(D_METHOD("get_tile_offset_axis"), &TileSet::get_tile_offset_axis);
  2995. ClassDB::bind_method(D_METHOD("set_tile_size", "size"), &TileSet::set_tile_size);
  2996. ClassDB::bind_method(D_METHOD("get_tile_size"), &TileSet::get_tile_size);
  2997. ADD_PROPERTY(PropertyInfo(Variant::INT, "tile_shape", PROPERTY_HINT_ENUM, "Square,Isometric,Half-Offset Square,Hexagon"), "set_tile_shape", "get_tile_shape");
  2998. 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");
  2999. ADD_PROPERTY(PropertyInfo(Variant::INT, "tile_offset_axis", PROPERTY_HINT_ENUM, "Horizontal Offset,Vertical Offset"), "set_tile_offset_axis", "get_tile_offset_axis");
  3000. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "tile_size", PROPERTY_HINT_NONE, "suffix:px"), "set_tile_size", "get_tile_size");
  3001. // Rendering.
  3002. ClassDB::bind_method(D_METHOD("set_uv_clipping", "uv_clipping"), &TileSet::set_uv_clipping);
  3003. ClassDB::bind_method(D_METHOD("is_uv_clipping"), &TileSet::is_uv_clipping);
  3004. ClassDB::bind_method(D_METHOD("get_occlusion_layers_count"), &TileSet::get_occlusion_layers_count);
  3005. ClassDB::bind_method(D_METHOD("add_occlusion_layer", "to_position"), &TileSet::add_occlusion_layer, DEFVAL(-1));
  3006. ClassDB::bind_method(D_METHOD("move_occlusion_layer", "layer_index", "to_position"), &TileSet::move_occlusion_layer);
  3007. ClassDB::bind_method(D_METHOD("remove_occlusion_layer", "layer_index"), &TileSet::remove_occlusion_layer);
  3008. ClassDB::bind_method(D_METHOD("set_occlusion_layer_light_mask", "layer_index", "light_mask"), &TileSet::set_occlusion_layer_light_mask);
  3009. ClassDB::bind_method(D_METHOD("get_occlusion_layer_light_mask", "layer_index"), &TileSet::get_occlusion_layer_light_mask);
  3010. ClassDB::bind_method(D_METHOD("set_occlusion_layer_sdf_collision", "layer_index", "sdf_collision"), &TileSet::set_occlusion_layer_sdf_collision);
  3011. ClassDB::bind_method(D_METHOD("get_occlusion_layer_sdf_collision", "layer_index"), &TileSet::get_occlusion_layer_sdf_collision);
  3012. // Physics
  3013. ClassDB::bind_method(D_METHOD("get_physics_layers_count"), &TileSet::get_physics_layers_count);
  3014. ClassDB::bind_method(D_METHOD("add_physics_layer", "to_position"), &TileSet::add_physics_layer, DEFVAL(-1));
  3015. ClassDB::bind_method(D_METHOD("move_physics_layer", "layer_index", "to_position"), &TileSet::move_physics_layer);
  3016. ClassDB::bind_method(D_METHOD("remove_physics_layer", "layer_index"), &TileSet::remove_physics_layer);
  3017. ClassDB::bind_method(D_METHOD("set_physics_layer_collision_layer", "layer_index", "layer"), &TileSet::set_physics_layer_collision_layer);
  3018. ClassDB::bind_method(D_METHOD("get_physics_layer_collision_layer", "layer_index"), &TileSet::get_physics_layer_collision_layer);
  3019. ClassDB::bind_method(D_METHOD("set_physics_layer_collision_mask", "layer_index", "mask"), &TileSet::set_physics_layer_collision_mask);
  3020. ClassDB::bind_method(D_METHOD("get_physics_layer_collision_mask", "layer_index"), &TileSet::get_physics_layer_collision_mask);
  3021. ClassDB::bind_method(D_METHOD("set_physics_layer_physics_material", "layer_index", "physics_material"), &TileSet::set_physics_layer_physics_material);
  3022. ClassDB::bind_method(D_METHOD("get_physics_layer_physics_material", "layer_index"), &TileSet::get_physics_layer_physics_material);
  3023. // Terrains
  3024. ClassDB::bind_method(D_METHOD("get_terrain_sets_count"), &TileSet::get_terrain_sets_count);
  3025. ClassDB::bind_method(D_METHOD("add_terrain_set", "to_position"), &TileSet::add_terrain_set, DEFVAL(-1));
  3026. ClassDB::bind_method(D_METHOD("move_terrain_set", "terrain_set", "to_position"), &TileSet::move_terrain_set);
  3027. ClassDB::bind_method(D_METHOD("remove_terrain_set", "terrain_set"), &TileSet::remove_terrain_set);
  3028. ClassDB::bind_method(D_METHOD("set_terrain_set_mode", "terrain_set", "mode"), &TileSet::set_terrain_set_mode);
  3029. ClassDB::bind_method(D_METHOD("get_terrain_set_mode", "terrain_set"), &TileSet::get_terrain_set_mode);
  3030. ClassDB::bind_method(D_METHOD("get_terrains_count", "terrain_set"), &TileSet::get_terrains_count);
  3031. ClassDB::bind_method(D_METHOD("add_terrain", "terrain_set", "to_position"), &TileSet::add_terrain, DEFVAL(-1));
  3032. ClassDB::bind_method(D_METHOD("move_terrain", "terrain_set", "terrain_index", "to_position"), &TileSet::move_terrain);
  3033. ClassDB::bind_method(D_METHOD("remove_terrain", "terrain_set", "terrain_index"), &TileSet::remove_terrain);
  3034. ClassDB::bind_method(D_METHOD("set_terrain_name", "terrain_set", "terrain_index", "name"), &TileSet::set_terrain_name);
  3035. ClassDB::bind_method(D_METHOD("get_terrain_name", "terrain_set", "terrain_index"), &TileSet::get_terrain_name);
  3036. ClassDB::bind_method(D_METHOD("set_terrain_color", "terrain_set", "terrain_index", "color"), &TileSet::set_terrain_color);
  3037. ClassDB::bind_method(D_METHOD("get_terrain_color", "terrain_set", "terrain_index"), &TileSet::get_terrain_color);
  3038. // Navigation
  3039. ClassDB::bind_method(D_METHOD("get_navigation_layers_count"), &TileSet::get_navigation_layers_count);
  3040. ClassDB::bind_method(D_METHOD("add_navigation_layer", "to_position"), &TileSet::add_navigation_layer, DEFVAL(-1));
  3041. ClassDB::bind_method(D_METHOD("move_navigation_layer", "layer_index", "to_position"), &TileSet::move_navigation_layer);
  3042. ClassDB::bind_method(D_METHOD("remove_navigation_layer", "layer_index"), &TileSet::remove_navigation_layer);
  3043. ClassDB::bind_method(D_METHOD("set_navigation_layer_layers", "layer_index", "layers"), &TileSet::set_navigation_layer_layers);
  3044. ClassDB::bind_method(D_METHOD("get_navigation_layer_layers", "layer_index"), &TileSet::get_navigation_layer_layers);
  3045. ClassDB::bind_method(D_METHOD("set_navigation_layer_layer_value", "layer_index", "layer_number", "value"), &TileSet::set_navigation_layer_layer_value);
  3046. ClassDB::bind_method(D_METHOD("get_navigation_layer_layer_value", "layer_index", "layer_number"), &TileSet::get_navigation_layer_layer_value);
  3047. // Custom data
  3048. ClassDB::bind_method(D_METHOD("get_custom_data_layers_count"), &TileSet::get_custom_data_layers_count);
  3049. ClassDB::bind_method(D_METHOD("add_custom_data_layer", "to_position"), &TileSet::add_custom_data_layer, DEFVAL(-1));
  3050. ClassDB::bind_method(D_METHOD("move_custom_data_layer", "layer_index", "to_position"), &TileSet::move_custom_data_layer);
  3051. ClassDB::bind_method(D_METHOD("remove_custom_data_layer", "layer_index"), &TileSet::remove_custom_data_layer);
  3052. ClassDB::bind_method(D_METHOD("get_custom_data_layer_by_name", "layer_name"), &TileSet::get_custom_data_layer_by_name);
  3053. ClassDB::bind_method(D_METHOD("set_custom_data_layer_name", "layer_index", "layer_name"), &TileSet::set_custom_data_layer_name);
  3054. ClassDB::bind_method(D_METHOD("get_custom_data_layer_name", "layer_index"), &TileSet::get_custom_data_layer_name);
  3055. ClassDB::bind_method(D_METHOD("set_custom_data_layer_type", "layer_index", "layer_type"), &TileSet::set_custom_data_layer_type);
  3056. ClassDB::bind_method(D_METHOD("get_custom_data_layer_type", "layer_index"), &TileSet::get_custom_data_layer_type);
  3057. // Tile proxies
  3058. ClassDB::bind_method(D_METHOD("set_source_level_tile_proxy", "source_from", "source_to"), &TileSet::set_source_level_tile_proxy);
  3059. ClassDB::bind_method(D_METHOD("get_source_level_tile_proxy", "source_from"), &TileSet::get_source_level_tile_proxy);
  3060. ClassDB::bind_method(D_METHOD("has_source_level_tile_proxy", "source_from"), &TileSet::has_source_level_tile_proxy);
  3061. ClassDB::bind_method(D_METHOD("remove_source_level_tile_proxy", "source_from"), &TileSet::remove_source_level_tile_proxy);
  3062. 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);
  3063. ClassDB::bind_method(D_METHOD("get_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::get_coords_level_tile_proxy);
  3064. ClassDB::bind_method(D_METHOD("has_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::has_coords_level_tile_proxy);
  3065. ClassDB::bind_method(D_METHOD("remove_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::remove_coords_level_tile_proxy);
  3066. 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);
  3067. ClassDB::bind_method(D_METHOD("get_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::get_alternative_level_tile_proxy);
  3068. ClassDB::bind_method(D_METHOD("has_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::has_alternative_level_tile_proxy);
  3069. ClassDB::bind_method(D_METHOD("remove_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::remove_alternative_level_tile_proxy);
  3070. ClassDB::bind_method(D_METHOD("map_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::map_tile_proxy);
  3071. ClassDB::bind_method(D_METHOD("cleanup_invalid_tile_proxies"), &TileSet::cleanup_invalid_tile_proxies);
  3072. ClassDB::bind_method(D_METHOD("clear_tile_proxies"), &TileSet::clear_tile_proxies);
  3073. // Patterns
  3074. ClassDB::bind_method(D_METHOD("add_pattern", "pattern", "index"), &TileSet::add_pattern, DEFVAL(-1));
  3075. ClassDB::bind_method(D_METHOD("get_pattern", "index"), &TileSet::get_pattern, DEFVAL(-1));
  3076. ClassDB::bind_method(D_METHOD("remove_pattern", "index"), &TileSet::remove_pattern);
  3077. ClassDB::bind_method(D_METHOD("get_patterns_count"), &TileSet::get_patterns_count);
  3078. ADD_GROUP("Rendering", "");
  3079. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uv_clipping"), "set_uv_clipping", "is_uv_clipping");
  3080. ADD_ARRAY("occlusion_layers", "occlusion_layer_");
  3081. ADD_GROUP("", "");
  3082. ADD_ARRAY("physics_layers", "physics_layer_");
  3083. ADD_ARRAY("terrain_sets", "terrain_set_");
  3084. ADD_ARRAY("navigation_layers", "navigation_layer_");
  3085. ADD_ARRAY("custom_data_layers", "custom_data_layer_");
  3086. // -- Enum binding --
  3087. BIND_ENUM_CONSTANT(TILE_SHAPE_SQUARE);
  3088. BIND_ENUM_CONSTANT(TILE_SHAPE_ISOMETRIC);
  3089. BIND_ENUM_CONSTANT(TILE_SHAPE_HALF_OFFSET_SQUARE);
  3090. BIND_ENUM_CONSTANT(TILE_SHAPE_HEXAGON);
  3091. BIND_ENUM_CONSTANT(TILE_LAYOUT_STACKED);
  3092. BIND_ENUM_CONSTANT(TILE_LAYOUT_STACKED_OFFSET);
  3093. BIND_ENUM_CONSTANT(TILE_LAYOUT_STAIRS_RIGHT);
  3094. BIND_ENUM_CONSTANT(TILE_LAYOUT_STAIRS_DOWN);
  3095. BIND_ENUM_CONSTANT(TILE_LAYOUT_DIAMOND_RIGHT);
  3096. BIND_ENUM_CONSTANT(TILE_LAYOUT_DIAMOND_DOWN);
  3097. BIND_ENUM_CONSTANT(TILE_OFFSET_AXIS_HORIZONTAL);
  3098. BIND_ENUM_CONSTANT(TILE_OFFSET_AXIS_VERTICAL);
  3099. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_RIGHT_SIDE);
  3100. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_RIGHT_CORNER);
  3101. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE);
  3102. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER);
  3103. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_SIDE);
  3104. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_CORNER);
  3105. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_LEFT_SIDE);
  3106. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_LEFT_CORNER);
  3107. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_LEFT_SIDE);
  3108. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_LEFT_CORNER);
  3109. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_LEFT_SIDE);
  3110. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_LEFT_CORNER);
  3111. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_SIDE);
  3112. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_CORNER);
  3113. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  3114. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_RIGHT_CORNER);
  3115. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_CORNERS_AND_SIDES);
  3116. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_CORNERS);
  3117. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_SIDES);
  3118. }
  3119. TileSet::TileSet() {
  3120. // Instantiate the tile meshes.
  3121. tile_lines_mesh.instantiate();
  3122. tile_filled_mesh.instantiate();
  3123. }
  3124. TileSet::~TileSet() {
  3125. #ifndef DISABLE_DEPRECATED
  3126. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  3127. memdelete(E.value);
  3128. }
  3129. #endif // DISABLE_DEPRECATED
  3130. while (!source_ids.is_empty()) {
  3131. remove_source(source_ids[0]);
  3132. }
  3133. }
  3134. /////////////////////////////// TileSetSource //////////////////////////////////////
  3135. void TileSetSource::set_tile_set(const TileSet *p_tile_set) {
  3136. tile_set = p_tile_set;
  3137. }
  3138. void TileSetSource::reset_state() {
  3139. tile_set = nullptr;
  3140. };
  3141. void TileSetSource::_bind_methods() {
  3142. // Base tiles
  3143. ClassDB::bind_method(D_METHOD("get_tiles_count"), &TileSetSource::get_tiles_count);
  3144. ClassDB::bind_method(D_METHOD("get_tile_id", "index"), &TileSetSource::get_tile_id);
  3145. ClassDB::bind_method(D_METHOD("has_tile", "atlas_coords"), &TileSetSource::has_tile);
  3146. // Alternative tiles
  3147. ClassDB::bind_method(D_METHOD("get_alternative_tiles_count", "atlas_coords"), &TileSetSource::get_alternative_tiles_count);
  3148. ClassDB::bind_method(D_METHOD("get_alternative_tile_id", "atlas_coords", "index"), &TileSetSource::get_alternative_tile_id);
  3149. ClassDB::bind_method(D_METHOD("has_alternative_tile", "atlas_coords", "alternative_tile"), &TileSetSource::has_alternative_tile);
  3150. }
  3151. /////////////////////////////// TileSetAtlasSource //////////////////////////////////////
  3152. void TileSetAtlasSource::set_tile_set(const TileSet *p_tile_set) {
  3153. tile_set = p_tile_set;
  3154. // Set the TileSet on all TileData.
  3155. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3156. for (KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  3157. E_alternative.value->set_tile_set(tile_set);
  3158. }
  3159. }
  3160. }
  3161. const TileSet *TileSetAtlasSource::get_tile_set() const {
  3162. return tile_set;
  3163. }
  3164. void TileSetAtlasSource::notify_tile_data_properties_should_change() {
  3165. // Set the TileSet on all TileData.
  3166. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3167. for (KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  3168. E_alternative.value->notify_tile_data_properties_should_change();
  3169. }
  3170. }
  3171. }
  3172. void TileSetAtlasSource::add_occlusion_layer(int p_to_pos) {
  3173. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3174. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3175. E_alternative.value->add_occlusion_layer(p_to_pos);
  3176. }
  3177. }
  3178. }
  3179. void TileSetAtlasSource::move_occlusion_layer(int p_from_index, int p_to_pos) {
  3180. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3181. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3182. E_alternative.value->move_occlusion_layer(p_from_index, p_to_pos);
  3183. }
  3184. }
  3185. }
  3186. void TileSetAtlasSource::remove_occlusion_layer(int p_index) {
  3187. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3188. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3189. E_alternative.value->remove_occlusion_layer(p_index);
  3190. }
  3191. }
  3192. }
  3193. void TileSetAtlasSource::add_physics_layer(int p_to_pos) {
  3194. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3195. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3196. E_alternative.value->add_physics_layer(p_to_pos);
  3197. }
  3198. }
  3199. }
  3200. void TileSetAtlasSource::move_physics_layer(int p_from_index, int p_to_pos) {
  3201. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3202. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3203. E_alternative.value->move_physics_layer(p_from_index, p_to_pos);
  3204. }
  3205. }
  3206. }
  3207. void TileSetAtlasSource::remove_physics_layer(int p_index) {
  3208. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3209. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3210. E_alternative.value->remove_physics_layer(p_index);
  3211. }
  3212. }
  3213. }
  3214. void TileSetAtlasSource::add_terrain_set(int p_to_pos) {
  3215. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3216. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3217. E_alternative.value->add_terrain_set(p_to_pos);
  3218. }
  3219. }
  3220. }
  3221. void TileSetAtlasSource::move_terrain_set(int p_from_index, int p_to_pos) {
  3222. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3223. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3224. E_alternative.value->move_terrain_set(p_from_index, p_to_pos);
  3225. }
  3226. }
  3227. }
  3228. void TileSetAtlasSource::remove_terrain_set(int p_index) {
  3229. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3230. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3231. E_alternative.value->remove_terrain_set(p_index);
  3232. }
  3233. }
  3234. }
  3235. void TileSetAtlasSource::add_terrain(int p_terrain_set, int p_to_pos) {
  3236. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3237. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3238. E_alternative.value->add_terrain(p_terrain_set, p_to_pos);
  3239. }
  3240. }
  3241. }
  3242. void TileSetAtlasSource::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  3243. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3244. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3245. E_alternative.value->move_terrain(p_terrain_set, p_from_index, p_to_pos);
  3246. }
  3247. }
  3248. }
  3249. void TileSetAtlasSource::remove_terrain(int p_terrain_set, int p_index) {
  3250. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3251. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3252. E_alternative.value->remove_terrain(p_terrain_set, p_index);
  3253. }
  3254. }
  3255. }
  3256. void TileSetAtlasSource::add_navigation_layer(int p_to_pos) {
  3257. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3258. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3259. E_alternative.value->add_navigation_layer(p_to_pos);
  3260. }
  3261. }
  3262. }
  3263. void TileSetAtlasSource::move_navigation_layer(int p_from_index, int p_to_pos) {
  3264. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3265. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3266. E_alternative.value->move_navigation_layer(p_from_index, p_to_pos);
  3267. }
  3268. }
  3269. }
  3270. void TileSetAtlasSource::remove_navigation_layer(int p_index) {
  3271. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3272. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3273. E_alternative.value->remove_navigation_layer(p_index);
  3274. }
  3275. }
  3276. }
  3277. void TileSetAtlasSource::add_custom_data_layer(int p_to_pos) {
  3278. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3279. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3280. E_alternative.value->add_custom_data_layer(p_to_pos);
  3281. }
  3282. }
  3283. }
  3284. void TileSetAtlasSource::move_custom_data_layer(int p_from_index, int p_to_pos) {
  3285. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3286. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3287. E_alternative.value->move_custom_data_layer(p_from_index, p_to_pos);
  3288. }
  3289. }
  3290. }
  3291. void TileSetAtlasSource::remove_custom_data_layer(int p_index) {
  3292. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3293. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3294. E_alternative.value->remove_custom_data_layer(p_index);
  3295. }
  3296. }
  3297. }
  3298. void TileSetAtlasSource::reset_state() {
  3299. tile_set = nullptr;
  3300. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3301. for (const KeyValue<int, TileData *> &E_tile_data : E_tile.value.alternatives) {
  3302. memdelete(E_tile_data.value);
  3303. }
  3304. }
  3305. _coords_mapping_cache.clear();
  3306. tiles.clear();
  3307. tiles_ids.clear();
  3308. _queue_update_padded_texture();
  3309. }
  3310. void TileSetAtlasSource::set_texture(Ref<Texture2D> p_texture) {
  3311. if (texture.is_valid()) {
  3312. texture->disconnect(SNAME("changed"), callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  3313. }
  3314. texture = p_texture;
  3315. if (texture.is_valid()) {
  3316. texture->connect(SNAME("changed"), callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  3317. }
  3318. _clear_tiles_outside_texture();
  3319. _queue_update_padded_texture();
  3320. emit_changed();
  3321. }
  3322. Ref<Texture2D> TileSetAtlasSource::get_texture() const {
  3323. return texture;
  3324. }
  3325. void TileSetAtlasSource::set_margins(Vector2i p_margins) {
  3326. if (p_margins.x < 0 || p_margins.y < 0) {
  3327. WARN_PRINT("Atlas source margins should be positive.");
  3328. margins = Vector2i(MAX(0, p_margins.x), MAX(0, p_margins.y));
  3329. } else {
  3330. margins = p_margins;
  3331. }
  3332. _clear_tiles_outside_texture();
  3333. _queue_update_padded_texture();
  3334. emit_changed();
  3335. }
  3336. Vector2i TileSetAtlasSource::get_margins() const {
  3337. return margins;
  3338. }
  3339. void TileSetAtlasSource::set_separation(Vector2i p_separation) {
  3340. if (p_separation.x < 0 || p_separation.y < 0) {
  3341. WARN_PRINT("Atlas source separation should be positive.");
  3342. separation = Vector2i(MAX(0, p_separation.x), MAX(0, p_separation.y));
  3343. } else {
  3344. separation = p_separation;
  3345. }
  3346. _clear_tiles_outside_texture();
  3347. _queue_update_padded_texture();
  3348. emit_changed();
  3349. }
  3350. Vector2i TileSetAtlasSource::get_separation() const {
  3351. return separation;
  3352. }
  3353. void TileSetAtlasSource::set_texture_region_size(Vector2i p_tile_size) {
  3354. if (p_tile_size.x <= 0 || p_tile_size.y <= 0) {
  3355. WARN_PRINT("Atlas source tile_size should be strictly positive.");
  3356. texture_region_size = Vector2i(MAX(1, p_tile_size.x), MAX(1, p_tile_size.y));
  3357. } else {
  3358. texture_region_size = p_tile_size;
  3359. }
  3360. _clear_tiles_outside_texture();
  3361. _queue_update_padded_texture();
  3362. emit_changed();
  3363. }
  3364. Vector2i TileSetAtlasSource::get_texture_region_size() const {
  3365. return texture_region_size;
  3366. }
  3367. void TileSetAtlasSource::set_use_texture_padding(bool p_use_padding) {
  3368. if (use_texture_padding == p_use_padding) {
  3369. return;
  3370. }
  3371. use_texture_padding = p_use_padding;
  3372. _queue_update_padded_texture();
  3373. emit_changed();
  3374. }
  3375. bool TileSetAtlasSource::get_use_texture_padding() const {
  3376. return use_texture_padding;
  3377. }
  3378. Vector2i TileSetAtlasSource::get_atlas_grid_size() const {
  3379. Ref<Texture2D> txt = get_texture();
  3380. if (!txt.is_valid()) {
  3381. return Vector2i();
  3382. }
  3383. ERR_FAIL_COND_V(texture_region_size.x <= 0 || texture_region_size.y <= 0, Vector2i());
  3384. Size2i valid_area = txt->get_size() - margins;
  3385. // Compute the number of valid tiles in the tiles atlas
  3386. Size2i grid_size;
  3387. if (valid_area.x >= texture_region_size.x && valid_area.y >= texture_region_size.y) {
  3388. valid_area -= texture_region_size;
  3389. grid_size = Size2i(1, 1) + valid_area / (texture_region_size + separation);
  3390. }
  3391. return grid_size;
  3392. }
  3393. bool TileSetAtlasSource::_set(const StringName &p_name, const Variant &p_value) {
  3394. Vector<String> components = String(p_name).split("/", true, 2);
  3395. // Compute the vector2i if we have coordinates.
  3396. Vector<String> coords_split = components[0].split(":");
  3397. Vector2i coords = TileSetSource::INVALID_ATLAS_COORDS;
  3398. if (coords_split.size() == 2 && coords_split[0].is_valid_int() && coords_split[1].is_valid_int()) {
  3399. coords = Vector2i(coords_split[0].to_int(), coords_split[1].to_int());
  3400. }
  3401. // Properties.
  3402. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  3403. // Create the tile if needed.
  3404. if (!has_tile(coords)) {
  3405. create_tile(coords);
  3406. }
  3407. if (components.size() >= 2) {
  3408. // Properties.
  3409. if (components[1] == "size_in_atlas") {
  3410. move_tile_in_atlas(coords, coords, p_value);
  3411. return true;
  3412. } else if (components[1] == "next_alternative_id") {
  3413. tiles[coords].next_alternative_id = p_value;
  3414. return true;
  3415. } else if (components[1] == "animation_columns") {
  3416. set_tile_animation_columns(coords, p_value);
  3417. return true;
  3418. } else if (components[1] == "animation_separation") {
  3419. set_tile_animation_separation(coords, p_value);
  3420. return true;
  3421. } else if (components[1] == "animation_speed") {
  3422. set_tile_animation_speed(coords, p_value);
  3423. return true;
  3424. } else if (components[1] == "animation_frames_count") {
  3425. set_tile_animation_frames_count(coords, p_value);
  3426. return true;
  3427. } else if (components.size() >= 3 && components[1].begins_with("animation_frame_") && components[1].trim_prefix("animation_frame_").is_valid_int()) {
  3428. int frame = components[1].trim_prefix("animation_frame_").to_int();
  3429. if (components[2] == "duration") {
  3430. if (frame >= get_tile_animation_frames_count(coords)) {
  3431. set_tile_animation_frames_count(coords, frame + 1);
  3432. }
  3433. set_tile_animation_frame_duration(coords, frame, p_value);
  3434. return true;
  3435. }
  3436. return false;
  3437. } else if (components[1].is_valid_int()) {
  3438. int alternative_id = components[1].to_int();
  3439. if (alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE) {
  3440. // Create the alternative if needed ?
  3441. if (!has_alternative_tile(coords, alternative_id)) {
  3442. create_alternative_tile(coords, alternative_id);
  3443. }
  3444. if (!tiles[coords].alternatives.has(alternative_id)) {
  3445. tiles[coords].alternatives[alternative_id] = memnew(TileData);
  3446. tiles[coords].alternatives[alternative_id]->set_tile_set(tile_set);
  3447. tiles[coords].alternatives[alternative_id]->set_allow_transform(alternative_id > 0);
  3448. tiles[coords].alternatives_ids.push_back(alternative_id);
  3449. }
  3450. if (components.size() >= 3) {
  3451. bool valid;
  3452. tiles[coords].alternatives[alternative_id]->set(components[2], p_value, &valid);
  3453. return valid;
  3454. } else {
  3455. // Only create the alternative if it did not exist yet.
  3456. return true;
  3457. }
  3458. }
  3459. }
  3460. }
  3461. }
  3462. return false;
  3463. }
  3464. bool TileSetAtlasSource::_get(const StringName &p_name, Variant &r_ret) const {
  3465. Vector<String> components = String(p_name).split("/", true, 2);
  3466. // Properties.
  3467. Vector<String> coords_split = components[0].split(":");
  3468. if (coords_split.size() == 2 && coords_split[0].is_valid_int() && coords_split[1].is_valid_int()) {
  3469. Vector2i coords = Vector2i(coords_split[0].to_int(), coords_split[1].to_int());
  3470. if (tiles.has(coords)) {
  3471. if (components.size() >= 2) {
  3472. // Properties.
  3473. if (components[1] == "size_in_atlas") {
  3474. r_ret = tiles[coords].size_in_atlas;
  3475. return true;
  3476. } else if (components[1] == "next_alternative_id") {
  3477. r_ret = tiles[coords].next_alternative_id;
  3478. return true;
  3479. } else if (components[1] == "animation_columns") {
  3480. r_ret = get_tile_animation_columns(coords);
  3481. return true;
  3482. } else if (components[1] == "animation_separation") {
  3483. r_ret = get_tile_animation_separation(coords);
  3484. return true;
  3485. } else if (components[1] == "animation_speed") {
  3486. r_ret = get_tile_animation_speed(coords);
  3487. return true;
  3488. } else if (components[1] == "animation_frames_count") {
  3489. r_ret = get_tile_animation_frames_count(coords);
  3490. return true;
  3491. } else if (components.size() >= 3 && components[1].begins_with("animation_frame_") && components[1].trim_prefix("animation_frame_").is_valid_int()) {
  3492. int frame = components[1].trim_prefix("animation_frame_").to_int();
  3493. if (frame < 0 || frame >= get_tile_animation_frames_count(coords)) {
  3494. return false;
  3495. }
  3496. if (components[2] == "duration") {
  3497. r_ret = get_tile_animation_frame_duration(coords, frame);
  3498. return true;
  3499. }
  3500. return false;
  3501. } else if (components[1].is_valid_int()) {
  3502. int alternative_id = components[1].to_int();
  3503. if (alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE && tiles[coords].alternatives.has(alternative_id)) {
  3504. if (components.size() >= 3) {
  3505. bool valid;
  3506. r_ret = tiles[coords].alternatives[alternative_id]->get(components[2], &valid);
  3507. return valid;
  3508. } else {
  3509. // Only to notify the tile alternative exists.
  3510. r_ret = alternative_id;
  3511. return true;
  3512. }
  3513. }
  3514. }
  3515. }
  3516. }
  3517. }
  3518. return false;
  3519. }
  3520. void TileSetAtlasSource::_get_property_list(List<PropertyInfo> *p_list) const {
  3521. // Atlases data.
  3522. PropertyInfo property_info;
  3523. for (const KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3524. List<PropertyInfo> tile_property_list;
  3525. // size_in_atlas
  3526. property_info = PropertyInfo(Variant::VECTOR2I, "size_in_atlas", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3527. if (E_tile.value.size_in_atlas == Vector2i(1, 1)) {
  3528. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3529. }
  3530. tile_property_list.push_back(property_info);
  3531. // next_alternative_id
  3532. property_info = PropertyInfo(Variant::INT, "next_alternative_id", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3533. if (E_tile.value.next_alternative_id == 1) {
  3534. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3535. }
  3536. tile_property_list.push_back(property_info);
  3537. // animation_columns.
  3538. property_info = PropertyInfo(Variant::INT, "animation_columns", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3539. if (E_tile.value.animation_columns == 0) {
  3540. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3541. }
  3542. tile_property_list.push_back(property_info);
  3543. // animation_separation.
  3544. property_info = PropertyInfo(Variant::INT, "animation_separation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3545. if (E_tile.value.animation_separation == Vector2i()) {
  3546. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3547. }
  3548. tile_property_list.push_back(property_info);
  3549. // animation_speed.
  3550. property_info = PropertyInfo(Variant::FLOAT, "animation_speed", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3551. if (E_tile.value.animation_speed == 1.0) {
  3552. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3553. }
  3554. tile_property_list.push_back(property_info);
  3555. // animation_frames_count.
  3556. tile_property_list.push_back(PropertyInfo(Variant::INT, "animation_frames_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  3557. // animation_frame_*.
  3558. bool store_durations = tiles[E_tile.key].animation_frames_durations.size() >= 2;
  3559. for (int i = 0; i < (int)tiles[E_tile.key].animation_frames_durations.size(); i++) {
  3560. property_info = PropertyInfo(Variant::FLOAT, vformat("animation_frame_%d/duration", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3561. if (!store_durations) {
  3562. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3563. }
  3564. tile_property_list.push_back(property_info);
  3565. }
  3566. for (const KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  3567. // Add a dummy property to show the alternative exists.
  3568. tile_property_list.push_back(PropertyInfo(Variant::INT, vformat("%d", E_alternative.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  3569. // Get the alternative tile's properties and append them to the list of properties.
  3570. List<PropertyInfo> alternative_property_list;
  3571. E_alternative.value->get_property_list(&alternative_property_list);
  3572. for (PropertyInfo &alternative_property_info : alternative_property_list) {
  3573. Variant default_value = ClassDB::class_get_default_property_value("TileData", alternative_property_info.name);
  3574. Variant value = E_alternative.value->get(alternative_property_info.name);
  3575. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {
  3576. alternative_property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3577. }
  3578. alternative_property_info.name = vformat("%s/%s", vformat("%d", E_alternative.key), alternative_property_info.name);
  3579. tile_property_list.push_back(alternative_property_info);
  3580. }
  3581. }
  3582. // Add all alternative.
  3583. for (PropertyInfo &tile_property_info : tile_property_list) {
  3584. tile_property_info.name = vformat("%s/%s", vformat("%d:%d", E_tile.key.x, E_tile.key.y), tile_property_info.name);
  3585. p_list->push_back(tile_property_info);
  3586. }
  3587. }
  3588. }
  3589. void TileSetAtlasSource::create_tile(const Vector2i p_atlas_coords, const Vector2i p_size) {
  3590. // Create a tile if it does not exists.
  3591. ERR_FAIL_COND(p_atlas_coords.x < 0 || p_atlas_coords.y < 0);
  3592. ERR_FAIL_COND(p_size.x <= 0 || p_size.y <= 0);
  3593. bool room_for_tile = has_room_for_tile(p_atlas_coords, p_size, 1, Vector2i(), 1);
  3594. 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.");
  3595. // Initialize the tile data.
  3596. TileAlternativesData tad;
  3597. tad.size_in_atlas = p_size;
  3598. tad.animation_frames_durations.push_back(1.0);
  3599. tad.alternatives[0] = memnew(TileData);
  3600. tad.alternatives[0]->set_tile_set(tile_set);
  3601. tad.alternatives[0]->set_allow_transform(false);
  3602. tad.alternatives[0]->connect("changed", callable_mp((Resource *)this, &TileSetAtlasSource::emit_changed));
  3603. tad.alternatives[0]->notify_property_list_changed();
  3604. tad.alternatives_ids.push_back(0);
  3605. // Create and resize the tile.
  3606. tiles.insert(p_atlas_coords, tad);
  3607. tiles_ids.push_back(p_atlas_coords);
  3608. tiles_ids.sort();
  3609. _create_coords_mapping_cache(p_atlas_coords);
  3610. _queue_update_padded_texture();
  3611. emit_signal(SNAME("changed"));
  3612. }
  3613. void TileSetAtlasSource::remove_tile(Vector2i p_atlas_coords) {
  3614. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3615. // Remove all covered positions from the mapping cache
  3616. _clear_coords_mapping_cache(p_atlas_coords);
  3617. // Free tile data.
  3618. for (const KeyValue<int, TileData *> &E_tile_data : tiles[p_atlas_coords].alternatives) {
  3619. memdelete(E_tile_data.value);
  3620. }
  3621. // Delete the tile
  3622. tiles.erase(p_atlas_coords);
  3623. tiles_ids.erase(p_atlas_coords);
  3624. tiles_ids.sort();
  3625. _queue_update_padded_texture();
  3626. emit_signal(SNAME("changed"));
  3627. }
  3628. bool TileSetAtlasSource::has_tile(Vector2i p_atlas_coords) const {
  3629. return tiles.has(p_atlas_coords);
  3630. }
  3631. Vector2i TileSetAtlasSource::get_tile_at_coords(Vector2i p_atlas_coords) const {
  3632. if (!_coords_mapping_cache.has(p_atlas_coords)) {
  3633. return INVALID_ATLAS_COORDS;
  3634. }
  3635. return _coords_mapping_cache[p_atlas_coords];
  3636. }
  3637. void TileSetAtlasSource::set_tile_animation_columns(const Vector2i p_atlas_coords, int p_frame_columns) {
  3638. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3639. ERR_FAIL_COND(p_frame_columns < 0);
  3640. TileAlternativesData &tad = tiles[p_atlas_coords];
  3641. 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);
  3642. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  3643. _clear_coords_mapping_cache(p_atlas_coords);
  3644. tiles[p_atlas_coords].animation_columns = p_frame_columns;
  3645. _create_coords_mapping_cache(p_atlas_coords);
  3646. _queue_update_padded_texture();
  3647. emit_signal(SNAME("changed"));
  3648. }
  3649. int TileSetAtlasSource::get_tile_animation_columns(const Vector2i p_atlas_coords) const {
  3650. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3651. return tiles[p_atlas_coords].animation_columns;
  3652. }
  3653. void TileSetAtlasSource::set_tile_animation_separation(const Vector2i p_atlas_coords, const Vector2i p_separation) {
  3654. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3655. ERR_FAIL_COND(p_separation.x < 0 || p_separation.y < 0);
  3656. TileAlternativesData &tad = tiles[p_atlas_coords];
  3657. 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);
  3658. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  3659. _clear_coords_mapping_cache(p_atlas_coords);
  3660. tiles[p_atlas_coords].animation_separation = p_separation;
  3661. _create_coords_mapping_cache(p_atlas_coords);
  3662. _queue_update_padded_texture();
  3663. emit_signal(SNAME("changed"));
  3664. }
  3665. Vector2i TileSetAtlasSource::get_tile_animation_separation(const Vector2i p_atlas_coords) const {
  3666. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3667. return tiles[p_atlas_coords].animation_separation;
  3668. }
  3669. void TileSetAtlasSource::set_tile_animation_speed(const Vector2i p_atlas_coords, real_t p_speed) {
  3670. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3671. ERR_FAIL_COND(p_speed <= 0);
  3672. tiles[p_atlas_coords].animation_speed = p_speed;
  3673. emit_signal(SNAME("changed"));
  3674. }
  3675. real_t TileSetAtlasSource::get_tile_animation_speed(const Vector2i p_atlas_coords) const {
  3676. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1.0, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3677. return tiles[p_atlas_coords].animation_speed;
  3678. }
  3679. void TileSetAtlasSource::set_tile_animation_frames_count(const Vector2i p_atlas_coords, int p_frames_count) {
  3680. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3681. ERR_FAIL_COND(p_frames_count < 1);
  3682. int old_size = tiles[p_atlas_coords].animation_frames_durations.size();
  3683. if (p_frames_count == old_size) {
  3684. return;
  3685. }
  3686. TileAlternativesData &tad = tiles[p_atlas_coords];
  3687. 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);
  3688. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  3689. _clear_coords_mapping_cache(p_atlas_coords);
  3690. tiles[p_atlas_coords].animation_frames_durations.resize(p_frames_count);
  3691. for (int i = old_size; i < p_frames_count; i++) {
  3692. tiles[p_atlas_coords].animation_frames_durations[i] = 1.0;
  3693. }
  3694. _create_coords_mapping_cache(p_atlas_coords);
  3695. _queue_update_padded_texture();
  3696. notify_property_list_changed();
  3697. emit_signal(SNAME("changed"));
  3698. }
  3699. int TileSetAtlasSource::get_tile_animation_frames_count(const Vector2i p_atlas_coords) const {
  3700. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3701. return tiles[p_atlas_coords].animation_frames_durations.size();
  3702. }
  3703. void TileSetAtlasSource::set_tile_animation_frame_duration(const Vector2i p_atlas_coords, int p_frame_index, real_t p_duration) {
  3704. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3705. ERR_FAIL_INDEX(p_frame_index, (int)tiles[p_atlas_coords].animation_frames_durations.size());
  3706. ERR_FAIL_COND(p_duration <= 0.0);
  3707. tiles[p_atlas_coords].animation_frames_durations[p_frame_index] = p_duration;
  3708. emit_signal(SNAME("changed"));
  3709. }
  3710. real_t TileSetAtlasSource::get_tile_animation_frame_duration(const Vector2i p_atlas_coords, int p_frame_index) 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. ERR_FAIL_INDEX_V(p_frame_index, (int)tiles[p_atlas_coords].animation_frames_durations.size(), 0.0);
  3713. return tiles[p_atlas_coords].animation_frames_durations[p_frame_index];
  3714. }
  3715. real_t TileSetAtlasSource::get_tile_animation_total_duration(const Vector2i p_atlas_coords) const {
  3716. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3717. real_t sum = 0.0;
  3718. for (const real_t &duration : tiles[p_atlas_coords].animation_frames_durations) {
  3719. sum += duration;
  3720. }
  3721. return sum;
  3722. }
  3723. Vector2i TileSetAtlasSource::get_tile_size_in_atlas(Vector2i p_atlas_coords) const {
  3724. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(-1, -1), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3725. return tiles[p_atlas_coords].size_in_atlas;
  3726. }
  3727. int TileSetAtlasSource::get_tiles_count() const {
  3728. return tiles_ids.size();
  3729. }
  3730. Vector2i TileSetAtlasSource::get_tile_id(int p_index) const {
  3731. ERR_FAIL_INDEX_V(p_index, tiles_ids.size(), TileSetSource::INVALID_ATLAS_COORDS);
  3732. return tiles_ids[p_index];
  3733. }
  3734. 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 {
  3735. if (p_atlas_coords.x < 0 || p_atlas_coords.y < 0) {
  3736. return false;
  3737. }
  3738. if (p_size.x <= 0 || p_size.y <= 0) {
  3739. return false;
  3740. }
  3741. Size2i atlas_grid_size = get_atlas_grid_size();
  3742. for (int frame = 0; frame < p_frames_count; frame++) {
  3743. 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));
  3744. for (int x = 0; x < p_size.x; x++) {
  3745. for (int y = 0; y < p_size.y; y++) {
  3746. Vector2i coords = frame_coords + Vector2i(x, y);
  3747. if (_coords_mapping_cache.has(coords) && _coords_mapping_cache[coords] != p_ignored_tile) {
  3748. return false;
  3749. }
  3750. if (coords.x >= atlas_grid_size.x || coords.y >= atlas_grid_size.y) {
  3751. return false;
  3752. }
  3753. }
  3754. }
  3755. }
  3756. return true;
  3757. }
  3758. PackedVector2Array TileSetAtlasSource::get_tiles_to_be_removed_on_change(Ref<Texture2D> p_texture, Vector2i p_margins, Vector2i p_separation, Vector2i p_texture_region_size) {
  3759. ERR_FAIL_COND_V(p_margins.x < 0 || p_margins.y < 0, PackedVector2Array());
  3760. ERR_FAIL_COND_V(p_separation.x < 0 || p_separation.y < 0, PackedVector2Array());
  3761. ERR_FAIL_COND_V(p_texture_region_size.x <= 0 || p_texture_region_size.y <= 0, PackedVector2Array());
  3762. // Compute the new atlas grid size.
  3763. Size2 new_grid_size;
  3764. if (p_texture.is_valid()) {
  3765. Size2i valid_area = p_texture->get_size() - p_margins;
  3766. // Compute the number of valid tiles in the tiles atlas
  3767. if (valid_area.x >= p_texture_region_size.x && valid_area.y >= p_texture_region_size.y) {
  3768. valid_area -= p_texture_region_size;
  3769. new_grid_size = Size2i(1, 1) + valid_area / (p_texture_region_size + p_separation);
  3770. }
  3771. }
  3772. Vector<Vector2> output;
  3773. for (KeyValue<Vector2i, TileAlternativesData> &E : tiles) {
  3774. for (unsigned int frame = 0; frame < E.value.animation_frames_durations.size(); frame++) {
  3775. 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));
  3776. frame_coords += E.value.size_in_atlas;
  3777. if (frame_coords.x > new_grid_size.x || frame_coords.y > new_grid_size.y) {
  3778. output.push_back(E.key);
  3779. break;
  3780. }
  3781. }
  3782. }
  3783. return output;
  3784. }
  3785. Rect2i TileSetAtlasSource::get_tile_texture_region(Vector2i p_atlas_coords, int p_frame) const {
  3786. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Rect2i(), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3787. ERR_FAIL_INDEX_V(p_frame, (int)tiles[p_atlas_coords].animation_frames_durations.size(), Rect2i());
  3788. const TileAlternativesData &tad = tiles[p_atlas_coords];
  3789. Vector2i size_in_atlas = tad.size_in_atlas;
  3790. Vector2 region_size = texture_region_size * size_in_atlas + separation * (size_in_atlas - Vector2i(1, 1));
  3791. 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));
  3792. Vector2 origin = margins + (frame_coords * (texture_region_size + separation));
  3793. return Rect2(origin, region_size);
  3794. }
  3795. bool TileSetAtlasSource::is_position_in_tile_texture_region(const Vector2i p_atlas_coords, int p_alternative_tile, Vector2 p_position) const {
  3796. Size2 size = get_tile_texture_region(p_atlas_coords).size;
  3797. Rect2 rect = Rect2(-size / 2 - get_tile_data(p_atlas_coords, p_alternative_tile)->get_texture_origin(), size);
  3798. return rect.has_point(p_position);
  3799. }
  3800. // Getters for texture and tile region (padded or not)
  3801. Ref<Texture2D> TileSetAtlasSource::get_runtime_texture() const {
  3802. if (use_texture_padding) {
  3803. return padded_texture;
  3804. } else {
  3805. return texture;
  3806. }
  3807. }
  3808. Rect2i TileSetAtlasSource::get_runtime_tile_texture_region(Vector2i p_atlas_coords, int p_frame) const {
  3809. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Rect2i(), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3810. ERR_FAIL_INDEX_V(p_frame, (int)tiles[p_atlas_coords].animation_frames_durations.size(), Rect2i());
  3811. Rect2i src_rect = get_tile_texture_region(p_atlas_coords, p_frame);
  3812. if (use_texture_padding) {
  3813. const TileAlternativesData &tad = tiles[p_atlas_coords];
  3814. 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));
  3815. Vector2i base_pos = frame_coords * (texture_region_size + Vector2i(2, 2)) + Vector2i(1, 1);
  3816. return Rect2i(base_pos, src_rect.size);
  3817. } else {
  3818. return src_rect;
  3819. }
  3820. }
  3821. void TileSetAtlasSource::move_tile_in_atlas(Vector2i p_atlas_coords, Vector2i p_new_atlas_coords, Vector2i p_new_size) {
  3822. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3823. TileAlternativesData &tad = tiles[p_atlas_coords];
  3824. // Compute the actual new rect from arguments.
  3825. Vector2i new_atlas_coords = (p_new_atlas_coords != INVALID_ATLAS_COORDS) ? p_new_atlas_coords : p_atlas_coords;
  3826. Vector2i new_size = (p_new_size != Vector2i(-1, -1)) ? p_new_size : tad.size_in_atlas;
  3827. if (new_atlas_coords == p_atlas_coords && new_size == tad.size_in_atlas) {
  3828. return;
  3829. }
  3830. 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);
  3831. 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));
  3832. _clear_coords_mapping_cache(p_atlas_coords);
  3833. // Move the tile and update its size.
  3834. if (new_atlas_coords != p_atlas_coords) {
  3835. tiles[new_atlas_coords] = tiles[p_atlas_coords];
  3836. tiles.erase(p_atlas_coords);
  3837. tiles_ids.erase(p_atlas_coords);
  3838. tiles_ids.push_back(new_atlas_coords);
  3839. tiles_ids.sort();
  3840. }
  3841. tiles[new_atlas_coords].size_in_atlas = new_size;
  3842. _create_coords_mapping_cache(new_atlas_coords);
  3843. _queue_update_padded_texture();
  3844. emit_signal(SNAME("changed"));
  3845. }
  3846. int TileSetAtlasSource::create_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_id_override) {
  3847. 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)));
  3848. 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));
  3849. int new_alternative_id = p_alternative_id_override >= 0 ? p_alternative_id_override : tiles[p_atlas_coords].next_alternative_id;
  3850. tiles[p_atlas_coords].alternatives[new_alternative_id] = memnew(TileData);
  3851. tiles[p_atlas_coords].alternatives[new_alternative_id]->set_tile_set(tile_set);
  3852. tiles[p_atlas_coords].alternatives[new_alternative_id]->set_allow_transform(true);
  3853. tiles[p_atlas_coords].alternatives[new_alternative_id]->connect("changed", callable_mp((Resource *)this, &TileSetAtlasSource::emit_changed));
  3854. tiles[p_atlas_coords].alternatives[new_alternative_id]->notify_property_list_changed();
  3855. tiles[p_atlas_coords].alternatives_ids.push_back(new_alternative_id);
  3856. tiles[p_atlas_coords].alternatives_ids.sort();
  3857. _compute_next_alternative_id(p_atlas_coords);
  3858. emit_signal(SNAME("changed"));
  3859. return new_alternative_id;
  3860. }
  3861. void TileSetAtlasSource::remove_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) {
  3862. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3863. 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)));
  3864. ERR_FAIL_COND_MSG(p_alternative_tile == 0, "Cannot remove the alternative with id 0, the base tile alternative cannot be removed.");
  3865. memdelete(tiles[p_atlas_coords].alternatives[p_alternative_tile]);
  3866. tiles[p_atlas_coords].alternatives.erase(p_alternative_tile);
  3867. tiles[p_atlas_coords].alternatives_ids.erase(p_alternative_tile);
  3868. tiles[p_atlas_coords].alternatives_ids.sort();
  3869. emit_signal(SNAME("changed"));
  3870. }
  3871. void TileSetAtlasSource::set_alternative_tile_id(const Vector2i p_atlas_coords, int p_alternative_tile, int p_new_id) {
  3872. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3873. 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)));
  3874. ERR_FAIL_COND_MSG(p_alternative_tile == 0, "Cannot change the alternative with id 0, the base tile alternative cannot be modified.");
  3875. 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)));
  3876. tiles[p_atlas_coords].alternatives[p_new_id] = tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3877. tiles[p_atlas_coords].alternatives_ids.push_back(p_new_id);
  3878. tiles[p_atlas_coords].alternatives.erase(p_alternative_tile);
  3879. tiles[p_atlas_coords].alternatives_ids.erase(p_alternative_tile);
  3880. tiles[p_atlas_coords].alternatives_ids.sort();
  3881. emit_signal(SNAME("changed"));
  3882. }
  3883. bool TileSetAtlasSource::has_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  3884. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), false, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3885. return tiles[p_atlas_coords].alternatives.has(p_alternative_tile);
  3886. }
  3887. int TileSetAtlasSource::get_next_alternative_tile_id(const Vector2i p_atlas_coords) const {
  3888. 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)));
  3889. return tiles[p_atlas_coords].next_alternative_id;
  3890. }
  3891. int TileSetAtlasSource::get_alternative_tiles_count(const Vector2i p_atlas_coords) const {
  3892. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), -1, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3893. return tiles[p_atlas_coords].alternatives_ids.size();
  3894. }
  3895. int TileSetAtlasSource::get_alternative_tile_id(const Vector2i p_atlas_coords, int p_index) const {
  3896. 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)));
  3897. ERR_FAIL_INDEX_V(p_index, tiles[p_atlas_coords].alternatives_ids.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  3898. return tiles[p_atlas_coords].alternatives_ids[p_index];
  3899. }
  3900. TileData *TileSetAtlasSource::get_tile_data(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  3901. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3902. 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)));
  3903. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3904. }
  3905. void TileSetAtlasSource::_bind_methods() {
  3906. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TileSetAtlasSource::set_texture);
  3907. ClassDB::bind_method(D_METHOD("get_texture"), &TileSetAtlasSource::get_texture);
  3908. ClassDB::bind_method(D_METHOD("set_margins", "margins"), &TileSetAtlasSource::set_margins);
  3909. ClassDB::bind_method(D_METHOD("get_margins"), &TileSetAtlasSource::get_margins);
  3910. ClassDB::bind_method(D_METHOD("set_separation", "separation"), &TileSetAtlasSource::set_separation);
  3911. ClassDB::bind_method(D_METHOD("get_separation"), &TileSetAtlasSource::get_separation);
  3912. ClassDB::bind_method(D_METHOD("set_texture_region_size", "texture_region_size"), &TileSetAtlasSource::set_texture_region_size);
  3913. ClassDB::bind_method(D_METHOD("get_texture_region_size"), &TileSetAtlasSource::get_texture_region_size);
  3914. ClassDB::bind_method(D_METHOD("set_use_texture_padding", "use_texture_padding"), &TileSetAtlasSource::set_use_texture_padding);
  3915. ClassDB::bind_method(D_METHOD("get_use_texture_padding"), &TileSetAtlasSource::get_use_texture_padding);
  3916. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_NO_EDITOR), "set_texture", "get_texture");
  3917. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "margins", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NO_EDITOR), "set_margins", "get_margins");
  3918. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "separation", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NO_EDITOR), "set_separation", "get_separation");
  3919. 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");
  3920. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_texture_padding", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_use_texture_padding", "get_use_texture_padding");
  3921. // Base tiles
  3922. ClassDB::bind_method(D_METHOD("create_tile", "atlas_coords", "size"), &TileSetAtlasSource::create_tile, DEFVAL(Vector2i(1, 1)));
  3923. 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
  3924. 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)));
  3925. ClassDB::bind_method(D_METHOD("get_tile_size_in_atlas", "atlas_coords"), &TileSetAtlasSource::get_tile_size_in_atlas);
  3926. 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));
  3927. 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);
  3928. ClassDB::bind_method(D_METHOD("get_tile_at_coords", "atlas_coords"), &TileSetAtlasSource::get_tile_at_coords);
  3929. ClassDB::bind_method(D_METHOD("set_tile_animation_columns", "atlas_coords", "frame_columns"), &TileSetAtlasSource::set_tile_animation_columns);
  3930. ClassDB::bind_method(D_METHOD("get_tile_animation_columns", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_columns);
  3931. ClassDB::bind_method(D_METHOD("set_tile_animation_separation", "atlas_coords", "separation"), &TileSetAtlasSource::set_tile_animation_separation);
  3932. ClassDB::bind_method(D_METHOD("get_tile_animation_separation", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_separation);
  3933. ClassDB::bind_method(D_METHOD("set_tile_animation_speed", "atlas_coords", "speed"), &TileSetAtlasSource::set_tile_animation_speed);
  3934. ClassDB::bind_method(D_METHOD("get_tile_animation_speed", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_speed);
  3935. ClassDB::bind_method(D_METHOD("set_tile_animation_frames_count", "atlas_coords", "frames_count"), &TileSetAtlasSource::set_tile_animation_frames_count);
  3936. ClassDB::bind_method(D_METHOD("get_tile_animation_frames_count", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_frames_count);
  3937. ClassDB::bind_method(D_METHOD("set_tile_animation_frame_duration", "atlas_coords", "frame_index", "duration"), &TileSetAtlasSource::set_tile_animation_frame_duration);
  3938. ClassDB::bind_method(D_METHOD("get_tile_animation_frame_duration", "atlas_coords", "frame_index"), &TileSetAtlasSource::get_tile_animation_frame_duration);
  3939. ClassDB::bind_method(D_METHOD("get_tile_animation_total_duration", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_total_duration);
  3940. // Alternative tiles
  3941. ClassDB::bind_method(D_METHOD("create_alternative_tile", "atlas_coords", "alternative_id_override"), &TileSetAtlasSource::create_alternative_tile, DEFVAL(INVALID_TILE_ALTERNATIVE));
  3942. ClassDB::bind_method(D_METHOD("remove_alternative_tile", "atlas_coords", "alternative_tile"), &TileSetAtlasSource::remove_alternative_tile);
  3943. ClassDB::bind_method(D_METHOD("set_alternative_tile_id", "atlas_coords", "alternative_tile", "new_id"), &TileSetAtlasSource::set_alternative_tile_id);
  3944. ClassDB::bind_method(D_METHOD("get_next_alternative_tile_id", "atlas_coords"), &TileSetAtlasSource::get_next_alternative_tile_id);
  3945. ClassDB::bind_method(D_METHOD("get_tile_data", "atlas_coords", "alternative_tile"), &TileSetAtlasSource::get_tile_data);
  3946. // Helpers.
  3947. ClassDB::bind_method(D_METHOD("get_atlas_grid_size"), &TileSetAtlasSource::get_atlas_grid_size);
  3948. ClassDB::bind_method(D_METHOD("get_tile_texture_region", "atlas_coords", "frame"), &TileSetAtlasSource::get_tile_texture_region, DEFVAL(0));
  3949. // Getters for texture and tile region (padded or not)
  3950. ClassDB::bind_method(D_METHOD("_update_padded_texture"), &TileSetAtlasSource::_update_padded_texture);
  3951. ClassDB::bind_method(D_METHOD("get_runtime_texture"), &TileSetAtlasSource::get_runtime_texture);
  3952. ClassDB::bind_method(D_METHOD("get_runtime_tile_texture_region", "atlas_coords", "frame"), &TileSetAtlasSource::get_runtime_tile_texture_region);
  3953. }
  3954. TileSetAtlasSource::~TileSetAtlasSource() {
  3955. // Free everything needed.
  3956. for (KeyValue<Vector2i, TileAlternativesData> &E_alternatives : tiles) {
  3957. for (KeyValue<int, TileData *> &E_tile_data : E_alternatives.value.alternatives) {
  3958. memdelete(E_tile_data.value);
  3959. }
  3960. }
  3961. }
  3962. TileData *TileSetAtlasSource::_get_atlas_tile_data(Vector2i p_atlas_coords, int p_alternative_tile) {
  3963. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3964. 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)));
  3965. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3966. }
  3967. const TileData *TileSetAtlasSource::_get_atlas_tile_data(Vector2i p_atlas_coords, int p_alternative_tile) const {
  3968. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3969. 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)));
  3970. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3971. }
  3972. void TileSetAtlasSource::_compute_next_alternative_id(const Vector2i p_atlas_coords) {
  3973. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3974. while (tiles[p_atlas_coords].alternatives.has(tiles[p_atlas_coords].next_alternative_id)) {
  3975. tiles[p_atlas_coords].next_alternative_id = (tiles[p_atlas_coords].next_alternative_id % 1073741823) + 1; // 2 ** 30
  3976. };
  3977. }
  3978. void TileSetAtlasSource::_clear_coords_mapping_cache(Vector2i p_atlas_coords) {
  3979. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3980. TileAlternativesData &tad = tiles[p_atlas_coords];
  3981. for (int frame = 0; frame < (int)tad.animation_frames_durations.size(); frame++) {
  3982. 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));
  3983. for (int x = 0; x < tad.size_in_atlas.x; x++) {
  3984. for (int y = 0; y < tad.size_in_atlas.y; y++) {
  3985. Vector2i coords = frame_coords + Vector2i(x, y);
  3986. if (!_coords_mapping_cache.has(coords)) {
  3987. WARN_PRINT(vformat("TileSetAtlasSource has no cached tile at position %s, the position cache might be corrupted.", coords));
  3988. } else {
  3989. if (_coords_mapping_cache[coords] != p_atlas_coords) {
  3990. WARN_PRINT(vformat("The position cache at position %s is pointing to a wrong tile, the position cache might be corrupted.", coords));
  3991. }
  3992. _coords_mapping_cache.erase(coords);
  3993. }
  3994. }
  3995. }
  3996. }
  3997. }
  3998. void TileSetAtlasSource::_create_coords_mapping_cache(Vector2i p_atlas_coords) {
  3999. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4000. TileAlternativesData &tad = tiles[p_atlas_coords];
  4001. for (int frame = 0; frame < (int)tad.animation_frames_durations.size(); frame++) {
  4002. 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));
  4003. for (int x = 0; x < tad.size_in_atlas.x; x++) {
  4004. for (int y = 0; y < tad.size_in_atlas.y; y++) {
  4005. Vector2i coords = frame_coords + Vector2i(x, y);
  4006. if (_coords_mapping_cache.has(coords)) {
  4007. WARN_PRINT(vformat("The cache already has a tile for position %s, the position cache might be corrupted.", coords));
  4008. }
  4009. _coords_mapping_cache[coords] = p_atlas_coords;
  4010. }
  4011. }
  4012. }
  4013. }
  4014. void TileSetAtlasSource::_clear_tiles_outside_texture() {
  4015. LocalVector<Vector2i> to_remove;
  4016. for (const KeyValue<Vector2i, TileSetAtlasSource::TileAlternativesData> &E : tiles) {
  4017. 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)) {
  4018. to_remove.push_back(E.key);
  4019. }
  4020. }
  4021. for (const Vector2i &v : to_remove) {
  4022. remove_tile(v);
  4023. }
  4024. }
  4025. void TileSetAtlasSource::_queue_update_padded_texture() {
  4026. padded_texture_needs_update = true;
  4027. call_deferred(SNAME("_update_padded_texture"));
  4028. }
  4029. void TileSetAtlasSource::_update_padded_texture() {
  4030. if (!padded_texture_needs_update) {
  4031. return;
  4032. }
  4033. padded_texture_needs_update = false;
  4034. padded_texture = Ref<ImageTexture>();
  4035. if (!texture.is_valid()) {
  4036. return;
  4037. }
  4038. if (!use_texture_padding) {
  4039. return;
  4040. }
  4041. Size2 size = get_atlas_grid_size() * (texture_region_size + Vector2i(2, 2));
  4042. Ref<Image> src = texture->get_image();
  4043. if (!src.is_valid()) {
  4044. return;
  4045. }
  4046. Ref<Image> image = Image::create_empty(size.x, size.y, false, src->get_format());
  4047. for (KeyValue<Vector2i, TileAlternativesData> kv : tiles) {
  4048. for (int frame = 0; frame < (int)kv.value.animation_frames_durations.size(); frame++) {
  4049. // Compute the source rects.
  4050. Rect2i src_rect = get_tile_texture_region(kv.key, frame);
  4051. Rect2i top_src_rect = Rect2i(src_rect.position, Vector2i(src_rect.size.x, 1));
  4052. Rect2i bottom_src_rect = Rect2i(src_rect.position + Vector2i(0, src_rect.size.y - 1), Vector2i(src_rect.size.x, 1));
  4053. Rect2i left_src_rect = Rect2i(src_rect.position, Vector2i(1, src_rect.size.y));
  4054. Rect2i right_src_rect = Rect2i(src_rect.position + Vector2i(src_rect.size.x - 1, 0), Vector2i(1, src_rect.size.y));
  4055. // Copy the tile and the paddings.
  4056. 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));
  4057. Vector2i base_pos = frame_coords * (texture_region_size + Vector2i(2, 2)) + Vector2i(1, 1);
  4058. image->blit_rect(*src, src_rect, base_pos);
  4059. image->blit_rect(*src, top_src_rect, base_pos + Vector2i(0, -1));
  4060. image->blit_rect(*src, bottom_src_rect, base_pos + Vector2i(0, src_rect.size.y));
  4061. image->blit_rect(*src, left_src_rect, base_pos + Vector2i(-1, 0));
  4062. image->blit_rect(*src, right_src_rect, base_pos + Vector2i(src_rect.size.x, 0));
  4063. image->set_pixelv(base_pos + Vector2i(-1, -1), src->get_pixelv(src_rect.position));
  4064. image->set_pixelv(base_pos + Vector2i(src_rect.size.x, -1), src->get_pixelv(src_rect.position + Vector2i(src_rect.size.x - 1, 0)));
  4065. image->set_pixelv(base_pos + Vector2i(-1, src_rect.size.y), src->get_pixelv(src_rect.position + Vector2i(0, src_rect.size.y - 1)));
  4066. 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)));
  4067. }
  4068. }
  4069. if (!padded_texture.is_valid()) {
  4070. padded_texture.instantiate();
  4071. }
  4072. padded_texture->set_image(image);
  4073. emit_changed();
  4074. }
  4075. /////////////////////////////// TileSetScenesCollectionSource //////////////////////////////////////
  4076. void TileSetScenesCollectionSource::_compute_next_alternative_id() {
  4077. while (scenes.has(next_scene_id)) {
  4078. next_scene_id = (next_scene_id % 1073741823) + 1; // 2 ** 30
  4079. };
  4080. }
  4081. int TileSetScenesCollectionSource::get_tiles_count() const {
  4082. return 1;
  4083. }
  4084. Vector2i TileSetScenesCollectionSource::get_tile_id(int p_tile_index) const {
  4085. ERR_FAIL_COND_V(p_tile_index != 0, TileSetSource::INVALID_ATLAS_COORDS);
  4086. return Vector2i();
  4087. }
  4088. bool TileSetScenesCollectionSource::has_tile(Vector2i p_atlas_coords) const {
  4089. return p_atlas_coords == Vector2i();
  4090. }
  4091. int TileSetScenesCollectionSource::get_alternative_tiles_count(const Vector2i p_atlas_coords) const {
  4092. return scenes_ids.size();
  4093. }
  4094. int TileSetScenesCollectionSource::get_alternative_tile_id(const Vector2i p_atlas_coords, int p_index) const {
  4095. ERR_FAIL_COND_V(p_atlas_coords != Vector2i(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  4096. ERR_FAIL_INDEX_V(p_index, scenes_ids.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  4097. return scenes_ids[p_index];
  4098. }
  4099. bool TileSetScenesCollectionSource::has_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  4100. ERR_FAIL_COND_V(p_atlas_coords != Vector2i(), false);
  4101. return scenes.has(p_alternative_tile);
  4102. }
  4103. int TileSetScenesCollectionSource::create_scene_tile(Ref<PackedScene> p_packed_scene, int p_id_override) {
  4104. 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));
  4105. int new_scene_id = p_id_override >= 0 ? p_id_override : next_scene_id;
  4106. scenes[new_scene_id] = SceneData();
  4107. scenes_ids.push_back(new_scene_id);
  4108. scenes_ids.sort();
  4109. set_scene_tile_scene(new_scene_id, p_packed_scene);
  4110. _compute_next_alternative_id();
  4111. emit_signal(SNAME("changed"));
  4112. return new_scene_id;
  4113. }
  4114. void TileSetScenesCollectionSource::set_scene_tile_id(int p_id, int p_new_id) {
  4115. ERR_FAIL_COND(p_new_id < 0);
  4116. ERR_FAIL_COND(!has_scene_tile_id(p_id));
  4117. ERR_FAIL_COND(has_scene_tile_id(p_new_id));
  4118. scenes[p_new_id] = SceneData();
  4119. scenes[p_new_id] = scenes[p_id];
  4120. scenes_ids.push_back(p_new_id);
  4121. scenes_ids.sort();
  4122. _compute_next_alternative_id();
  4123. scenes.erase(p_id);
  4124. scenes_ids.erase(p_id);
  4125. emit_signal(SNAME("changed"));
  4126. }
  4127. void TileSetScenesCollectionSource::set_scene_tile_scene(int p_id, Ref<PackedScene> p_packed_scene) {
  4128. ERR_FAIL_COND(!scenes.has(p_id));
  4129. if (p_packed_scene.is_valid()) {
  4130. // Check if it extends CanvasItem.
  4131. Ref<SceneState> scene_state = p_packed_scene->get_state();
  4132. String type;
  4133. while (scene_state.is_valid() && type.is_empty()) {
  4134. // Make sure we have a root node. Supposed to be at 0 index because find_node_by_path() does not seem to work.
  4135. ERR_FAIL_COND(scene_state->get_node_count() < 1);
  4136. type = scene_state->get_node_type(0);
  4137. scene_state = scene_state->get_base_scene_state();
  4138. }
  4139. 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()));
  4140. bool extends_correct_class = ClassDB::is_parent_class(type, "Control") || ClassDB::is_parent_class(type, "Node2D");
  4141. ERR_FAIL_COND_MSG(!extends_correct_class, vformat("Invalid PackedScene for TileSetScenesCollectionSource: %s. Root node should extend Control or Node2D. Found %s instead.", p_packed_scene->get_path(), type));
  4142. scenes[p_id].scene = p_packed_scene;
  4143. } else {
  4144. scenes[p_id].scene = Ref<PackedScene>();
  4145. }
  4146. emit_signal(SNAME("changed"));
  4147. }
  4148. Ref<PackedScene> TileSetScenesCollectionSource::get_scene_tile_scene(int p_id) const {
  4149. ERR_FAIL_COND_V(!scenes.has(p_id), Ref<PackedScene>());
  4150. return scenes[p_id].scene;
  4151. }
  4152. void TileSetScenesCollectionSource::set_scene_tile_display_placeholder(int p_id, bool p_display_placeholder) {
  4153. ERR_FAIL_COND(!scenes.has(p_id));
  4154. scenes[p_id].display_placeholder = p_display_placeholder;
  4155. emit_signal(SNAME("changed"));
  4156. }
  4157. bool TileSetScenesCollectionSource::get_scene_tile_display_placeholder(int p_id) const {
  4158. ERR_FAIL_COND_V(!scenes.has(p_id), false);
  4159. return scenes[p_id].display_placeholder;
  4160. }
  4161. void TileSetScenesCollectionSource::remove_scene_tile(int p_id) {
  4162. ERR_FAIL_COND(!scenes.has(p_id));
  4163. scenes.erase(p_id);
  4164. scenes_ids.erase(p_id);
  4165. emit_signal(SNAME("changed"));
  4166. }
  4167. int TileSetScenesCollectionSource::get_next_scene_tile_id() const {
  4168. return next_scene_id;
  4169. }
  4170. bool TileSetScenesCollectionSource::_set(const StringName &p_name, const Variant &p_value) {
  4171. Vector<String> components = String(p_name).split("/", true, 2);
  4172. if (components.size() >= 2 && components[0] == "scenes" && components[1].is_valid_int()) {
  4173. int scene_id = components[1].to_int();
  4174. if (components.size() >= 3 && components[2] == "scene") {
  4175. if (has_scene_tile_id(scene_id)) {
  4176. set_scene_tile_scene(scene_id, p_value);
  4177. } else {
  4178. create_scene_tile(p_value, scene_id);
  4179. }
  4180. return true;
  4181. } else if (components.size() >= 3 && components[2] == "display_placeholder") {
  4182. if (!has_scene_tile_id(scene_id)) {
  4183. create_scene_tile(p_value, scene_id);
  4184. }
  4185. return true;
  4186. }
  4187. }
  4188. return false;
  4189. }
  4190. bool TileSetScenesCollectionSource::_get(const StringName &p_name, Variant &r_ret) const {
  4191. Vector<String> components = String(p_name).split("/", true, 2);
  4192. if (components.size() >= 2 && components[0] == "scenes" && components[1].is_valid_int() && scenes.has(components[1].to_int())) {
  4193. if (components.size() >= 3 && components[2] == "scene") {
  4194. r_ret = scenes[components[1].to_int()].scene;
  4195. return true;
  4196. } else if (components.size() >= 3 && components[2] == "display_placeholder") {
  4197. r_ret = scenes[components[1].to_int()].scene;
  4198. return true;
  4199. }
  4200. }
  4201. return false;
  4202. }
  4203. void TileSetScenesCollectionSource::_get_property_list(List<PropertyInfo> *p_list) const {
  4204. for (int i = 0; i < scenes_ids.size(); i++) {
  4205. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("scenes/%d/scene", scenes_ids[i]), PROPERTY_HINT_RESOURCE_TYPE, "TileSetScenesCollectionSource"));
  4206. PropertyInfo property_info = PropertyInfo(Variant::BOOL, vformat("scenes/%d/display_placeholder", scenes_ids[i]));
  4207. if (scenes[scenes_ids[i]].display_placeholder == false) {
  4208. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4209. }
  4210. p_list->push_back(property_info);
  4211. }
  4212. }
  4213. void TileSetScenesCollectionSource::_bind_methods() {
  4214. ClassDB::bind_method(D_METHOD("get_scene_tiles_count"), &TileSetScenesCollectionSource::get_scene_tiles_count);
  4215. ClassDB::bind_method(D_METHOD("get_scene_tile_id", "index"), &TileSetScenesCollectionSource::get_scene_tile_id);
  4216. ClassDB::bind_method(D_METHOD("has_scene_tile_id", "id"), &TileSetScenesCollectionSource::has_scene_tile_id);
  4217. ClassDB::bind_method(D_METHOD("create_scene_tile", "packed_scene", "id_override"), &TileSetScenesCollectionSource::create_scene_tile, DEFVAL(INVALID_TILE_ALTERNATIVE));
  4218. ClassDB::bind_method(D_METHOD("set_scene_tile_id", "id", "new_id"), &TileSetScenesCollectionSource::set_scene_tile_id);
  4219. ClassDB::bind_method(D_METHOD("set_scene_tile_scene", "id", "packed_scene"), &TileSetScenesCollectionSource::set_scene_tile_scene);
  4220. ClassDB::bind_method(D_METHOD("get_scene_tile_scene", "id"), &TileSetScenesCollectionSource::get_scene_tile_scene);
  4221. ClassDB::bind_method(D_METHOD("set_scene_tile_display_placeholder", "id", "display_placeholder"), &TileSetScenesCollectionSource::set_scene_tile_display_placeholder);
  4222. ClassDB::bind_method(D_METHOD("get_scene_tile_display_placeholder", "id"), &TileSetScenesCollectionSource::get_scene_tile_display_placeholder);
  4223. ClassDB::bind_method(D_METHOD("remove_scene_tile", "id"), &TileSetScenesCollectionSource::remove_scene_tile);
  4224. ClassDB::bind_method(D_METHOD("get_next_scene_tile_id"), &TileSetScenesCollectionSource::get_next_scene_tile_id);
  4225. }
  4226. /////////////////////////////// TileData //////////////////////////////////////
  4227. void TileData::set_tile_set(const TileSet *p_tile_set) {
  4228. tile_set = p_tile_set;
  4229. notify_tile_data_properties_should_change();
  4230. }
  4231. void TileData::notify_tile_data_properties_should_change() {
  4232. if (!tile_set) {
  4233. return;
  4234. }
  4235. occluders.resize(tile_set->get_occlusion_layers_count());
  4236. physics.resize(tile_set->get_physics_layers_count());
  4237. for (int bit_index = 0; bit_index < 16; bit_index++) {
  4238. if (terrain_set < 0 || terrain_peering_bits[bit_index] >= tile_set->get_terrains_count(terrain_set)) {
  4239. terrain_peering_bits[bit_index] = -1;
  4240. }
  4241. }
  4242. navigation.resize(tile_set->get_navigation_layers_count());
  4243. // Convert custom data to the new type.
  4244. custom_data.resize(tile_set->get_custom_data_layers_count());
  4245. for (int i = 0; i < custom_data.size(); i++) {
  4246. if (custom_data[i].get_type() != tile_set->get_custom_data_layer_type(i)) {
  4247. Variant new_val;
  4248. Callable::CallError error;
  4249. if (Variant::can_convert(custom_data[i].get_type(), tile_set->get_custom_data_layer_type(i))) {
  4250. const Variant *args[] = { &custom_data[i] };
  4251. Variant::construct(tile_set->get_custom_data_layer_type(i), new_val, args, 1, error);
  4252. } else {
  4253. Variant::construct(tile_set->get_custom_data_layer_type(i), new_val, nullptr, 0, error);
  4254. }
  4255. custom_data.write[i] = new_val;
  4256. }
  4257. }
  4258. notify_property_list_changed();
  4259. emit_signal(SNAME("changed"));
  4260. }
  4261. void TileData::add_occlusion_layer(int p_to_pos) {
  4262. if (p_to_pos < 0) {
  4263. p_to_pos = occluders.size();
  4264. }
  4265. ERR_FAIL_INDEX(p_to_pos, occluders.size() + 1);
  4266. occluders.insert(p_to_pos, Ref<OccluderPolygon2D>());
  4267. }
  4268. void TileData::move_occlusion_layer(int p_from_index, int p_to_pos) {
  4269. ERR_FAIL_INDEX(p_from_index, occluders.size());
  4270. ERR_FAIL_INDEX(p_to_pos, occluders.size() + 1);
  4271. occluders.insert(p_to_pos, occluders[p_from_index]);
  4272. occluders.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4273. }
  4274. void TileData::remove_occlusion_layer(int p_index) {
  4275. ERR_FAIL_INDEX(p_index, occluders.size());
  4276. occluders.remove_at(p_index);
  4277. }
  4278. void TileData::add_physics_layer(int p_to_pos) {
  4279. if (p_to_pos < 0) {
  4280. p_to_pos = physics.size();
  4281. }
  4282. ERR_FAIL_INDEX(p_to_pos, physics.size() + 1);
  4283. physics.insert(p_to_pos, PhysicsLayerTileData());
  4284. }
  4285. void TileData::move_physics_layer(int p_from_index, int p_to_pos) {
  4286. ERR_FAIL_INDEX(p_from_index, physics.size());
  4287. ERR_FAIL_INDEX(p_to_pos, physics.size() + 1);
  4288. physics.insert(p_to_pos, physics[p_from_index]);
  4289. physics.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4290. }
  4291. void TileData::remove_physics_layer(int p_index) {
  4292. ERR_FAIL_INDEX(p_index, physics.size());
  4293. physics.remove_at(p_index);
  4294. }
  4295. void TileData::add_terrain_set(int p_to_pos) {
  4296. if (p_to_pos >= 0 && p_to_pos <= terrain_set) {
  4297. terrain_set += 1;
  4298. }
  4299. }
  4300. void TileData::move_terrain_set(int p_from_index, int p_to_pos) {
  4301. if (p_from_index == terrain_set) {
  4302. terrain_set = (p_from_index < p_to_pos) ? p_to_pos - 1 : p_to_pos;
  4303. } else {
  4304. if (p_from_index < terrain_set) {
  4305. terrain_set -= 1;
  4306. }
  4307. if (p_to_pos <= terrain_set) {
  4308. terrain_set += 1;
  4309. }
  4310. }
  4311. }
  4312. void TileData::remove_terrain_set(int p_index) {
  4313. if (p_index == terrain_set) {
  4314. terrain_set = -1;
  4315. for (int i = 0; i < 16; i++) {
  4316. terrain_peering_bits[i] = -1;
  4317. }
  4318. } else if (terrain_set > p_index) {
  4319. terrain_set -= 1;
  4320. }
  4321. }
  4322. void TileData::add_terrain(int p_terrain_set, int p_to_pos) {
  4323. if (terrain_set == p_terrain_set) {
  4324. for (int i = 0; i < 16; i++) {
  4325. if (p_to_pos >= 0 && p_to_pos <= terrain_peering_bits[i]) {
  4326. terrain_peering_bits[i] += 1;
  4327. }
  4328. }
  4329. }
  4330. }
  4331. void TileData::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  4332. if (terrain_set == p_terrain_set) {
  4333. for (int i = 0; i < 16; i++) {
  4334. if (p_from_index == terrain_peering_bits[i]) {
  4335. terrain_peering_bits[i] = (p_from_index < p_to_pos) ? p_to_pos - 1 : p_to_pos;
  4336. } else {
  4337. if (p_from_index < terrain_peering_bits[i]) {
  4338. terrain_peering_bits[i] -= 1;
  4339. }
  4340. if (p_to_pos <= terrain_peering_bits[i]) {
  4341. terrain_peering_bits[i] += 1;
  4342. }
  4343. }
  4344. }
  4345. }
  4346. }
  4347. void TileData::remove_terrain(int p_terrain_set, int p_index) {
  4348. if (terrain_set == p_terrain_set) {
  4349. if (terrain == p_index) {
  4350. terrain = -1;
  4351. }
  4352. for (int i = 0; i < 16; i++) {
  4353. if (terrain_peering_bits[i] == p_index) {
  4354. terrain_peering_bits[i] = -1;
  4355. } else if (terrain_peering_bits[i] > p_index) {
  4356. terrain_peering_bits[i] -= 1;
  4357. }
  4358. }
  4359. }
  4360. }
  4361. void TileData::add_navigation_layer(int p_to_pos) {
  4362. if (p_to_pos < 0) {
  4363. p_to_pos = navigation.size();
  4364. }
  4365. ERR_FAIL_INDEX(p_to_pos, navigation.size() + 1);
  4366. navigation.insert(p_to_pos, Ref<NavigationPolygon>());
  4367. }
  4368. void TileData::move_navigation_layer(int p_from_index, int p_to_pos) {
  4369. ERR_FAIL_INDEX(p_from_index, navigation.size());
  4370. ERR_FAIL_INDEX(p_to_pos, navigation.size() + 1);
  4371. navigation.insert(p_to_pos, navigation[p_from_index]);
  4372. navigation.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4373. }
  4374. void TileData::remove_navigation_layer(int p_index) {
  4375. ERR_FAIL_INDEX(p_index, navigation.size());
  4376. navigation.remove_at(p_index);
  4377. }
  4378. void TileData::add_custom_data_layer(int p_to_pos) {
  4379. if (p_to_pos < 0) {
  4380. p_to_pos = custom_data.size();
  4381. }
  4382. ERR_FAIL_INDEX(p_to_pos, custom_data.size() + 1);
  4383. custom_data.insert(p_to_pos, Variant());
  4384. }
  4385. void TileData::move_custom_data_layer(int p_from_index, int p_to_pos) {
  4386. ERR_FAIL_INDEX(p_from_index, custom_data.size());
  4387. ERR_FAIL_INDEX(p_to_pos, custom_data.size() + 1);
  4388. custom_data.insert(p_to_pos, custom_data[p_from_index]);
  4389. custom_data.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4390. }
  4391. void TileData::remove_custom_data_layer(int p_index) {
  4392. ERR_FAIL_INDEX(p_index, custom_data.size());
  4393. custom_data.remove_at(p_index);
  4394. }
  4395. void TileData::set_allow_transform(bool p_allow_transform) {
  4396. allow_transform = p_allow_transform;
  4397. }
  4398. bool TileData::is_allowing_transform() const {
  4399. return allow_transform;
  4400. }
  4401. TileData *TileData::duplicate() {
  4402. TileData *output = memnew(TileData);
  4403. output->tile_set = tile_set;
  4404. output->allow_transform = allow_transform;
  4405. // Rendering
  4406. output->flip_h = flip_h;
  4407. output->flip_v = flip_v;
  4408. output->transpose = transpose;
  4409. output->texture_origin = texture_origin;
  4410. output->material = material;
  4411. output->modulate = modulate;
  4412. output->z_index = z_index;
  4413. output->y_sort_origin = y_sort_origin;
  4414. output->occluders = occluders;
  4415. // Physics
  4416. output->physics = physics;
  4417. // Terrain
  4418. output->terrain_set = -1;
  4419. memcpy(output->terrain_peering_bits, terrain_peering_bits, 16 * sizeof(int));
  4420. // Navigation
  4421. output->navigation = navigation;
  4422. // Misc
  4423. output->probability = probability;
  4424. // Custom data
  4425. output->custom_data = custom_data;
  4426. return output;
  4427. }
  4428. // Rendering
  4429. void TileData::set_flip_h(bool p_flip_h) {
  4430. ERR_FAIL_COND_MSG(!allow_transform && p_flip_h, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  4431. flip_h = p_flip_h;
  4432. emit_signal(SNAME("changed"));
  4433. }
  4434. bool TileData::get_flip_h() const {
  4435. return flip_h;
  4436. }
  4437. void TileData::set_flip_v(bool p_flip_v) {
  4438. ERR_FAIL_COND_MSG(!allow_transform && p_flip_v, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  4439. flip_v = p_flip_v;
  4440. emit_signal(SNAME("changed"));
  4441. }
  4442. bool TileData::get_flip_v() const {
  4443. return flip_v;
  4444. }
  4445. void TileData::set_transpose(bool p_transpose) {
  4446. ERR_FAIL_COND_MSG(!allow_transform && p_transpose, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  4447. transpose = p_transpose;
  4448. emit_signal(SNAME("changed"));
  4449. }
  4450. bool TileData::get_transpose() const {
  4451. return transpose;
  4452. }
  4453. void TileData::set_texture_origin(Vector2i p_texture_origin) {
  4454. texture_origin = p_texture_origin;
  4455. emit_signal(SNAME("changed"));
  4456. }
  4457. Vector2i TileData::get_texture_origin() const {
  4458. return texture_origin;
  4459. }
  4460. void TileData::set_material(Ref<Material> p_material) {
  4461. material = p_material;
  4462. emit_signal(SNAME("changed"));
  4463. }
  4464. Ref<Material> TileData::get_material() const {
  4465. return material;
  4466. }
  4467. void TileData::set_modulate(Color p_modulate) {
  4468. modulate = p_modulate;
  4469. emit_signal(SNAME("changed"));
  4470. }
  4471. Color TileData::get_modulate() const {
  4472. return modulate;
  4473. }
  4474. void TileData::set_z_index(int p_z_index) {
  4475. z_index = p_z_index;
  4476. emit_signal(SNAME("changed"));
  4477. }
  4478. int TileData::get_z_index() const {
  4479. return z_index;
  4480. }
  4481. void TileData::set_y_sort_origin(int p_y_sort_origin) {
  4482. y_sort_origin = p_y_sort_origin;
  4483. emit_signal(SNAME("changed"));
  4484. }
  4485. int TileData::get_y_sort_origin() const {
  4486. return y_sort_origin;
  4487. }
  4488. void TileData::set_occluder(int p_layer_id, Ref<OccluderPolygon2D> p_occluder_polygon) {
  4489. ERR_FAIL_INDEX(p_layer_id, occluders.size());
  4490. occluders.write[p_layer_id] = p_occluder_polygon;
  4491. emit_signal(SNAME("changed"));
  4492. }
  4493. Ref<OccluderPolygon2D> TileData::get_occluder(int p_layer_id) const {
  4494. ERR_FAIL_INDEX_V(p_layer_id, occluders.size(), Ref<OccluderPolygon2D>());
  4495. return occluders[p_layer_id];
  4496. }
  4497. // Physics
  4498. void TileData::set_constant_linear_velocity(int p_layer_id, const Vector2 &p_velocity) {
  4499. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4500. physics.write[p_layer_id].linear_velocity = p_velocity;
  4501. emit_signal(SNAME("changed"));
  4502. }
  4503. Vector2 TileData::get_constant_linear_velocity(int p_layer_id) const {
  4504. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Vector2());
  4505. return physics[p_layer_id].linear_velocity;
  4506. }
  4507. void TileData::set_constant_angular_velocity(int p_layer_id, real_t p_velocity) {
  4508. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4509. physics.write[p_layer_id].angular_velocity = p_velocity;
  4510. emit_signal(SNAME("changed"));
  4511. }
  4512. real_t TileData::get_constant_angular_velocity(int p_layer_id) const {
  4513. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0.0);
  4514. return physics[p_layer_id].angular_velocity;
  4515. }
  4516. void TileData::set_collision_polygons_count(int p_layer_id, int p_polygons_count) {
  4517. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4518. ERR_FAIL_COND(p_polygons_count < 0);
  4519. if (p_polygons_count == physics.write[p_layer_id].polygons.size()) {
  4520. return;
  4521. }
  4522. physics.write[p_layer_id].polygons.resize(p_polygons_count);
  4523. notify_property_list_changed();
  4524. emit_signal(SNAME("changed"));
  4525. }
  4526. int TileData::get_collision_polygons_count(int p_layer_id) const {
  4527. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0);
  4528. return physics[p_layer_id].polygons.size();
  4529. }
  4530. void TileData::add_collision_polygon(int p_layer_id) {
  4531. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4532. physics.write[p_layer_id].polygons.push_back(PhysicsLayerTileData::PolygonShapeTileData());
  4533. emit_signal(SNAME("changed"));
  4534. }
  4535. void TileData::remove_collision_polygon(int p_layer_id, int p_polygon_index) {
  4536. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4537. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4538. physics.write[p_layer_id].polygons.remove_at(p_polygon_index);
  4539. emit_signal(SNAME("changed"));
  4540. }
  4541. void TileData::set_collision_polygon_points(int p_layer_id, int p_polygon_index, Vector<Vector2> p_polygon) {
  4542. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4543. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4544. ERR_FAIL_COND_MSG(p_polygon.size() != 0 && p_polygon.size() < 3, "Invalid polygon. Needs either 0 or more than 3 points.");
  4545. if (p_polygon.is_empty()) {
  4546. physics.write[p_layer_id].polygons.write[p_polygon_index].shapes.clear();
  4547. } else {
  4548. // Decompose into convex shapes.
  4549. Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(p_polygon);
  4550. ERR_FAIL_COND_MSG(decomp.is_empty(), "Could not decompose the polygon into convex shapes.");
  4551. physics.write[p_layer_id].polygons.write[p_polygon_index].shapes.resize(decomp.size());
  4552. for (int i = 0; i < decomp.size(); i++) {
  4553. Ref<ConvexPolygonShape2D> shape;
  4554. shape.instantiate();
  4555. shape->set_points(decomp[i]);
  4556. physics.write[p_layer_id].polygons.write[p_polygon_index].shapes[i] = shape;
  4557. }
  4558. }
  4559. physics.write[p_layer_id].polygons.write[p_polygon_index].polygon = p_polygon;
  4560. emit_signal(SNAME("changed"));
  4561. }
  4562. Vector<Vector2> TileData::get_collision_polygon_points(int p_layer_id, int p_polygon_index) const {
  4563. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Vector<Vector2>());
  4564. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), Vector<Vector2>());
  4565. return physics[p_layer_id].polygons[p_polygon_index].polygon;
  4566. }
  4567. void TileData::set_collision_polygon_one_way(int p_layer_id, int p_polygon_index, bool p_one_way) {
  4568. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4569. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4570. physics.write[p_layer_id].polygons.write[p_polygon_index].one_way = p_one_way;
  4571. emit_signal(SNAME("changed"));
  4572. }
  4573. bool TileData::is_collision_polygon_one_way(int p_layer_id, int p_polygon_index) const {
  4574. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), false);
  4575. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), false);
  4576. return physics[p_layer_id].polygons[p_polygon_index].one_way;
  4577. }
  4578. void TileData::set_collision_polygon_one_way_margin(int p_layer_id, int p_polygon_index, float p_one_way_margin) {
  4579. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4580. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4581. physics.write[p_layer_id].polygons.write[p_polygon_index].one_way_margin = p_one_way_margin;
  4582. emit_signal(SNAME("changed"));
  4583. }
  4584. float TileData::get_collision_polygon_one_way_margin(int p_layer_id, int p_polygon_index) const {
  4585. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0.0);
  4586. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), 0.0);
  4587. return physics[p_layer_id].polygons[p_polygon_index].one_way_margin;
  4588. }
  4589. int TileData::get_collision_polygon_shapes_count(int p_layer_id, int p_polygon_index) const {
  4590. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0);
  4591. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), 0);
  4592. return physics[p_layer_id].polygons[p_polygon_index].shapes.size();
  4593. }
  4594. Ref<ConvexPolygonShape2D> TileData::get_collision_polygon_shape(int p_layer_id, int p_polygon_index, int shape_index) const {
  4595. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Ref<ConvexPolygonShape2D>());
  4596. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), Ref<ConvexPolygonShape2D>());
  4597. ERR_FAIL_INDEX_V(shape_index, (int)physics[p_layer_id].polygons[p_polygon_index].shapes.size(), Ref<ConvexPolygonShape2D>());
  4598. return physics[p_layer_id].polygons[p_polygon_index].shapes[shape_index];
  4599. }
  4600. // Terrain
  4601. void TileData::set_terrain_set(int p_terrain_set) {
  4602. ERR_FAIL_COND(p_terrain_set < -1);
  4603. if (p_terrain_set == terrain_set) {
  4604. return;
  4605. }
  4606. if (tile_set) {
  4607. ERR_FAIL_COND(p_terrain_set >= tile_set->get_terrain_sets_count());
  4608. terrain = -1;
  4609. for (int i = 0; i < 16; i++) {
  4610. terrain_peering_bits[i] = -1;
  4611. }
  4612. }
  4613. terrain_set = p_terrain_set;
  4614. notify_property_list_changed();
  4615. emit_signal(SNAME("changed"));
  4616. }
  4617. int TileData::get_terrain_set() const {
  4618. return terrain_set;
  4619. }
  4620. void TileData::set_terrain(int p_terrain) {
  4621. ERR_FAIL_COND(terrain_set < 0);
  4622. ERR_FAIL_COND(p_terrain < -1);
  4623. if (tile_set) {
  4624. ERR_FAIL_COND(p_terrain >= tile_set->get_terrains_count(terrain_set));
  4625. }
  4626. terrain = p_terrain;
  4627. emit_signal(SNAME("changed"));
  4628. }
  4629. int TileData::get_terrain() const {
  4630. return terrain;
  4631. }
  4632. void TileData::set_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit, int p_terrain_index) {
  4633. ERR_FAIL_INDEX(p_peering_bit, TileSet::CellNeighbor::CELL_NEIGHBOR_MAX);
  4634. ERR_FAIL_COND(terrain_set < 0);
  4635. ERR_FAIL_COND(p_terrain_index < -1);
  4636. if (tile_set) {
  4637. ERR_FAIL_COND(p_terrain_index >= tile_set->get_terrains_count(terrain_set));
  4638. ERR_FAIL_COND(!is_valid_terrain_peering_bit(p_peering_bit));
  4639. }
  4640. terrain_peering_bits[p_peering_bit] = p_terrain_index;
  4641. emit_signal(SNAME("changed"));
  4642. }
  4643. int TileData::get_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit) const {
  4644. ERR_FAIL_COND_V(!is_valid_terrain_peering_bit(p_peering_bit), -1);
  4645. return terrain_peering_bits[p_peering_bit];
  4646. }
  4647. bool TileData::is_valid_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit) const {
  4648. ERR_FAIL_COND_V(!tile_set, false);
  4649. return tile_set->is_valid_terrain_peering_bit(terrain_set, p_peering_bit);
  4650. }
  4651. TileSet::TerrainsPattern TileData::get_terrains_pattern() const {
  4652. ERR_FAIL_COND_V(!tile_set, TileSet::TerrainsPattern());
  4653. TileSet::TerrainsPattern output(tile_set, terrain_set);
  4654. output.set_terrain(terrain);
  4655. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4656. if (tile_set->is_valid_terrain_peering_bit(terrain_set, TileSet::CellNeighbor(i))) {
  4657. output.set_terrain_peering_bit(TileSet::CellNeighbor(i), get_terrain_peering_bit(TileSet::CellNeighbor(i)));
  4658. }
  4659. }
  4660. return output;
  4661. }
  4662. // Navigation
  4663. void TileData::set_navigation_polygon(int p_layer_id, Ref<NavigationPolygon> p_navigation_polygon) {
  4664. ERR_FAIL_INDEX(p_layer_id, navigation.size());
  4665. navigation.write[p_layer_id] = p_navigation_polygon;
  4666. emit_signal(SNAME("changed"));
  4667. }
  4668. Ref<NavigationPolygon> TileData::get_navigation_polygon(int p_layer_id) const {
  4669. ERR_FAIL_INDEX_V(p_layer_id, navigation.size(), Ref<NavigationPolygon>());
  4670. return navigation[p_layer_id];
  4671. }
  4672. // Misc
  4673. void TileData::set_probability(float p_probability) {
  4674. ERR_FAIL_COND(p_probability < 0.0);
  4675. probability = p_probability;
  4676. emit_signal(SNAME("changed"));
  4677. }
  4678. float TileData::get_probability() const {
  4679. return probability;
  4680. }
  4681. // Custom data
  4682. void TileData::set_custom_data(String p_layer_name, Variant p_value) {
  4683. ERR_FAIL_COND(!tile_set);
  4684. int p_layer_id = tile_set->get_custom_data_layer_by_name(p_layer_name);
  4685. ERR_FAIL_COND_MSG(p_layer_id < 0, vformat("TileSet has no layer with name: %s", p_layer_name));
  4686. set_custom_data_by_layer_id(p_layer_id, p_value);
  4687. }
  4688. Variant TileData::get_custom_data(String p_layer_name) const {
  4689. ERR_FAIL_COND_V(!tile_set, Variant());
  4690. int p_layer_id = tile_set->get_custom_data_layer_by_name(p_layer_name);
  4691. ERR_FAIL_COND_V_MSG(p_layer_id < 0, Variant(), vformat("TileSet has no layer with name: %s", p_layer_name));
  4692. return get_custom_data_by_layer_id(p_layer_id);
  4693. }
  4694. void TileData::set_custom_data_by_layer_id(int p_layer_id, Variant p_value) {
  4695. ERR_FAIL_INDEX(p_layer_id, custom_data.size());
  4696. custom_data.write[p_layer_id] = p_value;
  4697. emit_signal(SNAME("changed"));
  4698. }
  4699. Variant TileData::get_custom_data_by_layer_id(int p_layer_id) const {
  4700. ERR_FAIL_INDEX_V(p_layer_id, custom_data.size(), Variant());
  4701. return custom_data[p_layer_id];
  4702. }
  4703. bool TileData::_set(const StringName &p_name, const Variant &p_value) {
  4704. #ifndef DISABLE_DEPRECATED
  4705. if (p_name == "texture_offset") {
  4706. texture_origin = p_value;
  4707. return true;
  4708. }
  4709. #endif
  4710. Vector<String> components = String(p_name).split("/", true, 2);
  4711. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  4712. // Occlusion layers.
  4713. int layer_index = components[0].trim_prefix("occlusion_layer_").to_int();
  4714. ERR_FAIL_COND_V(layer_index < 0, false);
  4715. if (components[1] == "polygon") {
  4716. Ref<OccluderPolygon2D> polygon = p_value;
  4717. if (layer_index >= occluders.size()) {
  4718. if (tile_set) {
  4719. return false;
  4720. } else {
  4721. occluders.resize(layer_index + 1);
  4722. }
  4723. }
  4724. set_occluder(layer_index, polygon);
  4725. return true;
  4726. }
  4727. } else if (components.size() >= 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  4728. // Physics layers.
  4729. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  4730. ERR_FAIL_COND_V(layer_index < 0, false);
  4731. if (components.size() == 2) {
  4732. if (layer_index >= physics.size()) {
  4733. if (tile_set) {
  4734. return false;
  4735. } else {
  4736. physics.resize(layer_index + 1);
  4737. }
  4738. }
  4739. if (components[1] == "linear_velocity") {
  4740. set_constant_linear_velocity(layer_index, p_value);
  4741. return true;
  4742. } else if (components[1] == "angular_velocity") {
  4743. set_constant_angular_velocity(layer_index, p_value);
  4744. return true;
  4745. } else if (components[1] == "polygons_count") {
  4746. if (p_value.get_type() != Variant::INT) {
  4747. return false;
  4748. }
  4749. set_collision_polygons_count(layer_index, p_value);
  4750. return true;
  4751. }
  4752. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  4753. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  4754. ERR_FAIL_COND_V(polygon_index < 0, false);
  4755. if (components[2] == "points" || components[2] == "one_way" || components[2] == "one_way_margin") {
  4756. if (layer_index >= physics.size()) {
  4757. if (tile_set) {
  4758. return false;
  4759. } else {
  4760. physics.resize(layer_index + 1);
  4761. }
  4762. }
  4763. if (polygon_index >= physics[layer_index].polygons.size()) {
  4764. physics.write[layer_index].polygons.resize(polygon_index + 1);
  4765. }
  4766. }
  4767. if (components[2] == "points") {
  4768. Vector<Vector2> polygon = p_value;
  4769. set_collision_polygon_points(layer_index, polygon_index, polygon);
  4770. return true;
  4771. } else if (components[2] == "one_way") {
  4772. set_collision_polygon_one_way(layer_index, polygon_index, p_value);
  4773. return true;
  4774. } else if (components[2] == "one_way_margin") {
  4775. set_collision_polygon_one_way_margin(layer_index, polygon_index, p_value);
  4776. return true;
  4777. }
  4778. }
  4779. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  4780. // Navigation layers.
  4781. int layer_index = components[0].trim_prefix("navigation_layer_").to_int();
  4782. ERR_FAIL_COND_V(layer_index < 0, false);
  4783. if (components[1] == "polygon") {
  4784. Ref<NavigationPolygon> polygon = p_value;
  4785. if (layer_index >= navigation.size()) {
  4786. if (tile_set) {
  4787. return false;
  4788. } else {
  4789. navigation.resize(layer_index + 1);
  4790. }
  4791. }
  4792. set_navigation_polygon(layer_index, polygon);
  4793. return true;
  4794. }
  4795. } else if (components.size() == 2 && components[0] == "terrains_peering_bit") {
  4796. // Terrains.
  4797. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4798. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  4799. if (components[1] == TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]) {
  4800. set_terrain_peering_bit(bit, p_value);
  4801. return true;
  4802. }
  4803. }
  4804. return false;
  4805. } else if (components.size() == 1 && components[0].begins_with("custom_data_") && components[0].trim_prefix("custom_data_").is_valid_int()) {
  4806. // Custom data layers.
  4807. int layer_index = components[0].trim_prefix("custom_data_").to_int();
  4808. ERR_FAIL_COND_V(layer_index < 0, false);
  4809. if (layer_index >= custom_data.size()) {
  4810. if (tile_set) {
  4811. return false;
  4812. } else {
  4813. custom_data.resize(layer_index + 1);
  4814. }
  4815. }
  4816. set_custom_data_by_layer_id(layer_index, p_value);
  4817. return true;
  4818. }
  4819. return false;
  4820. }
  4821. bool TileData::_get(const StringName &p_name, Variant &r_ret) const {
  4822. #ifndef DISABLE_DEPRECATED
  4823. if (p_name == "texture_offset") {
  4824. r_ret = texture_origin;
  4825. return true;
  4826. }
  4827. #endif
  4828. Vector<String> components = String(p_name).split("/", true, 2);
  4829. if (tile_set) {
  4830. if (components.size() == 2 && components[0].begins_with("occlusion_layer") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  4831. // Occlusion layers.
  4832. int layer_index = components[0].trim_prefix("occlusion_layer_").to_int();
  4833. ERR_FAIL_COND_V(layer_index < 0, false);
  4834. if (layer_index >= occluders.size()) {
  4835. return false;
  4836. }
  4837. if (components[1] == "polygon") {
  4838. r_ret = get_occluder(layer_index);
  4839. return true;
  4840. }
  4841. } else if (components.size() >= 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  4842. // Physics layers.
  4843. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  4844. ERR_FAIL_COND_V(layer_index < 0, false);
  4845. if (layer_index >= physics.size()) {
  4846. return false;
  4847. }
  4848. if (components.size() == 2) {
  4849. if (components[1] == "linear_velocity") {
  4850. r_ret = get_constant_linear_velocity(layer_index);
  4851. return true;
  4852. } else if (components[1] == "angular_velocity") {
  4853. r_ret = get_constant_angular_velocity(layer_index);
  4854. return true;
  4855. } else if (components[1] == "polygons_count") {
  4856. r_ret = get_collision_polygons_count(layer_index);
  4857. return true;
  4858. }
  4859. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  4860. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  4861. ERR_FAIL_COND_V(polygon_index < 0, false);
  4862. if (polygon_index >= physics[layer_index].polygons.size()) {
  4863. return false;
  4864. }
  4865. if (components[2] == "points") {
  4866. r_ret = get_collision_polygon_points(layer_index, polygon_index);
  4867. return true;
  4868. } else if (components[2] == "one_way") {
  4869. r_ret = is_collision_polygon_one_way(layer_index, polygon_index);
  4870. return true;
  4871. } else if (components[2] == "one_way_margin") {
  4872. r_ret = get_collision_polygon_one_way_margin(layer_index, polygon_index);
  4873. return true;
  4874. }
  4875. }
  4876. } else if (components.size() == 2 && components[0] == "terrains_peering_bit") {
  4877. // Terrains.
  4878. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4879. if (components[1] == TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]) {
  4880. r_ret = terrain_peering_bits[i];
  4881. return true;
  4882. }
  4883. }
  4884. return false;
  4885. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  4886. // Occlusion layers.
  4887. int layer_index = components[0].trim_prefix("navigation_layer_").to_int();
  4888. ERR_FAIL_COND_V(layer_index < 0, false);
  4889. if (layer_index >= navigation.size()) {
  4890. return false;
  4891. }
  4892. if (components[1] == "polygon") {
  4893. r_ret = get_navigation_polygon(layer_index);
  4894. return true;
  4895. }
  4896. } else if (components.size() == 1 && components[0].begins_with("custom_data_") && components[0].trim_prefix("custom_data_").is_valid_int()) {
  4897. // Custom data layers.
  4898. int layer_index = components[0].trim_prefix("custom_data_").to_int();
  4899. ERR_FAIL_COND_V(layer_index < 0, false);
  4900. if (layer_index >= custom_data.size()) {
  4901. return false;
  4902. }
  4903. r_ret = get_custom_data_by_layer_id(layer_index);
  4904. return true;
  4905. }
  4906. }
  4907. return false;
  4908. }
  4909. void TileData::_get_property_list(List<PropertyInfo> *p_list) const {
  4910. PropertyInfo property_info;
  4911. // Add the groups manually.
  4912. if (tile_set) {
  4913. // Occlusion layers.
  4914. p_list->push_back(PropertyInfo(Variant::NIL, "Rendering", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4915. for (int i = 0; i < occluders.size(); i++) {
  4916. // occlusion_layer_%d/polygon
  4917. property_info = PropertyInfo(Variant::OBJECT, vformat("occlusion_layer_%d/polygon", i), PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D", PROPERTY_USAGE_DEFAULT);
  4918. if (!occluders[i].is_valid()) {
  4919. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4920. }
  4921. p_list->push_back(property_info);
  4922. }
  4923. // Physics layers.
  4924. p_list->push_back(PropertyInfo(Variant::NIL, "Physics", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4925. for (int i = 0; i < physics.size(); i++) {
  4926. p_list->push_back(PropertyInfo(Variant::VECTOR2, vformat("physics_layer_%d/linear_velocity", i), PROPERTY_HINT_NONE));
  4927. p_list->push_back(PropertyInfo(Variant::FLOAT, vformat("physics_layer_%d/angular_velocity", i), PROPERTY_HINT_NONE));
  4928. p_list->push_back(PropertyInfo(Variant::INT, vformat("physics_layer_%d/polygons_count", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  4929. for (int j = 0; j < physics[i].polygons.size(); j++) {
  4930. // physics_layer_%d/points
  4931. property_info = PropertyInfo(Variant::ARRAY, vformat("physics_layer_%d/polygon_%d/points", i, j), PROPERTY_HINT_ARRAY_TYPE, "Vector2", PROPERTY_USAGE_DEFAULT);
  4932. if (physics[i].polygons[j].polygon.is_empty()) {
  4933. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4934. }
  4935. p_list->push_back(property_info);
  4936. // physics_layer_%d/polygon_%d/one_way
  4937. property_info = PropertyInfo(Variant::BOOL, vformat("physics_layer_%d/polygon_%d/one_way", i, j));
  4938. if (physics[i].polygons[j].one_way == false) {
  4939. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4940. }
  4941. p_list->push_back(property_info);
  4942. // physics_layer_%d/polygon_%d/one_way_margin
  4943. property_info = PropertyInfo(Variant::FLOAT, vformat("physics_layer_%d/polygon_%d/one_way_margin", i, j));
  4944. if (physics[i].polygons[j].one_way_margin == 1.0) {
  4945. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4946. }
  4947. p_list->push_back(property_info);
  4948. }
  4949. }
  4950. // Terrain data
  4951. if (terrain_set >= 0) {
  4952. p_list->push_back(PropertyInfo(Variant::NIL, "Terrains", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4953. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4954. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  4955. if (is_valid_terrain_peering_bit(bit)) {
  4956. property_info = PropertyInfo(Variant::INT, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]));
  4957. if (get_terrain_peering_bit(bit) == -1) {
  4958. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4959. }
  4960. p_list->push_back(property_info);
  4961. }
  4962. }
  4963. }
  4964. // Navigation layers.
  4965. p_list->push_back(PropertyInfo(Variant::NIL, "Navigation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4966. for (int i = 0; i < navigation.size(); i++) {
  4967. property_info = PropertyInfo(Variant::OBJECT, vformat("navigation_layer_%d/polygon", i), PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon", PROPERTY_USAGE_DEFAULT);
  4968. if (!navigation[i].is_valid()) {
  4969. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4970. }
  4971. p_list->push_back(property_info);
  4972. }
  4973. // Custom data layers.
  4974. p_list->push_back(PropertyInfo(Variant::NIL, "Custom data", PROPERTY_HINT_NONE, "custom_data_", PROPERTY_USAGE_GROUP));
  4975. for (int i = 0; i < custom_data.size(); i++) {
  4976. Variant default_val;
  4977. Callable::CallError error;
  4978. Variant::construct(custom_data[i].get_type(), default_val, nullptr, 0, error);
  4979. 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);
  4980. if (custom_data[i] == default_val) {
  4981. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4982. }
  4983. p_list->push_back(property_info);
  4984. }
  4985. }
  4986. }
  4987. void TileData::_bind_methods() {
  4988. // Rendering.
  4989. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &TileData::set_flip_h);
  4990. ClassDB::bind_method(D_METHOD("get_flip_h"), &TileData::get_flip_h);
  4991. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &TileData::set_flip_v);
  4992. ClassDB::bind_method(D_METHOD("get_flip_v"), &TileData::get_flip_v);
  4993. ClassDB::bind_method(D_METHOD("set_transpose", "transpose"), &TileData::set_transpose);
  4994. ClassDB::bind_method(D_METHOD("get_transpose"), &TileData::get_transpose);
  4995. ClassDB::bind_method(D_METHOD("set_material", "material"), &TileData::set_material);
  4996. ClassDB::bind_method(D_METHOD("get_material"), &TileData::get_material);
  4997. ClassDB::bind_method(D_METHOD("set_texture_origin", "texture_origin"), &TileData::set_texture_origin);
  4998. ClassDB::bind_method(D_METHOD("get_texture_origin"), &TileData::get_texture_origin);
  4999. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &TileData::set_modulate);
  5000. ClassDB::bind_method(D_METHOD("get_modulate"), &TileData::get_modulate);
  5001. ClassDB::bind_method(D_METHOD("set_z_index", "z_index"), &TileData::set_z_index);
  5002. ClassDB::bind_method(D_METHOD("get_z_index"), &TileData::get_z_index);
  5003. ClassDB::bind_method(D_METHOD("set_y_sort_origin", "y_sort_origin"), &TileData::set_y_sort_origin);
  5004. ClassDB::bind_method(D_METHOD("get_y_sort_origin"), &TileData::get_y_sort_origin);
  5005. ClassDB::bind_method(D_METHOD("set_occluder", "layer_id", "occluder_polygon"), &TileData::set_occluder);
  5006. ClassDB::bind_method(D_METHOD("get_occluder", "layer_id"), &TileData::get_occluder);
  5007. // Physics.
  5008. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "layer_id", "velocity"), &TileData::set_constant_linear_velocity);
  5009. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity", "layer_id"), &TileData::get_constant_linear_velocity);
  5010. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "layer_id", "velocity"), &TileData::set_constant_angular_velocity);
  5011. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity", "layer_id"), &TileData::get_constant_angular_velocity);
  5012. ClassDB::bind_method(D_METHOD("set_collision_polygons_count", "layer_id", "polygons_count"), &TileData::set_collision_polygons_count);
  5013. ClassDB::bind_method(D_METHOD("get_collision_polygons_count", "layer_id"), &TileData::get_collision_polygons_count);
  5014. ClassDB::bind_method(D_METHOD("add_collision_polygon", "layer_id"), &TileData::add_collision_polygon);
  5015. ClassDB::bind_method(D_METHOD("remove_collision_polygon", "layer_id", "polygon_index"), &TileData::remove_collision_polygon);
  5016. ClassDB::bind_method(D_METHOD("set_collision_polygon_points", "layer_id", "polygon_index", "polygon"), &TileData::set_collision_polygon_points);
  5017. ClassDB::bind_method(D_METHOD("get_collision_polygon_points", "layer_id", "polygon_index"), &TileData::get_collision_polygon_points);
  5018. ClassDB::bind_method(D_METHOD("set_collision_polygon_one_way", "layer_id", "polygon_index", "one_way"), &TileData::set_collision_polygon_one_way);
  5019. ClassDB::bind_method(D_METHOD("is_collision_polygon_one_way", "layer_id", "polygon_index"), &TileData::is_collision_polygon_one_way);
  5020. 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);
  5021. ClassDB::bind_method(D_METHOD("get_collision_polygon_one_way_margin", "layer_id", "polygon_index"), &TileData::get_collision_polygon_one_way_margin);
  5022. // Terrain
  5023. ClassDB::bind_method(D_METHOD("set_terrain_set", "terrain_set"), &TileData::set_terrain_set);
  5024. ClassDB::bind_method(D_METHOD("get_terrain_set"), &TileData::get_terrain_set);
  5025. ClassDB::bind_method(D_METHOD("set_terrain", "terrain"), &TileData::set_terrain);
  5026. ClassDB::bind_method(D_METHOD("get_terrain"), &TileData::get_terrain);
  5027. ClassDB::bind_method(D_METHOD("set_terrain_peering_bit", "peering_bit", "terrain"), &TileData::set_terrain_peering_bit);
  5028. ClassDB::bind_method(D_METHOD("get_terrain_peering_bit", "peering_bit"), &TileData::get_terrain_peering_bit);
  5029. // Navigation
  5030. ClassDB::bind_method(D_METHOD("set_navigation_polygon", "layer_id", "navigation_polygon"), &TileData::set_navigation_polygon);
  5031. ClassDB::bind_method(D_METHOD("get_navigation_polygon", "layer_id"), &TileData::get_navigation_polygon);
  5032. // Misc.
  5033. ClassDB::bind_method(D_METHOD("set_probability", "probability"), &TileData::set_probability);
  5034. ClassDB::bind_method(D_METHOD("get_probability"), &TileData::get_probability);
  5035. // Custom data.
  5036. ClassDB::bind_method(D_METHOD("set_custom_data", "layer_name", "value"), &TileData::set_custom_data);
  5037. ClassDB::bind_method(D_METHOD("get_custom_data", "layer_name"), &TileData::get_custom_data);
  5038. ClassDB::bind_method(D_METHOD("set_custom_data_by_layer_id", "layer_id", "value"), &TileData::set_custom_data_by_layer_id);
  5039. ClassDB::bind_method(D_METHOD("get_custom_data_by_layer_id", "layer_id"), &TileData::get_custom_data_by_layer_id);
  5040. ADD_GROUP("Rendering", "");
  5041. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "get_flip_h");
  5042. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "get_flip_v");
  5043. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transpose"), "set_transpose", "get_transpose");
  5044. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "texture_origin", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_origin", "get_texture_origin");
  5045. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  5046. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemMaterial,ShaderMaterial"), "set_material", "get_material");
  5047. ADD_PROPERTY(PropertyInfo(Variant::INT, "z_index"), "set_z_index", "get_z_index");
  5048. ADD_PROPERTY(PropertyInfo(Variant::INT, "y_sort_origin"), "set_y_sort_origin", "get_y_sort_origin");
  5049. ADD_GROUP("Terrains", "");
  5050. ADD_PROPERTY(PropertyInfo(Variant::INT, "terrain_set"), "set_terrain_set", "get_terrain_set");
  5051. ADD_PROPERTY(PropertyInfo(Variant::INT, "terrain"), "set_terrain", "get_terrain");
  5052. ADD_GROUP("Miscellaneous", "");
  5053. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "probability"), "set_probability", "get_probability");
  5054. ADD_SIGNAL(MethodInfo("changed"));
  5055. }