tile_set.cpp 283 KB

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