tile_set.cpp 272 KB

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