tile_set.cpp 281 KB

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