tile_set.cpp 281 KB

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