tile_set.cpp 283 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170
  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 "tile_set.compat.inc"
  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/image_texture.h"
  38. #ifndef NAVIGATION_2D_DISABLED
  39. #include "servers/navigation_server_2d.h"
  40. #endif // NAVIGATION_2D_DISABLED
  41. /////////////////////////////// TileMapPattern //////////////////////////////////////
  42. void TileMapPattern::_set_tile_data(const Vector<int> &p_data) {
  43. int c = p_data.size();
  44. const int *r = p_data.ptr();
  45. int offset = 3;
  46. ERR_FAIL_COND_MSG(c % offset != 0, "Corrupted tile data.");
  47. clear();
  48. for (int i = 0; i < c; i += offset) {
  49. const uint8_t *ptr = (const uint8_t *)&r[i];
  50. uint8_t local[12];
  51. for (int j = 0; j < 12; j++) {
  52. local[j] = ptr[j];
  53. }
  54. #ifdef BIG_ENDIAN_ENABLED
  55. SWAP(local[0], local[3]);
  56. SWAP(local[1], local[2]);
  57. SWAP(local[4], local[7]);
  58. SWAP(local[5], local[6]);
  59. SWAP(local[8], local[11]);
  60. SWAP(local[9], local[10]);
  61. #endif
  62. int16_t x = decode_uint16(&local[0]);
  63. int16_t y = decode_uint16(&local[2]);
  64. uint16_t source_id = decode_uint16(&local[4]);
  65. uint16_t atlas_coords_x = decode_uint16(&local[6]);
  66. uint16_t atlas_coords_y = decode_uint16(&local[8]);
  67. uint16_t alternative_tile = decode_uint16(&local[10]);
  68. set_cell(Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile);
  69. }
  70. emit_signal(CoreStringName(changed));
  71. }
  72. Vector<int> TileMapPattern::_get_tile_data() const {
  73. // Export tile data to raw format
  74. Vector<int> data;
  75. data.resize(pattern.size() * 3);
  76. int *w = data.ptrw();
  77. // Save in highest format
  78. int idx = 0;
  79. for (const KeyValue<Vector2i, TileMapCell> &E : pattern) {
  80. uint8_t *ptr = (uint8_t *)&w[idx];
  81. encode_uint16((int16_t)(E.key.x), &ptr[0]);
  82. encode_uint16((int16_t)(E.key.y), &ptr[2]);
  83. encode_uint16(E.value.source_id, &ptr[4]);
  84. encode_uint16(E.value.coord_x, &ptr[6]);
  85. encode_uint16(E.value.coord_y, &ptr[8]);
  86. encode_uint16(E.value.alternative_tile, &ptr[10]);
  87. idx += 3;
  88. }
  89. return data;
  90. }
  91. void TileMapPattern::set_cell(const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) {
  92. 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));
  93. size = size.max(p_coords + Vector2i(1, 1));
  94. pattern[p_coords] = TileMapCell(p_source_id, p_atlas_coords, p_alternative_tile);
  95. emit_changed();
  96. }
  97. bool TileMapPattern::has_cell(const Vector2i &p_coords) const {
  98. return pattern.has(p_coords);
  99. }
  100. void TileMapPattern::remove_cell(const Vector2i &p_coords, bool p_update_size) {
  101. ERR_FAIL_COND(!pattern.has(p_coords));
  102. pattern.erase(p_coords);
  103. if (p_update_size) {
  104. size = Size2i();
  105. for (const KeyValue<Vector2i, TileMapCell> &E : pattern) {
  106. size = size.max(E.key + Vector2i(1, 1));
  107. }
  108. }
  109. emit_changed();
  110. }
  111. int TileMapPattern::get_cell_source_id(const Vector2i &p_coords) const {
  112. ERR_FAIL_COND_V(!pattern.has(p_coords), TileSet::INVALID_SOURCE);
  113. return pattern[p_coords].source_id;
  114. }
  115. Vector2i TileMapPattern::get_cell_atlas_coords(const Vector2i &p_coords) const {
  116. ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetSource::INVALID_ATLAS_COORDS);
  117. return pattern[p_coords].get_atlas_coords();
  118. }
  119. int TileMapPattern::get_cell_alternative_tile(const Vector2i &p_coords) const {
  120. ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetSource::INVALID_TILE_ALTERNATIVE);
  121. return pattern[p_coords].alternative_tile;
  122. }
  123. TypedArray<Vector2i> TileMapPattern::get_used_cells() const {
  124. // Returns the cells used in the tilemap.
  125. TypedArray<Vector2i> a;
  126. a.resize(pattern.size());
  127. int i = 0;
  128. for (const KeyValue<Vector2i, TileMapCell> &E : pattern) {
  129. Vector2i p(E.key.x, E.key.y);
  130. a[i++] = p;
  131. }
  132. return a;
  133. }
  134. Size2i TileMapPattern::get_size() const {
  135. return size;
  136. }
  137. void TileMapPattern::set_size(const Size2i &p_size) {
  138. for (const KeyValue<Vector2i, TileMapCell> &E : pattern) {
  139. Vector2i coords = E.key;
  140. if (p_size.x <= coords.x || p_size.y <= coords.y) {
  141. ERR_FAIL_MSG(vformat("Cannot set pattern size to %s, it contains a tile at %s. Size can only be increased.", p_size, coords));
  142. };
  143. }
  144. size = p_size;
  145. emit_changed();
  146. }
  147. bool TileMapPattern::is_empty() const {
  148. return pattern.is_empty();
  149. }
  150. void TileMapPattern::clear() {
  151. size = Size2i();
  152. pattern.clear();
  153. emit_changed();
  154. }
  155. bool TileMapPattern::_set(const StringName &p_name, const Variant &p_value) {
  156. if (p_name == "tile_data") {
  157. if (p_value.is_array()) {
  158. _set_tile_data(p_value);
  159. return true;
  160. }
  161. return false;
  162. }
  163. return false;
  164. }
  165. bool TileMapPattern::_get(const StringName &p_name, Variant &r_ret) const {
  166. if (p_name == "tile_data") {
  167. r_ret = _get_tile_data();
  168. return true;
  169. }
  170. return false;
  171. }
  172. void TileMapPattern::_get_property_list(List<PropertyInfo> *p_list) const {
  173. p_list->push_back(PropertyInfo(Variant::OBJECT, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  174. }
  175. void TileMapPattern::_bind_methods() {
  176. 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));
  177. ClassDB::bind_method(D_METHOD("has_cell", "coords"), &TileMapPattern::has_cell);
  178. ClassDB::bind_method(D_METHOD("remove_cell", "coords", "update_size"), &TileMapPattern::remove_cell);
  179. ClassDB::bind_method(D_METHOD("get_cell_source_id", "coords"), &TileMapPattern::get_cell_source_id);
  180. ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "coords"), &TileMapPattern::get_cell_atlas_coords);
  181. ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "coords"), &TileMapPattern::get_cell_alternative_tile);
  182. ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMapPattern::get_used_cells);
  183. ClassDB::bind_method(D_METHOD("get_size"), &TileMapPattern::get_size);
  184. ClassDB::bind_method(D_METHOD("set_size", "size"), &TileMapPattern::set_size);
  185. ClassDB::bind_method(D_METHOD("is_empty"), &TileMapPattern::is_empty);
  186. }
  187. /////////////////////////////// TileSet //////////////////////////////////////
  188. bool TileSet::TerrainsPattern::is_valid() const {
  189. return valid;
  190. }
  191. bool TileSet::TerrainsPattern::is_erase_pattern() const {
  192. return not_empty_terrains_count == 0;
  193. }
  194. bool TileSet::TerrainsPattern::operator<(const TerrainsPattern &p_terrains_pattern) const {
  195. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  196. if (is_valid_bit[i] != p_terrains_pattern.is_valid_bit[i]) {
  197. return is_valid_bit[i] < p_terrains_pattern.is_valid_bit[i];
  198. }
  199. }
  200. if (terrain != p_terrains_pattern.terrain) {
  201. return terrain < p_terrains_pattern.terrain;
  202. }
  203. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  204. if (is_valid_bit[i] && bits[i] != p_terrains_pattern.bits[i]) {
  205. return bits[i] < p_terrains_pattern.bits[i];
  206. }
  207. }
  208. return false;
  209. }
  210. bool TileSet::TerrainsPattern::operator==(const TerrainsPattern &p_terrains_pattern) const {
  211. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  212. if (is_valid_bit[i] != p_terrains_pattern.is_valid_bit[i]) {
  213. return false;
  214. }
  215. if (is_valid_bit[i] && bits[i] != p_terrains_pattern.bits[i]) {
  216. return false;
  217. }
  218. }
  219. if (terrain != p_terrains_pattern.terrain) {
  220. return false;
  221. }
  222. return true;
  223. }
  224. void TileSet::TerrainsPattern::set_terrain(int p_terrain) {
  225. ERR_FAIL_COND(p_terrain < -1);
  226. terrain = p_terrain;
  227. }
  228. int TileSet::TerrainsPattern::get_terrain() const {
  229. return terrain;
  230. }
  231. void TileSet::TerrainsPattern::set_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit, int p_terrain) {
  232. ERR_FAIL_COND(p_peering_bit == TileSet::CELL_NEIGHBOR_MAX);
  233. ERR_FAIL_COND(!is_valid_bit[p_peering_bit]);
  234. ERR_FAIL_COND(p_terrain < -1);
  235. // Update the "is_erase_pattern" status.
  236. if (p_terrain >= 0 && bits[p_peering_bit] < 0) {
  237. not_empty_terrains_count++;
  238. } else if (p_terrain < 0 && bits[p_peering_bit] >= 0) {
  239. not_empty_terrains_count--;
  240. }
  241. bits[p_peering_bit] = p_terrain;
  242. }
  243. int TileSet::TerrainsPattern::get_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit) const {
  244. ERR_FAIL_COND_V(p_peering_bit == TileSet::CELL_NEIGHBOR_MAX, -1);
  245. ERR_FAIL_COND_V(!is_valid_bit[p_peering_bit], -1);
  246. return bits[p_peering_bit];
  247. }
  248. void TileSet::TerrainsPattern::from_array(Array p_terrains) {
  249. set_terrain(p_terrains[0]);
  250. int in_array_index = 1;
  251. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  252. if (is_valid_bit[i]) {
  253. ERR_FAIL_INDEX(in_array_index, p_terrains.size());
  254. set_terrain_peering_bit(TileSet::CellNeighbor(i), p_terrains[in_array_index]);
  255. in_array_index++;
  256. }
  257. }
  258. }
  259. Array TileSet::TerrainsPattern::as_array() const {
  260. Array output = { get_terrain() };
  261. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  262. if (is_valid_bit[i]) {
  263. output.push_back(bits[i]);
  264. }
  265. }
  266. return output;
  267. }
  268. TileSet::TerrainsPattern::TerrainsPattern(const TileSet *p_tile_set, int p_terrain_set) {
  269. ERR_FAIL_COND(p_terrain_set < 0);
  270. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  271. is_valid_bit[i] = (p_tile_set->is_valid_terrain_peering_bit(p_terrain_set, TileSet::CellNeighbor(i)));
  272. bits[i] = -1;
  273. }
  274. valid = true;
  275. }
  276. const int TileSet::INVALID_SOURCE = -1;
  277. const char *TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[] = {
  278. PNAME("right_side"),
  279. PNAME("right_corner"),
  280. PNAME("bottom_right_side"),
  281. PNAME("bottom_right_corner"),
  282. PNAME("bottom_side"),
  283. PNAME("bottom_corner"),
  284. PNAME("bottom_left_side"),
  285. PNAME("bottom_left_corner"),
  286. PNAME("left_side"),
  287. PNAME("left_corner"),
  288. PNAME("top_left_side"),
  289. PNAME("top_left_corner"),
  290. PNAME("top_side"),
  291. PNAME("top_corner"),
  292. PNAME("top_right_side"),
  293. PNAME("top_right_corner"),
  294. };
  295. // -- Shape and layout --
  296. void TileSet::set_tile_shape(TileSet::TileShape p_shape) {
  297. tile_shape = p_shape;
  298. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  299. E_source.value->notify_tile_data_properties_should_change();
  300. }
  301. terrain_bits_meshes_dirty = true;
  302. tile_meshes_dirty = true;
  303. notify_property_list_changed();
  304. emit_changed();
  305. }
  306. TileSet::TileShape TileSet::get_tile_shape() const {
  307. return tile_shape;
  308. }
  309. void TileSet::set_tile_layout(TileSet::TileLayout p_layout) {
  310. tile_layout = p_layout;
  311. emit_changed();
  312. }
  313. TileSet::TileLayout TileSet::get_tile_layout() const {
  314. return tile_layout;
  315. }
  316. void TileSet::set_tile_offset_axis(TileSet::TileOffsetAxis p_alignment) {
  317. tile_offset_axis = p_alignment;
  318. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  319. E_source.value->notify_tile_data_properties_should_change();
  320. }
  321. terrain_bits_meshes_dirty = true;
  322. tile_meshes_dirty = true;
  323. emit_changed();
  324. }
  325. TileSet::TileOffsetAxis TileSet::get_tile_offset_axis() const {
  326. return tile_offset_axis;
  327. }
  328. void TileSet::set_tile_size(Size2i p_size) {
  329. ERR_FAIL_COND(p_size.x < 1 || p_size.y < 1);
  330. tile_size = p_size;
  331. terrain_bits_meshes_dirty = true;
  332. tile_meshes_dirty = true;
  333. emit_changed();
  334. }
  335. Size2i TileSet::get_tile_size() const {
  336. return tile_size;
  337. }
  338. int TileSet::get_next_source_id() const {
  339. return next_source_id;
  340. }
  341. void TileSet::_update_terrains_cache() {
  342. if (terrains_cache_dirty) {
  343. // Organizes tiles into structures.
  344. per_terrain_pattern_tiles.resize(terrain_sets.size());
  345. for (RBMap<TileSet::TerrainsPattern, RBSet<TileMapCell>> &tiles : per_terrain_pattern_tiles) {
  346. tiles.clear();
  347. }
  348. for (const KeyValue<int, Ref<TileSetSource>> &kv : sources) {
  349. Ref<TileSetSource> source = kv.value;
  350. Ref<TileSetAtlasSource> atlas_source = source;
  351. if (atlas_source.is_valid()) {
  352. for (int tile_index = 0; tile_index < source->get_tiles_count(); tile_index++) {
  353. Vector2i tile_id = source->get_tile_id(tile_index);
  354. for (int alternative_index = 0; alternative_index < source->get_alternative_tiles_count(tile_id); alternative_index++) {
  355. int alternative_id = source->get_alternative_tile_id(tile_id, alternative_index);
  356. // Executed for each tile_data.
  357. TileData *tile_data = atlas_source->get_tile_data(tile_id, alternative_id);
  358. int terrain_set = tile_data->get_terrain_set();
  359. if (terrain_set >= 0) {
  360. TileMapCell cell;
  361. cell.source_id = kv.key;
  362. cell.set_atlas_coords(tile_id);
  363. cell.alternative_tile = alternative_id;
  364. TileSet::TerrainsPattern terrains_pattern = tile_data->get_terrains_pattern();
  365. // Main terrain.
  366. if (terrains_pattern.get_terrain() >= 0) {
  367. per_terrain_pattern_tiles[terrain_set][terrains_pattern].insert(cell);
  368. }
  369. // Terrain bits.
  370. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  371. CellNeighbor bit = CellNeighbor(i);
  372. if (is_valid_terrain_peering_bit(terrain_set, bit)) {
  373. int terrain = terrains_pattern.get_terrain_peering_bit(bit);
  374. if (terrain >= 0) {
  375. per_terrain_pattern_tiles[terrain_set][terrains_pattern].insert(cell);
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. // Add the empty cell in the possible patterns and cells.
  385. for (int i = 0; i < terrain_sets.size(); i++) {
  386. TileSet::TerrainsPattern empty_pattern(this, i);
  387. TileMapCell empty_cell;
  388. empty_cell.source_id = TileSet::INVALID_SOURCE;
  389. empty_cell.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  390. empty_cell.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  391. per_terrain_pattern_tiles[i][empty_pattern].insert(empty_cell);
  392. }
  393. terrains_cache_dirty = false;
  394. }
  395. }
  396. void TileSet::_compute_next_source_id() {
  397. while (sources.has(next_source_id)) {
  398. next_source_id = (next_source_id + 1) % 1073741824; // 2 ** 30
  399. };
  400. }
  401. // Sources management
  402. int TileSet::add_source(Ref<TileSetSource> p_tile_set_source, int p_atlas_source_id_override) {
  403. ERR_FAIL_COND_V(p_tile_set_source.is_null(), TileSet::INVALID_SOURCE);
  404. 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));
  405. 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));
  406. int new_source_id = p_atlas_source_id_override >= 0 ? p_atlas_source_id_override : next_source_id;
  407. sources[new_source_id] = p_tile_set_source;
  408. source_ids.push_back(new_source_id);
  409. source_ids.sort();
  410. TileSet *old_tileset = p_tile_set_source->get_tile_set();
  411. if (old_tileset != this && old_tileset != nullptr) {
  412. // If the source is already in a TileSet (might happen when duplicating), remove it from the other TileSet.
  413. old_tileset->remove_source_ptr(p_tile_set_source.ptr());
  414. }
  415. p_tile_set_source->set_tile_set(this);
  416. _compute_next_source_id();
  417. sources[new_source_id]->connect_changed(callable_mp(this, &TileSet::_source_changed));
  418. terrains_cache_dirty = true;
  419. emit_changed();
  420. return new_source_id;
  421. }
  422. void TileSet::remove_source(int p_source_id) {
  423. 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));
  424. sources[p_source_id]->disconnect_changed(callable_mp(this, &TileSet::_source_changed));
  425. sources[p_source_id]->set_tile_set(nullptr);
  426. sources.erase(p_source_id);
  427. source_ids.erase(p_source_id);
  428. source_ids.sort();
  429. terrains_cache_dirty = true;
  430. emit_changed();
  431. }
  432. void TileSet::remove_source_ptr(TileSetSource *p_tile_set_source) {
  433. for (const KeyValue<int, Ref<TileSetSource>> &kv : sources) {
  434. if (kv.value.ptr() == p_tile_set_source) {
  435. remove_source(kv.key);
  436. return;
  437. }
  438. }
  439. ERR_FAIL_MSG(vformat("Attempting to remove source from a tileset, but the tileset doesn't have it: %s", p_tile_set_source));
  440. }
  441. void TileSet::set_source_id(int p_source_id, int p_new_source_id) {
  442. ERR_FAIL_COND(p_new_source_id < 0);
  443. 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));
  444. if (p_source_id == p_new_source_id) {
  445. return;
  446. }
  447. 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));
  448. sources[p_new_source_id] = sources[p_source_id];
  449. sources.erase(p_source_id);
  450. source_ids.erase(p_source_id);
  451. source_ids.push_back(p_new_source_id);
  452. source_ids.sort();
  453. _compute_next_source_id();
  454. terrains_cache_dirty = true;
  455. emit_changed();
  456. }
  457. bool TileSet::has_source(int p_source_id) const {
  458. return sources.has(p_source_id);
  459. }
  460. Ref<TileSetSource> TileSet::get_source(int p_source_id) const {
  461. ERR_FAIL_COND_V_MSG(!sources.has(p_source_id), nullptr, vformat("No TileSet atlas source with id %d.", p_source_id));
  462. return sources[p_source_id];
  463. }
  464. int TileSet::get_source_count() const {
  465. return source_ids.size();
  466. }
  467. int TileSet::get_source_id(int p_index) const {
  468. ERR_FAIL_INDEX_V(p_index, source_ids.size(), TileSet::INVALID_SOURCE);
  469. return source_ids[p_index];
  470. }
  471. // Rendering
  472. void TileSet::set_uv_clipping(bool p_uv_clipping) {
  473. if (uv_clipping == p_uv_clipping) {
  474. return;
  475. }
  476. uv_clipping = p_uv_clipping;
  477. emit_changed();
  478. }
  479. bool TileSet::is_uv_clipping() const {
  480. return uv_clipping;
  481. }
  482. int TileSet::get_occlusion_layers_count() const {
  483. return occlusion_layers.size();
  484. }
  485. void TileSet::add_occlusion_layer(int p_index) {
  486. if (p_index < 0) {
  487. p_index = occlusion_layers.size();
  488. }
  489. ERR_FAIL_INDEX(p_index, occlusion_layers.size() + 1);
  490. occlusion_layers.insert(p_index, OcclusionLayer());
  491. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  492. source.value->add_occlusion_layer(p_index);
  493. }
  494. notify_property_list_changed();
  495. emit_changed();
  496. }
  497. void TileSet::move_occlusion_layer(int p_from_index, int p_to_pos) {
  498. ERR_FAIL_INDEX(p_from_index, occlusion_layers.size());
  499. ERR_FAIL_INDEX(p_to_pos, occlusion_layers.size() + 1);
  500. occlusion_layers.insert(p_to_pos, occlusion_layers[p_from_index]);
  501. occlusion_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  502. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  503. source.value->move_occlusion_layer(p_from_index, p_to_pos);
  504. }
  505. notify_property_list_changed();
  506. emit_changed();
  507. }
  508. void TileSet::remove_occlusion_layer(int p_index) {
  509. ERR_FAIL_INDEX(p_index, occlusion_layers.size());
  510. occlusion_layers.remove_at(p_index);
  511. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  512. source.value->remove_occlusion_layer(p_index);
  513. }
  514. notify_property_list_changed();
  515. emit_changed();
  516. }
  517. void TileSet::set_occlusion_layer_light_mask(int p_layer_index, int p_light_mask) {
  518. ERR_FAIL_INDEX(p_layer_index, occlusion_layers.size());
  519. occlusion_layers.write[p_layer_index].light_mask = p_light_mask;
  520. emit_changed();
  521. }
  522. int TileSet::get_occlusion_layer_light_mask(int p_layer_index) const {
  523. ERR_FAIL_INDEX_V(p_layer_index, occlusion_layers.size(), 0);
  524. return occlusion_layers[p_layer_index].light_mask;
  525. }
  526. void TileSet::set_occlusion_layer_sdf_collision(int p_layer_index, bool p_sdf_collision) {
  527. ERR_FAIL_INDEX(p_layer_index, occlusion_layers.size());
  528. occlusion_layers.write[p_layer_index].sdf_collision = p_sdf_collision;
  529. emit_changed();
  530. }
  531. bool TileSet::get_occlusion_layer_sdf_collision(int p_layer_index) const {
  532. ERR_FAIL_INDEX_V(p_layer_index, occlusion_layers.size(), false);
  533. return occlusion_layers[p_layer_index].sdf_collision;
  534. }
  535. #ifndef PHYSICS_2D_DISABLED
  536. int TileSet::get_physics_layers_count() const {
  537. return physics_layers.size();
  538. }
  539. void TileSet::add_physics_layer(int p_index) {
  540. if (p_index < 0) {
  541. p_index = physics_layers.size();
  542. }
  543. ERR_FAIL_INDEX(p_index, physics_layers.size() + 1);
  544. physics_layers.insert(p_index, PhysicsLayer());
  545. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  546. source.value->add_physics_layer(p_index);
  547. }
  548. notify_property_list_changed();
  549. emit_changed();
  550. }
  551. void TileSet::move_physics_layer(int p_from_index, int p_to_pos) {
  552. ERR_FAIL_INDEX(p_from_index, physics_layers.size());
  553. ERR_FAIL_INDEX(p_to_pos, physics_layers.size() + 1);
  554. physics_layers.insert(p_to_pos, physics_layers[p_from_index]);
  555. physics_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  556. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  557. source.value->move_physics_layer(p_from_index, p_to_pos);
  558. }
  559. notify_property_list_changed();
  560. emit_changed();
  561. }
  562. void TileSet::remove_physics_layer(int p_index) {
  563. ERR_FAIL_INDEX(p_index, physics_layers.size());
  564. physics_layers.remove_at(p_index);
  565. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  566. source.value->remove_physics_layer(p_index);
  567. }
  568. notify_property_list_changed();
  569. emit_changed();
  570. }
  571. void TileSet::set_physics_layer_collision_layer(int p_layer_index, uint32_t p_layer) {
  572. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  573. physics_layers.write[p_layer_index].collision_layer = p_layer;
  574. emit_changed();
  575. }
  576. uint32_t TileSet::get_physics_layer_collision_layer(int p_layer_index) const {
  577. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), 0);
  578. return physics_layers[p_layer_index].collision_layer;
  579. }
  580. void TileSet::set_physics_layer_collision_mask(int p_layer_index, uint32_t p_mask) {
  581. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  582. physics_layers.write[p_layer_index].collision_mask = p_mask;
  583. emit_changed();
  584. }
  585. uint32_t TileSet::get_physics_layer_collision_mask(int p_layer_index) const {
  586. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), 0);
  587. return physics_layers[p_layer_index].collision_mask;
  588. }
  589. void TileSet::set_physics_layer_collision_priority(int p_layer_index, real_t p_priority) {
  590. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  591. physics_layers.write[p_layer_index].collision_priority = p_priority;
  592. emit_changed();
  593. }
  594. real_t TileSet::get_physics_layer_collision_priority(int p_layer_index) const {
  595. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), 0);
  596. return physics_layers[p_layer_index].collision_priority;
  597. }
  598. void TileSet::set_physics_layer_physics_material(int p_layer_index, Ref<PhysicsMaterial> p_physics_material) {
  599. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  600. physics_layers.write[p_layer_index].physics_material = p_physics_material;
  601. }
  602. Ref<PhysicsMaterial> TileSet::get_physics_layer_physics_material(int p_layer_index) const {
  603. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), Ref<PhysicsMaterial>());
  604. return physics_layers[p_layer_index].physics_material;
  605. }
  606. #endif // PHYSICS_2D_DISABLED
  607. // Terrains
  608. int TileSet::get_terrain_sets_count() const {
  609. return terrain_sets.size();
  610. }
  611. void TileSet::add_terrain_set(int p_index) {
  612. if (p_index < 0) {
  613. p_index = terrain_sets.size();
  614. }
  615. ERR_FAIL_INDEX(p_index, terrain_sets.size() + 1);
  616. terrain_sets.insert(p_index, TerrainSet());
  617. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  618. source.value->add_terrain_set(p_index);
  619. }
  620. notify_property_list_changed();
  621. terrains_cache_dirty = true;
  622. emit_changed();
  623. }
  624. void TileSet::move_terrain_set(int p_from_index, int p_to_pos) {
  625. ERR_FAIL_INDEX(p_from_index, terrain_sets.size());
  626. ERR_FAIL_INDEX(p_to_pos, terrain_sets.size() + 1);
  627. terrain_sets.insert(p_to_pos, terrain_sets[p_from_index]);
  628. terrain_sets.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  629. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  630. source.value->move_terrain_set(p_from_index, p_to_pos);
  631. }
  632. notify_property_list_changed();
  633. terrains_cache_dirty = true;
  634. emit_changed();
  635. }
  636. void TileSet::remove_terrain_set(int p_index) {
  637. ERR_FAIL_INDEX(p_index, terrain_sets.size());
  638. terrain_sets.remove_at(p_index);
  639. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  640. source.value->remove_terrain_set(p_index);
  641. }
  642. notify_property_list_changed();
  643. terrains_cache_dirty = true;
  644. emit_changed();
  645. }
  646. void TileSet::set_terrain_set_mode(int p_terrain_set, TerrainMode p_terrain_mode) {
  647. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  648. terrain_sets.write[p_terrain_set].mode = p_terrain_mode;
  649. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  650. E_source.value->notify_tile_data_properties_should_change();
  651. }
  652. notify_property_list_changed();
  653. terrains_cache_dirty = true;
  654. emit_changed();
  655. }
  656. TileSet::TerrainMode TileSet::get_terrain_set_mode(int p_terrain_set) const {
  657. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES);
  658. return terrain_sets[p_terrain_set].mode;
  659. }
  660. int TileSet::get_terrains_count(int p_terrain_set) const {
  661. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), -1);
  662. return terrain_sets[p_terrain_set].terrains.size();
  663. }
  664. void TileSet::add_terrain(int p_terrain_set, int p_index) {
  665. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  666. Vector<Terrain> &terrains = terrain_sets.write[p_terrain_set].terrains;
  667. if (p_index < 0) {
  668. p_index = terrains.size();
  669. }
  670. ERR_FAIL_INDEX(p_index, terrains.size() + 1);
  671. terrains.insert(p_index, Terrain());
  672. // Default name and color
  673. float hue_rotate = (terrains.size() % 16) / 16.0;
  674. Color c;
  675. c.set_hsv(Math::fmod(float(hue_rotate), float(1.0)), 0.5, 0.5);
  676. terrains.write[p_index].color = c;
  677. terrains.write[p_index].name = String(vformat("Terrain %d", p_index));
  678. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  679. source.value->add_terrain(p_terrain_set, p_index);
  680. }
  681. notify_property_list_changed();
  682. terrains_cache_dirty = true;
  683. emit_changed();
  684. }
  685. void TileSet::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  686. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  687. Vector<Terrain> &terrains = terrain_sets.write[p_terrain_set].terrains;
  688. ERR_FAIL_INDEX(p_from_index, terrains.size());
  689. ERR_FAIL_INDEX(p_to_pos, terrains.size() + 1);
  690. terrains.insert(p_to_pos, terrains[p_from_index]);
  691. terrains.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  692. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  693. source.value->move_terrain(p_terrain_set, p_from_index, p_to_pos);
  694. }
  695. notify_property_list_changed();
  696. terrains_cache_dirty = true;
  697. emit_changed();
  698. }
  699. void TileSet::remove_terrain(int p_terrain_set, int p_index) {
  700. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  701. Vector<Terrain> &terrains = terrain_sets.write[p_terrain_set].terrains;
  702. ERR_FAIL_INDEX(p_index, terrains.size());
  703. terrains.remove_at(p_index);
  704. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  705. source.value->remove_terrain(p_terrain_set, p_index);
  706. }
  707. notify_property_list_changed();
  708. terrains_cache_dirty = true;
  709. emit_changed();
  710. }
  711. void TileSet::set_terrain_name(int p_terrain_set, int p_terrain_index, String p_name) {
  712. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  713. ERR_FAIL_INDEX(p_terrain_index, terrain_sets[p_terrain_set].terrains.size());
  714. terrain_sets.write[p_terrain_set].terrains.write[p_terrain_index].name = p_name;
  715. emit_changed();
  716. }
  717. String TileSet::get_terrain_name(int p_terrain_set, int p_terrain_index) const {
  718. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), String());
  719. ERR_FAIL_INDEX_V(p_terrain_index, terrain_sets[p_terrain_set].terrains.size(), String());
  720. return terrain_sets[p_terrain_set].terrains[p_terrain_index].name;
  721. }
  722. void TileSet::set_terrain_color(int p_terrain_set, int p_terrain_index, Color p_color) {
  723. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  724. ERR_FAIL_INDEX(p_terrain_index, terrain_sets[p_terrain_set].terrains.size());
  725. if (p_color.a != 1.0) {
  726. WARN_PRINT("Terrain color should have alpha == 1.0");
  727. p_color.a = 1.0;
  728. }
  729. terrain_sets.write[p_terrain_set].terrains.write[p_terrain_index].color = p_color;
  730. emit_changed();
  731. }
  732. Color TileSet::get_terrain_color(int p_terrain_set, int p_terrain_index) const {
  733. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), Color());
  734. ERR_FAIL_INDEX_V(p_terrain_index, terrain_sets[p_terrain_set].terrains.size(), Color());
  735. return terrain_sets[p_terrain_set].terrains[p_terrain_index].color;
  736. }
  737. bool TileSet::is_valid_terrain_peering_bit_for_mode(TileSet::TerrainMode p_terrain_mode, TileSet::CellNeighbor p_peering_bit) const {
  738. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  739. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  740. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  741. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  742. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  743. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_SIDE) {
  744. return true;
  745. }
  746. }
  747. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  748. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  749. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  750. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  751. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER) {
  752. return true;
  753. }
  754. }
  755. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  756. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  757. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  758. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  759. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  760. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  761. return true;
  762. }
  763. }
  764. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  765. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  766. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  767. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  768. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  769. return true;
  770. }
  771. }
  772. } else {
  773. if (get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  774. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  775. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  776. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  777. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  778. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  779. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  780. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  781. return true;
  782. }
  783. }
  784. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  785. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  786. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  787. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  788. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  789. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_CORNER ||
  790. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER) {
  791. return true;
  792. }
  793. }
  794. } else {
  795. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  796. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  797. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  798. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  799. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  800. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  801. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  802. return true;
  803. }
  804. }
  805. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  806. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  807. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  808. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  809. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  810. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  811. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER) {
  812. return true;
  813. }
  814. }
  815. }
  816. }
  817. return false;
  818. }
  819. bool TileSet::is_valid_terrain_peering_bit(int p_terrain_set, TileSet::CellNeighbor p_peering_bit) const {
  820. if (p_terrain_set < 0 || p_terrain_set >= get_terrain_sets_count()) {
  821. return false;
  822. }
  823. TileSet::TerrainMode terrain_mode = get_terrain_set_mode(p_terrain_set);
  824. return is_valid_terrain_peering_bit_for_mode(terrain_mode, p_peering_bit);
  825. }
  826. #ifndef NAVIGATION_2D_DISABLED
  827. // Navigation
  828. int TileSet::get_navigation_layers_count() const {
  829. return navigation_layers.size();
  830. }
  831. void TileSet::add_navigation_layer(int p_index) {
  832. if (p_index < 0) {
  833. p_index = navigation_layers.size();
  834. }
  835. ERR_FAIL_INDEX(p_index, navigation_layers.size() + 1);
  836. navigation_layers.insert(p_index, NavigationLayer());
  837. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  838. source.value->add_navigation_layer(p_index);
  839. }
  840. notify_property_list_changed();
  841. emit_changed();
  842. }
  843. void TileSet::move_navigation_layer(int p_from_index, int p_to_pos) {
  844. ERR_FAIL_INDEX(p_from_index, navigation_layers.size());
  845. ERR_FAIL_INDEX(p_to_pos, navigation_layers.size() + 1);
  846. navigation_layers.insert(p_to_pos, navigation_layers[p_from_index]);
  847. navigation_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  848. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  849. source.value->move_navigation_layer(p_from_index, p_to_pos);
  850. }
  851. notify_property_list_changed();
  852. emit_changed();
  853. }
  854. void TileSet::remove_navigation_layer(int p_index) {
  855. ERR_FAIL_INDEX(p_index, navigation_layers.size());
  856. navigation_layers.remove_at(p_index);
  857. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  858. source.value->remove_navigation_layer(p_index);
  859. }
  860. notify_property_list_changed();
  861. emit_changed();
  862. }
  863. void TileSet::set_navigation_layer_layers(int p_layer_index, uint32_t p_layers) {
  864. ERR_FAIL_INDEX(p_layer_index, navigation_layers.size());
  865. navigation_layers.write[p_layer_index].layers = p_layers;
  866. emit_changed();
  867. }
  868. uint32_t TileSet::get_navigation_layer_layers(int p_layer_index) const {
  869. ERR_FAIL_INDEX_V(p_layer_index, navigation_layers.size(), 0);
  870. return navigation_layers[p_layer_index].layers;
  871. }
  872. void TileSet::set_navigation_layer_layer_value(int p_layer_index, int p_layer_number, bool p_value) {
  873. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  874. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  875. uint32_t _navigation_layers = get_navigation_layer_layers(p_layer_index);
  876. if (p_value) {
  877. _navigation_layers |= 1 << (p_layer_number - 1);
  878. } else {
  879. _navigation_layers &= ~(1 << (p_layer_number - 1));
  880. }
  881. set_navigation_layer_layers(p_layer_index, _navigation_layers);
  882. }
  883. bool TileSet::get_navigation_layer_layer_value(int p_layer_index, int p_layer_number) const {
  884. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  885. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  886. return get_navigation_layer_layers(p_layer_index) & (1 << (p_layer_number - 1));
  887. }
  888. #endif // NAVIGATION_2D_DISABLED
  889. // Custom data.
  890. int TileSet::get_custom_data_layers_count() const {
  891. return custom_data_layers.size();
  892. }
  893. void TileSet::add_custom_data_layer(int p_index) {
  894. if (p_index < 0) {
  895. p_index = custom_data_layers.size();
  896. }
  897. ERR_FAIL_INDEX(p_index, custom_data_layers.size() + 1);
  898. custom_data_layers.insert(p_index, CustomDataLayer());
  899. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  900. source.value->add_custom_data_layer(p_index);
  901. }
  902. notify_property_list_changed();
  903. emit_changed();
  904. }
  905. void TileSet::move_custom_data_layer(int p_from_index, int p_to_pos) {
  906. ERR_FAIL_INDEX(p_from_index, custom_data_layers.size());
  907. ERR_FAIL_INDEX(p_to_pos, custom_data_layers.size() + 1);
  908. custom_data_layers.insert(p_to_pos, custom_data_layers[p_from_index]);
  909. custom_data_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  910. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  911. source.value->move_custom_data_layer(p_from_index, p_to_pos);
  912. }
  913. notify_property_list_changed();
  914. emit_changed();
  915. }
  916. void TileSet::remove_custom_data_layer(int p_index) {
  917. ERR_FAIL_INDEX(p_index, custom_data_layers.size());
  918. custom_data_layers.remove_at(p_index);
  919. String to_erase;
  920. for (KeyValue<String, int> &E : custom_data_layers_by_name) {
  921. if (E.value == p_index) {
  922. to_erase = E.key;
  923. } else if (E.value > p_index) {
  924. E.value--;
  925. }
  926. }
  927. custom_data_layers_by_name.erase(to_erase);
  928. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  929. source.value->remove_custom_data_layer(p_index);
  930. }
  931. notify_property_list_changed();
  932. emit_changed();
  933. }
  934. int TileSet::get_custom_data_layer_by_name(String p_value) const {
  935. if (custom_data_layers_by_name.has(p_value)) {
  936. return custom_data_layers_by_name[p_value];
  937. } else {
  938. return -1;
  939. }
  940. }
  941. void TileSet::set_custom_data_layer_name(int p_layer_id, String p_value) {
  942. ERR_FAIL_INDEX(p_layer_id, custom_data_layers.size());
  943. // Exit if another property has the same name.
  944. if (!p_value.is_empty()) {
  945. for (int other_layer_id = 0; other_layer_id < get_custom_data_layers_count(); other_layer_id++) {
  946. if (other_layer_id != p_layer_id && get_custom_data_layer_name(other_layer_id) == p_value) {
  947. ERR_FAIL_MSG(vformat("There is already a custom property named %s", p_value));
  948. }
  949. }
  950. }
  951. if (p_value.is_empty() && custom_data_layers_by_name.has(p_value)) {
  952. custom_data_layers_by_name.erase(p_value);
  953. } else {
  954. custom_data_layers_by_name[p_value] = p_layer_id;
  955. }
  956. custom_data_layers.write[p_layer_id].name = p_value;
  957. emit_changed();
  958. }
  959. bool TileSet::has_custom_data_layer_by_name(const String &p_value) const {
  960. return custom_data_layers_by_name.has(p_value);
  961. }
  962. String TileSet::get_custom_data_layer_name(int p_layer_id) const {
  963. ERR_FAIL_INDEX_V(p_layer_id, custom_data_layers.size(), "");
  964. return custom_data_layers[p_layer_id].name;
  965. }
  966. void TileSet::set_custom_data_layer_type(int p_layer_id, Variant::Type p_value) {
  967. ERR_FAIL_INDEX(p_layer_id, custom_data_layers.size());
  968. custom_data_layers.write[p_layer_id].type = p_value;
  969. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  970. E_source.value->notify_tile_data_properties_should_change();
  971. }
  972. emit_changed();
  973. }
  974. Variant::Type TileSet::get_custom_data_layer_type(int p_layer_id) const {
  975. ERR_FAIL_INDEX_V(p_layer_id, custom_data_layers.size(), Variant::NIL);
  976. return custom_data_layers[p_layer_id].type;
  977. }
  978. void TileSet::set_source_level_tile_proxy(int p_source_from, int p_source_to) {
  979. ERR_FAIL_COND(p_source_from == TileSet::INVALID_SOURCE || p_source_to == TileSet::INVALID_SOURCE);
  980. source_level_proxies[p_source_from] = p_source_to;
  981. emit_changed();
  982. }
  983. int TileSet::get_source_level_tile_proxy(int p_source_from) {
  984. ERR_FAIL_COND_V(!source_level_proxies.has(p_source_from), TileSet::INVALID_SOURCE);
  985. return source_level_proxies[p_source_from];
  986. }
  987. bool TileSet::has_source_level_tile_proxy(int p_source_from) {
  988. return source_level_proxies.has(p_source_from);
  989. }
  990. void TileSet::remove_source_level_tile_proxy(int p_source_from) {
  991. ERR_FAIL_COND(!source_level_proxies.has(p_source_from));
  992. source_level_proxies.erase(p_source_from);
  993. emit_changed();
  994. }
  995. void TileSet::set_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_source_to, Vector2i p_coords_to) {
  996. ERR_FAIL_COND(p_source_from == TileSet::INVALID_SOURCE || p_source_to == TileSet::INVALID_SOURCE);
  997. ERR_FAIL_COND(p_coords_from == TileSetSource::INVALID_ATLAS_COORDS || p_coords_to == TileSetSource::INVALID_ATLAS_COORDS);
  998. Array from = { p_source_from, p_coords_from };
  999. Array to = { p_source_to, p_coords_to };
  1000. coords_level_proxies[from] = to;
  1001. emit_changed();
  1002. }
  1003. Array TileSet::get_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from) {
  1004. Array from = { p_source_from, p_coords_from };
  1005. ERR_FAIL_COND_V(!coords_level_proxies.has(from), Array());
  1006. return coords_level_proxies[from];
  1007. }
  1008. bool TileSet::has_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from) {
  1009. Array from = { p_source_from, p_coords_from };
  1010. return coords_level_proxies.has(from);
  1011. }
  1012. void TileSet::remove_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from) {
  1013. Array from = { p_source_from, p_coords_from };
  1014. ERR_FAIL_COND(!coords_level_proxies.has(from));
  1015. coords_level_proxies.erase(from);
  1016. emit_changed();
  1017. }
  1018. 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) {
  1019. ERR_FAIL_COND(p_source_from == TileSet::INVALID_SOURCE || p_source_to == TileSet::INVALID_SOURCE);
  1020. ERR_FAIL_COND(p_coords_from == TileSetSource::INVALID_ATLAS_COORDS || p_coords_to == TileSetSource::INVALID_ATLAS_COORDS);
  1021. Array from = { p_source_from, p_coords_from, p_alternative_from };
  1022. Array to = { p_source_to, p_coords_to, p_alternative_to };
  1023. alternative_level_proxies[from] = to;
  1024. emit_changed();
  1025. }
  1026. Array TileSet::get_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) {
  1027. Array from = { p_source_from, p_coords_from, p_alternative_from };
  1028. ERR_FAIL_COND_V(!alternative_level_proxies.has(from), Array());
  1029. return alternative_level_proxies[from];
  1030. }
  1031. bool TileSet::has_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) {
  1032. Array from = { p_source_from, p_coords_from, p_alternative_from };
  1033. return alternative_level_proxies.has(from);
  1034. }
  1035. void TileSet::remove_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) {
  1036. Array from = { p_source_from, p_coords_from, p_alternative_from };
  1037. ERR_FAIL_COND(!alternative_level_proxies.has(from));
  1038. alternative_level_proxies.erase(from);
  1039. emit_changed();
  1040. }
  1041. Array TileSet::get_source_level_tile_proxies() const {
  1042. Array output;
  1043. for (const KeyValue<int, int> &E : source_level_proxies) {
  1044. Array proxy;
  1045. proxy.push_back(E.key);
  1046. proxy.push_back(E.value);
  1047. output.push_back(proxy);
  1048. }
  1049. return output;
  1050. }
  1051. Array TileSet::get_coords_level_tile_proxies() const {
  1052. Array output;
  1053. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  1054. Array proxy;
  1055. proxy.append_array(E.key);
  1056. proxy.append_array(E.value);
  1057. output.push_back(proxy);
  1058. }
  1059. return output;
  1060. }
  1061. Array TileSet::get_alternative_level_tile_proxies() const {
  1062. Array output;
  1063. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  1064. Array proxy;
  1065. proxy.append_array(E.key);
  1066. proxy.append_array(E.value);
  1067. output.push_back(proxy);
  1068. }
  1069. return output;
  1070. }
  1071. Array TileSet::map_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) const {
  1072. Array from = { p_source_from, p_coords_from, p_alternative_from };
  1073. // Check if the tile is valid, and if so, don't map the tile and return the input.
  1074. if (has_source(p_source_from)) {
  1075. Ref<TileSetSource> source = get_source(p_source_from);
  1076. if (source->has_tile(p_coords_from) && source->has_alternative_tile(p_coords_from, p_alternative_from)) {
  1077. return from;
  1078. }
  1079. }
  1080. // Source, coords and alternative match.
  1081. if (alternative_level_proxies.has(from)) {
  1082. return alternative_level_proxies[from].duplicate();
  1083. }
  1084. // Source and coords match.
  1085. from.pop_back();
  1086. if (coords_level_proxies.has(from)) {
  1087. Array output = coords_level_proxies[from].duplicate();
  1088. output.push_back(p_alternative_from);
  1089. return output;
  1090. }
  1091. // Source matches.
  1092. if (source_level_proxies.has(p_source_from)) {
  1093. return Array{ source_level_proxies[p_source_from], p_coords_from, p_alternative_from };
  1094. }
  1095. return Array{ p_source_from, p_coords_from, p_alternative_from };
  1096. }
  1097. void TileSet::cleanup_invalid_tile_proxies() {
  1098. // Source level.
  1099. Vector<int> source_to_remove;
  1100. for (const KeyValue<int, int> &E : source_level_proxies) {
  1101. if (has_source(E.key)) {
  1102. source_to_remove.push_back(E.key);
  1103. }
  1104. }
  1105. for (int i = 0; i < source_to_remove.size(); i++) {
  1106. remove_source_level_tile_proxy(source_to_remove[i]);
  1107. }
  1108. // Coords level.
  1109. Vector<Array> coords_to_remove;
  1110. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  1111. Array a = E.key;
  1112. if (has_source(a[0]) && get_source(a[0])->has_tile(a[1])) {
  1113. coords_to_remove.push_back(a);
  1114. }
  1115. }
  1116. for (int i = 0; i < coords_to_remove.size(); i++) {
  1117. Array a = coords_to_remove[i];
  1118. remove_coords_level_tile_proxy(a[0], a[1]);
  1119. }
  1120. // Alternative level.
  1121. Vector<Array> alternative_to_remove;
  1122. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  1123. Array a = E.key;
  1124. if (has_source(a[0]) && get_source(a[0])->has_tile(a[1]) && get_source(a[0])->has_alternative_tile(a[1], a[2])) {
  1125. alternative_to_remove.push_back(a);
  1126. }
  1127. }
  1128. for (int i = 0; i < alternative_to_remove.size(); i++) {
  1129. Array a = alternative_to_remove[i];
  1130. remove_alternative_level_tile_proxy(a[0], a[1], a[2]);
  1131. }
  1132. }
  1133. void TileSet::clear_tile_proxies() {
  1134. source_level_proxies.clear();
  1135. coords_level_proxies.clear();
  1136. alternative_level_proxies.clear();
  1137. emit_changed();
  1138. }
  1139. int TileSet::add_pattern(Ref<TileMapPattern> p_pattern, int p_index) {
  1140. ERR_FAIL_COND_V(p_pattern.is_null(), -1);
  1141. ERR_FAIL_COND_V_MSG(p_pattern->is_empty(), -1, "Cannot add an empty pattern to the TileSet.");
  1142. for (const Ref<TileMapPattern> &pattern : patterns) {
  1143. ERR_FAIL_COND_V_MSG(pattern == p_pattern, -1, "TileSet has already this pattern.");
  1144. }
  1145. ERR_FAIL_COND_V(p_index > (int)patterns.size(), -1);
  1146. if (p_index < 0) {
  1147. p_index = patterns.size();
  1148. }
  1149. patterns.insert(p_index, p_pattern);
  1150. emit_changed();
  1151. return p_index;
  1152. }
  1153. Ref<TileMapPattern> TileSet::get_pattern(int p_index) {
  1154. ERR_FAIL_INDEX_V(p_index, (int)patterns.size(), Ref<TileMapPattern>());
  1155. return patterns[p_index];
  1156. }
  1157. void TileSet::remove_pattern(int p_index) {
  1158. ERR_FAIL_INDEX(p_index, (int)patterns.size());
  1159. patterns.remove_at(p_index);
  1160. emit_changed();
  1161. }
  1162. int TileSet::get_patterns_count() {
  1163. return patterns.size();
  1164. }
  1165. RBSet<TileSet::TerrainsPattern> TileSet::get_terrains_pattern_set(int p_terrain_set) {
  1166. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), RBSet<TileSet::TerrainsPattern>());
  1167. _update_terrains_cache();
  1168. RBSet<TileSet::TerrainsPattern> output;
  1169. for (KeyValue<TileSet::TerrainsPattern, RBSet<TileMapCell>> kv : per_terrain_pattern_tiles[p_terrain_set]) {
  1170. output.insert(kv.key);
  1171. }
  1172. return output;
  1173. }
  1174. RBSet<TileMapCell> TileSet::get_tiles_for_terrains_pattern(int p_terrain_set, TerrainsPattern p_terrain_tile_pattern) {
  1175. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), RBSet<TileMapCell>());
  1176. _update_terrains_cache();
  1177. return per_terrain_pattern_tiles[p_terrain_set][p_terrain_tile_pattern];
  1178. }
  1179. TileMapCell TileSet::get_random_tile_from_terrains_pattern(int p_terrain_set, TileSet::TerrainsPattern p_terrain_tile_pattern) {
  1180. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), TileMapCell());
  1181. _update_terrains_cache();
  1182. // Count the sum of probabilities.
  1183. double sum = 0.0;
  1184. RBSet<TileMapCell> set = per_terrain_pattern_tiles[p_terrain_set][p_terrain_tile_pattern];
  1185. for (const TileMapCell &E : set) {
  1186. if (E.source_id >= 0) {
  1187. Ref<TileSetSource> source = sources[E.source_id];
  1188. Ref<TileSetAtlasSource> atlas_source = source;
  1189. if (atlas_source.is_valid()) {
  1190. TileData *tile_data = atlas_source->get_tile_data(E.get_atlas_coords(), E.alternative_tile);
  1191. sum += tile_data->get_probability();
  1192. } else {
  1193. sum += 1.0;
  1194. }
  1195. } else {
  1196. sum += 1.0;
  1197. }
  1198. }
  1199. // Generate a random number.
  1200. double count = 0.0;
  1201. double picked = Math::random(0.0, sum);
  1202. // Pick the tile.
  1203. for (const TileMapCell &E : set) {
  1204. if (E.source_id >= 0) {
  1205. Ref<TileSetSource> source = sources[E.source_id];
  1206. Ref<TileSetAtlasSource> atlas_source = source;
  1207. if (atlas_source.is_valid()) {
  1208. TileData *tile_data = atlas_source->get_tile_data(E.get_atlas_coords(), E.alternative_tile);
  1209. count += tile_data->get_probability();
  1210. } else {
  1211. count += 1.0;
  1212. }
  1213. } else {
  1214. count += 1.0;
  1215. }
  1216. if (count >= picked) {
  1217. return E;
  1218. }
  1219. }
  1220. ERR_FAIL_V(TileMapCell());
  1221. }
  1222. Vector<Vector2> TileSet::get_tile_shape_polygon() const {
  1223. Vector<Vector2> points;
  1224. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1225. points.push_back(Vector2(-0.5, -0.5));
  1226. points.push_back(Vector2(0.5, -0.5));
  1227. points.push_back(Vector2(0.5, 0.5));
  1228. points.push_back(Vector2(-0.5, 0.5));
  1229. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1230. points.push_back(Vector2(0.0, -0.5));
  1231. points.push_back(Vector2(-0.5, 0.0));
  1232. points.push_back(Vector2(0.0, 0.5));
  1233. points.push_back(Vector2(0.5, 0.0));
  1234. } else {
  1235. float overlap = 0.0;
  1236. switch (tile_shape) {
  1237. case TileSet::TILE_SHAPE_HEXAGON:
  1238. overlap = 0.25;
  1239. break;
  1240. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1241. overlap = 0.0;
  1242. break;
  1243. default:
  1244. break;
  1245. }
  1246. points.push_back(Vector2(0.0, -0.5));
  1247. points.push_back(Vector2(-0.5, overlap - 0.5));
  1248. points.push_back(Vector2(-0.5, 0.5 - overlap));
  1249. points.push_back(Vector2(0.0, 0.5));
  1250. points.push_back(Vector2(0.5, 0.5 - overlap));
  1251. points.push_back(Vector2(0.5, overlap - 0.5));
  1252. if (get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  1253. for (int i = 0; i < points.size(); i++) {
  1254. points.write[i] = Vector2(points[i].y, points[i].x);
  1255. }
  1256. }
  1257. }
  1258. return points;
  1259. }
  1260. void TileSet::draw_tile_shape(CanvasItem *p_canvas_item, Transform2D p_transform, Color p_color, bool p_filled, Ref<Texture2D> p_texture) const {
  1261. if (tile_meshes_dirty) {
  1262. Vector<Vector2> shape = get_tile_shape_polygon();
  1263. Vector<Vector2> uvs;
  1264. uvs.resize(shape.size());
  1265. for (int i = 0; i < shape.size(); i++) {
  1266. uvs.write[i] = shape[i] + Vector2(0.5, 0.5);
  1267. }
  1268. Vector<Color> colors;
  1269. colors.resize(shape.size());
  1270. colors.fill(Color(1.0, 1.0, 1.0, 1.0));
  1271. // Filled mesh.
  1272. tile_filled_mesh->clear_surfaces();
  1273. Array a;
  1274. a.resize(Mesh::ARRAY_MAX);
  1275. a[Mesh::ARRAY_VERTEX] = shape;
  1276. a[Mesh::ARRAY_TEX_UV] = uvs;
  1277. a[Mesh::ARRAY_COLOR] = colors;
  1278. a[Mesh::ARRAY_INDEX] = Geometry2D::triangulate_polygon(shape);
  1279. tile_filled_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  1280. // Lines mesh.
  1281. tile_lines_mesh->clear_surfaces();
  1282. a.clear();
  1283. a.resize(Mesh::ARRAY_MAX);
  1284. // Add the first point again when drawing lines.
  1285. shape.push_back(shape[0]);
  1286. colors.push_back(colors[0]);
  1287. a[Mesh::ARRAY_VERTEX] = shape;
  1288. a[Mesh::ARRAY_COLOR] = colors;
  1289. tile_lines_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINE_STRIP, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  1290. tile_meshes_dirty = false;
  1291. }
  1292. if (p_filled) {
  1293. p_canvas_item->draw_mesh(tile_filled_mesh, p_texture, p_transform, p_color);
  1294. } else {
  1295. p_canvas_item->draw_mesh(tile_lines_mesh, Ref<Texture2D>(), p_transform, p_color);
  1296. }
  1297. }
  1298. Vector2 TileSet::map_to_local(const Vector2i &p_pos) const {
  1299. // SHOULD RETURN THE CENTER OF THE CELL.
  1300. Vector2 ret = p_pos;
  1301. if (tile_shape == TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE || tile_shape == TileSet::TILE_SHAPE_HEXAGON || tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1302. // Technically, those 3 shapes are equivalent, as they are basically half-offset, but with different levels or overlap.
  1303. // square = no overlap, hexagon = 0.25 overlap, isometric = 0.5 overlap.
  1304. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1305. switch (tile_layout) {
  1306. case TileSet::TILE_LAYOUT_STACKED:
  1307. ret = Vector2(ret.x + (Math::posmod(ret.y, 2) == 0 ? 0.0 : 0.5), ret.y);
  1308. break;
  1309. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  1310. ret = Vector2(ret.x + (Math::posmod(ret.y, 2) == 1 ? 0.0 : 0.5), ret.y);
  1311. break;
  1312. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  1313. ret = Vector2(ret.x + ret.y / 2, ret.y);
  1314. break;
  1315. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  1316. ret = Vector2(ret.x / 2, ret.y * 2 + ret.x);
  1317. break;
  1318. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  1319. ret = Vector2((ret.x + ret.y) / 2, ret.y - ret.x);
  1320. break;
  1321. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  1322. ret = Vector2((ret.x - ret.y) / 2, ret.y + ret.x);
  1323. break;
  1324. }
  1325. } else { // TILE_OFFSET_AXIS_VERTICAL.
  1326. switch (tile_layout) {
  1327. case TileSet::TILE_LAYOUT_STACKED:
  1328. ret = Vector2(ret.x, ret.y + (Math::posmod(ret.x, 2) == 0 ? 0.0 : 0.5));
  1329. break;
  1330. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  1331. ret = Vector2(ret.x, ret.y + (Math::posmod(ret.x, 2) == 1 ? 0.0 : 0.5));
  1332. break;
  1333. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  1334. ret = Vector2(ret.x * 2 + ret.y, ret.y / 2);
  1335. break;
  1336. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  1337. ret = Vector2(ret.x, ret.y + ret.x / 2);
  1338. break;
  1339. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  1340. ret = Vector2(ret.x + ret.y, (ret.y - ret.x) / 2);
  1341. break;
  1342. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  1343. ret = Vector2(ret.x - ret.y, (ret.y + ret.x) / 2);
  1344. break;
  1345. }
  1346. }
  1347. }
  1348. // Multiply by the overlapping ratio.
  1349. double overlapping_ratio = 1.0;
  1350. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1351. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1352. overlapping_ratio = 0.5;
  1353. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  1354. overlapping_ratio = 0.75;
  1355. }
  1356. ret.y *= overlapping_ratio;
  1357. } else { // TILE_OFFSET_AXIS_VERTICAL.
  1358. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1359. overlapping_ratio = 0.5;
  1360. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  1361. overlapping_ratio = 0.75;
  1362. }
  1363. ret.x *= overlapping_ratio;
  1364. }
  1365. return (ret + Vector2(0.5, 0.5)) * tile_size;
  1366. }
  1367. Vector2i TileSet::local_to_map(const Vector2 &p_local_position) const {
  1368. Vector2 ret = p_local_position;
  1369. ret /= tile_size;
  1370. // Divide by the overlapping ratio.
  1371. double overlapping_ratio = 1.0;
  1372. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1373. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1374. overlapping_ratio = 0.5;
  1375. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  1376. overlapping_ratio = 0.75;
  1377. }
  1378. ret.y /= overlapping_ratio;
  1379. } else { // TILE_OFFSET_AXIS_VERTICAL.
  1380. if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1381. overlapping_ratio = 0.5;
  1382. } else if (tile_shape == TileSet::TILE_SHAPE_HEXAGON) {
  1383. overlapping_ratio = 0.75;
  1384. }
  1385. ret.x /= overlapping_ratio;
  1386. }
  1387. // For each half-offset shape, we check if we are in the corner of the tile, and thus should correct the local position accordingly.
  1388. if (tile_shape == TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE || tile_shape == TileSet::TILE_SHAPE_HEXAGON || tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1389. // Technically, those 3 shapes are equivalent, as they are basically half-offset, but with different levels or overlap.
  1390. // square = no overlap, hexagon = 0.25 overlap, isometric = 0.5 overlap.
  1391. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1392. // Smart floor of the position
  1393. Vector2 raw_pos = ret;
  1394. if (Math::posmod(Math::floor(ret.y), 2) ^ (tile_layout == TileSet::TILE_LAYOUT_STACKED_OFFSET)) {
  1395. ret = Vector2(Math::floor(ret.x + 0.5) - 0.5, Math::floor(ret.y));
  1396. } else {
  1397. ret = ret.floor();
  1398. }
  1399. // Compute the tile offset, and if we might the output for a neighbor top tile.
  1400. Vector2 in_tile_pos = raw_pos - ret;
  1401. bool in_top_left_triangle = (in_tile_pos - Vector2(0.5, 0.0)).cross(Vector2(-0.5, 1.0 / overlapping_ratio - 1)) <= 0;
  1402. bool in_top_right_triangle = (in_tile_pos - Vector2(0.5, 0.0)).cross(Vector2(0.5, 1.0 / overlapping_ratio - 1)) > 0;
  1403. switch (tile_layout) {
  1404. case TileSet::TILE_LAYOUT_STACKED:
  1405. ret = ret.floor();
  1406. if (in_top_left_triangle) {
  1407. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 0 : -1, -1);
  1408. } else if (in_top_right_triangle) {
  1409. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 1 : 0, -1);
  1410. }
  1411. break;
  1412. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  1413. ret = ret.floor();
  1414. if (in_top_left_triangle) {
  1415. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? -1 : 0, -1);
  1416. } else if (in_top_right_triangle) {
  1417. ret += Vector2i(Math::posmod(Math::floor(ret.y), 2) ? 0 : 1, -1);
  1418. }
  1419. break;
  1420. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  1421. ret = Vector2(ret.x - ret.y / 2, ret.y).floor();
  1422. if (in_top_left_triangle) {
  1423. ret += Vector2i(0, -1);
  1424. } else if (in_top_right_triangle) {
  1425. ret += Vector2i(1, -1);
  1426. }
  1427. break;
  1428. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  1429. ret = Vector2(ret.x * 2, ret.y / 2 - ret.x).floor();
  1430. if (in_top_left_triangle) {
  1431. ret += Vector2i(-1, 0);
  1432. } else if (in_top_right_triangle) {
  1433. ret += Vector2i(1, -1);
  1434. }
  1435. break;
  1436. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  1437. ret = Vector2(ret.x - ret.y / 2, ret.y / 2 + ret.x).floor();
  1438. if (in_top_left_triangle) {
  1439. ret += Vector2i(0, -1);
  1440. } else if (in_top_right_triangle) {
  1441. ret += Vector2i(1, 0);
  1442. }
  1443. break;
  1444. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  1445. ret = Vector2(ret.x + ret.y / 2, ret.y / 2 - ret.x).floor();
  1446. if (in_top_left_triangle) {
  1447. ret += Vector2i(-1, 0);
  1448. } else if (in_top_right_triangle) {
  1449. ret += Vector2i(0, -1);
  1450. }
  1451. break;
  1452. }
  1453. } else { // TILE_OFFSET_AXIS_VERTICAL.
  1454. // Smart floor of the position.
  1455. Vector2 raw_pos = ret;
  1456. if (Math::posmod(Math::floor(ret.x), 2) ^ (tile_layout == TileSet::TILE_LAYOUT_STACKED_OFFSET)) {
  1457. ret = Vector2(Math::floor(ret.x), Math::floor(ret.y + 0.5) - 0.5);
  1458. } else {
  1459. ret = ret.floor();
  1460. }
  1461. // Compute the tile offset, and if we might the output for a neighbor top tile.
  1462. Vector2 in_tile_pos = raw_pos - ret;
  1463. bool in_top_left_triangle = (in_tile_pos - Vector2(0.0, 0.5)).cross(Vector2(1.0 / overlapping_ratio - 1, -0.5)) > 0;
  1464. bool in_bottom_left_triangle = (in_tile_pos - Vector2(0.0, 0.5)).cross(Vector2(1.0 / overlapping_ratio - 1, 0.5)) <= 0;
  1465. switch (tile_layout) {
  1466. case TileSet::TILE_LAYOUT_STACKED:
  1467. ret = ret.floor();
  1468. if (in_top_left_triangle) {
  1469. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 0 : -1);
  1470. } else if (in_bottom_left_triangle) {
  1471. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 1 : 0);
  1472. }
  1473. break;
  1474. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  1475. ret = ret.floor();
  1476. if (in_top_left_triangle) {
  1477. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? -1 : 0);
  1478. } else if (in_bottom_left_triangle) {
  1479. ret += Vector2i(-1, Math::posmod(Math::floor(ret.x), 2) ? 0 : 1);
  1480. }
  1481. break;
  1482. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  1483. ret = Vector2(ret.x / 2 - ret.y, ret.y * 2).floor();
  1484. if (in_top_left_triangle) {
  1485. ret += Vector2i(0, -1);
  1486. } else if (in_bottom_left_triangle) {
  1487. ret += Vector2i(-1, 1);
  1488. }
  1489. break;
  1490. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  1491. ret = Vector2(ret.x, ret.y - ret.x / 2).floor();
  1492. if (in_top_left_triangle) {
  1493. ret += Vector2i(-1, 0);
  1494. } else if (in_bottom_left_triangle) {
  1495. ret += Vector2i(-1, 1);
  1496. }
  1497. break;
  1498. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  1499. ret = Vector2(ret.x / 2 - ret.y, ret.y + ret.x / 2).floor();
  1500. if (in_top_left_triangle) {
  1501. ret += Vector2i(0, -1);
  1502. } else if (in_bottom_left_triangle) {
  1503. ret += Vector2i(-1, 0);
  1504. }
  1505. break;
  1506. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  1507. ret = Vector2(ret.x / 2 + ret.y, ret.y - ret.x / 2).floor();
  1508. if (in_top_left_triangle) {
  1509. ret += Vector2i(-1, 0);
  1510. } else if (in_bottom_left_triangle) {
  1511. ret += Vector2i(0, 1);
  1512. }
  1513. break;
  1514. }
  1515. }
  1516. } else {
  1517. ret = (ret + Vector2(0.00005, 0.00005)).floor();
  1518. }
  1519. return Vector2i(ret);
  1520. }
  1521. bool TileSet::is_existing_neighbor(TileSet::CellNeighbor p_cell_neighbor) const {
  1522. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1523. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  1524. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  1525. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  1526. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  1527. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  1528. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  1529. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  1530. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER;
  1531. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1532. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  1533. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  1534. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  1535. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  1536. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  1537. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  1538. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER ||
  1539. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  1540. } else {
  1541. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1542. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  1543. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  1544. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  1545. p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  1546. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  1547. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  1548. } else {
  1549. return p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  1550. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  1551. p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  1552. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  1553. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  1554. p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE;
  1555. }
  1556. }
  1557. }
  1558. Vector2i TileSet::get_neighbor_cell(const Vector2i &p_coords, TileSet::CellNeighbor p_cell_neighbor) const {
  1559. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1560. switch (p_cell_neighbor) {
  1561. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  1562. return p_coords + Vector2i(1, 0);
  1563. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1564. return p_coords + Vector2i(1, 1);
  1565. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  1566. return p_coords + Vector2i(0, 1);
  1567. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1568. return p_coords + Vector2i(-1, 1);
  1569. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  1570. return p_coords + Vector2i(-1, 0);
  1571. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1572. return p_coords + Vector2i(-1, -1);
  1573. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  1574. return p_coords + Vector2i(0, -1);
  1575. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1576. return p_coords + Vector2i(1, -1);
  1577. default:
  1578. ERR_FAIL_V(p_coords);
  1579. }
  1580. } else { // Half-offset shapes (square and hexagon).
  1581. switch (tile_layout) {
  1582. case TileSet::TILE_LAYOUT_STACKED: {
  1583. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1584. bool is_offset = p_coords.y % 2;
  1585. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  1586. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  1587. return p_coords + Vector2i(1, 0);
  1588. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1589. return p_coords + Vector2i(is_offset ? 1 : 0, 1);
  1590. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  1591. return p_coords + Vector2i(0, 2);
  1592. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1593. return p_coords + Vector2i(is_offset ? 0 : -1, 1);
  1594. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  1595. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  1596. return p_coords + Vector2i(-1, 0);
  1597. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1598. return p_coords + Vector2i(is_offset ? 0 : -1, -1);
  1599. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  1600. return p_coords + Vector2i(0, -2);
  1601. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1602. return p_coords + Vector2i(is_offset ? 1 : 0, -1);
  1603. } else {
  1604. ERR_FAIL_V(p_coords);
  1605. }
  1606. } else {
  1607. bool is_offset = p_coords.x % 2;
  1608. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  1609. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  1610. return p_coords + Vector2i(0, 1);
  1611. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1612. return p_coords + Vector2i(1, is_offset ? 1 : 0);
  1613. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  1614. return p_coords + Vector2i(2, 0);
  1615. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1616. return p_coords + Vector2i(1, is_offset ? 0 : -1);
  1617. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  1618. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  1619. return p_coords + Vector2i(0, -1);
  1620. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1621. return p_coords + Vector2i(-1, is_offset ? 0 : -1);
  1622. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  1623. return p_coords + Vector2i(-2, 0);
  1624. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1625. return p_coords + Vector2i(-1, is_offset ? 1 : 0);
  1626. } else {
  1627. ERR_FAIL_V(p_coords);
  1628. }
  1629. }
  1630. } break;
  1631. case TileSet::TILE_LAYOUT_STACKED_OFFSET: {
  1632. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1633. bool is_offset = p_coords.y % 2;
  1634. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  1635. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  1636. return p_coords + Vector2i(1, 0);
  1637. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1638. return p_coords + Vector2i(is_offset ? 0 : 1, 1);
  1639. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  1640. return p_coords + Vector2i(0, 2);
  1641. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1642. return p_coords + Vector2i(is_offset ? -1 : 0, 1);
  1643. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  1644. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  1645. return p_coords + Vector2i(-1, 0);
  1646. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1647. return p_coords + Vector2i(is_offset ? -1 : 0, -1);
  1648. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  1649. return p_coords + Vector2i(0, -2);
  1650. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1651. return p_coords + Vector2i(is_offset ? 0 : 1, -1);
  1652. } else {
  1653. ERR_FAIL_V(p_coords);
  1654. }
  1655. } else {
  1656. bool is_offset = p_coords.x % 2;
  1657. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  1658. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  1659. return p_coords + Vector2i(0, 1);
  1660. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1661. return p_coords + Vector2i(1, is_offset ? 0 : 1);
  1662. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  1663. return p_coords + Vector2i(2, 0);
  1664. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1665. return p_coords + Vector2i(1, is_offset ? -1 : 0);
  1666. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  1667. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  1668. return p_coords + Vector2i(0, -1);
  1669. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1670. return p_coords + Vector2i(-1, is_offset ? -1 : 0);
  1671. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  1672. return p_coords + Vector2i(-2, 0);
  1673. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1674. return p_coords + Vector2i(-1, is_offset ? 0 : 1);
  1675. } else {
  1676. ERR_FAIL_V(p_coords);
  1677. }
  1678. }
  1679. } break;
  1680. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  1681. case TileSet::TILE_LAYOUT_STAIRS_DOWN: {
  1682. if ((tile_layout == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  1683. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1684. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  1685. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  1686. return p_coords + Vector2i(1, 0);
  1687. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1688. return p_coords + Vector2i(0, 1);
  1689. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  1690. return p_coords + Vector2i(-1, 2);
  1691. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1692. return p_coords + Vector2i(-1, 1);
  1693. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  1694. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  1695. return p_coords + Vector2i(-1, 0);
  1696. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1697. return p_coords + Vector2i(0, -1);
  1698. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  1699. return p_coords + Vector2i(1, -2);
  1700. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1701. return p_coords + Vector2i(1, -1);
  1702. } else {
  1703. ERR_FAIL_V(p_coords);
  1704. }
  1705. } else {
  1706. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  1707. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  1708. return p_coords + Vector2i(0, 1);
  1709. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1710. return p_coords + Vector2i(1, 0);
  1711. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  1712. return p_coords + Vector2i(2, -1);
  1713. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1714. return p_coords + Vector2i(1, -1);
  1715. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  1716. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  1717. return p_coords + Vector2i(0, -1);
  1718. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1719. return p_coords + Vector2i(-1, 0);
  1720. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  1721. return p_coords + Vector2i(-2, 1);
  1722. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1723. return p_coords + Vector2i(-1, 1);
  1724. } else {
  1725. ERR_FAIL_V(p_coords);
  1726. }
  1727. }
  1728. } else {
  1729. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1730. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  1731. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  1732. return p_coords + Vector2i(2, -1);
  1733. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1734. return p_coords + Vector2i(1, 0);
  1735. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  1736. return p_coords + Vector2i(0, 1);
  1737. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1738. return p_coords + Vector2i(-1, 1);
  1739. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  1740. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  1741. return p_coords + Vector2i(-2, 1);
  1742. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1743. return p_coords + Vector2i(-1, 0);
  1744. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  1745. return p_coords + Vector2i(0, -1);
  1746. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1747. return p_coords + Vector2i(1, -1);
  1748. } else {
  1749. ERR_FAIL_V(p_coords);
  1750. }
  1751. } else {
  1752. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  1753. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  1754. return p_coords + Vector2i(-1, 2);
  1755. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1756. return p_coords + Vector2i(0, 1);
  1757. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  1758. return p_coords + Vector2i(1, 0);
  1759. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1760. return p_coords + Vector2i(1, -1);
  1761. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  1762. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  1763. return p_coords + Vector2i(1, -2);
  1764. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1765. return p_coords + Vector2i(0, -1);
  1766. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  1767. return p_coords + Vector2i(-1, 0);
  1768. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1769. return p_coords + Vector2i(-1, 1);
  1770. } else {
  1771. ERR_FAIL_V(p_coords);
  1772. }
  1773. }
  1774. }
  1775. } break;
  1776. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  1777. case TileSet::TILE_LAYOUT_DIAMOND_DOWN: {
  1778. if ((tile_layout == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  1779. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1780. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  1781. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  1782. return p_coords + Vector2i(1, 1);
  1783. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1784. return p_coords + Vector2i(0, 1);
  1785. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  1786. return p_coords + Vector2i(-1, 1);
  1787. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1788. return p_coords + Vector2i(-1, 0);
  1789. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  1790. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  1791. return p_coords + Vector2i(-1, -1);
  1792. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1793. return p_coords + Vector2i(0, -1);
  1794. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  1795. return p_coords + Vector2i(1, -1);
  1796. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1797. return p_coords + Vector2i(1, 0);
  1798. } else {
  1799. ERR_FAIL_V(p_coords);
  1800. }
  1801. } else {
  1802. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  1803. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  1804. return p_coords + Vector2i(1, 1);
  1805. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1806. return p_coords + Vector2i(1, 0);
  1807. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  1808. return p_coords + Vector2i(1, -1);
  1809. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1810. return p_coords + Vector2i(0, -1);
  1811. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  1812. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  1813. return p_coords + Vector2i(-1, -1);
  1814. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1815. return p_coords + Vector2i(-1, 0);
  1816. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  1817. return p_coords + Vector2i(-1, 1);
  1818. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1819. return p_coords + Vector2i(0, 1);
  1820. } else {
  1821. ERR_FAIL_V(p_coords);
  1822. }
  1823. }
  1824. } else {
  1825. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1826. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) ||
  1827. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_SIDE)) {
  1828. return p_coords + Vector2i(1, -1);
  1829. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1830. return p_coords + Vector2i(1, 0);
  1831. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) {
  1832. return p_coords + Vector2i(1, 1);
  1833. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1834. return p_coords + Vector2i(0, 1);
  1835. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) ||
  1836. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_SIDE)) {
  1837. return p_coords + Vector2i(-1, 1);
  1838. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1839. return p_coords + Vector2i(-1, 0);
  1840. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  1841. return p_coords + Vector2i(-1, -1);
  1842. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1843. return p_coords + Vector2i(0, -1);
  1844. } else {
  1845. ERR_FAIL_V(p_coords);
  1846. }
  1847. } else {
  1848. if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER) ||
  1849. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE)) {
  1850. return p_coords + Vector2i(-1, 1);
  1851. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE) {
  1852. return p_coords + Vector2i(0, 1);
  1853. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_RIGHT_CORNER) {
  1854. return p_coords + Vector2i(1, 1);
  1855. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  1856. return p_coords + Vector2i(1, 0);
  1857. } else if ((tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_CORNER) ||
  1858. (tile_shape != TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_SIDE)) {
  1859. return p_coords + Vector2i(1, -1);
  1860. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE) {
  1861. return p_coords + Vector2i(0, -1);
  1862. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC && p_cell_neighbor == TileSet::CELL_NEIGHBOR_LEFT_CORNER) {
  1863. return p_coords + Vector2i(-1, -1);
  1864. } else if (p_cell_neighbor == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE) {
  1865. return p_coords + Vector2i(-1, 0);
  1866. } else {
  1867. ERR_FAIL_V(p_coords);
  1868. }
  1869. }
  1870. }
  1871. } break;
  1872. default:
  1873. ERR_FAIL_V(p_coords);
  1874. }
  1875. }
  1876. }
  1877. TypedArray<Vector2i> TileSet::get_surrounding_cells(const Vector2i &p_coords) const {
  1878. TypedArray<Vector2i> around;
  1879. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1880. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE));
  1881. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE));
  1882. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_LEFT_SIDE));
  1883. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_TOP_SIDE));
  1884. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1885. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  1886. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  1887. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  1888. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  1889. } else {
  1890. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1891. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_RIGHT_SIDE));
  1892. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  1893. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  1894. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_LEFT_SIDE));
  1895. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  1896. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  1897. } else {
  1898. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE));
  1899. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_BOTTOM_SIDE));
  1900. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE));
  1901. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE));
  1902. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_TOP_SIDE));
  1903. around.push_back(get_neighbor_cell(p_coords, TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE));
  1904. }
  1905. }
  1906. return around;
  1907. }
  1908. Vector2i TileSet::map_pattern(const Vector2i &p_position_in_tilemap, const Vector2i &p_coords_in_pattern, Ref<TileMapPattern> p_pattern) const {
  1909. ERR_FAIL_COND_V(p_pattern.is_null(), Vector2i());
  1910. ERR_FAIL_COND_V(!p_pattern->has_cell(p_coords_in_pattern), Vector2i());
  1911. Vector2i output = p_position_in_tilemap + p_coords_in_pattern;
  1912. if (tile_shape != TileSet::TILE_SHAPE_SQUARE) {
  1913. if (tile_layout == TileSet::TILE_LAYOUT_STACKED) {
  1914. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(p_position_in_tilemap.y % 2) && bool(p_coords_in_pattern.y % 2)) {
  1915. output.x += 1;
  1916. } else if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(p_position_in_tilemap.x % 2) && bool(p_coords_in_pattern.x % 2)) {
  1917. output.y += 1;
  1918. }
  1919. } else if (tile_layout == TileSet::TILE_LAYOUT_STACKED_OFFSET) {
  1920. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL && bool(p_position_in_tilemap.y % 2) && bool(p_coords_in_pattern.y % 2)) {
  1921. output.x -= 1;
  1922. } else if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL && bool(p_position_in_tilemap.x % 2) && bool(p_coords_in_pattern.x % 2)) {
  1923. output.y -= 1;
  1924. }
  1925. }
  1926. }
  1927. return output;
  1928. }
  1929. void TileSet::draw_cells_outline(CanvasItem *p_canvas_item, const RBSet<Vector2i> &p_cells, Color p_color, Transform2D p_transform) const {
  1930. Vector<Vector2> polygon = get_tile_shape_polygon();
  1931. for (const Vector2i &E : p_cells) {
  1932. Vector2 center = map_to_local(E);
  1933. #define DRAW_SIDE_IF_NEEDED(side, polygon_index_from, polygon_index_to) \
  1934. if (!p_cells.has(get_neighbor_cell(E, side))) { \
  1935. Vector2 from = p_transform.xform(center + polygon[polygon_index_from] * tile_size); \
  1936. Vector2 to = p_transform.xform(center + polygon[polygon_index_to] * tile_size); \
  1937. p_canvas_item->draw_line(from, to, p_color); \
  1938. }
  1939. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1940. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_RIGHT_SIDE, 1, 2);
  1941. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_SIDE, 2, 3);
  1942. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_LEFT_SIDE, 3, 0);
  1943. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_SIDE, 0, 1);
  1944. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1945. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 2, 3);
  1946. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 1, 2);
  1947. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  1948. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 3, 0);
  1949. } else {
  1950. if (tile_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1951. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  1952. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 2, 3);
  1953. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_LEFT_SIDE, 1, 2);
  1954. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  1955. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 5, 0);
  1956. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_RIGHT_SIDE, 4, 5);
  1957. } else {
  1958. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE, 3, 4);
  1959. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_SIDE, 4, 5);
  1960. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE, 5, 0);
  1961. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE, 0, 1);
  1962. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_SIDE, 1, 2);
  1963. DRAW_SIDE_IF_NEEDED(TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE, 2, 3);
  1964. }
  1965. }
  1966. }
  1967. #undef DRAW_SIDE_IF_NEEDED
  1968. }
  1969. Vector<Point2> TileSet::get_terrain_polygon(int p_terrain_set) {
  1970. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1971. return _get_square_terrain_polygon(tile_size);
  1972. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1973. return _get_isometric_terrain_polygon(tile_size);
  1974. } else {
  1975. float overlap = 0.0;
  1976. switch (tile_shape) {
  1977. case TileSet::TILE_SHAPE_HEXAGON:
  1978. overlap = 0.25;
  1979. break;
  1980. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1981. overlap = 0.0;
  1982. break;
  1983. default:
  1984. break;
  1985. }
  1986. return _get_half_offset_terrain_polygon(tile_size, overlap, tile_offset_axis);
  1987. }
  1988. }
  1989. Vector<Point2> TileSet::get_terrain_peering_bit_polygon(int p_terrain_set, TileSet::CellNeighbor p_bit) {
  1990. ERR_FAIL_COND_V(p_terrain_set < 0 || p_terrain_set >= get_terrain_sets_count(), Vector<Point2>());
  1991. TileSet::TerrainMode terrain_mode = get_terrain_set_mode(p_terrain_set);
  1992. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1993. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1994. return _get_square_corner_or_side_terrain_peering_bit_polygon(tile_size, p_bit);
  1995. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1996. return _get_square_corner_terrain_peering_bit_polygon(tile_size, p_bit);
  1997. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1998. return _get_square_side_terrain_peering_bit_polygon(tile_size, p_bit);
  1999. }
  2000. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2001. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  2002. return _get_isometric_corner_or_side_terrain_peering_bit_polygon(tile_size, p_bit);
  2003. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  2004. return _get_isometric_corner_terrain_peering_bit_polygon(tile_size, p_bit);
  2005. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  2006. return _get_isometric_side_terrain_peering_bit_polygon(tile_size, p_bit);
  2007. }
  2008. } else {
  2009. float overlap = 0.0;
  2010. switch (tile_shape) {
  2011. case TileSet::TILE_SHAPE_HEXAGON:
  2012. overlap = 0.25;
  2013. break;
  2014. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  2015. overlap = 0.0;
  2016. break;
  2017. default:
  2018. break;
  2019. }
  2020. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  2021. return _get_half_offset_corner_or_side_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, p_bit);
  2022. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  2023. return _get_half_offset_corner_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, p_bit);
  2024. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  2025. return _get_half_offset_side_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, p_bit);
  2026. }
  2027. }
  2028. }
  2029. #define TERRAIN_ALPHA 0.6
  2030. void TileSet::draw_terrains(CanvasItem *p_canvas_item, Transform2D p_transform, const TileData *p_tile_data) {
  2031. ERR_FAIL_NULL(p_tile_data);
  2032. if (terrain_bits_meshes_dirty) {
  2033. // Recompute the meshes.
  2034. terrain_peering_bits_meshes.clear();
  2035. for (int terrain_mode_index = 0; terrain_mode_index < 3; terrain_mode_index++) {
  2036. TerrainMode terrain_mode = TerrainMode(terrain_mode_index);
  2037. // Center terrain
  2038. Vector<Vector2> polygon;
  2039. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  2040. polygon = _get_square_terrain_polygon(tile_size);
  2041. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2042. polygon = _get_isometric_terrain_polygon(tile_size);
  2043. } else {
  2044. float overlap = 0.0;
  2045. switch (tile_shape) {
  2046. case TileSet::TILE_SHAPE_HEXAGON:
  2047. overlap = 0.25;
  2048. break;
  2049. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  2050. overlap = 0.0;
  2051. break;
  2052. default:
  2053. break;
  2054. }
  2055. polygon = _get_half_offset_terrain_polygon(tile_size, overlap, tile_offset_axis);
  2056. }
  2057. {
  2058. Ref<ArrayMesh> mesh;
  2059. mesh.instantiate();
  2060. Vector<Vector2> uvs;
  2061. uvs.resize(polygon.size());
  2062. Vector<Color> colors;
  2063. colors.resize(polygon.size());
  2064. colors.fill(Color(1.0, 1.0, 1.0, 1.0));
  2065. Array a;
  2066. a.resize(Mesh::ARRAY_MAX);
  2067. a[Mesh::ARRAY_VERTEX] = polygon;
  2068. a[Mesh::ARRAY_TEX_UV] = uvs;
  2069. a[Mesh::ARRAY_COLOR] = colors;
  2070. a[Mesh::ARRAY_INDEX] = Geometry2D::triangulate_polygon(polygon);
  2071. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  2072. terrain_meshes[terrain_mode] = mesh;
  2073. }
  2074. // Peering bits
  2075. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2076. CellNeighbor bit = CellNeighbor(i);
  2077. if (is_valid_terrain_peering_bit_for_mode(terrain_mode, bit)) {
  2078. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  2079. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  2080. polygon = _get_square_corner_or_side_terrain_peering_bit_polygon(tile_size, bit);
  2081. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  2082. polygon = _get_square_corner_terrain_peering_bit_polygon(tile_size, bit);
  2083. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  2084. polygon = _get_square_side_terrain_peering_bit_polygon(tile_size, bit);
  2085. }
  2086. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  2087. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  2088. polygon = _get_isometric_corner_or_side_terrain_peering_bit_polygon(tile_size, bit);
  2089. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  2090. polygon = _get_isometric_corner_terrain_peering_bit_polygon(tile_size, bit);
  2091. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  2092. polygon = _get_isometric_side_terrain_peering_bit_polygon(tile_size, bit);
  2093. }
  2094. } else {
  2095. float overlap = 0.0;
  2096. switch (tile_shape) {
  2097. case TileSet::TILE_SHAPE_HEXAGON:
  2098. overlap = 0.25;
  2099. break;
  2100. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  2101. overlap = 0.0;
  2102. break;
  2103. default:
  2104. break;
  2105. }
  2106. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  2107. polygon = _get_half_offset_corner_or_side_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, bit);
  2108. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  2109. polygon = _get_half_offset_corner_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, bit);
  2110. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  2111. polygon = _get_half_offset_side_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, bit);
  2112. }
  2113. }
  2114. {
  2115. Ref<ArrayMesh> mesh;
  2116. mesh.instantiate();
  2117. Vector<Vector2> uvs;
  2118. uvs.resize(polygon.size());
  2119. Vector<Color> colors;
  2120. colors.resize(polygon.size());
  2121. colors.fill(Color(1.0, 1.0, 1.0, 1.0));
  2122. Array a;
  2123. a.resize(Mesh::ARRAY_MAX);
  2124. a[Mesh::ARRAY_VERTEX] = polygon;
  2125. a[Mesh::ARRAY_TEX_UV] = uvs;
  2126. a[Mesh::ARRAY_COLOR] = colors;
  2127. a[Mesh::ARRAY_INDEX] = Geometry2D::triangulate_polygon(polygon);
  2128. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  2129. terrain_peering_bits_meshes[terrain_mode][bit] = mesh;
  2130. }
  2131. }
  2132. }
  2133. }
  2134. terrain_bits_meshes_dirty = false;
  2135. }
  2136. int terrain_set = p_tile_data->get_terrain_set();
  2137. if (terrain_set < 0) {
  2138. return;
  2139. }
  2140. TileSet::TerrainMode terrain_mode = get_terrain_set_mode(terrain_set);
  2141. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), p_transform);
  2142. int terrain_id = p_tile_data->get_terrain();
  2143. if (terrain_id >= 0) {
  2144. Color color = get_terrain_color(terrain_set, terrain_id);
  2145. color.a = TERRAIN_ALPHA;
  2146. p_canvas_item->draw_mesh(terrain_meshes[terrain_mode], Ref<Texture2D>(), Transform2D(), color);
  2147. }
  2148. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2149. CellNeighbor bit = CellNeighbor(i);
  2150. if (is_valid_terrain_peering_bit(terrain_set, bit)) {
  2151. terrain_id = p_tile_data->get_terrain_peering_bit(bit);
  2152. if (terrain_id >= 0) {
  2153. Color color = get_terrain_color(terrain_set, terrain_id);
  2154. color.a = TERRAIN_ALPHA;
  2155. p_canvas_item->draw_mesh(terrain_peering_bits_meshes[terrain_mode][bit], Ref<Texture2D>(), Transform2D(), color);
  2156. }
  2157. }
  2158. }
  2159. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), Transform2D());
  2160. }
  2161. Vector<Vector<Ref<Texture2D>>> TileSet::generate_terrains_icons(Size2i p_size) {
  2162. // Counts the number of matching terrain tiles and find the best matching icon.
  2163. struct Count {
  2164. int count = 0;
  2165. float probability = 0.0;
  2166. Ref<Texture2D> texture;
  2167. Rect2i region;
  2168. };
  2169. Vector<Vector<Ref<Texture2D>>> output;
  2170. LocalVector<LocalVector<Count>> counts;
  2171. output.resize(get_terrain_sets_count());
  2172. counts.resize(get_terrain_sets_count());
  2173. for (int terrain_set = 0; terrain_set < get_terrain_sets_count(); terrain_set++) {
  2174. output.write[terrain_set].resize(get_terrains_count(terrain_set));
  2175. counts[terrain_set].resize(get_terrains_count(terrain_set));
  2176. }
  2177. for (int source_index = 0; source_index < get_source_count(); source_index++) {
  2178. int source_id = get_source_id(source_index);
  2179. Ref<TileSetSource> source = get_source(source_id);
  2180. Ref<TileSetAtlasSource> atlas_source = source;
  2181. if (atlas_source.is_valid()) {
  2182. for (int tile_index = 0; tile_index < source->get_tiles_count(); tile_index++) {
  2183. Vector2i tile_id = source->get_tile_id(tile_index);
  2184. for (int alternative_index = 0; alternative_index < source->get_alternative_tiles_count(tile_id); alternative_index++) {
  2185. int alternative_id = source->get_alternative_tile_id(tile_id, alternative_index);
  2186. TileData *tile_data = atlas_source->get_tile_data(tile_id, alternative_id);
  2187. int terrain_set = tile_data->get_terrain_set();
  2188. if (terrain_set >= 0) {
  2189. ERR_FAIL_INDEX_V(terrain_set, get_terrain_sets_count(), Vector<Vector<Ref<Texture2D>>>());
  2190. LocalVector<int> bit_counts;
  2191. bit_counts.resize(get_terrains_count(terrain_set));
  2192. for (int terrain = 0; terrain < get_terrains_count(terrain_set); terrain++) {
  2193. bit_counts[terrain] = 0;
  2194. }
  2195. if (tile_data->get_terrain() >= 0) {
  2196. bit_counts[tile_data->get_terrain()] += 10;
  2197. }
  2198. for (int terrain_bit = 0; terrain_bit < TileSet::CELL_NEIGHBOR_MAX; terrain_bit++) {
  2199. TileSet::CellNeighbor cell_neighbor = TileSet::CellNeighbor(terrain_bit);
  2200. if (is_valid_terrain_peering_bit(terrain_set, cell_neighbor)) {
  2201. int terrain = tile_data->get_terrain_peering_bit(cell_neighbor);
  2202. if (terrain >= 0) {
  2203. if (terrain >= (int)bit_counts.size()) {
  2204. WARN_PRINT(vformat("Invalid terrain peering bit: %d", terrain));
  2205. } else {
  2206. bit_counts[terrain] += 1;
  2207. }
  2208. }
  2209. }
  2210. }
  2211. for (int terrain = 0; terrain < get_terrains_count(terrain_set); terrain++) {
  2212. 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)) {
  2213. counts[terrain_set][terrain].count = bit_counts[terrain];
  2214. counts[terrain_set][terrain].probability = tile_data->get_probability();
  2215. counts[terrain_set][terrain].texture = atlas_source->get_texture();
  2216. counts[terrain_set][terrain].region = atlas_source->get_tile_texture_region(tile_id);
  2217. }
  2218. }
  2219. }
  2220. }
  2221. }
  2222. }
  2223. }
  2224. // Generate the icons.
  2225. for (int terrain_set = 0; terrain_set < get_terrain_sets_count(); terrain_set++) {
  2226. for (int terrain = 0; terrain < get_terrains_count(terrain_set); terrain++) {
  2227. Ref<Image> dst_image;
  2228. dst_image.instantiate();
  2229. if (counts[terrain_set][terrain].count > 0) {
  2230. // Get the best tile.
  2231. Ref<Texture2D> src_texture = counts[terrain_set][terrain].texture;
  2232. ERR_FAIL_COND_V(src_texture.is_null(), output);
  2233. Ref<Image> src_image = src_texture->get_image();
  2234. ERR_FAIL_COND_V(src_image.is_null(), output);
  2235. Rect2i region = counts[terrain_set][terrain].region;
  2236. dst_image->initialize_data(region.size.x, region.size.y, false, src_image->get_format());
  2237. dst_image->blit_rect(src_image, region, Point2i());
  2238. dst_image->convert(Image::FORMAT_RGBA8);
  2239. dst_image->resize(p_size.x, p_size.y, Image::INTERPOLATE_NEAREST);
  2240. } else {
  2241. dst_image->initialize_data(1, 1, false, Image::FORMAT_RGBA8);
  2242. dst_image->set_pixel(0, 0, get_terrain_color(terrain_set, terrain));
  2243. }
  2244. Ref<ImageTexture> icon = ImageTexture::create_from_image(dst_image);
  2245. icon->set_size_override(p_size);
  2246. output.write[terrain_set].write[terrain] = icon;
  2247. }
  2248. }
  2249. return output;
  2250. }
  2251. void TileSet::_source_changed() {
  2252. terrains_cache_dirty = true;
  2253. emit_changed();
  2254. }
  2255. Vector<Point2> TileSet::_get_square_terrain_polygon(Vector2i p_size) {
  2256. Rect2 rect(-Vector2(p_size) / 6.0, Vector2(p_size) / 3.0);
  2257. return {
  2258. rect.position,
  2259. Vector2(rect.get_end().x, rect.position.y),
  2260. rect.get_end(),
  2261. Vector2(rect.position.x, rect.get_end().y)
  2262. };
  2263. }
  2264. Vector<Point2> TileSet::_get_square_corner_or_side_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  2265. Rect2 bit_rect;
  2266. bit_rect.size = Vector2(p_size) / 3;
  2267. switch (p_bit) {
  2268. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2269. bit_rect.position = Vector2(1, -1);
  2270. break;
  2271. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2272. bit_rect.position = Vector2(1, 1);
  2273. break;
  2274. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2275. bit_rect.position = Vector2(-1, 1);
  2276. break;
  2277. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2278. bit_rect.position = Vector2(-3, 1);
  2279. break;
  2280. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2281. bit_rect.position = Vector2(-3, -1);
  2282. break;
  2283. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2284. bit_rect.position = Vector2(-3, -3);
  2285. break;
  2286. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2287. bit_rect.position = Vector2(-1, -3);
  2288. break;
  2289. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2290. bit_rect.position = Vector2(1, -3);
  2291. break;
  2292. default:
  2293. break;
  2294. }
  2295. bit_rect.position *= Vector2(p_size) / 6.0;
  2296. Vector<Vector2> polygon = {
  2297. bit_rect.position,
  2298. Vector2(bit_rect.get_end().x, bit_rect.position.y),
  2299. bit_rect.get_end(),
  2300. Vector2(bit_rect.position.x, bit_rect.get_end().y)
  2301. };
  2302. return polygon;
  2303. }
  2304. Vector<Point2> TileSet::_get_square_corner_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  2305. Vector2 unit = Vector2(p_size) / 6.0;
  2306. Vector<Vector2> polygon;
  2307. switch (p_bit) {
  2308. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2309. polygon.push_back(Vector2(0, 3) * unit);
  2310. polygon.push_back(Vector2(3, 3) * unit);
  2311. polygon.push_back(Vector2(3, 0) * unit);
  2312. polygon.push_back(Vector2(1, 0) * unit);
  2313. polygon.push_back(Vector2(1, 1) * unit);
  2314. polygon.push_back(Vector2(0, 1) * unit);
  2315. break;
  2316. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2317. polygon.push_back(Vector2(0, 3) * unit);
  2318. polygon.push_back(Vector2(-3, 3) * unit);
  2319. polygon.push_back(Vector2(-3, 0) * unit);
  2320. polygon.push_back(Vector2(-1, 0) * unit);
  2321. polygon.push_back(Vector2(-1, 1) * unit);
  2322. polygon.push_back(Vector2(0, 1) * unit);
  2323. break;
  2324. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2325. polygon.push_back(Vector2(0, -3) * unit);
  2326. polygon.push_back(Vector2(-3, -3) * unit);
  2327. polygon.push_back(Vector2(-3, 0) * unit);
  2328. polygon.push_back(Vector2(-1, 0) * unit);
  2329. polygon.push_back(Vector2(-1, -1) * unit);
  2330. polygon.push_back(Vector2(0, -1) * unit);
  2331. break;
  2332. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2333. polygon.push_back(Vector2(0, -3) * unit);
  2334. polygon.push_back(Vector2(3, -3) * unit);
  2335. polygon.push_back(Vector2(3, 0) * unit);
  2336. polygon.push_back(Vector2(1, 0) * unit);
  2337. polygon.push_back(Vector2(1, -1) * unit);
  2338. polygon.push_back(Vector2(0, -1) * unit);
  2339. break;
  2340. default:
  2341. break;
  2342. }
  2343. return polygon;
  2344. }
  2345. Vector<Point2> TileSet::_get_square_side_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  2346. Vector2 unit = Vector2(p_size) / 6.0;
  2347. Vector<Vector2> polygon;
  2348. switch (p_bit) {
  2349. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2350. polygon.push_back(Vector2(1, -1) * unit);
  2351. polygon.push_back(Vector2(3, -3) * unit);
  2352. polygon.push_back(Vector2(3, 3) * unit);
  2353. polygon.push_back(Vector2(1, 1) * unit);
  2354. break;
  2355. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2356. polygon.push_back(Vector2(-1, 1) * unit);
  2357. polygon.push_back(Vector2(-3, 3) * unit);
  2358. polygon.push_back(Vector2(3, 3) * unit);
  2359. polygon.push_back(Vector2(1, 1) * unit);
  2360. break;
  2361. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2362. polygon.push_back(Vector2(-1, -1) * unit);
  2363. polygon.push_back(Vector2(-3, -3) * unit);
  2364. polygon.push_back(Vector2(-3, 3) * unit);
  2365. polygon.push_back(Vector2(-1, 1) * unit);
  2366. break;
  2367. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2368. polygon.push_back(Vector2(-1, -1) * unit);
  2369. polygon.push_back(Vector2(-3, -3) * unit);
  2370. polygon.push_back(Vector2(3, -3) * unit);
  2371. polygon.push_back(Vector2(1, -1) * unit);
  2372. break;
  2373. default:
  2374. break;
  2375. }
  2376. return polygon;
  2377. }
  2378. Vector<Point2> TileSet::_get_isometric_terrain_polygon(Vector2i p_size) {
  2379. Vector2 unit = Vector2(p_size) / 6.0;
  2380. return {
  2381. Vector2(1, 0) * unit,
  2382. Vector2(0, 1) * unit,
  2383. Vector2(-1, 0) * unit,
  2384. Vector2(0, -1) * unit,
  2385. };
  2386. }
  2387. Vector<Point2> TileSet::_get_isometric_corner_or_side_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  2388. Vector2 unit = Vector2(p_size) / 6.0;
  2389. Vector<Vector2> polygon;
  2390. switch (p_bit) {
  2391. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2392. polygon.push_back(Vector2(1, 0) * unit);
  2393. polygon.push_back(Vector2(2, -1) * unit);
  2394. polygon.push_back(Vector2(3, 0) * unit);
  2395. polygon.push_back(Vector2(2, 1) * unit);
  2396. break;
  2397. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2398. polygon.push_back(Vector2(0, 1) * unit);
  2399. polygon.push_back(Vector2(1, 2) * unit);
  2400. polygon.push_back(Vector2(2, 1) * unit);
  2401. polygon.push_back(Vector2(1, 0) * unit);
  2402. break;
  2403. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2404. polygon.push_back(Vector2(0, 1) * unit);
  2405. polygon.push_back(Vector2(-1, 2) * unit);
  2406. polygon.push_back(Vector2(0, 3) * unit);
  2407. polygon.push_back(Vector2(1, 2) * unit);
  2408. break;
  2409. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2410. polygon.push_back(Vector2(0, 1) * unit);
  2411. polygon.push_back(Vector2(-1, 2) * unit);
  2412. polygon.push_back(Vector2(-2, 1) * unit);
  2413. polygon.push_back(Vector2(-1, 0) * unit);
  2414. break;
  2415. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2416. polygon.push_back(Vector2(-1, 0) * unit);
  2417. polygon.push_back(Vector2(-2, -1) * unit);
  2418. polygon.push_back(Vector2(-3, 0) * unit);
  2419. polygon.push_back(Vector2(-2, 1) * unit);
  2420. break;
  2421. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2422. polygon.push_back(Vector2(0, -1) * unit);
  2423. polygon.push_back(Vector2(-1, -2) * unit);
  2424. polygon.push_back(Vector2(-2, -1) * unit);
  2425. polygon.push_back(Vector2(-1, 0) * unit);
  2426. break;
  2427. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2428. polygon.push_back(Vector2(0, -1) * unit);
  2429. polygon.push_back(Vector2(-1, -2) * unit);
  2430. polygon.push_back(Vector2(0, -3) * unit);
  2431. polygon.push_back(Vector2(1, -2) * unit);
  2432. break;
  2433. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2434. polygon.push_back(Vector2(0, -1) * unit);
  2435. polygon.push_back(Vector2(1, -2) * unit);
  2436. polygon.push_back(Vector2(2, -1) * unit);
  2437. polygon.push_back(Vector2(1, 0) * unit);
  2438. break;
  2439. default:
  2440. break;
  2441. }
  2442. return polygon;
  2443. }
  2444. Vector<Point2> TileSet::_get_isometric_corner_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  2445. Vector2 unit = Vector2(p_size) / 6.0;
  2446. Vector<Vector2> polygon;
  2447. switch (p_bit) {
  2448. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2449. polygon.push_back(Vector2(0.5, -0.5) * unit);
  2450. polygon.push_back(Vector2(1.5, -1.5) * unit);
  2451. polygon.push_back(Vector2(3, 0) * unit);
  2452. polygon.push_back(Vector2(1.5, 1.5) * unit);
  2453. polygon.push_back(Vector2(0.5, 0.5) * unit);
  2454. polygon.push_back(Vector2(1, 0) * unit);
  2455. break;
  2456. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2457. polygon.push_back(Vector2(-0.5, 0.5) * unit);
  2458. polygon.push_back(Vector2(-1.5, 1.5) * unit);
  2459. polygon.push_back(Vector2(0, 3) * unit);
  2460. polygon.push_back(Vector2(1.5, 1.5) * unit);
  2461. polygon.push_back(Vector2(0.5, 0.5) * unit);
  2462. polygon.push_back(Vector2(0, 1) * unit);
  2463. break;
  2464. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2465. polygon.push_back(Vector2(-0.5, -0.5) * unit);
  2466. polygon.push_back(Vector2(-1.5, -1.5) * unit);
  2467. polygon.push_back(Vector2(-3, 0) * unit);
  2468. polygon.push_back(Vector2(-1.5, 1.5) * unit);
  2469. polygon.push_back(Vector2(-0.5, 0.5) * unit);
  2470. polygon.push_back(Vector2(-1, 0) * unit);
  2471. break;
  2472. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2473. polygon.push_back(Vector2(-0.5, -0.5) * unit);
  2474. polygon.push_back(Vector2(-1.5, -1.5) * unit);
  2475. polygon.push_back(Vector2(0, -3) * unit);
  2476. polygon.push_back(Vector2(1.5, -1.5) * unit);
  2477. polygon.push_back(Vector2(0.5, -0.5) * unit);
  2478. polygon.push_back(Vector2(0, -1) * unit);
  2479. break;
  2480. default:
  2481. break;
  2482. }
  2483. return polygon;
  2484. }
  2485. Vector<Point2> TileSet::_get_isometric_side_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  2486. Vector2 unit = Vector2(p_size) / 6.0;
  2487. Vector<Vector2> polygon;
  2488. switch (p_bit) {
  2489. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2490. polygon.push_back(Vector2(1, 0) * unit);
  2491. polygon.push_back(Vector2(3, 0) * unit);
  2492. polygon.push_back(Vector2(0, 3) * unit);
  2493. polygon.push_back(Vector2(0, 1) * unit);
  2494. break;
  2495. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2496. polygon.push_back(Vector2(-1, 0) * unit);
  2497. polygon.push_back(Vector2(-3, 0) * unit);
  2498. polygon.push_back(Vector2(0, 3) * unit);
  2499. polygon.push_back(Vector2(0, 1) * unit);
  2500. break;
  2501. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2502. polygon.push_back(Vector2(-1, 0) * unit);
  2503. polygon.push_back(Vector2(-3, 0) * unit);
  2504. polygon.push_back(Vector2(0, -3) * unit);
  2505. polygon.push_back(Vector2(0, -1) * unit);
  2506. break;
  2507. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2508. polygon.push_back(Vector2(1, 0) * unit);
  2509. polygon.push_back(Vector2(3, 0) * unit);
  2510. polygon.push_back(Vector2(0, -3) * unit);
  2511. polygon.push_back(Vector2(0, -1) * unit);
  2512. break;
  2513. default:
  2514. break;
  2515. }
  2516. return polygon;
  2517. }
  2518. Vector<Point2> TileSet::_get_half_offset_terrain_polygon(Vector2i p_size, float p_overlap, TileSet::TileOffsetAxis p_offset_axis) {
  2519. Vector2 unit = Vector2(p_size) / 6.0;
  2520. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2521. return {
  2522. Vector2(1, 1.0 - p_overlap * 2.0) * unit,
  2523. Vector2(0, 1) * unit,
  2524. Vector2(-1, 1.0 - p_overlap * 2.0) * unit,
  2525. Vector2(-1, -1.0 + p_overlap * 2.0) * unit,
  2526. Vector2(0, -1) * unit,
  2527. Vector2(1, -1.0 + p_overlap * 2.0) * unit,
  2528. };
  2529. } else {
  2530. return {
  2531. Vector2(1, 0) * unit,
  2532. Vector2(1.0 - p_overlap * 2.0, -1) * unit,
  2533. Vector2(-1.0 + p_overlap * 2.0, -1) * unit,
  2534. Vector2(-1, 0) * unit,
  2535. Vector2(-1.0 + p_overlap * 2.0, 1) * unit,
  2536. Vector2(1.0 - p_overlap * 2.0, 1) * unit,
  2537. };
  2538. }
  2539. }
  2540. 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) {
  2541. Vector<Vector2> point_list = {
  2542. Vector2(3, (3.0 * (1.0 - p_overlap * 2.0)) / 2.0),
  2543. Vector2(3, 3.0 * (1.0 - p_overlap * 2.0)),
  2544. Vector2(2, 3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  2545. Vector2(1, 3.0 - p_overlap * 2.0),
  2546. Vector2(0, 3),
  2547. Vector2(-1, 3.0 - p_overlap * 2.0),
  2548. Vector2(-2, 3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  2549. Vector2(-3, 3.0 * (1.0 - p_overlap * 2.0)),
  2550. Vector2(-3, (3.0 * (1.0 - p_overlap * 2.0)) / 2.0),
  2551. Vector2(-3, -(3.0 * (1.0 - p_overlap * 2.0)) / 2.0),
  2552. Vector2(-3, -3.0 * (1.0 - p_overlap * 2.0)),
  2553. Vector2(-2, -3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  2554. Vector2(-1, -(3.0 - p_overlap * 2.0)),
  2555. Vector2(0, -3),
  2556. Vector2(1, -(3.0 - p_overlap * 2.0)),
  2557. Vector2(2, -3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  2558. Vector2(3, -3.0 * (1.0 - p_overlap * 2.0)),
  2559. Vector2(3, -(3.0 * (1.0 - p_overlap * 2.0)) / 2.0)
  2560. };
  2561. Vector2 unit = Vector2(p_size) / 6.0;
  2562. Vector<Vector2> polygon;
  2563. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2564. for (int i = 0; i < point_list.size(); i++) {
  2565. point_list.write[i] = point_list[i] * unit;
  2566. }
  2567. switch (p_bit) {
  2568. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2569. polygon.push_back(point_list[17]);
  2570. polygon.push_back(point_list[0]);
  2571. break;
  2572. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2573. polygon.push_back(point_list[0]);
  2574. polygon.push_back(point_list[1]);
  2575. polygon.push_back(point_list[2]);
  2576. break;
  2577. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2578. polygon.push_back(point_list[2]);
  2579. polygon.push_back(point_list[3]);
  2580. break;
  2581. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2582. polygon.push_back(point_list[3]);
  2583. polygon.push_back(point_list[4]);
  2584. polygon.push_back(point_list[5]);
  2585. break;
  2586. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2587. polygon.push_back(point_list[5]);
  2588. polygon.push_back(point_list[6]);
  2589. break;
  2590. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2591. polygon.push_back(point_list[6]);
  2592. polygon.push_back(point_list[7]);
  2593. polygon.push_back(point_list[8]);
  2594. break;
  2595. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2596. polygon.push_back(point_list[8]);
  2597. polygon.push_back(point_list[9]);
  2598. break;
  2599. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2600. polygon.push_back(point_list[9]);
  2601. polygon.push_back(point_list[10]);
  2602. polygon.push_back(point_list[11]);
  2603. break;
  2604. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2605. polygon.push_back(point_list[11]);
  2606. polygon.push_back(point_list[12]);
  2607. break;
  2608. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2609. polygon.push_back(point_list[12]);
  2610. polygon.push_back(point_list[13]);
  2611. polygon.push_back(point_list[14]);
  2612. break;
  2613. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2614. polygon.push_back(point_list[14]);
  2615. polygon.push_back(point_list[15]);
  2616. break;
  2617. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2618. polygon.push_back(point_list[15]);
  2619. polygon.push_back(point_list[16]);
  2620. polygon.push_back(point_list[17]);
  2621. break;
  2622. default:
  2623. break;
  2624. }
  2625. } else {
  2626. for (int i = 0; i < point_list.size(); i++) {
  2627. point_list.write[i] = Vector2(point_list[i].y, point_list[i].x) * unit;
  2628. }
  2629. switch (p_bit) {
  2630. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2631. polygon.push_back(point_list[3]);
  2632. polygon.push_back(point_list[4]);
  2633. polygon.push_back(point_list[5]);
  2634. break;
  2635. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2636. polygon.push_back(point_list[2]);
  2637. polygon.push_back(point_list[3]);
  2638. break;
  2639. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2640. polygon.push_back(point_list[0]);
  2641. polygon.push_back(point_list[1]);
  2642. polygon.push_back(point_list[2]);
  2643. break;
  2644. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2645. polygon.push_back(point_list[17]);
  2646. polygon.push_back(point_list[0]);
  2647. break;
  2648. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2649. polygon.push_back(point_list[15]);
  2650. polygon.push_back(point_list[16]);
  2651. polygon.push_back(point_list[17]);
  2652. break;
  2653. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2654. polygon.push_back(point_list[14]);
  2655. polygon.push_back(point_list[15]);
  2656. break;
  2657. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2658. polygon.push_back(point_list[12]);
  2659. polygon.push_back(point_list[13]);
  2660. polygon.push_back(point_list[14]);
  2661. break;
  2662. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2663. polygon.push_back(point_list[11]);
  2664. polygon.push_back(point_list[12]);
  2665. break;
  2666. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2667. polygon.push_back(point_list[9]);
  2668. polygon.push_back(point_list[10]);
  2669. polygon.push_back(point_list[11]);
  2670. break;
  2671. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2672. polygon.push_back(point_list[8]);
  2673. polygon.push_back(point_list[9]);
  2674. break;
  2675. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2676. polygon.push_back(point_list[6]);
  2677. polygon.push_back(point_list[7]);
  2678. polygon.push_back(point_list[8]);
  2679. break;
  2680. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2681. polygon.push_back(point_list[5]);
  2682. polygon.push_back(point_list[6]);
  2683. break;
  2684. default:
  2685. break;
  2686. }
  2687. }
  2688. int half_polygon_size = polygon.size();
  2689. for (int i = 0; i < half_polygon_size; i++) {
  2690. polygon.push_back(polygon[half_polygon_size - 1 - i] / 3.0);
  2691. }
  2692. return polygon;
  2693. }
  2694. 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) {
  2695. Vector<Vector2> point_list = {
  2696. Vector2(3, 0),
  2697. Vector2(3, 3.0 * (1.0 - p_overlap * 2.0)),
  2698. Vector2(1.5, (3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  2699. Vector2(0, 3),
  2700. Vector2(-1.5, (3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  2701. Vector2(-3, 3.0 * (1.0 - p_overlap * 2.0)),
  2702. Vector2(-3, 0),
  2703. Vector2(-3, -3.0 * (1.0 - p_overlap * 2.0)),
  2704. Vector2(-1.5, -(3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  2705. Vector2(0, -3),
  2706. Vector2(1.5, -(3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  2707. Vector2(3, -3.0 * (1.0 - p_overlap * 2.0))
  2708. };
  2709. Vector2 unit = Vector2(p_size) / 6.0;
  2710. Vector<Vector2> polygon;
  2711. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2712. for (int i = 0; i < point_list.size(); i++) {
  2713. point_list.write[i] = point_list[i] * unit;
  2714. }
  2715. switch (p_bit) {
  2716. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2717. polygon.push_back(point_list[0]);
  2718. polygon.push_back(point_list[1]);
  2719. polygon.push_back(point_list[2]);
  2720. break;
  2721. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2722. polygon.push_back(point_list[2]);
  2723. polygon.push_back(point_list[3]);
  2724. polygon.push_back(point_list[4]);
  2725. break;
  2726. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2727. polygon.push_back(point_list[4]);
  2728. polygon.push_back(point_list[5]);
  2729. polygon.push_back(point_list[6]);
  2730. break;
  2731. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2732. polygon.push_back(point_list[6]);
  2733. polygon.push_back(point_list[7]);
  2734. polygon.push_back(point_list[8]);
  2735. break;
  2736. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2737. polygon.push_back(point_list[8]);
  2738. polygon.push_back(point_list[9]);
  2739. polygon.push_back(point_list[10]);
  2740. break;
  2741. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2742. polygon.push_back(point_list[10]);
  2743. polygon.push_back(point_list[11]);
  2744. polygon.push_back(point_list[0]);
  2745. break;
  2746. default:
  2747. break;
  2748. }
  2749. } else {
  2750. for (int i = 0; i < point_list.size(); i++) {
  2751. point_list.write[i] = Vector2(point_list[i].y, point_list[i].x) * unit;
  2752. }
  2753. switch (p_bit) {
  2754. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2755. polygon.push_back(point_list[2]);
  2756. polygon.push_back(point_list[3]);
  2757. polygon.push_back(point_list[4]);
  2758. break;
  2759. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2760. polygon.push_back(point_list[0]);
  2761. polygon.push_back(point_list[1]);
  2762. polygon.push_back(point_list[2]);
  2763. break;
  2764. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2765. polygon.push_back(point_list[10]);
  2766. polygon.push_back(point_list[11]);
  2767. polygon.push_back(point_list[0]);
  2768. break;
  2769. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2770. polygon.push_back(point_list[8]);
  2771. polygon.push_back(point_list[9]);
  2772. polygon.push_back(point_list[10]);
  2773. break;
  2774. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2775. polygon.push_back(point_list[6]);
  2776. polygon.push_back(point_list[7]);
  2777. polygon.push_back(point_list[8]);
  2778. break;
  2779. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2780. polygon.push_back(point_list[4]);
  2781. polygon.push_back(point_list[5]);
  2782. polygon.push_back(point_list[6]);
  2783. break;
  2784. default:
  2785. break;
  2786. }
  2787. }
  2788. int half_polygon_size = polygon.size();
  2789. for (int i = 0; i < half_polygon_size; i++) {
  2790. polygon.push_back(polygon[half_polygon_size - 1 - i] / 3.0);
  2791. }
  2792. return polygon;
  2793. }
  2794. 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) {
  2795. Vector<Vector2> point_list = {
  2796. Vector2(3, 3.0 * (1.0 - p_overlap * 2.0)),
  2797. Vector2(0, 3),
  2798. Vector2(-3, 3.0 * (1.0 - p_overlap * 2.0)),
  2799. Vector2(-3, -3.0 * (1.0 - p_overlap * 2.0)),
  2800. Vector2(0, -3),
  2801. Vector2(3, -3.0 * (1.0 - p_overlap * 2.0))
  2802. };
  2803. Vector2 unit = Vector2(p_size) / 6.0;
  2804. Vector<Vector2> polygon;
  2805. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2806. for (int i = 0; i < point_list.size(); i++) {
  2807. point_list.write[i] = point_list[i] * unit;
  2808. }
  2809. switch (p_bit) {
  2810. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2811. polygon.push_back(point_list[5]);
  2812. polygon.push_back(point_list[0]);
  2813. break;
  2814. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2815. polygon.push_back(point_list[0]);
  2816. polygon.push_back(point_list[1]);
  2817. break;
  2818. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2819. polygon.push_back(point_list[1]);
  2820. polygon.push_back(point_list[2]);
  2821. break;
  2822. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2823. polygon.push_back(point_list[2]);
  2824. polygon.push_back(point_list[3]);
  2825. break;
  2826. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2827. polygon.push_back(point_list[3]);
  2828. polygon.push_back(point_list[4]);
  2829. break;
  2830. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2831. polygon.push_back(point_list[4]);
  2832. polygon.push_back(point_list[5]);
  2833. break;
  2834. default:
  2835. break;
  2836. }
  2837. } else {
  2838. for (int i = 0; i < point_list.size(); i++) {
  2839. point_list.write[i] = Vector2(point_list[i].y, point_list[i].x) * unit;
  2840. }
  2841. switch (p_bit) {
  2842. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2843. polygon.push_back(point_list[0]);
  2844. polygon.push_back(point_list[1]);
  2845. break;
  2846. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2847. polygon.push_back(point_list[5]);
  2848. polygon.push_back(point_list[0]);
  2849. break;
  2850. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2851. polygon.push_back(point_list[4]);
  2852. polygon.push_back(point_list[5]);
  2853. break;
  2854. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2855. polygon.push_back(point_list[3]);
  2856. polygon.push_back(point_list[4]);
  2857. break;
  2858. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2859. polygon.push_back(point_list[2]);
  2860. polygon.push_back(point_list[3]);
  2861. break;
  2862. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2863. polygon.push_back(point_list[1]);
  2864. polygon.push_back(point_list[2]);
  2865. break;
  2866. default:
  2867. break;
  2868. }
  2869. }
  2870. int half_polygon_size = polygon.size();
  2871. for (int i = 0; i < half_polygon_size; i++) {
  2872. polygon.push_back(polygon[half_polygon_size - 1 - i] / 3.0);
  2873. }
  2874. return polygon;
  2875. }
  2876. void TileSet::reset_state() {
  2877. // Rendering
  2878. occlusion_layers.clear();
  2879. tile_lines_mesh.instantiate();
  2880. tile_filled_mesh.instantiate();
  2881. tile_meshes_dirty = true;
  2882. #ifndef PHYSICS_2D_DISABLED
  2883. // Physics
  2884. physics_layers.clear();
  2885. #endif // PHYSICS_2D_DISABLED
  2886. // Terrains
  2887. terrain_sets.clear();
  2888. terrain_meshes.clear();
  2889. terrain_peering_bits_meshes.clear();
  2890. per_terrain_pattern_tiles.clear();
  2891. terrains_cache_dirty = true;
  2892. // Navigation
  2893. navigation_layers.clear();
  2894. custom_data_layers.clear();
  2895. custom_data_layers_by_name.clear();
  2896. // Proxies
  2897. source_level_proxies.clear();
  2898. coords_level_proxies.clear();
  2899. alternative_level_proxies.clear();
  2900. #ifndef DISABLE_DEPRECATED
  2901. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  2902. memdelete(E.value);
  2903. }
  2904. compatibility_data.clear();
  2905. #endif // DISABLE_DEPRECATED
  2906. while (!source_ids.is_empty()) {
  2907. remove_source(source_ids[0]);
  2908. }
  2909. tile_shape = TILE_SHAPE_SQUARE;
  2910. tile_layout = TILE_LAYOUT_STACKED;
  2911. tile_offset_axis = TILE_OFFSET_AXIS_HORIZONTAL;
  2912. tile_size = Size2i(16, 16);
  2913. }
  2914. Vector2i TileSet::transform_coords_layout(const Vector2i &p_coords, TileSet::TileOffsetAxis p_offset_axis, TileSet::TileLayout p_from_layout, TileSet::TileLayout p_to_layout) {
  2915. // Transform to stacked layout.
  2916. Vector2i output = p_coords;
  2917. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  2918. SWAP(output.x, output.y);
  2919. }
  2920. switch (p_from_layout) {
  2921. case TileSet::TILE_LAYOUT_STACKED:
  2922. break;
  2923. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2924. if (output.y % 2) {
  2925. output.x -= 1;
  2926. }
  2927. break;
  2928. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2929. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2930. if ((p_from_layout == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  2931. if (output.y < 0 && bool(output.y % 2)) {
  2932. output = Vector2i(output.x + output.y / 2 - 1, output.y);
  2933. } else {
  2934. output = Vector2i(output.x + output.y / 2, output.y);
  2935. }
  2936. } else {
  2937. if (output.x < 0 && bool(output.x % 2)) {
  2938. output = Vector2i(output.x / 2 - 1, output.x + output.y * 2);
  2939. } else {
  2940. output = Vector2i(output.x / 2, output.x + output.y * 2);
  2941. }
  2942. }
  2943. break;
  2944. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2945. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2946. if ((p_from_layout == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  2947. if ((output.x + output.y) < 0 && (output.x - output.y) % 2) {
  2948. output = Vector2i((output.x + output.y) / 2 - 1, output.y - output.x);
  2949. } else {
  2950. output = Vector2i((output.x + output.y) / 2, -output.x + output.y);
  2951. }
  2952. } else {
  2953. if ((output.x - output.y) < 0 && (output.x + output.y) % 2) {
  2954. output = Vector2i((output.x - output.y) / 2 - 1, output.x + output.y);
  2955. } else {
  2956. output = Vector2i((output.x - output.y) / 2, output.x + output.y);
  2957. }
  2958. }
  2959. break;
  2960. }
  2961. switch (p_to_layout) {
  2962. case TileSet::TILE_LAYOUT_STACKED:
  2963. break;
  2964. case TileSet::TILE_LAYOUT_STACKED_OFFSET:
  2965. if (output.y % 2) {
  2966. output.x += 1;
  2967. }
  2968. break;
  2969. case TileSet::TILE_LAYOUT_STAIRS_RIGHT:
  2970. case TileSet::TILE_LAYOUT_STAIRS_DOWN:
  2971. if ((p_to_layout == TileSet::TILE_LAYOUT_STAIRS_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  2972. if (output.y < 0 && (output.y % 2)) {
  2973. output = Vector2i(output.x - output.y / 2 + 1, output.y);
  2974. } else {
  2975. output = Vector2i(output.x - output.y / 2, output.y);
  2976. }
  2977. } else {
  2978. if (output.y % 2) {
  2979. if (output.y < 0) {
  2980. output = Vector2i(2 * output.x + 1, -output.x + output.y / 2 - 1);
  2981. } else {
  2982. output = Vector2i(2 * output.x + 1, -output.x + output.y / 2);
  2983. }
  2984. } else {
  2985. output = Vector2i(2 * output.x, -output.x + output.y / 2);
  2986. }
  2987. }
  2988. break;
  2989. case TileSet::TILE_LAYOUT_DIAMOND_RIGHT:
  2990. case TileSet::TILE_LAYOUT_DIAMOND_DOWN:
  2991. if ((p_to_layout == TileSet::TILE_LAYOUT_DIAMOND_RIGHT) ^ (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL)) {
  2992. if (output.y % 2) {
  2993. if (output.y > 0) {
  2994. output = Vector2i(output.x - output.y / 2, output.x + output.y / 2 + 1);
  2995. } else {
  2996. output = Vector2i(output.x - output.y / 2 + 1, output.x + output.y / 2);
  2997. }
  2998. } else {
  2999. output = Vector2i(output.x - output.y / 2, output.x + output.y / 2);
  3000. }
  3001. } else {
  3002. if (output.y % 2) {
  3003. if (output.y < 0) {
  3004. output = Vector2i(output.x + output.y / 2, -output.x + output.y / 2 - 1);
  3005. } else {
  3006. output = Vector2i(output.x + output.y / 2 + 1, -output.x + output.y / 2);
  3007. }
  3008. } else {
  3009. output = Vector2i(output.x + output.y / 2, -output.x + output.y / 2);
  3010. }
  3011. }
  3012. break;
  3013. }
  3014. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  3015. SWAP(output.x, output.y);
  3016. }
  3017. return output;
  3018. }
  3019. const Vector2i TileSetSource::INVALID_ATLAS_COORDS = Vector2i(-1, -1);
  3020. const int TileSetSource::INVALID_TILE_ALTERNATIVE = -1;
  3021. #ifndef DISABLE_DEPRECATED
  3022. void TileSet::_compatibility_conversion() {
  3023. for (KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  3024. CompatibilityTileData *ctd = E.value;
  3025. // Add the texture
  3026. TileSetAtlasSource *atlas_source = memnew(TileSetAtlasSource);
  3027. int source_id = add_source(Ref<TileSetSource>(atlas_source));
  3028. atlas_source->set_texture(ctd->texture);
  3029. // Handle each tile as a new source. Not optimal but at least it should stay compatible.
  3030. switch (ctd->tile_mode) {
  3031. case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
  3032. atlas_source->set_margins(ctd->region.get_position());
  3033. atlas_source->set_texture_region_size(ctd->region.get_size());
  3034. Vector2i coords;
  3035. for (int flags = 0; flags < 8; flags++) {
  3036. bool flip_h = flags & 1;
  3037. bool flip_v = flags & 2;
  3038. bool transpose = flags & 4;
  3039. Transform2D xform;
  3040. xform = flip_h ? xform.scaled(Size2(-1, 1)) : xform;
  3041. xform = flip_v ? xform.scaled(Size2(1, -1)) : xform;
  3042. xform = transpose ? Transform2D(xform[1], xform[0], Vector2()) : xform;
  3043. int alternative_tile = 0;
  3044. if (!atlas_source->has_tile(coords)) {
  3045. atlas_source->create_tile(coords);
  3046. } else {
  3047. alternative_tile = atlas_source->create_alternative_tile(coords);
  3048. }
  3049. // Add to the mapping.
  3050. Array key_array = { flip_h, flip_v, transpose };
  3051. Array value_array = { source_id, coords, alternative_tile };
  3052. if (!compatibility_tilemap_mapping.has(E.key)) {
  3053. compatibility_tilemap_mapping[E.key] = RBMap<Array, Array>();
  3054. }
  3055. compatibility_tilemap_mapping[E.key][key_array] = value_array;
  3056. compatibility_tilemap_mapping_tile_modes[E.key] = COMPATIBILITY_TILE_MODE_SINGLE_TILE;
  3057. TileData *tile_data = atlas_source->get_tile_data(coords, alternative_tile);
  3058. ERR_CONTINUE(!tile_data);
  3059. tile_data->set_flip_h(flip_h);
  3060. tile_data->set_flip_v(flip_v);
  3061. tile_data->set_transpose(transpose);
  3062. tile_data->set_material(ctd->material);
  3063. tile_data->set_modulate(ctd->modulate);
  3064. tile_data->set_z_index(ctd->z_index);
  3065. if (ctd->occluder.is_valid()) {
  3066. if (get_occlusion_layers_count() < 1) {
  3067. add_occlusion_layer();
  3068. };
  3069. Ref<OccluderPolygon2D> occluder = ctd->occluder->duplicate();
  3070. Vector<Vector2> polygon = ctd->occluder->get_polygon();
  3071. for (int index = 0; index < polygon.size(); index++) {
  3072. polygon.write[index] = xform.xform(polygon[index] - ctd->region.get_size() / 2.0);
  3073. }
  3074. occluder->set_polygon(polygon);
  3075. tile_data->add_occluder_polygon(0);
  3076. tile_data->set_occluder_polygon(0, 0, occluder);
  3077. }
  3078. #ifndef NAVIGATION_2D_DISABLED
  3079. if (ctd->navigation.is_valid()) {
  3080. if (get_navigation_layers_count() < 1) {
  3081. add_navigation_layer();
  3082. }
  3083. Ref<NavigationPolygon> navigation = ctd->navigation->duplicate();
  3084. Vector<Vector2> vertices = navigation->get_vertices();
  3085. for (int index = 0; index < vertices.size(); index++) {
  3086. vertices.write[index] = xform.xform(vertices[index] - ctd->region.get_size() / 2.0);
  3087. }
  3088. navigation->set_vertices(vertices);
  3089. tile_data->set_navigation_polygon(0, navigation);
  3090. }
  3091. #endif // NAVIGATION_2D_DISABLED
  3092. tile_data->set_z_index(ctd->z_index);
  3093. #ifndef PHYSICS_2D_DISABLED
  3094. // Add the shapes.
  3095. if (ctd->shapes.size() > 0) {
  3096. if (get_physics_layers_count() < 1) {
  3097. add_physics_layer();
  3098. }
  3099. }
  3100. for (int k = 0; k < ctd->shapes.size(); k++) {
  3101. CompatibilityShapeData csd = ctd->shapes[k];
  3102. if (csd.autotile_coords == coords) {
  3103. Ref<ConvexPolygonShape2D> convex_shape = csd.shape; // Only ConvexPolygonShape2D are supported, which is the default type used by the 3.x editor
  3104. if (convex_shape.is_valid()) {
  3105. Vector<Vector2> polygon = convex_shape->get_points();
  3106. for (int point_index = 0; point_index < polygon.size(); point_index++) {
  3107. polygon.write[point_index] = xform.xform(csd.transform.xform(polygon[point_index]) - ctd->region.get_size() / 2.0);
  3108. }
  3109. tile_data->set_collision_polygons_count(0, tile_data->get_collision_polygons_count(0) + 1);
  3110. int index = tile_data->get_collision_polygons_count(0) - 1;
  3111. tile_data->set_collision_polygon_one_way(0, index, csd.one_way);
  3112. tile_data->set_collision_polygon_one_way_margin(0, index, csd.one_way_margin);
  3113. tile_data->set_collision_polygon_points(0, index, polygon);
  3114. }
  3115. }
  3116. }
  3117. #endif // PHYSICS_2D_DISABLED
  3118. }
  3119. // Update the size count.
  3120. if (!compatibility_size_count.has(ctd->region.get_size())) {
  3121. compatibility_size_count[ctd->region.get_size()] = 0;
  3122. }
  3123. compatibility_size_count[ctd->region.get_size()]++;
  3124. } break;
  3125. case COMPATIBILITY_TILE_MODE_AUTO_TILE: {
  3126. // Not supported. It would need manual conversion.
  3127. WARN_PRINT_ONCE("Could not convert 3.x autotiles to 4.x. This operation cannot be done automatically, autotiles must be re-created using the terrain system.");
  3128. } break;
  3129. case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
  3130. atlas_source->set_margins(ctd->region.get_position());
  3131. atlas_source->set_separation(Vector2i(ctd->autotile_spacing, ctd->autotile_spacing));
  3132. atlas_source->set_texture_region_size(ctd->autotile_tile_size);
  3133. Size2i atlas_size = ctd->region.get_size() / (ctd->autotile_tile_size + atlas_source->get_separation());
  3134. for (int i = 0; i < atlas_size.x; i++) {
  3135. for (int j = 0; j < atlas_size.y; j++) {
  3136. Vector2i coords = Vector2i(i, j);
  3137. for (int flags = 0; flags < 8; flags++) {
  3138. bool flip_h = flags & 1;
  3139. bool flip_v = flags & 2;
  3140. bool transpose = flags & 4;
  3141. Transform2D xform;
  3142. xform = flip_h ? xform.scaled(Size2(-1, 1)) : xform;
  3143. xform = flip_v ? xform.scaled(Size2(1, -1)) : xform;
  3144. xform = transpose ? Transform2D(xform[1], xform[0], Vector2()) : xform;
  3145. int alternative_tile = 0;
  3146. if (!atlas_source->has_tile(coords)) {
  3147. atlas_source->create_tile(coords);
  3148. } else {
  3149. alternative_tile = atlas_source->create_alternative_tile(coords);
  3150. }
  3151. // Add to the mapping.
  3152. Array key_array = { coords, flip_h, flip_v, transpose };
  3153. Array value_array = { source_id, coords, alternative_tile };
  3154. if (!compatibility_tilemap_mapping.has(E.key)) {
  3155. compatibility_tilemap_mapping[E.key] = RBMap<Array, Array>();
  3156. }
  3157. compatibility_tilemap_mapping[E.key][key_array] = value_array;
  3158. compatibility_tilemap_mapping_tile_modes[E.key] = COMPATIBILITY_TILE_MODE_ATLAS_TILE;
  3159. TileData *tile_data = atlas_source->get_tile_data(coords, alternative_tile);
  3160. ERR_CONTINUE(!tile_data);
  3161. tile_data->set_flip_h(flip_h);
  3162. tile_data->set_flip_v(flip_v);
  3163. tile_data->set_transpose(transpose);
  3164. tile_data->set_material(ctd->material);
  3165. tile_data->set_modulate(ctd->modulate);
  3166. tile_data->set_z_index(ctd->z_index);
  3167. if (ctd->autotile_occluder_map.has(coords)) {
  3168. if (get_occlusion_layers_count() < 1) {
  3169. add_occlusion_layer();
  3170. }
  3171. Ref<OccluderPolygon2D> occluder = ctd->autotile_occluder_map[coords]->duplicate();
  3172. Vector<Vector2> polygon = ctd->occluder->get_polygon();
  3173. for (int index = 0; index < polygon.size(); index++) {
  3174. polygon.write[index] = xform.xform(polygon[index] - ctd->region.get_size() / 2.0);
  3175. }
  3176. occluder->set_polygon(polygon);
  3177. tile_data->add_occluder_polygon(0);
  3178. tile_data->set_occluder_polygon(0, 0, occluder);
  3179. }
  3180. #ifndef NAVIGATION_2D_DISABLED
  3181. if (ctd->autotile_navpoly_map.has(coords)) {
  3182. if (get_navigation_layers_count() < 1) {
  3183. add_navigation_layer();
  3184. }
  3185. Ref<NavigationPolygon> navigation = ctd->autotile_navpoly_map[coords]->duplicate();
  3186. Vector<Vector2> vertices = navigation->get_vertices();
  3187. for (int index = 0; index < vertices.size(); index++) {
  3188. vertices.write[index] = xform.xform(vertices[index] - ctd->region.get_size() / 2.0);
  3189. }
  3190. navigation->set_vertices(vertices);
  3191. tile_data->set_navigation_polygon(0, navigation);
  3192. }
  3193. #endif // NAVIGATION_2D_DISABLED
  3194. if (ctd->autotile_priority_map.has(coords)) {
  3195. tile_data->set_probability(ctd->autotile_priority_map[coords]);
  3196. }
  3197. if (ctd->autotile_z_index_map.has(coords)) {
  3198. tile_data->set_z_index(ctd->autotile_z_index_map[coords]);
  3199. }
  3200. #ifndef PHYSICS_2D_DISABLED
  3201. // Add the shapes.
  3202. if (ctd->shapes.size() > 0) {
  3203. if (get_physics_layers_count() < 1) {
  3204. add_physics_layer();
  3205. }
  3206. }
  3207. for (int k = 0; k < ctd->shapes.size(); k++) {
  3208. CompatibilityShapeData csd = ctd->shapes[k];
  3209. if (csd.autotile_coords == coords) {
  3210. Ref<ConvexPolygonShape2D> convex_shape = csd.shape; // Only ConvexPolygonShape2D are supported, which is the default type used by the 3.x editor
  3211. if (convex_shape.is_valid()) {
  3212. Vector<Vector2> polygon = convex_shape->get_points();
  3213. for (int point_index = 0; point_index < polygon.size(); point_index++) {
  3214. polygon.write[point_index] = xform.xform(csd.transform.xform(polygon[point_index]) - ctd->autotile_tile_size / 2.0);
  3215. }
  3216. tile_data->set_collision_polygons_count(0, tile_data->get_collision_polygons_count(0) + 1);
  3217. int index = tile_data->get_collision_polygons_count(0) - 1;
  3218. tile_data->set_collision_polygon_one_way(0, index, csd.one_way);
  3219. tile_data->set_collision_polygon_one_way_margin(0, index, csd.one_way_margin);
  3220. tile_data->set_collision_polygon_points(0, index, polygon);
  3221. }
  3222. }
  3223. }
  3224. #endif // PHYSICS_2D_DISABLED
  3225. // -- TODO: handle --
  3226. // Those are offset for the whole atlas, they are likely useless for the atlases, but might make sense for single tiles.
  3227. // texture offset
  3228. // occluder_offset
  3229. // navigation_offset
  3230. // For terrains, ignored for now?
  3231. // bitmask_mode
  3232. // bitmask_flags
  3233. }
  3234. }
  3235. }
  3236. // Update the size count.
  3237. if (!compatibility_size_count.has(ctd->region.get_size())) {
  3238. compatibility_size_count[ctd->autotile_tile_size] = 0;
  3239. }
  3240. compatibility_size_count[ctd->autotile_tile_size] += atlas_size.x * atlas_size.y;
  3241. } break;
  3242. }
  3243. #ifndef PHYSICS_2D_DISABLED
  3244. // Offset all shapes
  3245. for (int k = 0; k < ctd->shapes.size(); k++) {
  3246. Ref<ConvexPolygonShape2D> convex = ctd->shapes[k].shape;
  3247. if (convex.is_valid()) {
  3248. Vector<Vector2> points = convex->get_points();
  3249. for (int i_point = 0; i_point < points.size(); i_point++) {
  3250. points.write[i_point] = points[i_point] - get_tile_size() / 2;
  3251. }
  3252. convex->set_points(points);
  3253. }
  3254. }
  3255. #endif // PHYSICS_2D_DISABLED
  3256. }
  3257. // Update the TileSet tile_size according to the most common size found.
  3258. Vector2i max_size = get_tile_size();
  3259. int max_count = 0;
  3260. for (KeyValue<Vector2i, int> kv : compatibility_size_count) {
  3261. if (kv.value > max_count) {
  3262. max_size = kv.key;
  3263. max_count = kv.value;
  3264. }
  3265. }
  3266. set_tile_size(max_size);
  3267. // Reset compatibility data (besides the histogram counts)
  3268. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  3269. memdelete(E.value);
  3270. }
  3271. compatibility_data = HashMap<int, CompatibilityTileData *>();
  3272. }
  3273. Array TileSet::compatibility_tilemap_map(int p_tile_id, Vector2i p_coords, bool p_flip_h, bool p_flip_v, bool p_transpose) {
  3274. Array cannot_convert_array = {
  3275. TileSet::INVALID_SOURCE,
  3276. TileSetAtlasSource::INVALID_ATLAS_COORDS,
  3277. TileSetAtlasSource::INVALID_TILE_ALTERNATIVE
  3278. };
  3279. if (!compatibility_tilemap_mapping.has(p_tile_id)) {
  3280. return cannot_convert_array;
  3281. }
  3282. int tile_mode = compatibility_tilemap_mapping_tile_modes[p_tile_id];
  3283. switch (tile_mode) {
  3284. case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
  3285. Array a = { p_flip_h, p_flip_v, p_transpose };
  3286. return compatibility_tilemap_mapping[p_tile_id][a];
  3287. }
  3288. case COMPATIBILITY_TILE_MODE_AUTO_TILE:
  3289. return cannot_convert_array;
  3290. break;
  3291. case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
  3292. Array a = { p_coords, p_flip_h, p_flip_v, p_transpose };
  3293. return compatibility_tilemap_mapping[p_tile_id][a];
  3294. }
  3295. default:
  3296. return cannot_convert_array;
  3297. break;
  3298. }
  3299. }
  3300. #endif // DISABLE_DEPRECATED
  3301. bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
  3302. Vector<String> components = String(p_name).split("/", true, 2);
  3303. #ifndef DISABLE_DEPRECATED
  3304. // TODO: This should be moved to a dedicated conversion system (see #50691)
  3305. if (components.size() >= 1 && components[0].is_valid_int()) {
  3306. int id = components[0].to_int();
  3307. // Get or create the compatibility object
  3308. CompatibilityTileData *ctd;
  3309. HashMap<int, CompatibilityTileData *>::Iterator E = compatibility_data.find(id);
  3310. if (!E) {
  3311. ctd = memnew(CompatibilityTileData);
  3312. compatibility_data.insert(id, ctd);
  3313. } else {
  3314. ctd = E->value;
  3315. }
  3316. if (components.size() < 2) {
  3317. return false;
  3318. }
  3319. String what = components[1];
  3320. if (what == "name") {
  3321. ctd->name = p_value;
  3322. } else if (what == "texture") {
  3323. ctd->texture = p_value;
  3324. } else if (what == "tex_offset") {
  3325. ctd->tex_offset = p_value;
  3326. } else if (what == "material") {
  3327. ctd->material = p_value;
  3328. } else if (what == "modulate") {
  3329. ctd->modulate = p_value;
  3330. } else if (what == "region") {
  3331. ctd->region = p_value;
  3332. } else if (what == "tile_mode") {
  3333. ctd->tile_mode = p_value;
  3334. } else if (what.left(9) == "autotile") {
  3335. what = what.substr(9);
  3336. if (what == "bitmask_mode") {
  3337. ctd->autotile_bitmask_mode = p_value;
  3338. } else if (what == "icon_coordinate") {
  3339. ctd->autotile_icon_coordinate = p_value;
  3340. } else if (what == "tile_size") {
  3341. ctd->autotile_tile_size = p_value;
  3342. } else if (what == "spacing") {
  3343. ctd->autotile_spacing = p_value;
  3344. } else if (what == "bitmask_flags") {
  3345. if (p_value.is_array()) {
  3346. Array p = p_value;
  3347. Vector2i last_coord;
  3348. while (p.size() > 0) {
  3349. if (p[0].get_type() == Variant::VECTOR2) {
  3350. last_coord = p[0];
  3351. } else if (p[0].get_type() == Variant::INT) {
  3352. ctd->autotile_bitmask_flags.insert(last_coord, p[0]);
  3353. }
  3354. p.pop_front();
  3355. }
  3356. }
  3357. } else if (what == "occluder_map") {
  3358. Array p = p_value;
  3359. Vector2 last_coord;
  3360. while (p.size() > 0) {
  3361. if (p[0].get_type() == Variant::VECTOR2) {
  3362. last_coord = p[0];
  3363. } else if (p[0].get_type() == Variant::OBJECT) {
  3364. ctd->autotile_occluder_map.insert(last_coord, p[0]);
  3365. }
  3366. p.pop_front();
  3367. }
  3368. } else if (what == "navpoly_map") {
  3369. Array p = p_value;
  3370. Vector2 last_coord;
  3371. while (p.size() > 0) {
  3372. if (p[0].get_type() == Variant::VECTOR2) {
  3373. last_coord = p[0];
  3374. } else if (p[0].get_type() == Variant::OBJECT) {
  3375. #ifndef NAVIGATION_2D_DISABLED
  3376. ctd->autotile_navpoly_map.insert(last_coord, p[0]);
  3377. #endif // NAVIGATION_2D_DISABLED
  3378. }
  3379. p.pop_front();
  3380. }
  3381. } else if (what == "priority_map") {
  3382. Array p = p_value;
  3383. Vector3 val;
  3384. Vector2 v;
  3385. int priority;
  3386. while (p.size() > 0) {
  3387. val = p[0];
  3388. if (val.z > 1) {
  3389. v.x = val.x;
  3390. v.y = val.y;
  3391. priority = (int)val.z;
  3392. ctd->autotile_priority_map.insert(v, priority);
  3393. }
  3394. p.pop_front();
  3395. }
  3396. } else if (what == "z_index_map") {
  3397. Array p = p_value;
  3398. Vector3 val;
  3399. Vector2 v;
  3400. int z_index;
  3401. while (p.size() > 0) {
  3402. val = p[0];
  3403. if (val.z != 0) {
  3404. v.x = val.x;
  3405. v.y = val.y;
  3406. z_index = (int)val.z;
  3407. ctd->autotile_z_index_map.insert(v, z_index);
  3408. }
  3409. p.pop_front();
  3410. }
  3411. }
  3412. } else if (what == "shapes") {
  3413. Array p = p_value;
  3414. for (int i = 0; i < p.size(); i++) {
  3415. CompatibilityShapeData csd;
  3416. Dictionary d = p[i];
  3417. for (const KeyValue<Variant, Variant> &kv : d) {
  3418. String key = kv.key;
  3419. if (key == "autotile_coord") {
  3420. csd.autotile_coords = kv.value;
  3421. } else if (key == "one_way") {
  3422. csd.one_way = kv.value;
  3423. } else if (key == "one_way_margin") {
  3424. csd.one_way_margin = kv.value;
  3425. } else if (key == "shape") {
  3426. #ifndef PHYSICS_2D_DISABLED
  3427. csd.shape = kv.value;
  3428. #endif // PHYSICS_2D_DISABLED
  3429. } else if (key == "shape_transform") {
  3430. csd.transform = kv.value;
  3431. }
  3432. }
  3433. ctd->shapes.push_back(csd);
  3434. }
  3435. } else if (what == "occluder") {
  3436. ctd->occluder = p_value;
  3437. } else if (what == "navigation") {
  3438. #ifndef NAVIGATION_2D_DISABLED
  3439. ctd->navigation = p_value;
  3440. #endif // NAVIGATION_2D_DISABLED
  3441. /*
  3442. // IGNORED FOR NOW, they seem duplicated data compared to the shapes array
  3443. } else if (what == "shape") {
  3444. } else if (what == "shape_offset") {
  3445. } else if (what == "shape_transform") {
  3446. } else if (what == "shape_one_way") {
  3447. } else if (what == "shape_one_way_margin") {
  3448. }
  3449. // IGNORED FOR NOW, maybe useless ?
  3450. else if (what == "occluder_offset") {
  3451. // Not
  3452. } else if (what == "navigation_offset") {
  3453. }
  3454. */
  3455. } else if (what == "z_index") {
  3456. ctd->z_index = p_value;
  3457. // TODO: remove the conversion from here, it's not where it should be done (see #50691)
  3458. _compatibility_conversion();
  3459. } else {
  3460. return false;
  3461. }
  3462. } else {
  3463. #endif // DISABLE_DEPRECATED
  3464. // This is now a new property.
  3465. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  3466. // Occlusion layers.
  3467. int index = components[0].trim_prefix("occlusion_layer_").to_int();
  3468. ERR_FAIL_COND_V(index < 0, false);
  3469. if (components[1] == "light_mask") {
  3470. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  3471. while (index >= occlusion_layers.size()) {
  3472. add_occlusion_layer();
  3473. }
  3474. set_occlusion_layer_light_mask(index, p_value);
  3475. return true;
  3476. } else if (components[1] == "sdf_collision") {
  3477. ERR_FAIL_COND_V(p_value.get_type() != Variant::BOOL, false);
  3478. while (index >= occlusion_layers.size()) {
  3479. add_occlusion_layer();
  3480. }
  3481. set_occlusion_layer_sdf_collision(index, p_value);
  3482. return true;
  3483. }
  3484. #ifndef PHYSICS_2D_DISABLED
  3485. } else if (components.size() == 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  3486. // Physics layers.
  3487. int index = components[0].trim_prefix("physics_layer_").to_int();
  3488. ERR_FAIL_COND_V(index < 0, false);
  3489. if (components[1] == "collision_layer") {
  3490. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  3491. while (index >= physics_layers.size()) {
  3492. add_physics_layer();
  3493. }
  3494. set_physics_layer_collision_layer(index, p_value);
  3495. return true;
  3496. } else if (components[1] == "collision_mask") {
  3497. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  3498. while (index >= physics_layers.size()) {
  3499. add_physics_layer();
  3500. }
  3501. set_physics_layer_collision_mask(index, p_value);
  3502. return true;
  3503. } else if (components[1] == "collision_priority") {
  3504. ERR_FAIL_COND_V(p_value.get_type() != Variant::FLOAT, false);
  3505. while (index >= physics_layers.size()) {
  3506. add_physics_layer();
  3507. }
  3508. set_physics_layer_collision_priority(index, p_value);
  3509. return true;
  3510. } else if (components[1] == "physics_material") {
  3511. Ref<PhysicsMaterial> physics_material = p_value;
  3512. while (index >= physics_layers.size()) {
  3513. add_physics_layer();
  3514. }
  3515. set_physics_layer_physics_material(index, physics_material);
  3516. return true;
  3517. }
  3518. #endif // PHYSICS_2D_DISABLED
  3519. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int()) {
  3520. // Terrains.
  3521. int terrain_set_index = components[0].trim_prefix("terrain_set_").to_int();
  3522. ERR_FAIL_COND_V(terrain_set_index < 0, false);
  3523. if (components[1] == "mode") {
  3524. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  3525. while (terrain_set_index >= terrain_sets.size()) {
  3526. add_terrain_set();
  3527. }
  3528. set_terrain_set_mode(terrain_set_index, TerrainMode(int(p_value)));
  3529. } else if (components.size() >= 3 && components[1].begins_with("terrain_") && components[1].trim_prefix("terrain_").is_valid_int()) {
  3530. int terrain_index = components[1].trim_prefix("terrain_").to_int();
  3531. ERR_FAIL_COND_V(terrain_index < 0, false);
  3532. if (components[2] == "name") {
  3533. ERR_FAIL_COND_V(!p_value.is_string(), false);
  3534. while (terrain_set_index >= terrain_sets.size()) {
  3535. add_terrain_set();
  3536. }
  3537. while (terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  3538. add_terrain(terrain_set_index);
  3539. }
  3540. set_terrain_name(terrain_set_index, terrain_index, p_value);
  3541. return true;
  3542. } else if (components[2] == "color") {
  3543. ERR_FAIL_COND_V(p_value.get_type() != Variant::COLOR, false);
  3544. while (terrain_set_index >= terrain_sets.size()) {
  3545. add_terrain_set();
  3546. }
  3547. while (terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  3548. add_terrain(terrain_set_index);
  3549. }
  3550. set_terrain_color(terrain_set_index, terrain_index, p_value);
  3551. return true;
  3552. }
  3553. }
  3554. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  3555. #ifndef NAVIGATION_2D_DISABLED
  3556. // Navigation layers.
  3557. int index = components[0].trim_prefix("navigation_layer_").to_int();
  3558. ERR_FAIL_COND_V(index < 0, false);
  3559. if (components[1] == "layers") {
  3560. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  3561. while (index >= navigation_layers.size()) {
  3562. add_navigation_layer();
  3563. }
  3564. set_navigation_layer_layers(index, p_value);
  3565. return true;
  3566. }
  3567. #endif // NAVIGATION_2D_DISABLED
  3568. } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int()) {
  3569. // Custom data layers.
  3570. int index = components[0].trim_prefix("custom_data_layer_").to_int();
  3571. ERR_FAIL_COND_V(index < 0, false);
  3572. if (components[1] == "name") {
  3573. ERR_FAIL_COND_V(!p_value.is_string(), false);
  3574. while (index >= custom_data_layers.size()) {
  3575. add_custom_data_layer();
  3576. }
  3577. set_custom_data_layer_name(index, p_value);
  3578. return true;
  3579. } else if (components[1] == "type") {
  3580. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  3581. while (index >= custom_data_layers.size()) {
  3582. add_custom_data_layer();
  3583. }
  3584. set_custom_data_layer_type(index, Variant::Type(int(p_value)));
  3585. return true;
  3586. }
  3587. } else if (components.size() == 2 && components[0] == "sources" && components[1].is_valid_int()) {
  3588. // Create source only if it does not exists.
  3589. int source_id = components[1].to_int();
  3590. if (has_source(source_id)) {
  3591. remove_source(source_id);
  3592. }
  3593. add_source(p_value, source_id);
  3594. return true;
  3595. } else if (components.size() == 2 && components[0] == "tile_proxies") {
  3596. ERR_FAIL_COND_V(p_value.get_type() != Variant::ARRAY, false);
  3597. Array a = p_value;
  3598. ERR_FAIL_COND_V(a.size() % 2 != 0, false);
  3599. if (components[1] == "source_level") {
  3600. for (int i = 0; i < a.size(); i += 2) {
  3601. set_source_level_tile_proxy(a[i], a[i + 1]);
  3602. }
  3603. return true;
  3604. } else if (components[1] == "coords_level") {
  3605. for (int i = 0; i < a.size(); i += 2) {
  3606. Array key = a[i];
  3607. Array value = a[i + 1];
  3608. set_coords_level_tile_proxy(key[0], key[1], value[0], value[1]);
  3609. }
  3610. return true;
  3611. } else if (components[1] == "alternative_level") {
  3612. for (int i = 0; i < a.size(); i += 2) {
  3613. Array key = a[i];
  3614. Array value = a[i + 1];
  3615. set_alternative_level_tile_proxy(key[0], key[1], key[2], value[0], value[1], value[2]);
  3616. }
  3617. return true;
  3618. }
  3619. return false;
  3620. } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) {
  3621. int pattern_index = components[0].trim_prefix("pattern_").to_int();
  3622. for (int i = patterns.size(); i <= pattern_index; i++) {
  3623. add_pattern(p_value);
  3624. }
  3625. return true;
  3626. }
  3627. #ifndef DISABLE_DEPRECATED
  3628. }
  3629. #endif // DISABLE_DEPRECATED
  3630. return false;
  3631. }
  3632. bool TileSet::_get(const StringName &p_name, Variant &r_ret) const {
  3633. Vector<String> components = String(p_name).split("/", true, 2);
  3634. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  3635. // Occlusion layers.
  3636. int index = components[0].trim_prefix("occlusion_layer_").to_int();
  3637. if (index < 0 || index >= occlusion_layers.size()) {
  3638. return false;
  3639. }
  3640. if (components[1] == "light_mask") {
  3641. r_ret = get_occlusion_layer_light_mask(index);
  3642. return true;
  3643. } else if (components[1] == "sdf_collision") {
  3644. r_ret = get_occlusion_layer_sdf_collision(index);
  3645. return true;
  3646. }
  3647. #ifndef PHYSICS_2D_DISABLED
  3648. } else if (components.size() == 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  3649. // Physics layers.
  3650. int index = components[0].trim_prefix("physics_layer_").to_int();
  3651. if (index < 0 || index >= physics_layers.size()) {
  3652. return false;
  3653. }
  3654. if (components[1] == "collision_layer") {
  3655. r_ret = get_physics_layer_collision_layer(index);
  3656. return true;
  3657. } else if (components[1] == "collision_mask") {
  3658. r_ret = get_physics_layer_collision_mask(index);
  3659. return true;
  3660. } else if (components[1] == "collision_priority") {
  3661. r_ret = get_physics_layer_collision_priority(index);
  3662. return true;
  3663. } else if (components[1] == "physics_material") {
  3664. r_ret = get_physics_layer_physics_material(index);
  3665. return true;
  3666. }
  3667. #endif // PHYSICS_2D_DISABLED
  3668. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int()) {
  3669. // Terrains.
  3670. int terrain_set_index = components[0].trim_prefix("terrain_set_").to_int();
  3671. if (terrain_set_index < 0 || terrain_set_index >= terrain_sets.size()) {
  3672. return false;
  3673. }
  3674. if (components[1] == "mode") {
  3675. r_ret = get_terrain_set_mode(terrain_set_index);
  3676. return true;
  3677. } else if (components.size() >= 3 && components[1].begins_with("terrain_") && components[1].trim_prefix("terrain_").is_valid_int()) {
  3678. int terrain_index = components[1].trim_prefix("terrain_").to_int();
  3679. if (terrain_index < 0 || terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  3680. return false;
  3681. }
  3682. if (components[2] == "name") {
  3683. r_ret = get_terrain_name(terrain_set_index, terrain_index);
  3684. return true;
  3685. } else if (components[2] == "color") {
  3686. r_ret = get_terrain_color(terrain_set_index, terrain_index);
  3687. return true;
  3688. }
  3689. }
  3690. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  3691. // navigation layers.
  3692. int index = components[0].trim_prefix("navigation_layer_").to_int();
  3693. if (index < 0 || index >= navigation_layers.size()) {
  3694. return false;
  3695. }
  3696. #ifndef NAVIGATION_2D_DISABLED
  3697. if (components[1] == "layers") {
  3698. r_ret = get_navigation_layer_layers(index);
  3699. return true;
  3700. }
  3701. #endif // NAVIGATION_2D_DISABLED
  3702. } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int()) {
  3703. // Custom data layers.
  3704. int index = components[0].trim_prefix("custom_data_layer_").to_int();
  3705. if (index < 0 || index >= custom_data_layers.size()) {
  3706. return false;
  3707. }
  3708. if (components[1] == "name") {
  3709. r_ret = get_custom_data_layer_name(index);
  3710. return true;
  3711. } else if (components[1] == "type") {
  3712. r_ret = get_custom_data_layer_type(index);
  3713. return true;
  3714. }
  3715. } else if (components.size() == 2 && components[0] == "sources" && components[1].is_valid_int()) {
  3716. // Atlases data.
  3717. int source_id = components[1].to_int();
  3718. if (has_source(source_id)) {
  3719. r_ret = get_source(source_id);
  3720. return true;
  3721. } else {
  3722. return false;
  3723. }
  3724. } else if (components.size() == 2 && components[0] == "tile_proxies") {
  3725. if (components[1] == "source_level") {
  3726. Array a;
  3727. for (const KeyValue<int, int> &E : source_level_proxies) {
  3728. a.push_back(E.key);
  3729. a.push_back(E.value);
  3730. }
  3731. r_ret = a;
  3732. return true;
  3733. } else if (components[1] == "coords_level") {
  3734. Array a;
  3735. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  3736. a.push_back(E.key);
  3737. a.push_back(E.value);
  3738. }
  3739. r_ret = a;
  3740. return true;
  3741. } else if (components[1] == "alternative_level") {
  3742. Array a;
  3743. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  3744. a.push_back(E.key);
  3745. a.push_back(E.value);
  3746. }
  3747. r_ret = a;
  3748. return true;
  3749. }
  3750. return false;
  3751. } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) {
  3752. int pattern_index = components[0].trim_prefix("pattern_").to_int();
  3753. if (pattern_index < 0 || pattern_index >= (int)patterns.size()) {
  3754. return false;
  3755. }
  3756. r_ret = patterns[pattern_index];
  3757. return true;
  3758. }
  3759. return false;
  3760. }
  3761. void TileSet::_get_property_list(List<PropertyInfo> *p_list) const {
  3762. PropertyInfo property_info;
  3763. // Rendering.
  3764. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Rendering", ""), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  3765. for (int i = 0; i < occlusion_layers.size(); i++) {
  3766. p_list->push_back(PropertyInfo(Variant::INT, vformat("occlusion_layer_%d/light_mask", i), PROPERTY_HINT_LAYERS_2D_RENDER));
  3767. // occlusion_layer_%d/sdf_collision
  3768. property_info = PropertyInfo(Variant::BOOL, vformat("occlusion_layer_%d/sdf_collision", i));
  3769. if (occlusion_layers[i].sdf_collision == false) {
  3770. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3771. }
  3772. p_list->push_back(property_info);
  3773. }
  3774. #ifndef PHYSICS_2D_DISABLED
  3775. // Physics.
  3776. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Physics", ""), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  3777. for (int i = 0; i < physics_layers.size(); i++) {
  3778. p_list->push_back(PropertyInfo(Variant::INT, vformat("physics_layer_%d/collision_layer", i), PROPERTY_HINT_LAYERS_2D_PHYSICS));
  3779. // physics_layer_%d/collision_mask
  3780. property_info = PropertyInfo(Variant::INT, vformat("physics_layer_%d/collision_mask", i), PROPERTY_HINT_LAYERS_2D_PHYSICS);
  3781. if (physics_layers[i].collision_mask == 1) {
  3782. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3783. }
  3784. p_list->push_back(property_info);
  3785. // physics_layer_%d/collision_priority
  3786. property_info = PropertyInfo(Variant::FLOAT, vformat("physics_layer_%d/collision_priority", i));
  3787. if (physics_layers[i].collision_priority == 1.0) {
  3788. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3789. }
  3790. p_list->push_back(property_info);
  3791. // physics_layer_%d/physics_material
  3792. property_info = PropertyInfo(Variant::OBJECT, vformat("physics_layer_%d/physics_material", i), PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial");
  3793. if (physics_layers[i].physics_material.is_null()) {
  3794. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3795. }
  3796. p_list->push_back(property_info);
  3797. }
  3798. #endif // PHYSICS_2D_DISABLED
  3799. // Terrains.
  3800. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Terrains", ""), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  3801. for (int terrain_set_index = 0; terrain_set_index < terrain_sets.size(); terrain_set_index++) {
  3802. 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"));
  3803. 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)));
  3804. for (int terrain_index = 0; terrain_index < terrain_sets[terrain_set_index].terrains.size(); terrain_index++) {
  3805. p_list->push_back(PropertyInfo(Variant::STRING, vformat("terrain_set_%d/terrain_%d/name", terrain_set_index, terrain_index)));
  3806. p_list->push_back(PropertyInfo(Variant::COLOR, vformat("terrain_set_%d/terrain_%d/color", terrain_set_index, terrain_index)));
  3807. }
  3808. }
  3809. // Navigation.
  3810. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Navigation", ""), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  3811. for (int i = 0; i < navigation_layers.size(); i++) {
  3812. p_list->push_back(PropertyInfo(Variant::INT, vformat("navigation_layer_%d/layers", i), PROPERTY_HINT_LAYERS_2D_NAVIGATION));
  3813. }
  3814. // Custom data.
  3815. String argt = "Any";
  3816. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  3817. argt += "," + Variant::get_type_name(Variant::Type(i));
  3818. }
  3819. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Custom Data", ""), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  3820. for (int i = 0; i < custom_data_layers.size(); i++) {
  3821. p_list->push_back(PropertyInfo(Variant::STRING, vformat("custom_data_layer_%d/name", i)));
  3822. p_list->push_back(PropertyInfo(Variant::INT, vformat("custom_data_layer_%d/type", i), PROPERTY_HINT_ENUM, argt));
  3823. }
  3824. // Sources.
  3825. // Note: sources have to be listed in at the end as some TileData rely on the TileSet properties being initialized first.
  3826. for (const KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  3827. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("sources/%d", E_source.key), PROPERTY_HINT_RESOURCE_TYPE, "TileSetAtlasSource", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_ALWAYS_DUPLICATE));
  3828. }
  3829. // Tile Proxies.
  3830. // Note: proxies need to be set after sources are set.
  3831. p_list->push_back(PropertyInfo(Variant::NIL, "Tile Proxies", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  3832. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/source_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  3833. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/coords_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  3834. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/alternative_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  3835. // Patterns.
  3836. for (unsigned int pattern_index = 0; pattern_index < patterns.size(); pattern_index++) {
  3837. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("pattern_%d", pattern_index), PROPERTY_HINT_RESOURCE_TYPE, "TileMapPattern", PROPERTY_USAGE_NO_EDITOR));
  3838. }
  3839. }
  3840. void TileSet::_validate_property(PropertyInfo &p_property) const {
  3841. if (!Engine::get_singleton()->is_editor_hint()) {
  3842. return;
  3843. }
  3844. if (p_property.name == "tile_layout" && tile_shape == TILE_SHAPE_SQUARE) {
  3845. p_property.usage ^= PROPERTY_USAGE_READ_ONLY;
  3846. } else if (p_property.name == "tile_offset_axis" && tile_shape == TILE_SHAPE_SQUARE) {
  3847. p_property.usage ^= PROPERTY_USAGE_READ_ONLY;
  3848. }
  3849. }
  3850. void TileSet::_bind_methods() {
  3851. // Sources management.
  3852. ClassDB::bind_method(D_METHOD("get_next_source_id"), &TileSet::get_next_source_id);
  3853. ClassDB::bind_method(D_METHOD("add_source", "source", "atlas_source_id_override"), &TileSet::add_source, DEFVAL(TileSet::INVALID_SOURCE));
  3854. ClassDB::bind_method(D_METHOD("remove_source", "source_id"), &TileSet::remove_source);
  3855. ClassDB::bind_method(D_METHOD("set_source_id", "source_id", "new_source_id"), &TileSet::set_source_id);
  3856. ClassDB::bind_method(D_METHOD("get_source_count"), &TileSet::get_source_count);
  3857. ClassDB::bind_method(D_METHOD("get_source_id", "index"), &TileSet::get_source_id);
  3858. ClassDB::bind_method(D_METHOD("has_source", "source_id"), &TileSet::has_source);
  3859. ClassDB::bind_method(D_METHOD("get_source", "source_id"), &TileSet::get_source);
  3860. // Shape and layout.
  3861. ClassDB::bind_method(D_METHOD("set_tile_shape", "shape"), &TileSet::set_tile_shape);
  3862. ClassDB::bind_method(D_METHOD("get_tile_shape"), &TileSet::get_tile_shape);
  3863. ClassDB::bind_method(D_METHOD("set_tile_layout", "layout"), &TileSet::set_tile_layout);
  3864. ClassDB::bind_method(D_METHOD("get_tile_layout"), &TileSet::get_tile_layout);
  3865. ClassDB::bind_method(D_METHOD("set_tile_offset_axis", "alignment"), &TileSet::set_tile_offset_axis);
  3866. ClassDB::bind_method(D_METHOD("get_tile_offset_axis"), &TileSet::get_tile_offset_axis);
  3867. ClassDB::bind_method(D_METHOD("set_tile_size", "size"), &TileSet::set_tile_size);
  3868. ClassDB::bind_method(D_METHOD("get_tile_size"), &TileSet::get_tile_size);
  3869. ADD_PROPERTY(PropertyInfo(Variant::INT, "tile_shape", PROPERTY_HINT_ENUM, "Square,Isometric,Half-Offset Square,Hexagon"), "set_tile_shape", "get_tile_shape");
  3870. 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");
  3871. ADD_PROPERTY(PropertyInfo(Variant::INT, "tile_offset_axis", PROPERTY_HINT_ENUM, "Horizontal Offset,Vertical Offset"), "set_tile_offset_axis", "get_tile_offset_axis");
  3872. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "tile_size", PROPERTY_HINT_NONE, "suffix:px"), "set_tile_size", "get_tile_size");
  3873. // Rendering.
  3874. ClassDB::bind_method(D_METHOD("set_uv_clipping", "uv_clipping"), &TileSet::set_uv_clipping);
  3875. ClassDB::bind_method(D_METHOD("is_uv_clipping"), &TileSet::is_uv_clipping);
  3876. ClassDB::bind_method(D_METHOD("get_occlusion_layers_count"), &TileSet::get_occlusion_layers_count);
  3877. ClassDB::bind_method(D_METHOD("add_occlusion_layer", "to_position"), &TileSet::add_occlusion_layer, DEFVAL(-1));
  3878. ClassDB::bind_method(D_METHOD("move_occlusion_layer", "layer_index", "to_position"), &TileSet::move_occlusion_layer);
  3879. ClassDB::bind_method(D_METHOD("remove_occlusion_layer", "layer_index"), &TileSet::remove_occlusion_layer);
  3880. ClassDB::bind_method(D_METHOD("set_occlusion_layer_light_mask", "layer_index", "light_mask"), &TileSet::set_occlusion_layer_light_mask);
  3881. ClassDB::bind_method(D_METHOD("get_occlusion_layer_light_mask", "layer_index"), &TileSet::get_occlusion_layer_light_mask);
  3882. ClassDB::bind_method(D_METHOD("set_occlusion_layer_sdf_collision", "layer_index", "sdf_collision"), &TileSet::set_occlusion_layer_sdf_collision);
  3883. ClassDB::bind_method(D_METHOD("get_occlusion_layer_sdf_collision", "layer_index"), &TileSet::get_occlusion_layer_sdf_collision);
  3884. #ifndef PHYSICS_2D_DISABLED
  3885. // Physics
  3886. ClassDB::bind_method(D_METHOD("get_physics_layers_count"), &TileSet::get_physics_layers_count);
  3887. ClassDB::bind_method(D_METHOD("add_physics_layer", "to_position"), &TileSet::add_physics_layer, DEFVAL(-1));
  3888. ClassDB::bind_method(D_METHOD("move_physics_layer", "layer_index", "to_position"), &TileSet::move_physics_layer);
  3889. ClassDB::bind_method(D_METHOD("remove_physics_layer", "layer_index"), &TileSet::remove_physics_layer);
  3890. ClassDB::bind_method(D_METHOD("set_physics_layer_collision_layer", "layer_index", "layer"), &TileSet::set_physics_layer_collision_layer);
  3891. ClassDB::bind_method(D_METHOD("get_physics_layer_collision_layer", "layer_index"), &TileSet::get_physics_layer_collision_layer);
  3892. ClassDB::bind_method(D_METHOD("set_physics_layer_collision_mask", "layer_index", "mask"), &TileSet::set_physics_layer_collision_mask);
  3893. ClassDB::bind_method(D_METHOD("get_physics_layer_collision_mask", "layer_index"), &TileSet::get_physics_layer_collision_mask);
  3894. ClassDB::bind_method(D_METHOD("set_physics_layer_collision_priority", "layer_index", "priority"), &TileSet::set_physics_layer_collision_priority);
  3895. ClassDB::bind_method(D_METHOD("get_physics_layer_collision_priority", "layer_index"), &TileSet::get_physics_layer_collision_priority);
  3896. ClassDB::bind_method(D_METHOD("set_physics_layer_physics_material", "layer_index", "physics_material"), &TileSet::set_physics_layer_physics_material);
  3897. ClassDB::bind_method(D_METHOD("get_physics_layer_physics_material", "layer_index"), &TileSet::get_physics_layer_physics_material);
  3898. #endif // PHYSICS_2D_DISABLED
  3899. // Terrains
  3900. ClassDB::bind_method(D_METHOD("get_terrain_sets_count"), &TileSet::get_terrain_sets_count);
  3901. ClassDB::bind_method(D_METHOD("add_terrain_set", "to_position"), &TileSet::add_terrain_set, DEFVAL(-1));
  3902. ClassDB::bind_method(D_METHOD("move_terrain_set", "terrain_set", "to_position"), &TileSet::move_terrain_set);
  3903. ClassDB::bind_method(D_METHOD("remove_terrain_set", "terrain_set"), &TileSet::remove_terrain_set);
  3904. ClassDB::bind_method(D_METHOD("set_terrain_set_mode", "terrain_set", "mode"), &TileSet::set_terrain_set_mode);
  3905. ClassDB::bind_method(D_METHOD("get_terrain_set_mode", "terrain_set"), &TileSet::get_terrain_set_mode);
  3906. ClassDB::bind_method(D_METHOD("get_terrains_count", "terrain_set"), &TileSet::get_terrains_count);
  3907. ClassDB::bind_method(D_METHOD("add_terrain", "terrain_set", "to_position"), &TileSet::add_terrain, DEFVAL(-1));
  3908. ClassDB::bind_method(D_METHOD("move_terrain", "terrain_set", "terrain_index", "to_position"), &TileSet::move_terrain);
  3909. ClassDB::bind_method(D_METHOD("remove_terrain", "terrain_set", "terrain_index"), &TileSet::remove_terrain);
  3910. ClassDB::bind_method(D_METHOD("set_terrain_name", "terrain_set", "terrain_index", "name"), &TileSet::set_terrain_name);
  3911. ClassDB::bind_method(D_METHOD("get_terrain_name", "terrain_set", "terrain_index"), &TileSet::get_terrain_name);
  3912. ClassDB::bind_method(D_METHOD("set_terrain_color", "terrain_set", "terrain_index", "color"), &TileSet::set_terrain_color);
  3913. ClassDB::bind_method(D_METHOD("get_terrain_color", "terrain_set", "terrain_index"), &TileSet::get_terrain_color);
  3914. #ifndef NAVIGATION_2D_DISABLED
  3915. // Navigation
  3916. ClassDB::bind_method(D_METHOD("get_navigation_layers_count"), &TileSet::get_navigation_layers_count);
  3917. ClassDB::bind_method(D_METHOD("add_navigation_layer", "to_position"), &TileSet::add_navigation_layer, DEFVAL(-1));
  3918. ClassDB::bind_method(D_METHOD("move_navigation_layer", "layer_index", "to_position"), &TileSet::move_navigation_layer);
  3919. ClassDB::bind_method(D_METHOD("remove_navigation_layer", "layer_index"), &TileSet::remove_navigation_layer);
  3920. ClassDB::bind_method(D_METHOD("set_navigation_layer_layers", "layer_index", "layers"), &TileSet::set_navigation_layer_layers);
  3921. ClassDB::bind_method(D_METHOD("get_navigation_layer_layers", "layer_index"), &TileSet::get_navigation_layer_layers);
  3922. ClassDB::bind_method(D_METHOD("set_navigation_layer_layer_value", "layer_index", "layer_number", "value"), &TileSet::set_navigation_layer_layer_value);
  3923. ClassDB::bind_method(D_METHOD("get_navigation_layer_layer_value", "layer_index", "layer_number"), &TileSet::get_navigation_layer_layer_value);
  3924. #endif // NAVIGATION_2D_DISABLED
  3925. // Custom data
  3926. ClassDB::bind_method(D_METHOD("get_custom_data_layers_count"), &TileSet::get_custom_data_layers_count);
  3927. ClassDB::bind_method(D_METHOD("add_custom_data_layer", "to_position"), &TileSet::add_custom_data_layer, DEFVAL(-1));
  3928. ClassDB::bind_method(D_METHOD("move_custom_data_layer", "layer_index", "to_position"), &TileSet::move_custom_data_layer);
  3929. ClassDB::bind_method(D_METHOD("remove_custom_data_layer", "layer_index"), &TileSet::remove_custom_data_layer);
  3930. ClassDB::bind_method(D_METHOD("get_custom_data_layer_by_name", "layer_name"), &TileSet::get_custom_data_layer_by_name);
  3931. ClassDB::bind_method(D_METHOD("set_custom_data_layer_name", "layer_index", "layer_name"), &TileSet::set_custom_data_layer_name);
  3932. ClassDB::bind_method(D_METHOD("has_custom_data_layer_by_name", "layer_name"), &TileSet::has_custom_data_layer_by_name);
  3933. ClassDB::bind_method(D_METHOD("get_custom_data_layer_name", "layer_index"), &TileSet::get_custom_data_layer_name);
  3934. ClassDB::bind_method(D_METHOD("set_custom_data_layer_type", "layer_index", "layer_type"), &TileSet::set_custom_data_layer_type);
  3935. ClassDB::bind_method(D_METHOD("get_custom_data_layer_type", "layer_index"), &TileSet::get_custom_data_layer_type);
  3936. // Tile proxies
  3937. ClassDB::bind_method(D_METHOD("set_source_level_tile_proxy", "source_from", "source_to"), &TileSet::set_source_level_tile_proxy);
  3938. ClassDB::bind_method(D_METHOD("get_source_level_tile_proxy", "source_from"), &TileSet::get_source_level_tile_proxy);
  3939. ClassDB::bind_method(D_METHOD("has_source_level_tile_proxy", "source_from"), &TileSet::has_source_level_tile_proxy);
  3940. ClassDB::bind_method(D_METHOD("remove_source_level_tile_proxy", "source_from"), &TileSet::remove_source_level_tile_proxy);
  3941. 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);
  3942. ClassDB::bind_method(D_METHOD("get_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::get_coords_level_tile_proxy);
  3943. ClassDB::bind_method(D_METHOD("has_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::has_coords_level_tile_proxy);
  3944. ClassDB::bind_method(D_METHOD("remove_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::remove_coords_level_tile_proxy);
  3945. 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);
  3946. ClassDB::bind_method(D_METHOD("get_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::get_alternative_level_tile_proxy);
  3947. ClassDB::bind_method(D_METHOD("has_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::has_alternative_level_tile_proxy);
  3948. ClassDB::bind_method(D_METHOD("remove_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::remove_alternative_level_tile_proxy);
  3949. ClassDB::bind_method(D_METHOD("map_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::map_tile_proxy);
  3950. ClassDB::bind_method(D_METHOD("cleanup_invalid_tile_proxies"), &TileSet::cleanup_invalid_tile_proxies);
  3951. ClassDB::bind_method(D_METHOD("clear_tile_proxies"), &TileSet::clear_tile_proxies);
  3952. // Patterns
  3953. ClassDB::bind_method(D_METHOD("add_pattern", "pattern", "index"), &TileSet::add_pattern, DEFVAL(-1));
  3954. ClassDB::bind_method(D_METHOD("get_pattern", "index"), &TileSet::get_pattern, DEFVAL(-1));
  3955. ClassDB::bind_method(D_METHOD("remove_pattern", "index"), &TileSet::remove_pattern);
  3956. ClassDB::bind_method(D_METHOD("get_patterns_count"), &TileSet::get_patterns_count);
  3957. ADD_GROUP("Rendering", "");
  3958. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uv_clipping"), "set_uv_clipping", "is_uv_clipping");
  3959. ADD_ARRAY("occlusion_layers", "occlusion_layer_");
  3960. ADD_GROUP("", "");
  3961. ADD_ARRAY("physics_layers", "physics_layer_");
  3962. ADD_ARRAY("terrain_sets", "terrain_set_");
  3963. #ifndef NAVIGATION_2D_DISABLED
  3964. ADD_ARRAY("navigation_layers", "navigation_layer_");
  3965. #endif // NAVIGATION_2D_DISABLED
  3966. ADD_ARRAY("custom_data_layers", "custom_data_layer_");
  3967. // -- Enum binding --
  3968. BIND_ENUM_CONSTANT(TILE_SHAPE_SQUARE);
  3969. BIND_ENUM_CONSTANT(TILE_SHAPE_ISOMETRIC);
  3970. BIND_ENUM_CONSTANT(TILE_SHAPE_HALF_OFFSET_SQUARE);
  3971. BIND_ENUM_CONSTANT(TILE_SHAPE_HEXAGON);
  3972. BIND_ENUM_CONSTANT(TILE_LAYOUT_STACKED);
  3973. BIND_ENUM_CONSTANT(TILE_LAYOUT_STACKED_OFFSET);
  3974. BIND_ENUM_CONSTANT(TILE_LAYOUT_STAIRS_RIGHT);
  3975. BIND_ENUM_CONSTANT(TILE_LAYOUT_STAIRS_DOWN);
  3976. BIND_ENUM_CONSTANT(TILE_LAYOUT_DIAMOND_RIGHT);
  3977. BIND_ENUM_CONSTANT(TILE_LAYOUT_DIAMOND_DOWN);
  3978. BIND_ENUM_CONSTANT(TILE_OFFSET_AXIS_HORIZONTAL);
  3979. BIND_ENUM_CONSTANT(TILE_OFFSET_AXIS_VERTICAL);
  3980. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_RIGHT_SIDE);
  3981. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_RIGHT_CORNER);
  3982. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE);
  3983. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER);
  3984. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_SIDE);
  3985. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_CORNER);
  3986. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_LEFT_SIDE);
  3987. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_LEFT_CORNER);
  3988. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_LEFT_SIDE);
  3989. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_LEFT_CORNER);
  3990. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_LEFT_SIDE);
  3991. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_LEFT_CORNER);
  3992. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_SIDE);
  3993. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_CORNER);
  3994. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  3995. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_RIGHT_CORNER);
  3996. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_CORNERS_AND_SIDES);
  3997. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_CORNERS);
  3998. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_SIDES);
  3999. }
  4000. TileSet::TileSet() {
  4001. // Instantiate the tile meshes.
  4002. tile_lines_mesh.instantiate();
  4003. tile_filled_mesh.instantiate();
  4004. }
  4005. TileSet::~TileSet() {
  4006. #ifndef DISABLE_DEPRECATED
  4007. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  4008. memdelete(E.value);
  4009. }
  4010. #endif // DISABLE_DEPRECATED
  4011. while (!source_ids.is_empty()) {
  4012. remove_source(source_ids[0]);
  4013. }
  4014. }
  4015. /////////////////////////////// TileSetSource //////////////////////////////////////
  4016. void TileSetSource::set_tile_set(const TileSet *p_tile_set) {
  4017. tile_set = p_tile_set;
  4018. }
  4019. TileSet *TileSetSource::get_tile_set() const {
  4020. return (TileSet *)tile_set;
  4021. }
  4022. void TileSetSource::reset_state() {
  4023. tile_set = nullptr;
  4024. }
  4025. void TileSetSource::_bind_methods() {
  4026. // Base tiles
  4027. ClassDB::bind_method(D_METHOD("get_tiles_count"), &TileSetSource::get_tiles_count);
  4028. ClassDB::bind_method(D_METHOD("get_tile_id", "index"), &TileSetSource::get_tile_id);
  4029. ClassDB::bind_method(D_METHOD("has_tile", "atlas_coords"), &TileSetSource::has_tile);
  4030. // Alternative tiles
  4031. ClassDB::bind_method(D_METHOD("get_alternative_tiles_count", "atlas_coords"), &TileSetSource::get_alternative_tiles_count);
  4032. ClassDB::bind_method(D_METHOD("get_alternative_tile_id", "atlas_coords", "index"), &TileSetSource::get_alternative_tile_id);
  4033. ClassDB::bind_method(D_METHOD("has_alternative_tile", "atlas_coords", "alternative_tile"), &TileSetSource::has_alternative_tile);
  4034. }
  4035. /////////////////////////////// TileSetAtlasSource //////////////////////////////////////
  4036. void TileSetAtlasSource::set_tile_set(const TileSet *p_tile_set) {
  4037. tile_set = p_tile_set;
  4038. // Set the TileSet on all TileData.
  4039. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  4040. for (KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  4041. E_alternative.value->set_tile_set(tile_set);
  4042. }
  4043. }
  4044. }
  4045. const TileSet *TileSetAtlasSource::get_tile_set() const {
  4046. return tile_set;
  4047. }
  4048. void TileSetAtlasSource::notify_tile_data_properties_should_change() {
  4049. // Set the TileSet on all TileData.
  4050. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  4051. for (KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  4052. E_alternative.value->notify_tile_data_properties_should_change();
  4053. }
  4054. }
  4055. }
  4056. void TileSetAtlasSource::add_occlusion_layer(int p_to_pos) {
  4057. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4058. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4059. E_alternative.value->add_occlusion_layer(p_to_pos);
  4060. }
  4061. }
  4062. }
  4063. void TileSetAtlasSource::move_occlusion_layer(int p_from_index, int p_to_pos) {
  4064. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4065. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4066. E_alternative.value->move_occlusion_layer(p_from_index, p_to_pos);
  4067. }
  4068. }
  4069. }
  4070. void TileSetAtlasSource::remove_occlusion_layer(int p_index) {
  4071. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4072. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4073. E_alternative.value->remove_occlusion_layer(p_index);
  4074. }
  4075. }
  4076. }
  4077. #ifndef PHYSICS_2D_DISABLED
  4078. void TileSetAtlasSource::add_physics_layer(int p_to_pos) {
  4079. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4080. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4081. E_alternative.value->add_physics_layer(p_to_pos);
  4082. }
  4083. }
  4084. }
  4085. void TileSetAtlasSource::move_physics_layer(int p_from_index, int p_to_pos) {
  4086. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4087. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4088. E_alternative.value->move_physics_layer(p_from_index, p_to_pos);
  4089. }
  4090. }
  4091. }
  4092. void TileSetAtlasSource::remove_physics_layer(int p_index) {
  4093. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4094. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4095. E_alternative.value->remove_physics_layer(p_index);
  4096. }
  4097. }
  4098. }
  4099. #endif // PHYSICS_2D_DISABLED
  4100. void TileSetAtlasSource::add_terrain_set(int p_to_pos) {
  4101. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4102. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4103. E_alternative.value->add_terrain_set(p_to_pos);
  4104. }
  4105. }
  4106. }
  4107. void TileSetAtlasSource::move_terrain_set(int p_from_index, int p_to_pos) {
  4108. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4109. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4110. E_alternative.value->move_terrain_set(p_from_index, p_to_pos);
  4111. }
  4112. }
  4113. }
  4114. void TileSetAtlasSource::remove_terrain_set(int p_index) {
  4115. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4116. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4117. E_alternative.value->remove_terrain_set(p_index);
  4118. }
  4119. }
  4120. }
  4121. void TileSetAtlasSource::add_terrain(int p_terrain_set, int p_to_pos) {
  4122. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4123. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4124. E_alternative.value->add_terrain(p_terrain_set, p_to_pos);
  4125. }
  4126. }
  4127. }
  4128. void TileSetAtlasSource::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  4129. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4130. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4131. E_alternative.value->move_terrain(p_terrain_set, p_from_index, p_to_pos);
  4132. }
  4133. }
  4134. }
  4135. void TileSetAtlasSource::remove_terrain(int p_terrain_set, int p_index) {
  4136. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4137. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4138. E_alternative.value->remove_terrain(p_terrain_set, p_index);
  4139. }
  4140. }
  4141. }
  4142. #ifndef NAVIGATION_2D_DISABLED
  4143. void TileSetAtlasSource::add_navigation_layer(int p_to_pos) {
  4144. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4145. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4146. E_alternative.value->add_navigation_layer(p_to_pos);
  4147. }
  4148. }
  4149. }
  4150. void TileSetAtlasSource::move_navigation_layer(int p_from_index, int p_to_pos) {
  4151. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4152. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4153. E_alternative.value->move_navigation_layer(p_from_index, p_to_pos);
  4154. }
  4155. }
  4156. }
  4157. void TileSetAtlasSource::remove_navigation_layer(int p_index) {
  4158. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4159. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4160. E_alternative.value->remove_navigation_layer(p_index);
  4161. }
  4162. }
  4163. }
  4164. #endif // NAVIGATION_2D_DISABLED
  4165. void TileSetAtlasSource::add_custom_data_layer(int p_to_pos) {
  4166. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4167. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4168. E_alternative.value->add_custom_data_layer(p_to_pos);
  4169. }
  4170. }
  4171. }
  4172. void TileSetAtlasSource::move_custom_data_layer(int p_from_index, int p_to_pos) {
  4173. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4174. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4175. E_alternative.value->move_custom_data_layer(p_from_index, p_to_pos);
  4176. }
  4177. }
  4178. }
  4179. void TileSetAtlasSource::remove_custom_data_layer(int p_index) {
  4180. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  4181. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  4182. E_alternative.value->remove_custom_data_layer(p_index);
  4183. }
  4184. }
  4185. }
  4186. void TileSetAtlasSource::reset_state() {
  4187. tile_set = nullptr;
  4188. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  4189. for (const KeyValue<int, TileData *> &E_tile_data : E_tile.value.alternatives) {
  4190. memdelete(E_tile_data.value);
  4191. }
  4192. }
  4193. _coords_mapping_cache.clear();
  4194. tiles.clear();
  4195. tiles_ids.clear();
  4196. _queue_update_padded_texture();
  4197. }
  4198. void TileSetAtlasSource::set_texture(Ref<Texture2D> p_texture) {
  4199. if (texture.is_valid()) {
  4200. texture->disconnect_changed(callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  4201. }
  4202. texture = p_texture;
  4203. if (texture.is_valid()) {
  4204. texture->connect_changed(callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  4205. }
  4206. _queue_update_padded_texture();
  4207. emit_changed();
  4208. }
  4209. Ref<Texture2D> TileSetAtlasSource::get_texture() const {
  4210. return texture;
  4211. }
  4212. void TileSetAtlasSource::set_margins(Vector2i p_margins) {
  4213. if (p_margins.x < 0 || p_margins.y < 0) {
  4214. WARN_PRINT("Atlas source margins should be positive.");
  4215. margins = p_margins.maxi(0);
  4216. } else {
  4217. margins = p_margins;
  4218. }
  4219. _queue_update_padded_texture();
  4220. emit_changed();
  4221. }
  4222. Vector2i TileSetAtlasSource::get_margins() const {
  4223. return margins;
  4224. }
  4225. void TileSetAtlasSource::set_separation(Vector2i p_separation) {
  4226. if (p_separation.x < 0 || p_separation.y < 0) {
  4227. WARN_PRINT("Atlas source separation should be positive.");
  4228. separation = p_separation.maxi(0);
  4229. } else {
  4230. separation = p_separation;
  4231. }
  4232. _queue_update_padded_texture();
  4233. emit_changed();
  4234. }
  4235. Vector2i TileSetAtlasSource::get_separation() const {
  4236. return separation;
  4237. }
  4238. void TileSetAtlasSource::set_texture_region_size(Vector2i p_tile_size) {
  4239. if (p_tile_size.x <= 0 || p_tile_size.y <= 0) {
  4240. WARN_PRINT("Atlas source tile_size should be strictly positive.");
  4241. texture_region_size = p_tile_size.maxi(1);
  4242. } else {
  4243. texture_region_size = p_tile_size;
  4244. }
  4245. _queue_update_padded_texture();
  4246. emit_changed();
  4247. }
  4248. Vector2i TileSetAtlasSource::get_texture_region_size() const {
  4249. return texture_region_size;
  4250. }
  4251. void TileSetAtlasSource::set_use_texture_padding(bool p_use_padding) {
  4252. if (use_texture_padding == p_use_padding) {
  4253. return;
  4254. }
  4255. use_texture_padding = p_use_padding;
  4256. _queue_update_padded_texture();
  4257. emit_changed();
  4258. }
  4259. bool TileSetAtlasSource::get_use_texture_padding() const {
  4260. return use_texture_padding;
  4261. }
  4262. Vector2i TileSetAtlasSource::get_atlas_grid_size() const {
  4263. Ref<Texture2D> txt = get_texture();
  4264. if (txt.is_null()) {
  4265. return Vector2i();
  4266. }
  4267. ERR_FAIL_COND_V(texture_region_size.x <= 0 || texture_region_size.y <= 0, Vector2i());
  4268. Size2i valid_area = txt->get_size() - margins;
  4269. // Compute the number of valid tiles in the tiles atlas
  4270. Size2i grid_size;
  4271. if (valid_area.x >= texture_region_size.x && valid_area.y >= texture_region_size.y) {
  4272. valid_area -= texture_region_size;
  4273. grid_size = Size2i(1, 1) + valid_area / (texture_region_size + separation);
  4274. }
  4275. return grid_size;
  4276. }
  4277. bool TileSetAtlasSource::_set(const StringName &p_name, const Variant &p_value) {
  4278. Vector<String> components = String(p_name).split("/", true, 2);
  4279. // Compute the vector2i if we have coordinates.
  4280. Vector<String> coords_split = components[0].split(":");
  4281. Vector2i coords = TileSetSource::INVALID_ATLAS_COORDS;
  4282. if (coords_split.size() == 2 && coords_split[0].is_valid_int() && coords_split[1].is_valid_int()) {
  4283. coords = Vector2i(coords_split[0].to_int(), coords_split[1].to_int());
  4284. }
  4285. // Properties.
  4286. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  4287. // Create the tile if needed.
  4288. if (!has_tile(coords)) {
  4289. create_tile(coords);
  4290. }
  4291. if (components.size() >= 2) {
  4292. // Properties.
  4293. if (components[1] == "size_in_atlas") {
  4294. move_tile_in_atlas(coords, coords, p_value);
  4295. return true;
  4296. } else if (components[1] == "next_alternative_id") {
  4297. tiles[coords].next_alternative_id = p_value;
  4298. return true;
  4299. } else if (components[1] == "animation_columns") {
  4300. set_tile_animation_columns(coords, p_value);
  4301. return true;
  4302. } else if (components[1] == "animation_separation") {
  4303. set_tile_animation_separation(coords, p_value);
  4304. return true;
  4305. } else if (components[1] == "animation_speed") {
  4306. set_tile_animation_speed(coords, p_value);
  4307. return true;
  4308. } else if (components[1] == "animation_mode") {
  4309. set_tile_animation_mode(coords, VariantCaster<TileSetAtlasSource::TileAnimationMode>::cast(p_value));
  4310. return true;
  4311. } else if (components[1] == "animation_frames_count") {
  4312. set_tile_animation_frames_count(coords, p_value);
  4313. return true;
  4314. } else if (components.size() >= 3 && components[1].begins_with("animation_frame_") && components[1].trim_prefix("animation_frame_").is_valid_int()) {
  4315. int frame = components[1].trim_prefix("animation_frame_").to_int();
  4316. if (components[2] == "duration") {
  4317. if (frame >= get_tile_animation_frames_count(coords)) {
  4318. set_tile_animation_frames_count(coords, frame + 1);
  4319. }
  4320. set_tile_animation_frame_duration(coords, frame, p_value);
  4321. return true;
  4322. }
  4323. return false;
  4324. } else if (components[1].is_valid_int()) {
  4325. int alternative_id = components[1].to_int();
  4326. if (alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE) {
  4327. // Create the alternative if needed ?
  4328. if (!has_alternative_tile(coords, alternative_id)) {
  4329. create_alternative_tile(coords, alternative_id);
  4330. }
  4331. if (!tiles[coords].alternatives.has(alternative_id)) {
  4332. tiles[coords].alternatives[alternative_id] = memnew(TileData);
  4333. tiles[coords].alternatives[alternative_id]->set_tile_set(tile_set);
  4334. tiles[coords].alternatives[alternative_id]->set_allow_transform(alternative_id > 0);
  4335. tiles[coords].alternatives_ids.push_back(alternative_id);
  4336. }
  4337. if (components.size() >= 3) {
  4338. bool valid;
  4339. tiles[coords].alternatives[alternative_id]->set(components[2], p_value, &valid);
  4340. return valid;
  4341. } else {
  4342. // Only create the alternative if it did not exist yet.
  4343. return true;
  4344. }
  4345. }
  4346. }
  4347. }
  4348. }
  4349. return false;
  4350. }
  4351. bool TileSetAtlasSource::_get(const StringName &p_name, Variant &r_ret) const {
  4352. Vector<String> components = String(p_name).split("/", true, 2);
  4353. // Properties.
  4354. Vector<String> coords_split = components[0].split(":");
  4355. if (coords_split.size() == 2 && coords_split[0].is_valid_int() && coords_split[1].is_valid_int()) {
  4356. Vector2i coords = Vector2i(coords_split[0].to_int(), coords_split[1].to_int());
  4357. if (tiles.has(coords)) {
  4358. if (components.size() >= 2) {
  4359. // Properties.
  4360. if (components[1] == "size_in_atlas") {
  4361. r_ret = tiles[coords].size_in_atlas;
  4362. return true;
  4363. } else if (components[1] == "next_alternative_id") {
  4364. r_ret = tiles[coords].next_alternative_id;
  4365. return true;
  4366. } else if (components[1] == "animation_columns") {
  4367. r_ret = get_tile_animation_columns(coords);
  4368. return true;
  4369. } else if (components[1] == "animation_separation") {
  4370. r_ret = get_tile_animation_separation(coords);
  4371. return true;
  4372. } else if (components[1] == "animation_speed") {
  4373. r_ret = get_tile_animation_speed(coords);
  4374. return true;
  4375. } else if (components[1] == "animation_mode") {
  4376. r_ret = get_tile_animation_mode(coords);
  4377. return true;
  4378. } else if (components[1] == "animation_frames_count") {
  4379. r_ret = get_tile_animation_frames_count(coords);
  4380. return true;
  4381. } else if (components.size() >= 3 && components[1].begins_with("animation_frame_") && components[1].trim_prefix("animation_frame_").is_valid_int()) {
  4382. int frame = components[1].trim_prefix("animation_frame_").to_int();
  4383. if (frame < 0 || frame >= get_tile_animation_frames_count(coords)) {
  4384. return false;
  4385. }
  4386. if (components[2] == "duration") {
  4387. r_ret = get_tile_animation_frame_duration(coords, frame);
  4388. return true;
  4389. }
  4390. return false;
  4391. } else if (components[1].is_valid_int()) {
  4392. int alternative_id = components[1].to_int();
  4393. if (alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE && tiles[coords].alternatives.has(alternative_id)) {
  4394. if (components.size() >= 3) {
  4395. bool valid;
  4396. r_ret = tiles[coords].alternatives[alternative_id]->get(components[2], &valid);
  4397. return valid;
  4398. } else {
  4399. // Only to notify the tile alternative exists.
  4400. r_ret = alternative_id;
  4401. return true;
  4402. }
  4403. }
  4404. }
  4405. }
  4406. }
  4407. }
  4408. return false;
  4409. }
  4410. void TileSetAtlasSource::_get_property_list(List<PropertyInfo> *p_list) const {
  4411. // Atlases data.
  4412. PropertyInfo property_info;
  4413. for (const KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  4414. List<PropertyInfo> tile_property_list;
  4415. // size_in_atlas
  4416. property_info = PropertyInfo(Variant::VECTOR2I, "size_in_atlas", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  4417. if (E_tile.value.size_in_atlas == Vector2i(1, 1)) {
  4418. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4419. }
  4420. tile_property_list.push_back(property_info);
  4421. // next_alternative_id
  4422. property_info = PropertyInfo(Variant::INT, "next_alternative_id", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  4423. if (E_tile.value.next_alternative_id == 1) {
  4424. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4425. }
  4426. tile_property_list.push_back(property_info);
  4427. // animation_columns.
  4428. property_info = PropertyInfo(Variant::INT, "animation_columns", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  4429. if (E_tile.value.animation_columns == 0) {
  4430. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4431. }
  4432. tile_property_list.push_back(property_info);
  4433. // animation_separation.
  4434. property_info = PropertyInfo(Variant::INT, "animation_separation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  4435. if (E_tile.value.animation_separation == Vector2i()) {
  4436. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4437. }
  4438. tile_property_list.push_back(property_info);
  4439. // animation_speed.
  4440. property_info = PropertyInfo(Variant::FLOAT, "animation_speed", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  4441. if (E_tile.value.animation_speed == 1.0) {
  4442. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4443. }
  4444. tile_property_list.push_back(property_info);
  4445. // animation_mode.
  4446. property_info = PropertyInfo(Variant::INT, "animation_mode", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  4447. if (E_tile.value.animation_mode == TILE_ANIMATION_MODE_DEFAULT) {
  4448. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4449. }
  4450. tile_property_list.push_back(property_info);
  4451. // animation_frames_count.
  4452. tile_property_list.push_back(PropertyInfo(Variant::INT, "animation_frames_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  4453. // animation_frame_*.
  4454. bool store_durations = tiles[E_tile.key].animation_frames_durations.size() >= 2;
  4455. for (int i = 0; i < (int)tiles[E_tile.key].animation_frames_durations.size(); i++) {
  4456. property_info = PropertyInfo(Variant::FLOAT, vformat("animation_frame_%d/duration", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  4457. if (!store_durations) {
  4458. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4459. }
  4460. tile_property_list.push_back(property_info);
  4461. }
  4462. for (const KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  4463. const String formatted_key = itos(E_alternative.key);
  4464. // Add a dummy property to show the alternative exists.
  4465. tile_property_list.push_back(PropertyInfo(Variant::INT, formatted_key, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  4466. // Get the alternative tile's properties and append them to the list of properties.
  4467. const String alternative_property_info_prefix = formatted_key + '/';
  4468. List<PropertyInfo> alternative_property_list;
  4469. E_alternative.value->get_property_list(&alternative_property_list);
  4470. for (PropertyInfo &alternative_property_info : alternative_property_list) {
  4471. Variant default_value = ClassDB::class_get_default_property_value("TileData", alternative_property_info.name);
  4472. Variant value = E_alternative.value->get(alternative_property_info.name);
  4473. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {
  4474. alternative_property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4475. }
  4476. alternative_property_info.name = alternative_property_info_prefix + alternative_property_info.name;
  4477. tile_property_list.push_back(alternative_property_info);
  4478. }
  4479. }
  4480. // Add all alternative.
  4481. const String property_info_prefix = vformat("%d:%d/", E_tile.key.x, E_tile.key.y);
  4482. for (PropertyInfo &tile_property_info : tile_property_list) {
  4483. tile_property_info.name = property_info_prefix + tile_property_info.name;
  4484. p_list->push_back(tile_property_info);
  4485. }
  4486. }
  4487. }
  4488. void TileSetAtlasSource::create_tile(const Vector2i p_atlas_coords, const Vector2i p_size) {
  4489. // Create a tile if it does not exists.
  4490. ERR_FAIL_COND(p_atlas_coords.x < 0 || p_atlas_coords.y < 0);
  4491. ERR_FAIL_COND(p_size.x <= 0 || p_size.y <= 0);
  4492. bool room_for_tile = has_room_for_tile(p_atlas_coords, p_size, 1, Vector2i(), 1);
  4493. 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.");
  4494. // Initialize the tile data.
  4495. TileAlternativesData tad;
  4496. tad.size_in_atlas = p_size;
  4497. tad.animation_frames_durations.push_back(1.0);
  4498. tad.alternatives[0] = memnew(TileData);
  4499. tad.alternatives[0]->set_tile_set(tile_set);
  4500. tad.alternatives[0]->set_allow_transform(false);
  4501. tad.alternatives[0]->connect(CoreStringName(changed), callable_mp((Resource *)this, &TileSetAtlasSource::emit_changed));
  4502. tad.alternatives[0]->notify_property_list_changed();
  4503. tad.alternatives_ids.push_back(0);
  4504. // Create and resize the tile.
  4505. tiles.insert(p_atlas_coords, tad);
  4506. tiles_ids.push_back(p_atlas_coords);
  4507. tiles_ids.sort();
  4508. _create_coords_mapping_cache(p_atlas_coords);
  4509. _queue_update_padded_texture();
  4510. _try_emit_changed();
  4511. }
  4512. void TileSetAtlasSource::remove_tile(Vector2i p_atlas_coords) {
  4513. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4514. // Remove all covered positions from the mapping cache
  4515. _clear_coords_mapping_cache(p_atlas_coords);
  4516. // Free tile data.
  4517. for (const KeyValue<int, TileData *> &E_tile_data : tiles[p_atlas_coords].alternatives) {
  4518. memdelete(E_tile_data.value);
  4519. }
  4520. // Delete the tile
  4521. tiles.erase(p_atlas_coords);
  4522. tiles_ids.erase(p_atlas_coords);
  4523. tiles_ids.sort();
  4524. _queue_update_padded_texture();
  4525. _try_emit_changed();
  4526. }
  4527. bool TileSetAtlasSource::has_tile(Vector2i p_atlas_coords) const {
  4528. return tiles.has(p_atlas_coords);
  4529. }
  4530. Vector2i TileSetAtlasSource::get_tile_at_coords(Vector2i p_atlas_coords) const {
  4531. if (!_coords_mapping_cache.has(p_atlas_coords)) {
  4532. return INVALID_ATLAS_COORDS;
  4533. }
  4534. return _coords_mapping_cache[p_atlas_coords];
  4535. }
  4536. void TileSetAtlasSource::set_tile_animation_columns(const Vector2i p_atlas_coords, int p_frame_columns) {
  4537. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4538. ERR_FAIL_COND(p_frame_columns < 0);
  4539. TileAlternativesData &tad = tiles[p_atlas_coords];
  4540. 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);
  4541. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  4542. _clear_coords_mapping_cache(p_atlas_coords);
  4543. tiles[p_atlas_coords].animation_columns = p_frame_columns;
  4544. _create_coords_mapping_cache(p_atlas_coords);
  4545. _queue_update_padded_texture();
  4546. _try_emit_changed();
  4547. }
  4548. int TileSetAtlasSource::get_tile_animation_columns(const Vector2i p_atlas_coords) const {
  4549. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4550. return tiles[p_atlas_coords].animation_columns;
  4551. }
  4552. void TileSetAtlasSource::set_tile_animation_separation(const Vector2i p_atlas_coords, const Vector2i p_separation) {
  4553. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4554. ERR_FAIL_COND(p_separation.x < 0 || p_separation.y < 0);
  4555. TileAlternativesData &tad = tiles[p_atlas_coords];
  4556. 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);
  4557. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  4558. _clear_coords_mapping_cache(p_atlas_coords);
  4559. tiles[p_atlas_coords].animation_separation = p_separation;
  4560. _create_coords_mapping_cache(p_atlas_coords);
  4561. _queue_update_padded_texture();
  4562. _try_emit_changed();
  4563. }
  4564. Vector2i TileSetAtlasSource::get_tile_animation_separation(const Vector2i p_atlas_coords) const {
  4565. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4566. return tiles[p_atlas_coords].animation_separation;
  4567. }
  4568. void TileSetAtlasSource::set_tile_animation_speed(const Vector2i p_atlas_coords, real_t p_speed) {
  4569. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4570. ERR_FAIL_COND(p_speed <= 0);
  4571. tiles[p_atlas_coords].animation_speed = p_speed;
  4572. _try_emit_changed();
  4573. }
  4574. real_t TileSetAtlasSource::get_tile_animation_speed(const Vector2i p_atlas_coords) const {
  4575. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1.0, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4576. return tiles[p_atlas_coords].animation_speed;
  4577. }
  4578. void TileSetAtlasSource::set_tile_animation_mode(const Vector2i p_atlas_coords, TileSetAtlasSource::TileAnimationMode p_mode) {
  4579. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4580. tiles[p_atlas_coords].animation_mode = p_mode;
  4581. _try_emit_changed();
  4582. }
  4583. TileSetAtlasSource::TileAnimationMode TileSetAtlasSource::get_tile_animation_mode(const Vector2i p_atlas_coords) const {
  4584. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), TILE_ANIMATION_MODE_DEFAULT, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4585. return tiles[p_atlas_coords].animation_mode;
  4586. }
  4587. void TileSetAtlasSource::set_tile_animation_frames_count(const Vector2i p_atlas_coords, int p_frames_count) {
  4588. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4589. ERR_FAIL_COND(p_frames_count < 1);
  4590. int old_size = tiles[p_atlas_coords].animation_frames_durations.size();
  4591. if (p_frames_count == old_size) {
  4592. return;
  4593. }
  4594. TileAlternativesData &tad = tiles[p_atlas_coords];
  4595. 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);
  4596. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  4597. _clear_coords_mapping_cache(p_atlas_coords);
  4598. tiles[p_atlas_coords].animation_frames_durations.resize(p_frames_count);
  4599. for (int i = old_size; i < p_frames_count; i++) {
  4600. tiles[p_atlas_coords].animation_frames_durations[i] = 1.0;
  4601. }
  4602. _create_coords_mapping_cache(p_atlas_coords);
  4603. _queue_update_padded_texture();
  4604. notify_property_list_changed();
  4605. _try_emit_changed();
  4606. }
  4607. int TileSetAtlasSource::get_tile_animation_frames_count(const Vector2i p_atlas_coords) const {
  4608. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4609. return tiles[p_atlas_coords].animation_frames_durations.size();
  4610. }
  4611. void TileSetAtlasSource::set_tile_animation_frame_duration(const Vector2i p_atlas_coords, int p_frame_index, real_t p_duration) {
  4612. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4613. ERR_FAIL_INDEX(p_frame_index, (int)tiles[p_atlas_coords].animation_frames_durations.size());
  4614. ERR_FAIL_COND(p_duration <= 0.0);
  4615. tiles[p_atlas_coords].animation_frames_durations[p_frame_index] = p_duration;
  4616. _try_emit_changed();
  4617. }
  4618. real_t TileSetAtlasSource::get_tile_animation_frame_duration(const Vector2i p_atlas_coords, int p_frame_index) const {
  4619. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4620. ERR_FAIL_INDEX_V(p_frame_index, (int)tiles[p_atlas_coords].animation_frames_durations.size(), 0.0);
  4621. return tiles[p_atlas_coords].animation_frames_durations[p_frame_index];
  4622. }
  4623. real_t TileSetAtlasSource::get_tile_animation_total_duration(const Vector2i p_atlas_coords) const {
  4624. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4625. real_t sum = 0.0;
  4626. for (const real_t &duration : tiles[p_atlas_coords].animation_frames_durations) {
  4627. sum += duration;
  4628. }
  4629. return sum;
  4630. }
  4631. Vector2i TileSetAtlasSource::get_tile_size_in_atlas(Vector2i p_atlas_coords) const {
  4632. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(-1, -1), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4633. return tiles[p_atlas_coords].size_in_atlas;
  4634. }
  4635. int TileSetAtlasSource::get_tiles_count() const {
  4636. return tiles_ids.size();
  4637. }
  4638. Vector2i TileSetAtlasSource::get_tile_id(int p_index) const {
  4639. ERR_FAIL_INDEX_V(p_index, tiles_ids.size(), TileSetSource::INVALID_ATLAS_COORDS);
  4640. return tiles_ids[p_index];
  4641. }
  4642. 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 {
  4643. if (p_atlas_coords.x < 0 || p_atlas_coords.y < 0) {
  4644. return false;
  4645. }
  4646. if (p_size.x <= 0 || p_size.y <= 0) {
  4647. return false;
  4648. }
  4649. if (p_frames_count <= 0) {
  4650. return false;
  4651. }
  4652. Size2i atlas_grid_size = get_atlas_grid_size();
  4653. for (int frame = 0; frame < p_frames_count; frame++) {
  4654. 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));
  4655. for (int x = 0; x < p_size.x; x++) {
  4656. for (int y = 0; y < p_size.y; y++) {
  4657. Vector2i coords = frame_coords + Vector2i(x, y);
  4658. if (_coords_mapping_cache.has(coords) && _coords_mapping_cache[coords] != p_ignored_tile) {
  4659. return false;
  4660. }
  4661. if (coords.x >= atlas_grid_size.x || coords.y >= atlas_grid_size.y) {
  4662. return false;
  4663. }
  4664. }
  4665. }
  4666. }
  4667. return true;
  4668. }
  4669. bool TileSetAtlasSource::has_tiles_outside_texture() const {
  4670. for (const KeyValue<Vector2i, TileSetAtlasSource::TileAlternativesData> &E : tiles) {
  4671. 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)) {
  4672. return true;
  4673. }
  4674. }
  4675. return false;
  4676. }
  4677. Vector<Vector2i> TileSetAtlasSource::get_tiles_outside_texture() const {
  4678. Vector<Vector2i> to_return;
  4679. for (const KeyValue<Vector2i, TileSetAtlasSource::TileAlternativesData> &E : tiles) {
  4680. 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)) {
  4681. to_return.push_back(E.key);
  4682. }
  4683. }
  4684. return to_return;
  4685. }
  4686. void TileSetAtlasSource::clear_tiles_outside_texture() {
  4687. LocalVector<Vector2i> to_remove;
  4688. for (const KeyValue<Vector2i, TileSetAtlasSource::TileAlternativesData> &E : tiles) {
  4689. 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)) {
  4690. to_remove.push_back(E.key);
  4691. }
  4692. }
  4693. for (const Vector2i &v : to_remove) {
  4694. remove_tile(v);
  4695. }
  4696. }
  4697. PackedVector2Array TileSetAtlasSource::get_tiles_to_be_removed_on_change(Ref<Texture2D> p_texture, Vector2i p_margins, Vector2i p_separation, Vector2i p_texture_region_size) {
  4698. ERR_FAIL_COND_V(p_margins.x < 0 || p_margins.y < 0, PackedVector2Array());
  4699. ERR_FAIL_COND_V(p_separation.x < 0 || p_separation.y < 0, PackedVector2Array());
  4700. ERR_FAIL_COND_V(p_texture_region_size.x <= 0 || p_texture_region_size.y <= 0, PackedVector2Array());
  4701. // Compute the new atlas grid size.
  4702. Size2 new_grid_size;
  4703. if (p_texture.is_valid()) {
  4704. Size2i valid_area = p_texture->get_size() - p_margins;
  4705. // Compute the number of valid tiles in the tiles atlas
  4706. if (valid_area.x >= p_texture_region_size.x && valid_area.y >= p_texture_region_size.y) {
  4707. valid_area -= p_texture_region_size;
  4708. new_grid_size = Size2i(1, 1) + valid_area / (p_texture_region_size + p_separation);
  4709. }
  4710. }
  4711. Vector<Vector2> output;
  4712. for (KeyValue<Vector2i, TileAlternativesData> &E : tiles) {
  4713. for (unsigned int frame = 0; frame < E.value.animation_frames_durations.size(); frame++) {
  4714. 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));
  4715. frame_coords += E.value.size_in_atlas;
  4716. if (frame_coords.x > new_grid_size.x || frame_coords.y > new_grid_size.y) {
  4717. output.push_back(E.key);
  4718. break;
  4719. }
  4720. }
  4721. }
  4722. return output;
  4723. }
  4724. Rect2i TileSetAtlasSource::get_tile_texture_region(Vector2i p_atlas_coords, int p_frame) const {
  4725. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Rect2i(), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4726. ERR_FAIL_INDEX_V(p_frame, (int)tiles[p_atlas_coords].animation_frames_durations.size(), Rect2i());
  4727. const TileAlternativesData &tad = tiles[p_atlas_coords];
  4728. Vector2i size_in_atlas = tad.size_in_atlas;
  4729. Vector2 region_size = texture_region_size * size_in_atlas + separation * (size_in_atlas - Vector2i(1, 1));
  4730. 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));
  4731. Vector2 origin = margins + (frame_coords * (texture_region_size + separation));
  4732. return Rect2(origin, region_size);
  4733. }
  4734. bool TileSetAtlasSource::is_position_in_tile_texture_region(const Vector2i p_atlas_coords, int p_alternative_tile, Vector2 p_position) const {
  4735. Size2 size = get_tile_texture_region(p_atlas_coords).size;
  4736. TileData *tile_data = get_tile_data(p_atlas_coords, p_alternative_tile);
  4737. if (tile_data->get_transpose()) {
  4738. size = Size2(size.y, size.x);
  4739. }
  4740. Rect2 rect = Rect2(-size / 2 - tile_data->get_texture_origin(), size);
  4741. return rect.has_point(p_position);
  4742. }
  4743. bool TileSetAtlasSource::is_rect_in_tile_texture_region(const Vector2i p_atlas_coords, int p_alternative_tile, Rect2 p_rect) const {
  4744. Size2 size = get_tile_texture_region(p_atlas_coords).size;
  4745. TileData *tile_data = get_tile_data(p_atlas_coords, p_alternative_tile);
  4746. if (tile_data->get_transpose()) {
  4747. size = Size2(size.y, size.x);
  4748. }
  4749. Rect2 rect = Rect2(-size / 2 - tile_data->get_texture_origin(), size);
  4750. return p_rect.intersection(rect) == p_rect;
  4751. }
  4752. int TileSetAtlasSource::alternative_no_transform(int p_alternative_id) {
  4753. return p_alternative_id & ~(TRANSFORM_FLIP_H | TRANSFORM_FLIP_V | TRANSFORM_TRANSPOSE);
  4754. }
  4755. // Getters for texture and tile region (padded or not)
  4756. Ref<Texture2D> TileSetAtlasSource::get_runtime_texture() const {
  4757. if (use_texture_padding) {
  4758. return padded_texture;
  4759. } else {
  4760. return texture;
  4761. }
  4762. }
  4763. Rect2i TileSetAtlasSource::get_runtime_tile_texture_region(Vector2i p_atlas_coords, int p_frame) const {
  4764. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Rect2i(), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4765. ERR_FAIL_INDEX_V(p_frame, (int)tiles[p_atlas_coords].animation_frames_durations.size(), Rect2i());
  4766. Rect2i src_rect = get_tile_texture_region(p_atlas_coords, p_frame);
  4767. if (use_texture_padding) {
  4768. const TileAlternativesData &tad = tiles[p_atlas_coords];
  4769. 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));
  4770. Vector2i base_pos = frame_coords * (texture_region_size + Vector2i(2, 2)) + Vector2i(1, 1);
  4771. return Rect2i(base_pos, src_rect.size);
  4772. } else {
  4773. return src_rect;
  4774. }
  4775. }
  4776. void TileSetAtlasSource::move_tile_in_atlas(Vector2i p_atlas_coords, Vector2i p_new_atlas_coords, Vector2i p_new_size) {
  4777. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4778. TileAlternativesData &tad = tiles[p_atlas_coords];
  4779. // Compute the actual new rect from arguments.
  4780. Vector2i new_atlas_coords = (p_new_atlas_coords != INVALID_ATLAS_COORDS) ? p_new_atlas_coords : p_atlas_coords;
  4781. Vector2i new_size = (p_new_size != Vector2i(-1, -1)) ? p_new_size : tad.size_in_atlas;
  4782. if (new_atlas_coords == p_atlas_coords && new_size == tad.size_in_atlas) {
  4783. return;
  4784. }
  4785. 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);
  4786. 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));
  4787. _clear_coords_mapping_cache(p_atlas_coords);
  4788. // Move the tile and update its size.
  4789. if (new_atlas_coords != p_atlas_coords) {
  4790. tiles[new_atlas_coords] = tiles[p_atlas_coords];
  4791. tiles.erase(p_atlas_coords);
  4792. tiles_ids.erase(p_atlas_coords);
  4793. tiles_ids.push_back(new_atlas_coords);
  4794. tiles_ids.sort();
  4795. }
  4796. tiles[new_atlas_coords].size_in_atlas = new_size;
  4797. _create_coords_mapping_cache(new_atlas_coords);
  4798. _queue_update_padded_texture();
  4799. _try_emit_changed();
  4800. }
  4801. int TileSetAtlasSource::create_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_id_override) {
  4802. 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)));
  4803. 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));
  4804. int new_alternative_id = p_alternative_id_override >= 0 ? p_alternative_id_override : tiles[p_atlas_coords].next_alternative_id;
  4805. tiles[p_atlas_coords].alternatives[new_alternative_id] = memnew(TileData);
  4806. tiles[p_atlas_coords].alternatives[new_alternative_id]->set_tile_set(tile_set);
  4807. tiles[p_atlas_coords].alternatives[new_alternative_id]->set_allow_transform(true);
  4808. tiles[p_atlas_coords].alternatives[new_alternative_id]->connect(CoreStringName(changed), callable_mp((Resource *)this, &TileSetAtlasSource::emit_changed));
  4809. tiles[p_atlas_coords].alternatives[new_alternative_id]->notify_property_list_changed();
  4810. tiles[p_atlas_coords].alternatives_ids.push_back(new_alternative_id);
  4811. tiles[p_atlas_coords].alternatives_ids.sort();
  4812. _compute_next_alternative_id(p_atlas_coords);
  4813. _try_emit_changed();
  4814. return new_alternative_id;
  4815. }
  4816. void TileSetAtlasSource::remove_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) {
  4817. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4818. 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)));
  4819. p_alternative_tile = alternative_no_transform(p_alternative_tile);
  4820. ERR_FAIL_COND_MSG(p_alternative_tile == 0, "Cannot remove the alternative with id 0, the base tile alternative cannot be removed.");
  4821. memdelete(tiles[p_atlas_coords].alternatives[p_alternative_tile]);
  4822. tiles[p_atlas_coords].alternatives.erase(p_alternative_tile);
  4823. tiles[p_atlas_coords].alternatives_ids.erase(p_alternative_tile);
  4824. tiles[p_atlas_coords].alternatives_ids.sort();
  4825. _try_emit_changed();
  4826. }
  4827. void TileSetAtlasSource::set_alternative_tile_id(const Vector2i p_atlas_coords, int p_alternative_tile, int p_new_id) {
  4828. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4829. 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)));
  4830. p_alternative_tile = alternative_no_transform(p_alternative_tile);
  4831. ERR_FAIL_COND_MSG(p_alternative_tile == 0, "Cannot change the alternative with id 0, the base tile alternative cannot be modified.");
  4832. 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)));
  4833. tiles[p_atlas_coords].alternatives[p_new_id] = tiles[p_atlas_coords].alternatives[p_alternative_tile];
  4834. tiles[p_atlas_coords].alternatives_ids.push_back(p_new_id);
  4835. tiles[p_atlas_coords].alternatives.erase(p_alternative_tile);
  4836. tiles[p_atlas_coords].alternatives_ids.erase(p_alternative_tile);
  4837. tiles[p_atlas_coords].alternatives_ids.sort();
  4838. _try_emit_changed();
  4839. }
  4840. bool TileSetAtlasSource::has_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  4841. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), false, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  4842. return tiles[p_atlas_coords].alternatives.has(alternative_no_transform(p_alternative_tile));
  4843. }
  4844. int TileSetAtlasSource::get_next_alternative_tile_id(const Vector2i p_atlas_coords) const {
  4845. 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)));
  4846. return tiles[p_atlas_coords].next_alternative_id;
  4847. }
  4848. int TileSetAtlasSource::get_alternative_tiles_count(const Vector2i p_atlas_coords) const {
  4849. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), -1, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  4850. return tiles[p_atlas_coords].alternatives_ids.size();
  4851. }
  4852. int TileSetAtlasSource::get_alternative_tile_id(const Vector2i p_atlas_coords, int p_index) const {
  4853. 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)));
  4854. p_index = alternative_no_transform(p_index);
  4855. ERR_FAIL_INDEX_V(p_index, tiles[p_atlas_coords].alternatives_ids.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  4856. return tiles[p_atlas_coords].alternatives_ids[p_index];
  4857. }
  4858. TileData *TileSetAtlasSource::get_tile_data(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  4859. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  4860. p_alternative_tile = alternative_no_transform(p_alternative_tile);
  4861. 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)));
  4862. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  4863. }
  4864. void TileSetAtlasSource::_notification(int p_notification) {
  4865. if (p_notification == NOTIFICATION_POSTINITIALIZE) {
  4866. initializing = false;
  4867. }
  4868. }
  4869. void TileSetAtlasSource::_bind_methods() {
  4870. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TileSetAtlasSource::set_texture);
  4871. ClassDB::bind_method(D_METHOD("get_texture"), &TileSetAtlasSource::get_texture);
  4872. ClassDB::bind_method(D_METHOD("set_margins", "margins"), &TileSetAtlasSource::set_margins);
  4873. ClassDB::bind_method(D_METHOD("get_margins"), &TileSetAtlasSource::get_margins);
  4874. ClassDB::bind_method(D_METHOD("set_separation", "separation"), &TileSetAtlasSource::set_separation);
  4875. ClassDB::bind_method(D_METHOD("get_separation"), &TileSetAtlasSource::get_separation);
  4876. ClassDB::bind_method(D_METHOD("set_texture_region_size", "texture_region_size"), &TileSetAtlasSource::set_texture_region_size);
  4877. ClassDB::bind_method(D_METHOD("get_texture_region_size"), &TileSetAtlasSource::get_texture_region_size);
  4878. ClassDB::bind_method(D_METHOD("set_use_texture_padding", "use_texture_padding"), &TileSetAtlasSource::set_use_texture_padding);
  4879. ClassDB::bind_method(D_METHOD("get_use_texture_padding"), &TileSetAtlasSource::get_use_texture_padding);
  4880. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_NO_EDITOR), "set_texture", "get_texture");
  4881. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "margins", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NO_EDITOR), "set_margins", "get_margins");
  4882. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "separation", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NO_EDITOR), "set_separation", "get_separation");
  4883. 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");
  4884. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_texture_padding", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_use_texture_padding", "get_use_texture_padding");
  4885. // Base tiles
  4886. ClassDB::bind_method(D_METHOD("create_tile", "atlas_coords", "size"), &TileSetAtlasSource::create_tile, DEFVAL(Vector2i(1, 1)));
  4887. 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
  4888. 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)));
  4889. ClassDB::bind_method(D_METHOD("get_tile_size_in_atlas", "atlas_coords"), &TileSetAtlasSource::get_tile_size_in_atlas);
  4890. 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));
  4891. 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);
  4892. ClassDB::bind_method(D_METHOD("get_tile_at_coords", "atlas_coords"), &TileSetAtlasSource::get_tile_at_coords);
  4893. ClassDB::bind_method(D_METHOD("has_tiles_outside_texture"), &TileSetAtlasSource::has_tiles_outside_texture);
  4894. ClassDB::bind_method(D_METHOD("clear_tiles_outside_texture"), &TileSetAtlasSource::clear_tiles_outside_texture);
  4895. ClassDB::bind_method(D_METHOD("set_tile_animation_columns", "atlas_coords", "frame_columns"), &TileSetAtlasSource::set_tile_animation_columns);
  4896. ClassDB::bind_method(D_METHOD("get_tile_animation_columns", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_columns);
  4897. ClassDB::bind_method(D_METHOD("set_tile_animation_separation", "atlas_coords", "separation"), &TileSetAtlasSource::set_tile_animation_separation);
  4898. ClassDB::bind_method(D_METHOD("get_tile_animation_separation", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_separation);
  4899. ClassDB::bind_method(D_METHOD("set_tile_animation_speed", "atlas_coords", "speed"), &TileSetAtlasSource::set_tile_animation_speed);
  4900. ClassDB::bind_method(D_METHOD("get_tile_animation_speed", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_speed);
  4901. ClassDB::bind_method(D_METHOD("set_tile_animation_mode", "atlas_coords", "mode"), &TileSetAtlasSource::set_tile_animation_mode);
  4902. ClassDB::bind_method(D_METHOD("get_tile_animation_mode", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_mode);
  4903. ClassDB::bind_method(D_METHOD("set_tile_animation_frames_count", "atlas_coords", "frames_count"), &TileSetAtlasSource::set_tile_animation_frames_count);
  4904. ClassDB::bind_method(D_METHOD("get_tile_animation_frames_count", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_frames_count);
  4905. ClassDB::bind_method(D_METHOD("set_tile_animation_frame_duration", "atlas_coords", "frame_index", "duration"), &TileSetAtlasSource::set_tile_animation_frame_duration);
  4906. ClassDB::bind_method(D_METHOD("get_tile_animation_frame_duration", "atlas_coords", "frame_index"), &TileSetAtlasSource::get_tile_animation_frame_duration);
  4907. ClassDB::bind_method(D_METHOD("get_tile_animation_total_duration", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_total_duration);
  4908. // Alternative tiles
  4909. ClassDB::bind_method(D_METHOD("create_alternative_tile", "atlas_coords", "alternative_id_override"), &TileSetAtlasSource::create_alternative_tile, DEFVAL(INVALID_TILE_ALTERNATIVE));
  4910. ClassDB::bind_method(D_METHOD("remove_alternative_tile", "atlas_coords", "alternative_tile"), &TileSetAtlasSource::remove_alternative_tile);
  4911. ClassDB::bind_method(D_METHOD("set_alternative_tile_id", "atlas_coords", "alternative_tile", "new_id"), &TileSetAtlasSource::set_alternative_tile_id);
  4912. ClassDB::bind_method(D_METHOD("get_next_alternative_tile_id", "atlas_coords"), &TileSetAtlasSource::get_next_alternative_tile_id);
  4913. ClassDB::bind_method(D_METHOD("get_tile_data", "atlas_coords", "alternative_tile"), &TileSetAtlasSource::get_tile_data);
  4914. // Helpers.
  4915. ClassDB::bind_method(D_METHOD("get_atlas_grid_size"), &TileSetAtlasSource::get_atlas_grid_size);
  4916. ClassDB::bind_method(D_METHOD("get_tile_texture_region", "atlas_coords", "frame"), &TileSetAtlasSource::get_tile_texture_region, DEFVAL(0));
  4917. // Getters for texture and tile region (padded or not)
  4918. ClassDB::bind_method(D_METHOD("get_runtime_texture"), &TileSetAtlasSource::get_runtime_texture);
  4919. ClassDB::bind_method(D_METHOD("get_runtime_tile_texture_region", "atlas_coords", "frame"), &TileSetAtlasSource::get_runtime_tile_texture_region);
  4920. BIND_ENUM_CONSTANT(TILE_ANIMATION_MODE_DEFAULT)
  4921. BIND_ENUM_CONSTANT(TILE_ANIMATION_MODE_RANDOM_START_TIMES)
  4922. BIND_ENUM_CONSTANT(TILE_ANIMATION_MODE_MAX)
  4923. BIND_CONSTANT(TRANSFORM_FLIP_H)
  4924. BIND_CONSTANT(TRANSFORM_FLIP_V)
  4925. BIND_CONSTANT(TRANSFORM_TRANSPOSE)
  4926. }
  4927. TileSetAtlasSource::~TileSetAtlasSource() {
  4928. // Free everything needed.
  4929. for (KeyValue<Vector2i, TileAlternativesData> &E_alternatives : tiles) {
  4930. for (KeyValue<int, TileData *> &E_tile_data : E_alternatives.value.alternatives) {
  4931. memdelete(E_tile_data.value);
  4932. }
  4933. }
  4934. }
  4935. TileData *TileSetAtlasSource::_get_atlas_tile_data(Vector2i p_atlas_coords, int p_alternative_tile) {
  4936. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4937. p_alternative_tile = alternative_no_transform(p_alternative_tile);
  4938. 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)));
  4939. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  4940. }
  4941. const TileData *TileSetAtlasSource::_get_atlas_tile_data(Vector2i p_atlas_coords, int p_alternative_tile) const {
  4942. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4943. 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)));
  4944. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  4945. }
  4946. void TileSetAtlasSource::_compute_next_alternative_id(const Vector2i p_atlas_coords) {
  4947. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  4948. while (tiles[p_atlas_coords].alternatives.has(tiles[p_atlas_coords].next_alternative_id)) {
  4949. tiles[p_atlas_coords].next_alternative_id = (tiles[p_atlas_coords].next_alternative_id % 1073741823) + 1; // 2 ** 30
  4950. };
  4951. }
  4952. void TileSetAtlasSource::_clear_coords_mapping_cache(Vector2i p_atlas_coords) {
  4953. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4954. TileAlternativesData &tad = tiles[p_atlas_coords];
  4955. for (int frame = 0; frame < (int)tad.animation_frames_durations.size(); frame++) {
  4956. 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));
  4957. for (int x = 0; x < tad.size_in_atlas.x; x++) {
  4958. for (int y = 0; y < tad.size_in_atlas.y; y++) {
  4959. Vector2i coords = frame_coords + Vector2i(x, y);
  4960. if (!_coords_mapping_cache.has(coords)) {
  4961. WARN_PRINT(vformat("TileSetAtlasSource has no cached tile at position %s, the position cache might be corrupted.", coords));
  4962. } else {
  4963. if (_coords_mapping_cache[coords] != p_atlas_coords) {
  4964. WARN_PRINT(vformat("The position cache at position %s is pointing to a wrong tile, the position cache might be corrupted.", coords));
  4965. }
  4966. _coords_mapping_cache.erase(coords);
  4967. }
  4968. }
  4969. }
  4970. }
  4971. }
  4972. void TileSetAtlasSource::_create_coords_mapping_cache(Vector2i p_atlas_coords) {
  4973. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  4974. TileAlternativesData &tad = tiles[p_atlas_coords];
  4975. for (int frame = 0; frame < (int)tad.animation_frames_durations.size(); frame++) {
  4976. 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));
  4977. for (int x = 0; x < tad.size_in_atlas.x; x++) {
  4978. for (int y = 0; y < tad.size_in_atlas.y; y++) {
  4979. Vector2i coords = frame_coords + Vector2i(x, y);
  4980. if (_coords_mapping_cache.has(coords)) {
  4981. WARN_PRINT(vformat("The cache already has a tile for position %s, the position cache might be corrupted.", coords));
  4982. }
  4983. _coords_mapping_cache[coords] = p_atlas_coords;
  4984. }
  4985. }
  4986. }
  4987. }
  4988. void TileSetAtlasSource::_queue_update_padded_texture() {
  4989. if (padded_texture_needs_update) {
  4990. return;
  4991. }
  4992. padded_texture_needs_update = true;
  4993. callable_mp(this, &TileSetAtlasSource::_update_padded_texture).call_deferred();
  4994. }
  4995. Ref<ImageTexture> TileSetAtlasSource::_create_padded_image_texture(const Ref<Texture2D> &p_source) {
  4996. ERR_FAIL_COND_V(p_source.is_null(), Ref<ImageTexture>());
  4997. Ref<Image> src_image = p_source->get_image();
  4998. if (src_image.is_null()) {
  4999. Ref<ImageTexture> ret;
  5000. ret.instantiate();
  5001. return ret;
  5002. }
  5003. if (src_image->is_compressed()) {
  5004. src_image = src_image->duplicate();
  5005. Error err = src_image->decompress();
  5006. ERR_FAIL_COND_V_MSG(err != OK, Ref<ImageTexture>(), "Unable to decompress image.");
  5007. }
  5008. Size2 size = get_atlas_grid_size() * (texture_region_size + Vector2i(2, 2));
  5009. Ref<Image> image = Image::create_empty(size.x, size.y, false, src_image->get_format());
  5010. for (KeyValue<Vector2i, TileAlternativesData> kv : tiles) {
  5011. for (int frame = 0; frame < (int)kv.value.animation_frames_durations.size(); frame++) {
  5012. // Compute the source rects.
  5013. Rect2i src_rect = get_tile_texture_region(kv.key, frame);
  5014. Rect2i top_src_rect = Rect2i(src_rect.position, Vector2i(src_rect.size.x, 1));
  5015. Rect2i bottom_src_rect = Rect2i(src_rect.position + Vector2i(0, src_rect.size.y - 1), Vector2i(src_rect.size.x, 1));
  5016. Rect2i left_src_rect = Rect2i(src_rect.position, Vector2i(1, src_rect.size.y));
  5017. Rect2i right_src_rect = Rect2i(src_rect.position + Vector2i(src_rect.size.x - 1, 0), Vector2i(1, src_rect.size.y));
  5018. // Copy the tile and the paddings.
  5019. 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));
  5020. Vector2i base_pos = frame_coords * (texture_region_size + Vector2i(2, 2)) + Vector2i(1, 1);
  5021. image->blit_rect(*src_image, src_rect, base_pos);
  5022. // Sides
  5023. image->blit_rect(*src_image, top_src_rect, base_pos + Vector2i(0, -1));
  5024. image->blit_rect(*src_image, bottom_src_rect, base_pos + Vector2i(0, src_rect.size.y));
  5025. image->blit_rect(*src_image, left_src_rect, base_pos + Vector2i(-1, 0));
  5026. image->blit_rect(*src_image, right_src_rect, base_pos + Vector2i(src_rect.size.x, 0));
  5027. // Corners
  5028. image->blit_rect(*src_image, Rect2i(src_rect.position, Vector2i(1, 1)), base_pos + Vector2i(-1, -1));
  5029. image->blit_rect(*src_image, Rect2i(src_rect.position + Vector2i(src_rect.size.x - 1, 0), Vector2i(1, 1)), base_pos + Vector2i(src_rect.size.x, -1));
  5030. image->blit_rect(*src_image, Rect2i(src_rect.position + Vector2i(0, src_rect.size.y - 1), Vector2i(1, 1)), base_pos + Vector2i(-1, src_rect.size.y));
  5031. image->blit_rect(*src_image, Rect2i(src_rect.position + Vector2i(src_rect.size.x - 1, src_rect.size.y - 1), Vector2i(1, 1)), base_pos + Vector2i(src_rect.size.x, src_rect.size.y));
  5032. }
  5033. }
  5034. return ImageTexture::create_from_image(image);
  5035. }
  5036. void TileSetAtlasSource::_update_padded_texture() {
  5037. if (!padded_texture_needs_update) {
  5038. return;
  5039. }
  5040. padded_texture_needs_update = false;
  5041. if (padded_texture.is_valid()) {
  5042. padded_texture->disconnect_changed(callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  5043. }
  5044. padded_texture = Ref<CanvasTexture>();
  5045. if (texture.is_null()) {
  5046. return;
  5047. }
  5048. if (!use_texture_padding) {
  5049. return;
  5050. }
  5051. padded_texture.instantiate();
  5052. Ref<CanvasTexture> src_canvas_texture = texture;
  5053. if (src_canvas_texture.is_valid()) {
  5054. // Use all textures.
  5055. // Diffuse
  5056. Ref<Texture2D> src = src_canvas_texture->get_diffuse_texture();
  5057. Ref<ImageTexture> image_texture;
  5058. if (src.is_valid()) {
  5059. padded_texture->set_diffuse_texture(_create_padded_image_texture(src));
  5060. }
  5061. // Normal
  5062. src = src_canvas_texture->get_normal_texture();
  5063. if (src.is_valid()) {
  5064. padded_texture->set_normal_texture(_create_padded_image_texture(src));
  5065. }
  5066. // Specular
  5067. src = src_canvas_texture->get_specular_texture();
  5068. if (src.is_valid()) {
  5069. padded_texture->set_specular_texture(_create_padded_image_texture(src));
  5070. }
  5071. // Other properties.
  5072. padded_texture->set_specular_color(src_canvas_texture->get_specular_color());
  5073. padded_texture->set_specular_shininess(src_canvas_texture->get_specular_shininess());
  5074. padded_texture->set_texture_filter(src_canvas_texture->get_texture_filter());
  5075. padded_texture->set_texture_repeat(src_canvas_texture->get_texture_repeat());
  5076. } else {
  5077. // Use only diffuse.
  5078. Ref<ImageTexture> image_texture = _create_padded_image_texture(texture);
  5079. padded_texture->set_diffuse_texture(image_texture);
  5080. }
  5081. padded_texture->connect_changed(callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  5082. emit_changed();
  5083. }
  5084. void TileSetAtlasSource::_try_emit_changed() {
  5085. if (!initializing) {
  5086. emit_changed();
  5087. }
  5088. }
  5089. /////////////////////////////// TileSetScenesCollectionSource //////////////////////////////////////
  5090. void TileSetScenesCollectionSource::_compute_next_alternative_id() {
  5091. while (scenes.has(next_scene_id)) {
  5092. next_scene_id = (next_scene_id % 1073741823) + 1; // 2 ** 30
  5093. };
  5094. }
  5095. void TileSetScenesCollectionSource::_try_emit_changed() {
  5096. if (!initializing) {
  5097. emit_changed();
  5098. }
  5099. }
  5100. int TileSetScenesCollectionSource::get_tiles_count() const {
  5101. return 1;
  5102. }
  5103. Vector2i TileSetScenesCollectionSource::get_tile_id(int p_tile_index) const {
  5104. ERR_FAIL_COND_V(p_tile_index != 0, TileSetSource::INVALID_ATLAS_COORDS);
  5105. return Vector2i();
  5106. }
  5107. bool TileSetScenesCollectionSource::has_tile(Vector2i p_atlas_coords) const {
  5108. return p_atlas_coords == Vector2i();
  5109. }
  5110. int TileSetScenesCollectionSource::get_alternative_tiles_count(const Vector2i p_atlas_coords) const {
  5111. return scenes_ids.size();
  5112. }
  5113. int TileSetScenesCollectionSource::get_alternative_tile_id(const Vector2i p_atlas_coords, int p_index) const {
  5114. ERR_FAIL_COND_V(p_atlas_coords != Vector2i(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  5115. ERR_FAIL_INDEX_V(p_index, scenes_ids.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  5116. return scenes_ids[p_index];
  5117. }
  5118. bool TileSetScenesCollectionSource::has_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  5119. ERR_FAIL_COND_V(p_atlas_coords != Vector2i(), false);
  5120. return scenes.has(p_alternative_tile);
  5121. }
  5122. int TileSetScenesCollectionSource::create_scene_tile(Ref<PackedScene> p_packed_scene, int p_id_override) {
  5123. 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));
  5124. int new_scene_id = p_id_override >= 0 ? p_id_override : next_scene_id;
  5125. scenes[new_scene_id] = SceneData();
  5126. scenes_ids.push_back(new_scene_id);
  5127. scenes_ids.sort();
  5128. set_scene_tile_scene(new_scene_id, p_packed_scene);
  5129. _compute_next_alternative_id();
  5130. _try_emit_changed();
  5131. return new_scene_id;
  5132. }
  5133. void TileSetScenesCollectionSource::set_scene_tile_id(int p_id, int p_new_id) {
  5134. ERR_FAIL_COND(p_new_id < 0);
  5135. ERR_FAIL_COND(!has_scene_tile_id(p_id));
  5136. ERR_FAIL_COND(has_scene_tile_id(p_new_id));
  5137. scenes[p_new_id] = SceneData();
  5138. scenes[p_new_id] = scenes[p_id];
  5139. scenes_ids.push_back(p_new_id);
  5140. scenes_ids.sort();
  5141. _compute_next_alternative_id();
  5142. scenes.erase(p_id);
  5143. scenes_ids.erase(p_id);
  5144. _try_emit_changed();
  5145. }
  5146. void TileSetScenesCollectionSource::set_scene_tile_scene(int p_id, Ref<PackedScene> p_packed_scene) {
  5147. ERR_FAIL_COND(!scenes.has(p_id));
  5148. if (p_packed_scene.is_valid()) {
  5149. // Check if it extends CanvasItem.
  5150. Ref<SceneState> scene_state = p_packed_scene->get_state();
  5151. String type;
  5152. while (scene_state.is_valid() && type.is_empty()) {
  5153. // Make sure we have a root node. Supposed to be at 0 index because find_node_by_path() does not seem to work.
  5154. ERR_FAIL_COND(scene_state->get_node_count() < 1);
  5155. type = scene_state->get_node_type(0);
  5156. scene_state = scene_state->get_base_scene_state();
  5157. }
  5158. ERR_FAIL_COND_EDMSG(type.is_empty(), vformat("Invalid PackedScene for TileSetScenesCollectionSource: %s. Could not get the type of the root node.", p_packed_scene->get_path()));
  5159. bool extends_correct_class = ClassDB::is_parent_class(type, "CanvasItem");
  5160. ERR_FAIL_COND_EDMSG(!extends_correct_class, vformat("Invalid PackedScene for TileSetScenesCollectionSource: %s. Root node should extend CanvasItem. Found %s instead.", p_packed_scene->get_path(), type));
  5161. scenes[p_id].scene = p_packed_scene;
  5162. } else {
  5163. scenes[p_id].scene = Ref<PackedScene>();
  5164. }
  5165. _try_emit_changed();
  5166. }
  5167. Ref<PackedScene> TileSetScenesCollectionSource::get_scene_tile_scene(int p_id) const {
  5168. ERR_FAIL_COND_V(!scenes.has(p_id), Ref<PackedScene>());
  5169. return scenes[p_id].scene;
  5170. }
  5171. void TileSetScenesCollectionSource::set_scene_tile_display_placeholder(int p_id, bool p_display_placeholder) {
  5172. ERR_FAIL_COND(!scenes.has(p_id));
  5173. scenes[p_id].display_placeholder = p_display_placeholder;
  5174. _try_emit_changed();
  5175. }
  5176. bool TileSetScenesCollectionSource::get_scene_tile_display_placeholder(int p_id) const {
  5177. ERR_FAIL_COND_V(!scenes.has(p_id), false);
  5178. return scenes[p_id].display_placeholder;
  5179. }
  5180. void TileSetScenesCollectionSource::remove_scene_tile(int p_id) {
  5181. ERR_FAIL_COND(!scenes.has(p_id));
  5182. scenes.erase(p_id);
  5183. scenes_ids.erase(p_id);
  5184. _try_emit_changed();
  5185. }
  5186. int TileSetScenesCollectionSource::get_next_scene_tile_id() const {
  5187. return next_scene_id;
  5188. }
  5189. bool TileSetScenesCollectionSource::_set(const StringName &p_name, const Variant &p_value) {
  5190. Vector<String> components = String(p_name).split("/", true, 2);
  5191. if (components.size() >= 2 && components[0] == "scenes" && components[1].is_valid_int()) {
  5192. int scene_id = components[1].to_int();
  5193. if (components.size() >= 3 && components[2] == "scene") {
  5194. if (has_scene_tile_id(scene_id)) {
  5195. set_scene_tile_scene(scene_id, p_value);
  5196. } else {
  5197. create_scene_tile(p_value, scene_id);
  5198. }
  5199. return true;
  5200. } else if (components.size() >= 3 && components[2] == "display_placeholder") {
  5201. if (!has_scene_tile_id(scene_id)) {
  5202. create_scene_tile(p_value, scene_id);
  5203. }
  5204. return true;
  5205. }
  5206. }
  5207. return false;
  5208. }
  5209. bool TileSetScenesCollectionSource::_get(const StringName &p_name, Variant &r_ret) const {
  5210. Vector<String> components = String(p_name).split("/", true, 2);
  5211. if (components.size() >= 2 && components[0] == "scenes" && components[1].is_valid_int() && scenes.has(components[1].to_int())) {
  5212. if (components.size() >= 3 && components[2] == "scene") {
  5213. r_ret = scenes[components[1].to_int()].scene;
  5214. return true;
  5215. } else if (components.size() >= 3 && components[2] == "display_placeholder") {
  5216. r_ret = scenes[components[1].to_int()].scene;
  5217. return true;
  5218. }
  5219. }
  5220. return false;
  5221. }
  5222. void TileSetScenesCollectionSource::_get_property_list(List<PropertyInfo> *p_list) const {
  5223. for (int i = 0; i < scenes_ids.size(); i++) {
  5224. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("%s/%d/%s", PNAME("scenes"), scenes_ids[i], PNAME("scene")), PROPERTY_HINT_RESOURCE_TYPE, "TileSetScenesCollectionSource"));
  5225. PropertyInfo property_info = PropertyInfo(Variant::BOOL, vformat("%s/%d/%s", PNAME("scenes"), scenes_ids[i], PNAME("display_placeholder")));
  5226. if (scenes[scenes_ids[i]].display_placeholder == false) {
  5227. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  5228. }
  5229. p_list->push_back(property_info);
  5230. }
  5231. }
  5232. void TileSetScenesCollectionSource::_notification(int p_notification) {
  5233. if (p_notification == NOTIFICATION_POSTINITIALIZE) {
  5234. initializing = false;
  5235. }
  5236. }
  5237. void TileSetScenesCollectionSource::_bind_methods() {
  5238. ClassDB::bind_method(D_METHOD("get_scene_tiles_count"), &TileSetScenesCollectionSource::get_scene_tiles_count);
  5239. ClassDB::bind_method(D_METHOD("get_scene_tile_id", "index"), &TileSetScenesCollectionSource::get_scene_tile_id);
  5240. ClassDB::bind_method(D_METHOD("has_scene_tile_id", "id"), &TileSetScenesCollectionSource::has_scene_tile_id);
  5241. ClassDB::bind_method(D_METHOD("create_scene_tile", "packed_scene", "id_override"), &TileSetScenesCollectionSource::create_scene_tile, DEFVAL(INVALID_TILE_ALTERNATIVE));
  5242. ClassDB::bind_method(D_METHOD("set_scene_tile_id", "id", "new_id"), &TileSetScenesCollectionSource::set_scene_tile_id);
  5243. ClassDB::bind_method(D_METHOD("set_scene_tile_scene", "id", "packed_scene"), &TileSetScenesCollectionSource::set_scene_tile_scene);
  5244. ClassDB::bind_method(D_METHOD("get_scene_tile_scene", "id"), &TileSetScenesCollectionSource::get_scene_tile_scene);
  5245. ClassDB::bind_method(D_METHOD("set_scene_tile_display_placeholder", "id", "display_placeholder"), &TileSetScenesCollectionSource::set_scene_tile_display_placeholder);
  5246. ClassDB::bind_method(D_METHOD("get_scene_tile_display_placeholder", "id"), &TileSetScenesCollectionSource::get_scene_tile_display_placeholder);
  5247. ClassDB::bind_method(D_METHOD("remove_scene_tile", "id"), &TileSetScenesCollectionSource::remove_scene_tile);
  5248. ClassDB::bind_method(D_METHOD("get_next_scene_tile_id"), &TileSetScenesCollectionSource::get_next_scene_tile_id);
  5249. }
  5250. /////////////////////////////// TileData //////////////////////////////////////
  5251. void TileData::set_tile_set(const TileSet *p_tile_set) {
  5252. tile_set = p_tile_set;
  5253. notify_tile_data_properties_should_change();
  5254. }
  5255. void TileData::notify_tile_data_properties_should_change() {
  5256. if (!tile_set) {
  5257. return;
  5258. }
  5259. occluders.resize(tile_set->get_occlusion_layers_count());
  5260. #ifndef PHYSICS_2D_DISABLED
  5261. physics.resize(tile_set->get_physics_layers_count());
  5262. #endif // PHYSICS_2D_DISABLED
  5263. for (int bit_index = 0; bit_index < 16; bit_index++) {
  5264. if (terrain_set < 0 || terrain_peering_bits[bit_index] >= tile_set->get_terrains_count(terrain_set)) {
  5265. terrain_peering_bits[bit_index] = -1;
  5266. }
  5267. }
  5268. #ifndef NAVIGATION_2D_DISABLED
  5269. navigation.resize(tile_set->get_navigation_layers_count());
  5270. #endif // NAVIGATION_2D_DISABLED
  5271. // Convert custom data to the new type.
  5272. custom_data.resize(tile_set->get_custom_data_layers_count());
  5273. for (int i = 0; i < custom_data.size(); i++) {
  5274. if (custom_data[i].get_type() != tile_set->get_custom_data_layer_type(i)) {
  5275. Variant new_val;
  5276. Callable::CallError error;
  5277. if (Variant::can_convert(custom_data[i].get_type(), tile_set->get_custom_data_layer_type(i))) {
  5278. const Variant *args[] = { &custom_data[i] };
  5279. Variant::construct(tile_set->get_custom_data_layer_type(i), new_val, args, 1, error);
  5280. } else {
  5281. Variant::construct(tile_set->get_custom_data_layer_type(i), new_val, nullptr, 0, error);
  5282. }
  5283. custom_data.write[i] = new_val;
  5284. }
  5285. }
  5286. notify_property_list_changed();
  5287. emit_signal(CoreStringName(changed));
  5288. }
  5289. void TileData::add_occlusion_layer(int p_to_pos) {
  5290. if (p_to_pos < 0) {
  5291. p_to_pos = occluders.size();
  5292. }
  5293. ERR_FAIL_INDEX(p_to_pos, occluders.size() + 1);
  5294. occluders.insert(p_to_pos, OcclusionLayerTileData());
  5295. }
  5296. void TileData::move_occlusion_layer(int p_from_index, int p_to_pos) {
  5297. ERR_FAIL_INDEX(p_from_index, occluders.size());
  5298. ERR_FAIL_INDEX(p_to_pos, occluders.size() + 1);
  5299. occluders.insert(p_to_pos, occluders[p_from_index]);
  5300. occluders.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  5301. }
  5302. void TileData::remove_occlusion_layer(int p_index) {
  5303. ERR_FAIL_INDEX(p_index, occluders.size());
  5304. occluders.remove_at(p_index);
  5305. }
  5306. #ifndef PHYSICS_2D_DISABLED
  5307. void TileData::add_physics_layer(int p_to_pos) {
  5308. if (p_to_pos < 0) {
  5309. p_to_pos = physics.size();
  5310. }
  5311. ERR_FAIL_INDEX(p_to_pos, physics.size() + 1);
  5312. physics.insert(p_to_pos, PhysicsLayerTileData());
  5313. }
  5314. void TileData::move_physics_layer(int p_from_index, int p_to_pos) {
  5315. ERR_FAIL_INDEX(p_from_index, physics.size());
  5316. ERR_FAIL_INDEX(p_to_pos, physics.size() + 1);
  5317. physics.insert(p_to_pos, physics[p_from_index]);
  5318. physics.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  5319. }
  5320. void TileData::remove_physics_layer(int p_index) {
  5321. ERR_FAIL_INDEX(p_index, physics.size());
  5322. physics.remove_at(p_index);
  5323. }
  5324. #endif // PHYSICS_2D_DISABLED
  5325. void TileData::add_terrain_set(int p_to_pos) {
  5326. if (p_to_pos >= 0 && p_to_pos <= terrain_set) {
  5327. terrain_set += 1;
  5328. }
  5329. }
  5330. void TileData::move_terrain_set(int p_from_index, int p_to_pos) {
  5331. if (p_from_index == terrain_set) {
  5332. terrain_set = (p_from_index < p_to_pos) ? p_to_pos - 1 : p_to_pos;
  5333. } else {
  5334. if (p_from_index < terrain_set) {
  5335. terrain_set -= 1;
  5336. }
  5337. if (p_to_pos <= terrain_set) {
  5338. terrain_set += 1;
  5339. }
  5340. }
  5341. }
  5342. void TileData::remove_terrain_set(int p_index) {
  5343. if (p_index == terrain_set) {
  5344. terrain_set = -1;
  5345. for (int i = 0; i < 16; i++) {
  5346. terrain_peering_bits[i] = -1;
  5347. }
  5348. } else if (terrain_set > p_index) {
  5349. terrain_set -= 1;
  5350. }
  5351. }
  5352. void TileData::add_terrain(int p_terrain_set, int p_to_pos) {
  5353. if (terrain_set == p_terrain_set) {
  5354. for (int i = 0; i < 16; i++) {
  5355. if (p_to_pos >= 0 && p_to_pos <= terrain_peering_bits[i]) {
  5356. terrain_peering_bits[i] += 1;
  5357. }
  5358. }
  5359. }
  5360. }
  5361. void TileData::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  5362. if (terrain_set == p_terrain_set) {
  5363. for (int i = 0; i < 16; i++) {
  5364. if (p_from_index == terrain_peering_bits[i]) {
  5365. terrain_peering_bits[i] = (p_from_index < p_to_pos) ? p_to_pos - 1 : p_to_pos;
  5366. } else {
  5367. if (p_from_index < terrain_peering_bits[i]) {
  5368. terrain_peering_bits[i] -= 1;
  5369. }
  5370. if (p_to_pos <= terrain_peering_bits[i]) {
  5371. terrain_peering_bits[i] += 1;
  5372. }
  5373. }
  5374. }
  5375. }
  5376. }
  5377. void TileData::remove_terrain(int p_terrain_set, int p_index) {
  5378. if (terrain_set == p_terrain_set) {
  5379. if (terrain == p_index) {
  5380. terrain = -1;
  5381. } else if (terrain > p_index) {
  5382. terrain -= 1;
  5383. }
  5384. for (int i = 0; i < 16; i++) {
  5385. if (terrain_peering_bits[i] == p_index) {
  5386. terrain_peering_bits[i] = -1;
  5387. } else if (terrain_peering_bits[i] > p_index) {
  5388. terrain_peering_bits[i] -= 1;
  5389. }
  5390. }
  5391. }
  5392. }
  5393. #ifndef NAVIGATION_2D_DISABLED
  5394. void TileData::add_navigation_layer(int p_to_pos) {
  5395. if (p_to_pos < 0) {
  5396. p_to_pos = navigation.size();
  5397. }
  5398. ERR_FAIL_INDEX(p_to_pos, navigation.size() + 1);
  5399. navigation.insert(p_to_pos, NavigationLayerTileData());
  5400. }
  5401. void TileData::move_navigation_layer(int p_from_index, int p_to_pos) {
  5402. ERR_FAIL_INDEX(p_from_index, navigation.size());
  5403. ERR_FAIL_INDEX(p_to_pos, navigation.size() + 1);
  5404. navigation.insert(p_to_pos, navigation[p_from_index]);
  5405. navigation.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  5406. }
  5407. void TileData::remove_navigation_layer(int p_index) {
  5408. ERR_FAIL_INDEX(p_index, navigation.size());
  5409. navigation.remove_at(p_index);
  5410. }
  5411. #endif // NAVIGATION_2D_DISABLED
  5412. void TileData::add_custom_data_layer(int p_to_pos) {
  5413. if (p_to_pos < 0) {
  5414. p_to_pos = custom_data.size();
  5415. }
  5416. ERR_FAIL_INDEX(p_to_pos, custom_data.size() + 1);
  5417. custom_data.insert(p_to_pos, Variant());
  5418. }
  5419. void TileData::move_custom_data_layer(int p_from_index, int p_to_pos) {
  5420. ERR_FAIL_INDEX(p_from_index, custom_data.size());
  5421. ERR_FAIL_INDEX(p_to_pos, custom_data.size() + 1);
  5422. custom_data.insert(p_to_pos, custom_data[p_from_index]);
  5423. custom_data.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  5424. }
  5425. void TileData::remove_custom_data_layer(int p_index) {
  5426. ERR_FAIL_INDEX(p_index, custom_data.size());
  5427. custom_data.remove_at(p_index);
  5428. }
  5429. void TileData::set_allow_transform(bool p_allow_transform) {
  5430. allow_transform = p_allow_transform;
  5431. }
  5432. bool TileData::is_allowing_transform() const {
  5433. return allow_transform;
  5434. }
  5435. TileData *TileData::duplicate() {
  5436. TileData *output = memnew(TileData);
  5437. output->tile_set = tile_set;
  5438. output->allow_transform = allow_transform;
  5439. // Rendering
  5440. output->flip_h = flip_h;
  5441. output->flip_v = flip_v;
  5442. output->transpose = transpose;
  5443. output->texture_origin = texture_origin;
  5444. output->material = material;
  5445. output->modulate = modulate;
  5446. output->z_index = z_index;
  5447. output->y_sort_origin = y_sort_origin;
  5448. output->occluders = occluders;
  5449. #ifndef PHYSICS_2D_DISABLED
  5450. // Physics
  5451. output->physics = physics;
  5452. #endif // PHYSICS_2D_DISABLED
  5453. // Terrain
  5454. output->terrain_set = -1;
  5455. memcpy(output->terrain_peering_bits, terrain_peering_bits, 16 * sizeof(int));
  5456. #ifndef NAVIGATION_2D_DISABLED
  5457. // Navigation
  5458. output->navigation = navigation;
  5459. #endif // NAVIGATION_2D_DISABLED
  5460. // Misc
  5461. output->probability = probability;
  5462. // Custom data
  5463. output->custom_data = custom_data;
  5464. return output;
  5465. }
  5466. // Rendering
  5467. void TileData::set_flip_h(bool p_flip_h) {
  5468. ERR_FAIL_COND_MSG(!allow_transform && p_flip_h, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  5469. flip_h = p_flip_h;
  5470. emit_signal(CoreStringName(changed));
  5471. }
  5472. bool TileData::get_flip_h() const {
  5473. return flip_h;
  5474. }
  5475. void TileData::set_flip_v(bool p_flip_v) {
  5476. ERR_FAIL_COND_MSG(!allow_transform && p_flip_v, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  5477. flip_v = p_flip_v;
  5478. emit_signal(CoreStringName(changed));
  5479. }
  5480. bool TileData::get_flip_v() const {
  5481. return flip_v;
  5482. }
  5483. void TileData::set_transpose(bool p_transpose) {
  5484. ERR_FAIL_COND_MSG(!allow_transform && p_transpose, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  5485. transpose = p_transpose;
  5486. emit_signal(CoreStringName(changed));
  5487. }
  5488. bool TileData::get_transpose() const {
  5489. return transpose;
  5490. }
  5491. void TileData::set_texture_origin(Vector2i p_texture_origin) {
  5492. texture_origin = p_texture_origin;
  5493. emit_signal(CoreStringName(changed));
  5494. }
  5495. Vector2i TileData::get_texture_origin() const {
  5496. return texture_origin;
  5497. }
  5498. void TileData::set_material(Ref<Material> p_material) {
  5499. material = p_material;
  5500. emit_signal(CoreStringName(changed));
  5501. }
  5502. Ref<Material> TileData::get_material() const {
  5503. return material;
  5504. }
  5505. void TileData::set_modulate(Color p_modulate) {
  5506. modulate = p_modulate;
  5507. emit_signal(CoreStringName(changed));
  5508. }
  5509. Color TileData::get_modulate() const {
  5510. return modulate;
  5511. }
  5512. void TileData::set_z_index(int p_z_index) {
  5513. z_index = p_z_index;
  5514. emit_signal(CoreStringName(changed));
  5515. }
  5516. int TileData::get_z_index() const {
  5517. return z_index;
  5518. }
  5519. void TileData::set_y_sort_origin(int p_y_sort_origin) {
  5520. y_sort_origin = p_y_sort_origin;
  5521. emit_signal(CoreStringName(changed));
  5522. }
  5523. int TileData::get_y_sort_origin() const {
  5524. return y_sort_origin;
  5525. }
  5526. #ifndef DISABLE_DEPRECATED
  5527. void TileData::set_occluder(int p_layer_id, Ref<OccluderPolygon2D> p_occluder_polygon) {
  5528. ERR_FAIL_INDEX(p_layer_id, occluders.size());
  5529. if (get_occluder_polygons_count(p_layer_id) == 0) {
  5530. add_occluder_polygon(p_layer_id);
  5531. }
  5532. set_occluder_polygon(p_layer_id, 0, p_occluder_polygon);
  5533. emit_signal(CoreStringName(changed));
  5534. }
  5535. Ref<OccluderPolygon2D> TileData::get_occluder(int p_layer_id, bool p_flip_h, bool p_flip_v, bool p_transpose) const {
  5536. ERR_FAIL_INDEX_V(p_layer_id, occluders.size(), Ref<OccluderPolygon2D>());
  5537. if (get_occluder_polygons_count(p_layer_id) == 0) {
  5538. return Ref<OccluderPolygon2D>();
  5539. }
  5540. return get_occluder_polygon(p_layer_id, 0, p_flip_h, p_flip_v, p_transpose);
  5541. }
  5542. #endif // DISABLE_DEPRECATED
  5543. void TileData::set_occluder_polygons_count(int p_layer_id, int p_polygons_count) {
  5544. ERR_FAIL_INDEX(p_layer_id, occluders.size());
  5545. ERR_FAIL_COND(p_polygons_count < 0);
  5546. if (p_polygons_count == occluders.write[p_layer_id].polygons.size()) {
  5547. return;
  5548. }
  5549. occluders.write[p_layer_id].polygons.resize(p_polygons_count);
  5550. notify_property_list_changed();
  5551. emit_signal(CoreStringName(changed));
  5552. }
  5553. int TileData::get_occluder_polygons_count(int p_layer_id) const {
  5554. ERR_FAIL_INDEX_V(p_layer_id, occluders.size(), 0);
  5555. return occluders[p_layer_id].polygons.size();
  5556. }
  5557. void TileData::add_occluder_polygon(int p_layer_id) {
  5558. ERR_FAIL_INDEX(p_layer_id, occluders.size());
  5559. occluders.write[p_layer_id].polygons.push_back(OcclusionLayerTileData::PolygonOccluderTileData());
  5560. emit_signal(CoreStringName(changed));
  5561. }
  5562. void TileData::remove_occluder_polygon(int p_layer_id, int p_polygon_index) {
  5563. ERR_FAIL_INDEX(p_layer_id, occluders.size());
  5564. ERR_FAIL_INDEX(p_polygon_index, occluders[p_layer_id].polygons.size());
  5565. occluders.write[p_layer_id].polygons.remove_at(p_polygon_index);
  5566. emit_signal(CoreStringName(changed));
  5567. }
  5568. void TileData::set_occluder_polygon(int p_layer_id, int p_polygon_index, const Ref<OccluderPolygon2D> &p_occluder_polygon) {
  5569. ERR_FAIL_INDEX(p_layer_id, occluders.size());
  5570. ERR_FAIL_INDEX(p_polygon_index, occluders[p_layer_id].polygons.size());
  5571. OcclusionLayerTileData::PolygonOccluderTileData &polygon_occluder_tile_data = occluders.write[p_layer_id].polygons.write[p_polygon_index];
  5572. polygon_occluder_tile_data.occluder_polygon = p_occluder_polygon;
  5573. polygon_occluder_tile_data.transformed_polygon_occluders.clear();
  5574. emit_signal(CoreStringName(changed));
  5575. }
  5576. Ref<OccluderPolygon2D> TileData::get_occluder_polygon(int p_layer_id, int p_polygon_index, bool p_flip_h, bool p_flip_v, bool p_transpose) const {
  5577. ERR_FAIL_INDEX_V(p_layer_id, occluders.size(), Ref<OccluderPolygon2D>());
  5578. ERR_FAIL_INDEX_V(p_polygon_index, occluders[p_layer_id].polygons.size(), Ref<OccluderPolygon2D>());
  5579. const OcclusionLayerTileData &layer_tile_data = occluders[p_layer_id];
  5580. const Ref<OccluderPolygon2D> &occluder_polygon = layer_tile_data.polygons[p_polygon_index].occluder_polygon;
  5581. int key = int(p_flip_h) | int(p_flip_v) << 1 | int(p_transpose) << 2;
  5582. if (key == 0) {
  5583. return occluder_polygon;
  5584. }
  5585. if (occluder_polygon.is_null()) {
  5586. return Ref<OccluderPolygon2D>();
  5587. }
  5588. HashMap<int, Ref<OccluderPolygon2D>>::Iterator I = layer_tile_data.polygons[p_polygon_index].transformed_polygon_occluders.find(key);
  5589. if (!I) {
  5590. Ref<OccluderPolygon2D> transformed_polygon;
  5591. transformed_polygon.instantiate();
  5592. transformed_polygon->set_polygon(get_transformed_vertices(occluder_polygon->get_polygon(), p_flip_h, p_flip_v, p_transpose));
  5593. layer_tile_data.polygons[p_polygon_index].transformed_polygon_occluders[key] = transformed_polygon;
  5594. return transformed_polygon;
  5595. } else {
  5596. return I->value;
  5597. }
  5598. }
  5599. #ifndef PHYSICS_2D_DISABLED
  5600. // Physics
  5601. void TileData::set_constant_linear_velocity(int p_layer_id, const Vector2 &p_velocity) {
  5602. ERR_FAIL_INDEX(p_layer_id, physics.size());
  5603. physics.write[p_layer_id].linear_velocity = p_velocity;
  5604. emit_signal(CoreStringName(changed));
  5605. }
  5606. Vector2 TileData::get_constant_linear_velocity(int p_layer_id) const {
  5607. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Vector2());
  5608. return physics[p_layer_id].linear_velocity;
  5609. }
  5610. void TileData::set_constant_angular_velocity(int p_layer_id, real_t p_velocity) {
  5611. ERR_FAIL_INDEX(p_layer_id, physics.size());
  5612. physics.write[p_layer_id].angular_velocity = p_velocity;
  5613. emit_signal(CoreStringName(changed));
  5614. }
  5615. real_t TileData::get_constant_angular_velocity(int p_layer_id) const {
  5616. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0.0);
  5617. return physics[p_layer_id].angular_velocity;
  5618. }
  5619. void TileData::set_collision_polygons_count(int p_layer_id, int p_polygons_count) {
  5620. ERR_FAIL_INDEX(p_layer_id, physics.size());
  5621. ERR_FAIL_COND(p_polygons_count < 0);
  5622. if (p_polygons_count == physics.write[p_layer_id].polygons.size()) {
  5623. return;
  5624. }
  5625. physics.write[p_layer_id].polygons.resize(p_polygons_count);
  5626. notify_property_list_changed();
  5627. emit_signal(CoreStringName(changed));
  5628. }
  5629. int TileData::get_collision_polygons_count(int p_layer_id) const {
  5630. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0);
  5631. return physics[p_layer_id].polygons.size();
  5632. }
  5633. void TileData::add_collision_polygon(int p_layer_id) {
  5634. ERR_FAIL_INDEX(p_layer_id, physics.size());
  5635. physics.write[p_layer_id].polygons.push_back(PhysicsLayerTileData::PolygonShapeTileData());
  5636. emit_signal(CoreStringName(changed));
  5637. }
  5638. void TileData::remove_collision_polygon(int p_layer_id, int p_polygon_index) {
  5639. ERR_FAIL_INDEX(p_layer_id, physics.size());
  5640. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  5641. physics.write[p_layer_id].polygons.remove_at(p_polygon_index);
  5642. emit_signal(CoreStringName(changed));
  5643. }
  5644. void TileData::set_collision_polygon_points(int p_layer_id, int p_polygon_index, Vector<Vector2> p_polygon) {
  5645. ERR_FAIL_INDEX(p_layer_id, physics.size());
  5646. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  5647. ERR_FAIL_COND_MSG(p_polygon.size() != 0 && p_polygon.size() < 3, "Invalid polygon. Needs either 0 or more than 3 points.");
  5648. TileData::PhysicsLayerTileData::PolygonShapeTileData &polygon_shape_tile_data = physics.write[p_layer_id].polygons.write[p_polygon_index];
  5649. if (p_polygon.is_empty()) {
  5650. polygon_shape_tile_data.shapes.clear();
  5651. } else {
  5652. // Decompose into convex shapes.
  5653. Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(p_polygon);
  5654. ERR_FAIL_COND_MSG(decomp.is_empty(), "Could not decompose the polygon into convex shapes.");
  5655. polygon_shape_tile_data.shapes.resize(decomp.size());
  5656. for (int i = 0; i < decomp.size(); i++) {
  5657. Ref<ConvexPolygonShape2D> shape;
  5658. shape.instantiate();
  5659. shape->set_points(decomp[i]);
  5660. polygon_shape_tile_data.shapes[i] = shape;
  5661. }
  5662. }
  5663. polygon_shape_tile_data.transformed_shapes.clear();
  5664. polygon_shape_tile_data.polygon = p_polygon;
  5665. emit_signal(CoreStringName(changed));
  5666. }
  5667. Vector<Vector2> TileData::get_collision_polygon_points(int p_layer_id, int p_polygon_index) const {
  5668. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Vector<Vector2>());
  5669. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), Vector<Vector2>());
  5670. return Vector<Vector2>(physics[p_layer_id].polygons[p_polygon_index].polygon);
  5671. }
  5672. void TileData::set_collision_polygon_one_way(int p_layer_id, int p_polygon_index, bool p_one_way) {
  5673. ERR_FAIL_INDEX(p_layer_id, physics.size());
  5674. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  5675. physics.write[p_layer_id].polygons.write[p_polygon_index].one_way = p_one_way;
  5676. emit_signal(CoreStringName(changed));
  5677. }
  5678. bool TileData::is_collision_polygon_one_way(int p_layer_id, int p_polygon_index) const {
  5679. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), false);
  5680. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), false);
  5681. return physics[p_layer_id].polygons[p_polygon_index].one_way;
  5682. }
  5683. void TileData::set_collision_polygon_one_way_margin(int p_layer_id, int p_polygon_index, float p_one_way_margin) {
  5684. ERR_FAIL_INDEX(p_layer_id, physics.size());
  5685. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  5686. physics.write[p_layer_id].polygons.write[p_polygon_index].one_way_margin = p_one_way_margin;
  5687. emit_signal(CoreStringName(changed));
  5688. }
  5689. float TileData::get_collision_polygon_one_way_margin(int p_layer_id, int p_polygon_index) const {
  5690. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0.0);
  5691. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), 0.0);
  5692. return physics[p_layer_id].polygons[p_polygon_index].one_way_margin;
  5693. }
  5694. int TileData::get_collision_polygon_shapes_count(int p_layer_id, int p_polygon_index) const {
  5695. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0);
  5696. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), 0);
  5697. return physics[p_layer_id].polygons[p_polygon_index].shapes.size();
  5698. }
  5699. Ref<ConvexPolygonShape2D> TileData::get_collision_polygon_shape(int p_layer_id, int p_polygon_index, int shape_index, bool p_flip_h, bool p_flip_v, bool p_transpose) const {
  5700. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Ref<ConvexPolygonShape2D>());
  5701. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), Ref<ConvexPolygonShape2D>());
  5702. ERR_FAIL_INDEX_V(shape_index, (int)physics[p_layer_id].polygons[p_polygon_index].shapes.size(), Ref<ConvexPolygonShape2D>());
  5703. const PhysicsLayerTileData &layer_tile_data = physics[p_layer_id];
  5704. const PhysicsLayerTileData::PolygonShapeTileData &shapes_data = layer_tile_data.polygons[p_polygon_index];
  5705. int key = int(p_flip_h) | int(p_flip_v) << 1 | int(p_transpose) << 2;
  5706. if (key == 0) {
  5707. return shapes_data.shapes[shape_index];
  5708. }
  5709. if (shapes_data.shapes[shape_index].is_null()) {
  5710. return Ref<ConvexPolygonShape2D>();
  5711. }
  5712. HashMap<int, LocalVector<Ref<ConvexPolygonShape2D>>>::Iterator I = shapes_data.transformed_shapes.find(key);
  5713. if (!I) {
  5714. int size = shapes_data.shapes.size();
  5715. shapes_data.transformed_shapes[key].resize(size);
  5716. for (int i = 0; i < size; i++) {
  5717. Ref<ConvexPolygonShape2D> transformed_polygon;
  5718. transformed_polygon.instantiate();
  5719. transformed_polygon->set_points(get_transformed_vertices(shapes_data.shapes[i]->get_points(), p_flip_h, p_flip_v, p_transpose));
  5720. shapes_data.transformed_shapes[key][i] = transformed_polygon;
  5721. }
  5722. return shapes_data.transformed_shapes[key][shape_index];
  5723. } else {
  5724. return I->value[shape_index];
  5725. }
  5726. }
  5727. #endif // PHYSICS_2D_DISABLED
  5728. // Terrain
  5729. void TileData::set_terrain_set(int p_terrain_set) {
  5730. ERR_FAIL_COND(p_terrain_set < -1);
  5731. if (p_terrain_set == terrain_set) {
  5732. return;
  5733. }
  5734. if (tile_set) {
  5735. ERR_FAIL_COND(p_terrain_set >= tile_set->get_terrain_sets_count());
  5736. terrain = -1;
  5737. for (int i = 0; i < 16; i++) {
  5738. terrain_peering_bits[i] = -1;
  5739. }
  5740. }
  5741. terrain_set = p_terrain_set;
  5742. notify_property_list_changed();
  5743. emit_signal(CoreStringName(changed));
  5744. }
  5745. int TileData::get_terrain_set() const {
  5746. return terrain_set;
  5747. }
  5748. void TileData::set_terrain(int p_terrain) {
  5749. ERR_FAIL_COND(p_terrain < -1);
  5750. ERR_FAIL_COND(terrain_set < 0 && p_terrain != -1);
  5751. if (tile_set && terrain_set >= 0) {
  5752. ERR_FAIL_COND(p_terrain >= tile_set->get_terrains_count(terrain_set));
  5753. }
  5754. terrain = p_terrain;
  5755. emit_signal(CoreStringName(changed));
  5756. }
  5757. int TileData::get_terrain() const {
  5758. return terrain;
  5759. }
  5760. void TileData::set_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit, int p_terrain_index) {
  5761. ERR_FAIL_INDEX(p_peering_bit, TileSet::CellNeighbor::CELL_NEIGHBOR_MAX);
  5762. ERR_FAIL_COND(p_terrain_index < -1);
  5763. ERR_FAIL_COND(terrain_set < 0 && p_terrain_index != -1);
  5764. if (tile_set && terrain_set >= 0) {
  5765. ERR_FAIL_COND(p_terrain_index >= tile_set->get_terrains_count(terrain_set));
  5766. ERR_FAIL_COND(!is_valid_terrain_peering_bit(p_peering_bit));
  5767. }
  5768. terrain_peering_bits[p_peering_bit] = p_terrain_index;
  5769. emit_signal(CoreStringName(changed));
  5770. }
  5771. int TileData::get_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit) const {
  5772. ERR_FAIL_COND_V(!is_valid_terrain_peering_bit(p_peering_bit), -1);
  5773. return terrain_peering_bits[p_peering_bit];
  5774. }
  5775. bool TileData::is_valid_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit) const {
  5776. ERR_FAIL_NULL_V(tile_set, false);
  5777. return tile_set->is_valid_terrain_peering_bit(terrain_set, p_peering_bit);
  5778. }
  5779. TileSet::TerrainsPattern TileData::get_terrains_pattern() const {
  5780. ERR_FAIL_NULL_V(tile_set, TileSet::TerrainsPattern());
  5781. TileSet::TerrainsPattern output(tile_set, terrain_set);
  5782. output.set_terrain(terrain);
  5783. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  5784. if (tile_set->is_valid_terrain_peering_bit(terrain_set, TileSet::CellNeighbor(i))) {
  5785. output.set_terrain_peering_bit(TileSet::CellNeighbor(i), get_terrain_peering_bit(TileSet::CellNeighbor(i)));
  5786. }
  5787. }
  5788. return output;
  5789. }
  5790. #ifndef NAVIGATION_2D_DISABLED
  5791. // Navigation
  5792. void TileData::set_navigation_polygon(int p_layer_id, Ref<NavigationPolygon> p_navigation_polygon) {
  5793. ERR_FAIL_INDEX(p_layer_id, navigation.size());
  5794. navigation.write[p_layer_id].navigation_polygon = p_navigation_polygon;
  5795. navigation.write[p_layer_id].transformed_navigation_polygon.clear();
  5796. emit_signal(CoreStringName(changed));
  5797. }
  5798. Ref<NavigationPolygon> TileData::get_navigation_polygon(int p_layer_id, bool p_flip_h, bool p_flip_v, bool p_transpose) const {
  5799. ERR_FAIL_INDEX_V(p_layer_id, navigation.size(), Ref<NavigationPolygon>());
  5800. const NavigationLayerTileData &layer_tile_data = navigation[p_layer_id];
  5801. int key = int(p_flip_h) | int(p_flip_v) << 1 | int(p_transpose) << 2;
  5802. if (key == 0) {
  5803. return layer_tile_data.navigation_polygon;
  5804. }
  5805. if (layer_tile_data.navigation_polygon.is_null()) {
  5806. return Ref<NavigationPolygon>();
  5807. }
  5808. HashMap<int, Ref<NavigationPolygon>>::Iterator I = layer_tile_data.transformed_navigation_polygon.find(key);
  5809. if (!I) {
  5810. Ref<NavigationPolygon> transformed_polygon;
  5811. transformed_polygon.instantiate();
  5812. PackedVector2Array new_points = get_transformed_vertices(layer_tile_data.navigation_polygon->get_vertices(), p_flip_h, p_flip_v, p_transpose);
  5813. const Vector<Vector<Vector2>> outlines = layer_tile_data.navigation_polygon->get_outlines();
  5814. int outline_count = outlines.size();
  5815. Vector<Vector<Vector2>> new_outlines;
  5816. new_outlines.resize(outline_count);
  5817. for (int i = 0; i < outline_count; i++) {
  5818. new_outlines.write[i] = get_transformed_vertices(outlines[i], p_flip_h, p_flip_v, p_transpose);
  5819. }
  5820. transformed_polygon->set_data(new_points, layer_tile_data.navigation_polygon->get_polygons(), new_outlines);
  5821. layer_tile_data.transformed_navigation_polygon[key] = transformed_polygon;
  5822. return transformed_polygon;
  5823. } else {
  5824. return I->value;
  5825. }
  5826. }
  5827. #endif // NAVIGATION_2D_DISABLED
  5828. // Misc
  5829. void TileData::set_probability(float p_probability) {
  5830. ERR_FAIL_COND(p_probability < 0.0);
  5831. probability = p_probability;
  5832. emit_signal(CoreStringName(changed));
  5833. }
  5834. float TileData::get_probability() const {
  5835. return probability;
  5836. }
  5837. // Custom data
  5838. void TileData::set_custom_data(String p_layer_name, Variant p_value) {
  5839. ERR_FAIL_NULL(tile_set);
  5840. int p_layer_id = tile_set->get_custom_data_layer_by_name(p_layer_name);
  5841. ERR_FAIL_COND_MSG(p_layer_id < 0, vformat("TileSet has no layer with name: %s", p_layer_name));
  5842. set_custom_data_by_layer_id(p_layer_id, p_value);
  5843. }
  5844. Variant TileData::get_custom_data(String p_layer_name) const {
  5845. ERR_FAIL_NULL_V(tile_set, Variant());
  5846. int p_layer_id = tile_set->get_custom_data_layer_by_name(p_layer_name);
  5847. ERR_FAIL_COND_V_MSG(p_layer_id < 0, Variant(), vformat("TileSet has no layer with name: %s", p_layer_name));
  5848. return get_custom_data_by_layer_id(p_layer_id);
  5849. }
  5850. bool TileData::has_custom_data(const String &p_layer_name) const {
  5851. ERR_FAIL_NULL_V(tile_set, false);
  5852. return tile_set->has_custom_data_layer_by_name(p_layer_name);
  5853. }
  5854. void TileData::set_custom_data_by_layer_id(int p_layer_id, Variant p_value) {
  5855. ERR_FAIL_INDEX(p_layer_id, custom_data.size());
  5856. custom_data.write[p_layer_id] = p_value;
  5857. emit_signal(CoreStringName(changed));
  5858. }
  5859. Variant TileData::get_custom_data_by_layer_id(int p_layer_id) const {
  5860. ERR_FAIL_INDEX_V(p_layer_id, custom_data.size(), Variant());
  5861. return custom_data[p_layer_id];
  5862. }
  5863. PackedVector2Array TileData::get_transformed_vertices(const PackedVector2Array &p_vertices, bool p_flip_h, bool p_flip_v, bool p_transpose) {
  5864. const Vector2 *r = p_vertices.ptr();
  5865. int size = p_vertices.size();
  5866. PackedVector2Array new_points;
  5867. new_points.resize(size);
  5868. Vector2 *w = new_points.ptrw();
  5869. for (int i = 0; i < size; i++) {
  5870. Vector2 v;
  5871. if (p_transpose) {
  5872. v = Vector2(r[i].y, r[i].x);
  5873. } else {
  5874. v = r[i];
  5875. }
  5876. if (p_flip_h) {
  5877. v.x *= -1;
  5878. }
  5879. if (p_flip_v) {
  5880. v.y *= -1;
  5881. }
  5882. w[i] = v;
  5883. }
  5884. return new_points;
  5885. }
  5886. bool TileData::_set(const StringName &p_name, const Variant &p_value) {
  5887. #ifndef DISABLE_DEPRECATED
  5888. if (p_name == "texture_offset") {
  5889. texture_origin = p_value;
  5890. return true;
  5891. }
  5892. #endif
  5893. Vector<String> components = String(p_name).split("/", true, 2);
  5894. if (components.size() >= 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  5895. // Occlusion layers.
  5896. int layer_index = components[0].trim_prefix("occlusion_layer_").to_int();
  5897. ERR_FAIL_COND_V(layer_index < 0, false);
  5898. if (components.size() == 2) {
  5899. if (components[1] == "polygon") {
  5900. // Kept for compatibility.
  5901. Ref<OccluderPolygon2D> polygon = p_value;
  5902. if (layer_index >= occluders.size()) {
  5903. if (tile_set) {
  5904. return false;
  5905. } else {
  5906. occluders.resize(layer_index + 1);
  5907. }
  5908. }
  5909. if (get_occluder_polygons_count(layer_index) == 0) {
  5910. add_occluder_polygon(layer_index);
  5911. }
  5912. set_occluder_polygon(layer_index, 0, polygon);
  5913. return true;
  5914. } else if (components[1] == "polygons_count") {
  5915. if (p_value.get_type() != Variant::INT) {
  5916. return false;
  5917. }
  5918. set_occluder_polygons_count(layer_index, p_value);
  5919. return true;
  5920. }
  5921. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  5922. // Polygons.
  5923. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  5924. ERR_FAIL_COND_V(polygon_index < 0, false);
  5925. if (layer_index >= occluders.size()) {
  5926. if (tile_set) {
  5927. return false;
  5928. } else {
  5929. occluders.resize(layer_index + 1);
  5930. }
  5931. }
  5932. if (polygon_index >= occluders[layer_index].polygons.size()) {
  5933. occluders.write[layer_index].polygons.resize(polygon_index + 1);
  5934. }
  5935. if (components[2] == "polygon") {
  5936. Ref<OccluderPolygon2D> polygon = p_value;
  5937. set_occluder_polygon(layer_index, polygon_index, polygon);
  5938. return true;
  5939. }
  5940. }
  5941. }
  5942. #ifndef PHYSICS_2D_DISABLED
  5943. else if (components.size() >= 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  5944. // Physics layers.
  5945. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  5946. ERR_FAIL_COND_V(layer_index < 0, false);
  5947. if (components.size() == 2) {
  5948. if (layer_index >= physics.size()) {
  5949. if (tile_set) {
  5950. return false;
  5951. } else {
  5952. physics.resize(layer_index + 1);
  5953. }
  5954. }
  5955. if (components[1] == "linear_velocity") {
  5956. set_constant_linear_velocity(layer_index, p_value);
  5957. return true;
  5958. } else if (components[1] == "angular_velocity") {
  5959. set_constant_angular_velocity(layer_index, p_value);
  5960. return true;
  5961. } else if (components[1] == "polygons_count") {
  5962. if (p_value.get_type() != Variant::INT) {
  5963. return false;
  5964. }
  5965. set_collision_polygons_count(layer_index, p_value);
  5966. return true;
  5967. }
  5968. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  5969. // Polygons.
  5970. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  5971. ERR_FAIL_COND_V(polygon_index < 0, false);
  5972. if (components[2] == "points" || components[2] == "one_way" || components[2] == "one_way_margin") {
  5973. if (layer_index >= physics.size()) {
  5974. if (tile_set) {
  5975. return false;
  5976. } else {
  5977. physics.resize(layer_index + 1);
  5978. }
  5979. }
  5980. if (polygon_index >= physics[layer_index].polygons.size()) {
  5981. physics.write[layer_index].polygons.resize(polygon_index + 1);
  5982. }
  5983. }
  5984. if (components[2] == "points") {
  5985. Vector<Vector2> polygon = p_value;
  5986. set_collision_polygon_points(layer_index, polygon_index, polygon);
  5987. return true;
  5988. } else if (components[2] == "one_way") {
  5989. set_collision_polygon_one_way(layer_index, polygon_index, p_value);
  5990. return true;
  5991. } else if (components[2] == "one_way_margin") {
  5992. set_collision_polygon_one_way_margin(layer_index, polygon_index, p_value);
  5993. return true;
  5994. }
  5995. }
  5996. }
  5997. #endif // PHYSICS_2D_DISABLED
  5998. #ifndef NAVIGATION_2D_DISABLED
  5999. else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  6000. // Navigation layers.
  6001. int layer_index = components[0].trim_prefix("navigation_layer_").to_int();
  6002. ERR_FAIL_COND_V(layer_index < 0, false);
  6003. if (components[1] == "polygon") {
  6004. Ref<NavigationPolygon> polygon = p_value;
  6005. if (layer_index >= navigation.size()) {
  6006. if (tile_set) {
  6007. return false;
  6008. } else {
  6009. navigation.resize(layer_index + 1);
  6010. }
  6011. }
  6012. set_navigation_polygon(layer_index, polygon);
  6013. return true;
  6014. }
  6015. }
  6016. #endif // NAVIGATION_2D_DISABLED
  6017. else if (components.size() == 2 && components[0] == "terrains_peering_bit") {
  6018. // Terrains.
  6019. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  6020. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  6021. if (components[1] == TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]) {
  6022. set_terrain_peering_bit(bit, p_value);
  6023. return true;
  6024. }
  6025. }
  6026. return false;
  6027. } else if (components.size() == 1 && components[0].begins_with("custom_data_") && components[0].trim_prefix("custom_data_").is_valid_int()) {
  6028. // Custom data layers.
  6029. int layer_index = components[0].trim_prefix("custom_data_").to_int();
  6030. ERR_FAIL_COND_V(layer_index < 0, false);
  6031. if (layer_index >= custom_data.size()) {
  6032. if (tile_set) {
  6033. return false;
  6034. } else {
  6035. custom_data.resize(layer_index + 1);
  6036. }
  6037. }
  6038. set_custom_data_by_layer_id(layer_index, p_value);
  6039. return true;
  6040. }
  6041. return false;
  6042. }
  6043. bool TileData::_get(const StringName &p_name, Variant &r_ret) const {
  6044. #ifndef DISABLE_DEPRECATED
  6045. if (p_name == "texture_offset") {
  6046. r_ret = texture_origin;
  6047. return true;
  6048. }
  6049. #endif
  6050. Vector<String> components = String(p_name).split("/", true, 2);
  6051. if (tile_set) {
  6052. if (components.size() >= 2 && components[0].begins_with("occlusion_layer") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  6053. // Occlusion layers.
  6054. int layer_index = components[0].trim_prefix("occlusion_layer_").to_int();
  6055. ERR_FAIL_COND_V(layer_index < 0, false);
  6056. if (layer_index >= occluders.size()) {
  6057. return false;
  6058. }
  6059. if (components.size() == 2) {
  6060. if (components[1] == "polygon") {
  6061. // Kept for compatibility.
  6062. if (occluders[layer_index].polygons.is_empty()) {
  6063. return false;
  6064. }
  6065. r_ret = get_occluder_polygon(layer_index, 0);
  6066. return true;
  6067. } else if (components[1] == "polygons_count") {
  6068. r_ret = get_occluder_polygons_count(layer_index);
  6069. return true;
  6070. }
  6071. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  6072. // Polygons.
  6073. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  6074. ERR_FAIL_COND_V(polygon_index < 0, false);
  6075. if (polygon_index >= occluders[layer_index].polygons.size()) {
  6076. return false;
  6077. }
  6078. if (components[2] == "polygon") {
  6079. r_ret = get_occluder_polygon(layer_index, polygon_index);
  6080. return true;
  6081. }
  6082. }
  6083. }
  6084. #ifndef PHYSICS_2D_DISABLED
  6085. else if (components.size() >= 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  6086. // Physics layers.
  6087. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  6088. ERR_FAIL_COND_V(layer_index < 0, false);
  6089. if (layer_index >= physics.size()) {
  6090. return false;
  6091. }
  6092. if (components.size() == 2) {
  6093. if (components[1] == "linear_velocity") {
  6094. r_ret = get_constant_linear_velocity(layer_index);
  6095. return true;
  6096. } else if (components[1] == "angular_velocity") {
  6097. r_ret = get_constant_angular_velocity(layer_index);
  6098. return true;
  6099. } else if (components[1] == "polygons_count") {
  6100. r_ret = get_collision_polygons_count(layer_index);
  6101. return true;
  6102. }
  6103. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  6104. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  6105. ERR_FAIL_COND_V(polygon_index < 0, false);
  6106. if (polygon_index >= physics[layer_index].polygons.size()) {
  6107. return false;
  6108. }
  6109. if (components[2] == "points") {
  6110. r_ret = get_collision_polygon_points(layer_index, polygon_index);
  6111. return true;
  6112. } else if (components[2] == "one_way") {
  6113. r_ret = is_collision_polygon_one_way(layer_index, polygon_index);
  6114. return true;
  6115. } else if (components[2] == "one_way_margin") {
  6116. r_ret = get_collision_polygon_one_way_margin(layer_index, polygon_index);
  6117. return true;
  6118. }
  6119. }
  6120. }
  6121. #endif // PHYSICS_2D_DISABLED
  6122. else if (components.size() == 2 && components[0] == "terrains_peering_bit") {
  6123. // Terrains.
  6124. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  6125. if (components[1] == TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]) {
  6126. r_ret = terrain_peering_bits[i];
  6127. return true;
  6128. }
  6129. }
  6130. return false;
  6131. }
  6132. #ifndef NAVIGATION_2D_DISABLED
  6133. else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  6134. // Occlusion layers.
  6135. int layer_index = components[0].trim_prefix("navigation_layer_").to_int();
  6136. ERR_FAIL_COND_V(layer_index < 0, false);
  6137. if (layer_index >= navigation.size()) {
  6138. return false;
  6139. }
  6140. if (components[1] == "polygon") {
  6141. r_ret = get_navigation_polygon(layer_index);
  6142. return true;
  6143. }
  6144. }
  6145. #endif // NAVIGATION_2D_DISABLED
  6146. else if (components.size() == 1 && components[0].begins_with("custom_data_") && components[0].trim_prefix("custom_data_").is_valid_int()) {
  6147. // Custom data layers.
  6148. int layer_index = components[0].trim_prefix("custom_data_").to_int();
  6149. ERR_FAIL_COND_V(layer_index < 0, false);
  6150. if (layer_index >= custom_data.size()) {
  6151. return false;
  6152. }
  6153. r_ret = get_custom_data_by_layer_id(layer_index);
  6154. return true;
  6155. }
  6156. }
  6157. return false;
  6158. }
  6159. void TileData::_get_property_list(List<PropertyInfo> *p_list) const {
  6160. PropertyInfo property_info;
  6161. // Add the groups manually.
  6162. if (tile_set) {
  6163. // Occlusion layers.
  6164. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Rendering", ""), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  6165. for (int i = 0; i < occluders.size(); i++) {
  6166. p_list->push_back(PropertyInfo(Variant::INT, vformat("occlusion_layer_%d/%s", i, PNAME("polygons_count")), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  6167. for (int j = 0; j < occluders[i].polygons.size(); j++) {
  6168. // occlusion_layer_%d/polygon_%d/polygon
  6169. property_info = PropertyInfo(Variant::OBJECT, vformat("occlusion_layer_%d/polygon_%d/%s", i, j, PNAME("polygon")), PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D", PROPERTY_USAGE_DEFAULT);
  6170. if (occluders[i].polygons[j].occluder_polygon.is_null()) {
  6171. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  6172. }
  6173. p_list->push_back(property_info);
  6174. }
  6175. }
  6176. #ifndef PHYSICS_2D_DISABLED
  6177. // Physics layers.
  6178. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Physics", ""), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  6179. for (int i = 0; i < physics.size(); i++) {
  6180. // physics_layer_%d/linear_velocity
  6181. property_info = PropertyInfo(Variant::VECTOR2, vformat("physics_layer_%d/%s", i, PNAME("linear_velocity")), PROPERTY_HINT_NONE);
  6182. if (physics[i].linear_velocity == Vector2()) {
  6183. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  6184. }
  6185. p_list->push_back(property_info);
  6186. // physics_layer_%d/angular_velocity
  6187. property_info = PropertyInfo(Variant::FLOAT, vformat("physics_layer_%d/%s", i, PNAME("angular_velocity")), PROPERTY_HINT_NONE);
  6188. if (physics[i].angular_velocity == 0.0) {
  6189. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  6190. }
  6191. p_list->push_back(property_info);
  6192. p_list->push_back(PropertyInfo(Variant::INT, vformat("physics_layer_%d/%s", i, PNAME("polygons_count")), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  6193. for (int j = 0; j < physics[i].polygons.size(); j++) {
  6194. // physics_layer_%d/points
  6195. property_info = PropertyInfo(Variant::ARRAY, vformat("physics_layer_%d/polygon_%d/%s", i, j, PNAME("points")), PROPERTY_HINT_ARRAY_TYPE, "Vector2", PROPERTY_USAGE_DEFAULT);
  6196. if (physics[i].polygons[j].polygon.is_empty()) {
  6197. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  6198. }
  6199. p_list->push_back(property_info);
  6200. // physics_layer_%d/polygon_%d/one_way
  6201. property_info = PropertyInfo(Variant::BOOL, vformat("physics_layer_%d/polygon_%d/%s", i, j, PNAME("one_way")));
  6202. if (physics[i].polygons[j].one_way == false) {
  6203. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  6204. }
  6205. p_list->push_back(property_info);
  6206. // physics_layer_%d/polygon_%d/one_way_margin
  6207. property_info = PropertyInfo(Variant::FLOAT, vformat("physics_layer_%d/polygon_%d/%s", i, j, PNAME("one_way_margin")));
  6208. if (physics[i].polygons[j].one_way_margin == 1.0) {
  6209. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  6210. }
  6211. p_list->push_back(property_info);
  6212. }
  6213. }
  6214. #endif // PHYSICS_2D_DISABLED
  6215. // Terrain data
  6216. if (terrain_set >= 0) {
  6217. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Terrains", ""), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  6218. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  6219. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  6220. if (is_valid_terrain_peering_bit(bit)) {
  6221. property_info = PropertyInfo(Variant::INT, vformat("%s/%s", PNAME("terrains_peering_bit"), TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]));
  6222. if (get_terrain_peering_bit(bit) == -1) {
  6223. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  6224. }
  6225. p_list->push_back(property_info);
  6226. }
  6227. }
  6228. }
  6229. #ifndef NAVIGATION_2D_DISABLED
  6230. // Navigation layers.
  6231. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Navigation", ""), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  6232. for (int i = 0; i < navigation.size(); i++) {
  6233. property_info = PropertyInfo(Variant::OBJECT, vformat("navigation_layer_%d/%s", i, PNAME("polygon")), PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon", PROPERTY_USAGE_DEFAULT);
  6234. if (navigation[i].navigation_polygon.is_null()) {
  6235. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  6236. }
  6237. p_list->push_back(property_info);
  6238. }
  6239. #endif // NAVIGATION_2D_DISABLED
  6240. // Custom data layers.
  6241. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Custom Data", "custom_data_"), PROPERTY_HINT_NONE, "custom_data_", PROPERTY_USAGE_GROUP));
  6242. for (int i = 0; i < custom_data.size(); i++) {
  6243. Variant default_val;
  6244. Callable::CallError error;
  6245. Variant::construct(custom_data[i].get_type(), default_val, nullptr, 0, error);
  6246. 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);
  6247. if (custom_data[i] == default_val) {
  6248. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  6249. }
  6250. p_list->push_back(property_info);
  6251. }
  6252. }
  6253. }
  6254. void TileData::_bind_methods() {
  6255. // Rendering.
  6256. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &TileData::set_flip_h);
  6257. ClassDB::bind_method(D_METHOD("get_flip_h"), &TileData::get_flip_h);
  6258. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &TileData::set_flip_v);
  6259. ClassDB::bind_method(D_METHOD("get_flip_v"), &TileData::get_flip_v);
  6260. ClassDB::bind_method(D_METHOD("set_transpose", "transpose"), &TileData::set_transpose);
  6261. ClassDB::bind_method(D_METHOD("get_transpose"), &TileData::get_transpose);
  6262. ClassDB::bind_method(D_METHOD("set_material", "material"), &TileData::set_material);
  6263. ClassDB::bind_method(D_METHOD("get_material"), &TileData::get_material);
  6264. ClassDB::bind_method(D_METHOD("set_texture_origin", "texture_origin"), &TileData::set_texture_origin);
  6265. ClassDB::bind_method(D_METHOD("get_texture_origin"), &TileData::get_texture_origin);
  6266. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &TileData::set_modulate);
  6267. ClassDB::bind_method(D_METHOD("get_modulate"), &TileData::get_modulate);
  6268. ClassDB::bind_method(D_METHOD("set_z_index", "z_index"), &TileData::set_z_index);
  6269. ClassDB::bind_method(D_METHOD("get_z_index"), &TileData::get_z_index);
  6270. ClassDB::bind_method(D_METHOD("set_y_sort_origin", "y_sort_origin"), &TileData::set_y_sort_origin);
  6271. ClassDB::bind_method(D_METHOD("get_y_sort_origin"), &TileData::get_y_sort_origin);
  6272. ClassDB::bind_method(D_METHOD("set_occluder_polygons_count", "layer_id", "polygons_count"), &TileData::set_occluder_polygons_count);
  6273. ClassDB::bind_method(D_METHOD("get_occluder_polygons_count", "layer_id"), &TileData::get_occluder_polygons_count);
  6274. ClassDB::bind_method(D_METHOD("add_occluder_polygon", "layer_id"), &TileData::add_occluder_polygon);
  6275. ClassDB::bind_method(D_METHOD("remove_occluder_polygon", "layer_id", "polygon_index"), &TileData::remove_occluder_polygon);
  6276. ClassDB::bind_method(D_METHOD("set_occluder_polygon", "layer_id", "polygon_index", "polygon"), &TileData::set_occluder_polygon);
  6277. ClassDB::bind_method(D_METHOD("get_occluder_polygon", "layer_id", "polygon_index", "flip_h", "flip_v", "transpose"), &TileData::get_occluder_polygon, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  6278. #ifndef DISABLE_DEPRECATED
  6279. ClassDB::bind_method(D_METHOD("set_occluder", "layer_id", "occluder_polygon"), &TileData::set_occluder);
  6280. ClassDB::bind_method(D_METHOD("get_occluder", "layer_id", "flip_h", "flip_v", "transpose"), &TileData::get_occluder, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  6281. #endif // DISABLE_DEPRECATED
  6282. #ifndef PHYSICS_2D_DISABLED
  6283. // Physics.
  6284. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "layer_id", "velocity"), &TileData::set_constant_linear_velocity);
  6285. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity", "layer_id"), &TileData::get_constant_linear_velocity);
  6286. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "layer_id", "velocity"), &TileData::set_constant_angular_velocity);
  6287. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity", "layer_id"), &TileData::get_constant_angular_velocity);
  6288. ClassDB::bind_method(D_METHOD("set_collision_polygons_count", "layer_id", "polygons_count"), &TileData::set_collision_polygons_count);
  6289. ClassDB::bind_method(D_METHOD("get_collision_polygons_count", "layer_id"), &TileData::get_collision_polygons_count);
  6290. ClassDB::bind_method(D_METHOD("add_collision_polygon", "layer_id"), &TileData::add_collision_polygon);
  6291. ClassDB::bind_method(D_METHOD("remove_collision_polygon", "layer_id", "polygon_index"), &TileData::remove_collision_polygon);
  6292. ClassDB::bind_method(D_METHOD("set_collision_polygon_points", "layer_id", "polygon_index", "polygon"), &TileData::set_collision_polygon_points);
  6293. ClassDB::bind_method(D_METHOD("get_collision_polygon_points", "layer_id", "polygon_index"), &TileData::get_collision_polygon_points);
  6294. ClassDB::bind_method(D_METHOD("set_collision_polygon_one_way", "layer_id", "polygon_index", "one_way"), &TileData::set_collision_polygon_one_way);
  6295. ClassDB::bind_method(D_METHOD("is_collision_polygon_one_way", "layer_id", "polygon_index"), &TileData::is_collision_polygon_one_way);
  6296. 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);
  6297. ClassDB::bind_method(D_METHOD("get_collision_polygon_one_way_margin", "layer_id", "polygon_index"), &TileData::get_collision_polygon_one_way_margin);
  6298. #endif // PHYSICS_2D_DISABLED
  6299. // Terrain
  6300. ClassDB::bind_method(D_METHOD("set_terrain_set", "terrain_set"), &TileData::set_terrain_set);
  6301. ClassDB::bind_method(D_METHOD("get_terrain_set"), &TileData::get_terrain_set);
  6302. ClassDB::bind_method(D_METHOD("set_terrain", "terrain"), &TileData::set_terrain);
  6303. ClassDB::bind_method(D_METHOD("get_terrain"), &TileData::get_terrain);
  6304. ClassDB::bind_method(D_METHOD("set_terrain_peering_bit", "peering_bit", "terrain"), &TileData::set_terrain_peering_bit);
  6305. ClassDB::bind_method(D_METHOD("get_terrain_peering_bit", "peering_bit"), &TileData::get_terrain_peering_bit);
  6306. ClassDB::bind_method(D_METHOD("is_valid_terrain_peering_bit", "peering_bit"), &TileData::is_valid_terrain_peering_bit);
  6307. #ifndef NAVIGATION_2D_DISABLED
  6308. // Navigation
  6309. ClassDB::bind_method(D_METHOD("set_navigation_polygon", "layer_id", "navigation_polygon"), &TileData::set_navigation_polygon);
  6310. ClassDB::bind_method(D_METHOD("get_navigation_polygon", "layer_id", "flip_h", "flip_v", "transpose"), &TileData::get_navigation_polygon, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  6311. #endif // NAVIGATION_2D_DISABLED
  6312. // Misc.
  6313. ClassDB::bind_method(D_METHOD("set_probability", "probability"), &TileData::set_probability);
  6314. ClassDB::bind_method(D_METHOD("get_probability"), &TileData::get_probability);
  6315. // Custom data.
  6316. ClassDB::bind_method(D_METHOD("set_custom_data", "layer_name", "value"), &TileData::set_custom_data);
  6317. ClassDB::bind_method(D_METHOD("get_custom_data", "layer_name"), &TileData::get_custom_data);
  6318. ClassDB::bind_method(D_METHOD("has_custom_data", "layer_name"), &TileData::has_custom_data);
  6319. ClassDB::bind_method(D_METHOD("set_custom_data_by_layer_id", "layer_id", "value"), &TileData::set_custom_data_by_layer_id);
  6320. ClassDB::bind_method(D_METHOD("get_custom_data_by_layer_id", "layer_id"), &TileData::get_custom_data_by_layer_id);
  6321. ADD_GROUP("Rendering", "");
  6322. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "get_flip_h");
  6323. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "get_flip_v");
  6324. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transpose"), "set_transpose", "get_transpose");
  6325. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "texture_origin", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_origin", "get_texture_origin");
  6326. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  6327. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemMaterial,ShaderMaterial"), "set_material", "get_material");
  6328. ADD_PROPERTY(PropertyInfo(Variant::INT, "z_index"), "set_z_index", "get_z_index");
  6329. ADD_PROPERTY(PropertyInfo(Variant::INT, "y_sort_origin"), "set_y_sort_origin", "get_y_sort_origin");
  6330. ADD_GROUP("Terrains", "");
  6331. ADD_PROPERTY(PropertyInfo(Variant::INT, "terrain_set"), "set_terrain_set", "get_terrain_set");
  6332. ADD_PROPERTY(PropertyInfo(Variant::INT, "terrain"), "set_terrain", "get_terrain");
  6333. ADD_GROUP("Miscellaneous", "");
  6334. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "probability"), "set_probability", "get_probability");
  6335. ADD_SIGNAL(MethodInfo("changed"));
  6336. }