tile_set.cpp 213 KB

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