tile_set.cpp 190 KB

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