tile_set.cpp 189 KB

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