tile_set.cpp 219 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761
  1. /*************************************************************************/
  2. /* tile_set.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/core_string_names.h"
  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/2d/navigation_region_2d.h"
  37. #include "scene/gui/control.h"
  38. #include "scene/resources/convex_polygon_shape_2d.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 = Vector2i();
  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. Vector2i TileMapPattern::get_size() const {
  134. return size;
  135. }
  136. void TileMapPattern::set_size(const Vector2i &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 = Vector2i();
  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. "right_side",
  279. "right_corner",
  280. "bottom_right_side",
  281. "bottom_right_corner",
  282. "bottom_side",
  283. "bottom_corner",
  284. "bottom_left_side",
  285. "bottom_left_corner",
  286. "left_side",
  287. "left_corner",
  288. "top_left_side",
  289. "top_left_corner",
  290. "top_side",
  291. "top_corner",
  292. "top_right_side",
  293. "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 (int i = 0; i < (int)per_terrain_pattern_tiles.size(); i++) {
  346. per_terrain_pattern_tiles[i].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. int new_source_id = p_atlas_source_id_override >= 0 ? p_atlas_source_id_override : next_source_id;
  406. sources[new_source_id] = p_tile_set_source;
  407. source_ids.append(new_source_id);
  408. source_ids.sort();
  409. p_tile_set_source->set_tile_set(this);
  410. _compute_next_source_id();
  411. sources[new_source_id]->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TileSet::_source_changed));
  412. terrains_cache_dirty = true;
  413. emit_changed();
  414. return new_source_id;
  415. }
  416. void TileSet::remove_source(int p_source_id) {
  417. 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));
  418. sources[p_source_id]->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TileSet::_source_changed));
  419. sources[p_source_id]->set_tile_set(nullptr);
  420. sources.erase(p_source_id);
  421. source_ids.erase(p_source_id);
  422. source_ids.sort();
  423. terrains_cache_dirty = true;
  424. emit_changed();
  425. }
  426. void TileSet::set_source_id(int p_source_id, int p_new_source_id) {
  427. ERR_FAIL_COND(p_new_source_id < 0);
  428. 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));
  429. if (p_source_id == p_new_source_id) {
  430. return;
  431. }
  432. 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));
  433. sources[p_new_source_id] = sources[p_source_id];
  434. sources.erase(p_source_id);
  435. source_ids.erase(p_source_id);
  436. source_ids.append(p_new_source_id);
  437. source_ids.sort();
  438. _compute_next_source_id();
  439. terrains_cache_dirty = true;
  440. emit_changed();
  441. }
  442. bool TileSet::has_source(int p_source_id) const {
  443. return sources.has(p_source_id);
  444. }
  445. Ref<TileSetSource> TileSet::get_source(int p_source_id) const {
  446. ERR_FAIL_COND_V_MSG(!sources.has(p_source_id), nullptr, vformat("No TileSet atlas source with id %d.", p_source_id));
  447. return sources[p_source_id];
  448. }
  449. int TileSet::get_source_count() const {
  450. return source_ids.size();
  451. }
  452. int TileSet::get_source_id(int p_index) const {
  453. ERR_FAIL_INDEX_V(p_index, source_ids.size(), TileSet::INVALID_SOURCE);
  454. return source_ids[p_index];
  455. }
  456. // Rendering
  457. void TileSet::set_uv_clipping(bool p_uv_clipping) {
  458. if (uv_clipping == p_uv_clipping) {
  459. return;
  460. }
  461. uv_clipping = p_uv_clipping;
  462. emit_changed();
  463. }
  464. bool TileSet::is_uv_clipping() const {
  465. return uv_clipping;
  466. };
  467. int TileSet::get_occlusion_layers_count() const {
  468. return occlusion_layers.size();
  469. };
  470. void TileSet::add_occlusion_layer(int p_index) {
  471. if (p_index < 0) {
  472. p_index = occlusion_layers.size();
  473. }
  474. ERR_FAIL_INDEX(p_index, occlusion_layers.size() + 1);
  475. occlusion_layers.insert(p_index, OcclusionLayer());
  476. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  477. source.value->add_occlusion_layer(p_index);
  478. }
  479. notify_property_list_changed();
  480. emit_changed();
  481. }
  482. void TileSet::move_occlusion_layer(int p_from_index, int p_to_pos) {
  483. ERR_FAIL_INDEX(p_from_index, occlusion_layers.size());
  484. ERR_FAIL_INDEX(p_to_pos, occlusion_layers.size() + 1);
  485. occlusion_layers.insert(p_to_pos, occlusion_layers[p_from_index]);
  486. occlusion_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  487. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  488. source.value->move_occlusion_layer(p_from_index, p_to_pos);
  489. }
  490. notify_property_list_changed();
  491. emit_changed();
  492. }
  493. void TileSet::remove_occlusion_layer(int p_index) {
  494. ERR_FAIL_INDEX(p_index, occlusion_layers.size());
  495. occlusion_layers.remove_at(p_index);
  496. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  497. source.value->remove_occlusion_layer(p_index);
  498. }
  499. notify_property_list_changed();
  500. emit_changed();
  501. }
  502. void TileSet::set_occlusion_layer_light_mask(int p_layer_index, int p_light_mask) {
  503. ERR_FAIL_INDEX(p_layer_index, occlusion_layers.size());
  504. occlusion_layers.write[p_layer_index].light_mask = p_light_mask;
  505. emit_changed();
  506. }
  507. int TileSet::get_occlusion_layer_light_mask(int p_layer_index) const {
  508. ERR_FAIL_INDEX_V(p_layer_index, occlusion_layers.size(), 0);
  509. return occlusion_layers[p_layer_index].light_mask;
  510. }
  511. void TileSet::set_occlusion_layer_sdf_collision(int p_layer_index, bool p_sdf_collision) {
  512. ERR_FAIL_INDEX(p_layer_index, occlusion_layers.size());
  513. occlusion_layers.write[p_layer_index].sdf_collision = p_sdf_collision;
  514. emit_changed();
  515. }
  516. bool TileSet::get_occlusion_layer_sdf_collision(int p_layer_index) const {
  517. ERR_FAIL_INDEX_V(p_layer_index, occlusion_layers.size(), false);
  518. return occlusion_layers[p_layer_index].sdf_collision;
  519. }
  520. int TileSet::get_physics_layers_count() const {
  521. return physics_layers.size();
  522. }
  523. void TileSet::add_physics_layer(int p_index) {
  524. if (p_index < 0) {
  525. p_index = physics_layers.size();
  526. }
  527. ERR_FAIL_INDEX(p_index, physics_layers.size() + 1);
  528. physics_layers.insert(p_index, PhysicsLayer());
  529. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  530. source.value->add_physics_layer(p_index);
  531. }
  532. notify_property_list_changed();
  533. emit_changed();
  534. }
  535. void TileSet::move_physics_layer(int p_from_index, int p_to_pos) {
  536. ERR_FAIL_INDEX(p_from_index, physics_layers.size());
  537. ERR_FAIL_INDEX(p_to_pos, physics_layers.size() + 1);
  538. physics_layers.insert(p_to_pos, physics_layers[p_from_index]);
  539. physics_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  540. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  541. source.value->move_physics_layer(p_from_index, p_to_pos);
  542. }
  543. notify_property_list_changed();
  544. emit_changed();
  545. }
  546. void TileSet::remove_physics_layer(int p_index) {
  547. ERR_FAIL_INDEX(p_index, physics_layers.size());
  548. physics_layers.remove_at(p_index);
  549. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  550. source.value->remove_physics_layer(p_index);
  551. }
  552. notify_property_list_changed();
  553. emit_changed();
  554. }
  555. void TileSet::set_physics_layer_collision_layer(int p_layer_index, uint32_t p_layer) {
  556. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  557. physics_layers.write[p_layer_index].collision_layer = p_layer;
  558. emit_changed();
  559. }
  560. uint32_t TileSet::get_physics_layer_collision_layer(int p_layer_index) const {
  561. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), 0);
  562. return physics_layers[p_layer_index].collision_layer;
  563. }
  564. void TileSet::set_physics_layer_collision_mask(int p_layer_index, uint32_t p_mask) {
  565. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  566. physics_layers.write[p_layer_index].collision_mask = p_mask;
  567. emit_changed();
  568. }
  569. uint32_t TileSet::get_physics_layer_collision_mask(int p_layer_index) const {
  570. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), 0);
  571. return physics_layers[p_layer_index].collision_mask;
  572. }
  573. void TileSet::set_physics_layer_physics_material(int p_layer_index, Ref<PhysicsMaterial> p_physics_material) {
  574. ERR_FAIL_INDEX(p_layer_index, physics_layers.size());
  575. physics_layers.write[p_layer_index].physics_material = p_physics_material;
  576. }
  577. Ref<PhysicsMaterial> TileSet::get_physics_layer_physics_material(int p_layer_index) const {
  578. ERR_FAIL_INDEX_V(p_layer_index, physics_layers.size(), Ref<PhysicsMaterial>());
  579. return physics_layers[p_layer_index].physics_material;
  580. }
  581. // Terrains
  582. int TileSet::get_terrain_sets_count() const {
  583. return terrain_sets.size();
  584. }
  585. void TileSet::add_terrain_set(int p_index) {
  586. if (p_index < 0) {
  587. p_index = terrain_sets.size();
  588. }
  589. ERR_FAIL_INDEX(p_index, terrain_sets.size() + 1);
  590. terrain_sets.insert(p_index, TerrainSet());
  591. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  592. source.value->add_terrain_set(p_index);
  593. }
  594. notify_property_list_changed();
  595. terrains_cache_dirty = true;
  596. emit_changed();
  597. }
  598. void TileSet::move_terrain_set(int p_from_index, int p_to_pos) {
  599. ERR_FAIL_INDEX(p_from_index, terrain_sets.size());
  600. ERR_FAIL_INDEX(p_to_pos, terrain_sets.size() + 1);
  601. terrain_sets.insert(p_to_pos, terrain_sets[p_from_index]);
  602. terrain_sets.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  603. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  604. source.value->move_terrain_set(p_from_index, p_to_pos);
  605. }
  606. notify_property_list_changed();
  607. terrains_cache_dirty = true;
  608. emit_changed();
  609. }
  610. void TileSet::remove_terrain_set(int p_index) {
  611. ERR_FAIL_INDEX(p_index, terrain_sets.size());
  612. terrain_sets.remove_at(p_index);
  613. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  614. source.value->remove_terrain_set(p_index);
  615. }
  616. notify_property_list_changed();
  617. terrains_cache_dirty = true;
  618. emit_changed();
  619. }
  620. void TileSet::set_terrain_set_mode(int p_terrain_set, TerrainMode p_terrain_mode) {
  621. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  622. terrain_sets.write[p_terrain_set].mode = p_terrain_mode;
  623. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  624. E_source.value->notify_tile_data_properties_should_change();
  625. }
  626. notify_property_list_changed();
  627. terrains_cache_dirty = true;
  628. emit_changed();
  629. }
  630. TileSet::TerrainMode TileSet::get_terrain_set_mode(int p_terrain_set) const {
  631. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES);
  632. return terrain_sets[p_terrain_set].mode;
  633. }
  634. int TileSet::get_terrains_count(int p_terrain_set) const {
  635. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), -1);
  636. return terrain_sets[p_terrain_set].terrains.size();
  637. }
  638. void TileSet::add_terrain(int p_terrain_set, int p_index) {
  639. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  640. Vector<Terrain> &terrains = terrain_sets.write[p_terrain_set].terrains;
  641. if (p_index < 0) {
  642. p_index = terrains.size();
  643. }
  644. ERR_FAIL_INDEX(p_index, terrains.size() + 1);
  645. terrains.insert(p_index, Terrain());
  646. // Default name and color
  647. float hue_rotate = (terrains.size() % 16) / 16.0;
  648. Color c;
  649. c.set_hsv(Math::fmod(float(hue_rotate), float(1.0)), 0.5, 0.5);
  650. terrains.write[p_index].color = c;
  651. terrains.write[p_index].name = String(vformat("Terrain %d", p_index));
  652. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  653. source.value->add_terrain(p_terrain_set, p_index);
  654. }
  655. notify_property_list_changed();
  656. terrains_cache_dirty = true;
  657. emit_changed();
  658. }
  659. void TileSet::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  660. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  661. Vector<Terrain> &terrains = terrain_sets.write[p_terrain_set].terrains;
  662. ERR_FAIL_INDEX(p_from_index, terrains.size());
  663. ERR_FAIL_INDEX(p_to_pos, terrains.size() + 1);
  664. terrains.insert(p_to_pos, terrains[p_from_index]);
  665. terrains.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  666. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  667. source.value->move_terrain(p_terrain_set, p_from_index, p_to_pos);
  668. }
  669. notify_property_list_changed();
  670. terrains_cache_dirty = true;
  671. emit_changed();
  672. }
  673. void TileSet::remove_terrain(int p_terrain_set, int p_index) {
  674. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  675. Vector<Terrain> &terrains = terrain_sets.write[p_terrain_set].terrains;
  676. ERR_FAIL_INDEX(p_index, terrains.size());
  677. terrains.remove_at(p_index);
  678. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  679. source.value->remove_terrain(p_terrain_set, p_index);
  680. }
  681. notify_property_list_changed();
  682. terrains_cache_dirty = true;
  683. emit_changed();
  684. }
  685. void TileSet::set_terrain_name(int p_terrain_set, int p_terrain_index, String p_name) {
  686. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  687. ERR_FAIL_INDEX(p_terrain_index, terrain_sets[p_terrain_set].terrains.size());
  688. terrain_sets.write[p_terrain_set].terrains.write[p_terrain_index].name = p_name;
  689. emit_changed();
  690. }
  691. String TileSet::get_terrain_name(int p_terrain_set, int p_terrain_index) const {
  692. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), String());
  693. ERR_FAIL_INDEX_V(p_terrain_index, terrain_sets[p_terrain_set].terrains.size(), String());
  694. return terrain_sets[p_terrain_set].terrains[p_terrain_index].name;
  695. }
  696. void TileSet::set_terrain_color(int p_terrain_set, int p_terrain_index, Color p_color) {
  697. ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
  698. ERR_FAIL_INDEX(p_terrain_index, terrain_sets[p_terrain_set].terrains.size());
  699. if (p_color.a != 1.0) {
  700. WARN_PRINT("Terrain color should have alpha == 1.0");
  701. p_color.a = 1.0;
  702. }
  703. terrain_sets.write[p_terrain_set].terrains.write[p_terrain_index].color = p_color;
  704. emit_changed();
  705. }
  706. Color TileSet::get_terrain_color(int p_terrain_set, int p_terrain_index) const {
  707. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), Color());
  708. ERR_FAIL_INDEX_V(p_terrain_index, terrain_sets[p_terrain_set].terrains.size(), Color());
  709. return terrain_sets[p_terrain_set].terrains[p_terrain_index].color;
  710. }
  711. bool TileSet::is_valid_terrain_peering_bit_for_mode(TileSet::TerrainMode p_terrain_mode, TileSet::CellNeighbor p_peering_bit) const {
  712. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  713. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  714. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  715. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  716. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  717. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_SIDE) {
  718. return true;
  719. }
  720. }
  721. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  722. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  723. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  724. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  725. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER) {
  726. return true;
  727. }
  728. }
  729. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  730. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  731. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  732. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  733. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  734. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  735. return true;
  736. }
  737. }
  738. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  739. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  740. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  741. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  742. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_CORNER) {
  743. return true;
  744. }
  745. }
  746. } else {
  747. if (get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  748. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  749. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_SIDE ||
  750. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  751. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  752. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_SIDE ||
  753. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  754. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  755. return true;
  756. }
  757. }
  758. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  759. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  760. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_CORNER ||
  761. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  762. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  763. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_CORNER ||
  764. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER) {
  765. return true;
  766. }
  767. }
  768. } else {
  769. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_SIDES) {
  770. if (p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE ||
  771. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_SIDE ||
  772. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE ||
  773. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE ||
  774. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_SIDE ||
  775. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE) {
  776. return true;
  777. }
  778. }
  779. if (p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES || p_terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  780. if (p_peering_bit == TileSet::CELL_NEIGHBOR_RIGHT_CORNER ||
  781. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER ||
  782. p_peering_bit == TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER ||
  783. p_peering_bit == TileSet::CELL_NEIGHBOR_LEFT_CORNER ||
  784. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER ||
  785. p_peering_bit == TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER) {
  786. return true;
  787. }
  788. }
  789. }
  790. }
  791. return false;
  792. }
  793. bool TileSet::is_valid_terrain_peering_bit(int p_terrain_set, TileSet::CellNeighbor p_peering_bit) const {
  794. if (p_terrain_set < 0 || p_terrain_set >= get_terrain_sets_count()) {
  795. return false;
  796. }
  797. TileSet::TerrainMode terrain_mode = get_terrain_set_mode(p_terrain_set);
  798. return is_valid_terrain_peering_bit_for_mode(terrain_mode, p_peering_bit);
  799. }
  800. // Navigation
  801. int TileSet::get_navigation_layers_count() const {
  802. return navigation_layers.size();
  803. }
  804. void TileSet::add_navigation_layer(int p_index) {
  805. if (p_index < 0) {
  806. p_index = navigation_layers.size();
  807. }
  808. ERR_FAIL_INDEX(p_index, navigation_layers.size() + 1);
  809. navigation_layers.insert(p_index, NavigationLayer());
  810. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  811. source.value->add_navigation_layer(p_index);
  812. }
  813. notify_property_list_changed();
  814. emit_changed();
  815. }
  816. void TileSet::move_navigation_layer(int p_from_index, int p_to_pos) {
  817. ERR_FAIL_INDEX(p_from_index, navigation_layers.size());
  818. ERR_FAIL_INDEX(p_to_pos, navigation_layers.size() + 1);
  819. navigation_layers.insert(p_to_pos, navigation_layers[p_from_index]);
  820. navigation_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  821. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  822. source.value->move_navigation_layer(p_from_index, p_to_pos);
  823. }
  824. notify_property_list_changed();
  825. emit_changed();
  826. }
  827. void TileSet::remove_navigation_layer(int p_index) {
  828. ERR_FAIL_INDEX(p_index, navigation_layers.size());
  829. navigation_layers.remove_at(p_index);
  830. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  831. source.value->remove_navigation_layer(p_index);
  832. }
  833. notify_property_list_changed();
  834. emit_changed();
  835. }
  836. void TileSet::set_navigation_layer_layers(int p_layer_index, uint32_t p_layers) {
  837. ERR_FAIL_INDEX(p_layer_index, navigation_layers.size());
  838. navigation_layers.write[p_layer_index].layers = p_layers;
  839. emit_changed();
  840. }
  841. uint32_t TileSet::get_navigation_layer_layers(int p_layer_index) const {
  842. ERR_FAIL_INDEX_V(p_layer_index, navigation_layers.size(), 0);
  843. return navigation_layers[p_layer_index].layers;
  844. }
  845. // Custom data.
  846. int TileSet::get_custom_data_layers_count() const {
  847. return custom_data_layers.size();
  848. }
  849. void TileSet::add_custom_data_layer(int p_index) {
  850. if (p_index < 0) {
  851. p_index = custom_data_layers.size();
  852. }
  853. ERR_FAIL_INDEX(p_index, custom_data_layers.size() + 1);
  854. custom_data_layers.insert(p_index, CustomDataLayer());
  855. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  856. source.value->add_custom_data_layer(p_index);
  857. }
  858. notify_property_list_changed();
  859. emit_changed();
  860. }
  861. void TileSet::move_custom_data_layer(int p_from_index, int p_to_pos) {
  862. ERR_FAIL_INDEX(p_from_index, custom_data_layers.size());
  863. ERR_FAIL_INDEX(p_to_pos, custom_data_layers.size() + 1);
  864. custom_data_layers.insert(p_to_pos, custom_data_layers[p_from_index]);
  865. custom_data_layers.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  866. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  867. source.value->move_custom_data_layer(p_from_index, p_to_pos);
  868. }
  869. notify_property_list_changed();
  870. emit_changed();
  871. }
  872. void TileSet::remove_custom_data_layer(int p_index) {
  873. ERR_FAIL_INDEX(p_index, custom_data_layers.size());
  874. custom_data_layers.remove_at(p_index);
  875. for (KeyValue<String, int> E : custom_data_layers_by_name) {
  876. if (E.value == p_index) {
  877. custom_data_layers_by_name.erase(E.key);
  878. break;
  879. }
  880. }
  881. for (KeyValue<int, Ref<TileSetSource>> source : sources) {
  882. source.value->remove_custom_data_layer(p_index);
  883. }
  884. notify_property_list_changed();
  885. emit_changed();
  886. }
  887. int TileSet::get_custom_data_layer_by_name(String p_value) const {
  888. if (custom_data_layers_by_name.has(p_value)) {
  889. return custom_data_layers_by_name[p_value];
  890. } else {
  891. return -1;
  892. }
  893. }
  894. void TileSet::set_custom_data_layer_name(int p_layer_id, String p_value) {
  895. ERR_FAIL_INDEX(p_layer_id, custom_data_layers.size());
  896. // Exit if another property has the same name.
  897. if (!p_value.is_empty()) {
  898. for (int other_layer_id = 0; other_layer_id < get_custom_data_layers_count(); other_layer_id++) {
  899. if (other_layer_id != p_layer_id && get_custom_data_layer_name(other_layer_id) == p_value) {
  900. ERR_FAIL_MSG(vformat("There is already a custom property named %s", p_value));
  901. }
  902. }
  903. }
  904. if (p_value.is_empty() && custom_data_layers_by_name.has(p_value)) {
  905. custom_data_layers_by_name.erase(p_value);
  906. } else {
  907. custom_data_layers_by_name[p_value] = p_layer_id;
  908. }
  909. custom_data_layers.write[p_layer_id].name = p_value;
  910. emit_changed();
  911. }
  912. String TileSet::get_custom_data_layer_name(int p_layer_id) const {
  913. ERR_FAIL_INDEX_V(p_layer_id, custom_data_layers.size(), "");
  914. return custom_data_layers[p_layer_id].name;
  915. }
  916. void TileSet::set_custom_data_layer_type(int p_layer_id, Variant::Type p_value) {
  917. ERR_FAIL_INDEX(p_layer_id, custom_data_layers.size());
  918. custom_data_layers.write[p_layer_id].type = p_value;
  919. for (KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  920. E_source.value->notify_tile_data_properties_should_change();
  921. }
  922. emit_changed();
  923. }
  924. Variant::Type TileSet::get_custom_data_layer_type(int p_layer_id) const {
  925. ERR_FAIL_INDEX_V(p_layer_id, custom_data_layers.size(), Variant::NIL);
  926. return custom_data_layers[p_layer_id].type;
  927. }
  928. void TileSet::set_source_level_tile_proxy(int p_source_from, int p_source_to) {
  929. ERR_FAIL_COND(p_source_from == TileSet::INVALID_SOURCE || p_source_to == TileSet::INVALID_SOURCE);
  930. source_level_proxies[p_source_from] = p_source_to;
  931. emit_changed();
  932. }
  933. int TileSet::get_source_level_tile_proxy(int p_source_from) {
  934. ERR_FAIL_COND_V(!source_level_proxies.has(p_source_from), TileSet::INVALID_SOURCE);
  935. return source_level_proxies[p_source_from];
  936. }
  937. bool TileSet::has_source_level_tile_proxy(int p_source_from) {
  938. return source_level_proxies.has(p_source_from);
  939. }
  940. void TileSet::remove_source_level_tile_proxy(int p_source_from) {
  941. ERR_FAIL_COND(!source_level_proxies.has(p_source_from));
  942. source_level_proxies.erase(p_source_from);
  943. emit_changed();
  944. }
  945. void TileSet::set_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_source_to, Vector2i p_coords_to) {
  946. ERR_FAIL_COND(p_source_from == TileSet::INVALID_SOURCE || p_source_to == TileSet::INVALID_SOURCE);
  947. ERR_FAIL_COND(p_coords_from == TileSetSource::INVALID_ATLAS_COORDS || p_coords_to == TileSetSource::INVALID_ATLAS_COORDS);
  948. Array from;
  949. from.push_back(p_source_from);
  950. from.push_back(p_coords_from);
  951. Array to;
  952. to.push_back(p_source_to);
  953. to.push_back(p_coords_to);
  954. coords_level_proxies[from] = to;
  955. emit_changed();
  956. }
  957. Array TileSet::get_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from) {
  958. Array from;
  959. from.push_back(p_source_from);
  960. from.push_back(p_coords_from);
  961. ERR_FAIL_COND_V(!coords_level_proxies.has(from), Array());
  962. return coords_level_proxies[from];
  963. }
  964. bool TileSet::has_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from) {
  965. Array from;
  966. from.push_back(p_source_from);
  967. from.push_back(p_coords_from);
  968. return coords_level_proxies.has(from);
  969. }
  970. void TileSet::remove_coords_level_tile_proxy(int p_source_from, Vector2i p_coords_from) {
  971. Array from;
  972. from.push_back(p_source_from);
  973. from.push_back(p_coords_from);
  974. ERR_FAIL_COND(!coords_level_proxies.has(from));
  975. coords_level_proxies.erase(from);
  976. emit_changed();
  977. }
  978. 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) {
  979. ERR_FAIL_COND(p_source_from == TileSet::INVALID_SOURCE || p_source_to == TileSet::INVALID_SOURCE);
  980. ERR_FAIL_COND(p_coords_from == TileSetSource::INVALID_ATLAS_COORDS || p_coords_to == TileSetSource::INVALID_ATLAS_COORDS);
  981. Array from;
  982. from.push_back(p_source_from);
  983. from.push_back(p_coords_from);
  984. from.push_back(p_alternative_from);
  985. Array to;
  986. to.push_back(p_source_to);
  987. to.push_back(p_coords_to);
  988. to.push_back(p_alternative_to);
  989. alternative_level_proxies[from] = to;
  990. emit_changed();
  991. }
  992. Array TileSet::get_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) {
  993. Array from;
  994. from.push_back(p_source_from);
  995. from.push_back(p_coords_from);
  996. from.push_back(p_alternative_from);
  997. ERR_FAIL_COND_V(!alternative_level_proxies.has(from), Array());
  998. return alternative_level_proxies[from];
  999. }
  1000. bool TileSet::has_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) {
  1001. Array from;
  1002. from.push_back(p_source_from);
  1003. from.push_back(p_coords_from);
  1004. from.push_back(p_alternative_from);
  1005. return alternative_level_proxies.has(from);
  1006. }
  1007. void TileSet::remove_alternative_level_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) {
  1008. Array from;
  1009. from.push_back(p_source_from);
  1010. from.push_back(p_coords_from);
  1011. from.push_back(p_alternative_from);
  1012. ERR_FAIL_COND(!alternative_level_proxies.has(from));
  1013. alternative_level_proxies.erase(from);
  1014. emit_changed();
  1015. }
  1016. Array TileSet::get_source_level_tile_proxies() const {
  1017. Array output;
  1018. for (const KeyValue<int, int> &E : source_level_proxies) {
  1019. Array proxy;
  1020. proxy.push_back(E.key);
  1021. proxy.push_back(E.value);
  1022. output.push_back(proxy);
  1023. }
  1024. return output;
  1025. }
  1026. Array TileSet::get_coords_level_tile_proxies() const {
  1027. Array output;
  1028. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  1029. Array proxy;
  1030. proxy.append_array(E.key);
  1031. proxy.append_array(E.value);
  1032. output.push_back(proxy);
  1033. }
  1034. return output;
  1035. }
  1036. Array TileSet::get_alternative_level_tile_proxies() const {
  1037. Array output;
  1038. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  1039. Array proxy;
  1040. proxy.append_array(E.key);
  1041. proxy.append_array(E.value);
  1042. output.push_back(proxy);
  1043. }
  1044. return output;
  1045. }
  1046. Array TileSet::map_tile_proxy(int p_source_from, Vector2i p_coords_from, int p_alternative_from) const {
  1047. Array from;
  1048. from.push_back(p_source_from);
  1049. from.push_back(p_coords_from);
  1050. from.push_back(p_alternative_from);
  1051. // Check if the tile is valid, and if so, don't map the tile and return the input.
  1052. if (has_source(p_source_from)) {
  1053. Ref<TileSetSource> source = get_source(p_source_from);
  1054. if (source->has_tile(p_coords_from) && source->has_alternative_tile(p_coords_from, p_alternative_from)) {
  1055. return from;
  1056. }
  1057. }
  1058. // Source, coords and alternative match.
  1059. if (alternative_level_proxies.has(from)) {
  1060. return alternative_level_proxies[from].duplicate();
  1061. }
  1062. // Source and coords match.
  1063. from.pop_back();
  1064. if (coords_level_proxies.has(from)) {
  1065. Array output = coords_level_proxies[from].duplicate();
  1066. output.push_back(p_alternative_from);
  1067. return output;
  1068. }
  1069. // Source matches.
  1070. if (source_level_proxies.has(p_source_from)) {
  1071. Array output;
  1072. output.push_back(source_level_proxies[p_source_from]);
  1073. output.push_back(p_coords_from);
  1074. output.push_back(p_alternative_from);
  1075. return output;
  1076. }
  1077. Array output;
  1078. output.push_back(p_source_from);
  1079. output.push_back(p_coords_from);
  1080. output.push_back(p_alternative_from);
  1081. return output;
  1082. }
  1083. void TileSet::cleanup_invalid_tile_proxies() {
  1084. // Source level.
  1085. Vector<int> source_to_remove;
  1086. for (const KeyValue<int, int> &E : source_level_proxies) {
  1087. if (has_source(E.key)) {
  1088. source_to_remove.append(E.key);
  1089. }
  1090. }
  1091. for (int i = 0; i < source_to_remove.size(); i++) {
  1092. remove_source_level_tile_proxy(source_to_remove[i]);
  1093. }
  1094. // Coords level.
  1095. Vector<Array> coords_to_remove;
  1096. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  1097. Array a = E.key;
  1098. if (has_source(a[0]) && get_source(a[0])->has_tile(a[1])) {
  1099. coords_to_remove.append(a);
  1100. }
  1101. }
  1102. for (int i = 0; i < coords_to_remove.size(); i++) {
  1103. Array a = coords_to_remove[i];
  1104. remove_coords_level_tile_proxy(a[0], a[1]);
  1105. }
  1106. // Alternative level.
  1107. Vector<Array> alternative_to_remove;
  1108. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  1109. Array a = E.key;
  1110. if (has_source(a[0]) && get_source(a[0])->has_tile(a[1]) && get_source(a[0])->has_alternative_tile(a[1], a[2])) {
  1111. alternative_to_remove.append(a);
  1112. }
  1113. }
  1114. for (int i = 0; i < alternative_to_remove.size(); i++) {
  1115. Array a = alternative_to_remove[i];
  1116. remove_alternative_level_tile_proxy(a[0], a[1], a[2]);
  1117. }
  1118. }
  1119. void TileSet::clear_tile_proxies() {
  1120. source_level_proxies.clear();
  1121. coords_level_proxies.clear();
  1122. alternative_level_proxies.clear();
  1123. emit_changed();
  1124. }
  1125. int TileSet::add_pattern(Ref<TileMapPattern> p_pattern, int p_index) {
  1126. ERR_FAIL_COND_V(!p_pattern.is_valid(), -1);
  1127. ERR_FAIL_COND_V_MSG(p_pattern->is_empty(), -1, "Cannot add an empty pattern to the TileSet.");
  1128. for (unsigned int i = 0; i < patterns.size(); i++) {
  1129. ERR_FAIL_COND_V_MSG(patterns[i] == p_pattern, -1, "TileSet has already this pattern.");
  1130. }
  1131. ERR_FAIL_COND_V(p_index > (int)patterns.size(), -1);
  1132. if (p_index < 0) {
  1133. p_index = patterns.size();
  1134. }
  1135. patterns.insert(p_index, p_pattern);
  1136. emit_changed();
  1137. return p_index;
  1138. }
  1139. Ref<TileMapPattern> TileSet::get_pattern(int p_index) {
  1140. ERR_FAIL_INDEX_V(p_index, (int)patterns.size(), Ref<TileMapPattern>());
  1141. return patterns[p_index];
  1142. }
  1143. void TileSet::remove_pattern(int p_index) {
  1144. ERR_FAIL_INDEX(p_index, (int)patterns.size());
  1145. patterns.remove_at(p_index);
  1146. emit_changed();
  1147. }
  1148. int TileSet::get_patterns_count() {
  1149. return patterns.size();
  1150. }
  1151. RBSet<TileSet::TerrainsPattern> TileSet::get_terrains_pattern_set(int p_terrain_set) {
  1152. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), RBSet<TileSet::TerrainsPattern>());
  1153. _update_terrains_cache();
  1154. RBSet<TileSet::TerrainsPattern> output;
  1155. for (KeyValue<TileSet::TerrainsPattern, RBSet<TileMapCell>> kv : per_terrain_pattern_tiles[p_terrain_set]) {
  1156. output.insert(kv.key);
  1157. }
  1158. return output;
  1159. }
  1160. RBSet<TileMapCell> TileSet::get_tiles_for_terrains_pattern(int p_terrain_set, TerrainsPattern p_terrain_tile_pattern) {
  1161. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), RBSet<TileMapCell>());
  1162. _update_terrains_cache();
  1163. return per_terrain_pattern_tiles[p_terrain_set][p_terrain_tile_pattern];
  1164. }
  1165. TileMapCell TileSet::get_random_tile_from_terrains_pattern(int p_terrain_set, TileSet::TerrainsPattern p_terrain_tile_pattern) {
  1166. ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), TileMapCell());
  1167. _update_terrains_cache();
  1168. // Count the sum of probabilities.
  1169. double sum = 0.0;
  1170. RBSet<TileMapCell> set = per_terrain_pattern_tiles[p_terrain_set][p_terrain_tile_pattern];
  1171. for (const TileMapCell &E : set) {
  1172. if (E.source_id >= 0) {
  1173. Ref<TileSetSource> source = sources[E.source_id];
  1174. Ref<TileSetAtlasSource> atlas_source = source;
  1175. if (atlas_source.is_valid()) {
  1176. TileData *tile_data = atlas_source->get_tile_data(E.get_atlas_coords(), E.alternative_tile);
  1177. sum += tile_data->get_probability();
  1178. } else {
  1179. sum += 1.0;
  1180. }
  1181. } else {
  1182. sum += 1.0;
  1183. }
  1184. }
  1185. // Generate a random number.
  1186. double count = 0.0;
  1187. double picked = Math::random(0.0, sum);
  1188. // Pick the tile.
  1189. for (const TileMapCell &E : set) {
  1190. if (E.source_id >= 0) {
  1191. Ref<TileSetSource> source = sources[E.source_id];
  1192. Ref<TileSetAtlasSource> atlas_source = source;
  1193. if (atlas_source.is_valid()) {
  1194. TileData *tile_data = atlas_source->get_tile_data(E.get_atlas_coords(), E.alternative_tile);
  1195. count += tile_data->get_probability();
  1196. } else {
  1197. count += 1.0;
  1198. }
  1199. } else {
  1200. count += 1.0;
  1201. }
  1202. if (count >= picked) {
  1203. return E;
  1204. }
  1205. }
  1206. ERR_FAIL_V(TileMapCell());
  1207. }
  1208. Vector<Vector2> TileSet::get_tile_shape_polygon() {
  1209. Vector<Vector2> points;
  1210. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1211. points.append(Vector2(-0.5, -0.5));
  1212. points.append(Vector2(0.5, -0.5));
  1213. points.append(Vector2(0.5, 0.5));
  1214. points.append(Vector2(-0.5, 0.5));
  1215. } else {
  1216. float overlap = 0.0;
  1217. switch (tile_shape) {
  1218. case TileSet::TILE_SHAPE_ISOMETRIC:
  1219. overlap = 0.5;
  1220. break;
  1221. case TileSet::TILE_SHAPE_HEXAGON:
  1222. overlap = 0.25;
  1223. break;
  1224. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1225. overlap = 0.0;
  1226. break;
  1227. default:
  1228. break;
  1229. }
  1230. points.append(Vector2(0.0, -0.5));
  1231. points.append(Vector2(-0.5, overlap - 0.5));
  1232. points.append(Vector2(-0.5, 0.5 - overlap));
  1233. points.append(Vector2(0.0, 0.5));
  1234. points.append(Vector2(0.5, 0.5 - overlap));
  1235. points.append(Vector2(0.5, overlap - 0.5));
  1236. if (get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL) {
  1237. for (int i = 0; i < points.size(); i++) {
  1238. points.write[i] = Vector2(points[i].y, points[i].x);
  1239. }
  1240. }
  1241. }
  1242. return points;
  1243. }
  1244. void TileSet::draw_tile_shape(CanvasItem *p_canvas_item, Transform2D p_transform, Color p_color, bool p_filled, Ref<Texture2D> p_texture) {
  1245. if (tile_meshes_dirty) {
  1246. Vector<Vector2> shape = get_tile_shape_polygon();
  1247. Vector<Vector2> uvs;
  1248. uvs.resize(shape.size());
  1249. for (int i = 0; i < shape.size(); i++) {
  1250. uvs.write[i] = shape[i] + Vector2(0.5, 0.5);
  1251. }
  1252. Vector<Color> colors;
  1253. colors.resize(shape.size());
  1254. colors.fill(Color(1.0, 1.0, 1.0, 1.0));
  1255. // Filled mesh.
  1256. tile_filled_mesh->clear_surfaces();
  1257. Array a;
  1258. a.resize(Mesh::ARRAY_MAX);
  1259. a[Mesh::ARRAY_VERTEX] = shape;
  1260. a[Mesh::ARRAY_TEX_UV] = uvs;
  1261. a[Mesh::ARRAY_COLOR] = colors;
  1262. a[Mesh::ARRAY_INDEX] = Geometry2D::triangulate_polygon(shape);
  1263. tile_filled_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  1264. // Lines mesh.
  1265. tile_lines_mesh->clear_surfaces();
  1266. a.clear();
  1267. a.resize(Mesh::ARRAY_MAX);
  1268. // Add the first point again when drawing lines.
  1269. shape.push_back(shape[0]);
  1270. colors.push_back(colors[0]);
  1271. a[Mesh::ARRAY_VERTEX] = shape;
  1272. a[Mesh::ARRAY_COLOR] = colors;
  1273. tile_lines_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINE_STRIP, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  1274. tile_meshes_dirty = false;
  1275. }
  1276. if (p_filled) {
  1277. p_canvas_item->draw_mesh(tile_filled_mesh, p_texture, p_transform, p_color);
  1278. } else {
  1279. p_canvas_item->draw_mesh(tile_lines_mesh, Ref<Texture2D>(), p_transform, p_color);
  1280. }
  1281. }
  1282. Vector<Point2> TileSet::get_terrain_polygon(int p_terrain_set) {
  1283. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1284. return _get_square_terrain_polygon(tile_size);
  1285. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1286. return _get_isometric_terrain_polygon(tile_size);
  1287. } else {
  1288. float overlap = 0.0;
  1289. switch (tile_shape) {
  1290. case TileSet::TILE_SHAPE_HEXAGON:
  1291. overlap = 0.25;
  1292. break;
  1293. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1294. overlap = 0.0;
  1295. break;
  1296. default:
  1297. break;
  1298. }
  1299. return _get_half_offset_terrain_polygon(tile_size, overlap, tile_offset_axis);
  1300. }
  1301. return Vector<Point2>();
  1302. }
  1303. Vector<Point2> TileSet::get_terrain_peering_bit_polygon(int p_terrain_set, TileSet::CellNeighbor p_bit) {
  1304. ERR_FAIL_COND_V(p_terrain_set < 0 || p_terrain_set >= get_terrain_sets_count(), Vector<Point2>());
  1305. TileSet::TerrainMode terrain_mode = get_terrain_set_mode(p_terrain_set);
  1306. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1307. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1308. return _get_square_corner_or_side_terrain_peering_bit_polygon(tile_size, p_bit);
  1309. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1310. return _get_square_corner_terrain_peering_bit_polygon(tile_size, p_bit);
  1311. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1312. return _get_square_side_terrain_peering_bit_polygon(tile_size, p_bit);
  1313. }
  1314. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1315. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1316. return _get_isometric_corner_or_side_terrain_peering_bit_polygon(tile_size, p_bit);
  1317. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1318. return _get_isometric_corner_terrain_peering_bit_polygon(tile_size, p_bit);
  1319. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1320. return _get_isometric_side_terrain_peering_bit_polygon(tile_size, p_bit);
  1321. }
  1322. } else {
  1323. float overlap = 0.0;
  1324. switch (tile_shape) {
  1325. case TileSet::TILE_SHAPE_HEXAGON:
  1326. overlap = 0.25;
  1327. break;
  1328. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1329. overlap = 0.0;
  1330. break;
  1331. default:
  1332. break;
  1333. }
  1334. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1335. return _get_half_offset_corner_or_side_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, p_bit);
  1336. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1337. return _get_half_offset_corner_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, p_bit);
  1338. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1339. return _get_half_offset_side_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, p_bit);
  1340. }
  1341. }
  1342. }
  1343. #define TERRAIN_ALPHA 0.6
  1344. void TileSet::draw_terrains(CanvasItem *p_canvas_item, Transform2D p_transform, const TileData *p_tile_data) {
  1345. ERR_FAIL_COND(!p_tile_data);
  1346. if (terrain_bits_meshes_dirty) {
  1347. // Recompute the meshes.
  1348. terrain_peering_bits_meshes.clear();
  1349. for (int terrain_mode_index = 0; terrain_mode_index < 3; terrain_mode_index++) {
  1350. TerrainMode terrain_mode = TerrainMode(terrain_mode_index);
  1351. // Center terrain
  1352. Vector<Vector2> polygon;
  1353. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1354. polygon = _get_square_terrain_polygon(tile_size);
  1355. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1356. polygon = _get_isometric_terrain_polygon(tile_size);
  1357. } else {
  1358. float overlap = 0.0;
  1359. switch (tile_shape) {
  1360. case TileSet::TILE_SHAPE_HEXAGON:
  1361. overlap = 0.25;
  1362. break;
  1363. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1364. overlap = 0.0;
  1365. break;
  1366. default:
  1367. break;
  1368. }
  1369. polygon = _get_half_offset_terrain_polygon(tile_size, overlap, tile_offset_axis);
  1370. }
  1371. {
  1372. Ref<ArrayMesh> mesh;
  1373. mesh.instantiate();
  1374. Vector<Vector2> uvs;
  1375. uvs.resize(polygon.size());
  1376. Vector<Color> colors;
  1377. colors.resize(polygon.size());
  1378. colors.fill(Color(1.0, 1.0, 1.0, 1.0));
  1379. Array a;
  1380. a.resize(Mesh::ARRAY_MAX);
  1381. a[Mesh::ARRAY_VERTEX] = polygon;
  1382. a[Mesh::ARRAY_TEX_UV] = uvs;
  1383. a[Mesh::ARRAY_COLOR] = colors;
  1384. a[Mesh::ARRAY_INDEX] = Geometry2D::triangulate_polygon(polygon);
  1385. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  1386. terrain_meshes[terrain_mode] = mesh;
  1387. }
  1388. // Peering bits
  1389. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1390. CellNeighbor bit = CellNeighbor(i);
  1391. if (is_valid_terrain_peering_bit_for_mode(terrain_mode, bit)) {
  1392. if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
  1393. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1394. polygon = _get_square_corner_or_side_terrain_peering_bit_polygon(tile_size, bit);
  1395. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1396. polygon = _get_square_corner_terrain_peering_bit_polygon(tile_size, bit);
  1397. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1398. polygon = _get_square_side_terrain_peering_bit_polygon(tile_size, bit);
  1399. }
  1400. } else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
  1401. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1402. polygon = _get_isometric_corner_or_side_terrain_peering_bit_polygon(tile_size, bit);
  1403. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1404. polygon = _get_isometric_corner_terrain_peering_bit_polygon(tile_size, bit);
  1405. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1406. polygon = _get_isometric_side_terrain_peering_bit_polygon(tile_size, bit);
  1407. }
  1408. } else {
  1409. float overlap = 0.0;
  1410. switch (tile_shape) {
  1411. case TileSet::TILE_SHAPE_HEXAGON:
  1412. overlap = 0.25;
  1413. break;
  1414. case TileSet::TILE_SHAPE_HALF_OFFSET_SQUARE:
  1415. overlap = 0.0;
  1416. break;
  1417. default:
  1418. break;
  1419. }
  1420. if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  1421. polygon = _get_half_offset_corner_or_side_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, bit);
  1422. } else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  1423. polygon = _get_half_offset_corner_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, bit);
  1424. } else { // TileData::TERRAIN_MODE_MATCH_SIDES
  1425. polygon = _get_half_offset_side_terrain_peering_bit_polygon(tile_size, overlap, tile_offset_axis, bit);
  1426. }
  1427. }
  1428. {
  1429. Ref<ArrayMesh> mesh;
  1430. mesh.instantiate();
  1431. Vector<Vector2> uvs;
  1432. uvs.resize(polygon.size());
  1433. Vector<Color> colors;
  1434. colors.resize(polygon.size());
  1435. colors.fill(Color(1.0, 1.0, 1.0, 1.0));
  1436. Array a;
  1437. a.resize(Mesh::ARRAY_MAX);
  1438. a[Mesh::ARRAY_VERTEX] = polygon;
  1439. a[Mesh::ARRAY_TEX_UV] = uvs;
  1440. a[Mesh::ARRAY_COLOR] = colors;
  1441. a[Mesh::ARRAY_INDEX] = Geometry2D::triangulate_polygon(polygon);
  1442. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a, Array(), Dictionary(), Mesh::ARRAY_FLAG_USE_2D_VERTICES);
  1443. terrain_peering_bits_meshes[terrain_mode][bit] = mesh;
  1444. }
  1445. }
  1446. }
  1447. }
  1448. terrain_bits_meshes_dirty = false;
  1449. }
  1450. int terrain_set = p_tile_data->get_terrain_set();
  1451. if (terrain_set < 0) {
  1452. return;
  1453. }
  1454. TileSet::TerrainMode terrain_mode = get_terrain_set_mode(terrain_set);
  1455. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), p_transform);
  1456. int terrain_id = p_tile_data->get_terrain();
  1457. if (terrain_id >= 0) {
  1458. Color color = get_terrain_color(terrain_set, terrain_id);
  1459. color.a = TERRAIN_ALPHA;
  1460. p_canvas_item->draw_mesh(terrain_meshes[terrain_mode], Ref<Texture2D>(), Transform2D(), color);
  1461. }
  1462. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1463. CellNeighbor bit = CellNeighbor(i);
  1464. if (is_valid_terrain_peering_bit(terrain_set, bit)) {
  1465. terrain_id = p_tile_data->get_terrain_peering_bit(bit);
  1466. if (terrain_id >= 0) {
  1467. Color color = get_terrain_color(terrain_set, terrain_id);
  1468. color.a = TERRAIN_ALPHA;
  1469. p_canvas_item->draw_mesh(terrain_peering_bits_meshes[terrain_mode][bit], Ref<Texture2D>(), Transform2D(), color);
  1470. }
  1471. }
  1472. }
  1473. RenderingServer::get_singleton()->canvas_item_add_set_transform(p_canvas_item->get_canvas_item(), Transform2D());
  1474. }
  1475. Vector<Vector<Ref<Texture2D>>> TileSet::generate_terrains_icons(Size2i p_size) {
  1476. // Counts the number of matching terrain tiles and find the best matching icon.
  1477. struct Count {
  1478. int count = 0;
  1479. float probability = 0.0;
  1480. Ref<Texture2D> texture;
  1481. Rect2i region;
  1482. };
  1483. Vector<Vector<Ref<Texture2D>>> output;
  1484. LocalVector<LocalVector<Count>> counts;
  1485. output.resize(get_terrain_sets_count());
  1486. counts.resize(get_terrain_sets_count());
  1487. for (int terrain_set = 0; terrain_set < get_terrain_sets_count(); terrain_set++) {
  1488. output.write[terrain_set].resize(get_terrains_count(terrain_set));
  1489. counts[terrain_set].resize(get_terrains_count(terrain_set));
  1490. }
  1491. for (int source_index = 0; source_index < get_source_count(); source_index++) {
  1492. int source_id = get_source_id(source_index);
  1493. Ref<TileSetSource> source = get_source(source_id);
  1494. Ref<TileSetAtlasSource> atlas_source = source;
  1495. if (atlas_source.is_valid()) {
  1496. for (int tile_index = 0; tile_index < source->get_tiles_count(); tile_index++) {
  1497. Vector2i tile_id = source->get_tile_id(tile_index);
  1498. for (int alternative_index = 0; alternative_index < source->get_alternative_tiles_count(tile_id); alternative_index++) {
  1499. int alternative_id = source->get_alternative_tile_id(tile_id, alternative_index);
  1500. TileData *tile_data = atlas_source->get_tile_data(tile_id, alternative_id);
  1501. int terrain_set = tile_data->get_terrain_set();
  1502. if (terrain_set >= 0) {
  1503. ERR_FAIL_INDEX_V(terrain_set, get_terrain_sets_count(), Vector<Vector<Ref<Texture2D>>>());
  1504. LocalVector<int> bit_counts;
  1505. bit_counts.resize(get_terrains_count(terrain_set));
  1506. for (int terrain = 0; terrain < get_terrains_count(terrain_set); terrain++) {
  1507. bit_counts[terrain] = 0;
  1508. }
  1509. if (tile_data->get_terrain() >= 0) {
  1510. bit_counts[tile_data->get_terrain()] += 10;
  1511. }
  1512. for (int terrain_bit = 0; terrain_bit < TileSet::CELL_NEIGHBOR_MAX; terrain_bit++) {
  1513. TileSet::CellNeighbor cell_neighbor = TileSet::CellNeighbor(terrain_bit);
  1514. if (is_valid_terrain_peering_bit(terrain_set, cell_neighbor)) {
  1515. int terrain = tile_data->get_terrain_peering_bit(cell_neighbor);
  1516. if (terrain >= 0) {
  1517. if (terrain >= (int)bit_counts.size()) {
  1518. WARN_PRINT(vformat("Invalid terrain peering bit: %d", terrain));
  1519. } else {
  1520. bit_counts[terrain] += 1;
  1521. }
  1522. }
  1523. }
  1524. }
  1525. for (int terrain = 0; terrain < get_terrains_count(terrain_set); terrain++) {
  1526. 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)) {
  1527. counts[terrain_set][terrain].count = bit_counts[terrain];
  1528. counts[terrain_set][terrain].probability = tile_data->get_probability();
  1529. counts[terrain_set][terrain].texture = atlas_source->get_texture();
  1530. counts[terrain_set][terrain].region = atlas_source->get_tile_texture_region(tile_id);
  1531. }
  1532. }
  1533. }
  1534. }
  1535. }
  1536. }
  1537. }
  1538. // Generate the icons.
  1539. for (int terrain_set = 0; terrain_set < get_terrain_sets_count(); terrain_set++) {
  1540. for (int terrain = 0; terrain < get_terrains_count(terrain_set); terrain++) {
  1541. Ref<Image> image;
  1542. image.instantiate();
  1543. if (counts[terrain_set][terrain].count > 0) {
  1544. // Get the best tile.
  1545. Ref<Texture2D> texture = counts[terrain_set][terrain].texture;
  1546. Rect2i region = counts[terrain_set][terrain].region;
  1547. image->create(region.size.x, region.size.y, false, Image::FORMAT_RGBA8);
  1548. image->blit_rect(texture->get_image(), region, Point2i());
  1549. image->resize(p_size.x, p_size.y, Image::INTERPOLATE_NEAREST);
  1550. } else {
  1551. image->create(1, 1, false, Image::FORMAT_RGBA8);
  1552. image->set_pixel(0, 0, get_terrain_color(terrain_set, terrain));
  1553. }
  1554. Ref<ImageTexture> icon = ImageTexture::create_from_image(image);
  1555. icon->set_size_override(p_size);
  1556. output.write[terrain_set].write[terrain] = icon;
  1557. }
  1558. }
  1559. return output;
  1560. }
  1561. void TileSet::_source_changed() {
  1562. terrains_cache_dirty = true;
  1563. emit_changed();
  1564. }
  1565. Vector<Point2> TileSet::_get_square_terrain_polygon(Vector2i p_size) {
  1566. Rect2 rect(-Vector2(p_size) / 6.0, Vector2(p_size) / 3.0);
  1567. return {
  1568. rect.position,
  1569. Vector2(rect.get_end().x, rect.position.y),
  1570. rect.get_end(),
  1571. Vector2(rect.position.x, rect.get_end().y)
  1572. };
  1573. }
  1574. Vector<Point2> TileSet::_get_square_corner_or_side_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1575. Rect2 bit_rect;
  1576. bit_rect.size = Vector2(p_size) / 3;
  1577. switch (p_bit) {
  1578. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  1579. bit_rect.position = Vector2(1, -1);
  1580. break;
  1581. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1582. bit_rect.position = Vector2(1, 1);
  1583. break;
  1584. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  1585. bit_rect.position = Vector2(-1, 1);
  1586. break;
  1587. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1588. bit_rect.position = Vector2(-3, 1);
  1589. break;
  1590. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  1591. bit_rect.position = Vector2(-3, -1);
  1592. break;
  1593. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1594. bit_rect.position = Vector2(-3, -3);
  1595. break;
  1596. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  1597. bit_rect.position = Vector2(-1, -3);
  1598. break;
  1599. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1600. bit_rect.position = Vector2(1, -3);
  1601. break;
  1602. default:
  1603. break;
  1604. }
  1605. bit_rect.position *= Vector2(p_size) / 6.0;
  1606. Vector<Vector2> polygon = {
  1607. bit_rect.position,
  1608. Vector2(bit_rect.get_end().x, bit_rect.position.y),
  1609. bit_rect.get_end(),
  1610. Vector2(bit_rect.position.x, bit_rect.get_end().y)
  1611. };
  1612. return polygon;
  1613. }
  1614. Vector<Point2> TileSet::_get_square_corner_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1615. Vector2 unit = Vector2(p_size) / 6.0;
  1616. Vector<Vector2> polygon;
  1617. switch (p_bit) {
  1618. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1619. polygon.push_back(Vector2(0, 3) * unit);
  1620. polygon.push_back(Vector2(3, 3) * unit);
  1621. polygon.push_back(Vector2(3, 0) * unit);
  1622. polygon.push_back(Vector2(1, 0) * unit);
  1623. polygon.push_back(Vector2(1, 1) * unit);
  1624. polygon.push_back(Vector2(0, 1) * unit);
  1625. break;
  1626. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1627. polygon.push_back(Vector2(0, 3) * unit);
  1628. polygon.push_back(Vector2(-3, 3) * unit);
  1629. polygon.push_back(Vector2(-3, 0) * unit);
  1630. polygon.push_back(Vector2(-1, 0) * unit);
  1631. polygon.push_back(Vector2(-1, 1) * unit);
  1632. polygon.push_back(Vector2(0, 1) * unit);
  1633. break;
  1634. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1635. polygon.push_back(Vector2(0, -3) * unit);
  1636. polygon.push_back(Vector2(-3, -3) * unit);
  1637. polygon.push_back(Vector2(-3, 0) * unit);
  1638. polygon.push_back(Vector2(-1, 0) * unit);
  1639. polygon.push_back(Vector2(-1, -1) * unit);
  1640. polygon.push_back(Vector2(0, -1) * unit);
  1641. break;
  1642. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1643. polygon.push_back(Vector2(0, -3) * unit);
  1644. polygon.push_back(Vector2(3, -3) * unit);
  1645. polygon.push_back(Vector2(3, 0) * unit);
  1646. polygon.push_back(Vector2(1, 0) * unit);
  1647. polygon.push_back(Vector2(1, -1) * unit);
  1648. polygon.push_back(Vector2(0, -1) * unit);
  1649. break;
  1650. default:
  1651. break;
  1652. }
  1653. return polygon;
  1654. }
  1655. Vector<Point2> TileSet::_get_square_side_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1656. Vector2 unit = Vector2(p_size) / 6.0;
  1657. Vector<Vector2> polygon;
  1658. switch (p_bit) {
  1659. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  1660. polygon.push_back(Vector2(1, -1) * unit);
  1661. polygon.push_back(Vector2(3, -3) * unit);
  1662. polygon.push_back(Vector2(3, 3) * unit);
  1663. polygon.push_back(Vector2(1, 1) * unit);
  1664. break;
  1665. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  1666. polygon.push_back(Vector2(-1, 1) * unit);
  1667. polygon.push_back(Vector2(-3, 3) * unit);
  1668. polygon.push_back(Vector2(3, 3) * unit);
  1669. polygon.push_back(Vector2(1, 1) * unit);
  1670. break;
  1671. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  1672. polygon.push_back(Vector2(-1, -1) * unit);
  1673. polygon.push_back(Vector2(-3, -3) * unit);
  1674. polygon.push_back(Vector2(-3, 3) * unit);
  1675. polygon.push_back(Vector2(-1, 1) * unit);
  1676. break;
  1677. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  1678. polygon.push_back(Vector2(-1, -1) * unit);
  1679. polygon.push_back(Vector2(-3, -3) * unit);
  1680. polygon.push_back(Vector2(3, -3) * unit);
  1681. polygon.push_back(Vector2(1, -1) * unit);
  1682. break;
  1683. default:
  1684. break;
  1685. }
  1686. return polygon;
  1687. }
  1688. Vector<Point2> TileSet::_get_isometric_terrain_polygon(Vector2i p_size) {
  1689. Vector2 unit = Vector2(p_size) / 6.0;
  1690. return {
  1691. Vector2(1, 0) * unit,
  1692. Vector2(0, 1) * unit,
  1693. Vector2(-1, 0) * unit,
  1694. Vector2(0, -1) * unit,
  1695. };
  1696. }
  1697. Vector<Point2> TileSet::_get_isometric_corner_or_side_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1698. Vector2 unit = Vector2(p_size) / 6.0;
  1699. Vector<Vector2> polygon;
  1700. switch (p_bit) {
  1701. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  1702. polygon.push_back(Vector2(1, 0) * unit);
  1703. polygon.push_back(Vector2(2, -1) * unit);
  1704. polygon.push_back(Vector2(3, 0) * unit);
  1705. polygon.push_back(Vector2(2, 1) * unit);
  1706. break;
  1707. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  1708. polygon.push_back(Vector2(0, 1) * unit);
  1709. polygon.push_back(Vector2(1, 2) * unit);
  1710. polygon.push_back(Vector2(2, 1) * unit);
  1711. polygon.push_back(Vector2(1, 0) * unit);
  1712. break;
  1713. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  1714. polygon.push_back(Vector2(0, 1) * unit);
  1715. polygon.push_back(Vector2(-1, 2) * unit);
  1716. polygon.push_back(Vector2(0, 3) * unit);
  1717. polygon.push_back(Vector2(1, 2) * unit);
  1718. break;
  1719. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  1720. polygon.push_back(Vector2(0, 1) * unit);
  1721. polygon.push_back(Vector2(-1, 2) * unit);
  1722. polygon.push_back(Vector2(-2, 1) * unit);
  1723. polygon.push_back(Vector2(-1, 0) * unit);
  1724. break;
  1725. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  1726. polygon.push_back(Vector2(-1, 0) * unit);
  1727. polygon.push_back(Vector2(-2, -1) * unit);
  1728. polygon.push_back(Vector2(-3, 0) * unit);
  1729. polygon.push_back(Vector2(-2, 1) * unit);
  1730. break;
  1731. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  1732. polygon.push_back(Vector2(0, -1) * unit);
  1733. polygon.push_back(Vector2(-1, -2) * unit);
  1734. polygon.push_back(Vector2(-2, -1) * unit);
  1735. polygon.push_back(Vector2(-1, 0) * unit);
  1736. break;
  1737. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  1738. polygon.push_back(Vector2(0, -1) * unit);
  1739. polygon.push_back(Vector2(-1, -2) * unit);
  1740. polygon.push_back(Vector2(0, -3) * unit);
  1741. polygon.push_back(Vector2(1, -2) * unit);
  1742. break;
  1743. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  1744. polygon.push_back(Vector2(0, -1) * unit);
  1745. polygon.push_back(Vector2(1, -2) * unit);
  1746. polygon.push_back(Vector2(2, -1) * unit);
  1747. polygon.push_back(Vector2(1, 0) * unit);
  1748. break;
  1749. default:
  1750. break;
  1751. }
  1752. return polygon;
  1753. }
  1754. Vector<Point2> TileSet::_get_isometric_corner_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1755. Vector2 unit = Vector2(p_size) / 6.0;
  1756. Vector<Vector2> polygon;
  1757. switch (p_bit) {
  1758. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  1759. polygon.push_back(Vector2(0.5, -0.5) * unit);
  1760. polygon.push_back(Vector2(1.5, -1.5) * unit);
  1761. polygon.push_back(Vector2(3, 0) * unit);
  1762. polygon.push_back(Vector2(1.5, 1.5) * unit);
  1763. polygon.push_back(Vector2(0.5, 0.5) * unit);
  1764. polygon.push_back(Vector2(1, 0) * unit);
  1765. break;
  1766. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  1767. polygon.push_back(Vector2(-0.5, 0.5) * unit);
  1768. polygon.push_back(Vector2(-1.5, 1.5) * unit);
  1769. polygon.push_back(Vector2(0, 3) * unit);
  1770. polygon.push_back(Vector2(1.5, 1.5) * unit);
  1771. polygon.push_back(Vector2(0.5, 0.5) * unit);
  1772. polygon.push_back(Vector2(0, 1) * unit);
  1773. break;
  1774. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  1775. polygon.push_back(Vector2(-0.5, -0.5) * unit);
  1776. polygon.push_back(Vector2(-1.5, -1.5) * unit);
  1777. polygon.push_back(Vector2(-3, 0) * unit);
  1778. polygon.push_back(Vector2(-1.5, 1.5) * unit);
  1779. polygon.push_back(Vector2(-0.5, 0.5) * unit);
  1780. polygon.push_back(Vector2(-1, 0) * unit);
  1781. break;
  1782. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  1783. polygon.push_back(Vector2(-0.5, -0.5) * unit);
  1784. polygon.push_back(Vector2(-1.5, -1.5) * unit);
  1785. polygon.push_back(Vector2(0, -3) * unit);
  1786. polygon.push_back(Vector2(1.5, -1.5) * unit);
  1787. polygon.push_back(Vector2(0.5, -0.5) * unit);
  1788. polygon.push_back(Vector2(0, -1) * unit);
  1789. break;
  1790. default:
  1791. break;
  1792. }
  1793. return polygon;
  1794. }
  1795. Vector<Point2> TileSet::_get_isometric_side_terrain_peering_bit_polygon(Vector2i p_size, TileSet::CellNeighbor p_bit) {
  1796. Vector2 unit = Vector2(p_size) / 6.0;
  1797. Vector<Vector2> polygon;
  1798. switch (p_bit) {
  1799. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  1800. polygon.push_back(Vector2(1, 0) * unit);
  1801. polygon.push_back(Vector2(3, 0) * unit);
  1802. polygon.push_back(Vector2(0, 3) * unit);
  1803. polygon.push_back(Vector2(0, 1) * unit);
  1804. break;
  1805. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  1806. polygon.push_back(Vector2(-1, 0) * unit);
  1807. polygon.push_back(Vector2(-3, 0) * unit);
  1808. polygon.push_back(Vector2(0, 3) * unit);
  1809. polygon.push_back(Vector2(0, 1) * unit);
  1810. break;
  1811. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  1812. polygon.push_back(Vector2(-1, 0) * unit);
  1813. polygon.push_back(Vector2(-3, 0) * unit);
  1814. polygon.push_back(Vector2(0, -3) * unit);
  1815. polygon.push_back(Vector2(0, -1) * unit);
  1816. break;
  1817. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  1818. polygon.push_back(Vector2(1, 0) * unit);
  1819. polygon.push_back(Vector2(3, 0) * unit);
  1820. polygon.push_back(Vector2(0, -3) * unit);
  1821. polygon.push_back(Vector2(0, -1) * unit);
  1822. break;
  1823. default:
  1824. break;
  1825. }
  1826. return polygon;
  1827. }
  1828. Vector<Point2> TileSet::_get_half_offset_terrain_polygon(Vector2i p_size, float p_overlap, TileSet::TileOffsetAxis p_offset_axis) {
  1829. Vector2 unit = Vector2(p_size) / 6.0;
  1830. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1831. return {
  1832. Vector2(1, 1.0 - p_overlap * 2.0) * unit,
  1833. Vector2(0, 1) * unit,
  1834. Vector2(-1, 1.0 - p_overlap * 2.0) * unit,
  1835. Vector2(-1, -1.0 + p_overlap * 2.0) * unit,
  1836. Vector2(0, -1) * unit,
  1837. Vector2(1, -1.0 + p_overlap * 2.0) * unit,
  1838. };
  1839. } else {
  1840. return {
  1841. Vector2(1, 0) * unit,
  1842. Vector2(1.0 - p_overlap * 2.0, -1) * unit,
  1843. Vector2(-1.0 + p_overlap * 2.0, -1) * unit,
  1844. Vector2(-1, 0) * unit,
  1845. Vector2(-1.0 + p_overlap * 2.0, 1) * unit,
  1846. Vector2(1.0 - p_overlap * 2.0, 1) * unit,
  1847. };
  1848. }
  1849. }
  1850. 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) {
  1851. Vector<Vector2> point_list = {
  1852. Vector2(3, (3.0 * (1.0 - p_overlap * 2.0)) / 2.0),
  1853. Vector2(3, 3.0 * (1.0 - p_overlap * 2.0)),
  1854. Vector2(2, 3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  1855. Vector2(1, 3.0 - p_overlap * 2.0),
  1856. Vector2(0, 3),
  1857. Vector2(-1, 3.0 - p_overlap * 2.0),
  1858. Vector2(-2, 3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  1859. Vector2(-3, 3.0 * (1.0 - p_overlap * 2.0)),
  1860. Vector2(-3, (3.0 * (1.0 - p_overlap * 2.0)) / 2.0),
  1861. Vector2(-3, -(3.0 * (1.0 - p_overlap * 2.0)) / 2.0),
  1862. Vector2(-3, -3.0 * (1.0 - p_overlap * 2.0)),
  1863. Vector2(-2, -3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  1864. Vector2(-1, -(3.0 - p_overlap * 2.0)),
  1865. Vector2(0, -3),
  1866. Vector2(1, -(3.0 - p_overlap * 2.0)),
  1867. Vector2(2, -3.0 * (1.0 - (p_overlap * 2.0) * 2.0 / 3.0)),
  1868. Vector2(3, -3.0 * (1.0 - p_overlap * 2.0)),
  1869. Vector2(3, -(3.0 * (1.0 - p_overlap * 2.0)) / 2.0)
  1870. };
  1871. Vector2 unit = Vector2(p_size) / 6.0;
  1872. Vector<Vector2> polygon;
  1873. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  1874. for (int i = 0; i < point_list.size(); i++) {
  1875. point_list.write[i] = point_list[i] * unit;
  1876. }
  1877. switch (p_bit) {
  1878. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  1879. polygon.push_back(point_list[17]);
  1880. polygon.push_back(point_list[0]);
  1881. break;
  1882. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1883. polygon.push_back(point_list[0]);
  1884. polygon.push_back(point_list[1]);
  1885. polygon.push_back(point_list[2]);
  1886. break;
  1887. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  1888. polygon.push_back(point_list[2]);
  1889. polygon.push_back(point_list[3]);
  1890. break;
  1891. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  1892. polygon.push_back(point_list[3]);
  1893. polygon.push_back(point_list[4]);
  1894. polygon.push_back(point_list[5]);
  1895. break;
  1896. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  1897. polygon.push_back(point_list[5]);
  1898. polygon.push_back(point_list[6]);
  1899. break;
  1900. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1901. polygon.push_back(point_list[6]);
  1902. polygon.push_back(point_list[7]);
  1903. polygon.push_back(point_list[8]);
  1904. break;
  1905. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  1906. polygon.push_back(point_list[8]);
  1907. polygon.push_back(point_list[9]);
  1908. break;
  1909. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1910. polygon.push_back(point_list[9]);
  1911. polygon.push_back(point_list[10]);
  1912. polygon.push_back(point_list[11]);
  1913. break;
  1914. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  1915. polygon.push_back(point_list[11]);
  1916. polygon.push_back(point_list[12]);
  1917. break;
  1918. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  1919. polygon.push_back(point_list[12]);
  1920. polygon.push_back(point_list[13]);
  1921. polygon.push_back(point_list[14]);
  1922. break;
  1923. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  1924. polygon.push_back(point_list[14]);
  1925. polygon.push_back(point_list[15]);
  1926. break;
  1927. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1928. polygon.push_back(point_list[15]);
  1929. polygon.push_back(point_list[16]);
  1930. polygon.push_back(point_list[17]);
  1931. break;
  1932. default:
  1933. break;
  1934. }
  1935. } else {
  1936. for (int i = 0; i < point_list.size(); i++) {
  1937. point_list.write[i] = Vector2(point_list[i].y, point_list[i].x) * unit;
  1938. }
  1939. switch (p_bit) {
  1940. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  1941. polygon.push_back(point_list[3]);
  1942. polygon.push_back(point_list[4]);
  1943. polygon.push_back(point_list[5]);
  1944. break;
  1945. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  1946. polygon.push_back(point_list[2]);
  1947. polygon.push_back(point_list[3]);
  1948. break;
  1949. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  1950. polygon.push_back(point_list[0]);
  1951. polygon.push_back(point_list[1]);
  1952. polygon.push_back(point_list[2]);
  1953. break;
  1954. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  1955. polygon.push_back(point_list[17]);
  1956. polygon.push_back(point_list[0]);
  1957. break;
  1958. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  1959. polygon.push_back(point_list[15]);
  1960. polygon.push_back(point_list[16]);
  1961. polygon.push_back(point_list[17]);
  1962. break;
  1963. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  1964. polygon.push_back(point_list[14]);
  1965. polygon.push_back(point_list[15]);
  1966. break;
  1967. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  1968. polygon.push_back(point_list[12]);
  1969. polygon.push_back(point_list[13]);
  1970. polygon.push_back(point_list[14]);
  1971. break;
  1972. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  1973. polygon.push_back(point_list[11]);
  1974. polygon.push_back(point_list[12]);
  1975. break;
  1976. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  1977. polygon.push_back(point_list[9]);
  1978. polygon.push_back(point_list[10]);
  1979. polygon.push_back(point_list[11]);
  1980. break;
  1981. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  1982. polygon.push_back(point_list[8]);
  1983. polygon.push_back(point_list[9]);
  1984. break;
  1985. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  1986. polygon.push_back(point_list[6]);
  1987. polygon.push_back(point_list[7]);
  1988. polygon.push_back(point_list[8]);
  1989. break;
  1990. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  1991. polygon.push_back(point_list[5]);
  1992. polygon.push_back(point_list[6]);
  1993. break;
  1994. default:
  1995. break;
  1996. }
  1997. }
  1998. int half_polygon_size = polygon.size();
  1999. for (int i = 0; i < half_polygon_size; i++) {
  2000. polygon.push_back(polygon[half_polygon_size - 1 - i] / 3.0);
  2001. }
  2002. return polygon;
  2003. }
  2004. 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) {
  2005. Vector<Vector2> point_list = {
  2006. Vector2(3, 0),
  2007. Vector2(3, 3.0 * (1.0 - p_overlap * 2.0)),
  2008. Vector2(1.5, (3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  2009. Vector2(0, 3),
  2010. Vector2(-1.5, (3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  2011. Vector2(-3, 3.0 * (1.0 - p_overlap * 2.0)),
  2012. Vector2(-3, 0),
  2013. Vector2(-3, -3.0 * (1.0 - p_overlap * 2.0)),
  2014. Vector2(-1.5, -(3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  2015. Vector2(0, -3),
  2016. Vector2(1.5, -(3.0 * (1.0 - p_overlap * 2.0) + 3.0) / 2.0),
  2017. Vector2(3, -3.0 * (1.0 - p_overlap * 2.0))
  2018. };
  2019. Vector2 unit = Vector2(p_size) / 6.0;
  2020. Vector<Vector2> polygon;
  2021. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2022. for (int i = 0; i < point_list.size(); i++) {
  2023. point_list.write[i] = point_list[i] * unit;
  2024. }
  2025. switch (p_bit) {
  2026. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2027. polygon.push_back(point_list[0]);
  2028. polygon.push_back(point_list[1]);
  2029. polygon.push_back(point_list[2]);
  2030. break;
  2031. case TileSet::CELL_NEIGHBOR_BOTTOM_CORNER:
  2032. polygon.push_back(point_list[2]);
  2033. polygon.push_back(point_list[3]);
  2034. polygon.push_back(point_list[4]);
  2035. break;
  2036. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2037. polygon.push_back(point_list[4]);
  2038. polygon.push_back(point_list[5]);
  2039. polygon.push_back(point_list[6]);
  2040. break;
  2041. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2042. polygon.push_back(point_list[6]);
  2043. polygon.push_back(point_list[7]);
  2044. polygon.push_back(point_list[8]);
  2045. break;
  2046. case TileSet::CELL_NEIGHBOR_TOP_CORNER:
  2047. polygon.push_back(point_list[8]);
  2048. polygon.push_back(point_list[9]);
  2049. polygon.push_back(point_list[10]);
  2050. break;
  2051. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2052. polygon.push_back(point_list[10]);
  2053. polygon.push_back(point_list[11]);
  2054. polygon.push_back(point_list[0]);
  2055. break;
  2056. default:
  2057. break;
  2058. }
  2059. } else {
  2060. for (int i = 0; i < point_list.size(); i++) {
  2061. point_list.write[i] = Vector2(point_list[i].y, point_list[i].x) * unit;
  2062. }
  2063. switch (p_bit) {
  2064. case TileSet::CELL_NEIGHBOR_RIGHT_CORNER:
  2065. polygon.push_back(point_list[2]);
  2066. polygon.push_back(point_list[3]);
  2067. polygon.push_back(point_list[4]);
  2068. break;
  2069. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER:
  2070. polygon.push_back(point_list[0]);
  2071. polygon.push_back(point_list[1]);
  2072. polygon.push_back(point_list[2]);
  2073. break;
  2074. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_CORNER:
  2075. polygon.push_back(point_list[10]);
  2076. polygon.push_back(point_list[11]);
  2077. polygon.push_back(point_list[0]);
  2078. break;
  2079. case TileSet::CELL_NEIGHBOR_LEFT_CORNER:
  2080. polygon.push_back(point_list[8]);
  2081. polygon.push_back(point_list[9]);
  2082. polygon.push_back(point_list[10]);
  2083. break;
  2084. case TileSet::CELL_NEIGHBOR_TOP_LEFT_CORNER:
  2085. polygon.push_back(point_list[6]);
  2086. polygon.push_back(point_list[7]);
  2087. polygon.push_back(point_list[8]);
  2088. break;
  2089. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_CORNER:
  2090. polygon.push_back(point_list[4]);
  2091. polygon.push_back(point_list[5]);
  2092. polygon.push_back(point_list[6]);
  2093. break;
  2094. default:
  2095. break;
  2096. }
  2097. }
  2098. int half_polygon_size = polygon.size();
  2099. for (int i = 0; i < half_polygon_size; i++) {
  2100. polygon.push_back(polygon[half_polygon_size - 1 - i] / 3.0);
  2101. }
  2102. return polygon;
  2103. }
  2104. 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) {
  2105. Vector<Vector2> point_list = {
  2106. Vector2(3, 3.0 * (1.0 - p_overlap * 2.0)),
  2107. Vector2(0, 3),
  2108. Vector2(-3, 3.0 * (1.0 - p_overlap * 2.0)),
  2109. Vector2(-3, -3.0 * (1.0 - p_overlap * 2.0)),
  2110. Vector2(0, -3),
  2111. Vector2(3, -3.0 * (1.0 - p_overlap * 2.0))
  2112. };
  2113. Vector2 unit = Vector2(p_size) / 6.0;
  2114. Vector<Vector2> polygon;
  2115. if (p_offset_axis == TileSet::TILE_OFFSET_AXIS_HORIZONTAL) {
  2116. for (int i = 0; i < point_list.size(); i++) {
  2117. point_list.write[i] = point_list[i] * unit;
  2118. }
  2119. switch (p_bit) {
  2120. case TileSet::CELL_NEIGHBOR_RIGHT_SIDE:
  2121. polygon.push_back(point_list[5]);
  2122. polygon.push_back(point_list[0]);
  2123. break;
  2124. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2125. polygon.push_back(point_list[0]);
  2126. polygon.push_back(point_list[1]);
  2127. break;
  2128. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2129. polygon.push_back(point_list[1]);
  2130. polygon.push_back(point_list[2]);
  2131. break;
  2132. case TileSet::CELL_NEIGHBOR_LEFT_SIDE:
  2133. polygon.push_back(point_list[2]);
  2134. polygon.push_back(point_list[3]);
  2135. break;
  2136. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2137. polygon.push_back(point_list[3]);
  2138. polygon.push_back(point_list[4]);
  2139. break;
  2140. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2141. polygon.push_back(point_list[4]);
  2142. polygon.push_back(point_list[5]);
  2143. break;
  2144. default:
  2145. break;
  2146. }
  2147. } else {
  2148. for (int i = 0; i < point_list.size(); i++) {
  2149. point_list.write[i] = Vector2(point_list[i].y, point_list[i].x) * unit;
  2150. }
  2151. switch (p_bit) {
  2152. case TileSet::CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE:
  2153. polygon.push_back(point_list[0]);
  2154. polygon.push_back(point_list[1]);
  2155. break;
  2156. case TileSet::CELL_NEIGHBOR_BOTTOM_SIDE:
  2157. polygon.push_back(point_list[5]);
  2158. polygon.push_back(point_list[0]);
  2159. break;
  2160. case TileSet::CELL_NEIGHBOR_BOTTOM_LEFT_SIDE:
  2161. polygon.push_back(point_list[4]);
  2162. polygon.push_back(point_list[5]);
  2163. break;
  2164. case TileSet::CELL_NEIGHBOR_TOP_LEFT_SIDE:
  2165. polygon.push_back(point_list[3]);
  2166. polygon.push_back(point_list[4]);
  2167. break;
  2168. case TileSet::CELL_NEIGHBOR_TOP_SIDE:
  2169. polygon.push_back(point_list[2]);
  2170. polygon.push_back(point_list[3]);
  2171. break;
  2172. case TileSet::CELL_NEIGHBOR_TOP_RIGHT_SIDE:
  2173. polygon.push_back(point_list[1]);
  2174. polygon.push_back(point_list[2]);
  2175. break;
  2176. default:
  2177. break;
  2178. }
  2179. }
  2180. int half_polygon_size = polygon.size();
  2181. for (int i = 0; i < half_polygon_size; i++) {
  2182. polygon.push_back(polygon[half_polygon_size - 1 - i] / 3.0);
  2183. }
  2184. return polygon;
  2185. }
  2186. void TileSet::reset_state() {
  2187. // Rendering
  2188. occlusion_layers.clear();
  2189. tile_lines_mesh.instantiate();
  2190. tile_filled_mesh.instantiate();
  2191. tile_meshes_dirty = true;
  2192. // Physics
  2193. physics_layers.clear();
  2194. // Terrains
  2195. terrain_sets.clear();
  2196. terrain_meshes.clear();
  2197. terrain_peering_bits_meshes.clear();
  2198. per_terrain_pattern_tiles.clear();
  2199. terrains_cache_dirty = true;
  2200. // Navigation
  2201. navigation_layers.clear();
  2202. custom_data_layers.clear();
  2203. custom_data_layers_by_name.clear();
  2204. // Proxies
  2205. source_level_proxies.clear();
  2206. coords_level_proxies.clear();
  2207. alternative_level_proxies.clear();
  2208. #ifndef DISABLE_DEPRECATED
  2209. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  2210. memdelete(E.value);
  2211. }
  2212. compatibility_data.clear();
  2213. #endif // DISABLE_DEPRECATED
  2214. while (!source_ids.is_empty()) {
  2215. remove_source(source_ids[0]);
  2216. }
  2217. }
  2218. const Vector2i TileSetSource::INVALID_ATLAS_COORDS = Vector2i(-1, -1);
  2219. const int TileSetSource::INVALID_TILE_ALTERNATIVE = -1;
  2220. #ifndef DISABLE_DEPRECATED
  2221. void TileSet::_compatibility_conversion() {
  2222. for (KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  2223. CompatibilityTileData *ctd = E.value;
  2224. // Add the texture
  2225. TileSetAtlasSource *atlas_source = memnew(TileSetAtlasSource);
  2226. int source_id = add_source(Ref<TileSetSource>(atlas_source));
  2227. atlas_source->set_texture(ctd->texture);
  2228. // Handle each tile as a new source. Not optimal but at least it should stay compatible.
  2229. switch (ctd->tile_mode) {
  2230. case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
  2231. atlas_source->set_margins(ctd->region.get_position());
  2232. atlas_source->set_texture_region_size(ctd->region.get_size());
  2233. Vector2i coords;
  2234. for (int flags = 0; flags < 8; flags++) {
  2235. bool flip_h = flags & 1;
  2236. bool flip_v = flags & 2;
  2237. bool transpose = flags & 4;
  2238. int alternative_tile = 0;
  2239. if (!atlas_source->has_tile(coords)) {
  2240. atlas_source->create_tile(coords);
  2241. } else {
  2242. alternative_tile = atlas_source->create_alternative_tile(coords);
  2243. }
  2244. // Add to the mapping.
  2245. Array key_array;
  2246. key_array.push_back(flip_h);
  2247. key_array.push_back(flip_v);
  2248. key_array.push_back(transpose);
  2249. Array value_array;
  2250. value_array.push_back(source_id);
  2251. value_array.push_back(coords);
  2252. value_array.push_back(alternative_tile);
  2253. if (!compatibility_tilemap_mapping.has(E.key)) {
  2254. compatibility_tilemap_mapping[E.key] = RBMap<Array, Array>();
  2255. }
  2256. compatibility_tilemap_mapping[E.key][key_array] = value_array;
  2257. compatibility_tilemap_mapping_tile_modes[E.key] = COMPATIBILITY_TILE_MODE_SINGLE_TILE;
  2258. TileData *tile_data = atlas_source->get_tile_data(coords, alternative_tile);
  2259. tile_data->set_flip_h(flip_h);
  2260. tile_data->set_flip_v(flip_v);
  2261. tile_data->set_transpose(transpose);
  2262. tile_data->set_material(ctd->material);
  2263. tile_data->set_modulate(ctd->modulate);
  2264. tile_data->set_z_index(ctd->z_index);
  2265. if (ctd->occluder.is_valid()) {
  2266. if (get_occlusion_layers_count() < 1) {
  2267. add_occlusion_layer();
  2268. }
  2269. tile_data->set_occluder(0, ctd->occluder);
  2270. }
  2271. if (ctd->navigation.is_valid()) {
  2272. if (get_navigation_layers_count() < 1) {
  2273. add_navigation_layer();
  2274. }
  2275. tile_data->set_navigation_polygon(0, ctd->autotile_navpoly_map[coords]);
  2276. }
  2277. tile_data->set_z_index(ctd->z_index);
  2278. // Add the shapes.
  2279. if (ctd->shapes.size() > 0) {
  2280. if (get_physics_layers_count() < 1) {
  2281. add_physics_layer();
  2282. }
  2283. }
  2284. for (int k = 0; k < ctd->shapes.size(); k++) {
  2285. CompatibilityShapeData csd = ctd->shapes[k];
  2286. if (csd.autotile_coords == coords) {
  2287. Ref<ConvexPolygonShape2D> convex_shape = csd.shape; // Only ConvexPolygonShape2D are supported, which is the default type used by the 3.x editor
  2288. if (convex_shape.is_valid()) {
  2289. Vector<Vector2> polygon = convex_shape->get_points();
  2290. for (int point_index = 0; point_index < polygon.size(); point_index++) {
  2291. polygon.write[point_index] = csd.transform.xform(polygon[point_index]);
  2292. }
  2293. tile_data->set_collision_polygons_count(0, tile_data->get_collision_polygons_count(0) + 1);
  2294. int index = tile_data->get_collision_polygons_count(0) - 1;
  2295. tile_data->set_collision_polygon_one_way(0, index, csd.one_way);
  2296. tile_data->set_collision_polygon_one_way_margin(0, index, csd.one_way_margin);
  2297. tile_data->set_collision_polygon_points(0, index, polygon);
  2298. }
  2299. }
  2300. }
  2301. }
  2302. } break;
  2303. case COMPATIBILITY_TILE_MODE_AUTO_TILE: {
  2304. // Not supported. It would need manual conversion.
  2305. } break;
  2306. case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
  2307. atlas_source->set_margins(ctd->region.get_position());
  2308. atlas_source->set_separation(Vector2i(ctd->autotile_spacing, ctd->autotile_spacing));
  2309. atlas_source->set_texture_region_size(ctd->autotile_tile_size);
  2310. Size2i atlas_size = ctd->region.get_size() / (ctd->autotile_tile_size + atlas_source->get_separation());
  2311. for (int i = 0; i < atlas_size.x; i++) {
  2312. for (int j = 0; j < atlas_size.y; j++) {
  2313. Vector2i coords = Vector2i(i, j);
  2314. for (int flags = 0; flags < 8; flags++) {
  2315. bool flip_h = flags & 1;
  2316. bool flip_v = flags & 2;
  2317. bool transpose = flags & 4;
  2318. int alternative_tile = 0;
  2319. if (!atlas_source->has_tile(coords)) {
  2320. atlas_source->create_tile(coords);
  2321. } else {
  2322. alternative_tile = atlas_source->create_alternative_tile(coords);
  2323. }
  2324. // Add to the mapping.
  2325. Array key_array;
  2326. key_array.push_back(coords);
  2327. key_array.push_back(flip_h);
  2328. key_array.push_back(flip_v);
  2329. key_array.push_back(transpose);
  2330. Array value_array;
  2331. value_array.push_back(source_id);
  2332. value_array.push_back(coords);
  2333. value_array.push_back(alternative_tile);
  2334. if (!compatibility_tilemap_mapping.has(E.key)) {
  2335. compatibility_tilemap_mapping[E.key] = RBMap<Array, Array>();
  2336. }
  2337. compatibility_tilemap_mapping[E.key][key_array] = value_array;
  2338. compatibility_tilemap_mapping_tile_modes[E.key] = COMPATIBILITY_TILE_MODE_ATLAS_TILE;
  2339. TileData *tile_data = atlas_source->get_tile_data(coords, alternative_tile);
  2340. tile_data->set_flip_h(flip_h);
  2341. tile_data->set_flip_v(flip_v);
  2342. tile_data->set_transpose(transpose);
  2343. tile_data->set_material(ctd->material);
  2344. tile_data->set_modulate(ctd->modulate);
  2345. tile_data->set_z_index(ctd->z_index);
  2346. if (ctd->autotile_occluder_map.has(coords)) {
  2347. if (get_occlusion_layers_count() < 1) {
  2348. add_occlusion_layer();
  2349. }
  2350. tile_data->set_occluder(0, ctd->autotile_occluder_map[coords]);
  2351. }
  2352. if (ctd->autotile_navpoly_map.has(coords)) {
  2353. if (get_navigation_layers_count() < 1) {
  2354. add_navigation_layer();
  2355. }
  2356. tile_data->set_navigation_polygon(0, ctd->autotile_navpoly_map[coords]);
  2357. }
  2358. if (ctd->autotile_priority_map.has(coords)) {
  2359. tile_data->set_probability(ctd->autotile_priority_map[coords]);
  2360. }
  2361. if (ctd->autotile_z_index_map.has(coords)) {
  2362. tile_data->set_z_index(ctd->autotile_z_index_map[coords]);
  2363. }
  2364. // Add the shapes.
  2365. if (ctd->shapes.size() > 0) {
  2366. if (get_physics_layers_count() < 1) {
  2367. add_physics_layer();
  2368. }
  2369. }
  2370. for (int k = 0; k < ctd->shapes.size(); k++) {
  2371. CompatibilityShapeData csd = ctd->shapes[k];
  2372. if (csd.autotile_coords == coords) {
  2373. Ref<ConvexPolygonShape2D> convex_shape = csd.shape; // Only ConvexPolygonShape2D are supported, which is the default type used by the 3.x editor
  2374. if (convex_shape.is_valid()) {
  2375. Vector<Vector2> polygon = convex_shape->get_points();
  2376. for (int point_index = 0; point_index < polygon.size(); point_index++) {
  2377. polygon.write[point_index] = csd.transform.xform(polygon[point_index]);
  2378. }
  2379. tile_data->set_collision_polygons_count(0, tile_data->get_collision_polygons_count(0) + 1);
  2380. int index = tile_data->get_collision_polygons_count(0) - 1;
  2381. tile_data->set_collision_polygon_one_way(0, index, csd.one_way);
  2382. tile_data->set_collision_polygon_one_way_margin(0, index, csd.one_way_margin);
  2383. tile_data->set_collision_polygon_points(0, index, polygon);
  2384. }
  2385. }
  2386. }
  2387. // -- TODO: handle --
  2388. // Those are offset for the whole atlas, they are likely useless for the atlases, but might make sense for single tiles.
  2389. // texture offset
  2390. // occluder_offset
  2391. // navigation_offset
  2392. // For terrains, ignored for now?
  2393. // bitmask_mode
  2394. // bitmask_flags
  2395. }
  2396. }
  2397. }
  2398. } break;
  2399. }
  2400. // Offset all shapes
  2401. for (int k = 0; k < ctd->shapes.size(); k++) {
  2402. Ref<ConvexPolygonShape2D> convex = ctd->shapes[k].shape;
  2403. if (convex.is_valid()) {
  2404. Vector<Vector2> points = convex->get_points();
  2405. for (int i_point = 0; i_point < points.size(); i_point++) {
  2406. points.write[i_point] = points[i_point] - get_tile_size() / 2;
  2407. }
  2408. convex->set_points(points);
  2409. }
  2410. }
  2411. }
  2412. // Reset compatibility data
  2413. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  2414. memdelete(E.value);
  2415. }
  2416. compatibility_data = HashMap<int, CompatibilityTileData *>();
  2417. }
  2418. Array TileSet::compatibility_tilemap_map(int p_tile_id, Vector2i p_coords, bool p_flip_h, bool p_flip_v, bool p_transpose) {
  2419. Array cannot_convert_array;
  2420. cannot_convert_array.push_back(TileSet::INVALID_SOURCE);
  2421. cannot_convert_array.push_back(TileSetAtlasSource::INVALID_ATLAS_COORDS);
  2422. cannot_convert_array.push_back(TileSetAtlasSource::INVALID_TILE_ALTERNATIVE);
  2423. if (!compatibility_tilemap_mapping.has(p_tile_id)) {
  2424. return cannot_convert_array;
  2425. }
  2426. int tile_mode = compatibility_tilemap_mapping_tile_modes[p_tile_id];
  2427. switch (tile_mode) {
  2428. case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
  2429. Array a;
  2430. a.push_back(p_flip_h);
  2431. a.push_back(p_flip_v);
  2432. a.push_back(p_transpose);
  2433. return compatibility_tilemap_mapping[p_tile_id][a];
  2434. }
  2435. case COMPATIBILITY_TILE_MODE_AUTO_TILE:
  2436. return cannot_convert_array;
  2437. break;
  2438. case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
  2439. Array a;
  2440. a.push_back(p_coords);
  2441. a.push_back(p_flip_h);
  2442. a.push_back(p_flip_v);
  2443. a.push_back(p_transpose);
  2444. return compatibility_tilemap_mapping[p_tile_id][a];
  2445. }
  2446. default:
  2447. return cannot_convert_array;
  2448. break;
  2449. }
  2450. };
  2451. #endif // DISABLE_DEPRECATED
  2452. bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
  2453. Vector<String> components = String(p_name).split("/", true, 2);
  2454. #ifndef DISABLE_DEPRECATED
  2455. // TODO: This should be moved to a dedicated conversion system (see #50691)
  2456. if (components.size() >= 1 && components[0].is_valid_int()) {
  2457. int id = components[0].to_int();
  2458. // Get or create the compatibility object
  2459. CompatibilityTileData *ctd;
  2460. HashMap<int, CompatibilityTileData *>::Iterator E = compatibility_data.find(id);
  2461. if (!E) {
  2462. ctd = memnew(CompatibilityTileData);
  2463. compatibility_data.insert(id, ctd);
  2464. } else {
  2465. ctd = E->value;
  2466. }
  2467. if (components.size() < 2) {
  2468. return false;
  2469. }
  2470. String what = components[1];
  2471. if (what == "name") {
  2472. ctd->name = p_value;
  2473. } else if (what == "texture") {
  2474. ctd->texture = p_value;
  2475. } else if (what == "tex_offset") {
  2476. ctd->tex_offset = p_value;
  2477. } else if (what == "material") {
  2478. ctd->material = p_value;
  2479. } else if (what == "modulate") {
  2480. ctd->modulate = p_value;
  2481. } else if (what == "region") {
  2482. ctd->region = p_value;
  2483. } else if (what == "tile_mode") {
  2484. ctd->tile_mode = p_value;
  2485. } else if (what.left(9) == "autotile") {
  2486. what = what.substr(9);
  2487. if (what == "bitmask_mode") {
  2488. ctd->autotile_bitmask_mode = p_value;
  2489. } else if (what == "icon_coordinate") {
  2490. ctd->autotile_icon_coordinate = p_value;
  2491. } else if (what == "tile_size") {
  2492. ctd->autotile_tile_size = p_value;
  2493. } else if (what == "spacing") {
  2494. ctd->autotile_spacing = p_value;
  2495. } else if (what == "bitmask_flags") {
  2496. if (p_value.is_array()) {
  2497. Array p = p_value;
  2498. Vector2i last_coord;
  2499. while (p.size() > 0) {
  2500. if (p[0].get_type() == Variant::VECTOR2) {
  2501. last_coord = p[0];
  2502. } else if (p[0].get_type() == Variant::INT) {
  2503. ctd->autotile_bitmask_flags.insert(last_coord, p[0]);
  2504. }
  2505. p.pop_front();
  2506. }
  2507. }
  2508. } else if (what == "occluder_map") {
  2509. Array p = p_value;
  2510. Vector2 last_coord;
  2511. while (p.size() > 0) {
  2512. if (p[0].get_type() == Variant::VECTOR2) {
  2513. last_coord = p[0];
  2514. } else if (p[0].get_type() == Variant::OBJECT) {
  2515. ctd->autotile_occluder_map.insert(last_coord, p[0]);
  2516. }
  2517. p.pop_front();
  2518. }
  2519. } else if (what == "navpoly_map") {
  2520. Array p = p_value;
  2521. Vector2 last_coord;
  2522. while (p.size() > 0) {
  2523. if (p[0].get_type() == Variant::VECTOR2) {
  2524. last_coord = p[0];
  2525. } else if (p[0].get_type() == Variant::OBJECT) {
  2526. ctd->autotile_navpoly_map.insert(last_coord, p[0]);
  2527. }
  2528. p.pop_front();
  2529. }
  2530. } else if (what == "priority_map") {
  2531. Array p = p_value;
  2532. Vector3 val;
  2533. Vector2 v;
  2534. int priority;
  2535. while (p.size() > 0) {
  2536. val = p[0];
  2537. if (val.z > 1) {
  2538. v.x = val.x;
  2539. v.y = val.y;
  2540. priority = (int)val.z;
  2541. ctd->autotile_priority_map.insert(v, priority);
  2542. }
  2543. p.pop_front();
  2544. }
  2545. } else if (what == "z_index_map") {
  2546. Array p = p_value;
  2547. Vector3 val;
  2548. Vector2 v;
  2549. int z_index;
  2550. while (p.size() > 0) {
  2551. val = p[0];
  2552. if (val.z != 0) {
  2553. v.x = val.x;
  2554. v.y = val.y;
  2555. z_index = (int)val.z;
  2556. ctd->autotile_z_index_map.insert(v, z_index);
  2557. }
  2558. p.pop_front();
  2559. }
  2560. }
  2561. } else if (what == "shapes") {
  2562. Array p = p_value;
  2563. for (int i = 0; i < p.size(); i++) {
  2564. CompatibilityShapeData csd;
  2565. Dictionary d = p[i];
  2566. for (int j = 0; j < d.size(); j++) {
  2567. String key = d.get_key_at_index(j);
  2568. if (key == "autotile_coord") {
  2569. csd.autotile_coords = d[key];
  2570. } else if (key == "one_way") {
  2571. csd.one_way = d[key];
  2572. } else if (key == "one_way_margin") {
  2573. csd.one_way_margin = d[key];
  2574. } else if (key == "shape") {
  2575. csd.shape = d[key];
  2576. } else if (key == "shape_transform") {
  2577. csd.transform = d[key];
  2578. }
  2579. }
  2580. ctd->shapes.push_back(csd);
  2581. }
  2582. /*
  2583. // IGNORED FOR NOW, they seem duplicated data compared to the shapes array
  2584. } else if (what == "shape") {
  2585. } else if (what == "shape_offset") {
  2586. } else if (what == "shape_transform") {
  2587. } else if (what == "shape_one_way") {
  2588. } else if (what == "shape_one_way_margin") {
  2589. }
  2590. // IGNORED FOR NOW, maybe useless ?
  2591. else if (what == "occluder_offset") {
  2592. // Not
  2593. } else if (what == "navigation_offset") {
  2594. }
  2595. */
  2596. } else if (what == "z_index") {
  2597. ctd->z_index = p_value;
  2598. // TODO: remove the conversion from here, it's not where it should be done (see #50691)
  2599. _compatibility_conversion();
  2600. } else {
  2601. return false;
  2602. }
  2603. } else {
  2604. #endif // DISABLE_DEPRECATED
  2605. // This is now a new property.
  2606. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  2607. // Occlusion layers.
  2608. int index = components[0].trim_prefix("occlusion_layer_").to_int();
  2609. ERR_FAIL_COND_V(index < 0, false);
  2610. if (components[1] == "light_mask") {
  2611. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2612. while (index >= occlusion_layers.size()) {
  2613. add_occlusion_layer();
  2614. }
  2615. set_occlusion_layer_light_mask(index, p_value);
  2616. return true;
  2617. } else if (components[1] == "sdf_collision") {
  2618. ERR_FAIL_COND_V(p_value.get_type() != Variant::BOOL, false);
  2619. while (index >= occlusion_layers.size()) {
  2620. add_occlusion_layer();
  2621. }
  2622. set_occlusion_layer_sdf_collision(index, p_value);
  2623. return true;
  2624. }
  2625. } else if (components.size() == 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  2626. // Physics layers.
  2627. int index = components[0].trim_prefix("physics_layer_").to_int();
  2628. ERR_FAIL_COND_V(index < 0, false);
  2629. if (components[1] == "collision_layer") {
  2630. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2631. while (index >= physics_layers.size()) {
  2632. add_physics_layer();
  2633. }
  2634. set_physics_layer_collision_layer(index, p_value);
  2635. return true;
  2636. } else if (components[1] == "collision_mask") {
  2637. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2638. while (index >= physics_layers.size()) {
  2639. add_physics_layer();
  2640. }
  2641. set_physics_layer_collision_mask(index, p_value);
  2642. return true;
  2643. } else if (components[1] == "physics_material") {
  2644. Ref<PhysicsMaterial> physics_material = p_value;
  2645. while (index >= physics_layers.size()) {
  2646. add_physics_layer();
  2647. }
  2648. set_physics_layer_physics_material(index, physics_material);
  2649. return true;
  2650. }
  2651. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int()) {
  2652. // Terrains.
  2653. int terrain_set_index = components[0].trim_prefix("terrain_set_").to_int();
  2654. ERR_FAIL_COND_V(terrain_set_index < 0, false);
  2655. if (components[1] == "mode") {
  2656. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2657. while (terrain_set_index >= terrain_sets.size()) {
  2658. add_terrain_set();
  2659. }
  2660. set_terrain_set_mode(terrain_set_index, TerrainMode(int(p_value)));
  2661. } else if (components.size() >= 3 && components[1].begins_with("terrain_") && components[1].trim_prefix("terrain_").is_valid_int()) {
  2662. int terrain_index = components[1].trim_prefix("terrain_").to_int();
  2663. ERR_FAIL_COND_V(terrain_index < 0, false);
  2664. if (components[2] == "name") {
  2665. ERR_FAIL_COND_V(p_value.get_type() != Variant::STRING, false);
  2666. while (terrain_set_index >= terrain_sets.size()) {
  2667. add_terrain_set();
  2668. }
  2669. while (terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  2670. add_terrain(terrain_set_index);
  2671. }
  2672. set_terrain_name(terrain_set_index, terrain_index, p_value);
  2673. return true;
  2674. } else if (components[2] == "color") {
  2675. ERR_FAIL_COND_V(p_value.get_type() != Variant::COLOR, false);
  2676. while (terrain_set_index >= terrain_sets.size()) {
  2677. add_terrain_set();
  2678. }
  2679. while (terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  2680. add_terrain(terrain_set_index);
  2681. }
  2682. set_terrain_color(terrain_set_index, terrain_index, p_value);
  2683. return true;
  2684. }
  2685. }
  2686. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  2687. // Navigation layers.
  2688. int index = components[0].trim_prefix("navigation_layer_").to_int();
  2689. ERR_FAIL_COND_V(index < 0, false);
  2690. if (components[1] == "layers") {
  2691. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2692. while (index >= navigation_layers.size()) {
  2693. add_navigation_layer();
  2694. }
  2695. set_navigation_layer_layers(index, p_value);
  2696. return true;
  2697. }
  2698. } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int()) {
  2699. // Custom data layers.
  2700. int index = components[0].trim_prefix("custom_data_layer_").to_int();
  2701. ERR_FAIL_COND_V(index < 0, false);
  2702. if (components[1] == "name") {
  2703. ERR_FAIL_COND_V(p_value.get_type() != Variant::STRING, false);
  2704. while (index >= custom_data_layers.size()) {
  2705. add_custom_data_layer();
  2706. }
  2707. set_custom_data_layer_name(index, p_value);
  2708. return true;
  2709. } else if (components[1] == "type") {
  2710. ERR_FAIL_COND_V(p_value.get_type() != Variant::INT, false);
  2711. while (index >= custom_data_layers.size()) {
  2712. add_custom_data_layer();
  2713. }
  2714. set_custom_data_layer_type(index, Variant::Type(int(p_value)));
  2715. return true;
  2716. }
  2717. } else if (components.size() == 2 && components[0] == "sources" && components[1].is_valid_int()) {
  2718. // Create source only if it does not exists.
  2719. int source_id = components[1].to_int();
  2720. if (!has_source(source_id)) {
  2721. add_source(p_value, source_id);
  2722. }
  2723. return true;
  2724. } else if (components.size() == 2 && components[0] == "tile_proxies") {
  2725. ERR_FAIL_COND_V(p_value.get_type() != Variant::ARRAY, false);
  2726. Array a = p_value;
  2727. ERR_FAIL_COND_V(a.size() % 2 != 0, false);
  2728. if (components[1] == "source_level") {
  2729. for (int i = 0; i < a.size(); i += 2) {
  2730. set_source_level_tile_proxy(a[i], a[i + 1]);
  2731. }
  2732. return true;
  2733. } else if (components[1] == "coords_level") {
  2734. for (int i = 0; i < a.size(); i += 2) {
  2735. Array key = a[i];
  2736. Array value = a[i + 1];
  2737. set_coords_level_tile_proxy(key[0], key[1], value[0], value[1]);
  2738. }
  2739. return true;
  2740. } else if (components[1] == "alternative_level") {
  2741. for (int i = 0; i < a.size(); i += 2) {
  2742. Array key = a[i];
  2743. Array value = a[i + 1];
  2744. set_alternative_level_tile_proxy(key[0], key[1], key[2], value[0], value[1], value[2]);
  2745. }
  2746. return true;
  2747. }
  2748. return false;
  2749. } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) {
  2750. int pattern_index = components[0].trim_prefix("pattern_").to_int();
  2751. for (int i = patterns.size(); i <= pattern_index; i++) {
  2752. add_pattern(p_value);
  2753. }
  2754. return true;
  2755. }
  2756. #ifndef DISABLE_DEPRECATED
  2757. }
  2758. #endif // DISABLE_DEPRECATED
  2759. return false;
  2760. }
  2761. bool TileSet::_get(const StringName &p_name, Variant &r_ret) const {
  2762. Vector<String> components = String(p_name).split("/", true, 2);
  2763. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  2764. // Occlusion layers.
  2765. int index = components[0].trim_prefix("occlusion_layer_").to_int();
  2766. if (index < 0 || index >= occlusion_layers.size()) {
  2767. return false;
  2768. }
  2769. if (components[1] == "light_mask") {
  2770. r_ret = get_occlusion_layer_light_mask(index);
  2771. return true;
  2772. } else if (components[1] == "sdf_collision") {
  2773. r_ret = get_occlusion_layer_sdf_collision(index);
  2774. return true;
  2775. }
  2776. } else if (components.size() == 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  2777. // Physics layers.
  2778. int index = components[0].trim_prefix("physics_layer_").to_int();
  2779. if (index < 0 || index >= physics_layers.size()) {
  2780. return false;
  2781. }
  2782. if (components[1] == "collision_layer") {
  2783. r_ret = get_physics_layer_collision_layer(index);
  2784. return true;
  2785. } else if (components[1] == "collision_mask") {
  2786. r_ret = get_physics_layer_collision_mask(index);
  2787. return true;
  2788. } else if (components[1] == "physics_material") {
  2789. r_ret = get_physics_layer_physics_material(index);
  2790. return true;
  2791. }
  2792. } else if (components.size() >= 2 && components[0].begins_with("terrain_set_") && components[0].trim_prefix("terrain_set_").is_valid_int()) {
  2793. // Terrains.
  2794. int terrain_set_index = components[0].trim_prefix("terrain_set_").to_int();
  2795. if (terrain_set_index < 0 || terrain_set_index >= terrain_sets.size()) {
  2796. return false;
  2797. }
  2798. if (components[1] == "mode") {
  2799. r_ret = get_terrain_set_mode(terrain_set_index);
  2800. return true;
  2801. } else if (components.size() >= 3 && components[1].begins_with("terrain_") && components[1].trim_prefix("terrain_").is_valid_int()) {
  2802. int terrain_index = components[1].trim_prefix("terrain_").to_int();
  2803. if (terrain_index < 0 || terrain_index >= terrain_sets[terrain_set_index].terrains.size()) {
  2804. return false;
  2805. }
  2806. if (components[2] == "name") {
  2807. r_ret = get_terrain_name(terrain_set_index, terrain_index);
  2808. return true;
  2809. } else if (components[2] == "color") {
  2810. r_ret = get_terrain_color(terrain_set_index, terrain_index);
  2811. return true;
  2812. }
  2813. }
  2814. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  2815. // navigation layers.
  2816. int index = components[0].trim_prefix("navigation_layer_").to_int();
  2817. if (index < 0 || index >= navigation_layers.size()) {
  2818. return false;
  2819. }
  2820. if (components[1] == "layers") {
  2821. r_ret = get_navigation_layer_layers(index);
  2822. return true;
  2823. }
  2824. } else if (components.size() == 2 && components[0].begins_with("custom_data_layer_") && components[0].trim_prefix("custom_data_layer_").is_valid_int()) {
  2825. // Custom data layers.
  2826. int index = components[0].trim_prefix("custom_data_layer_").to_int();
  2827. if (index < 0 || index >= custom_data_layers.size()) {
  2828. return false;
  2829. }
  2830. if (components[1] == "name") {
  2831. r_ret = get_custom_data_layer_name(index);
  2832. return true;
  2833. } else if (components[1] == "type") {
  2834. r_ret = get_custom_data_layer_type(index);
  2835. return true;
  2836. }
  2837. } else if (components.size() == 2 && components[0] == "sources" && components[1].is_valid_int()) {
  2838. // Atlases data.
  2839. int source_id = components[1].to_int();
  2840. if (has_source(source_id)) {
  2841. r_ret = get_source(source_id);
  2842. return true;
  2843. } else {
  2844. return false;
  2845. }
  2846. } else if (components.size() == 2 && components[0] == "tile_proxies") {
  2847. if (components[1] == "source_level") {
  2848. Array a;
  2849. for (const KeyValue<int, int> &E : source_level_proxies) {
  2850. a.push_back(E.key);
  2851. a.push_back(E.value);
  2852. }
  2853. r_ret = a;
  2854. return true;
  2855. } else if (components[1] == "coords_level") {
  2856. Array a;
  2857. for (const KeyValue<Array, Array> &E : coords_level_proxies) {
  2858. a.push_back(E.key);
  2859. a.push_back(E.value);
  2860. }
  2861. r_ret = a;
  2862. return true;
  2863. } else if (components[1] == "alternative_level") {
  2864. Array a;
  2865. for (const KeyValue<Array, Array> &E : alternative_level_proxies) {
  2866. a.push_back(E.key);
  2867. a.push_back(E.value);
  2868. }
  2869. r_ret = a;
  2870. return true;
  2871. }
  2872. return false;
  2873. } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) {
  2874. int pattern_index = components[0].trim_prefix("pattern_").to_int();
  2875. if (pattern_index < 0 || pattern_index >= (int)patterns.size()) {
  2876. return false;
  2877. }
  2878. r_ret = patterns[pattern_index];
  2879. return true;
  2880. }
  2881. return false;
  2882. }
  2883. void TileSet::_get_property_list(List<PropertyInfo> *p_list) const {
  2884. PropertyInfo property_info;
  2885. // Rendering.
  2886. p_list->push_back(PropertyInfo(Variant::NIL, "Rendering", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2887. for (int i = 0; i < occlusion_layers.size(); i++) {
  2888. p_list->push_back(PropertyInfo(Variant::INT, vformat("occlusion_layer_%d/light_mask", i), PROPERTY_HINT_LAYERS_2D_RENDER));
  2889. // occlusion_layer_%d/sdf_collision
  2890. property_info = PropertyInfo(Variant::BOOL, vformat("occlusion_layer_%d/sdf_collision", i));
  2891. if (occlusion_layers[i].sdf_collision == false) {
  2892. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  2893. }
  2894. p_list->push_back(property_info);
  2895. }
  2896. // Physics.
  2897. p_list->push_back(PropertyInfo(Variant::NIL, "Physics", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2898. for (int i = 0; i < physics_layers.size(); i++) {
  2899. p_list->push_back(PropertyInfo(Variant::INT, vformat("physics_layer_%d/collision_layer", i), PROPERTY_HINT_LAYERS_2D_PHYSICS));
  2900. // physics_layer_%d/collision_mask
  2901. property_info = PropertyInfo(Variant::INT, vformat("physics_layer_%d/collision_mask", i), PROPERTY_HINT_LAYERS_2D_PHYSICS);
  2902. if (physics_layers[i].collision_mask == 1) {
  2903. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  2904. }
  2905. p_list->push_back(property_info);
  2906. // physics_layer_%d/physics_material
  2907. property_info = PropertyInfo(Variant::OBJECT, vformat("physics_layer_%d/physics_material", i), PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial");
  2908. if (!physics_layers[i].physics_material.is_valid()) {
  2909. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  2910. }
  2911. p_list->push_back(property_info);
  2912. }
  2913. // Terrains.
  2914. p_list->push_back(PropertyInfo(Variant::NIL, "Terrains", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2915. for (int terrain_set_index = 0; terrain_set_index < terrain_sets.size(); terrain_set_index++) {
  2916. 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"));
  2917. 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)));
  2918. for (int terrain_index = 0; terrain_index < terrain_sets[terrain_set_index].terrains.size(); terrain_index++) {
  2919. p_list->push_back(PropertyInfo(Variant::STRING, vformat("terrain_set_%d/terrain_%d/name", terrain_set_index, terrain_index)));
  2920. p_list->push_back(PropertyInfo(Variant::COLOR, vformat("terrain_set_%d/terrain_%d/color", terrain_set_index, terrain_index)));
  2921. }
  2922. }
  2923. // Navigation.
  2924. p_list->push_back(PropertyInfo(Variant::NIL, "Navigation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2925. for (int i = 0; i < navigation_layers.size(); i++) {
  2926. p_list->push_back(PropertyInfo(Variant::INT, vformat("navigation_layer_%d/layers", i), PROPERTY_HINT_LAYERS_2D_NAVIGATION));
  2927. }
  2928. // Custom data.
  2929. String argt = "Any";
  2930. for (int i = 1; i < Variant::VARIANT_MAX; i++) {
  2931. argt += "," + Variant::get_type_name(Variant::Type(i));
  2932. }
  2933. p_list->push_back(PropertyInfo(Variant::NIL, "Custom data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2934. for (int i = 0; i < custom_data_layers.size(); i++) {
  2935. p_list->push_back(PropertyInfo(Variant::STRING, vformat("custom_data_layer_%d/name", i)));
  2936. p_list->push_back(PropertyInfo(Variant::INT, vformat("custom_data_layer_%d/type", i), PROPERTY_HINT_ENUM, argt));
  2937. }
  2938. // Sources.
  2939. // Note: sources have to be listed in at the end as some TileData rely on the TileSet properties being initialized first.
  2940. for (const KeyValue<int, Ref<TileSetSource>> &E_source : sources) {
  2941. p_list->push_back(PropertyInfo(Variant::INT, vformat("sources/%d", E_source.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2942. }
  2943. // Tile Proxies.
  2944. // Note: proxies need to be set after sources are set.
  2945. p_list->push_back(PropertyInfo(Variant::NIL, "Tile Proxies", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  2946. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/source_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2947. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/coords_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2948. p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/alternative_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  2949. // Patterns.
  2950. for (unsigned int pattern_index = 0; pattern_index < patterns.size(); pattern_index++) {
  2951. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("pattern_%d", pattern_index), PROPERTY_HINT_RESOURCE_TYPE, "TileMapPattern", PROPERTY_USAGE_NO_EDITOR));
  2952. }
  2953. }
  2954. void TileSet::_validate_property(PropertyInfo &p_property) const {
  2955. if (p_property.name == "tile_layout" && tile_shape == TILE_SHAPE_SQUARE) {
  2956. p_property.usage ^= PROPERTY_USAGE_READ_ONLY;
  2957. } else if (p_property.name == "tile_offset_axis" && tile_shape == TILE_SHAPE_SQUARE) {
  2958. p_property.usage ^= PROPERTY_USAGE_READ_ONLY;
  2959. }
  2960. }
  2961. void TileSet::_bind_methods() {
  2962. // Sources management.
  2963. ClassDB::bind_method(D_METHOD("get_next_source_id"), &TileSet::get_next_source_id);
  2964. ClassDB::bind_method(D_METHOD("add_source", "source", "atlas_source_id_override"), &TileSet::add_source, DEFVAL(TileSet::INVALID_SOURCE));
  2965. ClassDB::bind_method(D_METHOD("remove_source", "source_id"), &TileSet::remove_source);
  2966. ClassDB::bind_method(D_METHOD("set_source_id", "source_id", "new_source_id"), &TileSet::set_source_id);
  2967. ClassDB::bind_method(D_METHOD("get_source_count"), &TileSet::get_source_count);
  2968. ClassDB::bind_method(D_METHOD("get_source_id", "index"), &TileSet::get_source_id);
  2969. ClassDB::bind_method(D_METHOD("has_source", "source_id"), &TileSet::has_source);
  2970. ClassDB::bind_method(D_METHOD("get_source", "source_id"), &TileSet::get_source);
  2971. // Shape and layout.
  2972. ClassDB::bind_method(D_METHOD("set_tile_shape", "shape"), &TileSet::set_tile_shape);
  2973. ClassDB::bind_method(D_METHOD("get_tile_shape"), &TileSet::get_tile_shape);
  2974. ClassDB::bind_method(D_METHOD("set_tile_layout", "layout"), &TileSet::set_tile_layout);
  2975. ClassDB::bind_method(D_METHOD("get_tile_layout"), &TileSet::get_tile_layout);
  2976. ClassDB::bind_method(D_METHOD("set_tile_offset_axis", "alignment"), &TileSet::set_tile_offset_axis);
  2977. ClassDB::bind_method(D_METHOD("get_tile_offset_axis"), &TileSet::get_tile_offset_axis);
  2978. ClassDB::bind_method(D_METHOD("set_tile_size", "size"), &TileSet::set_tile_size);
  2979. ClassDB::bind_method(D_METHOD("get_tile_size"), &TileSet::get_tile_size);
  2980. ADD_PROPERTY(PropertyInfo(Variant::INT, "tile_shape", PROPERTY_HINT_ENUM, "Square,Isometric,Half-Offset Square,Hexagon"), "set_tile_shape", "get_tile_shape");
  2981. 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");
  2982. ADD_PROPERTY(PropertyInfo(Variant::INT, "tile_offset_axis", PROPERTY_HINT_ENUM, "Horizontal Offset,Vertical Offset"), "set_tile_offset_axis", "get_tile_offset_axis");
  2983. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "tile_size", PROPERTY_HINT_NONE, "suffix:px"), "set_tile_size", "get_tile_size");
  2984. // Rendering.
  2985. ClassDB::bind_method(D_METHOD("set_uv_clipping", "uv_clipping"), &TileSet::set_uv_clipping);
  2986. ClassDB::bind_method(D_METHOD("is_uv_clipping"), &TileSet::is_uv_clipping);
  2987. ClassDB::bind_method(D_METHOD("get_occlusion_layers_count"), &TileSet::get_occlusion_layers_count);
  2988. ClassDB::bind_method(D_METHOD("add_occlusion_layer", "to_position"), &TileSet::add_occlusion_layer, DEFVAL(-1));
  2989. ClassDB::bind_method(D_METHOD("move_occlusion_layer", "layer_index", "to_position"), &TileSet::move_occlusion_layer);
  2990. ClassDB::bind_method(D_METHOD("remove_occlusion_layer", "layer_index"), &TileSet::remove_occlusion_layer);
  2991. ClassDB::bind_method(D_METHOD("set_occlusion_layer_light_mask", "layer_index", "light_mask"), &TileSet::set_occlusion_layer_light_mask);
  2992. ClassDB::bind_method(D_METHOD("get_occlusion_layer_light_mask", "layer_index"), &TileSet::get_occlusion_layer_light_mask);
  2993. ClassDB::bind_method(D_METHOD("set_occlusion_layer_sdf_collision", "layer_index", "sdf_collision"), &TileSet::set_occlusion_layer_sdf_collision);
  2994. ClassDB::bind_method(D_METHOD("get_occlusion_layer_sdf_collision", "layer_index"), &TileSet::get_occlusion_layer_sdf_collision);
  2995. // Physics
  2996. ClassDB::bind_method(D_METHOD("get_physics_layers_count"), &TileSet::get_physics_layers_count);
  2997. ClassDB::bind_method(D_METHOD("add_physics_layer", "to_position"), &TileSet::add_physics_layer, DEFVAL(-1));
  2998. ClassDB::bind_method(D_METHOD("move_physics_layer", "layer_index", "to_position"), &TileSet::move_physics_layer);
  2999. ClassDB::bind_method(D_METHOD("remove_physics_layer", "layer_index"), &TileSet::remove_physics_layer);
  3000. ClassDB::bind_method(D_METHOD("set_physics_layer_collision_layer", "layer_index", "layer"), &TileSet::set_physics_layer_collision_layer);
  3001. ClassDB::bind_method(D_METHOD("get_physics_layer_collision_layer", "layer_index"), &TileSet::get_physics_layer_collision_layer);
  3002. ClassDB::bind_method(D_METHOD("set_physics_layer_collision_mask", "layer_index", "mask"), &TileSet::set_physics_layer_collision_mask);
  3003. ClassDB::bind_method(D_METHOD("get_physics_layer_collision_mask", "layer_index"), &TileSet::get_physics_layer_collision_mask);
  3004. ClassDB::bind_method(D_METHOD("set_physics_layer_physics_material", "layer_index", "physics_material"), &TileSet::set_physics_layer_physics_material);
  3005. ClassDB::bind_method(D_METHOD("get_physics_layer_physics_material", "layer_index"), &TileSet::get_physics_layer_physics_material);
  3006. // Terrains
  3007. ClassDB::bind_method(D_METHOD("get_terrain_sets_count"), &TileSet::get_terrain_sets_count);
  3008. ClassDB::bind_method(D_METHOD("add_terrain_set", "to_position"), &TileSet::add_terrain_set, DEFVAL(-1));
  3009. ClassDB::bind_method(D_METHOD("move_terrain_set", "terrain_set", "to_position"), &TileSet::move_terrain_set);
  3010. ClassDB::bind_method(D_METHOD("remove_terrain_set", "terrain_set"), &TileSet::remove_terrain_set);
  3011. ClassDB::bind_method(D_METHOD("set_terrain_set_mode", "terrain_set", "mode"), &TileSet::set_terrain_set_mode);
  3012. ClassDB::bind_method(D_METHOD("get_terrain_set_mode", "terrain_set"), &TileSet::get_terrain_set_mode);
  3013. ClassDB::bind_method(D_METHOD("get_terrains_count", "terrain_set"), &TileSet::get_terrains_count);
  3014. ClassDB::bind_method(D_METHOD("add_terrain", "terrain_set", "to_position"), &TileSet::add_terrain, DEFVAL(-1));
  3015. ClassDB::bind_method(D_METHOD("move_terrain", "terrain_set", "terrain_index", "to_position"), &TileSet::move_terrain);
  3016. ClassDB::bind_method(D_METHOD("remove_terrain", "terrain_set", "terrain_index"), &TileSet::remove_terrain);
  3017. ClassDB::bind_method(D_METHOD("set_terrain_name", "terrain_set", "terrain_index", "name"), &TileSet::set_terrain_name);
  3018. ClassDB::bind_method(D_METHOD("get_terrain_name", "terrain_set", "terrain_index"), &TileSet::get_terrain_name);
  3019. ClassDB::bind_method(D_METHOD("set_terrain_color", "terrain_set", "terrain_index", "color"), &TileSet::set_terrain_color);
  3020. ClassDB::bind_method(D_METHOD("get_terrain_color", "terrain_set", "terrain_index"), &TileSet::get_terrain_color);
  3021. // Navigation
  3022. ClassDB::bind_method(D_METHOD("get_navigation_layers_count"), &TileSet::get_navigation_layers_count);
  3023. ClassDB::bind_method(D_METHOD("add_navigation_layer", "to_position"), &TileSet::add_navigation_layer, DEFVAL(-1));
  3024. ClassDB::bind_method(D_METHOD("move_navigation_layer", "layer_index", "to_position"), &TileSet::move_navigation_layer);
  3025. ClassDB::bind_method(D_METHOD("remove_navigation_layer", "layer_index"), &TileSet::remove_navigation_layer);
  3026. ClassDB::bind_method(D_METHOD("set_navigation_layer_layers", "layer_index", "layers"), &TileSet::set_navigation_layer_layers);
  3027. ClassDB::bind_method(D_METHOD("get_navigation_layer_layers", "layer_index"), &TileSet::get_navigation_layer_layers);
  3028. // Custom data
  3029. ClassDB::bind_method(D_METHOD("get_custom_data_layers_count"), &TileSet::get_custom_data_layers_count);
  3030. ClassDB::bind_method(D_METHOD("add_custom_data_layer", "to_position"), &TileSet::add_custom_data_layer, DEFVAL(-1));
  3031. ClassDB::bind_method(D_METHOD("move_custom_data_layer", "layer_index", "to_position"), &TileSet::move_custom_data_layer);
  3032. ClassDB::bind_method(D_METHOD("remove_custom_data_layer", "layer_index"), &TileSet::remove_custom_data_layer);
  3033. ClassDB::bind_method(D_METHOD("get_custom_data_layer_by_name", "layer_name"), &TileSet::get_custom_data_layer_by_name);
  3034. ClassDB::bind_method(D_METHOD("set_custom_data_layer_name", "layer_index", "layer_name"), &TileSet::set_custom_data_layer_name);
  3035. ClassDB::bind_method(D_METHOD("get_custom_data_layer_name", "layer_index"), &TileSet::get_custom_data_layer_name);
  3036. ClassDB::bind_method(D_METHOD("set_custom_data_layer_type", "layer_index", "layer_type"), &TileSet::set_custom_data_layer_type);
  3037. ClassDB::bind_method(D_METHOD("get_custom_data_layer_type", "layer_index"), &TileSet::get_custom_data_layer_type);
  3038. // Tile proxies
  3039. ClassDB::bind_method(D_METHOD("set_source_level_tile_proxy", "source_from", "source_to"), &TileSet::set_source_level_tile_proxy);
  3040. ClassDB::bind_method(D_METHOD("get_source_level_tile_proxy", "source_from"), &TileSet::get_source_level_tile_proxy);
  3041. ClassDB::bind_method(D_METHOD("has_source_level_tile_proxy", "source_from"), &TileSet::has_source_level_tile_proxy);
  3042. ClassDB::bind_method(D_METHOD("remove_source_level_tile_proxy", "source_from"), &TileSet::remove_source_level_tile_proxy);
  3043. 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);
  3044. ClassDB::bind_method(D_METHOD("get_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::get_coords_level_tile_proxy);
  3045. ClassDB::bind_method(D_METHOD("has_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::has_coords_level_tile_proxy);
  3046. ClassDB::bind_method(D_METHOD("remove_coords_level_tile_proxy", "source_from", "coords_from"), &TileSet::remove_coords_level_tile_proxy);
  3047. 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);
  3048. ClassDB::bind_method(D_METHOD("get_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::get_alternative_level_tile_proxy);
  3049. ClassDB::bind_method(D_METHOD("has_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::has_alternative_level_tile_proxy);
  3050. ClassDB::bind_method(D_METHOD("remove_alternative_level_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::remove_alternative_level_tile_proxy);
  3051. ClassDB::bind_method(D_METHOD("map_tile_proxy", "source_from", "coords_from", "alternative_from"), &TileSet::map_tile_proxy);
  3052. ClassDB::bind_method(D_METHOD("cleanup_invalid_tile_proxies"), &TileSet::cleanup_invalid_tile_proxies);
  3053. ClassDB::bind_method(D_METHOD("clear_tile_proxies"), &TileSet::clear_tile_proxies);
  3054. // Patterns
  3055. ClassDB::bind_method(D_METHOD("add_pattern", "pattern", "index"), &TileSet::add_pattern, DEFVAL(-1));
  3056. ClassDB::bind_method(D_METHOD("get_pattern", "index"), &TileSet::get_pattern, DEFVAL(-1));
  3057. ClassDB::bind_method(D_METHOD("remove_pattern", "index"), &TileSet::remove_pattern);
  3058. ClassDB::bind_method(D_METHOD("get_patterns_count"), &TileSet::get_patterns_count);
  3059. ADD_GROUP("Rendering", "");
  3060. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uv_clipping"), "set_uv_clipping", "is_uv_clipping");
  3061. ADD_ARRAY("occlusion_layers", "occlusion_layer_");
  3062. ADD_GROUP("", "");
  3063. ADD_ARRAY("physics_layers", "physics_layer_");
  3064. ADD_ARRAY("terrain_sets", "terrain_set_");
  3065. ADD_ARRAY("navigation_layers", "navigation_layer_");
  3066. ADD_ARRAY("custom_data_layers", "custom_data_layer_");
  3067. // -- Enum binding --
  3068. BIND_ENUM_CONSTANT(TILE_SHAPE_SQUARE);
  3069. BIND_ENUM_CONSTANT(TILE_SHAPE_ISOMETRIC);
  3070. BIND_ENUM_CONSTANT(TILE_SHAPE_HALF_OFFSET_SQUARE);
  3071. BIND_ENUM_CONSTANT(TILE_SHAPE_HEXAGON);
  3072. BIND_ENUM_CONSTANT(TILE_LAYOUT_STACKED);
  3073. BIND_ENUM_CONSTANT(TILE_LAYOUT_STACKED_OFFSET);
  3074. BIND_ENUM_CONSTANT(TILE_LAYOUT_STAIRS_RIGHT);
  3075. BIND_ENUM_CONSTANT(TILE_LAYOUT_STAIRS_DOWN);
  3076. BIND_ENUM_CONSTANT(TILE_LAYOUT_DIAMOND_RIGHT);
  3077. BIND_ENUM_CONSTANT(TILE_LAYOUT_DIAMOND_DOWN);
  3078. BIND_ENUM_CONSTANT(TILE_OFFSET_AXIS_HORIZONTAL);
  3079. BIND_ENUM_CONSTANT(TILE_OFFSET_AXIS_VERTICAL);
  3080. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_RIGHT_SIDE);
  3081. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_RIGHT_CORNER);
  3082. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_RIGHT_SIDE);
  3083. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_RIGHT_CORNER);
  3084. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_SIDE);
  3085. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_CORNER);
  3086. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_LEFT_SIDE);
  3087. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_BOTTOM_LEFT_CORNER);
  3088. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_LEFT_SIDE);
  3089. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_LEFT_CORNER);
  3090. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_LEFT_SIDE);
  3091. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_LEFT_CORNER);
  3092. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_SIDE);
  3093. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_CORNER);
  3094. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_RIGHT_SIDE);
  3095. BIND_ENUM_CONSTANT(CELL_NEIGHBOR_TOP_RIGHT_CORNER);
  3096. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_CORNERS_AND_SIDES);
  3097. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_CORNERS);
  3098. BIND_ENUM_CONSTANT(TERRAIN_MODE_MATCH_SIDES);
  3099. }
  3100. TileSet::TileSet() {
  3101. // Instantiate the tile meshes.
  3102. tile_lines_mesh.instantiate();
  3103. tile_filled_mesh.instantiate();
  3104. }
  3105. TileSet::~TileSet() {
  3106. #ifndef DISABLE_DEPRECATED
  3107. for (const KeyValue<int, CompatibilityTileData *> &E : compatibility_data) {
  3108. memdelete(E.value);
  3109. }
  3110. #endif // DISABLE_DEPRECATED
  3111. while (!source_ids.is_empty()) {
  3112. remove_source(source_ids[0]);
  3113. }
  3114. }
  3115. /////////////////////////////// TileSetSource //////////////////////////////////////
  3116. void TileSetSource::set_tile_set(const TileSet *p_tile_set) {
  3117. tile_set = p_tile_set;
  3118. }
  3119. void TileSetSource::reset_state() {
  3120. tile_set = nullptr;
  3121. };
  3122. void TileSetSource::_bind_methods() {
  3123. // Base tiles
  3124. ClassDB::bind_method(D_METHOD("get_tiles_count"), &TileSetSource::get_tiles_count);
  3125. ClassDB::bind_method(D_METHOD("get_tile_id", "index"), &TileSetSource::get_tile_id);
  3126. ClassDB::bind_method(D_METHOD("has_tile", "atlas_coords"), &TileSetSource::has_tile);
  3127. // Alternative tiles
  3128. ClassDB::bind_method(D_METHOD("get_alternative_tiles_count", "atlas_coords"), &TileSetSource::get_alternative_tiles_count);
  3129. ClassDB::bind_method(D_METHOD("get_alternative_tile_id", "atlas_coords", "index"), &TileSetSource::get_alternative_tile_id);
  3130. ClassDB::bind_method(D_METHOD("has_alternative_tile", "atlas_coords", "alternative_tile"), &TileSetSource::has_alternative_tile);
  3131. }
  3132. /////////////////////////////// TileSetAtlasSource //////////////////////////////////////
  3133. void TileSetAtlasSource::set_tile_set(const TileSet *p_tile_set) {
  3134. tile_set = p_tile_set;
  3135. // Set the TileSet on all TileData.
  3136. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3137. for (KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  3138. E_alternative.value->set_tile_set(tile_set);
  3139. }
  3140. }
  3141. }
  3142. const TileSet *TileSetAtlasSource::get_tile_set() const {
  3143. return tile_set;
  3144. }
  3145. void TileSetAtlasSource::notify_tile_data_properties_should_change() {
  3146. // Set the TileSet on all TileData.
  3147. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3148. for (KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  3149. E_alternative.value->notify_tile_data_properties_should_change();
  3150. }
  3151. }
  3152. }
  3153. void TileSetAtlasSource::add_occlusion_layer(int p_to_pos) {
  3154. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3155. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3156. E_alternative.value->add_occlusion_layer(p_to_pos);
  3157. }
  3158. }
  3159. }
  3160. void TileSetAtlasSource::move_occlusion_layer(int p_from_index, int p_to_pos) {
  3161. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3162. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3163. E_alternative.value->move_occlusion_layer(p_from_index, p_to_pos);
  3164. }
  3165. }
  3166. }
  3167. void TileSetAtlasSource::remove_occlusion_layer(int p_index) {
  3168. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3169. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3170. E_alternative.value->remove_occlusion_layer(p_index);
  3171. }
  3172. }
  3173. }
  3174. void TileSetAtlasSource::add_physics_layer(int p_to_pos) {
  3175. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3176. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3177. E_alternative.value->add_physics_layer(p_to_pos);
  3178. }
  3179. }
  3180. }
  3181. void TileSetAtlasSource::move_physics_layer(int p_from_index, int p_to_pos) {
  3182. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3183. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3184. E_alternative.value->move_physics_layer(p_from_index, p_to_pos);
  3185. }
  3186. }
  3187. }
  3188. void TileSetAtlasSource::remove_physics_layer(int p_index) {
  3189. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3190. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3191. E_alternative.value->remove_physics_layer(p_index);
  3192. }
  3193. }
  3194. }
  3195. void TileSetAtlasSource::add_terrain_set(int p_to_pos) {
  3196. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3197. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3198. E_alternative.value->add_terrain_set(p_to_pos);
  3199. }
  3200. }
  3201. }
  3202. void TileSetAtlasSource::move_terrain_set(int p_from_index, int p_to_pos) {
  3203. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3204. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3205. E_alternative.value->move_terrain_set(p_from_index, p_to_pos);
  3206. }
  3207. }
  3208. }
  3209. void TileSetAtlasSource::remove_terrain_set(int p_index) {
  3210. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3211. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3212. E_alternative.value->remove_terrain_set(p_index);
  3213. }
  3214. }
  3215. }
  3216. void TileSetAtlasSource::add_terrain(int p_terrain_set, int p_to_pos) {
  3217. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3218. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3219. E_alternative.value->add_terrain(p_terrain_set, p_to_pos);
  3220. }
  3221. }
  3222. }
  3223. void TileSetAtlasSource::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  3224. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3225. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3226. E_alternative.value->move_terrain(p_terrain_set, p_from_index, p_to_pos);
  3227. }
  3228. }
  3229. }
  3230. void TileSetAtlasSource::remove_terrain(int p_terrain_set, int p_index) {
  3231. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3232. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3233. E_alternative.value->remove_terrain(p_terrain_set, p_index);
  3234. }
  3235. }
  3236. }
  3237. void TileSetAtlasSource::add_navigation_layer(int p_to_pos) {
  3238. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3239. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3240. E_alternative.value->add_navigation_layer(p_to_pos);
  3241. }
  3242. }
  3243. }
  3244. void TileSetAtlasSource::move_navigation_layer(int p_from_index, int p_to_pos) {
  3245. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3246. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3247. E_alternative.value->move_navigation_layer(p_from_index, p_to_pos);
  3248. }
  3249. }
  3250. }
  3251. void TileSetAtlasSource::remove_navigation_layer(int p_index) {
  3252. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3253. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3254. E_alternative.value->remove_navigation_layer(p_index);
  3255. }
  3256. }
  3257. }
  3258. void TileSetAtlasSource::add_custom_data_layer(int p_to_pos) {
  3259. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3260. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3261. E_alternative.value->add_custom_data_layer(p_to_pos);
  3262. }
  3263. }
  3264. }
  3265. void TileSetAtlasSource::move_custom_data_layer(int p_from_index, int p_to_pos) {
  3266. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3267. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3268. E_alternative.value->move_custom_data_layer(p_from_index, p_to_pos);
  3269. }
  3270. }
  3271. }
  3272. void TileSetAtlasSource::remove_custom_data_layer(int p_index) {
  3273. for (KeyValue<Vector2i, TileAlternativesData> E_tile : tiles) {
  3274. for (KeyValue<int, TileData *> E_alternative : E_tile.value.alternatives) {
  3275. E_alternative.value->remove_custom_data_layer(p_index);
  3276. }
  3277. }
  3278. }
  3279. void TileSetAtlasSource::reset_state() {
  3280. tile_set = nullptr;
  3281. for (KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3282. for (const KeyValue<int, TileData *> &E_tile_data : E_tile.value.alternatives) {
  3283. memdelete(E_tile_data.value);
  3284. }
  3285. }
  3286. _coords_mapping_cache.clear();
  3287. tiles.clear();
  3288. tiles_ids.clear();
  3289. _queue_update_padded_texture();
  3290. }
  3291. void TileSetAtlasSource::set_texture(Ref<Texture2D> p_texture) {
  3292. if (texture.is_valid()) {
  3293. texture->disconnect(SNAME("changed"), callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  3294. }
  3295. texture = p_texture;
  3296. if (texture.is_valid()) {
  3297. texture->connect(SNAME("changed"), callable_mp(this, &TileSetAtlasSource::_queue_update_padded_texture));
  3298. }
  3299. _clear_tiles_outside_texture();
  3300. _queue_update_padded_texture();
  3301. emit_changed();
  3302. }
  3303. Ref<Texture2D> TileSetAtlasSource::get_texture() const {
  3304. return texture;
  3305. }
  3306. void TileSetAtlasSource::set_margins(Vector2i p_margins) {
  3307. if (p_margins.x < 0 || p_margins.y < 0) {
  3308. WARN_PRINT("Atlas source margins should be positive.");
  3309. margins = Vector2i(MAX(0, p_margins.x), MAX(0, p_margins.y));
  3310. } else {
  3311. margins = p_margins;
  3312. }
  3313. _clear_tiles_outside_texture();
  3314. _queue_update_padded_texture();
  3315. emit_changed();
  3316. }
  3317. Vector2i TileSetAtlasSource::get_margins() const {
  3318. return margins;
  3319. }
  3320. void TileSetAtlasSource::set_separation(Vector2i p_separation) {
  3321. if (p_separation.x < 0 || p_separation.y < 0) {
  3322. WARN_PRINT("Atlas source separation should be positive.");
  3323. separation = Vector2i(MAX(0, p_separation.x), MAX(0, p_separation.y));
  3324. } else {
  3325. separation = p_separation;
  3326. }
  3327. _clear_tiles_outside_texture();
  3328. _queue_update_padded_texture();
  3329. emit_changed();
  3330. }
  3331. Vector2i TileSetAtlasSource::get_separation() const {
  3332. return separation;
  3333. }
  3334. void TileSetAtlasSource::set_texture_region_size(Vector2i p_tile_size) {
  3335. if (p_tile_size.x <= 0 || p_tile_size.y <= 0) {
  3336. WARN_PRINT("Atlas source tile_size should be strictly positive.");
  3337. texture_region_size = Vector2i(MAX(1, p_tile_size.x), MAX(1, p_tile_size.y));
  3338. } else {
  3339. texture_region_size = p_tile_size;
  3340. }
  3341. _clear_tiles_outside_texture();
  3342. _queue_update_padded_texture();
  3343. emit_changed();
  3344. }
  3345. Vector2i TileSetAtlasSource::get_texture_region_size() const {
  3346. return texture_region_size;
  3347. }
  3348. void TileSetAtlasSource::set_use_texture_padding(bool p_use_padding) {
  3349. if (use_texture_padding == p_use_padding) {
  3350. return;
  3351. }
  3352. use_texture_padding = p_use_padding;
  3353. _queue_update_padded_texture();
  3354. emit_changed();
  3355. }
  3356. bool TileSetAtlasSource::get_use_texture_padding() const {
  3357. return use_texture_padding;
  3358. }
  3359. Vector2i TileSetAtlasSource::get_atlas_grid_size() const {
  3360. Ref<Texture2D> texture = get_texture();
  3361. if (!texture.is_valid()) {
  3362. return Vector2i();
  3363. }
  3364. ERR_FAIL_COND_V(texture_region_size.x <= 0 || texture_region_size.y <= 0, Vector2i());
  3365. Size2i valid_area = texture->get_size() - margins;
  3366. // Compute the number of valid tiles in the tiles atlas
  3367. Size2i grid_size = Size2i();
  3368. if (valid_area.x >= texture_region_size.x && valid_area.y >= texture_region_size.y) {
  3369. valid_area -= texture_region_size;
  3370. grid_size = Size2i(1, 1) + valid_area / (texture_region_size + separation);
  3371. }
  3372. return grid_size;
  3373. }
  3374. bool TileSetAtlasSource::_set(const StringName &p_name, const Variant &p_value) {
  3375. Vector<String> components = String(p_name).split("/", true, 2);
  3376. // Compute the vector2i if we have coordinates.
  3377. Vector<String> coords_split = components[0].split(":");
  3378. Vector2i coords = TileSetSource::INVALID_ATLAS_COORDS;
  3379. if (coords_split.size() == 2 && coords_split[0].is_valid_int() && coords_split[1].is_valid_int()) {
  3380. coords = Vector2i(coords_split[0].to_int(), coords_split[1].to_int());
  3381. }
  3382. // Properties.
  3383. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  3384. // Create the tile if needed.
  3385. if (!has_tile(coords)) {
  3386. create_tile(coords);
  3387. }
  3388. if (components.size() >= 2) {
  3389. // Properties.
  3390. if (components[1] == "size_in_atlas") {
  3391. move_tile_in_atlas(coords, coords, p_value);
  3392. return true;
  3393. } else if (components[1] == "next_alternative_id") {
  3394. tiles[coords].next_alternative_id = p_value;
  3395. return true;
  3396. } else if (components[1] == "animation_columns") {
  3397. set_tile_animation_columns(coords, p_value);
  3398. return true;
  3399. } else if (components[1] == "animation_separation") {
  3400. set_tile_animation_separation(coords, p_value);
  3401. return true;
  3402. } else if (components[1] == "animation_speed") {
  3403. set_tile_animation_speed(coords, p_value);
  3404. return true;
  3405. } else if (components[1] == "animation_frames_count") {
  3406. set_tile_animation_frames_count(coords, p_value);
  3407. return true;
  3408. } else if (components.size() >= 3 && components[1].begins_with("animation_frame_") && components[1].trim_prefix("animation_frame_").is_valid_int()) {
  3409. int frame = components[1].trim_prefix("animation_frame_").to_int();
  3410. if (components[2] == "duration") {
  3411. if (frame >= get_tile_animation_frames_count(coords)) {
  3412. set_tile_animation_frames_count(coords, frame + 1);
  3413. }
  3414. set_tile_animation_frame_duration(coords, frame, p_value);
  3415. return true;
  3416. }
  3417. return false;
  3418. } else if (components[1].is_valid_int()) {
  3419. int alternative_id = components[1].to_int();
  3420. if (alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE) {
  3421. // Create the alternative if needed ?
  3422. if (!has_alternative_tile(coords, alternative_id)) {
  3423. create_alternative_tile(coords, alternative_id);
  3424. }
  3425. if (!tiles[coords].alternatives.has(alternative_id)) {
  3426. tiles[coords].alternatives[alternative_id] = memnew(TileData);
  3427. tiles[coords].alternatives[alternative_id]->set_tile_set(tile_set);
  3428. tiles[coords].alternatives[alternative_id]->set_allow_transform(alternative_id > 0);
  3429. tiles[coords].alternatives_ids.append(alternative_id);
  3430. }
  3431. if (components.size() >= 3) {
  3432. bool valid;
  3433. tiles[coords].alternatives[alternative_id]->set(components[2], p_value, &valid);
  3434. return valid;
  3435. } else {
  3436. // Only create the alternative if it did not exist yet.
  3437. return true;
  3438. }
  3439. }
  3440. }
  3441. }
  3442. }
  3443. return false;
  3444. }
  3445. bool TileSetAtlasSource::_get(const StringName &p_name, Variant &r_ret) const {
  3446. Vector<String> components = String(p_name).split("/", true, 2);
  3447. // Properties.
  3448. Vector<String> coords_split = components[0].split(":");
  3449. if (coords_split.size() == 2 && coords_split[0].is_valid_int() && coords_split[1].is_valid_int()) {
  3450. Vector2i coords = Vector2i(coords_split[0].to_int(), coords_split[1].to_int());
  3451. if (tiles.has(coords)) {
  3452. if (components.size() >= 2) {
  3453. // Properties.
  3454. if (components[1] == "size_in_atlas") {
  3455. r_ret = tiles[coords].size_in_atlas;
  3456. return true;
  3457. } else if (components[1] == "next_alternative_id") {
  3458. r_ret = tiles[coords].next_alternative_id;
  3459. return true;
  3460. } else if (components[1] == "animation_columns") {
  3461. r_ret = get_tile_animation_columns(coords);
  3462. return true;
  3463. } else if (components[1] == "animation_separation") {
  3464. r_ret = get_tile_animation_separation(coords);
  3465. return true;
  3466. } else if (components[1] == "animation_speed") {
  3467. r_ret = get_tile_animation_speed(coords);
  3468. return true;
  3469. } else if (components[1] == "animation_frames_count") {
  3470. r_ret = get_tile_animation_frames_count(coords);
  3471. return true;
  3472. } else if (components.size() >= 3 && components[1].begins_with("animation_frame_") && components[1].trim_prefix("animation_frame_").is_valid_int()) {
  3473. int frame = components[1].trim_prefix("animation_frame_").to_int();
  3474. if (frame < 0 || frame >= get_tile_animation_frames_count(coords)) {
  3475. return false;
  3476. }
  3477. if (components[2] == "duration") {
  3478. r_ret = get_tile_animation_frame_duration(coords, frame);
  3479. return true;
  3480. }
  3481. return false;
  3482. } else if (components[1].is_valid_int()) {
  3483. int alternative_id = components[1].to_int();
  3484. if (alternative_id != TileSetSource::INVALID_TILE_ALTERNATIVE && tiles[coords].alternatives.has(alternative_id)) {
  3485. if (components.size() >= 3) {
  3486. bool valid;
  3487. r_ret = tiles[coords].alternatives[alternative_id]->get(components[2], &valid);
  3488. return valid;
  3489. } else {
  3490. // Only to notify the tile alternative exists.
  3491. r_ret = alternative_id;
  3492. return true;
  3493. }
  3494. }
  3495. }
  3496. }
  3497. }
  3498. }
  3499. return false;
  3500. }
  3501. void TileSetAtlasSource::_get_property_list(List<PropertyInfo> *p_list) const {
  3502. // Atlases data.
  3503. PropertyInfo property_info;
  3504. for (const KeyValue<Vector2i, TileAlternativesData> &E_tile : tiles) {
  3505. List<PropertyInfo> tile_property_list;
  3506. // size_in_atlas
  3507. property_info = PropertyInfo(Variant::VECTOR2I, "size_in_atlas", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3508. if (E_tile.value.size_in_atlas == Vector2i(1, 1)) {
  3509. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3510. }
  3511. tile_property_list.push_back(property_info);
  3512. // next_alternative_id
  3513. property_info = PropertyInfo(Variant::INT, "next_alternative_id", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3514. if (E_tile.value.next_alternative_id == 1) {
  3515. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3516. }
  3517. tile_property_list.push_back(property_info);
  3518. // animation_columns.
  3519. property_info = PropertyInfo(Variant::INT, "animation_columns", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3520. if (E_tile.value.animation_columns == 0) {
  3521. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3522. }
  3523. tile_property_list.push_back(property_info);
  3524. // animation_separation.
  3525. property_info = PropertyInfo(Variant::INT, "animation_separation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3526. if (E_tile.value.animation_separation == Vector2i()) {
  3527. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3528. }
  3529. tile_property_list.push_back(property_info);
  3530. // animation_speed.
  3531. property_info = PropertyInfo(Variant::FLOAT, "animation_speed", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3532. if (E_tile.value.animation_speed == 1.0) {
  3533. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3534. }
  3535. tile_property_list.push_back(property_info);
  3536. // animation_frames_count.
  3537. tile_property_list.push_back(PropertyInfo(Variant::INT, "animation_frames_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  3538. // animation_frame_*.
  3539. bool store_durations = tiles[E_tile.key].animation_frames_durations.size() >= 2;
  3540. for (int i = 0; i < (int)tiles[E_tile.key].animation_frames_durations.size(); i++) {
  3541. property_info = PropertyInfo(Variant::FLOAT, vformat("animation_frame_%d/duration", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  3542. if (!store_durations) {
  3543. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3544. }
  3545. tile_property_list.push_back(property_info);
  3546. }
  3547. for (const KeyValue<int, TileData *> &E_alternative : E_tile.value.alternatives) {
  3548. // Add a dummy property to show the alternative exists.
  3549. tile_property_list.push_back(PropertyInfo(Variant::INT, vformat("%d", E_alternative.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  3550. // Get the alternative tile's properties and append them to the list of properties.
  3551. List<PropertyInfo> alternative_property_list;
  3552. E_alternative.value->get_property_list(&alternative_property_list);
  3553. for (PropertyInfo &alternative_property_info : alternative_property_list) {
  3554. Variant default_value = ClassDB::class_get_default_property_value("TileData", alternative_property_info.name);
  3555. Variant value = E_alternative.value->get(alternative_property_info.name);
  3556. if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {
  3557. alternative_property_info.usage ^= PROPERTY_USAGE_STORAGE;
  3558. }
  3559. alternative_property_info.name = vformat("%s/%s", vformat("%d", E_alternative.key), alternative_property_info.name);
  3560. tile_property_list.push_back(alternative_property_info);
  3561. }
  3562. }
  3563. // Add all alternative.
  3564. for (PropertyInfo &tile_property_info : tile_property_list) {
  3565. tile_property_info.name = vformat("%s/%s", vformat("%d:%d", E_tile.key.x, E_tile.key.y), tile_property_info.name);
  3566. p_list->push_back(tile_property_info);
  3567. }
  3568. }
  3569. }
  3570. void TileSetAtlasSource::create_tile(const Vector2i p_atlas_coords, const Vector2i p_size) {
  3571. // Create a tile if it does not exists.
  3572. ERR_FAIL_COND(p_atlas_coords.x < 0 || p_atlas_coords.y < 0);
  3573. ERR_FAIL_COND(p_size.x <= 0 || p_size.y <= 0);
  3574. bool room_for_tile = has_room_for_tile(p_atlas_coords, p_size, 1, Vector2i(), 1);
  3575. 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.");
  3576. // Initialize the tile data.
  3577. TileAlternativesData tad;
  3578. tad.size_in_atlas = p_size;
  3579. tad.animation_frames_durations.push_back(1.0);
  3580. tad.alternatives[0] = memnew(TileData);
  3581. tad.alternatives[0]->set_tile_set(tile_set);
  3582. tad.alternatives[0]->set_allow_transform(false);
  3583. tad.alternatives[0]->connect("changed", callable_mp((Resource *)this, &TileSetAtlasSource::emit_changed));
  3584. tad.alternatives[0]->notify_property_list_changed();
  3585. tad.alternatives_ids.append(0);
  3586. // Create and resize the tile.
  3587. tiles.insert(p_atlas_coords, tad);
  3588. tiles_ids.append(p_atlas_coords);
  3589. tiles_ids.sort();
  3590. _create_coords_mapping_cache(p_atlas_coords);
  3591. _queue_update_padded_texture();
  3592. emit_signal(SNAME("changed"));
  3593. }
  3594. void TileSetAtlasSource::remove_tile(Vector2i p_atlas_coords) {
  3595. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3596. // Remove all covered positions from the mapping cache
  3597. _clear_coords_mapping_cache(p_atlas_coords);
  3598. // Free tile data.
  3599. for (const KeyValue<int, TileData *> &E_tile_data : tiles[p_atlas_coords].alternatives) {
  3600. memdelete(E_tile_data.value);
  3601. }
  3602. // Delete the tile
  3603. tiles.erase(p_atlas_coords);
  3604. tiles_ids.erase(p_atlas_coords);
  3605. tiles_ids.sort();
  3606. _queue_update_padded_texture();
  3607. emit_signal(SNAME("changed"));
  3608. }
  3609. bool TileSetAtlasSource::has_tile(Vector2i p_atlas_coords) const {
  3610. return tiles.has(p_atlas_coords);
  3611. }
  3612. Vector2i TileSetAtlasSource::get_tile_at_coords(Vector2i p_atlas_coords) const {
  3613. if (!_coords_mapping_cache.has(p_atlas_coords)) {
  3614. return INVALID_ATLAS_COORDS;
  3615. }
  3616. return _coords_mapping_cache[p_atlas_coords];
  3617. }
  3618. void TileSetAtlasSource::set_tile_animation_columns(const Vector2i p_atlas_coords, int p_frame_columns) {
  3619. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3620. ERR_FAIL_COND(p_frame_columns < 0);
  3621. TileAlternativesData &tad = tiles[p_atlas_coords];
  3622. 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);
  3623. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  3624. _clear_coords_mapping_cache(p_atlas_coords);
  3625. tiles[p_atlas_coords].animation_columns = p_frame_columns;
  3626. _create_coords_mapping_cache(p_atlas_coords);
  3627. _queue_update_padded_texture();
  3628. emit_signal(SNAME("changed"));
  3629. }
  3630. int TileSetAtlasSource::get_tile_animation_columns(const Vector2i p_atlas_coords) const {
  3631. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3632. return tiles[p_atlas_coords].animation_columns;
  3633. }
  3634. void TileSetAtlasSource::set_tile_animation_separation(const Vector2i p_atlas_coords, const Vector2i p_separation) {
  3635. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3636. ERR_FAIL_COND(p_separation.x < 0 || p_separation.y < 0);
  3637. TileAlternativesData &tad = tiles[p_atlas_coords];
  3638. 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);
  3639. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  3640. _clear_coords_mapping_cache(p_atlas_coords);
  3641. tiles[p_atlas_coords].animation_separation = p_separation;
  3642. _create_coords_mapping_cache(p_atlas_coords);
  3643. _queue_update_padded_texture();
  3644. emit_signal(SNAME("changed"));
  3645. }
  3646. Vector2i TileSetAtlasSource::get_tile_animation_separation(const Vector2i p_atlas_coords) const {
  3647. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3648. return tiles[p_atlas_coords].animation_separation;
  3649. }
  3650. void TileSetAtlasSource::set_tile_animation_speed(const Vector2i p_atlas_coords, real_t p_speed) {
  3651. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3652. ERR_FAIL_COND(p_speed <= 0);
  3653. tiles[p_atlas_coords].animation_speed = p_speed;
  3654. emit_signal(SNAME("changed"));
  3655. }
  3656. real_t TileSetAtlasSource::get_tile_animation_speed(const Vector2i p_atlas_coords) const {
  3657. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1.0, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3658. return tiles[p_atlas_coords].animation_speed;
  3659. }
  3660. void TileSetAtlasSource::set_tile_animation_frames_count(const Vector2i p_atlas_coords, int p_frames_count) {
  3661. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3662. ERR_FAIL_COND(p_frames_count < 1);
  3663. int old_size = tiles[p_atlas_coords].animation_frames_durations.size();
  3664. if (p_frames_count == old_size) {
  3665. return;
  3666. }
  3667. TileAlternativesData &tad = tiles[p_atlas_coords];
  3668. 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);
  3669. ERR_FAIL_COND_MSG(!room_for_tile, "Cannot set animation columns count, tiles are already present in the space the tile would cover.");
  3670. _clear_coords_mapping_cache(p_atlas_coords);
  3671. tiles[p_atlas_coords].animation_frames_durations.resize(p_frames_count);
  3672. for (int i = old_size; i < p_frames_count; i++) {
  3673. tiles[p_atlas_coords].animation_frames_durations[i] = 1.0;
  3674. }
  3675. _create_coords_mapping_cache(p_atlas_coords);
  3676. _queue_update_padded_texture();
  3677. notify_property_list_changed();
  3678. emit_signal(SNAME("changed"));
  3679. }
  3680. int TileSetAtlasSource::get_tile_animation_frames_count(const Vector2i p_atlas_coords) const {
  3681. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3682. return tiles[p_atlas_coords].animation_frames_durations.size();
  3683. }
  3684. void TileSetAtlasSource::set_tile_animation_frame_duration(const Vector2i p_atlas_coords, int p_frame_index, real_t p_duration) {
  3685. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3686. ERR_FAIL_INDEX(p_frame_index, (int)tiles[p_atlas_coords].animation_frames_durations.size());
  3687. ERR_FAIL_COND(p_duration <= 0.0);
  3688. tiles[p_atlas_coords].animation_frames_durations[p_frame_index] = p_duration;
  3689. emit_signal(SNAME("changed"));
  3690. }
  3691. real_t TileSetAtlasSource::get_tile_animation_frame_duration(const Vector2i p_atlas_coords, int p_frame_index) const {
  3692. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3693. ERR_FAIL_INDEX_V(p_frame_index, (int)tiles[p_atlas_coords].animation_frames_durations.size(), 0.0);
  3694. return tiles[p_atlas_coords].animation_frames_durations[p_frame_index];
  3695. }
  3696. real_t TileSetAtlasSource::get_tile_animation_total_duration(const Vector2i p_atlas_coords) const {
  3697. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), 1, vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3698. real_t sum = 0.0;
  3699. for (int frame = 0; frame < (int)tiles[p_atlas_coords].animation_frames_durations.size(); frame++) {
  3700. sum += tiles[p_atlas_coords].animation_frames_durations[frame];
  3701. }
  3702. return sum;
  3703. }
  3704. Vector2i TileSetAtlasSource::get_tile_size_in_atlas(Vector2i p_atlas_coords) const {
  3705. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(-1, -1), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3706. return tiles[p_atlas_coords].size_in_atlas;
  3707. }
  3708. int TileSetAtlasSource::get_tiles_count() const {
  3709. return tiles_ids.size();
  3710. }
  3711. Vector2i TileSetAtlasSource::get_tile_id(int p_index) const {
  3712. ERR_FAIL_INDEX_V(p_index, tiles_ids.size(), TileSetSource::INVALID_ATLAS_COORDS);
  3713. return tiles_ids[p_index];
  3714. }
  3715. 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 {
  3716. if (p_atlas_coords.x < 0 || p_atlas_coords.y < 0) {
  3717. return false;
  3718. }
  3719. if (p_size.x <= 0 || p_size.y <= 0) {
  3720. return false;
  3721. }
  3722. Size2i atlas_grid_size = get_atlas_grid_size();
  3723. for (int frame = 0; frame < p_frames_count; frame++) {
  3724. 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));
  3725. for (int x = 0; x < p_size.x; x++) {
  3726. for (int y = 0; y < p_size.y; y++) {
  3727. Vector2i coords = frame_coords + Vector2i(x, y);
  3728. if (_coords_mapping_cache.has(coords) && _coords_mapping_cache[coords] != p_ignored_tile) {
  3729. return false;
  3730. }
  3731. if (coords.x >= atlas_grid_size.x || coords.y >= atlas_grid_size.y) {
  3732. return false;
  3733. }
  3734. }
  3735. }
  3736. }
  3737. return true;
  3738. }
  3739. PackedVector2Array TileSetAtlasSource::get_tiles_to_be_removed_on_change(Ref<Texture2D> p_texture, Vector2i p_margins, Vector2i p_separation, Vector2i p_texture_region_size) {
  3740. ERR_FAIL_COND_V(p_margins.x < 0 || p_margins.y < 0, PackedVector2Array());
  3741. ERR_FAIL_COND_V(p_separation.x < 0 || p_separation.y < 0, PackedVector2Array());
  3742. ERR_FAIL_COND_V(p_texture_region_size.x <= 0 || p_texture_region_size.y <= 0, PackedVector2Array());
  3743. // Compute the new atlas grid size.
  3744. Size2 new_grid_size;
  3745. if (p_texture.is_valid()) {
  3746. Size2i valid_area = p_texture->get_size() - p_margins;
  3747. // Compute the number of valid tiles in the tiles atlas
  3748. if (valid_area.x >= p_texture_region_size.x && valid_area.y >= p_texture_region_size.y) {
  3749. valid_area -= p_texture_region_size;
  3750. new_grid_size = Size2i(1, 1) + valid_area / (p_texture_region_size + p_separation);
  3751. }
  3752. }
  3753. Vector<Vector2> output;
  3754. for (KeyValue<Vector2i, TileAlternativesData> &E : tiles) {
  3755. for (unsigned int frame = 0; frame < E.value.animation_frames_durations.size(); frame++) {
  3756. 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));
  3757. frame_coords += E.value.size_in_atlas;
  3758. if (frame_coords.x > new_grid_size.x || frame_coords.y > new_grid_size.y) {
  3759. output.push_back(E.key);
  3760. break;
  3761. }
  3762. }
  3763. }
  3764. return output;
  3765. }
  3766. Rect2i TileSetAtlasSource::get_tile_texture_region(Vector2i p_atlas_coords, int p_frame) const {
  3767. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Rect2i(), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3768. ERR_FAIL_INDEX_V(p_frame, (int)tiles[p_atlas_coords].animation_frames_durations.size(), Rect2i());
  3769. const TileAlternativesData &tad = tiles[p_atlas_coords];
  3770. Vector2i size_in_atlas = tad.size_in_atlas;
  3771. Vector2 region_size = texture_region_size * size_in_atlas + separation * (size_in_atlas - Vector2i(1, 1));
  3772. 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));
  3773. Vector2 origin = margins + (frame_coords * (texture_region_size + separation));
  3774. return Rect2(origin, region_size);
  3775. }
  3776. Vector2i TileSetAtlasSource::get_tile_effective_texture_offset(Vector2i p_atlas_coords, int p_alternative_tile) const {
  3777. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Vector2i(), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3778. ERR_FAIL_COND_V_MSG(!has_alternative_tile(p_atlas_coords, p_alternative_tile), Vector2i(), vformat("TileSetAtlasSource has no alternative tile with id %d at %s.", p_alternative_tile, String(p_atlas_coords)));
  3779. ERR_FAIL_COND_V(!tile_set, Vector2i());
  3780. Vector2 margin = (get_tile_texture_region(p_atlas_coords).size - tile_set->get_tile_size()) / 2;
  3781. margin = Vector2i(MAX(0, margin.x), MAX(0, margin.y));
  3782. Vector2i effective_texture_offset = get_tile_data(p_atlas_coords, p_alternative_tile)->get_texture_offset();
  3783. if (ABS(effective_texture_offset.x) > margin.x || ABS(effective_texture_offset.y) > margin.y) {
  3784. effective_texture_offset = effective_texture_offset.clamp(-margin, margin);
  3785. }
  3786. return effective_texture_offset;
  3787. }
  3788. // Getters for texture and tile region (padded or not)
  3789. Ref<Texture2D> TileSetAtlasSource::get_runtime_texture() const {
  3790. if (use_texture_padding) {
  3791. return padded_texture;
  3792. } else {
  3793. return texture;
  3794. }
  3795. }
  3796. Rect2i TileSetAtlasSource::get_runtime_tile_texture_region(Vector2i p_atlas_coords, int p_frame) const {
  3797. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Rect2i(), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3798. ERR_FAIL_INDEX_V(p_frame, (int)tiles[p_atlas_coords].animation_frames_durations.size(), Rect2i());
  3799. Rect2i src_rect = get_tile_texture_region(p_atlas_coords, p_frame);
  3800. if (use_texture_padding) {
  3801. const TileAlternativesData &tad = tiles[p_atlas_coords];
  3802. 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));
  3803. Vector2i base_pos = frame_coords * (texture_region_size + Vector2i(2, 2)) + Vector2i(1, 1);
  3804. return Rect2i(base_pos, src_rect.size);
  3805. } else {
  3806. return src_rect;
  3807. }
  3808. }
  3809. void TileSetAtlasSource::move_tile_in_atlas(Vector2i p_atlas_coords, Vector2i p_new_atlas_coords, Vector2i p_new_size) {
  3810. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3811. TileAlternativesData &tad = tiles[p_atlas_coords];
  3812. // Compute the actual new rect from arguments.
  3813. Vector2i new_atlas_coords = (p_new_atlas_coords != INVALID_ATLAS_COORDS) ? p_new_atlas_coords : p_atlas_coords;
  3814. Vector2i new_size = (p_new_size != Vector2i(-1, -1)) ? p_new_size : tad.size_in_atlas;
  3815. if (new_atlas_coords == p_atlas_coords && new_size == tad.size_in_atlas) {
  3816. return;
  3817. }
  3818. 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);
  3819. 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));
  3820. _clear_coords_mapping_cache(p_atlas_coords);
  3821. // Move the tile and update its size.
  3822. if (new_atlas_coords != p_atlas_coords) {
  3823. tiles[new_atlas_coords] = tiles[p_atlas_coords];
  3824. tiles.erase(p_atlas_coords);
  3825. tiles_ids.erase(p_atlas_coords);
  3826. tiles_ids.append(new_atlas_coords);
  3827. tiles_ids.sort();
  3828. }
  3829. tiles[new_atlas_coords].size_in_atlas = new_size;
  3830. _create_coords_mapping_cache(new_atlas_coords);
  3831. _queue_update_padded_texture();
  3832. emit_signal(SNAME("changed"));
  3833. }
  3834. int TileSetAtlasSource::create_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_id_override) {
  3835. 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)));
  3836. 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));
  3837. int new_alternative_id = p_alternative_id_override >= 0 ? p_alternative_id_override : tiles[p_atlas_coords].next_alternative_id;
  3838. tiles[p_atlas_coords].alternatives[new_alternative_id] = memnew(TileData);
  3839. tiles[p_atlas_coords].alternatives[new_alternative_id]->set_tile_set(tile_set);
  3840. tiles[p_atlas_coords].alternatives[new_alternative_id]->set_allow_transform(true);
  3841. tiles[p_atlas_coords].alternatives[new_alternative_id]->notify_property_list_changed();
  3842. tiles[p_atlas_coords].alternatives_ids.append(new_alternative_id);
  3843. tiles[p_atlas_coords].alternatives_ids.sort();
  3844. _compute_next_alternative_id(p_atlas_coords);
  3845. emit_signal(SNAME("changed"));
  3846. return new_alternative_id;
  3847. }
  3848. void TileSetAtlasSource::remove_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) {
  3849. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3850. 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)));
  3851. ERR_FAIL_COND_MSG(p_alternative_tile == 0, "Cannot remove the alternative with id 0, the base tile alternative cannot be removed.");
  3852. memdelete(tiles[p_atlas_coords].alternatives[p_alternative_tile]);
  3853. tiles[p_atlas_coords].alternatives.erase(p_alternative_tile);
  3854. tiles[p_atlas_coords].alternatives_ids.erase(p_alternative_tile);
  3855. tiles[p_atlas_coords].alternatives_ids.sort();
  3856. emit_signal(SNAME("changed"));
  3857. }
  3858. void TileSetAtlasSource::set_alternative_tile_id(const Vector2i p_atlas_coords, int p_alternative_tile, int p_new_id) {
  3859. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3860. 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)));
  3861. ERR_FAIL_COND_MSG(p_alternative_tile == 0, "Cannot change the alternative with id 0, the base tile alternative cannot be modified.");
  3862. 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)));
  3863. tiles[p_atlas_coords].alternatives[p_new_id] = tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3864. tiles[p_atlas_coords].alternatives_ids.append(p_new_id);
  3865. tiles[p_atlas_coords].alternatives.erase(p_alternative_tile);
  3866. tiles[p_atlas_coords].alternatives_ids.erase(p_alternative_tile);
  3867. tiles[p_atlas_coords].alternatives_ids.sort();
  3868. emit_signal(SNAME("changed"));
  3869. }
  3870. bool TileSetAtlasSource::has_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  3871. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), false, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3872. return tiles[p_atlas_coords].alternatives.has(p_alternative_tile);
  3873. }
  3874. int TileSetAtlasSource::get_next_alternative_tile_id(const Vector2i p_atlas_coords) const {
  3875. 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)));
  3876. return tiles[p_atlas_coords].next_alternative_id;
  3877. }
  3878. int TileSetAtlasSource::get_alternative_tiles_count(const Vector2i p_atlas_coords) const {
  3879. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), -1, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3880. return tiles[p_atlas_coords].alternatives_ids.size();
  3881. }
  3882. int TileSetAtlasSource::get_alternative_tile_id(const Vector2i p_atlas_coords, int p_index) const {
  3883. 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)));
  3884. ERR_FAIL_INDEX_V(p_index, tiles[p_atlas_coords].alternatives_ids.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  3885. return tiles[p_atlas_coords].alternatives_ids[p_index];
  3886. }
  3887. TileData *TileSetAtlasSource::get_tile_data(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  3888. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("The TileSetAtlasSource atlas has no tile at %s.", String(p_atlas_coords)));
  3889. 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)));
  3890. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3891. }
  3892. void TileSetAtlasSource::_bind_methods() {
  3893. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TileSetAtlasSource::set_texture);
  3894. ClassDB::bind_method(D_METHOD("get_texture"), &TileSetAtlasSource::get_texture);
  3895. ClassDB::bind_method(D_METHOD("set_margins", "margins"), &TileSetAtlasSource::set_margins);
  3896. ClassDB::bind_method(D_METHOD("get_margins"), &TileSetAtlasSource::get_margins);
  3897. ClassDB::bind_method(D_METHOD("set_separation", "separation"), &TileSetAtlasSource::set_separation);
  3898. ClassDB::bind_method(D_METHOD("get_separation"), &TileSetAtlasSource::get_separation);
  3899. ClassDB::bind_method(D_METHOD("set_texture_region_size", "texture_region_size"), &TileSetAtlasSource::set_texture_region_size);
  3900. ClassDB::bind_method(D_METHOD("get_texture_region_size"), &TileSetAtlasSource::get_texture_region_size);
  3901. ClassDB::bind_method(D_METHOD("set_use_texture_padding", "use_texture_padding"), &TileSetAtlasSource::set_use_texture_padding);
  3902. ClassDB::bind_method(D_METHOD("get_use_texture_padding"), &TileSetAtlasSource::get_use_texture_padding);
  3903. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_NO_EDITOR), "set_texture", "get_texture");
  3904. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "margins", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NO_EDITOR), "set_margins", "get_margins");
  3905. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "separation", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NO_EDITOR), "set_separation", "get_separation");
  3906. 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");
  3907. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_texture_padding", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_use_texture_padding", "get_use_texture_padding");
  3908. // Base tiles
  3909. ClassDB::bind_method(D_METHOD("create_tile", "atlas_coords", "size"), &TileSetAtlasSource::create_tile, DEFVAL(Vector2i(1, 1)));
  3910. 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
  3911. 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)));
  3912. ClassDB::bind_method(D_METHOD("get_tile_size_in_atlas", "atlas_coords"), &TileSetAtlasSource::get_tile_size_in_atlas);
  3913. 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));
  3914. 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);
  3915. ClassDB::bind_method(D_METHOD("get_tile_at_coords", "atlas_coords"), &TileSetAtlasSource::get_tile_at_coords);
  3916. ClassDB::bind_method(D_METHOD("set_tile_animation_columns", "atlas_coords", "frame_columns"), &TileSetAtlasSource::set_tile_animation_columns);
  3917. ClassDB::bind_method(D_METHOD("get_tile_animation_columns", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_columns);
  3918. ClassDB::bind_method(D_METHOD("set_tile_animation_separation", "atlas_coords", "separation"), &TileSetAtlasSource::set_tile_animation_separation);
  3919. ClassDB::bind_method(D_METHOD("get_tile_animation_separation", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_separation);
  3920. ClassDB::bind_method(D_METHOD("set_tile_animation_speed", "atlas_coords", "speed"), &TileSetAtlasSource::set_tile_animation_speed);
  3921. ClassDB::bind_method(D_METHOD("get_tile_animation_speed", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_speed);
  3922. ClassDB::bind_method(D_METHOD("set_tile_animation_frames_count", "atlas_coords", "frames_count"), &TileSetAtlasSource::set_tile_animation_frames_count);
  3923. ClassDB::bind_method(D_METHOD("get_tile_animation_frames_count", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_frames_count);
  3924. ClassDB::bind_method(D_METHOD("set_tile_animation_frame_duration", "atlas_coords", "frame_index", "duration"), &TileSetAtlasSource::set_tile_animation_frame_duration);
  3925. ClassDB::bind_method(D_METHOD("get_tile_animation_frame_duration", "atlas_coords", "frame_index"), &TileSetAtlasSource::get_tile_animation_frame_duration);
  3926. ClassDB::bind_method(D_METHOD("get_tile_animation_total_duration", "atlas_coords"), &TileSetAtlasSource::get_tile_animation_total_duration);
  3927. // Alternative tiles
  3928. ClassDB::bind_method(D_METHOD("create_alternative_tile", "atlas_coords", "alternative_id_override"), &TileSetAtlasSource::create_alternative_tile, DEFVAL(INVALID_TILE_ALTERNATIVE));
  3929. ClassDB::bind_method(D_METHOD("remove_alternative_tile", "atlas_coords", "alternative_tile"), &TileSetAtlasSource::remove_alternative_tile);
  3930. ClassDB::bind_method(D_METHOD("set_alternative_tile_id", "atlas_coords", "alternative_tile", "new_id"), &TileSetAtlasSource::set_alternative_tile_id);
  3931. ClassDB::bind_method(D_METHOD("get_next_alternative_tile_id", "atlas_coords"), &TileSetAtlasSource::get_next_alternative_tile_id);
  3932. ClassDB::bind_method(D_METHOD("get_tile_data", "atlas_coords", "alternative_tile"), &TileSetAtlasSource::get_tile_data);
  3933. // Helpers.
  3934. ClassDB::bind_method(D_METHOD("get_atlas_grid_size"), &TileSetAtlasSource::get_atlas_grid_size);
  3935. ClassDB::bind_method(D_METHOD("get_tile_texture_region", "atlas_coords", "frame"), &TileSetAtlasSource::get_tile_texture_region, DEFVAL(0));
  3936. // Getters for texture and tile region (padded or not)
  3937. ClassDB::bind_method(D_METHOD("_update_padded_texture"), &TileSetAtlasSource::_update_padded_texture);
  3938. ClassDB::bind_method(D_METHOD("get_runtime_texture"), &TileSetAtlasSource::get_runtime_texture);
  3939. ClassDB::bind_method(D_METHOD("get_runtime_tile_texture_region", "atlas_coords", "frame"), &TileSetAtlasSource::get_runtime_tile_texture_region);
  3940. }
  3941. TileSetAtlasSource::~TileSetAtlasSource() {
  3942. // Free everything needed.
  3943. for (KeyValue<Vector2i, TileAlternativesData> &E_alternatives : tiles) {
  3944. for (KeyValue<int, TileData *> &E_tile_data : E_alternatives.value.alternatives) {
  3945. memdelete(E_tile_data.value);
  3946. }
  3947. }
  3948. }
  3949. TileData *TileSetAtlasSource::_get_atlas_tile_data(Vector2i p_atlas_coords, int p_alternative_tile) {
  3950. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3951. 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)));
  3952. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3953. }
  3954. const TileData *TileSetAtlasSource::_get_atlas_tile_data(Vector2i p_atlas_coords, int p_alternative_tile) const {
  3955. ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), nullptr, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3956. 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)));
  3957. return tiles[p_atlas_coords].alternatives[p_alternative_tile];
  3958. }
  3959. void TileSetAtlasSource::_compute_next_alternative_id(const Vector2i p_atlas_coords) {
  3960. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords)));
  3961. while (tiles[p_atlas_coords].alternatives.has(tiles[p_atlas_coords].next_alternative_id)) {
  3962. tiles[p_atlas_coords].next_alternative_id = (tiles[p_atlas_coords].next_alternative_id % 1073741823) + 1; // 2 ** 30
  3963. };
  3964. }
  3965. void TileSetAtlasSource::_clear_coords_mapping_cache(Vector2i p_atlas_coords) {
  3966. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3967. TileAlternativesData &tad = tiles[p_atlas_coords];
  3968. for (int frame = 0; frame < (int)tad.animation_frames_durations.size(); frame++) {
  3969. 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));
  3970. for (int x = 0; x < tad.size_in_atlas.x; x++) {
  3971. for (int y = 0; y < tad.size_in_atlas.y; y++) {
  3972. Vector2i coords = frame_coords + Vector2i(x, y);
  3973. if (!_coords_mapping_cache.has(coords)) {
  3974. WARN_PRINT(vformat("TileSetAtlasSource has no cached tile at position %s, the position cache might be corrupted.", coords));
  3975. } else {
  3976. if (_coords_mapping_cache[coords] != p_atlas_coords) {
  3977. WARN_PRINT(vformat("The position cache at position %s is pointing to a wrong tile, the position cache might be corrupted.", coords));
  3978. }
  3979. _coords_mapping_cache.erase(coords);
  3980. }
  3981. }
  3982. }
  3983. }
  3984. }
  3985. void TileSetAtlasSource::_create_coords_mapping_cache(Vector2i p_atlas_coords) {
  3986. ERR_FAIL_COND_MSG(!tiles.has(p_atlas_coords), vformat("TileSetAtlasSource has no tile at %s.", Vector2i(p_atlas_coords)));
  3987. TileAlternativesData &tad = tiles[p_atlas_coords];
  3988. for (int frame = 0; frame < (int)tad.animation_frames_durations.size(); frame++) {
  3989. 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));
  3990. for (int x = 0; x < tad.size_in_atlas.x; x++) {
  3991. for (int y = 0; y < tad.size_in_atlas.y; y++) {
  3992. Vector2i coords = frame_coords + Vector2i(x, y);
  3993. if (_coords_mapping_cache.has(coords)) {
  3994. WARN_PRINT(vformat("The cache already has a tile for position %s, the position cache might be corrupted.", coords));
  3995. }
  3996. _coords_mapping_cache[coords] = p_atlas_coords;
  3997. }
  3998. }
  3999. }
  4000. }
  4001. void TileSetAtlasSource::_clear_tiles_outside_texture() {
  4002. LocalVector<Vector2i> to_remove;
  4003. for (const KeyValue<Vector2i, TileSetAtlasSource::TileAlternativesData> &E : tiles) {
  4004. 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)) {
  4005. to_remove.push_back(E.key);
  4006. }
  4007. }
  4008. for (unsigned int i = 0; i < to_remove.size(); i++) {
  4009. remove_tile(to_remove[i]);
  4010. }
  4011. }
  4012. void TileSetAtlasSource::_queue_update_padded_texture() {
  4013. padded_texture_needs_update = true;
  4014. call_deferred(SNAME("_update_padded_texture"));
  4015. }
  4016. void TileSetAtlasSource::_update_padded_texture() {
  4017. if (!padded_texture_needs_update) {
  4018. return;
  4019. }
  4020. padded_texture_needs_update = false;
  4021. padded_texture = Ref<ImageTexture>();
  4022. if (!texture.is_valid()) {
  4023. return;
  4024. }
  4025. if (!use_texture_padding) {
  4026. return;
  4027. }
  4028. Size2 size = get_atlas_grid_size() * (texture_region_size + Vector2i(2, 2));
  4029. Ref<Image> src = texture->get_image();
  4030. if (!src.is_valid()) {
  4031. return;
  4032. }
  4033. Ref<Image> image;
  4034. image.instantiate();
  4035. image->create(size.x, size.y, false, src->get_format());
  4036. for (KeyValue<Vector2i, TileAlternativesData> kv : tiles) {
  4037. for (int frame = 0; frame < (int)kv.value.animation_frames_durations.size(); frame++) {
  4038. // Compute the source rects.
  4039. Rect2i src_rect = get_tile_texture_region(kv.key, frame);
  4040. Rect2i top_src_rect = Rect2i(src_rect.position, Vector2i(src_rect.size.x, 1));
  4041. Rect2i bottom_src_rect = Rect2i(src_rect.position + Vector2i(0, src_rect.size.y - 1), Vector2i(src_rect.size.x, 1));
  4042. Rect2i left_src_rect = Rect2i(src_rect.position, Vector2i(1, src_rect.size.y));
  4043. Rect2i right_src_rect = Rect2i(src_rect.position + Vector2i(src_rect.size.x - 1, 0), Vector2i(1, src_rect.size.y));
  4044. // Copy the tile and the paddings.
  4045. 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));
  4046. Vector2i base_pos = frame_coords * (texture_region_size + Vector2i(2, 2)) + Vector2i(1, 1);
  4047. image->blit_rect(*src, src_rect, base_pos);
  4048. image->blit_rect(*src, top_src_rect, base_pos + Vector2i(0, -1));
  4049. image->blit_rect(*src, bottom_src_rect, base_pos + Vector2i(0, src_rect.size.y));
  4050. image->blit_rect(*src, left_src_rect, base_pos + Vector2i(-1, 0));
  4051. image->blit_rect(*src, right_src_rect, base_pos + Vector2i(src_rect.size.x, 0));
  4052. image->set_pixelv(base_pos + Vector2i(-1, -1), src->get_pixelv(src_rect.position));
  4053. image->set_pixelv(base_pos + Vector2i(src_rect.size.x, -1), src->get_pixelv(src_rect.position + Vector2i(src_rect.size.x - 1, 0)));
  4054. image->set_pixelv(base_pos + Vector2i(-1, src_rect.size.y), src->get_pixelv(src_rect.position + Vector2i(0, src_rect.size.y - 1)));
  4055. image->set_pixelv(base_pos + Vector2i(src_rect.size.x, src_rect.size.y), src->get_pixelv(src_rect.position + Vector2i(src_rect.size.x - 1, src_rect.size.y - 1)));
  4056. }
  4057. }
  4058. if (!padded_texture.is_valid()) {
  4059. padded_texture.instantiate();
  4060. }
  4061. padded_texture->set_image(image);
  4062. emit_changed();
  4063. }
  4064. /////////////////////////////// TileSetScenesCollectionSource //////////////////////////////////////
  4065. void TileSetScenesCollectionSource::_compute_next_alternative_id() {
  4066. while (scenes.has(next_scene_id)) {
  4067. next_scene_id = (next_scene_id % 1073741823) + 1; // 2 ** 30
  4068. };
  4069. }
  4070. int TileSetScenesCollectionSource::get_tiles_count() const {
  4071. return 1;
  4072. }
  4073. Vector2i TileSetScenesCollectionSource::get_tile_id(int p_tile_index) const {
  4074. ERR_FAIL_COND_V(p_tile_index != 0, TileSetSource::INVALID_ATLAS_COORDS);
  4075. return Vector2i();
  4076. }
  4077. bool TileSetScenesCollectionSource::has_tile(Vector2i p_atlas_coords) const {
  4078. return p_atlas_coords == Vector2i();
  4079. }
  4080. int TileSetScenesCollectionSource::get_alternative_tiles_count(const Vector2i p_atlas_coords) const {
  4081. return scenes_ids.size();
  4082. }
  4083. int TileSetScenesCollectionSource::get_alternative_tile_id(const Vector2i p_atlas_coords, int p_index) const {
  4084. ERR_FAIL_COND_V(p_atlas_coords != Vector2i(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  4085. ERR_FAIL_INDEX_V(p_index, scenes_ids.size(), TileSetSource::INVALID_TILE_ALTERNATIVE);
  4086. return scenes_ids[p_index];
  4087. }
  4088. bool TileSetScenesCollectionSource::has_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_tile) const {
  4089. ERR_FAIL_COND_V(p_atlas_coords != Vector2i(), false);
  4090. return scenes.has(p_alternative_tile);
  4091. }
  4092. int TileSetScenesCollectionSource::create_scene_tile(Ref<PackedScene> p_packed_scene, int p_id_override) {
  4093. 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));
  4094. int new_scene_id = p_id_override >= 0 ? p_id_override : next_scene_id;
  4095. scenes[new_scene_id] = SceneData();
  4096. scenes_ids.append(new_scene_id);
  4097. scenes_ids.sort();
  4098. set_scene_tile_scene(new_scene_id, p_packed_scene);
  4099. _compute_next_alternative_id();
  4100. emit_signal(SNAME("changed"));
  4101. return new_scene_id;
  4102. }
  4103. void TileSetScenesCollectionSource::set_scene_tile_id(int p_id, int p_new_id) {
  4104. ERR_FAIL_COND(p_new_id < 0);
  4105. ERR_FAIL_COND(!has_scene_tile_id(p_id));
  4106. ERR_FAIL_COND(has_scene_tile_id(p_new_id));
  4107. scenes[p_new_id] = SceneData();
  4108. scenes[p_new_id] = scenes[p_id];
  4109. scenes_ids.append(p_new_id);
  4110. scenes_ids.sort();
  4111. _compute_next_alternative_id();
  4112. scenes.erase(p_id);
  4113. scenes_ids.erase(p_id);
  4114. emit_signal(SNAME("changed"));
  4115. }
  4116. void TileSetScenesCollectionSource::set_scene_tile_scene(int p_id, Ref<PackedScene> p_packed_scene) {
  4117. ERR_FAIL_COND(!scenes.has(p_id));
  4118. if (p_packed_scene.is_valid()) {
  4119. // Check if it extends CanvasItem.
  4120. Ref<SceneState> scene_state = p_packed_scene->get_state();
  4121. String type;
  4122. while (scene_state.is_valid() && type.is_empty()) {
  4123. // Make sure we have a root node. Supposed to be at 0 index because find_node_by_path() does not seem to work.
  4124. ERR_FAIL_COND(scene_state->get_node_count() < 1);
  4125. type = scene_state->get_node_type(0);
  4126. scene_state = scene_state->get_base_scene_state();
  4127. }
  4128. ERR_FAIL_COND_MSG(type.is_empty(), vformat("Invalid PackedScene for TileSetScenesCollectionSource: %s. Could not get the type of the root node.", p_packed_scene->get_path()));
  4129. bool extends_correct_class = ClassDB::is_parent_class(type, "Control") || ClassDB::is_parent_class(type, "Node2D");
  4130. ERR_FAIL_COND_MSG(!extends_correct_class, vformat("Invalid PackedScene for TileSetScenesCollectionSource: %s. Root node should extend Control or Node2D. Found %s instead.", p_packed_scene->get_path(), type));
  4131. scenes[p_id].scene = p_packed_scene;
  4132. } else {
  4133. scenes[p_id].scene = Ref<PackedScene>();
  4134. }
  4135. emit_signal(SNAME("changed"));
  4136. }
  4137. Ref<PackedScene> TileSetScenesCollectionSource::get_scene_tile_scene(int p_id) const {
  4138. ERR_FAIL_COND_V(!scenes.has(p_id), Ref<PackedScene>());
  4139. return scenes[p_id].scene;
  4140. }
  4141. void TileSetScenesCollectionSource::set_scene_tile_display_placeholder(int p_id, bool p_display_placeholder) {
  4142. ERR_FAIL_COND(!scenes.has(p_id));
  4143. scenes[p_id].display_placeholder = p_display_placeholder;
  4144. emit_signal(SNAME("changed"));
  4145. }
  4146. bool TileSetScenesCollectionSource::get_scene_tile_display_placeholder(int p_id) const {
  4147. ERR_FAIL_COND_V(!scenes.has(p_id), false);
  4148. return scenes[p_id].display_placeholder;
  4149. }
  4150. void TileSetScenesCollectionSource::remove_scene_tile(int p_id) {
  4151. ERR_FAIL_COND(!scenes.has(p_id));
  4152. scenes.erase(p_id);
  4153. scenes_ids.erase(p_id);
  4154. emit_signal(SNAME("changed"));
  4155. }
  4156. int TileSetScenesCollectionSource::get_next_scene_tile_id() const {
  4157. return next_scene_id;
  4158. }
  4159. bool TileSetScenesCollectionSource::_set(const StringName &p_name, const Variant &p_value) {
  4160. Vector<String> components = String(p_name).split("/", true, 2);
  4161. if (components.size() >= 2 && components[0] == "scenes" && components[1].is_valid_int()) {
  4162. int scene_id = components[1].to_int();
  4163. if (components.size() >= 3 && components[2] == "scene") {
  4164. if (has_scene_tile_id(scene_id)) {
  4165. set_scene_tile_scene(scene_id, p_value);
  4166. } else {
  4167. create_scene_tile(p_value, scene_id);
  4168. }
  4169. return true;
  4170. } else if (components.size() >= 3 && components[2] == "display_placeholder") {
  4171. if (!has_scene_tile_id(scene_id)) {
  4172. create_scene_tile(p_value, scene_id);
  4173. }
  4174. return true;
  4175. }
  4176. }
  4177. return false;
  4178. }
  4179. bool TileSetScenesCollectionSource::_get(const StringName &p_name, Variant &r_ret) const {
  4180. Vector<String> components = String(p_name).split("/", true, 2);
  4181. if (components.size() >= 2 && components[0] == "scenes" && components[1].is_valid_int() && scenes.has(components[1].to_int())) {
  4182. if (components.size() >= 3 && components[2] == "scene") {
  4183. r_ret = scenes[components[1].to_int()].scene;
  4184. return true;
  4185. } else if (components.size() >= 3 && components[2] == "display_placeholder") {
  4186. r_ret = scenes[components[1].to_int()].scene;
  4187. return true;
  4188. }
  4189. }
  4190. return false;
  4191. }
  4192. void TileSetScenesCollectionSource::_get_property_list(List<PropertyInfo> *p_list) const {
  4193. for (int i = 0; i < scenes_ids.size(); i++) {
  4194. p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("scenes/%d/scene", scenes_ids[i]), PROPERTY_HINT_RESOURCE_TYPE, "TileSetScenesCollectionSource"));
  4195. PropertyInfo property_info = PropertyInfo(Variant::BOOL, vformat("scenes/%d/display_placeholder", scenes_ids[i]));
  4196. if (scenes[scenes_ids[i]].display_placeholder == false) {
  4197. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4198. }
  4199. p_list->push_back(property_info);
  4200. }
  4201. }
  4202. void TileSetScenesCollectionSource::_bind_methods() {
  4203. ClassDB::bind_method(D_METHOD("get_scene_tiles_count"), &TileSetScenesCollectionSource::get_scene_tiles_count);
  4204. ClassDB::bind_method(D_METHOD("get_scene_tile_id", "index"), &TileSetScenesCollectionSource::get_scene_tile_id);
  4205. ClassDB::bind_method(D_METHOD("has_scene_tile_id", "id"), &TileSetScenesCollectionSource::has_scene_tile_id);
  4206. ClassDB::bind_method(D_METHOD("create_scene_tile", "packed_scene", "id_override"), &TileSetScenesCollectionSource::create_scene_tile, DEFVAL(INVALID_TILE_ALTERNATIVE));
  4207. ClassDB::bind_method(D_METHOD("set_scene_tile_id", "id", "new_id"), &TileSetScenesCollectionSource::set_scene_tile_id);
  4208. ClassDB::bind_method(D_METHOD("set_scene_tile_scene", "id", "packed_scene"), &TileSetScenesCollectionSource::set_scene_tile_scene);
  4209. ClassDB::bind_method(D_METHOD("get_scene_tile_scene", "id"), &TileSetScenesCollectionSource::get_scene_tile_scene);
  4210. ClassDB::bind_method(D_METHOD("set_scene_tile_display_placeholder", "id", "display_placeholder"), &TileSetScenesCollectionSource::set_scene_tile_display_placeholder);
  4211. ClassDB::bind_method(D_METHOD("get_scene_tile_display_placeholder", "id"), &TileSetScenesCollectionSource::get_scene_tile_display_placeholder);
  4212. ClassDB::bind_method(D_METHOD("remove_scene_tile", "id"), &TileSetScenesCollectionSource::remove_scene_tile);
  4213. ClassDB::bind_method(D_METHOD("get_next_scene_tile_id"), &TileSetScenesCollectionSource::get_next_scene_tile_id);
  4214. }
  4215. /////////////////////////////// TileData //////////////////////////////////////
  4216. void TileData::set_tile_set(const TileSet *p_tile_set) {
  4217. tile_set = p_tile_set;
  4218. notify_tile_data_properties_should_change();
  4219. }
  4220. void TileData::notify_tile_data_properties_should_change() {
  4221. if (!tile_set) {
  4222. return;
  4223. }
  4224. occluders.resize(tile_set->get_occlusion_layers_count());
  4225. physics.resize(tile_set->get_physics_layers_count());
  4226. for (int bit_index = 0; bit_index < 16; bit_index++) {
  4227. if (terrain_set < 0 || terrain_peering_bits[bit_index] >= tile_set->get_terrains_count(terrain_set)) {
  4228. terrain_peering_bits[bit_index] = -1;
  4229. }
  4230. }
  4231. navigation.resize(tile_set->get_navigation_layers_count());
  4232. // Convert custom data to the new type.
  4233. custom_data.resize(tile_set->get_custom_data_layers_count());
  4234. for (int i = 0; i < custom_data.size(); i++) {
  4235. if (custom_data[i].get_type() != tile_set->get_custom_data_layer_type(i)) {
  4236. Variant new_val;
  4237. Callable::CallError error;
  4238. if (Variant::can_convert(custom_data[i].get_type(), tile_set->get_custom_data_layer_type(i))) {
  4239. const Variant *args[] = { &custom_data[i] };
  4240. Variant::construct(tile_set->get_custom_data_layer_type(i), new_val, args, 1, error);
  4241. } else {
  4242. Variant::construct(tile_set->get_custom_data_layer_type(i), new_val, nullptr, 0, error);
  4243. }
  4244. custom_data.write[i] = new_val;
  4245. }
  4246. }
  4247. notify_property_list_changed();
  4248. emit_signal(SNAME("changed"));
  4249. }
  4250. void TileData::add_occlusion_layer(int p_to_pos) {
  4251. if (p_to_pos < 0) {
  4252. p_to_pos = occluders.size();
  4253. }
  4254. ERR_FAIL_INDEX(p_to_pos, occluders.size() + 1);
  4255. occluders.insert(p_to_pos, Ref<OccluderPolygon2D>());
  4256. }
  4257. void TileData::move_occlusion_layer(int p_from_index, int p_to_pos) {
  4258. ERR_FAIL_INDEX(p_from_index, occluders.size());
  4259. ERR_FAIL_INDEX(p_to_pos, occluders.size() + 1);
  4260. occluders.insert(p_to_pos, occluders[p_from_index]);
  4261. occluders.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4262. }
  4263. void TileData::remove_occlusion_layer(int p_index) {
  4264. ERR_FAIL_INDEX(p_index, occluders.size());
  4265. occluders.remove_at(p_index);
  4266. }
  4267. void TileData::add_physics_layer(int p_to_pos) {
  4268. if (p_to_pos < 0) {
  4269. p_to_pos = physics.size();
  4270. }
  4271. ERR_FAIL_INDEX(p_to_pos, physics.size() + 1);
  4272. physics.insert(p_to_pos, PhysicsLayerTileData());
  4273. }
  4274. void TileData::move_physics_layer(int p_from_index, int p_to_pos) {
  4275. ERR_FAIL_INDEX(p_from_index, physics.size());
  4276. ERR_FAIL_INDEX(p_to_pos, physics.size() + 1);
  4277. physics.insert(p_to_pos, physics[p_from_index]);
  4278. physics.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4279. }
  4280. void TileData::remove_physics_layer(int p_index) {
  4281. ERR_FAIL_INDEX(p_index, physics.size());
  4282. physics.remove_at(p_index);
  4283. }
  4284. void TileData::add_terrain_set(int p_to_pos) {
  4285. if (p_to_pos >= 0 && p_to_pos <= terrain_set) {
  4286. terrain_set += 1;
  4287. }
  4288. }
  4289. void TileData::move_terrain_set(int p_from_index, int p_to_pos) {
  4290. if (p_from_index == terrain_set) {
  4291. terrain_set = (p_from_index < p_to_pos) ? p_to_pos - 1 : p_to_pos;
  4292. } else {
  4293. if (p_from_index < terrain_set) {
  4294. terrain_set -= 1;
  4295. }
  4296. if (p_to_pos <= terrain_set) {
  4297. terrain_set += 1;
  4298. }
  4299. }
  4300. }
  4301. void TileData::remove_terrain_set(int p_index) {
  4302. if (p_index == terrain_set) {
  4303. terrain_set = -1;
  4304. for (int i = 0; i < 16; i++) {
  4305. terrain_peering_bits[i] = -1;
  4306. }
  4307. } else if (terrain_set > p_index) {
  4308. terrain_set -= 1;
  4309. }
  4310. }
  4311. void TileData::add_terrain(int p_terrain_set, int p_to_pos) {
  4312. if (terrain_set == p_terrain_set) {
  4313. for (int i = 0; i < 16; i++) {
  4314. if (p_to_pos >= 0 && p_to_pos <= terrain_peering_bits[i]) {
  4315. terrain_peering_bits[i] += 1;
  4316. }
  4317. }
  4318. }
  4319. }
  4320. void TileData::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) {
  4321. if (terrain_set == p_terrain_set) {
  4322. for (int i = 0; i < 16; i++) {
  4323. if (p_from_index == terrain_peering_bits[i]) {
  4324. terrain_peering_bits[i] = (p_from_index < p_to_pos) ? p_to_pos - 1 : p_to_pos;
  4325. } else {
  4326. if (p_from_index < terrain_peering_bits[i]) {
  4327. terrain_peering_bits[i] -= 1;
  4328. }
  4329. if (p_to_pos <= terrain_peering_bits[i]) {
  4330. terrain_peering_bits[i] += 1;
  4331. }
  4332. }
  4333. }
  4334. }
  4335. }
  4336. void TileData::remove_terrain(int p_terrain_set, int p_index) {
  4337. if (terrain_set == p_terrain_set) {
  4338. if (terrain == p_index) {
  4339. terrain = -1;
  4340. }
  4341. for (int i = 0; i < 16; i++) {
  4342. if (terrain_peering_bits[i] == p_index) {
  4343. terrain_peering_bits[i] = -1;
  4344. } else if (terrain_peering_bits[i] > p_index) {
  4345. terrain_peering_bits[i] -= 1;
  4346. }
  4347. }
  4348. }
  4349. }
  4350. void TileData::add_navigation_layer(int p_to_pos) {
  4351. if (p_to_pos < 0) {
  4352. p_to_pos = navigation.size();
  4353. }
  4354. ERR_FAIL_INDEX(p_to_pos, navigation.size() + 1);
  4355. navigation.insert(p_to_pos, Ref<NavigationPolygon>());
  4356. }
  4357. void TileData::move_navigation_layer(int p_from_index, int p_to_pos) {
  4358. ERR_FAIL_INDEX(p_from_index, navigation.size());
  4359. ERR_FAIL_INDEX(p_to_pos, navigation.size() + 1);
  4360. navigation.insert(p_to_pos, navigation[p_from_index]);
  4361. navigation.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4362. }
  4363. void TileData::remove_navigation_layer(int p_index) {
  4364. ERR_FAIL_INDEX(p_index, navigation.size());
  4365. navigation.remove_at(p_index);
  4366. }
  4367. void TileData::add_custom_data_layer(int p_to_pos) {
  4368. if (p_to_pos < 0) {
  4369. p_to_pos = custom_data.size();
  4370. }
  4371. ERR_FAIL_INDEX(p_to_pos, custom_data.size() + 1);
  4372. custom_data.insert(p_to_pos, Variant());
  4373. }
  4374. void TileData::move_custom_data_layer(int p_from_index, int p_to_pos) {
  4375. ERR_FAIL_INDEX(p_from_index, custom_data.size());
  4376. ERR_FAIL_INDEX(p_to_pos, custom_data.size() + 1);
  4377. custom_data.insert(p_to_pos, navigation[p_from_index]);
  4378. custom_data.remove_at(p_to_pos < p_from_index ? p_from_index + 1 : p_from_index);
  4379. }
  4380. void TileData::remove_custom_data_layer(int p_index) {
  4381. ERR_FAIL_INDEX(p_index, custom_data.size());
  4382. custom_data.remove_at(p_index);
  4383. }
  4384. void TileData::set_allow_transform(bool p_allow_transform) {
  4385. allow_transform = p_allow_transform;
  4386. }
  4387. bool TileData::is_allowing_transform() const {
  4388. return allow_transform;
  4389. }
  4390. TileData *TileData::duplicate() {
  4391. TileData *output = memnew(TileData);
  4392. output->tile_set = tile_set;
  4393. output->allow_transform = allow_transform;
  4394. // Rendering
  4395. output->flip_h = flip_h;
  4396. output->flip_v = flip_v;
  4397. output->transpose = transpose;
  4398. output->tex_offset = tex_offset;
  4399. output->material = material;
  4400. output->modulate = modulate;
  4401. output->z_index = z_index;
  4402. output->y_sort_origin = y_sort_origin;
  4403. output->occluders = occluders;
  4404. // Physics
  4405. output->physics = physics;
  4406. // Terrain
  4407. output->terrain_set = -1;
  4408. memcpy(output->terrain_peering_bits, terrain_peering_bits, 16 * sizeof(int));
  4409. // Navigation
  4410. output->navigation = navigation;
  4411. // Misc
  4412. output->probability = probability;
  4413. // Custom data
  4414. output->custom_data = custom_data;
  4415. return output;
  4416. }
  4417. // Rendering
  4418. void TileData::set_flip_h(bool p_flip_h) {
  4419. ERR_FAIL_COND_MSG(!allow_transform && p_flip_h, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  4420. flip_h = p_flip_h;
  4421. emit_signal(SNAME("changed"));
  4422. }
  4423. bool TileData::get_flip_h() const {
  4424. return flip_h;
  4425. }
  4426. void TileData::set_flip_v(bool p_flip_v) {
  4427. ERR_FAIL_COND_MSG(!allow_transform && p_flip_v, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  4428. flip_v = p_flip_v;
  4429. emit_signal(SNAME("changed"));
  4430. }
  4431. bool TileData::get_flip_v() const {
  4432. return flip_v;
  4433. }
  4434. void TileData::set_transpose(bool p_transpose) {
  4435. ERR_FAIL_COND_MSG(!allow_transform && p_transpose, "Transform is only allowed for alternative tiles (with its alternative_id != 0)");
  4436. transpose = p_transpose;
  4437. emit_signal(SNAME("changed"));
  4438. }
  4439. bool TileData::get_transpose() const {
  4440. return transpose;
  4441. }
  4442. void TileData::set_texture_offset(Vector2i p_texture_offset) {
  4443. tex_offset = p_texture_offset;
  4444. emit_signal(SNAME("changed"));
  4445. }
  4446. Vector2i TileData::get_texture_offset() const {
  4447. return tex_offset;
  4448. }
  4449. void TileData::set_material(Ref<Material> p_material) {
  4450. material = p_material;
  4451. emit_signal(SNAME("changed"));
  4452. }
  4453. Ref<Material> TileData::get_material() const {
  4454. return material;
  4455. }
  4456. void TileData::set_modulate(Color p_modulate) {
  4457. modulate = p_modulate;
  4458. emit_signal(SNAME("changed"));
  4459. }
  4460. Color TileData::get_modulate() const {
  4461. return modulate;
  4462. }
  4463. void TileData::set_z_index(int p_z_index) {
  4464. z_index = p_z_index;
  4465. emit_signal(SNAME("changed"));
  4466. }
  4467. int TileData::get_z_index() const {
  4468. return z_index;
  4469. }
  4470. void TileData::set_y_sort_origin(int p_y_sort_origin) {
  4471. y_sort_origin = p_y_sort_origin;
  4472. emit_signal(SNAME("changed"));
  4473. }
  4474. int TileData::get_y_sort_origin() const {
  4475. return y_sort_origin;
  4476. }
  4477. void TileData::set_occluder(int p_layer_id, Ref<OccluderPolygon2D> p_occluder_polygon) {
  4478. ERR_FAIL_INDEX(p_layer_id, occluders.size());
  4479. occluders.write[p_layer_id] = p_occluder_polygon;
  4480. emit_signal(SNAME("changed"));
  4481. }
  4482. Ref<OccluderPolygon2D> TileData::get_occluder(int p_layer_id) const {
  4483. ERR_FAIL_INDEX_V(p_layer_id, occluders.size(), Ref<OccluderPolygon2D>());
  4484. return occluders[p_layer_id];
  4485. }
  4486. // Physics
  4487. void TileData::set_constant_linear_velocity(int p_layer_id, const Vector2 &p_velocity) {
  4488. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4489. physics.write[p_layer_id].linear_velocity = p_velocity;
  4490. emit_signal(SNAME("changed"));
  4491. }
  4492. Vector2 TileData::get_constant_linear_velocity(int p_layer_id) const {
  4493. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Vector2());
  4494. return physics[p_layer_id].linear_velocity;
  4495. }
  4496. void TileData::set_constant_angular_velocity(int p_layer_id, real_t p_velocity) {
  4497. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4498. physics.write[p_layer_id].angular_velocity = p_velocity;
  4499. emit_signal(SNAME("changed"));
  4500. }
  4501. real_t TileData::get_constant_angular_velocity(int p_layer_id) const {
  4502. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0.0);
  4503. return physics[p_layer_id].angular_velocity;
  4504. }
  4505. void TileData::set_collision_polygons_count(int p_layer_id, int p_polygons_count) {
  4506. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4507. ERR_FAIL_COND(p_polygons_count < 0);
  4508. if (p_polygons_count == physics.write[p_layer_id].polygons.size()) {
  4509. return;
  4510. }
  4511. physics.write[p_layer_id].polygons.resize(p_polygons_count);
  4512. notify_property_list_changed();
  4513. emit_signal(SNAME("changed"));
  4514. }
  4515. int TileData::get_collision_polygons_count(int p_layer_id) const {
  4516. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0);
  4517. return physics[p_layer_id].polygons.size();
  4518. }
  4519. void TileData::add_collision_polygon(int p_layer_id) {
  4520. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4521. physics.write[p_layer_id].polygons.push_back(PhysicsLayerTileData::PolygonShapeTileData());
  4522. emit_signal(SNAME("changed"));
  4523. }
  4524. void TileData::remove_collision_polygon(int p_layer_id, int p_polygon_index) {
  4525. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4526. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4527. physics.write[p_layer_id].polygons.remove_at(p_polygon_index);
  4528. emit_signal(SNAME("changed"));
  4529. }
  4530. void TileData::set_collision_polygon_points(int p_layer_id, int p_polygon_index, Vector<Vector2> p_polygon) {
  4531. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4532. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4533. ERR_FAIL_COND_MSG(p_polygon.size() != 0 && p_polygon.size() < 3, "Invalid polygon. Needs either 0 or more than 3 points.");
  4534. if (p_polygon.is_empty()) {
  4535. physics.write[p_layer_id].polygons.write[p_polygon_index].shapes.clear();
  4536. } else {
  4537. // Decompose into convex shapes.
  4538. Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(p_polygon);
  4539. ERR_FAIL_COND_MSG(decomp.is_empty(), "Could not decompose the polygon into convex shapes.");
  4540. physics.write[p_layer_id].polygons.write[p_polygon_index].shapes.resize(decomp.size());
  4541. for (int i = 0; i < decomp.size(); i++) {
  4542. Ref<ConvexPolygonShape2D> shape;
  4543. shape.instantiate();
  4544. shape->set_points(decomp[i]);
  4545. physics.write[p_layer_id].polygons.write[p_polygon_index].shapes[i] = shape;
  4546. }
  4547. }
  4548. physics.write[p_layer_id].polygons.write[p_polygon_index].polygon = p_polygon;
  4549. emit_signal(SNAME("changed"));
  4550. }
  4551. Vector<Vector2> TileData::get_collision_polygon_points(int p_layer_id, int p_polygon_index) const {
  4552. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Vector<Vector2>());
  4553. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), Vector<Vector2>());
  4554. return physics[p_layer_id].polygons[p_polygon_index].polygon;
  4555. }
  4556. void TileData::set_collision_polygon_one_way(int p_layer_id, int p_polygon_index, bool p_one_way) {
  4557. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4558. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4559. physics.write[p_layer_id].polygons.write[p_polygon_index].one_way = p_one_way;
  4560. emit_signal(SNAME("changed"));
  4561. }
  4562. bool TileData::is_collision_polygon_one_way(int p_layer_id, int p_polygon_index) const {
  4563. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), false);
  4564. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), false);
  4565. return physics[p_layer_id].polygons[p_polygon_index].one_way;
  4566. }
  4567. void TileData::set_collision_polygon_one_way_margin(int p_layer_id, int p_polygon_index, float p_one_way_margin) {
  4568. ERR_FAIL_INDEX(p_layer_id, physics.size());
  4569. ERR_FAIL_INDEX(p_polygon_index, physics[p_layer_id].polygons.size());
  4570. physics.write[p_layer_id].polygons.write[p_polygon_index].one_way_margin = p_one_way_margin;
  4571. emit_signal(SNAME("changed"));
  4572. }
  4573. float TileData::get_collision_polygon_one_way_margin(int p_layer_id, int p_polygon_index) const {
  4574. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0.0);
  4575. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), 0.0);
  4576. return physics[p_layer_id].polygons[p_polygon_index].one_way_margin;
  4577. }
  4578. int TileData::get_collision_polygon_shapes_count(int p_layer_id, int p_polygon_index) const {
  4579. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), 0);
  4580. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), 0);
  4581. return physics[p_layer_id].polygons[p_polygon_index].shapes.size();
  4582. }
  4583. Ref<ConvexPolygonShape2D> TileData::get_collision_polygon_shape(int p_layer_id, int p_polygon_index, int shape_index) const {
  4584. ERR_FAIL_INDEX_V(p_layer_id, physics.size(), Ref<ConvexPolygonShape2D>());
  4585. ERR_FAIL_INDEX_V(p_polygon_index, physics[p_layer_id].polygons.size(), Ref<ConvexPolygonShape2D>());
  4586. ERR_FAIL_INDEX_V(shape_index, (int)physics[p_layer_id].polygons[p_polygon_index].shapes.size(), Ref<ConvexPolygonShape2D>());
  4587. return physics[p_layer_id].polygons[p_polygon_index].shapes[shape_index];
  4588. }
  4589. // Terrain
  4590. void TileData::set_terrain_set(int p_terrain_set) {
  4591. ERR_FAIL_COND(p_terrain_set < -1);
  4592. if (p_terrain_set == terrain_set) {
  4593. return;
  4594. }
  4595. if (tile_set) {
  4596. ERR_FAIL_COND(p_terrain_set >= tile_set->get_terrain_sets_count());
  4597. for (int i = 0; i < 16; i++) {
  4598. terrain_peering_bits[i] = -1;
  4599. }
  4600. }
  4601. terrain_set = p_terrain_set;
  4602. notify_property_list_changed();
  4603. emit_signal(SNAME("changed"));
  4604. }
  4605. int TileData::get_terrain_set() const {
  4606. return terrain_set;
  4607. }
  4608. void TileData::set_terrain(int p_terrain) {
  4609. ERR_FAIL_COND(terrain_set < 0);
  4610. ERR_FAIL_COND(p_terrain < -1);
  4611. if (tile_set) {
  4612. ERR_FAIL_COND(p_terrain >= tile_set->get_terrains_count(terrain_set));
  4613. }
  4614. terrain = p_terrain;
  4615. emit_signal(SNAME("changed"));
  4616. }
  4617. int TileData::get_terrain() const {
  4618. return terrain;
  4619. }
  4620. void TileData::set_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit, int p_terrain_index) {
  4621. ERR_FAIL_INDEX(p_peering_bit, TileSet::CellNeighbor::CELL_NEIGHBOR_MAX);
  4622. ERR_FAIL_COND(terrain_set < 0);
  4623. ERR_FAIL_COND(p_terrain_index < -1);
  4624. if (tile_set) {
  4625. ERR_FAIL_COND(p_terrain_index >= tile_set->get_terrains_count(terrain_set));
  4626. ERR_FAIL_COND(!is_valid_terrain_peering_bit(p_peering_bit));
  4627. }
  4628. terrain_peering_bits[p_peering_bit] = p_terrain_index;
  4629. emit_signal(SNAME("changed"));
  4630. }
  4631. int TileData::get_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit) const {
  4632. ERR_FAIL_COND_V(!is_valid_terrain_peering_bit(p_peering_bit), -1);
  4633. return terrain_peering_bits[p_peering_bit];
  4634. }
  4635. bool TileData::is_valid_terrain_peering_bit(TileSet::CellNeighbor p_peering_bit) const {
  4636. ERR_FAIL_COND_V(!tile_set, false);
  4637. return tile_set->is_valid_terrain_peering_bit(terrain_set, p_peering_bit);
  4638. }
  4639. TileSet::TerrainsPattern TileData::get_terrains_pattern() const {
  4640. ERR_FAIL_COND_V(!tile_set, TileSet::TerrainsPattern());
  4641. TileSet::TerrainsPattern output(tile_set, terrain_set);
  4642. output.set_terrain(terrain);
  4643. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4644. if (tile_set->is_valid_terrain_peering_bit(terrain_set, TileSet::CellNeighbor(i))) {
  4645. output.set_terrain_peering_bit(TileSet::CellNeighbor(i), get_terrain_peering_bit(TileSet::CellNeighbor(i)));
  4646. }
  4647. }
  4648. return output;
  4649. }
  4650. // Navigation
  4651. void TileData::set_navigation_polygon(int p_layer_id, Ref<NavigationPolygon> p_navigation_polygon) {
  4652. ERR_FAIL_INDEX(p_layer_id, navigation.size());
  4653. navigation.write[p_layer_id] = p_navigation_polygon;
  4654. emit_signal(SNAME("changed"));
  4655. }
  4656. Ref<NavigationPolygon> TileData::get_navigation_polygon(int p_layer_id) const {
  4657. ERR_FAIL_INDEX_V(p_layer_id, navigation.size(), Ref<NavigationPolygon>());
  4658. return navigation[p_layer_id];
  4659. }
  4660. // Misc
  4661. void TileData::set_probability(float p_probability) {
  4662. ERR_FAIL_COND(p_probability < 0.0);
  4663. probability = p_probability;
  4664. emit_signal(SNAME("changed"));
  4665. }
  4666. float TileData::get_probability() const {
  4667. return probability;
  4668. }
  4669. // Custom data
  4670. void TileData::set_custom_data(String p_layer_name, Variant p_value) {
  4671. ERR_FAIL_COND(!tile_set);
  4672. int p_layer_id = tile_set->get_custom_data_layer_by_name(p_layer_name);
  4673. ERR_FAIL_COND_MSG(p_layer_id < 0, vformat("TileSet has no layer with name: %s", p_layer_name));
  4674. set_custom_data_by_layer_id(p_layer_id, p_value);
  4675. }
  4676. Variant TileData::get_custom_data(String p_layer_name) const {
  4677. ERR_FAIL_COND_V(!tile_set, Variant());
  4678. int p_layer_id = tile_set->get_custom_data_layer_by_name(p_layer_name);
  4679. ERR_FAIL_COND_V_MSG(p_layer_id < 0, Variant(), vformat("TileSet has no layer with name: %s", p_layer_name));
  4680. return get_custom_data_by_layer_id(p_layer_id);
  4681. }
  4682. void TileData::set_custom_data_by_layer_id(int p_layer_id, Variant p_value) {
  4683. ERR_FAIL_INDEX(p_layer_id, custom_data.size());
  4684. custom_data.write[p_layer_id] = p_value;
  4685. emit_signal(SNAME("changed"));
  4686. }
  4687. Variant TileData::get_custom_data_by_layer_id(int p_layer_id) const {
  4688. ERR_FAIL_INDEX_V(p_layer_id, custom_data.size(), Variant());
  4689. return custom_data[p_layer_id];
  4690. }
  4691. bool TileData::_set(const StringName &p_name, const Variant &p_value) {
  4692. Vector<String> components = String(p_name).split("/", true, 2);
  4693. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  4694. // Occlusion layers.
  4695. int layer_index = components[0].trim_prefix("occlusion_layer_").to_int();
  4696. ERR_FAIL_COND_V(layer_index < 0, false);
  4697. if (components[1] == "polygon") {
  4698. Ref<OccluderPolygon2D> polygon = p_value;
  4699. if (layer_index >= occluders.size()) {
  4700. if (tile_set) {
  4701. return false;
  4702. } else {
  4703. occluders.resize(layer_index + 1);
  4704. }
  4705. }
  4706. set_occluder(layer_index, polygon);
  4707. return true;
  4708. }
  4709. } else if (components.size() >= 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  4710. // Physics layers.
  4711. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  4712. ERR_FAIL_COND_V(layer_index < 0, false);
  4713. if (components.size() == 2) {
  4714. if (layer_index >= physics.size()) {
  4715. if (tile_set) {
  4716. return false;
  4717. } else {
  4718. physics.resize(layer_index + 1);
  4719. }
  4720. }
  4721. if (components[1] == "linear_velocity") {
  4722. set_constant_linear_velocity(layer_index, p_value);
  4723. return true;
  4724. } else if (components[1] == "angular_velocity") {
  4725. set_constant_angular_velocity(layer_index, p_value);
  4726. return true;
  4727. } else if (components[1] == "polygons_count") {
  4728. if (p_value.get_type() != Variant::INT) {
  4729. return false;
  4730. }
  4731. set_collision_polygons_count(layer_index, p_value);
  4732. return true;
  4733. }
  4734. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  4735. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  4736. ERR_FAIL_COND_V(polygon_index < 0, false);
  4737. if (components[2] == "points" || components[2] == "one_way" || components[2] == "one_way_margin") {
  4738. if (layer_index >= physics.size()) {
  4739. if (tile_set) {
  4740. return false;
  4741. } else {
  4742. physics.resize(layer_index + 1);
  4743. }
  4744. }
  4745. if (polygon_index >= physics[layer_index].polygons.size()) {
  4746. physics.write[layer_index].polygons.resize(polygon_index + 1);
  4747. }
  4748. }
  4749. if (components[2] == "points") {
  4750. Vector<Vector2> polygon = p_value;
  4751. set_collision_polygon_points(layer_index, polygon_index, polygon);
  4752. return true;
  4753. } else if (components[2] == "one_way") {
  4754. set_collision_polygon_one_way(layer_index, polygon_index, p_value);
  4755. return true;
  4756. } else if (components[2] == "one_way_margin") {
  4757. set_collision_polygon_one_way_margin(layer_index, polygon_index, p_value);
  4758. return true;
  4759. }
  4760. }
  4761. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  4762. // Navigation layers.
  4763. int layer_index = components[0].trim_prefix("navigation_layer_").to_int();
  4764. ERR_FAIL_COND_V(layer_index < 0, false);
  4765. if (components[1] == "polygon") {
  4766. Ref<NavigationPolygon> polygon = p_value;
  4767. if (layer_index >= navigation.size()) {
  4768. if (tile_set) {
  4769. return false;
  4770. } else {
  4771. navigation.resize(layer_index + 1);
  4772. }
  4773. }
  4774. set_navigation_polygon(layer_index, polygon);
  4775. return true;
  4776. }
  4777. } else if (components.size() == 2 && components[0] == "terrains_peering_bit") {
  4778. // Terrains.
  4779. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4780. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  4781. if (components[1] == TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]) {
  4782. set_terrain_peering_bit(bit, p_value);
  4783. return true;
  4784. }
  4785. }
  4786. return false;
  4787. } else if (components.size() == 1 && components[0].begins_with("custom_data_") && components[0].trim_prefix("custom_data_").is_valid_int()) {
  4788. // Custom data layers.
  4789. int layer_index = components[0].trim_prefix("custom_data_").to_int();
  4790. ERR_FAIL_COND_V(layer_index < 0, false);
  4791. if (layer_index >= custom_data.size()) {
  4792. if (tile_set) {
  4793. return false;
  4794. } else {
  4795. custom_data.resize(layer_index + 1);
  4796. }
  4797. }
  4798. set_custom_data_by_layer_id(layer_index, p_value);
  4799. return true;
  4800. }
  4801. return false;
  4802. }
  4803. bool TileData::_get(const StringName &p_name, Variant &r_ret) const {
  4804. Vector<String> components = String(p_name).split("/", true, 2);
  4805. if (tile_set) {
  4806. if (components.size() == 2 && components[0].begins_with("occlusion_layer") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  4807. // Occlusion layers.
  4808. int layer_index = components[0].trim_prefix("occlusion_layer_").to_int();
  4809. ERR_FAIL_COND_V(layer_index < 0, false);
  4810. if (layer_index >= occluders.size()) {
  4811. return false;
  4812. }
  4813. if (components[1] == "polygon") {
  4814. r_ret = get_occluder(layer_index);
  4815. return true;
  4816. }
  4817. } else if (components.size() >= 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  4818. // Physics layers.
  4819. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  4820. ERR_FAIL_COND_V(layer_index < 0, false);
  4821. if (layer_index >= physics.size()) {
  4822. return false;
  4823. }
  4824. if (components.size() == 2) {
  4825. if (components[1] == "linear_velocity") {
  4826. r_ret = get_constant_linear_velocity(layer_index);
  4827. return true;
  4828. } else if (components[1] == "angular_velocity") {
  4829. r_ret = get_constant_angular_velocity(layer_index);
  4830. return true;
  4831. } else if (components[1] == "polygons_count") {
  4832. r_ret = get_collision_polygons_count(layer_index);
  4833. return true;
  4834. }
  4835. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  4836. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  4837. ERR_FAIL_COND_V(polygon_index < 0, false);
  4838. if (polygon_index >= physics[layer_index].polygons.size()) {
  4839. return false;
  4840. }
  4841. if (components[2] == "points") {
  4842. r_ret = get_collision_polygon_points(layer_index, polygon_index);
  4843. return true;
  4844. } else if (components[2] == "one_way") {
  4845. r_ret = is_collision_polygon_one_way(layer_index, polygon_index);
  4846. return true;
  4847. } else if (components[2] == "one_way_margin") {
  4848. r_ret = get_collision_polygon_one_way_margin(layer_index, polygon_index);
  4849. return true;
  4850. }
  4851. }
  4852. } else if (components.size() == 2 && components[0] == "terrains_peering_bit") {
  4853. // Terrains.
  4854. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4855. if (components[1] == TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]) {
  4856. r_ret = terrain_peering_bits[i];
  4857. return true;
  4858. }
  4859. }
  4860. return false;
  4861. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  4862. // Occlusion layers.
  4863. int layer_index = components[0].trim_prefix("navigation_layer_").to_int();
  4864. ERR_FAIL_COND_V(layer_index < 0, false);
  4865. if (layer_index >= navigation.size()) {
  4866. return false;
  4867. }
  4868. if (components[1] == "polygon") {
  4869. r_ret = get_navigation_polygon(layer_index);
  4870. return true;
  4871. }
  4872. } else if (components.size() == 1 && components[0].begins_with("custom_data_") && components[0].trim_prefix("custom_data_").is_valid_int()) {
  4873. // Custom data layers.
  4874. int layer_index = components[0].trim_prefix("custom_data_").to_int();
  4875. ERR_FAIL_COND_V(layer_index < 0, false);
  4876. if (layer_index >= custom_data.size()) {
  4877. return false;
  4878. }
  4879. r_ret = get_custom_data_by_layer_id(layer_index);
  4880. return true;
  4881. }
  4882. }
  4883. return false;
  4884. }
  4885. void TileData::_get_property_list(List<PropertyInfo> *p_list) const {
  4886. PropertyInfo property_info;
  4887. // Add the groups manually.
  4888. if (tile_set) {
  4889. // Occlusion layers.
  4890. p_list->push_back(PropertyInfo(Variant::NIL, "Rendering", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4891. for (int i = 0; i < occluders.size(); i++) {
  4892. // occlusion_layer_%d/polygon
  4893. property_info = PropertyInfo(Variant::OBJECT, vformat("occlusion_layer_%d/polygon", i), PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D", PROPERTY_USAGE_DEFAULT);
  4894. if (!occluders[i].is_valid()) {
  4895. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4896. }
  4897. p_list->push_back(property_info);
  4898. }
  4899. // Physics layers.
  4900. p_list->push_back(PropertyInfo(Variant::NIL, "Physics", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4901. for (int i = 0; i < physics.size(); i++) {
  4902. p_list->push_back(PropertyInfo(Variant::VECTOR2, vformat("physics_layer_%d/linear_velocity", i), PROPERTY_HINT_NONE));
  4903. p_list->push_back(PropertyInfo(Variant::FLOAT, vformat("physics_layer_%d/angular_velocity", i), PROPERTY_HINT_NONE));
  4904. p_list->push_back(PropertyInfo(Variant::INT, vformat("physics_layer_%d/polygons_count", i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  4905. for (int j = 0; j < physics[i].polygons.size(); j++) {
  4906. // physics_layer_%d/points
  4907. property_info = PropertyInfo(Variant::ARRAY, vformat("physics_layer_%d/polygon_%d/points", i, j), PROPERTY_HINT_ARRAY_TYPE, "Vector2", PROPERTY_USAGE_DEFAULT);
  4908. if (physics[i].polygons[j].polygon.is_empty()) {
  4909. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4910. }
  4911. p_list->push_back(property_info);
  4912. // physics_layer_%d/polygon_%d/one_way
  4913. property_info = PropertyInfo(Variant::BOOL, vformat("physics_layer_%d/polygon_%d/one_way", i, j));
  4914. if (physics[i].polygons[j].one_way == false) {
  4915. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4916. }
  4917. p_list->push_back(property_info);
  4918. // physics_layer_%d/polygon_%d/one_way_margin
  4919. property_info = PropertyInfo(Variant::FLOAT, vformat("physics_layer_%d/polygon_%d/one_way_margin", i, j));
  4920. if (physics[i].polygons[j].one_way_margin == 1.0) {
  4921. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4922. }
  4923. p_list->push_back(property_info);
  4924. }
  4925. }
  4926. // Terrain data
  4927. if (terrain_set >= 0) {
  4928. p_list->push_back(PropertyInfo(Variant::NIL, "Terrains", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4929. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  4930. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  4931. if (is_valid_terrain_peering_bit(bit)) {
  4932. property_info = PropertyInfo(Variant::INT, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]));
  4933. if (get_terrain_peering_bit(bit) == -1) {
  4934. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4935. }
  4936. p_list->push_back(property_info);
  4937. }
  4938. }
  4939. }
  4940. // Navigation layers.
  4941. p_list->push_back(PropertyInfo(Variant::NIL, "Navigation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP));
  4942. for (int i = 0; i < navigation.size(); i++) {
  4943. property_info = PropertyInfo(Variant::OBJECT, vformat("navigation_layer_%d/polygon", i), PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon", PROPERTY_USAGE_DEFAULT);
  4944. if (!navigation[i].is_valid()) {
  4945. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4946. }
  4947. p_list->push_back(property_info);
  4948. }
  4949. // Custom data layers.
  4950. p_list->push_back(PropertyInfo(Variant::NIL, "Custom data", PROPERTY_HINT_NONE, "custom_data_", PROPERTY_USAGE_GROUP));
  4951. for (int i = 0; i < custom_data.size(); i++) {
  4952. Variant default_val;
  4953. Callable::CallError error;
  4954. Variant::construct(custom_data[i].get_type(), default_val, nullptr, 0, error);
  4955. 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);
  4956. if (custom_data[i] == default_val) {
  4957. property_info.usage ^= PROPERTY_USAGE_STORAGE;
  4958. }
  4959. p_list->push_back(property_info);
  4960. }
  4961. }
  4962. }
  4963. void TileData::_bind_methods() {
  4964. // Rendering.
  4965. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &TileData::set_flip_h);
  4966. ClassDB::bind_method(D_METHOD("get_flip_h"), &TileData::get_flip_h);
  4967. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &TileData::set_flip_v);
  4968. ClassDB::bind_method(D_METHOD("get_flip_v"), &TileData::get_flip_v);
  4969. ClassDB::bind_method(D_METHOD("set_transpose", "transpose"), &TileData::set_transpose);
  4970. ClassDB::bind_method(D_METHOD("get_transpose"), &TileData::get_transpose);
  4971. ClassDB::bind_method(D_METHOD("set_material", "material"), &TileData::set_material);
  4972. ClassDB::bind_method(D_METHOD("get_material"), &TileData::get_material);
  4973. ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &TileData::set_texture_offset);
  4974. ClassDB::bind_method(D_METHOD("get_texture_offset"), &TileData::get_texture_offset);
  4975. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &TileData::set_modulate);
  4976. ClassDB::bind_method(D_METHOD("get_modulate"), &TileData::get_modulate);
  4977. ClassDB::bind_method(D_METHOD("set_z_index", "z_index"), &TileData::set_z_index);
  4978. ClassDB::bind_method(D_METHOD("get_z_index"), &TileData::get_z_index);
  4979. ClassDB::bind_method(D_METHOD("set_y_sort_origin", "y_sort_origin"), &TileData::set_y_sort_origin);
  4980. ClassDB::bind_method(D_METHOD("get_y_sort_origin"), &TileData::get_y_sort_origin);
  4981. ClassDB::bind_method(D_METHOD("set_occluder", "layer_id", "occluder_polygon"), &TileData::set_occluder);
  4982. ClassDB::bind_method(D_METHOD("get_occluder", "layer_id"), &TileData::get_occluder);
  4983. // Physics.
  4984. ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "layer_id", "velocity"), &TileData::set_constant_linear_velocity);
  4985. ClassDB::bind_method(D_METHOD("get_constant_linear_velocity", "layer_id"), &TileData::get_constant_linear_velocity);
  4986. ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "layer_id", "velocity"), &TileData::set_constant_angular_velocity);
  4987. ClassDB::bind_method(D_METHOD("get_constant_angular_velocity", "layer_id"), &TileData::get_constant_angular_velocity);
  4988. ClassDB::bind_method(D_METHOD("set_collision_polygons_count", "layer_id", "polygons_count"), &TileData::set_collision_polygons_count);
  4989. ClassDB::bind_method(D_METHOD("get_collision_polygons_count", "layer_id"), &TileData::get_collision_polygons_count);
  4990. ClassDB::bind_method(D_METHOD("add_collision_polygon", "layer_id"), &TileData::add_collision_polygon);
  4991. ClassDB::bind_method(D_METHOD("remove_collision_polygon", "layer_id", "polygon_index"), &TileData::remove_collision_polygon);
  4992. ClassDB::bind_method(D_METHOD("set_collision_polygon_points", "layer_id", "polygon_index", "polygon"), &TileData::set_collision_polygon_points);
  4993. ClassDB::bind_method(D_METHOD("get_collision_polygon_points", "layer_id", "polygon_index"), &TileData::get_collision_polygon_points);
  4994. ClassDB::bind_method(D_METHOD("set_collision_polygon_one_way", "layer_id", "polygon_index", "one_way"), &TileData::set_collision_polygon_one_way);
  4995. ClassDB::bind_method(D_METHOD("is_collision_polygon_one_way", "layer_id", "polygon_index"), &TileData::is_collision_polygon_one_way);
  4996. 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);
  4997. ClassDB::bind_method(D_METHOD("get_collision_polygon_one_way_margin", "layer_id", "polygon_index"), &TileData::get_collision_polygon_one_way_margin);
  4998. // Terrain
  4999. ClassDB::bind_method(D_METHOD("set_terrain_set", "terrain_set"), &TileData::set_terrain_set);
  5000. ClassDB::bind_method(D_METHOD("get_terrain_set"), &TileData::get_terrain_set);
  5001. ClassDB::bind_method(D_METHOD("set_terrain", "terrain"), &TileData::set_terrain);
  5002. ClassDB::bind_method(D_METHOD("get_terrain"), &TileData::get_terrain);
  5003. ClassDB::bind_method(D_METHOD("set_terrain_peering_bit", "peering_bit", "terrain"), &TileData::set_terrain_peering_bit);
  5004. ClassDB::bind_method(D_METHOD("get_terrain_peering_bit", "peering_bit"), &TileData::get_terrain_peering_bit);
  5005. // Navigation
  5006. ClassDB::bind_method(D_METHOD("set_navigation_polygon", "layer_id", "navigation_polygon"), &TileData::set_navigation_polygon);
  5007. ClassDB::bind_method(D_METHOD("get_navigation_polygon", "layer_id"), &TileData::get_navigation_polygon);
  5008. // Misc.
  5009. ClassDB::bind_method(D_METHOD("set_probability", "probability"), &TileData::set_probability);
  5010. ClassDB::bind_method(D_METHOD("get_probability"), &TileData::get_probability);
  5011. // Custom data.
  5012. ClassDB::bind_method(D_METHOD("set_custom_data", "layer_name", "value"), &TileData::set_custom_data);
  5013. ClassDB::bind_method(D_METHOD("get_custom_data", "layer_name"), &TileData::get_custom_data);
  5014. ClassDB::bind_method(D_METHOD("set_custom_data_by_layer_id", "layer_id", "value"), &TileData::set_custom_data_by_layer_id);
  5015. ClassDB::bind_method(D_METHOD("get_custom_data_by_layer_id", "layer_id"), &TileData::get_custom_data_by_layer_id);
  5016. ADD_GROUP("Rendering", "");
  5017. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "get_flip_h");
  5018. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "get_flip_v");
  5019. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transpose"), "set_transpose", "get_transpose");
  5020. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "texture_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_offset", "get_texture_offset");
  5021. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  5022. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "CanvasItemMaterial,ShaderMaterial"), "set_material", "get_material");
  5023. ADD_PROPERTY(PropertyInfo(Variant::INT, "z_index"), "set_z_index", "get_z_index");
  5024. ADD_PROPERTY(PropertyInfo(Variant::INT, "y_sort_origin"), "set_y_sort_origin", "get_y_sort_origin");
  5025. ADD_GROUP("Terrains", "");
  5026. ADD_PROPERTY(PropertyInfo(Variant::INT, "terrain_set"), "set_terrain_set", "get_terrain_set");
  5027. ADD_PROPERTY(PropertyInfo(Variant::INT, "terrain"), "set_terrain", "get_terrain");
  5028. ADD_GROUP("Miscellaneous", "");
  5029. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "probability"), "set_probability", "get_probability");
  5030. ADD_SIGNAL(MethodInfo("changed"));
  5031. }