tile_map_layer_editor.cpp 187 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541
  1. /**************************************************************************/
  2. /* tile_map_layer_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "tile_map_layer_editor.h"
  31. #include "tiles_editor_plugin.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_resource_preview.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/multi_node_edit.h"
  37. #include "editor/plugins/canvas_item_editor_plugin.h"
  38. #include "editor/themes/editor_scale.h"
  39. #include "scene/2d/camera_2d.h"
  40. #include "scene/2d/tile_map_layer.h"
  41. #include "scene/gui/center_container.h"
  42. #include "scene/gui/split_container.h"
  43. #include "core/input/input.h"
  44. #include "core/math/geometry_2d.h"
  45. #include "core/os/keyboard.h"
  46. TileMapLayer *TileMapLayerSubEditorPlugin::_get_edited_layer() const {
  47. return Object::cast_to<TileMapLayer>(ObjectDB::get_instance(edited_tile_map_layer_id));
  48. }
  49. void TileMapLayerEditorTilesPlugin::tile_set_changed() {
  50. _update_fix_selected_and_hovered();
  51. _update_tile_set_sources_list();
  52. _update_source_display();
  53. _update_patterns_list();
  54. }
  55. void TileMapLayerEditorTilesPlugin::_on_random_tile_checkbox_toggled(bool p_pressed) {
  56. scatter_controls_container->set_visible(p_pressed);
  57. }
  58. void TileMapLayerEditorTilesPlugin::_on_scattering_spinbox_changed(double p_value) {
  59. scattering = p_value;
  60. }
  61. void TileMapLayerEditorTilesPlugin::_update_toolbar() {
  62. // Stop draggig if needed.
  63. _stop_dragging();
  64. // Hide all settings.
  65. for (int i = 0; i < tools_settings->get_child_count(); i++) {
  66. Object::cast_to<CanvasItem>(tools_settings->get_child(i))->hide();
  67. }
  68. // Show only the correct settings.
  69. if (tool_buttons_group->get_pressed_button() == select_tool_button) {
  70. transform_toolbar->show();
  71. } else if (tool_buttons_group->get_pressed_button() != bucket_tool_button) {
  72. tools_settings_vsep->show();
  73. picker_button->show();
  74. erase_button->show();
  75. transform_toolbar->show();
  76. tools_settings_vsep_2->show();
  77. random_tile_toggle->show();
  78. scatter_controls_container->set_visible(random_tile_toggle->is_pressed());
  79. } else {
  80. tools_settings_vsep->show();
  81. picker_button->show();
  82. erase_button->show();
  83. transform_toolbar->show();
  84. tools_settings_vsep_2->show();
  85. bucket_contiguous_checkbox->show();
  86. random_tile_toggle->show();
  87. scatter_controls_container->set_visible(random_tile_toggle->is_pressed());
  88. }
  89. }
  90. void TileMapLayerEditorTilesPlugin::_update_transform_buttons() {
  91. TileMapLayer *edited_layer = _get_edited_layer();
  92. if (!edited_layer) {
  93. return;
  94. }
  95. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  96. if (tile_set.is_null() || selection_pattern.is_null()) {
  97. return;
  98. }
  99. bool has_scene_tile = false;
  100. for (const KeyValue<Vector2i, TileMapCell> &E : selection_pattern->get_pattern()) {
  101. if (Object::cast_to<TileSetScenesCollectionSource>(tile_set->get_source(E.value.source_id).ptr())) {
  102. has_scene_tile = true;
  103. break;
  104. }
  105. }
  106. if (has_scene_tile) {
  107. _set_transform_buttons_state({}, { transform_button_rotate_left, transform_button_rotate_right, transform_button_flip_h, transform_button_flip_v },
  108. TTR("Can't transform scene tiles."));
  109. } else if (tile_set->get_tile_shape() != TileSet::TILE_SHAPE_SQUARE && selection_pattern->get_size() != Vector2i(1, 1)) {
  110. _set_transform_buttons_state({ transform_button_flip_h, transform_button_flip_v }, { transform_button_rotate_left, transform_button_rotate_right },
  111. TTR("Can't rotate patterns when using non-square tile grid."));
  112. } else {
  113. _set_transform_buttons_state({ transform_button_rotate_left, transform_button_rotate_right, transform_button_flip_h, transform_button_flip_v }, {}, "");
  114. }
  115. }
  116. void TileMapLayerEditorTilesPlugin::_set_transform_buttons_state(const Vector<Button *> &p_enabled_buttons, const Vector<Button *> &p_disabled_buttons, const String &p_why_disabled) {
  117. for (Button *button : p_enabled_buttons) {
  118. button->set_disabled(false);
  119. button->set_tooltip_text("");
  120. }
  121. for (Button *button : p_disabled_buttons) {
  122. button->set_disabled(true);
  123. button->set_tooltip_text(p_why_disabled);
  124. }
  125. }
  126. Vector<TileMapLayerSubEditorPlugin::TabData> TileMapLayerEditorTilesPlugin::get_tabs() const {
  127. Vector<TileMapLayerSubEditorPlugin::TabData> tabs;
  128. tabs.push_back({ toolbar, tiles_bottom_panel });
  129. tabs.push_back({ toolbar, patterns_bottom_panel });
  130. return tabs;
  131. }
  132. void TileMapLayerEditorTilesPlugin::_tab_changed() {
  133. if (tiles_bottom_panel->is_visible_in_tree()) {
  134. _update_selection_pattern_from_tileset_tiles_selection();
  135. } else if (patterns_bottom_panel->is_visible_in_tree()) {
  136. _update_selection_pattern_from_tileset_pattern_selection();
  137. }
  138. }
  139. void TileMapLayerEditorTilesPlugin::_update_tile_set_sources_list() {
  140. // Update the sources.
  141. int old_current = sources_list->get_current();
  142. int old_source = -1;
  143. if (old_current > -1) {
  144. old_source = sources_list->get_item_metadata(old_current);
  145. } else {
  146. old_source = sources_list->get_meta("old_source", -1);
  147. }
  148. sources_list->set_meta("old_source", old_source);
  149. sources_list->clear();
  150. TileMapLayer *edited_layer = _get_edited_layer();
  151. if (!edited_layer) {
  152. return;
  153. }
  154. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  155. if (tile_set.is_null()) {
  156. return;
  157. }
  158. if (!tile_set->has_source(old_source)) {
  159. old_source = -1;
  160. }
  161. List<int> source_ids = TilesEditorUtils::get_singleton()->get_sorted_sources(tile_set);
  162. for (const int &source_id : source_ids) {
  163. TileSetSource *source = *tile_set->get_source(source_id);
  164. Ref<Texture2D> texture;
  165. String item_text;
  166. // Common to all type of sources.
  167. if (!source->get_name().is_empty()) {
  168. item_text = source->get_name();
  169. }
  170. // Atlas source.
  171. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  172. if (atlas_source) {
  173. texture = atlas_source->get_texture();
  174. if (item_text.is_empty()) {
  175. if (texture.is_valid()) {
  176. item_text = texture->get_path().get_file();
  177. } else {
  178. item_text = vformat(TTR("No Texture Atlas Source (ID: %d)"), source_id);
  179. }
  180. }
  181. }
  182. // Scene collection source.
  183. TileSetScenesCollectionSource *scene_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  184. if (scene_collection_source) {
  185. texture = tiles_bottom_panel->get_editor_theme_icon(SNAME("PackedScene"));
  186. if (item_text.is_empty()) {
  187. if (scene_collection_source->get_scene_tiles_count() > 0) {
  188. item_text = vformat(TTR("Scene Collection Source (ID: %d)"), source_id);
  189. } else {
  190. item_text = vformat(TTR("Empty Scene Collection Source (ID: %d)"), source_id);
  191. }
  192. }
  193. }
  194. // Use default if not valid.
  195. if (item_text.is_empty()) {
  196. item_text = vformat(TTR("Unknown Type Source (ID: %d)"), source_id);
  197. }
  198. if (!texture.is_valid()) {
  199. texture = missing_atlas_texture_icon;
  200. }
  201. sources_list->add_item(item_text, texture);
  202. sources_list->set_item_metadata(-1, source_id);
  203. }
  204. if (sources_list->get_item_count() > 0) {
  205. if (old_source >= 0) {
  206. for (int i = 0; i < sources_list->get_item_count(); i++) {
  207. if ((int)sources_list->get_item_metadata(i) == old_source) {
  208. sources_list->set_current(i);
  209. sources_list->ensure_current_is_visible();
  210. break;
  211. }
  212. }
  213. } else {
  214. sources_list->set_current(0);
  215. }
  216. sources_list->emit_signal(SceneStringName(item_selected), sources_list->get_current());
  217. }
  218. // Synchronize the lists.
  219. TilesEditorUtils::get_singleton()->set_sources_lists_current(sources_list->get_current());
  220. }
  221. void TileMapLayerEditorTilesPlugin::_update_source_display() {
  222. // Update the atlas display.
  223. TileMapLayer *edited_layer = _get_edited_layer();
  224. if (!edited_layer) {
  225. return;
  226. }
  227. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  228. if (tile_set.is_null()) {
  229. return;
  230. }
  231. int source_index = sources_list->get_current();
  232. if (source_index >= 0 && source_index < sources_list->get_item_count()) {
  233. atlas_sources_split_container->show();
  234. missing_source_label->hide();
  235. int source_id = sources_list->get_item_metadata(source_index);
  236. TileSetSource *source = *tile_set->get_source(source_id);
  237. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  238. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  239. if (atlas_source) {
  240. tile_atlas_view->show();
  241. scene_tiles_list->hide();
  242. invalid_source_label->hide();
  243. _update_atlas_view();
  244. } else if (scenes_collection_source) {
  245. tile_atlas_view->hide();
  246. scene_tiles_list->show();
  247. invalid_source_label->hide();
  248. _update_scenes_collection_view();
  249. } else {
  250. tile_atlas_view->hide();
  251. scene_tiles_list->hide();
  252. invalid_source_label->show();
  253. }
  254. } else {
  255. atlas_sources_split_container->hide();
  256. missing_source_label->show();
  257. tile_atlas_view->hide();
  258. scene_tiles_list->hide();
  259. invalid_source_label->hide();
  260. }
  261. }
  262. void TileMapLayerEditorTilesPlugin::_patterns_item_list_gui_input(const Ref<InputEvent> &p_event) {
  263. TileMapLayer *edited_layer = _get_edited_layer();
  264. if (!edited_layer) {
  265. return;
  266. }
  267. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  268. if (tile_set.is_null()) {
  269. return;
  270. }
  271. if (ED_IS_SHORTCUT("tiles_editor/paste", p_event) && p_event->is_pressed() && !p_event->is_echo()) {
  272. select_last_pattern = true;
  273. int new_pattern_index = tile_set->get_patterns_count();
  274. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  275. undo_redo->create_action(TTR("Add TileSet pattern"));
  276. undo_redo->add_do_method(*tile_set, "add_pattern", tile_map_clipboard, new_pattern_index);
  277. undo_redo->add_undo_method(*tile_set, "remove_pattern", new_pattern_index);
  278. undo_redo->commit_action();
  279. patterns_item_list->accept_event();
  280. }
  281. if (ED_IS_SHORTCUT("tiles_editor/delete", p_event) && p_event->is_pressed() && !p_event->is_echo()) {
  282. Vector<int> selected = patterns_item_list->get_selected_items();
  283. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  284. undo_redo->create_action(TTR("Remove TileSet patterns"));
  285. for (int i = 0; i < selected.size(); i++) {
  286. int pattern_index = selected[i];
  287. undo_redo->add_do_method(*tile_set, "remove_pattern", pattern_index);
  288. undo_redo->add_undo_method(*tile_set, "add_pattern", tile_set->get_pattern(pattern_index), pattern_index);
  289. }
  290. undo_redo->commit_action();
  291. patterns_item_list->accept_event();
  292. }
  293. }
  294. void TileMapLayerEditorTilesPlugin::_pattern_preview_done(Ref<TileMapPattern> p_pattern, Ref<Texture2D> p_texture) {
  295. // TODO optimize ?
  296. for (int i = 0; i < patterns_item_list->get_item_count(); i++) {
  297. if (patterns_item_list->get_item_metadata(i) == p_pattern) {
  298. patterns_item_list->set_item_icon(i, p_texture);
  299. break;
  300. }
  301. }
  302. }
  303. void TileMapLayerEditorTilesPlugin::_update_patterns_list() {
  304. TileMapLayer *edited_layer = _get_edited_layer();
  305. if (!edited_layer) {
  306. return;
  307. }
  308. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  309. if (tile_set.is_null()) {
  310. return;
  311. }
  312. // Recreate the items.
  313. patterns_item_list->clear();
  314. for (int i = 0; i < tile_set->get_patterns_count(); i++) {
  315. int id = patterns_item_list->add_item("");
  316. patterns_item_list->set_item_metadata(id, tile_set->get_pattern(i));
  317. patterns_item_list->set_item_tooltip(id, vformat(TTR("Index: %d"), i));
  318. TilesEditorUtils::get_singleton()->queue_pattern_preview(tile_set, tile_set->get_pattern(i), callable_mp(this, &TileMapLayerEditorTilesPlugin::_pattern_preview_done));
  319. }
  320. // Update the label visibility.
  321. patterns_help_label->set_visible(patterns_item_list->get_item_count() == 0);
  322. // Added a new pattern, thus select the last one.
  323. if (select_last_pattern) {
  324. patterns_item_list->select(tile_set->get_patterns_count() - 1);
  325. patterns_item_list->grab_focus();
  326. _update_selection_pattern_from_tileset_pattern_selection();
  327. }
  328. select_last_pattern = false;
  329. }
  330. void TileMapLayerEditorTilesPlugin::_update_atlas_view() {
  331. TileMapLayer *edited_layer = _get_edited_layer();
  332. if (!edited_layer) {
  333. return;
  334. }
  335. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  336. if (tile_set.is_null()) {
  337. return;
  338. }
  339. int source_id = sources_list->get_item_metadata(sources_list->get_current());
  340. TileSetSource *source = *tile_set->get_source(source_id);
  341. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  342. ERR_FAIL_NULL(atlas_source);
  343. tile_atlas_view->set_atlas_source(*tile_set, atlas_source, source_id);
  344. TilesEditorUtils::get_singleton()->synchronize_atlas_view(tile_atlas_view);
  345. tile_atlas_control->queue_redraw();
  346. }
  347. void TileMapLayerEditorTilesPlugin::_update_scenes_collection_view() {
  348. TileMapLayer *edited_layer = _get_edited_layer();
  349. if (!edited_layer) {
  350. return;
  351. }
  352. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  353. if (tile_set.is_null()) {
  354. return;
  355. }
  356. int source_id = sources_list->get_item_metadata(sources_list->get_current());
  357. TileSetSource *source = *tile_set->get_source(source_id);
  358. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  359. ERR_FAIL_NULL(scenes_collection_source);
  360. // Clear the list.
  361. scene_tiles_list->clear();
  362. // Rebuild the list.
  363. for (int i = 0; i < scenes_collection_source->get_scene_tiles_count(); i++) {
  364. int scene_id = scenes_collection_source->get_scene_tile_id(i);
  365. Ref<PackedScene> scene = scenes_collection_source->get_scene_tile_scene(scene_id);
  366. int item_index = 0;
  367. if (scene.is_valid()) {
  368. item_index = scene_tiles_list->add_item(vformat("%s (Path: %s, ID: %d)", scene->get_path().get_file().get_basename(), scene->get_path(), scene_id));
  369. Variant udata = i;
  370. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(scene, this, "_scene_thumbnail_done", udata);
  371. } else {
  372. item_index = scene_tiles_list->add_item(TTR("Tile with Invalid Scene"), tiles_bottom_panel->get_editor_theme_icon(SNAME("PackedScene")));
  373. }
  374. scene_tiles_list->set_item_metadata(item_index, scene_id);
  375. // Check if in selection.
  376. if (tile_set_selection.has(TileMapCell(source_id, Vector2i(), scene_id))) {
  377. scene_tiles_list->select(item_index, false);
  378. }
  379. }
  380. if (scene_tiles_list->get_item_count() == 0) {
  381. scene_tiles_list->add_item(TTR("The selected scene collection source has no scenes. Add scenes in the TileSet bottom tab."));
  382. scene_tiles_list->set_item_disabled(-1, true);
  383. }
  384. // Icon size update.
  385. int int_size = int(EDITOR_GET("filesystem/file_dialog/thumbnail_size")) * EDSCALE;
  386. scene_tiles_list->set_fixed_icon_size(Vector2(int_size, int_size));
  387. }
  388. void TileMapLayerEditorTilesPlugin::_scene_thumbnail_done(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_ud) {
  389. int index = p_ud;
  390. if (index >= 0 && index < scene_tiles_list->get_item_count()) {
  391. scene_tiles_list->set_item_icon(index, p_preview);
  392. }
  393. }
  394. void TileMapLayerEditorTilesPlugin::_scenes_list_multi_selected(int p_index, bool p_selected) {
  395. TileMapLayer *edited_layer = _get_edited_layer();
  396. if (!edited_layer) {
  397. return;
  398. }
  399. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  400. if (tile_set.is_null()) {
  401. return;
  402. }
  403. // Add or remove the Tile form the selection.
  404. int scene_id = scene_tiles_list->get_item_metadata(p_index);
  405. int source_id = sources_list->get_item_metadata(sources_list->get_current());
  406. TileSetSource *source = *tile_set->get_source(source_id);
  407. TileSetScenesCollectionSource *scenes_collection_source = Object::cast_to<TileSetScenesCollectionSource>(source);
  408. ERR_FAIL_NULL(scenes_collection_source);
  409. TileMapCell selected = TileMapCell(source_id, Vector2i(), scene_id);
  410. // Clear the selection if shift is not pressed.
  411. if (!Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  412. tile_set_selection.clear();
  413. }
  414. if (p_selected) {
  415. tile_set_selection.insert(selected);
  416. } else {
  417. if (tile_set_selection.has(selected)) {
  418. tile_set_selection.erase(selected);
  419. }
  420. }
  421. _update_selection_pattern_from_tileset_tiles_selection();
  422. }
  423. void TileMapLayerEditorTilesPlugin::_scenes_list_lmb_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index) {
  424. if (p_mouse_button_index != MouseButton::LEFT) {
  425. return;
  426. }
  427. scene_tiles_list->deselect_all();
  428. tile_set_selection.clear();
  429. tile_map_selection.clear();
  430. selection_pattern.instantiate();
  431. _update_selection_pattern_from_tileset_tiles_selection();
  432. }
  433. void TileMapLayerEditorTilesPlugin::_update_theme() {
  434. source_sort_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Sort")));
  435. select_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("ToolSelect")));
  436. paint_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Edit")));
  437. line_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Line")));
  438. rect_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Rectangle")));
  439. bucket_tool_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Bucket")));
  440. picker_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("ColorPick")));
  441. erase_button->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Eraser")));
  442. random_tile_toggle->set_button_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("RandomNumberGenerator")));
  443. transform_button_rotate_left->set_button_icon(tiles_bottom_panel->get_editor_theme_icon("RotateLeft"));
  444. transform_button_rotate_right->set_button_icon(tiles_bottom_panel->get_editor_theme_icon("RotateRight"));
  445. transform_button_flip_h->set_button_icon(tiles_bottom_panel->get_editor_theme_icon("MirrorX"));
  446. transform_button_flip_v->set_button_icon(tiles_bottom_panel->get_editor_theme_icon("MirrorY"));
  447. missing_atlas_texture_icon = tiles_bottom_panel->get_editor_theme_icon(SNAME("TileSet"));
  448. _update_tile_set_sources_list();
  449. }
  450. bool TileMapLayerEditorTilesPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  451. if (!(tiles_bottom_panel->is_visible_in_tree() || patterns_bottom_panel->is_visible_in_tree())) {
  452. // If the bottom editor is not visible, we ignore inputs.
  453. return false;
  454. }
  455. if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
  456. _stop_dragging();
  457. return false;
  458. }
  459. TileMapLayer *edited_layer = _get_edited_layer();
  460. if (!edited_layer) {
  461. return false;
  462. }
  463. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  464. if (tile_set.is_null()) {
  465. return false;
  466. }
  467. // Shortcuts
  468. if (ED_IS_SHORTCUT("tiles_editor/cut", p_event) || ED_IS_SHORTCUT("tiles_editor/copy", p_event)) {
  469. // Fill in the clipboard.
  470. if (!tile_map_selection.is_empty()) {
  471. tile_map_clipboard.instantiate();
  472. TypedArray<Vector2i> coords_array;
  473. for (const Vector2i &E : tile_map_selection) {
  474. coords_array.push_back(E);
  475. }
  476. tile_map_clipboard = edited_layer->get_pattern(coords_array);
  477. }
  478. if (ED_IS_SHORTCUT("tiles_editor/cut", p_event)) {
  479. // Delete selected tiles.
  480. if (!tile_map_selection.is_empty()) {
  481. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  482. undo_redo->create_action(TTR("Delete tiles"));
  483. for (const Vector2i &coords : tile_map_selection) {
  484. undo_redo->add_do_method(edited_layer, "set_cell", coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  485. undo_redo->add_undo_method(edited_layer, "set_cell", coords, edited_layer->get_cell_source_id(coords), edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords));
  486. }
  487. undo_redo->add_undo_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  488. tile_map_selection.clear();
  489. undo_redo->add_do_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  490. undo_redo->commit_action();
  491. }
  492. }
  493. return true;
  494. }
  495. if (ED_IS_SHORTCUT("tiles_editor/paste", p_event)) {
  496. if (drag_type == DRAG_TYPE_NONE) {
  497. drag_type = DRAG_TYPE_CLIPBOARD_PASTE;
  498. }
  499. CanvasItemEditor::get_singleton()->update_viewport();
  500. return true;
  501. }
  502. if (ED_IS_SHORTCUT("tiles_editor/cancel", p_event)) {
  503. if (drag_type == DRAG_TYPE_CLIPBOARD_PASTE) {
  504. drag_type = DRAG_TYPE_NONE;
  505. CanvasItemEditor::get_singleton()->update_viewport();
  506. return true;
  507. }
  508. }
  509. if (ED_IS_SHORTCUT("tiles_editor/delete", p_event)) {
  510. // Delete selected tiles.
  511. if (!tile_map_selection.is_empty()) {
  512. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  513. undo_redo->create_action(TTR("Delete tiles"));
  514. for (const Vector2i &coords : tile_map_selection) {
  515. undo_redo->add_do_method(edited_layer, "set_cell", coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  516. undo_redo->add_undo_method(edited_layer, "set_cell", coords, edited_layer->get_cell_source_id(coords), edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords));
  517. }
  518. undo_redo->add_undo_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  519. tile_map_selection.clear();
  520. undo_redo->add_do_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  521. undo_redo->commit_action();
  522. }
  523. return true;
  524. }
  525. Ref<InputEventKey> k = p_event;
  526. if (k.is_valid() && k->is_pressed() && !k->is_echo()) {
  527. for (BaseButton *b : viewport_shortcut_buttons) {
  528. if (b->is_disabled()) {
  529. continue;
  530. }
  531. if (b->get_shortcut().is_valid() && b->get_shortcut()->matches_event(p_event)) {
  532. if (b->is_toggle_mode()) {
  533. b->set_pressed(b->get_button_group().is_valid() || !b->is_pressed());
  534. } else {
  535. // Can't press a button without toggle mode, so just emit the signal directly.
  536. b->emit_signal(SceneStringName(pressed));
  537. }
  538. return true;
  539. }
  540. }
  541. }
  542. Ref<InputEventMouseMotion> mm = p_event;
  543. if (mm.is_valid()) {
  544. has_mouse = true;
  545. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  546. Vector2 mpos = xform.affine_inverse().xform(mm->get_position());
  547. switch (drag_type) {
  548. case DRAG_TYPE_PAINT: {
  549. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(drag_start_mouse_pos, drag_last_mouse_pos, mpos, drag_erasing);
  550. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  551. Vector2i coords = E.key;
  552. if (!drag_modified.has(coords)) {
  553. drag_modified.insert(coords, edited_layer->get_cell(coords));
  554. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  555. continue;
  556. }
  557. edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  558. }
  559. }
  560. _fix_invalid_tiles_in_tile_map_selection();
  561. } break;
  562. case DRAG_TYPE_BUCKET: {
  563. Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos));
  564. for (int i = 0; i < line.size(); i++) {
  565. if (!drag_modified.has(line[i])) {
  566. HashMap<Vector2i, TileMapCell> to_draw = _draw_bucket_fill(line[i], bucket_contiguous_checkbox->is_pressed(), drag_erasing);
  567. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  568. Vector2i coords = E.key;
  569. if (!drag_modified.has(coords)) {
  570. drag_modified.insert(coords, edited_layer->get_cell(coords));
  571. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  572. continue;
  573. }
  574. edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  575. }
  576. }
  577. }
  578. }
  579. _fix_invalid_tiles_in_tile_map_selection();
  580. } break;
  581. default:
  582. break;
  583. }
  584. drag_last_mouse_pos = mpos;
  585. CanvasItemEditor::get_singleton()->update_viewport();
  586. return true;
  587. }
  588. Ref<InputEventMouseButton> mb = p_event;
  589. if (mb.is_valid()) {
  590. has_mouse = true;
  591. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  592. Vector2 mpos = xform.affine_inverse().xform(mb->get_position());
  593. if (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT) {
  594. if (mb->is_pressed()) {
  595. // Pressed
  596. if (erase_button->is_pressed() || mb->get_button_index() == MouseButton::RIGHT) {
  597. drag_erasing = true;
  598. }
  599. if (drag_type == DRAG_TYPE_CLIPBOARD_PASTE) {
  600. // Cancel tile pasting on right-click
  601. if (mb->get_button_index() == MouseButton::RIGHT) {
  602. drag_type = DRAG_TYPE_NONE;
  603. }
  604. } else if (tool_buttons_group->get_pressed_button() == select_tool_button) {
  605. drag_start_mouse_pos = mpos;
  606. if (tile_map_selection.has(tile_set->local_to_map(drag_start_mouse_pos)) && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed()) {
  607. // Move the selection
  608. _update_selection_pattern_from_tilemap_selection(); // Make sure the pattern is up to date before moving.
  609. drag_type = DRAG_TYPE_MOVE;
  610. drag_modified.clear();
  611. for (const Vector2i &E : tile_map_selection) {
  612. Vector2i coords = E;
  613. drag_modified.insert(coords, edited_layer->get_cell(coords));
  614. edited_layer->set_cell(coords, TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  615. }
  616. } else {
  617. // Select tiles
  618. drag_type = DRAG_TYPE_SELECT;
  619. }
  620. } else {
  621. // Check if we are picking a tile.
  622. if (picker_button->is_pressed() || (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT))) {
  623. drag_type = DRAG_TYPE_PICK;
  624. drag_start_mouse_pos = mpos;
  625. } else {
  626. // Paint otherwise.
  627. if (tool_buttons_group->get_pressed_button() == paint_tool_button && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  628. drag_type = DRAG_TYPE_PAINT;
  629. drag_start_mouse_pos = mpos;
  630. drag_modified.clear();
  631. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(drag_start_mouse_pos, mpos, mpos, drag_erasing);
  632. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  633. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  634. continue;
  635. }
  636. Vector2i coords = E.key;
  637. if (!drag_modified.has(coords)) {
  638. drag_modified.insert(coords, edited_layer->get_cell(coords));
  639. }
  640. edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  641. }
  642. _fix_invalid_tiles_in_tile_map_selection();
  643. } else if (tool_buttons_group->get_pressed_button() == line_tool_button || (tool_buttons_group->get_pressed_button() == paint_tool_button && Input::get_singleton()->is_key_pressed(Key::SHIFT) && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL))) {
  644. drag_type = DRAG_TYPE_LINE;
  645. drag_start_mouse_pos = mpos;
  646. drag_modified.clear();
  647. } else if (tool_buttons_group->get_pressed_button() == rect_tool_button || (tool_buttons_group->get_pressed_button() == paint_tool_button && Input::get_singleton()->is_key_pressed(Key::SHIFT) && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL))) {
  648. drag_type = DRAG_TYPE_RECT;
  649. drag_start_mouse_pos = mpos;
  650. drag_modified.clear();
  651. } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button) {
  652. drag_type = DRAG_TYPE_BUCKET;
  653. drag_start_mouse_pos = mpos;
  654. drag_modified.clear();
  655. Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos));
  656. for (int i = 0; i < line.size(); i++) {
  657. if (!drag_modified.has(line[i])) {
  658. HashMap<Vector2i, TileMapCell> to_draw = _draw_bucket_fill(line[i], bucket_contiguous_checkbox->is_pressed(), drag_erasing);
  659. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  660. Vector2i coords = E.key;
  661. if (!drag_modified.has(coords)) {
  662. drag_modified.insert(coords, edited_layer->get_cell(coords));
  663. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  664. continue;
  665. }
  666. edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  667. }
  668. }
  669. }
  670. }
  671. _fix_invalid_tiles_in_tile_map_selection();
  672. }
  673. }
  674. }
  675. } else {
  676. // Released.
  677. if (drag_type == DRAG_TYPE_NONE) {
  678. drag_erasing = false;
  679. return false;
  680. } else {
  681. _stop_dragging();
  682. }
  683. drag_erasing = false;
  684. }
  685. CanvasItemEditor::get_singleton()->update_viewport();
  686. return true;
  687. }
  688. drag_last_mouse_pos = mpos;
  689. }
  690. return false;
  691. }
  692. void TileMapLayerEditorTilesPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) {
  693. TileMapLayer *edited_layer = _get_edited_layer();
  694. if (!edited_layer) {
  695. return;
  696. }
  697. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  698. if (tile_set.is_null()) {
  699. return;
  700. }
  701. if (!edited_layer->is_visible_in_tree()) {
  702. return;
  703. }
  704. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  705. Vector2 mpos = edited_layer->get_local_mouse_position();
  706. Vector2i tile_shape_size = tile_set->get_tile_size();
  707. // Draw the selection.
  708. if ((tiles_bottom_panel->is_visible_in_tree() || patterns_bottom_panel->is_visible_in_tree()) && tool_buttons_group->get_pressed_button() == select_tool_button) {
  709. // In select mode, we only draw the current selection if we are modifying it (pressing control or shift).
  710. if (drag_type == DRAG_TYPE_MOVE || (drag_type == DRAG_TYPE_SELECT && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT))) {
  711. // Do nothing.
  712. } else {
  713. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  714. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  715. tile_set->draw_cells_outline(p_overlay, tile_map_selection, selection_color, xform);
  716. }
  717. }
  718. // Handle the preview of the tiles to be placed.
  719. if ((tiles_bottom_panel->is_visible_in_tree() || patterns_bottom_panel->is_visible_in_tree()) && CanvasItemEditor::get_singleton()->get_current_tool() == CanvasItemEditor::TOOL_SELECT && has_mouse) { // Only if the tilemap editor is opened and the viewport is hovered.
  720. HashMap<Vector2i, TileMapCell> preview;
  721. Rect2i drawn_grid_rect;
  722. if (drag_type == DRAG_TYPE_PICK) {
  723. // Draw the area being picked.
  724. Rect2i rect = Rect2i(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos) - tile_set->local_to_map(drag_start_mouse_pos)).abs();
  725. rect.size += Vector2i(1, 1);
  726. for (int x = rect.position.x; x < rect.get_end().x; x++) {
  727. for (int y = rect.position.y; y < rect.get_end().y; y++) {
  728. Vector2i coords = Vector2i(x, y);
  729. if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  730. Transform2D tile_xform(0, tile_shape_size, 0, tile_set->map_to_local(coords));
  731. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0), false);
  732. }
  733. }
  734. }
  735. } else if (drag_type == DRAG_TYPE_SELECT) {
  736. // Draw the area being selected.
  737. Rect2i rect = Rect2i(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos) - tile_set->local_to_map(drag_start_mouse_pos)).abs();
  738. rect.size += Vector2i(1, 1);
  739. RBSet<Vector2i> to_draw;
  740. for (int x = rect.position.x; x < rect.get_end().x; x++) {
  741. for (int y = rect.position.y; y < rect.get_end().y; y++) {
  742. Vector2i coords = Vector2i(x, y);
  743. if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  744. to_draw.insert(coords);
  745. }
  746. Transform2D tile_xform(0, tile_shape_size, 0, tile_set->map_to_local(coords));
  747. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0, 0.2), true);
  748. }
  749. }
  750. tile_set->draw_cells_outline(p_overlay, to_draw, Color(1.0, 1.0, 1.0), xform);
  751. } else if (drag_type == DRAG_TYPE_MOVE) {
  752. if (!(patterns_item_list->is_visible_in_tree() && patterns_item_list->has_point(patterns_item_list->get_local_mouse_position()))) {
  753. // Preview when moving.
  754. Vector2i top_left;
  755. if (!tile_map_selection.is_empty()) {
  756. top_left = tile_map_selection.front()->get();
  757. }
  758. for (const Vector2i &E : tile_map_selection) {
  759. top_left = top_left.min(E);
  760. }
  761. Vector2i offset = drag_start_mouse_pos - tile_set->map_to_local(top_left);
  762. offset = tile_set->local_to_map(mpos - offset) - tile_set->local_to_map(drag_start_mouse_pos - offset);
  763. TypedArray<Vector2i> selection_used_cells = selection_pattern->get_used_cells();
  764. for (int i = 0; i < selection_used_cells.size(); i++) {
  765. Vector2i coords = tile_set->map_pattern(offset + top_left, selection_used_cells[i], selection_pattern);
  766. preview[coords] = TileMapCell(selection_pattern->get_cell_source_id(selection_used_cells[i]), selection_pattern->get_cell_atlas_coords(selection_used_cells[i]), selection_pattern->get_cell_alternative_tile(selection_used_cells[i]));
  767. }
  768. }
  769. } else if (drag_type == DRAG_TYPE_CLIPBOARD_PASTE) {
  770. // Preview when pasting.
  771. Vector2 mouse_offset = (Vector2(tile_map_clipboard->get_size()) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size();
  772. TypedArray<Vector2i> clipboard_used_cells = tile_map_clipboard->get_used_cells();
  773. for (int i = 0; i < clipboard_used_cells.size(); i++) {
  774. Vector2i coords = tile_set->map_pattern(tile_set->local_to_map(mpos - mouse_offset), clipboard_used_cells[i], tile_map_clipboard);
  775. preview[coords] = TileMapCell(tile_map_clipboard->get_cell_source_id(clipboard_used_cells[i]), tile_map_clipboard->get_cell_atlas_coords(clipboard_used_cells[i]), tile_map_clipboard->get_cell_alternative_tile(clipboard_used_cells[i]));
  776. }
  777. } else if (!picker_button->is_pressed() && !(drag_type == DRAG_TYPE_NONE && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT))) {
  778. bool expand_grid = false;
  779. if (tool_buttons_group->get_pressed_button() == paint_tool_button && drag_type == DRAG_TYPE_NONE) {
  780. // Preview for a single pattern.
  781. preview = _draw_line(mpos, mpos, mpos, erase_button->is_pressed());
  782. expand_grid = true;
  783. } else if (tool_buttons_group->get_pressed_button() == line_tool_button || drag_type == DRAG_TYPE_LINE) {
  784. if (drag_type == DRAG_TYPE_NONE) {
  785. // Preview for a single pattern.
  786. preview = _draw_line(mpos, mpos, mpos, erase_button->is_pressed());
  787. expand_grid = true;
  788. } else if (drag_type == DRAG_TYPE_LINE) {
  789. // Preview for a line pattern.
  790. preview = _draw_line(drag_start_mouse_pos, drag_start_mouse_pos, mpos, drag_erasing);
  791. expand_grid = true;
  792. }
  793. } else if (drag_type == DRAG_TYPE_RECT) {
  794. // Preview for a rect pattern.
  795. preview = _draw_rect(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing);
  796. expand_grid = true;
  797. } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button && drag_type == DRAG_TYPE_NONE) {
  798. // Preview for a fill pattern.
  799. preview = _draw_bucket_fill(tile_set->local_to_map(mpos), bucket_contiguous_checkbox->is_pressed(), erase_button->is_pressed());
  800. }
  801. // Expand the grid if needed
  802. if (expand_grid && !preview.is_empty()) {
  803. drawn_grid_rect = Rect2i(preview.begin()->key, Vector2i(0, 0));
  804. for (const KeyValue<Vector2i, TileMapCell> &E : preview) {
  805. drawn_grid_rect.expand_to(E.key);
  806. }
  807. drawn_grid_rect.size += Vector2i(1, 1);
  808. }
  809. }
  810. if (!preview.is_empty()) {
  811. const int fading = 5;
  812. // Draw the lines of the grid behind the preview.
  813. bool display_grid = EDITOR_GET("editors/tiles_editor/display_grid");
  814. if (display_grid) {
  815. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  816. if (drawn_grid_rect.size.x > 0 && drawn_grid_rect.size.y > 0) {
  817. drawn_grid_rect = drawn_grid_rect.grow(fading);
  818. for (int x = drawn_grid_rect.position.x; x < (drawn_grid_rect.position.x + drawn_grid_rect.size.x); x++) {
  819. for (int y = drawn_grid_rect.position.y; y < (drawn_grid_rect.position.y + drawn_grid_rect.size.y); y++) {
  820. Vector2i pos_in_rect = Vector2i(x, y) - drawn_grid_rect.position;
  821. // Fade out the border of the grid.
  822. float left_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.x), 0.0f, 1.0f);
  823. float right_opacity = CLAMP(Math::inverse_lerp((float)drawn_grid_rect.size.x, (float)(drawn_grid_rect.size.x - fading), (float)(pos_in_rect.x + 1)), 0.0f, 1.0f);
  824. float top_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.y), 0.0f, 1.0f);
  825. float bottom_opacity = CLAMP(Math::inverse_lerp((float)drawn_grid_rect.size.y, (float)(drawn_grid_rect.size.y - fading), (float)(pos_in_rect.y + 1)), 0.0f, 1.0f);
  826. float opacity = CLAMP(MIN(left_opacity, MIN(right_opacity, MIN(top_opacity, bottom_opacity))) + 0.1, 0.0f, 1.0f);
  827. Transform2D tile_xform;
  828. tile_xform.set_origin(tile_set->map_to_local(Vector2(x, y)));
  829. tile_xform.set_scale(tile_shape_size);
  830. Color color = grid_color;
  831. color.a = color.a * opacity;
  832. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, color, false);
  833. }
  834. }
  835. }
  836. }
  837. // Draw the preview.
  838. for (const KeyValue<Vector2i, TileMapCell> &E : preview) {
  839. Transform2D tile_xform;
  840. tile_xform.set_origin(tile_set->map_to_local(E.key));
  841. tile_xform.set_scale(tile_set->get_tile_size());
  842. if (!(drag_erasing || erase_button->is_pressed()) && random_tile_toggle->is_pressed()) {
  843. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true);
  844. } else {
  845. if (tile_set->has_source(E.value.source_id)) {
  846. TileSetSource *source = *tile_set->get_source(E.value.source_id);
  847. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  848. if (atlas_source) {
  849. // Get tile data.
  850. TileData *tile_data = atlas_source->get_tile_data(E.value.get_atlas_coords(), E.value.alternative_tile);
  851. if (!tile_data) {
  852. continue;
  853. }
  854. // Compute the offset
  855. Rect2i source_rect = atlas_source->get_tile_texture_region(E.value.get_atlas_coords());
  856. Vector2i tile_offset = tile_data->get_texture_origin();
  857. // Compute the destination rectangle in the CanvasItem.
  858. Rect2 dest_rect;
  859. dest_rect.size = source_rect.size;
  860. bool transpose = tile_data->get_transpose() ^ bool(E.value.alternative_tile & TileSetAtlasSource::TRANSFORM_TRANSPOSE);
  861. if (transpose) {
  862. dest_rect.position = (tile_set->map_to_local(E.key) - Vector2(dest_rect.size.y, dest_rect.size.x) / 2 - tile_offset);
  863. } else {
  864. dest_rect.position = (tile_set->map_to_local(E.key) - dest_rect.size / 2 - tile_offset);
  865. }
  866. if (tile_data->get_flip_h() ^ bool(E.value.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_H)) {
  867. dest_rect.size.x = -dest_rect.size.x;
  868. }
  869. if (tile_data->get_flip_v() ^ bool(E.value.alternative_tile & TileSetAtlasSource::TRANSFORM_FLIP_V)) {
  870. dest_rect.size.y = -dest_rect.size.y;
  871. }
  872. // Get the tile modulation.
  873. Color modulate = tile_data->get_modulate() * edited_layer->get_modulate_in_tree() * edited_layer->get_self_modulate();
  874. // Draw the tile.
  875. p_overlay->draw_set_transform_matrix(xform);
  876. p_overlay->draw_texture_rect_region(atlas_source->get_texture(), dest_rect, source_rect, modulate * Color(1.0, 1.0, 1.0, 0.5), transpose, tile_set->is_uv_clipping());
  877. p_overlay->draw_set_transform_matrix(Transform2D());
  878. } else {
  879. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true);
  880. }
  881. } else {
  882. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(0.0, 0.0, 0.0, 0.5), true);
  883. }
  884. }
  885. }
  886. }
  887. Ref<Font> font = p_overlay->get_theme_font(SceneStringName(font), SNAME("Label"));
  888. int font_size = p_overlay->get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  889. Point2 msgpos = Point2(20 * EDSCALE, p_overlay->get_size().y - 20 * EDSCALE);
  890. String text = tile_set->local_to_map(edited_layer->get_local_mouse_position());
  891. if (drag_type == DRAG_TYPE_RECT) {
  892. Vector2i size = tile_set->local_to_map(edited_layer->get_local_mouse_position()) - tile_set->local_to_map(drag_start_mouse_pos);
  893. text += vformat(" %s (%dx%d)", TTR("Drawing Rect:"), ABS(size.x) + 1, ABS(size.y) + 1);
  894. }
  895. p_overlay->draw_string(font, msgpos + Point2(1, 1), text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
  896. p_overlay->draw_string(font, msgpos + Point2(-1, -1), text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
  897. p_overlay->draw_string(font, msgpos, text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1));
  898. }
  899. }
  900. void TileMapLayerEditorTilesPlugin::_mouse_exited_viewport() {
  901. has_mouse = false;
  902. CanvasItemEditor::get_singleton()->update_viewport();
  903. }
  904. TileMapCell TileMapLayerEditorTilesPlugin::_pick_random_tile(Ref<TileMapPattern> p_pattern) {
  905. TileMapLayer *edited_layer = _get_edited_layer();
  906. if (!edited_layer) {
  907. return TileMapCell();
  908. }
  909. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  910. if (tile_set.is_null()) {
  911. return TileMapCell();
  912. }
  913. TypedArray<Vector2i> used_cells = p_pattern->get_used_cells();
  914. double sum = 0.0;
  915. for (int i = 0; i < used_cells.size(); i++) {
  916. int source_id = p_pattern->get_cell_source_id(used_cells[i]);
  917. Vector2i atlas_coords = p_pattern->get_cell_atlas_coords(used_cells[i]);
  918. int alternative_tile = p_pattern->get_cell_alternative_tile(used_cells[i]);
  919. TileSetSource *source = *tile_set->get_source(source_id);
  920. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  921. if (atlas_source) {
  922. TileData *tile_data = atlas_source->get_tile_data(atlas_coords, alternative_tile);
  923. ERR_FAIL_NULL_V(tile_data, TileMapCell());
  924. sum += tile_data->get_probability();
  925. } else {
  926. sum += 1.0;
  927. }
  928. }
  929. double empty_probability = sum * scattering;
  930. double current = 0.0;
  931. double rand = Math::random(0.0, sum + empty_probability);
  932. for (int i = 0; i < used_cells.size(); i++) {
  933. int source_id = p_pattern->get_cell_source_id(used_cells[i]);
  934. Vector2i atlas_coords = p_pattern->get_cell_atlas_coords(used_cells[i]);
  935. int alternative_tile = p_pattern->get_cell_alternative_tile(used_cells[i]);
  936. TileSetSource *source = *tile_set->get_source(source_id);
  937. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  938. if (atlas_source) {
  939. current += atlas_source->get_tile_data(atlas_coords, alternative_tile)->get_probability();
  940. } else {
  941. current += 1.0;
  942. }
  943. if (current >= rand) {
  944. return TileMapCell(source_id, atlas_coords, alternative_tile);
  945. }
  946. }
  947. return TileMapCell();
  948. }
  949. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTilesPlugin::_draw_line(Vector2 p_start_drag_mouse_pos, Vector2 p_from_mouse_pos, Vector2 p_to_mouse_pos, bool p_erase) {
  950. TileMapLayer *edited_layer = _get_edited_layer();
  951. if (!edited_layer) {
  952. return HashMap<Vector2i, TileMapCell>();
  953. }
  954. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  955. if (tile_set.is_null()) {
  956. return HashMap<Vector2i, TileMapCell>();
  957. }
  958. // Get or create the pattern.
  959. Ref<TileMapPattern> pattern = p_erase ? erase_pattern : selection_pattern;
  960. HashMap<Vector2i, TileMapCell> output;
  961. if (!pattern->is_empty()) {
  962. // Paint the tiles on the tile map.
  963. if (!p_erase && random_tile_toggle->is_pressed()) {
  964. // Paint a random tile.
  965. Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(p_from_mouse_pos), tile_set->local_to_map(p_to_mouse_pos));
  966. for (int i = 0; i < line.size(); i++) {
  967. output.insert(line[i], _pick_random_tile(pattern));
  968. }
  969. } else {
  970. // Paint the pattern.
  971. // If we paint several tiles, we virtually move the mouse as if it was in the center of the "brush"
  972. Vector2 mouse_offset = (Vector2(pattern->get_size()) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size();
  973. Vector2i last_hovered_cell = tile_set->local_to_map(p_from_mouse_pos - mouse_offset);
  974. Vector2i new_hovered_cell = tile_set->local_to_map(p_to_mouse_pos - mouse_offset);
  975. Vector2i drag_start_cell = tile_set->local_to_map(p_start_drag_mouse_pos - mouse_offset);
  976. TypedArray<Vector2i> used_cells = pattern->get_used_cells();
  977. Vector2i offset = Vector2i(Math::posmod(drag_start_cell.x, pattern->get_size().x), Math::posmod(drag_start_cell.y, pattern->get_size().y)); // Note: no posmodv for Vector2i for now. Meh.s
  978. Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, (last_hovered_cell - offset) / pattern->get_size(), (new_hovered_cell - offset) / pattern->get_size());
  979. for (int i = 0; i < line.size(); i++) {
  980. Vector2i top_left = line[i] * pattern->get_size() + offset;
  981. for (int j = 0; j < used_cells.size(); j++) {
  982. Vector2i coords = tile_set->map_pattern(top_left, used_cells[j], pattern);
  983. output.insert(coords, TileMapCell(pattern->get_cell_source_id(used_cells[j]), pattern->get_cell_atlas_coords(used_cells[j]), pattern->get_cell_alternative_tile(used_cells[j])));
  984. }
  985. }
  986. }
  987. }
  988. return output;
  989. }
  990. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTilesPlugin::_draw_rect(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase) {
  991. TileMapLayer *edited_layer = _get_edited_layer();
  992. if (!edited_layer) {
  993. return HashMap<Vector2i, TileMapCell>();
  994. }
  995. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  996. if (tile_set.is_null()) {
  997. return HashMap<Vector2i, TileMapCell>();
  998. }
  999. // Create the rect to draw.
  1000. Rect2i rect = Rect2i(p_start_cell, p_end_cell - p_start_cell).abs();
  1001. rect.size += Vector2i(1, 1);
  1002. // Get or create the pattern.
  1003. Ref<TileMapPattern> pattern = p_erase ? erase_pattern : selection_pattern;
  1004. HashMap<Vector2i, TileMapCell> err_output;
  1005. ERR_FAIL_COND_V(pattern->is_empty(), err_output);
  1006. // Compute the offset to align things to the bottom or right.
  1007. bool aligned_right = p_end_cell.x < p_start_cell.x;
  1008. bool valigned_bottom = p_end_cell.y < p_start_cell.y;
  1009. Vector2i offset = Vector2i(aligned_right ? -(pattern->get_size().x - (rect.get_size().x % pattern->get_size().x)) : 0, valigned_bottom ? -(pattern->get_size().y - (rect.get_size().y % pattern->get_size().y)) : 0);
  1010. HashMap<Vector2i, TileMapCell> output;
  1011. if (!pattern->is_empty()) {
  1012. if (!p_erase && random_tile_toggle->is_pressed()) {
  1013. // Paint a random tile.
  1014. for (int x = 0; x < rect.size.x; x++) {
  1015. for (int y = 0; y < rect.size.y; y++) {
  1016. Vector2i coords = rect.position + Vector2i(x, y);
  1017. output.insert(coords, _pick_random_tile(pattern));
  1018. }
  1019. }
  1020. } else {
  1021. // Paint the pattern.
  1022. TypedArray<Vector2i> used_cells = pattern->get_used_cells();
  1023. for (int x = 0; x <= rect.size.x / pattern->get_size().x; x++) {
  1024. for (int y = 0; y <= rect.size.y / pattern->get_size().y; y++) {
  1025. Vector2i pattern_coords = rect.position + Vector2i(x, y) * pattern->get_size() + offset;
  1026. for (int j = 0; j < used_cells.size(); j++) {
  1027. Vector2i coords = pattern_coords + used_cells[j];
  1028. if (rect.has_point(coords)) {
  1029. output.insert(coords, TileMapCell(pattern->get_cell_source_id(used_cells[j]), pattern->get_cell_atlas_coords(used_cells[j]), pattern->get_cell_alternative_tile(used_cells[j])));
  1030. }
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }
  1036. return output;
  1037. }
  1038. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTilesPlugin::_draw_bucket_fill(Vector2i p_coords, bool p_contiguous, bool p_erase) {
  1039. TileMapLayer *edited_layer = _get_edited_layer();
  1040. if (!edited_layer) {
  1041. return HashMap<Vector2i, TileMapCell>();
  1042. }
  1043. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1044. if (tile_set.is_null()) {
  1045. return HashMap<Vector2i, TileMapCell>();
  1046. }
  1047. HashMap<Vector2i, TileMapCell> output;
  1048. // Get or create the pattern.
  1049. Ref<TileMapPattern> pattern = p_erase ? erase_pattern : selection_pattern;
  1050. if (!pattern->is_empty()) {
  1051. TileMapCell source_cell = edited_layer->get_cell(p_coords);
  1052. // If we are filling empty tiles, compute the tilemap boundaries.
  1053. Rect2i boundaries;
  1054. if (source_cell.source_id == TileSet::INVALID_SOURCE) {
  1055. boundaries = edited_layer->get_used_rect();
  1056. }
  1057. if (p_contiguous) {
  1058. // Replace continuous tiles like the source.
  1059. RBSet<Vector2i> already_checked;
  1060. List<Vector2i> to_check;
  1061. to_check.push_back(p_coords);
  1062. while (!to_check.is_empty()) {
  1063. Vector2i coords = to_check.back()->get();
  1064. to_check.pop_back();
  1065. if (!already_checked.has(coords)) {
  1066. if (source_cell.source_id == edited_layer->get_cell_source_id(coords) &&
  1067. source_cell.get_atlas_coords() == edited_layer->get_cell_atlas_coords(coords) &&
  1068. source_cell.alternative_tile == edited_layer->get_cell_alternative_tile(coords) &&
  1069. (source_cell.source_id != TileSet::INVALID_SOURCE || boundaries.has_point(coords))) {
  1070. if (!p_erase && random_tile_toggle->is_pressed()) {
  1071. // Paint a random tile.
  1072. output.insert(coords, _pick_random_tile(pattern));
  1073. } else {
  1074. // Paint the pattern.
  1075. Vector2i pattern_coords = (coords - p_coords) % pattern->get_size(); // Note: it would be good to have posmodv for Vector2i.
  1076. pattern_coords.x = pattern_coords.x < 0 ? pattern_coords.x + pattern->get_size().x : pattern_coords.x;
  1077. pattern_coords.y = pattern_coords.y < 0 ? pattern_coords.y + pattern->get_size().y : pattern_coords.y;
  1078. if (pattern->has_cell(pattern_coords)) {
  1079. output.insert(coords, TileMapCell(pattern->get_cell_source_id(pattern_coords), pattern->get_cell_atlas_coords(pattern_coords), pattern->get_cell_alternative_tile(pattern_coords)));
  1080. } else {
  1081. output.insert(coords, TileMapCell());
  1082. }
  1083. }
  1084. // Get surrounding tiles (handles different tile shapes).
  1085. TypedArray<Vector2i> around = tile_set->get_surrounding_cells(coords);
  1086. for (int i = 0; i < around.size(); i++) {
  1087. to_check.push_back(around[i]);
  1088. }
  1089. }
  1090. already_checked.insert(coords);
  1091. }
  1092. }
  1093. } else {
  1094. // Replace all tiles like the source.
  1095. TypedArray<Vector2i> to_check;
  1096. if (source_cell.source_id == TileSet::INVALID_SOURCE) {
  1097. Rect2i rect = edited_layer->get_used_rect();
  1098. if (!rect.has_area()) {
  1099. rect = Rect2i(p_coords, Vector2i(1, 1));
  1100. }
  1101. for (int x = boundaries.position.x; x < boundaries.get_end().x; x++) {
  1102. for (int y = boundaries.position.y; y < boundaries.get_end().y; y++) {
  1103. to_check.append(Vector2i(x, y));
  1104. }
  1105. }
  1106. } else {
  1107. to_check = edited_layer->get_used_cells();
  1108. }
  1109. for (int i = 0; i < to_check.size(); i++) {
  1110. Vector2i coords = to_check[i];
  1111. if (source_cell.source_id == edited_layer->get_cell_source_id(coords) &&
  1112. source_cell.get_atlas_coords() == edited_layer->get_cell_atlas_coords(coords) &&
  1113. source_cell.alternative_tile == edited_layer->get_cell_alternative_tile(coords) &&
  1114. (source_cell.source_id != TileSet::INVALID_SOURCE || boundaries.has_point(coords))) {
  1115. if (!p_erase && random_tile_toggle->is_pressed()) {
  1116. // Paint a random tile.
  1117. output.insert(coords, _pick_random_tile(pattern));
  1118. } else {
  1119. // Paint the pattern.
  1120. Vector2i pattern_coords = (coords - p_coords) % pattern->get_size(); // Note: it would be good to have posmodv for Vector2i.
  1121. pattern_coords.x = pattern_coords.x < 0 ? pattern_coords.x + pattern->get_size().x : pattern_coords.x;
  1122. pattern_coords.y = pattern_coords.y < 0 ? pattern_coords.y + pattern->get_size().y : pattern_coords.y;
  1123. if (pattern->has_cell(pattern_coords)) {
  1124. output.insert(coords, TileMapCell(pattern->get_cell_source_id(pattern_coords), pattern->get_cell_atlas_coords(pattern_coords), pattern->get_cell_alternative_tile(pattern_coords)));
  1125. } else {
  1126. output.insert(coords, TileMapCell());
  1127. }
  1128. }
  1129. }
  1130. }
  1131. }
  1132. }
  1133. return output;
  1134. }
  1135. void TileMapLayerEditorTilesPlugin::_stop_dragging() {
  1136. if (drag_type == DRAG_TYPE_NONE) {
  1137. return;
  1138. }
  1139. TileMapLayer *edited_layer = _get_edited_layer();
  1140. if (!edited_layer) {
  1141. return;
  1142. }
  1143. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1144. if (tile_set.is_null()) {
  1145. return;
  1146. }
  1147. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  1148. Vector2 mpos = xform.affine_inverse().xform(CanvasItemEditor::get_singleton()->get_viewport_control()->get_local_mouse_position());
  1149. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1150. switch (drag_type) {
  1151. case DRAG_TYPE_SELECT: {
  1152. undo_redo->create_action_for_history(TTR("Change selection"), EditorNode::get_editor_data().get_current_edited_scene_history_id());
  1153. undo_redo->add_undo_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  1154. if (!Input::get_singleton()->is_key_pressed(Key::SHIFT) && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1155. tile_map_selection.clear();
  1156. }
  1157. Rect2i rect = Rect2i(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos) - tile_set->local_to_map(drag_start_mouse_pos)).abs();
  1158. for (int x = rect.position.x; x <= rect.get_end().x; x++) {
  1159. for (int y = rect.position.y; y <= rect.get_end().y; y++) {
  1160. Vector2i coords = Vector2i(x, y);
  1161. if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1162. if (tile_map_selection.has(coords)) {
  1163. tile_map_selection.erase(coords);
  1164. }
  1165. } else {
  1166. if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  1167. tile_map_selection.insert(coords);
  1168. }
  1169. }
  1170. }
  1171. }
  1172. undo_redo->add_do_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  1173. undo_redo->commit_action(false);
  1174. _update_selection_pattern_from_tilemap_selection();
  1175. _update_tileset_selection_from_selection_pattern();
  1176. } break;
  1177. case DRAG_TYPE_MOVE: {
  1178. if (patterns_item_list->is_visible_in_tree() && patterns_item_list->has_point(patterns_item_list->get_local_mouse_position())) {
  1179. // Restore the cells.
  1180. for (KeyValue<Vector2i, TileMapCell> kv : drag_modified) {
  1181. edited_layer->set_cell(kv.key, kv.value.source_id, kv.value.get_atlas_coords(), kv.value.alternative_tile);
  1182. }
  1183. if (!EditorNode::get_singleton()->is_resource_read_only(tile_set)) {
  1184. // Creating a pattern in the pattern list.
  1185. select_last_pattern = true;
  1186. int new_pattern_index = tile_set->get_patterns_count();
  1187. undo_redo->create_action(TTR("Add TileSet pattern"));
  1188. undo_redo->add_do_method(*tile_set, "add_pattern", selection_pattern, new_pattern_index);
  1189. undo_redo->add_undo_method(*tile_set, "remove_pattern", new_pattern_index);
  1190. undo_redo->commit_action();
  1191. }
  1192. } else {
  1193. // Get the top-left cell.
  1194. Vector2i top_left;
  1195. if (!tile_map_selection.is_empty()) {
  1196. top_left = tile_map_selection.front()->get();
  1197. }
  1198. for (const Vector2i &E : tile_map_selection) {
  1199. top_left = top_left.min(E);
  1200. }
  1201. // Get the offset from the mouse.
  1202. Vector2i offset = drag_start_mouse_pos - tile_set->map_to_local(top_left);
  1203. offset = tile_set->local_to_map(mpos - offset) - tile_set->local_to_map(drag_start_mouse_pos - offset);
  1204. TypedArray<Vector2i> selection_used_cells = selection_pattern->get_used_cells();
  1205. // Build the list of cells to undo.
  1206. Vector2i coords;
  1207. HashMap<Vector2i, TileMapCell> cells_undo;
  1208. for (int i = 0; i < selection_used_cells.size(); i++) {
  1209. coords = tile_set->map_pattern(top_left + offset, selection_used_cells[i], selection_pattern);
  1210. cells_undo[coords] = TileMapCell(edited_layer->get_cell_source_id(coords), edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords));
  1211. }
  1212. for (int i = 0; i < selection_used_cells.size(); i++) {
  1213. coords = tile_set->map_pattern(top_left, selection_used_cells[i], selection_pattern);
  1214. cells_undo[coords] = TileMapCell(drag_modified[coords].source_id, drag_modified[coords].get_atlas_coords(), drag_modified[coords].alternative_tile);
  1215. }
  1216. // Build the list of cells to do.
  1217. HashMap<Vector2i, TileMapCell> cells_do;
  1218. for (int i = 0; i < selection_used_cells.size(); i++) {
  1219. coords = tile_set->map_pattern(top_left, selection_used_cells[i], selection_pattern);
  1220. cells_do[coords] = TileMapCell();
  1221. }
  1222. for (int i = 0; i < selection_used_cells.size(); i++) {
  1223. coords = tile_set->map_pattern(top_left + offset, selection_used_cells[i], selection_pattern);
  1224. cells_do[coords] = TileMapCell(selection_pattern->get_cell_source_id(selection_used_cells[i]), selection_pattern->get_cell_atlas_coords(selection_used_cells[i]), selection_pattern->get_cell_alternative_tile(selection_used_cells[i]));
  1225. }
  1226. // Move the tiles.
  1227. undo_redo->create_action(TTR("Move tiles"));
  1228. for (const KeyValue<Vector2i, TileMapCell> &E : cells_do) {
  1229. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1230. }
  1231. for (const KeyValue<Vector2i, TileMapCell> &E : cells_undo) {
  1232. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1233. }
  1234. // Update the selection.
  1235. undo_redo->add_undo_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  1236. tile_map_selection.clear();
  1237. for (int i = 0; i < selection_used_cells.size(); i++) {
  1238. coords = tile_set->map_pattern(top_left + offset, selection_used_cells[i], selection_pattern);
  1239. tile_map_selection.insert(coords);
  1240. }
  1241. undo_redo->add_do_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  1242. undo_redo->commit_action();
  1243. }
  1244. } break;
  1245. case DRAG_TYPE_PICK: {
  1246. Rect2i rect = Rect2i(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos) - tile_set->local_to_map(drag_start_mouse_pos)).abs();
  1247. rect.size += Vector2i(1, 1);
  1248. int picked_source = -1;
  1249. TypedArray<Vector2i> coords_array;
  1250. for (int x = rect.position.x; x < rect.get_end().x; x++) {
  1251. for (int y = rect.position.y; y < rect.get_end().y; y++) {
  1252. Vector2i coords = Vector2i(x, y);
  1253. int source = edited_layer->get_cell_source_id(coords);
  1254. if (source != TileSet::INVALID_SOURCE) {
  1255. coords_array.push_back(coords);
  1256. if (picked_source == -1) {
  1257. picked_source = source;
  1258. } else if (picked_source != source) {
  1259. picked_source = -2;
  1260. }
  1261. }
  1262. }
  1263. }
  1264. if (picked_source >= 0) {
  1265. for (int i = 0; i < sources_list->get_item_count(); i++) {
  1266. if (int(sources_list->get_item_metadata(i)) == picked_source) {
  1267. sources_list->set_current(i);
  1268. TilesEditorUtils::get_singleton()->set_sources_lists_current(i);
  1269. break;
  1270. }
  1271. }
  1272. sources_list->ensure_current_is_visible();
  1273. }
  1274. Ref<TileMapPattern> new_selection_pattern = edited_layer->get_pattern(coords_array);
  1275. if (!new_selection_pattern->is_empty()) {
  1276. selection_pattern = new_selection_pattern;
  1277. _update_tileset_selection_from_selection_pattern();
  1278. }
  1279. picker_button->set_pressed(false);
  1280. } break;
  1281. case DRAG_TYPE_PAINT: {
  1282. undo_redo->create_action(TTR("Paint tiles"));
  1283. for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) {
  1284. undo_redo->add_do_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key));
  1285. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1286. }
  1287. undo_redo->commit_action(false);
  1288. } break;
  1289. case DRAG_TYPE_LINE: {
  1290. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(drag_start_mouse_pos, drag_start_mouse_pos, mpos, drag_erasing);
  1291. undo_redo->create_action(TTR("Paint tiles"));
  1292. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  1293. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  1294. continue;
  1295. }
  1296. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1297. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key));
  1298. }
  1299. undo_redo->commit_action();
  1300. } break;
  1301. case DRAG_TYPE_RECT: {
  1302. HashMap<Vector2i, TileMapCell> to_draw = _draw_rect(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing);
  1303. undo_redo->create_action(TTR("Paint tiles"));
  1304. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  1305. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  1306. continue;
  1307. }
  1308. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1309. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key));
  1310. }
  1311. undo_redo->commit_action();
  1312. } break;
  1313. case DRAG_TYPE_BUCKET: {
  1314. undo_redo->create_action(TTR("Paint tiles"));
  1315. for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) {
  1316. undo_redo->add_do_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key));
  1317. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1318. }
  1319. undo_redo->commit_action(false);
  1320. } break;
  1321. case DRAG_TYPE_CLIPBOARD_PASTE: {
  1322. Vector2 mouse_offset = (Vector2(tile_map_clipboard->get_size()) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size();
  1323. undo_redo->create_action(TTR("Paste tiles"));
  1324. TypedArray<Vector2i> used_cells = tile_map_clipboard->get_used_cells();
  1325. for (int i = 0; i < used_cells.size(); i++) {
  1326. Vector2i coords = tile_set->map_pattern(tile_set->local_to_map(mpos - mouse_offset), used_cells[i], tile_map_clipboard);
  1327. undo_redo->add_do_method(edited_layer, "set_cell", coords, tile_map_clipboard->get_cell_source_id(used_cells[i]), tile_map_clipboard->get_cell_atlas_coords(used_cells[i]), tile_map_clipboard->get_cell_alternative_tile(used_cells[i]));
  1328. undo_redo->add_undo_method(edited_layer, "set_cell", coords, edited_layer->get_cell_source_id(coords), edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords));
  1329. }
  1330. undo_redo->commit_action();
  1331. } break;
  1332. default:
  1333. break;
  1334. }
  1335. drag_type = DRAG_TYPE_NONE;
  1336. }
  1337. void TileMapLayerEditorTilesPlugin::_apply_transform(int p_type) {
  1338. if (selection_pattern.is_null() || selection_pattern->is_empty()) {
  1339. return;
  1340. }
  1341. Ref<TileMapPattern> transformed_pattern;
  1342. transformed_pattern.instantiate();
  1343. bool keep_shape = selection_pattern->get_size() == Vector2i(1, 1);
  1344. Vector2i size = selection_pattern->get_size();
  1345. for (int y = 0; y < size.y; y++) {
  1346. for (int x = 0; x < size.x; x++) {
  1347. Vector2i src_coords = Vector2i(x, y);
  1348. if (!selection_pattern->has_cell(src_coords)) {
  1349. continue;
  1350. }
  1351. Vector2i dst_coords;
  1352. if (keep_shape) {
  1353. dst_coords = src_coords;
  1354. } else if (p_type == TRANSFORM_ROTATE_LEFT) {
  1355. dst_coords = Vector2i(y, size.x - x - 1);
  1356. } else if (p_type == TRANSFORM_ROTATE_RIGHT) {
  1357. dst_coords = Vector2i(size.y - y - 1, x);
  1358. } else if (p_type == TRANSFORM_FLIP_H) {
  1359. dst_coords = Vector2i(size.x - x - 1, y);
  1360. } else if (p_type == TRANSFORM_FLIP_V) {
  1361. dst_coords = Vector2i(x, size.y - y - 1);
  1362. }
  1363. transformed_pattern->set_cell(dst_coords,
  1364. selection_pattern->get_cell_source_id(src_coords), selection_pattern->get_cell_atlas_coords(src_coords),
  1365. _get_transformed_alternative(selection_pattern->get_cell_alternative_tile(src_coords), p_type));
  1366. }
  1367. }
  1368. selection_pattern = transformed_pattern;
  1369. CanvasItemEditor::get_singleton()->update_viewport();
  1370. }
  1371. int TileMapLayerEditorTilesPlugin::_get_transformed_alternative(int p_alternative_id, int p_transform) {
  1372. bool transform_flip_h = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_H;
  1373. bool transform_flip_v = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_V;
  1374. bool transform_transpose = p_alternative_id & TileSetAtlasSource::TRANSFORM_TRANSPOSE;
  1375. switch (p_transform) {
  1376. case TRANSFORM_ROTATE_LEFT:
  1377. case TRANSFORM_ROTATE_RIGHT: {
  1378. // A matrix with every possible flip/transpose combination, sorted by what comes next when you rotate.
  1379. const LocalVector<bool> rotation_matrix = {
  1380. // NOLINTBEGIN(modernize-use-bool-literals)
  1381. 0, 0, 0,
  1382. 0, 1, 1,
  1383. 1, 1, 0,
  1384. 1, 0, 1,
  1385. 1, 0, 0,
  1386. 0, 0, 1,
  1387. 0, 1, 0,
  1388. 1, 1, 1
  1389. // NOLINTEND(modernize-use-bool-literals)
  1390. };
  1391. for (int i = 0; i < 8; i++) {
  1392. if (transform_flip_h == rotation_matrix[i * 3] && transform_flip_v == rotation_matrix[i * 3 + 1] && transform_transpose == rotation_matrix[i * 3 + 2]) {
  1393. if (p_transform == TRANSFORM_ROTATE_LEFT) {
  1394. i = i / 4 * 4 + (i + 1) % 4;
  1395. } else {
  1396. i = i / 4 * 4 + Math::posmod(i - 1, 4);
  1397. }
  1398. transform_flip_h = rotation_matrix[i * 3];
  1399. transform_flip_v = rotation_matrix[i * 3 + 1];
  1400. transform_transpose = rotation_matrix[i * 3 + 2];
  1401. break;
  1402. }
  1403. }
  1404. } break;
  1405. case TRANSFORM_FLIP_H: {
  1406. transform_flip_h = !transform_flip_h;
  1407. } break;
  1408. case TRANSFORM_FLIP_V: {
  1409. transform_flip_v = !transform_flip_v;
  1410. } break;
  1411. }
  1412. return TileSetAtlasSource::alternative_no_transform(p_alternative_id) |
  1413. int(transform_flip_h) * TileSetAtlasSource::TRANSFORM_FLIP_H |
  1414. int(transform_flip_v) * TileSetAtlasSource::TRANSFORM_FLIP_V |
  1415. int(transform_transpose) * TileSetAtlasSource::TRANSFORM_TRANSPOSE;
  1416. }
  1417. void TileMapLayerEditorTilesPlugin::_update_fix_selected_and_hovered() {
  1418. TileMapLayer *edited_layer = _get_edited_layer();
  1419. if (!edited_layer) {
  1420. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1421. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1422. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1423. tile_set_selection.clear();
  1424. patterns_item_list->deselect_all();
  1425. tile_map_selection.clear();
  1426. selection_pattern.instantiate();
  1427. return;
  1428. }
  1429. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1430. if (tile_set.is_null()) {
  1431. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1432. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1433. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1434. tile_set_selection.clear();
  1435. patterns_item_list->deselect_all();
  1436. tile_map_selection.clear();
  1437. selection_pattern.instantiate();
  1438. return;
  1439. }
  1440. int source_index = sources_list->get_current();
  1441. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1442. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1443. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1444. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1445. tile_set_selection.clear();
  1446. patterns_item_list->deselect_all();
  1447. tile_map_selection.clear();
  1448. selection_pattern.instantiate();
  1449. return;
  1450. }
  1451. int source_id = sources_list->get_item_metadata(source_index);
  1452. // Clear hovered if needed.
  1453. if (source_id != hovered_tile.source_id ||
  1454. !tile_set->has_source(hovered_tile.source_id) ||
  1455. !tile_set->get_source(hovered_tile.source_id)->has_tile(hovered_tile.get_atlas_coords()) ||
  1456. !tile_set->get_source(hovered_tile.source_id)->has_alternative_tile(hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile)) {
  1457. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1458. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1459. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1460. }
  1461. // Cleanup tile set selection.
  1462. for (RBSet<TileMapCell>::Element *E = tile_set_selection.front(); E;) {
  1463. RBSet<TileMapCell>::Element *N = E->next();
  1464. const TileMapCell *selected = &(E->get());
  1465. if (!tile_set->has_source(selected->source_id) ||
  1466. !tile_set->get_source(selected->source_id)->has_tile(selected->get_atlas_coords()) ||
  1467. !tile_set->get_source(selected->source_id)->has_alternative_tile(selected->get_atlas_coords(), selected->alternative_tile)) {
  1468. tile_set_selection.erase(E);
  1469. }
  1470. E = N;
  1471. }
  1472. // Cleanup selection.
  1473. for (const KeyValue<Vector2i, TileMapCell> &E : selection_pattern->get_pattern()) {
  1474. const Vector2i key = E.key;
  1475. const TileMapCell &selected = E.value;
  1476. if (!tile_set->has_source(selected.source_id) ||
  1477. !tile_set->get_source(selected.source_id)->has_tile(selected.get_atlas_coords()) ||
  1478. !tile_set->get_source(selected.source_id)->has_alternative_tile(selected.get_atlas_coords(), selected.alternative_tile)) {
  1479. selection_pattern->remove_cell(key);
  1480. }
  1481. }
  1482. }
  1483. void TileMapLayerEditorTilesPlugin::_fix_invalid_tiles_in_tile_map_selection() {
  1484. TileMapLayer *edited_layer = _get_edited_layer();
  1485. if (!edited_layer) {
  1486. return;
  1487. }
  1488. RBSet<Vector2i> to_remove;
  1489. for (Vector2i selected : tile_map_selection) {
  1490. TileMapCell cell = edited_layer->get_cell(selected);
  1491. if (cell.source_id == TileSet::INVALID_SOURCE && cell.get_atlas_coords() == TileSetSource::INVALID_ATLAS_COORDS && cell.alternative_tile == TileSetAtlasSource::INVALID_TILE_ALTERNATIVE) {
  1492. to_remove.insert(selected);
  1493. }
  1494. }
  1495. for (Vector2i cell : to_remove) {
  1496. tile_map_selection.erase(cell);
  1497. }
  1498. }
  1499. void TileMapLayerEditorTilesPlugin::patterns_item_list_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index) {
  1500. if (p_mouse_button_index == MouseButton::LEFT) {
  1501. _update_selection_pattern_from_tileset_pattern_selection();
  1502. }
  1503. }
  1504. void TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tilemap_selection() {
  1505. TileMapLayer *edited_layer = _get_edited_layer();
  1506. if (!edited_layer) {
  1507. return;
  1508. }
  1509. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1510. if (tile_set.is_null()) {
  1511. return;
  1512. }
  1513. selection_pattern.instantiate();
  1514. TypedArray<Vector2i> coords_array;
  1515. for (const Vector2i &E : tile_map_selection) {
  1516. coords_array.push_back(E);
  1517. }
  1518. selection_pattern = edited_layer->get_pattern(coords_array);
  1519. _update_transform_buttons();
  1520. }
  1521. void TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_tiles_selection() {
  1522. TileMapLayer *edited_layer = _get_edited_layer();
  1523. if (!edited_layer) {
  1524. return;
  1525. }
  1526. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1527. if (tile_set.is_null()) {
  1528. return;
  1529. }
  1530. // Clear the tilemap selection.
  1531. tile_map_selection.clear();
  1532. // Clear the selected pattern.
  1533. selection_pattern.instantiate();
  1534. // Group per source.
  1535. HashMap<int, List<const TileMapCell *>> per_source;
  1536. for (const TileMapCell &E : tile_set_selection) {
  1537. per_source[E.source_id].push_back(&(E));
  1538. }
  1539. int vertical_offset = 0;
  1540. for (const KeyValue<int, List<const TileMapCell *>> &E_source : per_source) {
  1541. // Per source.
  1542. List<const TileMapCell *> unorganized;
  1543. Rect2i encompassing_rect_coords;
  1544. HashMap<Vector2i, const TileMapCell *> organized_pattern;
  1545. TileSetSource *source = *tile_set->get_source(E_source.key);
  1546. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1547. if (atlas_source) {
  1548. // Organize using coordinates.
  1549. for (const TileMapCell *current : E_source.value) {
  1550. if (current->alternative_tile == 0) {
  1551. organized_pattern[current->get_atlas_coords()] = current;
  1552. } else {
  1553. unorganized.push_back(current);
  1554. }
  1555. }
  1556. // Compute the encompassing rect for the organized pattern.
  1557. HashMap<Vector2i, const TileMapCell *>::Iterator E_cell = organized_pattern.begin();
  1558. if (E_cell) {
  1559. encompassing_rect_coords = Rect2i(E_cell->key, Vector2i(1, 1));
  1560. for (; E_cell; ++E_cell) {
  1561. encompassing_rect_coords.expand_to(E_cell->key + Vector2i(1, 1));
  1562. encompassing_rect_coords.expand_to(E_cell->key);
  1563. }
  1564. }
  1565. } else {
  1566. // Add everything unorganized.
  1567. for (const TileMapCell *cell : E_source.value) {
  1568. unorganized.push_back(cell);
  1569. }
  1570. }
  1571. // Now add everything to the output pattern.
  1572. for (const KeyValue<Vector2i, const TileMapCell *> &E_cell : organized_pattern) {
  1573. selection_pattern->set_cell(E_cell.key - encompassing_rect_coords.position + Vector2i(0, vertical_offset), E_cell.value->source_id, E_cell.value->get_atlas_coords(), E_cell.value->alternative_tile);
  1574. }
  1575. Vector2i organized_size = selection_pattern->get_size();
  1576. int unorganized_index = 0;
  1577. for (const TileMapCell *cell : unorganized) {
  1578. selection_pattern->set_cell(Vector2(organized_size.x + unorganized_index, vertical_offset), cell->source_id, cell->get_atlas_coords(), cell->alternative_tile);
  1579. unorganized_index++;
  1580. }
  1581. vertical_offset += MAX(organized_size.y, 1);
  1582. }
  1583. CanvasItemEditor::get_singleton()->update_viewport();
  1584. _update_transform_buttons();
  1585. }
  1586. void TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_pattern_selection() {
  1587. TileMapLayer *edited_layer = _get_edited_layer();
  1588. if (!edited_layer) {
  1589. return;
  1590. }
  1591. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1592. if (tile_set.is_null()) {
  1593. return;
  1594. }
  1595. // Clear the tilemap selection.
  1596. tile_map_selection.clear();
  1597. // Clear the selected pattern.
  1598. selection_pattern.instantiate();
  1599. if (patterns_item_list->get_selected_items().size() >= 1) {
  1600. selection_pattern = patterns_item_list->get_item_metadata(patterns_item_list->get_selected_items()[0]);
  1601. }
  1602. CanvasItemEditor::get_singleton()->update_viewport();
  1603. }
  1604. void TileMapLayerEditorTilesPlugin::_update_tileset_selection_from_selection_pattern() {
  1605. tile_set_selection.clear();
  1606. TypedArray<Vector2i> used_cells = selection_pattern->get_used_cells();
  1607. for (int i = 0; i < used_cells.size(); i++) {
  1608. Vector2i coords = used_cells[i];
  1609. if (selection_pattern->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  1610. tile_set_selection.insert(TileMapCell(selection_pattern->get_cell_source_id(coords), selection_pattern->get_cell_atlas_coords(coords), selection_pattern->get_cell_alternative_tile(coords)));
  1611. }
  1612. }
  1613. _update_source_display();
  1614. tile_atlas_control->queue_redraw();
  1615. alternative_tiles_control->queue_redraw();
  1616. _update_transform_buttons();
  1617. }
  1618. void TileMapLayerEditorTilesPlugin::_tile_atlas_control_draw() {
  1619. TileMapLayer *edited_layer = _get_edited_layer();
  1620. if (!edited_layer) {
  1621. return;
  1622. }
  1623. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1624. if (tile_set.is_null()) {
  1625. return;
  1626. }
  1627. int source_index = sources_list->get_current();
  1628. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1629. return;
  1630. }
  1631. int source_id = sources_list->get_item_metadata(source_index);
  1632. if (!tile_set->has_source(source_id)) {
  1633. return;
  1634. }
  1635. TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  1636. if (!atlas) {
  1637. return;
  1638. }
  1639. // Draw the selection.
  1640. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  1641. Color selection_color = Color().from_hsv(Math::fposmod(grid_color.get_h() + 0.5, 1.0), grid_color.get_s(), grid_color.get_v(), 1.0);
  1642. for (const TileMapCell &E : tile_set_selection) {
  1643. int16_t untransformed_alternative_id = E.alternative_tile & TileSetAtlasSource::UNTRANSFORM_MASK;
  1644. if (E.source_id == source_id && untransformed_alternative_id == 0) {
  1645. for (int frame = 0; frame < atlas->get_tile_animation_frames_count(E.get_atlas_coords()); frame++) {
  1646. Color color = selection_color;
  1647. if (frame > 0) {
  1648. color.a *= 0.3;
  1649. }
  1650. TilesEditorUtils::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(E.get_atlas_coords(), frame), color);
  1651. }
  1652. }
  1653. }
  1654. // Draw the hovered tile.
  1655. if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile == 0 && !tile_set_dragging_selection) {
  1656. for (int frame = 0; frame < atlas->get_tile_animation_frames_count(hovered_tile.get_atlas_coords()); frame++) {
  1657. Color color = Color(1.0, 0.8, 0.0, frame == 0 ? 0.6 : 0.3);
  1658. TilesEditorUtils::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(hovered_tile.get_atlas_coords(), frame), color);
  1659. }
  1660. }
  1661. // Draw the selection rect.
  1662. if (tile_set_dragging_selection) {
  1663. Vector2i start_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_set_drag_start_mouse_pos, true);
  1664. Vector2i end_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1665. Rect2i region = Rect2i(start_tile, end_tile - start_tile).abs();
  1666. region.size += Vector2i(1, 1);
  1667. RBSet<Vector2i> to_draw;
  1668. for (int x = region.position.x; x < region.get_end().x; x++) {
  1669. for (int y = region.position.y; y < region.get_end().y; y++) {
  1670. Vector2i tile = atlas->get_tile_at_coords(Vector2i(x, y));
  1671. if (tile != TileSetSource::INVALID_ATLAS_COORDS) {
  1672. to_draw.insert(tile);
  1673. }
  1674. }
  1675. }
  1676. for (const Vector2i &E : to_draw) {
  1677. TilesEditorUtils::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(E));
  1678. }
  1679. }
  1680. }
  1681. void TileMapLayerEditorTilesPlugin::_tile_atlas_control_mouse_exited() {
  1682. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1683. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1684. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1685. tile_atlas_control->queue_redraw();
  1686. }
  1687. void TileMapLayerEditorTilesPlugin::_tile_atlas_control_gui_input(const Ref<InputEvent> &p_event) {
  1688. TileMapLayer *edited_layer = _get_edited_layer();
  1689. if (!edited_layer) {
  1690. return;
  1691. }
  1692. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1693. if (tile_set.is_null()) {
  1694. return;
  1695. }
  1696. int source_index = sources_list->get_current();
  1697. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1698. return;
  1699. }
  1700. int source_id = sources_list->get_item_metadata(source_index);
  1701. if (!tile_set->has_source(source_id)) {
  1702. return;
  1703. }
  1704. TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  1705. if (!atlas) {
  1706. return;
  1707. }
  1708. // Update the hovered tile
  1709. hovered_tile.source_id = source_id;
  1710. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1711. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1712. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  1713. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1714. coords = atlas->get_tile_at_coords(coords);
  1715. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1716. hovered_tile.set_atlas_coords(coords);
  1717. hovered_tile.alternative_tile = 0;
  1718. }
  1719. }
  1720. Ref<InputEventMouseMotion> mm = p_event;
  1721. if (mm.is_valid()) {
  1722. tile_atlas_control->queue_redraw();
  1723. alternative_tiles_control->queue_redraw();
  1724. }
  1725. Ref<InputEventMouseButton> mb = p_event;
  1726. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  1727. if (mb->is_pressed()) { // Pressed
  1728. tile_set_dragging_selection = true;
  1729. tile_set_drag_start_mouse_pos = tile_atlas_control->get_local_mouse_position();
  1730. if (!mb->is_shift_pressed()) {
  1731. tile_set_selection.clear();
  1732. }
  1733. if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile == 0) {
  1734. if (mb->is_shift_pressed() && tile_set_selection.has(TileMapCell(source_id, hovered_tile.get_atlas_coords(), 0))) {
  1735. tile_set_selection.erase(TileMapCell(source_id, hovered_tile.get_atlas_coords(), 0));
  1736. } else {
  1737. tile_set_selection.insert(TileMapCell(source_id, hovered_tile.get_atlas_coords(), 0));
  1738. }
  1739. }
  1740. _update_selection_pattern_from_tileset_tiles_selection();
  1741. } else { // Released
  1742. if (tile_set_dragging_selection) {
  1743. if (!mb->is_shift_pressed()) {
  1744. tile_set_selection.clear();
  1745. }
  1746. // Compute the covered area.
  1747. Vector2i start_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_set_drag_start_mouse_pos, true);
  1748. Vector2i end_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1749. if (start_tile != TileSetSource::INVALID_ATLAS_COORDS && end_tile != TileSetSource::INVALID_ATLAS_COORDS) {
  1750. Rect2i region = Rect2i(start_tile, end_tile - start_tile).abs();
  1751. region.size += Vector2i(1, 1);
  1752. // To update the selection, we copy the selected/not selected status of the tiles we drag from.
  1753. Vector2i start_coords = atlas->get_tile_at_coords(start_tile);
  1754. if (mb->is_shift_pressed() && start_coords != TileSetSource::INVALID_ATLAS_COORDS && !tile_set_selection.has(TileMapCell(source_id, start_coords, 0))) {
  1755. // Remove from the selection.
  1756. for (int x = region.position.x; x < region.get_end().x; x++) {
  1757. for (int y = region.position.y; y < region.get_end().y; y++) {
  1758. Vector2i tile_coords = atlas->get_tile_at_coords(Vector2i(x, y));
  1759. if (tile_coords != TileSetSource::INVALID_ATLAS_COORDS && tile_set_selection.has(TileMapCell(source_id, tile_coords, 0))) {
  1760. tile_set_selection.erase(TileMapCell(source_id, tile_coords, 0));
  1761. }
  1762. }
  1763. }
  1764. } else {
  1765. // Insert in the selection.
  1766. for (int x = region.position.x; x < region.get_end().x; x++) {
  1767. for (int y = region.position.y; y < region.get_end().y; y++) {
  1768. Vector2i tile_coords = atlas->get_tile_at_coords(Vector2i(x, y));
  1769. if (tile_coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1770. tile_set_selection.insert(TileMapCell(source_id, tile_coords, 0));
  1771. }
  1772. }
  1773. }
  1774. }
  1775. }
  1776. _update_selection_pattern_from_tileset_tiles_selection();
  1777. }
  1778. tile_set_dragging_selection = false;
  1779. }
  1780. tile_atlas_control->queue_redraw();
  1781. }
  1782. }
  1783. void TileMapLayerEditorTilesPlugin::_tile_alternatives_control_draw() {
  1784. TileMapLayer *edited_layer = _get_edited_layer();
  1785. if (!edited_layer) {
  1786. return;
  1787. }
  1788. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1789. if (tile_set.is_null()) {
  1790. return;
  1791. }
  1792. int source_index = sources_list->get_current();
  1793. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1794. return;
  1795. }
  1796. int source_id = sources_list->get_item_metadata(source_index);
  1797. if (!tile_set->has_source(source_id)) {
  1798. return;
  1799. }
  1800. TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  1801. if (!atlas) {
  1802. return;
  1803. }
  1804. // Draw the selection.
  1805. for (const TileMapCell &E : tile_set_selection) {
  1806. int16_t untransformed_alternative_id = E.alternative_tile & TileSetAtlasSource::UNTRANSFORM_MASK;
  1807. if (E.source_id == source_id && E.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && untransformed_alternative_id > 0) {
  1808. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(E.get_atlas_coords(), untransformed_alternative_id);
  1809. if (rect != Rect2i()) {
  1810. TilesEditorUtils::draw_selection_rect(alternative_tiles_control, rect);
  1811. }
  1812. }
  1813. }
  1814. // Draw hovered tile.
  1815. if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile > 0) {
  1816. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile);
  1817. if (rect != Rect2i()) {
  1818. TilesEditorUtils::draw_selection_rect(alternative_tiles_control, rect, Color(1.0, 0.8, 0.0, 0.5));
  1819. }
  1820. }
  1821. }
  1822. void TileMapLayerEditorTilesPlugin::_tile_alternatives_control_mouse_exited() {
  1823. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1824. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1825. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1826. alternative_tiles_control->queue_redraw();
  1827. }
  1828. void TileMapLayerEditorTilesPlugin::_tile_alternatives_control_gui_input(const Ref<InputEvent> &p_event) {
  1829. TileMapLayer *edited_layer = _get_edited_layer();
  1830. if (!edited_layer) {
  1831. return;
  1832. }
  1833. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1834. if (tile_set.is_null()) {
  1835. return;
  1836. }
  1837. int source_index = sources_list->get_current();
  1838. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1839. return;
  1840. }
  1841. int source_id = sources_list->get_item_metadata(source_index);
  1842. if (!tile_set->has_source(source_id)) {
  1843. return;
  1844. }
  1845. TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  1846. if (!atlas) {
  1847. return;
  1848. }
  1849. // Update the hovered tile
  1850. hovered_tile.source_id = source_id;
  1851. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1852. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1853. Vector3i alternative_coords = tile_atlas_view->get_alternative_tile_at_pos(alternative_tiles_control->get_local_mouse_position());
  1854. Vector2i coords = Vector2i(alternative_coords.x, alternative_coords.y);
  1855. int alternative = alternative_coords.z;
  1856. if (coords != TileSetSource::INVALID_ATLAS_COORDS && alternative != TileSetSource::INVALID_TILE_ALTERNATIVE) {
  1857. hovered_tile.set_atlas_coords(coords);
  1858. hovered_tile.alternative_tile = alternative;
  1859. }
  1860. Ref<InputEventMouseMotion> mm = p_event;
  1861. if (mm.is_valid()) {
  1862. tile_atlas_control->queue_redraw();
  1863. alternative_tiles_control->queue_redraw();
  1864. }
  1865. Ref<InputEventMouseButton> mb = p_event;
  1866. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  1867. if (mb->is_pressed()) { // Pressed
  1868. // Left click pressed.
  1869. if (!mb->is_shift_pressed()) {
  1870. tile_set_selection.clear();
  1871. }
  1872. if (coords != TileSetSource::INVALID_ATLAS_COORDS && alternative != TileSetAtlasSource::INVALID_TILE_ALTERNATIVE) {
  1873. if (mb->is_shift_pressed() && tile_set_selection.has(TileMapCell(source_id, hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile))) {
  1874. tile_set_selection.erase(TileMapCell(source_id, hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile));
  1875. } else {
  1876. tile_set_selection.insert(TileMapCell(source_id, hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile));
  1877. }
  1878. }
  1879. _update_selection_pattern_from_tileset_tiles_selection();
  1880. }
  1881. tile_atlas_control->queue_redraw();
  1882. alternative_tiles_control->queue_redraw();
  1883. }
  1884. }
  1885. void TileMapLayerEditorTilesPlugin::_set_tile_map_selection(const TypedArray<Vector2i> &p_selection) {
  1886. tile_map_selection.clear();
  1887. for (int i = 0; i < p_selection.size(); i++) {
  1888. tile_map_selection.insert(p_selection[i]);
  1889. }
  1890. _update_selection_pattern_from_tilemap_selection();
  1891. _update_tileset_selection_from_selection_pattern();
  1892. CanvasItemEditor::get_singleton()->update_viewport();
  1893. }
  1894. TypedArray<Vector2i> TileMapLayerEditorTilesPlugin::_get_tile_map_selection() const {
  1895. TypedArray<Vector2i> output;
  1896. for (const Vector2i &E : tile_map_selection) {
  1897. output.push_back(E);
  1898. }
  1899. return output;
  1900. }
  1901. void TileMapLayerEditorTilesPlugin::_set_source_sort(int p_sort) {
  1902. for (int i = 0; i != TilesEditorUtils::SOURCE_SORT_MAX; i++) {
  1903. source_sort_button->get_popup()->set_item_checked(i, (i == (int)p_sort));
  1904. }
  1905. TilesEditorUtils::get_singleton()->set_sorting_option(p_sort);
  1906. _update_tile_set_sources_list();
  1907. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "tile_source_sort", p_sort);
  1908. }
  1909. void TileMapLayerEditorTilesPlugin::_bind_methods() {
  1910. ClassDB::bind_method(D_METHOD("_scene_thumbnail_done"), &TileMapLayerEditorTilesPlugin::_scene_thumbnail_done);
  1911. ClassDB::bind_method(D_METHOD("_set_tile_map_selection", "selection"), &TileMapLayerEditorTilesPlugin::_set_tile_map_selection);
  1912. ClassDB::bind_method(D_METHOD("_get_tile_map_selection"), &TileMapLayerEditorTilesPlugin::_get_tile_map_selection);
  1913. }
  1914. void TileMapLayerEditorTilesPlugin::edit(ObjectID p_tile_map_layer_id) {
  1915. _stop_dragging(); // Avoids staying in a wrong drag state.
  1916. // Disable sort button if the tileset is read-only
  1917. TileMapLayer *edited_layer = _get_edited_layer();
  1918. Ref<TileSet> tile_set;
  1919. if (edited_layer) {
  1920. tile_set = edited_layer->get_tile_set();
  1921. if (tile_set.is_valid()) {
  1922. source_sort_button->set_disabled(EditorNode::get_singleton()->is_resource_read_only(tile_set));
  1923. }
  1924. }
  1925. TileMapLayer *new_tile_map_layer = Object::cast_to<TileMapLayer>(ObjectDB::get_instance(edited_tile_map_layer_id));
  1926. Ref<TileSet> new_tile_set;
  1927. if (new_tile_map_layer) {
  1928. new_tile_set = new_tile_map_layer->get_tile_set();
  1929. }
  1930. if (tile_set.is_valid() && tile_set != new_tile_set) {
  1931. // Clear the selection.
  1932. tile_set_selection.clear();
  1933. patterns_item_list->deselect_all();
  1934. tile_map_selection.clear();
  1935. selection_pattern.instantiate();
  1936. }
  1937. edited_tile_map_layer_id = p_tile_map_layer_id;
  1938. }
  1939. TileMapLayerEditorTilesPlugin::TileMapLayerEditorTilesPlugin() {
  1940. CanvasItemEditor::get_singleton()
  1941. ->get_viewport_control()
  1942. ->connect(SceneStringName(mouse_exited), callable_mp(this, &TileMapLayerEditorTilesPlugin::_mouse_exited_viewport));
  1943. // --- Initialize references ---
  1944. tile_map_clipboard.instantiate();
  1945. selection_pattern.instantiate();
  1946. erase_pattern.instantiate();
  1947. erase_pattern->set_cell(Vector2i(0, 0), TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  1948. // --- Toolbar ---
  1949. toolbar = memnew(HBoxContainer);
  1950. HBoxContainer *tilemap_tiles_tools_buttons = memnew(HBoxContainer);
  1951. tool_buttons_group.instantiate();
  1952. select_tool_button = memnew(Button);
  1953. select_tool_button->set_theme_type_variation("FlatButton");
  1954. select_tool_button->set_toggle_mode(true);
  1955. select_tool_button->set_button_group(tool_buttons_group);
  1956. select_tool_button->set_shortcut(ED_SHORTCUT("tiles_editor/selection_tool", TTR("Selection"), Key::S));
  1957. select_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1958. tilemap_tiles_tools_buttons->add_child(select_tool_button);
  1959. viewport_shortcut_buttons.push_back(select_tool_button);
  1960. paint_tool_button = memnew(Button);
  1961. paint_tool_button->set_theme_type_variation("FlatButton");
  1962. paint_tool_button->set_toggle_mode(true);
  1963. paint_tool_button->set_button_group(tool_buttons_group);
  1964. paint_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/paint_tool"));
  1965. paint_tool_button->set_tooltip_text(TTR("Shift: Draw line.") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Shift: Draw rectangle."));
  1966. paint_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1967. tilemap_tiles_tools_buttons->add_child(paint_tool_button);
  1968. viewport_shortcut_buttons.push_back(paint_tool_button);
  1969. line_tool_button = memnew(Button);
  1970. line_tool_button->set_theme_type_variation("FlatButton");
  1971. line_tool_button->set_toggle_mode(true);
  1972. line_tool_button->set_button_group(tool_buttons_group);
  1973. // TRANSLATORS: This refers to the line tool in the tilemap editor.
  1974. line_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/line_tool"));
  1975. line_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1976. tilemap_tiles_tools_buttons->add_child(line_tool_button);
  1977. viewport_shortcut_buttons.push_back(line_tool_button);
  1978. rect_tool_button = memnew(Button);
  1979. rect_tool_button->set_theme_type_variation("FlatButton");
  1980. rect_tool_button->set_toggle_mode(true);
  1981. rect_tool_button->set_button_group(tool_buttons_group);
  1982. rect_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/rect_tool"));
  1983. rect_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1984. tilemap_tiles_tools_buttons->add_child(rect_tool_button);
  1985. viewport_shortcut_buttons.push_back(rect_tool_button);
  1986. bucket_tool_button = memnew(Button);
  1987. bucket_tool_button->set_theme_type_variation("FlatButton");
  1988. bucket_tool_button->set_toggle_mode(true);
  1989. bucket_tool_button->set_button_group(tool_buttons_group);
  1990. bucket_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/bucket_tool"));
  1991. bucket_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1992. tilemap_tiles_tools_buttons->add_child(bucket_tool_button);
  1993. toolbar->add_child(tilemap_tiles_tools_buttons);
  1994. viewport_shortcut_buttons.push_back(bucket_tool_button);
  1995. // -- TileMap tool settings --
  1996. tools_settings = memnew(HBoxContainer);
  1997. toolbar->add_child(tools_settings);
  1998. tools_settings_vsep = memnew(VSeparator);
  1999. tools_settings->add_child(tools_settings_vsep);
  2000. // Picker
  2001. picker_button = memnew(Button);
  2002. picker_button->set_theme_type_variation("FlatButton");
  2003. picker_button->set_toggle_mode(true);
  2004. picker_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/picker"));
  2005. Key key = (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) ? Key::META : Key::CTRL;
  2006. picker_button->set_tooltip_text(vformat(TTR("Alternatively hold %s with other tools to pick tile."), find_keycode_name(key)));
  2007. picker_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  2008. tools_settings->add_child(picker_button);
  2009. viewport_shortcut_buttons.push_back(picker_button);
  2010. // Erase button.
  2011. erase_button = memnew(Button);
  2012. erase_button->set_theme_type_variation("FlatButton");
  2013. erase_button->set_toggle_mode(true);
  2014. erase_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/eraser"));
  2015. erase_button->set_tooltip_text(TTR("Alternatively use RMB to erase tiles."));
  2016. erase_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  2017. tools_settings->add_child(erase_button);
  2018. viewport_shortcut_buttons.push_back(erase_button);
  2019. // Transform toolbar.
  2020. transform_toolbar = memnew(HBoxContainer);
  2021. tools_settings->add_child(transform_toolbar);
  2022. transform_toolbar->add_child(memnew(VSeparator));
  2023. transform_button_rotate_left = memnew(Button);
  2024. transform_button_rotate_left->set_theme_type_variation("FlatButton");
  2025. transform_button_rotate_left->set_shortcut(ED_SHORTCUT("tiles_editor/rotate_tile_left", TTR("Rotate Tile Left"), Key::Z));
  2026. transform_toolbar->add_child(transform_button_rotate_left);
  2027. transform_button_rotate_left->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_ROTATE_LEFT));
  2028. viewport_shortcut_buttons.push_back(transform_button_rotate_left);
  2029. transform_button_rotate_right = memnew(Button);
  2030. transform_button_rotate_right->set_theme_type_variation("FlatButton");
  2031. transform_button_rotate_right->set_shortcut(ED_SHORTCUT("tiles_editor/rotate_tile_right", TTR("Rotate Tile Right"), Key::X));
  2032. transform_toolbar->add_child(transform_button_rotate_right);
  2033. transform_button_rotate_right->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_ROTATE_RIGHT));
  2034. viewport_shortcut_buttons.push_back(transform_button_rotate_right);
  2035. transform_button_flip_h = memnew(Button);
  2036. transform_button_flip_h->set_theme_type_variation("FlatButton");
  2037. transform_button_flip_h->set_shortcut(ED_SHORTCUT("tiles_editor/flip_tile_horizontal", TTR("Flip Tile Horizontally"), Key::C));
  2038. transform_toolbar->add_child(transform_button_flip_h);
  2039. transform_button_flip_h->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_FLIP_H));
  2040. viewport_shortcut_buttons.push_back(transform_button_flip_h);
  2041. transform_button_flip_v = memnew(Button);
  2042. transform_button_flip_v->set_theme_type_variation("FlatButton");
  2043. transform_button_flip_v->set_shortcut(ED_SHORTCUT("tiles_editor/flip_tile_vertical", TTR("Flip Tile Vertically"), Key::V));
  2044. transform_toolbar->add_child(transform_button_flip_v);
  2045. transform_button_flip_v->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_FLIP_V));
  2046. viewport_shortcut_buttons.push_back(transform_button_flip_v);
  2047. // Separator 2.
  2048. tools_settings_vsep_2 = memnew(VSeparator);
  2049. tools_settings->add_child(tools_settings_vsep_2);
  2050. // Continuous checkbox.
  2051. bucket_contiguous_checkbox = memnew(CheckBox);
  2052. bucket_contiguous_checkbox->set_flat(true);
  2053. bucket_contiguous_checkbox->set_text(TTR("Contiguous"));
  2054. bucket_contiguous_checkbox->set_pressed(true);
  2055. tools_settings->add_child(bucket_contiguous_checkbox);
  2056. // Random tile checkbox.
  2057. random_tile_toggle = memnew(Button);
  2058. random_tile_toggle->set_theme_type_variation("FlatButton");
  2059. random_tile_toggle->set_toggle_mode(true);
  2060. random_tile_toggle->set_tooltip_text(TTR("Place Random Tile"));
  2061. random_tile_toggle->connect(SceneStringName(toggled), callable_mp(this, &TileMapLayerEditorTilesPlugin::_on_random_tile_checkbox_toggled));
  2062. tools_settings->add_child(random_tile_toggle);
  2063. // Random tile scattering.
  2064. scatter_controls_container = memnew(HBoxContainer);
  2065. scatter_label = memnew(Label);
  2066. scatter_label->set_tooltip_text(TTR("Modifies the chance of painting nothing instead of a randomly selected tile."));
  2067. scatter_label->set_text(TTR("Scattering:"));
  2068. scatter_controls_container->add_child(scatter_label);
  2069. scatter_spinbox = memnew(SpinBox);
  2070. scatter_spinbox->set_min(0.0);
  2071. scatter_spinbox->set_max(1000);
  2072. scatter_spinbox->set_step(0.001);
  2073. scatter_spinbox->set_tooltip_text(TTR("Modifies the chance of painting nothing instead of a randomly selected tile."));
  2074. scatter_spinbox->get_line_edit()->add_theme_constant_override("minimum_character_width", 4);
  2075. scatter_spinbox->connect(SceneStringName(value_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_on_scattering_spinbox_changed));
  2076. scatter_controls_container->add_child(scatter_spinbox);
  2077. tools_settings->add_child(scatter_controls_container);
  2078. _on_random_tile_checkbox_toggled(false);
  2079. // Default tool.
  2080. paint_tool_button->set_pressed(true);
  2081. _update_toolbar();
  2082. // --- Bottom panel tiles ---
  2083. tiles_bottom_panel = memnew(VBoxContainer);
  2084. // FIXME: This can trigger theme updates when the nodes that we want to update are not yet available.
  2085. // The toolbar should be extracted to a dedicated control and theme updates should be handled through
  2086. // the notification.
  2087. tiles_bottom_panel->connect(SceneStringName(theme_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_theme));
  2088. tiles_bottom_panel->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_stop_dragging));
  2089. tiles_bottom_panel->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tab_changed));
  2090. tiles_bottom_panel->set_name(TTR("Tiles"));
  2091. missing_source_label = memnew(Label);
  2092. missing_source_label->set_text(TTR("This TileMap's TileSet has no Tile Source configured. Go to the TileSet bottom panel to add one."));
  2093. missing_source_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2094. missing_source_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2095. missing_source_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2096. missing_source_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  2097. missing_source_label->hide();
  2098. tiles_bottom_panel->add_child(missing_source_label);
  2099. atlas_sources_split_container = memnew(HSplitContainer);
  2100. atlas_sources_split_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2101. atlas_sources_split_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2102. tiles_bottom_panel->add_child(atlas_sources_split_container);
  2103. VBoxContainer *split_container_left_side = memnew(VBoxContainer);
  2104. split_container_left_side->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2105. split_container_left_side->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2106. split_container_left_side->set_stretch_ratio(0.25);
  2107. split_container_left_side->set_custom_minimum_size(Size2(70, 0) * EDSCALE);
  2108. atlas_sources_split_container->add_child(split_container_left_side);
  2109. HBoxContainer *sources_bottom_actions = memnew(HBoxContainer);
  2110. sources_bottom_actions->set_alignment(HBoxContainer::ALIGNMENT_END);
  2111. source_sort_button = memnew(MenuButton);
  2112. source_sort_button->set_flat(false);
  2113. source_sort_button->set_theme_type_variation("FlatMenuButton");
  2114. source_sort_button->set_tooltip_text(TTR("Sort sources"));
  2115. PopupMenu *p = source_sort_button->get_popup();
  2116. p->connect(SceneStringName(id_pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_set_source_sort));
  2117. p->add_radio_check_item(TTR("Sort by ID (Ascending)"), TilesEditorUtils::SOURCE_SORT_ID);
  2118. p->add_radio_check_item(TTR("Sort by ID (Descending)"), TilesEditorUtils::SOURCE_SORT_ID_REVERSE);
  2119. p->add_radio_check_item(TTR("Sort by Name (Ascending)"), TilesEditorUtils::SOURCE_SORT_NAME);
  2120. p->add_radio_check_item(TTR("Sort by Name (Descending)"), TilesEditorUtils::SOURCE_SORT_NAME_REVERSE);
  2121. p->set_item_checked(TilesEditorUtils::SOURCE_SORT_ID, true);
  2122. sources_bottom_actions->add_child(source_sort_button);
  2123. sources_list = memnew(ItemList);
  2124. sources_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  2125. sources_list->set_fixed_icon_size(Size2(60, 60) * EDSCALE);
  2126. sources_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2127. sources_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2128. sources_list->set_stretch_ratio(0.25);
  2129. sources_list->set_custom_minimum_size(Size2(70, 0) * EDSCALE);
  2130. sources_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  2131. sources_list->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_source_display).unbind(1));
  2132. sources_list->connect(SceneStringName(item_selected), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_sources_lists_current));
  2133. sources_list->connect("item_activated", callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::display_tile_set_editor_panel).unbind(1));
  2134. sources_list->connect(SceneStringName(visibility_changed), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::synchronize_sources_list).bind(sources_list, source_sort_button));
  2135. sources_list->add_user_signal(MethodInfo("sort_request"));
  2136. sources_list->connect("sort_request", callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_tile_set_sources_list));
  2137. split_container_left_side->add_child(sources_list);
  2138. split_container_left_side->add_child(sources_bottom_actions);
  2139. // Tile atlas source.
  2140. tile_atlas_view = memnew(TileAtlasView);
  2141. tile_atlas_view->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2142. tile_atlas_view->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2143. tile_atlas_view->set_texture_grid_visible(false);
  2144. tile_atlas_view->set_tile_shape_grid_visible(false);
  2145. tile_atlas_view->connect("transform_changed", callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_atlas_view_transform));
  2146. atlas_sources_split_container->add_child(tile_atlas_view);
  2147. tile_atlas_control = memnew(Control);
  2148. tile_atlas_control->connect(SceneStringName(draw), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_atlas_control_draw));
  2149. tile_atlas_control->connect(SceneStringName(mouse_exited), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_atlas_control_mouse_exited));
  2150. tile_atlas_control->connect(SceneStringName(gui_input), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_atlas_control_gui_input));
  2151. tile_atlas_view->add_control_over_atlas_tiles(tile_atlas_control);
  2152. alternative_tiles_control = memnew(Control);
  2153. alternative_tiles_control->connect(SceneStringName(draw), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_alternatives_control_draw));
  2154. alternative_tiles_control->connect(SceneStringName(mouse_exited), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_alternatives_control_mouse_exited));
  2155. alternative_tiles_control->connect(SceneStringName(gui_input), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_alternatives_control_gui_input));
  2156. tile_atlas_view->add_control_over_alternative_tiles(alternative_tiles_control);
  2157. // Scenes collection source.
  2158. scene_tiles_list = memnew(ItemList);
  2159. scene_tiles_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  2160. scene_tiles_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2161. scene_tiles_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2162. scene_tiles_list->set_select_mode(ItemList::SELECT_MULTI);
  2163. scene_tiles_list->connect("multi_selected", callable_mp(this, &TileMapLayerEditorTilesPlugin::_scenes_list_multi_selected));
  2164. scene_tiles_list->connect("empty_clicked", callable_mp(this, &TileMapLayerEditorTilesPlugin::_scenes_list_lmb_empty_clicked));
  2165. scene_tiles_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  2166. atlas_sources_split_container->add_child(scene_tiles_list);
  2167. // Invalid source label.
  2168. invalid_source_label = memnew(Label);
  2169. invalid_source_label->set_text(TTR("Invalid source selected."));
  2170. invalid_source_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2171. invalid_source_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2172. invalid_source_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2173. invalid_source_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  2174. invalid_source_label->hide();
  2175. atlas_sources_split_container->add_child(invalid_source_label);
  2176. // --- Bottom panel patterns ---
  2177. patterns_bottom_panel = memnew(VBoxContainer);
  2178. patterns_bottom_panel->set_name(TTR("Patterns"));
  2179. patterns_bottom_panel->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tab_changed));
  2180. int thumbnail_size = 64;
  2181. patterns_item_list = memnew(ItemList);
  2182. patterns_item_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  2183. patterns_item_list->set_max_columns(0);
  2184. patterns_item_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  2185. patterns_item_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  2186. patterns_item_list->set_max_text_lines(2);
  2187. patterns_item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  2188. patterns_item_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2189. patterns_item_list->connect(SceneStringName(gui_input), callable_mp(this, &TileMapLayerEditorTilesPlugin::_patterns_item_list_gui_input));
  2190. patterns_item_list->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_pattern_selection).unbind(1));
  2191. patterns_item_list->connect("item_activated", callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_pattern_selection).unbind(1));
  2192. patterns_item_list->connect("empty_clicked", callable_mp(this, &TileMapLayerEditorTilesPlugin::patterns_item_list_empty_clicked));
  2193. patterns_bottom_panel->add_child(patterns_item_list);
  2194. patterns_help_label = memnew(Label);
  2195. patterns_help_label->set_text(TTR("Drag and drop or paste a TileMap selection here to store a pattern."));
  2196. patterns_help_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2197. patterns_help_label->set_anchors_and_offsets_preset(Control::PRESET_CENTER);
  2198. patterns_item_list->add_child(patterns_help_label);
  2199. // Update.
  2200. _update_source_display();
  2201. }
  2202. TileMapLayerEditorTilesPlugin::~TileMapLayerEditorTilesPlugin() {
  2203. }
  2204. void TileMapLayerEditorTerrainsPlugin::tile_set_changed() {
  2205. _update_terrains_cache();
  2206. _update_terrains_tree();
  2207. _update_tiles_list();
  2208. }
  2209. void TileMapLayerEditorTerrainsPlugin::_update_toolbar() {
  2210. // Hide all settings.
  2211. for (int i = 0; i < tools_settings->get_child_count(); i++) {
  2212. Object::cast_to<CanvasItem>(tools_settings->get_child(i))->hide();
  2213. }
  2214. // Show only the correct settings.
  2215. if (tool_buttons_group->get_pressed_button() != bucket_tool_button) {
  2216. tools_settings_vsep->show();
  2217. picker_button->show();
  2218. erase_button->show();
  2219. } else {
  2220. tools_settings_vsep->show();
  2221. picker_button->show();
  2222. erase_button->show();
  2223. tools_settings_vsep_2->show();
  2224. bucket_contiguous_checkbox->show();
  2225. }
  2226. }
  2227. Vector<TileMapLayerSubEditorPlugin::TabData> TileMapLayerEditorTerrainsPlugin::get_tabs() const {
  2228. Vector<TileMapLayerSubEditorPlugin::TabData> tabs;
  2229. tabs.push_back({ toolbar, main_vbox_container });
  2230. return tabs;
  2231. }
  2232. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_terrain_path_or_connect(const Vector<Vector2i> &p_to_paint, int p_terrain_set, int p_terrain, bool p_connect) const {
  2233. TileMapLayer *edited_layer = _get_edited_layer();
  2234. if (!edited_layer) {
  2235. return HashMap<Vector2i, TileMapCell>();
  2236. }
  2237. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2238. if (tile_set.is_null()) {
  2239. return HashMap<Vector2i, TileMapCell>();
  2240. }
  2241. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output;
  2242. if (p_connect) {
  2243. terrain_fill_output = edited_layer->terrain_fill_connect(p_to_paint, p_terrain_set, p_terrain, false);
  2244. } else {
  2245. terrain_fill_output = edited_layer->terrain_fill_path(p_to_paint, p_terrain_set, p_terrain, false);
  2246. }
  2247. // Make the painted path a set for faster lookups
  2248. HashSet<Vector2i> painted_set;
  2249. for (Vector2i coords : p_to_paint) {
  2250. painted_set.insert(coords);
  2251. }
  2252. HashMap<Vector2i, TileMapCell> output;
  2253. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  2254. if (painted_set.has(kv.key)) {
  2255. // Paint a random tile with the correct terrain for the painted path.
  2256. output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2257. } else {
  2258. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2259. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2260. TileMapCell cell = edited_layer->get_cell(kv.key);
  2261. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2262. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2263. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2264. if (atlas_source) {
  2265. // Get tile data.
  2266. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2267. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2268. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2269. }
  2270. }
  2271. }
  2272. if (in_map_terrain_pattern != kv.value) {
  2273. output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2274. }
  2275. }
  2276. }
  2277. return output;
  2278. }
  2279. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_terrain_pattern(const Vector<Vector2i> &p_to_paint, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const {
  2280. TileMapLayer *edited_layer = _get_edited_layer();
  2281. if (!edited_layer) {
  2282. return HashMap<Vector2i, TileMapCell>();
  2283. }
  2284. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2285. if (tile_set.is_null()) {
  2286. return HashMap<Vector2i, TileMapCell>();
  2287. }
  2288. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = edited_layer->terrain_fill_pattern(p_to_paint, p_terrain_set, p_terrains_pattern, false);
  2289. // Make the painted path a set for faster lookups
  2290. HashSet<Vector2i> painted_set;
  2291. for (Vector2i coords : p_to_paint) {
  2292. painted_set.insert(coords);
  2293. }
  2294. HashMap<Vector2i, TileMapCell> output;
  2295. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  2296. if (painted_set.has(kv.key)) {
  2297. // Paint a random tile with the correct terrain for the painted path.
  2298. output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2299. } else {
  2300. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2301. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2302. TileMapCell cell = edited_layer->get_cell(kv.key);
  2303. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2304. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2305. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2306. if (atlas_source) {
  2307. // Get tile data.
  2308. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2309. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2310. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2311. }
  2312. }
  2313. }
  2314. if (in_map_terrain_pattern != kv.value) {
  2315. output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2316. }
  2317. }
  2318. }
  2319. return output;
  2320. }
  2321. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_line(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase) {
  2322. TileMapLayer *edited_layer = _get_edited_layer();
  2323. if (!edited_layer) {
  2324. return HashMap<Vector2i, TileMapCell>();
  2325. }
  2326. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2327. if (tile_set.is_null()) {
  2328. return HashMap<Vector2i, TileMapCell>();
  2329. }
  2330. if (p_erase) {
  2331. return _draw_terrain_pattern(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, TileSet::TerrainsPattern(*tile_set, selected_terrain_set));
  2332. } else {
  2333. if (selected_type == SELECTED_TYPE_CONNECT) {
  2334. return _draw_terrain_path_or_connect(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, selected_terrain, true);
  2335. } else if (selected_type == SELECTED_TYPE_PATH) {
  2336. return _draw_terrain_path_or_connect(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, selected_terrain, false);
  2337. } else { // SELECTED_TYPE_PATTERN
  2338. return _draw_terrain_pattern(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, selected_terrains_pattern);
  2339. }
  2340. }
  2341. }
  2342. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_rect(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase) {
  2343. TileMapLayer *edited_layer = _get_edited_layer();
  2344. if (!edited_layer) {
  2345. return HashMap<Vector2i, TileMapCell>();
  2346. }
  2347. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2348. if (tile_set.is_null()) {
  2349. return HashMap<Vector2i, TileMapCell>();
  2350. }
  2351. Rect2i rect;
  2352. rect.set_position(p_start_cell);
  2353. rect.set_end(p_end_cell);
  2354. rect = rect.abs();
  2355. Vector<Vector2i> to_draw;
  2356. for (int x = rect.position.x; x <= rect.get_end().x; x++) {
  2357. for (int y = rect.position.y; y <= rect.get_end().y; y++) {
  2358. to_draw.append(Vector2i(x, y));
  2359. }
  2360. }
  2361. if (p_erase) {
  2362. return _draw_terrain_pattern(to_draw, selected_terrain_set, TileSet::TerrainsPattern(*tile_set, selected_terrain_set));
  2363. } else {
  2364. if (selected_type == SELECTED_TYPE_CONNECT || selected_type == SELECTED_TYPE_PATH) {
  2365. return _draw_terrain_path_or_connect(to_draw, selected_terrain_set, selected_terrain, true);
  2366. } else { // SELECTED_TYPE_PATTERN
  2367. return _draw_terrain_pattern(to_draw, selected_terrain_set, selected_terrains_pattern);
  2368. }
  2369. }
  2370. }
  2371. RBSet<Vector2i> TileMapLayerEditorTerrainsPlugin::_get_cells_for_bucket_fill(Vector2i p_coords, bool p_contiguous) {
  2372. const TileMapLayer *edited_layer = _get_edited_layer();
  2373. if (!edited_layer) {
  2374. return RBSet<Vector2i>();
  2375. }
  2376. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2377. if (tile_set.is_null()) {
  2378. return RBSet<Vector2i>();
  2379. }
  2380. TileMapCell source_cell = edited_layer->get_cell(p_coords);
  2381. TileSet::TerrainsPattern source_pattern(*tile_set, selected_terrain_set);
  2382. if (source_cell.source_id != TileSet::INVALID_SOURCE) {
  2383. TileData *tile_data = nullptr;
  2384. Ref<TileSetSource> source = tile_set->get_source(source_cell.source_id);
  2385. Ref<TileSetAtlasSource> atlas_source = source;
  2386. if (atlas_source.is_valid()) {
  2387. tile_data = atlas_source->get_tile_data(source_cell.get_atlas_coords(), source_cell.alternative_tile);
  2388. }
  2389. if (!tile_data) {
  2390. return RBSet<Vector2i>();
  2391. }
  2392. source_pattern = tile_data->get_terrains_pattern();
  2393. }
  2394. // If we are filling empty tiles, compute the tilemap boundaries.
  2395. Rect2i boundaries;
  2396. if (source_cell.source_id == TileSet::INVALID_SOURCE) {
  2397. boundaries = edited_layer->get_used_rect();
  2398. }
  2399. RBSet<Vector2i> output;
  2400. if (p_contiguous) {
  2401. // Replace continuous tiles like the source.
  2402. RBSet<Vector2i> already_checked;
  2403. List<Vector2i> to_check;
  2404. to_check.push_back(p_coords);
  2405. while (!to_check.is_empty()) {
  2406. Vector2i coords = to_check.back()->get();
  2407. to_check.pop_back();
  2408. if (!already_checked.has(coords)) {
  2409. // Get the candidate cell pattern.
  2410. TileSet::TerrainsPattern candidate_pattern(*tile_set, selected_terrain_set);
  2411. if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  2412. TileData *tile_data = nullptr;
  2413. Ref<TileSetSource> source = tile_set->get_source(edited_layer->get_cell_source_id(coords));
  2414. Ref<TileSetAtlasSource> atlas_source = source;
  2415. if (atlas_source.is_valid()) {
  2416. tile_data = atlas_source->get_tile_data(edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords));
  2417. }
  2418. if (tile_data) {
  2419. candidate_pattern = tile_data->get_terrains_pattern();
  2420. }
  2421. }
  2422. // Draw.
  2423. if (candidate_pattern == source_pattern && (!source_pattern.is_erase_pattern() || boundaries.has_point(coords))) {
  2424. output.insert(coords);
  2425. // Get surrounding tiles (handles different tile shapes).
  2426. TypedArray<Vector2i> around = tile_set->get_surrounding_cells(coords);
  2427. for (int i = 0; i < around.size(); i++) {
  2428. to_check.push_back(around[i]);
  2429. }
  2430. }
  2431. already_checked.insert(coords);
  2432. }
  2433. }
  2434. } else {
  2435. // Replace all tiles like the source.
  2436. TypedArray<Vector2i> to_check;
  2437. if (source_cell.source_id == TileSet::INVALID_SOURCE) {
  2438. Rect2i rect = edited_layer->get_used_rect();
  2439. if (!rect.has_area()) {
  2440. rect = Rect2i(p_coords, Vector2i(1, 1));
  2441. }
  2442. for (int x = boundaries.position.x; x < boundaries.get_end().x; x++) {
  2443. for (int y = boundaries.position.y; y < boundaries.get_end().y; y++) {
  2444. to_check.append(Vector2i(x, y));
  2445. }
  2446. }
  2447. } else {
  2448. to_check = edited_layer->get_used_cells();
  2449. }
  2450. for (int i = 0; i < to_check.size(); i++) {
  2451. Vector2i coords = to_check[i];
  2452. // Get the candidate cell pattern.
  2453. TileSet::TerrainsPattern candidate_pattern;
  2454. if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  2455. TileData *tile_data = nullptr;
  2456. Ref<TileSetSource> source = tile_set->get_source(edited_layer->get_cell_source_id(coords));
  2457. Ref<TileSetAtlasSource> atlas_source = source;
  2458. if (atlas_source.is_valid()) {
  2459. tile_data = atlas_source->get_tile_data(edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords));
  2460. }
  2461. if (tile_data) {
  2462. candidate_pattern = tile_data->get_terrains_pattern();
  2463. }
  2464. }
  2465. // Draw.
  2466. if (candidate_pattern == source_pattern && (!source_pattern.is_erase_pattern() || boundaries.has_point(coords))) {
  2467. output.insert(coords);
  2468. }
  2469. }
  2470. }
  2471. return output;
  2472. }
  2473. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_bucket_fill(Vector2i p_coords, bool p_contiguous, bool p_erase) {
  2474. const TileMapLayer *edited_layer = _get_edited_layer();
  2475. if (!edited_layer) {
  2476. return HashMap<Vector2i, TileMapCell>();
  2477. }
  2478. const Ref<TileSet> &tile_set = edited_layer->get_tile_set();
  2479. if (tile_set.is_null()) {
  2480. return HashMap<Vector2i, TileMapCell>();
  2481. }
  2482. RBSet<Vector2i> cells_to_draw = _get_cells_for_bucket_fill(p_coords, p_contiguous);
  2483. Vector<Vector2i> cells_to_draw_as_vector;
  2484. for (Vector2i cell : cells_to_draw) {
  2485. cells_to_draw_as_vector.append(cell);
  2486. }
  2487. if (p_erase) {
  2488. return _draw_terrain_pattern(cells_to_draw_as_vector, selected_terrain_set, TileSet::TerrainsPattern(*tile_set, selected_terrain_set));
  2489. } else {
  2490. if (selected_type == SELECTED_TYPE_CONNECT || selected_type == SELECTED_TYPE_PATH) {
  2491. return _draw_terrain_path_or_connect(cells_to_draw_as_vector, selected_terrain_set, selected_terrain, true);
  2492. } else { // SELECTED_TYPE_PATTERN
  2493. return _draw_terrain_pattern(cells_to_draw_as_vector, selected_terrain_set, selected_terrains_pattern);
  2494. }
  2495. }
  2496. }
  2497. void TileMapLayerEditorTerrainsPlugin::_stop_dragging() {
  2498. TileMapLayer *edited_layer = _get_edited_layer();
  2499. if (!edited_layer) {
  2500. return;
  2501. }
  2502. const Ref<TileSet> &tile_set = edited_layer->get_tile_set();
  2503. if (tile_set.is_null()) {
  2504. return;
  2505. }
  2506. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  2507. Vector2 mpos = xform.affine_inverse().xform(CanvasItemEditor::get_singleton()->get_viewport_control()->get_local_mouse_position());
  2508. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2509. switch (drag_type) {
  2510. case DRAG_TYPE_PICK: {
  2511. Vector2i coords = tile_set->local_to_map(mpos);
  2512. TileMapCell cell = edited_layer->get_cell(coords);
  2513. TileData *tile_data = nullptr;
  2514. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  2515. Ref<TileSetAtlasSource> atlas_source = source;
  2516. if (atlas_source.is_valid()) {
  2517. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2518. }
  2519. if (tile_data) {
  2520. TileSet::TerrainsPattern terrains_pattern = tile_data->get_terrains_pattern();
  2521. // Find the tree item for the right terrain set.
  2522. bool need_tree_item_switch = true;
  2523. TreeItem *tree_item = terrains_tree->get_selected();
  2524. int new_terrain_set = -1;
  2525. if (tree_item) {
  2526. Dictionary metadata_dict = tree_item->get_metadata(0);
  2527. if (metadata_dict.has("terrain_set") && metadata_dict.has("terrain_id")) {
  2528. int terrain_set = metadata_dict["terrain_set"];
  2529. int terrain_id = metadata_dict["terrain_id"];
  2530. if (per_terrain_terrains_patterns[terrain_set][terrain_id].has(terrains_pattern)) {
  2531. new_terrain_set = terrain_set;
  2532. need_tree_item_switch = false;
  2533. }
  2534. }
  2535. }
  2536. if (need_tree_item_switch) {
  2537. for (tree_item = terrains_tree->get_root()->get_first_child(); tree_item; tree_item = tree_item->get_next_visible()) {
  2538. Dictionary metadata_dict = tree_item->get_metadata(0);
  2539. if (metadata_dict.has("terrain_set") && metadata_dict.has("terrain_id")) {
  2540. int terrain_set = metadata_dict["terrain_set"];
  2541. int terrain_id = metadata_dict["terrain_id"];
  2542. if (per_terrain_terrains_patterns[terrain_set][terrain_id].has(terrains_pattern)) {
  2543. // Found
  2544. new_terrain_set = terrain_set;
  2545. tree_item->select(0);
  2546. _update_tiles_list();
  2547. break;
  2548. }
  2549. }
  2550. }
  2551. }
  2552. // Find the list item for the given tile.
  2553. if (tree_item) {
  2554. for (int i = 0; i < terrains_tile_list->get_item_count(); i++) {
  2555. Dictionary metadata_dict = terrains_tile_list->get_item_metadata(i);
  2556. if (int(metadata_dict["type"]) == SELECTED_TYPE_PATTERN) {
  2557. TileSet::TerrainsPattern in_meta_terrains_pattern(*tile_set, new_terrain_set);
  2558. in_meta_terrains_pattern.from_array(metadata_dict["terrains_pattern"]);
  2559. if (in_meta_terrains_pattern == terrains_pattern) {
  2560. terrains_tile_list->select(i);
  2561. break;
  2562. }
  2563. }
  2564. }
  2565. } else {
  2566. ERR_PRINT("Terrain tile not found.");
  2567. }
  2568. }
  2569. picker_button->set_pressed(false);
  2570. } break;
  2571. case DRAG_TYPE_PAINT: {
  2572. undo_redo->create_action(TTR("Paint terrain"));
  2573. for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) {
  2574. undo_redo->add_do_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key));
  2575. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2576. }
  2577. undo_redo->commit_action(false);
  2578. } break;
  2579. case DRAG_TYPE_LINE: {
  2580. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing);
  2581. undo_redo->create_action(TTR("Paint terrain"));
  2582. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2583. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  2584. continue;
  2585. }
  2586. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2587. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key));
  2588. }
  2589. undo_redo->commit_action();
  2590. } break;
  2591. case DRAG_TYPE_RECT: {
  2592. HashMap<Vector2i, TileMapCell> to_draw = _draw_rect(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing);
  2593. undo_redo->create_action(TTR("Paint terrain"));
  2594. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2595. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  2596. continue;
  2597. }
  2598. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2599. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key));
  2600. }
  2601. undo_redo->commit_action();
  2602. } break;
  2603. case DRAG_TYPE_BUCKET: {
  2604. undo_redo->create_action(TTR("Paint terrain"));
  2605. for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) {
  2606. undo_redo->add_do_method(edited_layer, "set_cell", E.key, edited_layer->get_cell_source_id(E.key), edited_layer->get_cell_atlas_coords(E.key), edited_layer->get_cell_alternative_tile(E.key));
  2607. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2608. }
  2609. undo_redo->commit_action(false);
  2610. } break;
  2611. default:
  2612. break;
  2613. }
  2614. drag_type = DRAG_TYPE_NONE;
  2615. }
  2616. void TileMapLayerEditorTerrainsPlugin::_mouse_exited_viewport() {
  2617. has_mouse = false;
  2618. CanvasItemEditor::get_singleton()->update_viewport();
  2619. }
  2620. void TileMapLayerEditorTerrainsPlugin::_update_selection() {
  2621. const TileMapLayer *edited_layer = _get_edited_layer();
  2622. if (!edited_layer) {
  2623. return;
  2624. }
  2625. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2626. if (tile_set.is_null()) {
  2627. return;
  2628. }
  2629. // Get the selected terrain.
  2630. selected_terrain_set = -1;
  2631. selected_terrains_pattern = TileSet::TerrainsPattern();
  2632. TreeItem *selected_tree_item = terrains_tree->get_selected();
  2633. if (selected_tree_item && selected_tree_item->get_metadata(0)) {
  2634. Dictionary metadata_dict = selected_tree_item->get_metadata(0);
  2635. // Selected terrain
  2636. selected_terrain_set = metadata_dict["terrain_set"];
  2637. selected_terrain = metadata_dict["terrain_id"];
  2638. // Selected mode/terrain pattern
  2639. if (erase_button->is_pressed()) {
  2640. selected_type = SELECTED_TYPE_PATTERN;
  2641. selected_terrains_pattern = TileSet::TerrainsPattern(*tile_set, selected_terrain_set);
  2642. } else if (terrains_tile_list->is_anything_selected()) {
  2643. metadata_dict = terrains_tile_list->get_item_metadata(terrains_tile_list->get_selected_items()[0]);
  2644. if (int(metadata_dict["type"]) == SELECTED_TYPE_CONNECT) {
  2645. selected_type = SELECTED_TYPE_CONNECT;
  2646. } else if (int(metadata_dict["type"]) == SELECTED_TYPE_PATH) {
  2647. selected_type = SELECTED_TYPE_PATH;
  2648. } else if (int(metadata_dict["type"]) == SELECTED_TYPE_PATTERN) {
  2649. selected_type = SELECTED_TYPE_PATTERN;
  2650. selected_terrains_pattern = TileSet::TerrainsPattern(*tile_set, selected_terrain_set);
  2651. selected_terrains_pattern.from_array(metadata_dict["terrains_pattern"]);
  2652. } else {
  2653. ERR_FAIL();
  2654. }
  2655. }
  2656. }
  2657. }
  2658. bool TileMapLayerEditorTerrainsPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  2659. if (!main_vbox_container->is_visible_in_tree()) {
  2660. // If the bottom editor is not visible, we ignore inputs.
  2661. return false;
  2662. }
  2663. if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
  2664. return false;
  2665. }
  2666. TileMapLayer *edited_layer = _get_edited_layer();
  2667. if (!edited_layer) {
  2668. return false;
  2669. }
  2670. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2671. if (tile_set.is_null()) {
  2672. return false;
  2673. }
  2674. _update_selection();
  2675. Ref<InputEventKey> k = p_event;
  2676. if (k.is_valid() && k->is_pressed() && !k->is_echo()) {
  2677. for (BaseButton *b : viewport_shortcut_buttons) {
  2678. if (b->get_shortcut().is_valid() && b->get_shortcut()->matches_event(p_event)) {
  2679. b->set_pressed(b->get_button_group().is_valid() || !b->is_pressed());
  2680. return true;
  2681. }
  2682. }
  2683. }
  2684. Ref<InputEventMouseMotion> mm = p_event;
  2685. if (mm.is_valid()) {
  2686. has_mouse = true;
  2687. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  2688. Vector2 mpos = xform.affine_inverse().xform(mm->get_position());
  2689. switch (drag_type) {
  2690. case DRAG_TYPE_PAINT: {
  2691. if (selected_terrain_set >= 0) {
  2692. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos), drag_erasing);
  2693. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2694. if (!drag_modified.has(E.key)) {
  2695. drag_modified[E.key] = edited_layer->get_cell(E.key);
  2696. }
  2697. edited_layer->set_cell(E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2698. }
  2699. }
  2700. } break;
  2701. default:
  2702. break;
  2703. }
  2704. drag_last_mouse_pos = mpos;
  2705. CanvasItemEditor::get_singleton()->update_viewport();
  2706. return true;
  2707. }
  2708. Ref<InputEventMouseButton> mb = p_event;
  2709. if (mb.is_valid()) {
  2710. has_mouse = true;
  2711. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  2712. Vector2 mpos = xform.affine_inverse().xform(mb->get_position());
  2713. if (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT) {
  2714. if (mb->is_pressed()) {
  2715. // Pressed
  2716. if (erase_button->is_pressed() || mb->get_button_index() == MouseButton::RIGHT) {
  2717. drag_erasing = true;
  2718. }
  2719. if (picker_button->is_pressed()) {
  2720. drag_type = DRAG_TYPE_PICK;
  2721. } else {
  2722. // Paint otherwise.
  2723. if (tool_buttons_group->get_pressed_button() == paint_tool_button && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  2724. if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) {
  2725. return true;
  2726. }
  2727. drag_type = DRAG_TYPE_PAINT;
  2728. drag_start_mouse_pos = mpos;
  2729. drag_modified.clear();
  2730. Vector2i cell = tile_set->local_to_map(mpos);
  2731. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(cell, cell, drag_erasing);
  2732. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2733. drag_modified[E.key] = edited_layer->get_cell(E.key);
  2734. edited_layer->set_cell(E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2735. }
  2736. } else if (tool_buttons_group->get_pressed_button() == line_tool_button || (tool_buttons_group->get_pressed_button() == paint_tool_button && Input::get_singleton()->is_key_pressed(Key::SHIFT) && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL))) {
  2737. if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) {
  2738. return true;
  2739. }
  2740. drag_type = DRAG_TYPE_LINE;
  2741. drag_start_mouse_pos = mpos;
  2742. drag_modified.clear();
  2743. } else if (tool_buttons_group->get_pressed_button() == rect_tool_button || (tool_buttons_group->get_pressed_button() == paint_tool_button && Input::get_singleton()->is_key_pressed(Key::SHIFT) && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL))) {
  2744. if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) {
  2745. return true;
  2746. }
  2747. drag_type = DRAG_TYPE_RECT;
  2748. drag_start_mouse_pos = mpos;
  2749. drag_modified.clear();
  2750. } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button) {
  2751. if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) {
  2752. return true;
  2753. }
  2754. drag_type = DRAG_TYPE_BUCKET;
  2755. drag_start_mouse_pos = mpos;
  2756. drag_modified.clear();
  2757. Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos));
  2758. for (int i = 0; i < line.size(); i++) {
  2759. if (!drag_modified.has(line[i])) {
  2760. HashMap<Vector2i, TileMapCell> to_draw = _draw_bucket_fill(line[i], bucket_contiguous_checkbox->is_pressed(), drag_erasing);
  2761. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2762. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  2763. continue;
  2764. }
  2765. Vector2i coords = E.key;
  2766. if (!drag_modified.has(coords)) {
  2767. drag_modified.insert(coords, edited_layer->get_cell(coords));
  2768. }
  2769. edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2770. }
  2771. }
  2772. }
  2773. }
  2774. }
  2775. } else {
  2776. // Released
  2777. _stop_dragging();
  2778. drag_erasing = false;
  2779. }
  2780. CanvasItemEditor::get_singleton()->update_viewport();
  2781. return true;
  2782. }
  2783. drag_last_mouse_pos = mpos;
  2784. }
  2785. return false;
  2786. }
  2787. void TileMapLayerEditorTerrainsPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) {
  2788. const TileMapLayer *edited_layer = _get_edited_layer();
  2789. if (!edited_layer) {
  2790. return;
  2791. }
  2792. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2793. if (tile_set.is_null()) {
  2794. return;
  2795. }
  2796. if (!edited_layer->is_visible_in_tree()) {
  2797. return;
  2798. }
  2799. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  2800. Vector2 mpos = edited_layer->get_local_mouse_position();
  2801. Vector2i tile_shape_size = tile_set->get_tile_size();
  2802. // Handle the preview of the tiles to be placed.
  2803. if (main_vbox_container->is_visible_in_tree() && has_mouse) { // Only if the tilemap editor is opened and the viewport is hovered.
  2804. RBSet<Vector2i> preview;
  2805. Rect2i drawn_grid_rect;
  2806. if (drag_type == DRAG_TYPE_PICK) {
  2807. // Draw the area being picked.
  2808. Vector2i coords = tile_set->local_to_map(mpos);
  2809. if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  2810. Transform2D tile_xform;
  2811. tile_xform.set_origin(tile_set->map_to_local(coords));
  2812. tile_xform.set_scale(tile_shape_size);
  2813. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0), false);
  2814. }
  2815. } else if (!picker_button->is_pressed() && !(drag_type == DRAG_TYPE_NONE && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && !Input::get_singleton()->is_key_pressed(Key::SHIFT))) {
  2816. bool expand_grid = false;
  2817. if (tool_buttons_group->get_pressed_button() == paint_tool_button && drag_type == DRAG_TYPE_NONE) {
  2818. // Preview for a single tile.
  2819. preview.insert(tile_set->local_to_map(mpos));
  2820. expand_grid = true;
  2821. } else if (tool_buttons_group->get_pressed_button() == line_tool_button || drag_type == DRAG_TYPE_LINE) {
  2822. if (drag_type == DRAG_TYPE_NONE) {
  2823. // Preview for a single tile.
  2824. preview.insert(tile_set->local_to_map(mpos));
  2825. } else if (drag_type == DRAG_TYPE_LINE) {
  2826. // Preview for a line.
  2827. Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos));
  2828. for (int i = 0; i < line.size(); i++) {
  2829. preview.insert(line[i]);
  2830. }
  2831. expand_grid = true;
  2832. }
  2833. } else if (drag_type == DRAG_TYPE_RECT) {
  2834. // Preview for a rect.
  2835. Rect2i rect;
  2836. rect.set_position(tile_set->local_to_map(drag_start_mouse_pos));
  2837. rect.set_end(tile_set->local_to_map(mpos));
  2838. rect = rect.abs();
  2839. HashMap<Vector2i, TileSet::TerrainsPattern> to_draw;
  2840. for (int x = rect.position.x; x <= rect.get_end().x; x++) {
  2841. for (int y = rect.position.y; y <= rect.get_end().y; y++) {
  2842. preview.insert(Vector2i(x, y));
  2843. }
  2844. }
  2845. expand_grid = true;
  2846. } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button && drag_type == DRAG_TYPE_NONE) {
  2847. // Preview for a fill.
  2848. preview = _get_cells_for_bucket_fill(tile_set->local_to_map(mpos), bucket_contiguous_checkbox->is_pressed());
  2849. }
  2850. // Expand the grid if needed
  2851. if (expand_grid && !preview.is_empty()) {
  2852. drawn_grid_rect = Rect2i(preview.front()->get(), Vector2i(1, 1));
  2853. for (const Vector2i &E : preview) {
  2854. drawn_grid_rect.expand_to(E);
  2855. }
  2856. }
  2857. }
  2858. if (!preview.is_empty()) {
  2859. const int fading = 5;
  2860. // Draw the lines of the grid behind the preview.
  2861. bool display_grid = EDITOR_GET("editors/tiles_editor/display_grid");
  2862. if (display_grid) {
  2863. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  2864. if (drawn_grid_rect.size.x > 0 && drawn_grid_rect.size.y > 0) {
  2865. drawn_grid_rect = drawn_grid_rect.grow(fading);
  2866. for (int x = drawn_grid_rect.position.x; x < (drawn_grid_rect.position.x + drawn_grid_rect.size.x); x++) {
  2867. for (int y = drawn_grid_rect.position.y; y < (drawn_grid_rect.position.y + drawn_grid_rect.size.y); y++) {
  2868. Vector2i pos_in_rect = Vector2i(x, y) - drawn_grid_rect.position;
  2869. // Fade out the border of the grid.
  2870. float left_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.x), 0.0f, 1.0f);
  2871. float right_opacity = CLAMP(Math::inverse_lerp((float)drawn_grid_rect.size.x, (float)(drawn_grid_rect.size.x - fading), (float)(pos_in_rect.x + 1)), 0.0f, 1.0f);
  2872. float top_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.y), 0.0f, 1.0f);
  2873. float bottom_opacity = CLAMP(Math::inverse_lerp((float)drawn_grid_rect.size.y, (float)(drawn_grid_rect.size.y - fading), (float)(pos_in_rect.y + 1)), 0.0f, 1.0f);
  2874. float opacity = CLAMP(MIN(left_opacity, MIN(right_opacity, MIN(top_opacity, bottom_opacity))) + 0.1, 0.0f, 1.0f);
  2875. Transform2D tile_xform;
  2876. tile_xform.set_origin(tile_set->map_to_local(Vector2(x, y)));
  2877. tile_xform.set_scale(tile_shape_size);
  2878. Color color = grid_color;
  2879. color.a = color.a * opacity;
  2880. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, color, false);
  2881. }
  2882. }
  2883. }
  2884. }
  2885. // Draw the preview.
  2886. for (const Vector2i &E : preview) {
  2887. Transform2D tile_xform;
  2888. tile_xform.set_origin(tile_set->map_to_local(E));
  2889. tile_xform.set_scale(tile_set->get_tile_size());
  2890. if (drag_erasing || erase_button->is_pressed()) {
  2891. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(0.0, 0.0, 0.0, 0.5), true);
  2892. } else {
  2893. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true);
  2894. }
  2895. }
  2896. }
  2897. }
  2898. }
  2899. void TileMapLayerEditorTerrainsPlugin::_update_terrains_cache() {
  2900. const TileMapLayer *edited_layer = _get_edited_layer();
  2901. if (!edited_layer) {
  2902. return;
  2903. }
  2904. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2905. if (tile_set.is_null()) {
  2906. return;
  2907. }
  2908. // Organizes tiles into structures.
  2909. per_terrain_terrains_patterns.resize(tile_set->get_terrain_sets_count());
  2910. for (int i = 0; i < tile_set->get_terrain_sets_count(); i++) {
  2911. per_terrain_terrains_patterns[i].resize(tile_set->get_terrains_count(i));
  2912. for (RBSet<TileSet::TerrainsPattern> &pattern : per_terrain_terrains_patterns[i]) {
  2913. pattern.clear();
  2914. }
  2915. }
  2916. for (int source_index = 0; source_index < tile_set->get_source_count(); source_index++) {
  2917. int source_id = tile_set->get_source_id(source_index);
  2918. Ref<TileSetSource> source = tile_set->get_source(source_id);
  2919. Ref<TileSetAtlasSource> atlas_source = source;
  2920. if (atlas_source.is_valid()) {
  2921. for (int tile_index = 0; tile_index < source->get_tiles_count(); tile_index++) {
  2922. Vector2i tile_id = source->get_tile_id(tile_index);
  2923. for (int alternative_index = 0; alternative_index < source->get_alternative_tiles_count(tile_id); alternative_index++) {
  2924. int alternative_id = source->get_alternative_tile_id(tile_id, alternative_index);
  2925. TileData *tile_data = atlas_source->get_tile_data(tile_id, alternative_id);
  2926. int terrain_set = tile_data->get_terrain_set();
  2927. if (terrain_set >= 0) {
  2928. ERR_FAIL_INDEX(terrain_set, (int)per_terrain_terrains_patterns.size());
  2929. TileMapCell cell;
  2930. cell.source_id = source_id;
  2931. cell.set_atlas_coords(tile_id);
  2932. cell.alternative_tile = alternative_id;
  2933. TileSet::TerrainsPattern terrains_pattern = tile_data->get_terrains_pattern();
  2934. // Terrain center bit
  2935. int terrain = terrains_pattern.get_terrain();
  2936. if (terrain >= 0 && terrain < (int)per_terrain_terrains_patterns[terrain_set].size()) {
  2937. per_terrain_terrains_patterns[terrain_set][terrain].insert(terrains_pattern);
  2938. }
  2939. // Terrain bits.
  2940. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2941. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2942. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  2943. terrain = terrains_pattern.get_terrain_peering_bit(bit);
  2944. if (terrain >= 0 && terrain < (int)per_terrain_terrains_patterns[terrain_set].size()) {
  2945. per_terrain_terrains_patterns[terrain_set][terrain].insert(terrains_pattern);
  2946. }
  2947. }
  2948. }
  2949. }
  2950. }
  2951. }
  2952. }
  2953. }
  2954. }
  2955. void TileMapLayerEditorTerrainsPlugin::_update_terrains_tree() {
  2956. terrains_tree->clear();
  2957. terrains_tree->create_item();
  2958. const TileMapLayer *edited_layer = _get_edited_layer();
  2959. ERR_FAIL_NULL(edited_layer);
  2960. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2961. if (tile_set.is_null()) {
  2962. return;
  2963. }
  2964. // Fill in the terrain list.
  2965. Vector<Vector<Ref<Texture2D>>> icons = tile_set->generate_terrains_icons(Size2(16, 16) * EDSCALE);
  2966. for (int terrain_set_index = 0; terrain_set_index < tile_set->get_terrain_sets_count(); terrain_set_index++) {
  2967. // Add an item for the terrain set.
  2968. TreeItem *terrain_set_tree_item = terrains_tree->create_item();
  2969. String matches;
  2970. if (tile_set->get_terrain_set_mode(terrain_set_index) == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  2971. terrain_set_tree_item->set_icon(0, main_vbox_container->get_editor_theme_icon(SNAME("TerrainMatchCornersAndSides")));
  2972. matches = String(TTR("Matches Corners and Sides"));
  2973. } else if (tile_set->get_terrain_set_mode(terrain_set_index) == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  2974. terrain_set_tree_item->set_icon(0, main_vbox_container->get_editor_theme_icon(SNAME("TerrainMatchCorners")));
  2975. matches = String(TTR("Matches Corners Only"));
  2976. } else {
  2977. terrain_set_tree_item->set_icon(0, main_vbox_container->get_editor_theme_icon(SNAME("TerrainMatchSides")));
  2978. matches = String(TTR("Matches Sides Only"));
  2979. }
  2980. terrain_set_tree_item->set_text(0, vformat(TTR("Terrain Set %d (%s)"), terrain_set_index, matches));
  2981. terrain_set_tree_item->set_selectable(0, false);
  2982. for (int terrain_index = 0; terrain_index < tile_set->get_terrains_count(terrain_set_index); terrain_index++) {
  2983. // Add the item to the terrain list.
  2984. TreeItem *terrain_tree_item = terrains_tree->create_item(terrain_set_tree_item);
  2985. terrain_tree_item->set_text(0, tile_set->get_terrain_name(terrain_set_index, terrain_index));
  2986. terrain_tree_item->set_icon_max_width(0, 32 * EDSCALE);
  2987. terrain_tree_item->set_icon(0, icons[terrain_set_index][terrain_index]);
  2988. Dictionary metadata_dict;
  2989. metadata_dict["terrain_set"] = terrain_set_index;
  2990. metadata_dict["terrain_id"] = terrain_index;
  2991. terrain_tree_item->set_metadata(0, metadata_dict);
  2992. }
  2993. }
  2994. }
  2995. void TileMapLayerEditorTerrainsPlugin::_update_tiles_list() {
  2996. terrains_tile_list->clear();
  2997. const TileMapLayer *edited_layer = _get_edited_layer();
  2998. if (!edited_layer) {
  2999. return;
  3000. }
  3001. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  3002. if (tile_set.is_null()) {
  3003. return;
  3004. }
  3005. TreeItem *selected_tree_item = terrains_tree->get_selected();
  3006. if (selected_tree_item && selected_tree_item->get_metadata(0)) {
  3007. Dictionary metadata_dict = selected_tree_item->get_metadata(0);
  3008. int sel_terrain_set = metadata_dict["terrain_set"];
  3009. int sel_terrain_id = metadata_dict["terrain_id"];
  3010. ERR_FAIL_INDEX(sel_terrain_set, tile_set->get_terrain_sets_count());
  3011. ERR_FAIL_INDEX(sel_terrain_id, tile_set->get_terrains_count(sel_terrain_set));
  3012. // Add the two first generic modes
  3013. int item_index = terrains_tile_list->add_icon_item(main_vbox_container->get_editor_theme_icon(SNAME("TerrainConnect")));
  3014. terrains_tile_list->set_item_tooltip(item_index, TTR("Connect mode: paints a terrain, then connects it with the surrounding tiles with the same terrain."));
  3015. Dictionary list_metadata_dict;
  3016. list_metadata_dict["type"] = SELECTED_TYPE_CONNECT;
  3017. terrains_tile_list->set_item_metadata(item_index, list_metadata_dict);
  3018. item_index = terrains_tile_list->add_icon_item(main_vbox_container->get_editor_theme_icon(SNAME("TerrainPath")));
  3019. terrains_tile_list->set_item_tooltip(item_index, TTR("Path mode: paints a terrain, then connects it to the previous tile painted within the same stroke."));
  3020. list_metadata_dict = Dictionary();
  3021. list_metadata_dict["type"] = SELECTED_TYPE_PATH;
  3022. terrains_tile_list->set_item_metadata(item_index, list_metadata_dict);
  3023. // Sort the items in a map by the number of corresponding terrains.
  3024. RBMap<int, RBSet<TileSet::TerrainsPattern>> sorted;
  3025. for (const TileSet::TerrainsPattern &E : per_terrain_terrains_patterns[sel_terrain_set][sel_terrain_id]) {
  3026. // Count the number of matching sides/terrains.
  3027. int count = 0;
  3028. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  3029. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  3030. if (tile_set->is_valid_terrain_peering_bit(sel_terrain_set, bit) && E.get_terrain_peering_bit(bit) == sel_terrain_id) {
  3031. count++;
  3032. }
  3033. }
  3034. sorted[count].insert(E);
  3035. }
  3036. for (RBMap<int, RBSet<TileSet::TerrainsPattern>>::Element *E_set = sorted.back(); E_set; E_set = E_set->prev()) {
  3037. for (const TileSet::TerrainsPattern &E : E_set->get()) {
  3038. TileSet::TerrainsPattern terrains_pattern = E;
  3039. // Get the icon.
  3040. Ref<Texture2D> icon;
  3041. Rect2 region;
  3042. bool transpose = false;
  3043. double max_probability = -1.0;
  3044. for (const TileMapCell &cell : tile_set->get_tiles_for_terrains_pattern(sel_terrain_set, terrains_pattern)) {
  3045. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  3046. Ref<TileSetAtlasSource> atlas_source = source;
  3047. if (atlas_source.is_valid()) {
  3048. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  3049. if (tile_data->get_probability() > max_probability) {
  3050. icon = atlas_source->get_texture();
  3051. region = atlas_source->get_tile_texture_region(cell.get_atlas_coords());
  3052. if (tile_data->get_flip_h()) {
  3053. region.position.x += region.size.x;
  3054. region.size.x = -region.size.x;
  3055. }
  3056. if (tile_data->get_flip_v()) {
  3057. region.position.y += region.size.y;
  3058. region.size.y = -region.size.y;
  3059. }
  3060. transpose = tile_data->get_transpose();
  3061. max_probability = tile_data->get_probability();
  3062. }
  3063. }
  3064. }
  3065. // Create the ItemList's item.
  3066. item_index = terrains_tile_list->add_item("");
  3067. terrains_tile_list->set_item_icon(item_index, icon);
  3068. terrains_tile_list->set_item_icon_region(item_index, region);
  3069. terrains_tile_list->set_item_icon_transposed(item_index, transpose);
  3070. list_metadata_dict = Dictionary();
  3071. list_metadata_dict["type"] = SELECTED_TYPE_PATTERN;
  3072. list_metadata_dict["terrains_pattern"] = terrains_pattern.as_array();
  3073. terrains_tile_list->set_item_metadata(item_index, list_metadata_dict);
  3074. }
  3075. }
  3076. if (terrains_tile_list->get_item_count() > 0) {
  3077. terrains_tile_list->select(0);
  3078. }
  3079. }
  3080. }
  3081. void TileMapLayerEditorTerrainsPlugin::_update_theme() {
  3082. paint_tool_button->set_button_icon(main_vbox_container->get_editor_theme_icon(SNAME("Edit")));
  3083. line_tool_button->set_button_icon(main_vbox_container->get_editor_theme_icon(SNAME("Line")));
  3084. rect_tool_button->set_button_icon(main_vbox_container->get_editor_theme_icon(SNAME("Rectangle")));
  3085. bucket_tool_button->set_button_icon(main_vbox_container->get_editor_theme_icon(SNAME("Bucket")));
  3086. picker_button->set_button_icon(main_vbox_container->get_editor_theme_icon(SNAME("ColorPick")));
  3087. erase_button->set_button_icon(main_vbox_container->get_editor_theme_icon(SNAME("Eraser")));
  3088. _update_tiles_list();
  3089. }
  3090. void TileMapLayerEditorTerrainsPlugin::edit(ObjectID p_edited_tile_map_layer_id) {
  3091. _stop_dragging(); // Avoids staying in a wrong drag state.
  3092. if (edited_tile_map_layer_id != p_edited_tile_map_layer_id) {
  3093. edited_tile_map_layer_id = p_edited_tile_map_layer_id;
  3094. // Clear the selection.
  3095. _update_terrains_cache();
  3096. _update_terrains_tree();
  3097. _update_tiles_list();
  3098. }
  3099. }
  3100. TileMapLayerEditorTerrainsPlugin::TileMapLayerEditorTerrainsPlugin() {
  3101. main_vbox_container = memnew(VBoxContainer);
  3102. // FIXME: This can trigger theme updates when the nodes that we want to update are not yet available.
  3103. // The toolbar should be extracted to a dedicated control and theme updates should be handled through
  3104. // the notification.
  3105. main_vbox_container->connect(SceneStringName(theme_changed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_theme));
  3106. main_vbox_container->set_name(TTR("Terrains"));
  3107. HSplitContainer *tilemap_tab_terrains = memnew(HSplitContainer);
  3108. tilemap_tab_terrains->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  3109. tilemap_tab_terrains->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  3110. main_vbox_container->add_child(tilemap_tab_terrains);
  3111. terrains_tree = memnew(Tree);
  3112. terrains_tree->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  3113. terrains_tree->set_stretch_ratio(0.25);
  3114. terrains_tree->set_custom_minimum_size(Size2(70, 0) * EDSCALE);
  3115. terrains_tree->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  3116. terrains_tree->set_hide_root(true);
  3117. terrains_tree->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_tiles_list));
  3118. tilemap_tab_terrains->add_child(terrains_tree);
  3119. terrains_tile_list = memnew(ItemList);
  3120. terrains_tile_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  3121. terrains_tile_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  3122. terrains_tile_list->set_max_columns(0);
  3123. terrains_tile_list->set_same_column_width(true);
  3124. terrains_tile_list->set_fixed_icon_size(Size2(32, 32) * EDSCALE);
  3125. terrains_tile_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  3126. tilemap_tab_terrains->add_child(terrains_tile_list);
  3127. // --- Toolbar ---
  3128. toolbar = memnew(HBoxContainer);
  3129. HBoxContainer *tilemap_tiles_tools_buttons = memnew(HBoxContainer);
  3130. tool_buttons_group.instantiate();
  3131. paint_tool_button = memnew(Button);
  3132. paint_tool_button->set_theme_type_variation("FlatButton");
  3133. paint_tool_button->set_toggle_mode(true);
  3134. paint_tool_button->set_button_group(tool_buttons_group);
  3135. paint_tool_button->set_pressed(true);
  3136. paint_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/paint_tool"));
  3137. paint_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar));
  3138. tilemap_tiles_tools_buttons->add_child(paint_tool_button);
  3139. viewport_shortcut_buttons.push_back(paint_tool_button);
  3140. line_tool_button = memnew(Button);
  3141. line_tool_button->set_theme_type_variation("FlatButton");
  3142. line_tool_button->set_toggle_mode(true);
  3143. line_tool_button->set_button_group(tool_buttons_group);
  3144. line_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/line_tool"));
  3145. line_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar));
  3146. tilemap_tiles_tools_buttons->add_child(line_tool_button);
  3147. viewport_shortcut_buttons.push_back(line_tool_button);
  3148. rect_tool_button = memnew(Button);
  3149. rect_tool_button->set_theme_type_variation("FlatButton");
  3150. rect_tool_button->set_toggle_mode(true);
  3151. rect_tool_button->set_button_group(tool_buttons_group);
  3152. rect_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/rect_tool"));
  3153. rect_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar));
  3154. tilemap_tiles_tools_buttons->add_child(rect_tool_button);
  3155. viewport_shortcut_buttons.push_back(rect_tool_button);
  3156. bucket_tool_button = memnew(Button);
  3157. bucket_tool_button->set_theme_type_variation("FlatButton");
  3158. bucket_tool_button->set_toggle_mode(true);
  3159. bucket_tool_button->set_button_group(tool_buttons_group);
  3160. bucket_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/bucket_tool"));
  3161. bucket_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar));
  3162. tilemap_tiles_tools_buttons->add_child(bucket_tool_button);
  3163. viewport_shortcut_buttons.push_back(bucket_tool_button);
  3164. toolbar->add_child(tilemap_tiles_tools_buttons);
  3165. // -- TileMap tool settings --
  3166. tools_settings = memnew(HBoxContainer);
  3167. toolbar->add_child(tools_settings);
  3168. tools_settings_vsep = memnew(VSeparator);
  3169. tools_settings->add_child(tools_settings_vsep);
  3170. // Picker
  3171. picker_button = memnew(Button);
  3172. picker_button->set_theme_type_variation("FlatButton");
  3173. picker_button->set_toggle_mode(true);
  3174. picker_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/picker"));
  3175. picker_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  3176. tools_settings->add_child(picker_button);
  3177. viewport_shortcut_buttons.push_back(picker_button);
  3178. // Erase button.
  3179. erase_button = memnew(Button);
  3180. erase_button->set_theme_type_variation("FlatButton");
  3181. erase_button->set_toggle_mode(true);
  3182. erase_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/eraser"));
  3183. erase_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  3184. tools_settings->add_child(erase_button);
  3185. viewport_shortcut_buttons.push_back(erase_button);
  3186. // Separator 2.
  3187. tools_settings_vsep_2 = memnew(VSeparator);
  3188. tools_settings->add_child(tools_settings_vsep_2);
  3189. // Continuous checkbox.
  3190. bucket_contiguous_checkbox = memnew(CheckBox);
  3191. bucket_contiguous_checkbox->set_flat(true);
  3192. bucket_contiguous_checkbox->set_text(TTR("Contiguous"));
  3193. bucket_contiguous_checkbox->set_pressed(true);
  3194. tools_settings->add_child(bucket_contiguous_checkbox);
  3195. }
  3196. TileMapLayerEditorTerrainsPlugin::~TileMapLayerEditorTerrainsPlugin() {
  3197. }
  3198. TileMapLayer *TileMapLayerEditor::_get_edited_layer() const {
  3199. return Object::cast_to<TileMapLayer>(ObjectDB::get_instance(edited_tile_map_layer_id));
  3200. }
  3201. void TileMapLayerEditor::_find_tile_map_layers_in_scene(Node *p_current, const Node *p_owner, Vector<TileMapLayer *> &r_list) const {
  3202. ERR_FAIL_COND(!p_current || !p_owner);
  3203. if (p_current != p_owner) {
  3204. if (!p_current->get_owner()) {
  3205. return;
  3206. }
  3207. if (p_current->get_owner() != p_owner && !p_owner->is_editable_instance(p_current->get_owner())) {
  3208. return;
  3209. }
  3210. }
  3211. TileMapLayer *layer = Object::cast_to<TileMapLayer>(p_current);
  3212. if (layer) {
  3213. r_list.append(layer);
  3214. }
  3215. for (int i = 0; i < p_current->get_child_count(); i++) {
  3216. Node *child = p_current->get_child(i);
  3217. _find_tile_map_layers_in_scene(child, p_owner, r_list);
  3218. }
  3219. }
  3220. void TileMapLayerEditor::_update_tile_map_layers_in_scene_list_cache() {
  3221. if (!layers_in_scene_list_cache_needs_update) {
  3222. return;
  3223. }
  3224. EditorNode *en = EditorNode::get_singleton();
  3225. Node *edited_scene_root = en->get_edited_scene();
  3226. if (!edited_scene_root) {
  3227. return;
  3228. }
  3229. tile_map_layers_in_scene_cache.clear();
  3230. _find_tile_map_layers_in_scene(edited_scene_root, edited_scene_root, tile_map_layers_in_scene_cache);
  3231. layers_in_scene_list_cache_needs_update = false;
  3232. }
  3233. void TileMapLayerEditor::_node_change(Node *p_node) {
  3234. if (!layers_in_scene_list_cache_needs_update && p_node->is_part_of_edited_scene() && Object::cast_to<TileMapLayer>(p_node)) {
  3235. layers_in_scene_list_cache_needs_update = true;
  3236. }
  3237. }
  3238. void TileMapLayerEditor::_notification(int p_what) {
  3239. switch (p_what) {
  3240. case NOTIFICATION_ENTER_TREE: {
  3241. get_tree()->connect("node_added", callable_mp(this, &TileMapLayerEditor::_node_change));
  3242. get_tree()->connect("node_removed", callable_mp(this, &TileMapLayerEditor::_node_change));
  3243. } break;
  3244. case NOTIFICATION_EXIT_TREE: {
  3245. get_tree()->disconnect("node_added", callable_mp(this, &TileMapLayerEditor::_node_change));
  3246. get_tree()->disconnect("node_removed", callable_mp(this, &TileMapLayerEditor::_node_change));
  3247. } break;
  3248. case NOTIFICATION_THEME_CHANGED: {
  3249. missing_tile_texture = get_editor_theme_icon(SNAME("StatusWarning"));
  3250. warning_pattern_texture = get_editor_theme_icon(SNAME("WarningPattern"));
  3251. advanced_menu_button->set_button_icon(get_editor_theme_icon(SNAME("Tools")));
  3252. select_previous_layer->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
  3253. select_next_layer->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
  3254. select_all_layers->set_button_icon(get_editor_theme_icon(SNAME("FileList")));
  3255. toggle_grid_button->set_button_icon(get_editor_theme_icon(SNAME("Grid")));
  3256. toggle_highlight_selected_layer_button->set_button_icon(get_editor_theme_icon(SNAME("TileMapHighlightSelected")));
  3257. } break;
  3258. case NOTIFICATION_INTERNAL_PROCESS: {
  3259. if (is_visible_in_tree() && tile_map_layer_changed_needs_update) {
  3260. _update_bottom_panel();
  3261. _update_layers_selector();
  3262. tabs_plugins[tabs_bar->get_current_tab()]->tile_set_changed();
  3263. CanvasItemEditor::get_singleton()->update_viewport();
  3264. tile_map_layer_changed_needs_update = false;
  3265. }
  3266. } break;
  3267. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  3268. if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/tiles_editor")) {
  3269. toggle_grid_button->set_pressed_no_signal(EDITOR_GET("editors/tiles_editor/display_grid"));
  3270. toggle_highlight_selected_layer_button->set_pressed_no_signal(EDITOR_GET("editors/tiles_editor/highlight_selected_layer"));
  3271. }
  3272. } break;
  3273. }
  3274. }
  3275. void TileMapLayerEditor::_on_grid_toggled(bool p_pressed) {
  3276. EditorSettings::get_singleton()->set("editors/tiles_editor/display_grid", p_pressed);
  3277. CanvasItemEditor::get_singleton()->update_viewport();
  3278. }
  3279. void TileMapLayerEditor::_select_previous_layer_pressed() {
  3280. _layers_select_next_or_previous(false);
  3281. }
  3282. void TileMapLayerEditor::_select_next_layer_pressed() {
  3283. _layers_select_next_or_previous(true);
  3284. }
  3285. void TileMapLayerEditor::_select_all_layers_pressed() {
  3286. EditorNode *en = EditorNode::get_singleton();
  3287. Node *edited_scene_root = en->get_edited_scene();
  3288. ERR_FAIL_NULL(edited_scene_root);
  3289. en->get_editor_selection()->clear();
  3290. if (tile_map_layers_in_scene_cache.size() == 1) {
  3291. en->edit_node(tile_map_layers_in_scene_cache[0]);
  3292. en->get_editor_selection()->add_node(tile_map_layers_in_scene_cache[0]);
  3293. } else {
  3294. _update_tile_map_layers_in_scene_list_cache();
  3295. Ref<MultiNodeEdit> multi_node_edit = memnew(MultiNodeEdit);
  3296. for (TileMapLayer *layer : tile_map_layers_in_scene_cache) {
  3297. multi_node_edit->add_node(edited_scene_root->get_path_to(layer));
  3298. en->get_editor_selection()->add_node(layer);
  3299. }
  3300. en->push_item(multi_node_edit.ptr());
  3301. }
  3302. }
  3303. void TileMapLayerEditor::_layers_selection_item_selected(int p_index) {
  3304. TileMapLayer *edited_layer = _get_edited_layer();
  3305. ERR_FAIL_NULL(edited_layer);
  3306. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3307. ERR_FAIL_NULL(tile_map);
  3308. TileMapLayer *new_edited = Object::cast_to<TileMapLayer>(tile_map->get_child(p_index));
  3309. edit(new_edited);
  3310. }
  3311. void TileMapLayerEditor::_update_layers_selector() {
  3312. const TileMapLayer *edited_layer = _get_edited_layer();
  3313. // Update the selector.
  3314. layers_selection_button->clear();
  3315. layers_selection_button->hide();
  3316. select_all_layers->show();
  3317. select_next_layer->set_disabled(false);
  3318. select_previous_layer->set_disabled(false);
  3319. advanced_menu_button->get_popup()->set_item_disabled(ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS, true);
  3320. if (edited_layer) {
  3321. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3322. if (tile_map && edited_layer->get_index_in_tile_map() >= 0) {
  3323. // Build the list of layers.
  3324. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3325. const TileMapLayer *layer = Object::cast_to<TileMapLayer>(tile_map->get_child(i));
  3326. if (layer) {
  3327. int index = layers_selection_button->get_item_count();
  3328. layers_selection_button->add_item(layer->get_name());
  3329. layers_selection_button->set_item_metadata(index, layer->get_name());
  3330. if (edited_layer == layer) {
  3331. layers_selection_button->select(index);
  3332. }
  3333. }
  3334. }
  3335. // Disable selector if there's no layer to select.
  3336. layers_selection_button->set_disabled(false);
  3337. if (layers_selection_button->get_item_count() == 0) {
  3338. layers_selection_button->set_disabled(true);
  3339. layers_selection_button->set_text(TTR("No Layers"));
  3340. }
  3341. // Disable next/previous if there's one or less layers.
  3342. if (layers_selection_button->get_item_count() <= 1) {
  3343. select_next_layer->set_disabled(true);
  3344. select_previous_layer->set_disabled(true);
  3345. }
  3346. layers_selection_button->show();
  3347. select_all_layers->hide();
  3348. // Enable the "extract as TileMapLayer" option only if we are editing a TleMap.
  3349. advanced_menu_button->get_popup()->set_item_disabled(ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS, false);
  3350. }
  3351. } else {
  3352. select_all_layers->hide();
  3353. select_next_layer->set_disabled(true);
  3354. select_previous_layer->set_disabled(true);
  3355. }
  3356. }
  3357. void TileMapLayerEditor::_clear_all_layers_highlighting() {
  3358. // Note: This function might be removed if we remove the TileMap node at some point.
  3359. // All processing could be done in _update_all_layers_highlighting otherwise.
  3360. TileMapLayer *edited_layer = _get_edited_layer();
  3361. // Use default mode.
  3362. if (edited_layer && edited_layer->get_index_in_tile_map() >= 0) {
  3363. // For the TileMap node.
  3364. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3365. if (tile_map) {
  3366. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3367. TileMapLayer *layer = Object::cast_to<TileMapLayer>(tile_map->get_child(i));
  3368. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT);
  3369. }
  3370. }
  3371. } else {
  3372. // For other TileMapLayer nodes.
  3373. _update_tile_map_layers_in_scene_list_cache();
  3374. for (TileMapLayer *layer : tile_map_layers_in_scene_cache) {
  3375. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT);
  3376. }
  3377. }
  3378. }
  3379. void TileMapLayerEditor::_update_all_layers_highlighting() {
  3380. EditorNode *en = EditorNode::get_singleton();
  3381. Node *edited_scene_root = en->get_edited_scene();
  3382. if (!edited_scene_root) {
  3383. return;
  3384. }
  3385. // Get selected layer.
  3386. TileMapLayer *edited_layer = _get_edited_layer();
  3387. bool highlight_selected_layer = EDITOR_GET("editors/tiles_editor/highlight_selected_layer");
  3388. if (edited_layer && highlight_selected_layer) {
  3389. int edited_z_index = edited_layer->get_z_index();
  3390. if (edited_layer->get_index_in_tile_map() >= 0) {
  3391. // For the TileMap node.
  3392. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3393. ERR_FAIL_NULL(tile_map);
  3394. bool passed = false;
  3395. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3396. TileMapLayer *layer = Object::cast_to<TileMapLayer>(tile_map->get_child(i));
  3397. if (layer == edited_layer) {
  3398. passed = true;
  3399. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT);
  3400. } else {
  3401. if (passed || layer->get_z_index() > edited_z_index) {
  3402. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_ABOVE);
  3403. } else {
  3404. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_BELOW);
  3405. }
  3406. }
  3407. }
  3408. } else {
  3409. // Update highlight mode for independent layers.
  3410. _update_tile_map_layers_in_scene_list_cache();
  3411. bool passed = false;
  3412. for (TileMapLayer *layer : tile_map_layers_in_scene_cache) {
  3413. if (layer == edited_layer) {
  3414. passed = true;
  3415. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT);
  3416. } else {
  3417. if (passed || layer->get_z_index() > edited_z_index) {
  3418. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_ABOVE);
  3419. } else {
  3420. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_BELOW);
  3421. }
  3422. }
  3423. }
  3424. }
  3425. }
  3426. }
  3427. void TileMapLayerEditor::_highlight_selected_layer_button_toggled(bool p_pressed) {
  3428. TileMapLayer *edited_layer = _get_edited_layer();
  3429. if (!edited_layer) {
  3430. return;
  3431. }
  3432. EditorSettings::get_singleton()->set("editors/tiles_editor/highlight_selected_layer", p_pressed);
  3433. if (p_pressed) {
  3434. _update_all_layers_highlighting();
  3435. } else {
  3436. _clear_all_layers_highlighting();
  3437. }
  3438. }
  3439. void TileMapLayerEditor::_advanced_menu_button_id_pressed(int p_id) {
  3440. TileMapLayer *edited_layer = _get_edited_layer();
  3441. if (!edited_layer) {
  3442. return;
  3443. }
  3444. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  3445. if (tile_set.is_null()) {
  3446. return;
  3447. }
  3448. if (p_id == ADVANCED_MENU_REPLACE_WITH_PROXIES) { // Replace Tile Proxies
  3449. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3450. undo_redo->create_action(TTR("Replace Tiles with Proxies"));
  3451. TypedArray<Vector2i> used_cells = edited_layer->get_used_cells();
  3452. for (int i = 0; i < used_cells.size(); i++) {
  3453. Vector2i cell_coords = used_cells[i];
  3454. TileMapCell from = edited_layer->get_cell(cell_coords);
  3455. Array to_array = tile_set->map_tile_proxy(from.source_id, from.get_atlas_coords(), from.alternative_tile);
  3456. TileMapCell to;
  3457. to.source_id = to_array[0];
  3458. to.set_atlas_coords(to_array[1]);
  3459. to.alternative_tile = to_array[2];
  3460. if (from != to) {
  3461. undo_redo->add_do_method(edited_layer, "set_cell", cell_coords, to.source_id, to.get_atlas_coords(), to.alternative_tile);
  3462. undo_redo->add_undo_method(edited_layer, "set_cell", cell_coords, from.source_id, from.get_atlas_coords(), from.alternative_tile);
  3463. }
  3464. }
  3465. undo_redo->commit_action();
  3466. } else if (p_id == ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS) { // Transform internal TileMap layers into TileMapLayers.
  3467. ERR_FAIL_COND(edited_layer->get_index_in_tile_map() < 0);
  3468. EditorNode *en = EditorNode::get_singleton();
  3469. Node *edited_scene_root = en->get_edited_scene();
  3470. ERR_FAIL_NULL(edited_scene_root);
  3471. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3472. undo_redo->create_action(TTR("Extract TileMap layers as individual TileMapLayer nodes"));
  3473. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3474. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3475. undo_redo->add_do_method(tile_map, "remove_layer", 0);
  3476. }
  3477. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3478. TileMapLayer *new_layer = tile_map->duplicate_layer_from_internal(i);
  3479. undo_redo->add_do_method(tile_map, "add_child", new_layer);
  3480. undo_redo->add_do_method(new_layer, "set_owner", edited_scene_root);
  3481. undo_redo->add_do_property(new_layer, "tile_set", tile_map->get_tileset()); // Workaround for a bug: #89947.
  3482. undo_redo->add_undo_method(tile_map, "remove_child", new_layer);
  3483. undo_redo->add_do_reference(new_layer);
  3484. }
  3485. List<PropertyInfo> prop_list;
  3486. tile_map->get_property_list(&prop_list);
  3487. for (PropertyInfo &prop : prop_list) {
  3488. undo_redo->add_undo_property(tile_map, prop.name, tile_map->get(prop.name));
  3489. }
  3490. undo_redo->commit_action();
  3491. }
  3492. }
  3493. void TileMapLayerEditor::_update_bottom_panel() {
  3494. const TileMapLayer *edited_layer = _get_edited_layer();
  3495. Ref<TileSet> tile_set;
  3496. if (edited_layer) {
  3497. tile_set = edited_layer->get_tile_set();
  3498. }
  3499. // Update state labels.
  3500. if (is_multi_node_edit) {
  3501. cant_edit_label->set_text(TTR("Can't edit multiple layers at once."));
  3502. cant_edit_label->show();
  3503. } else if (!edited_layer) {
  3504. cant_edit_label->set_text(TTR("The selected TileMap has no layer to edit."));
  3505. cant_edit_label->show();
  3506. } else if (!edited_layer->is_enabled() || !edited_layer->is_visible_in_tree()) {
  3507. cant_edit_label->set_text(TTR("The edited layer is disabled or invisible"));
  3508. cant_edit_label->show();
  3509. } else if (tile_set.is_null()) {
  3510. cant_edit_label->set_text(TTR("The edited TileMap or TileMapLayer node has no TileSet resource.\nCreate or load a TileSet resource in the Tile Set property in the inspector."));
  3511. cant_edit_label->show();
  3512. } else {
  3513. cant_edit_label->hide();
  3514. }
  3515. // Update tabs visibility.
  3516. for (int i = 0; i < int(tabs_data.size()); i++) {
  3517. TileMapLayerSubEditorPlugin::TabData &tab_data = tabs_data[i];
  3518. if (i == tabs_bar->get_current_tab()) {
  3519. tab_data.panel->set_visible(!cant_edit_label->is_visible());
  3520. } else {
  3521. tab_data.panel->hide();
  3522. }
  3523. }
  3524. }
  3525. Vector<Vector2i> TileMapLayerEditor::get_line(const TileMapLayer *p_tile_map_layer, Vector2i p_from_cell, Vector2i p_to_cell) {
  3526. ERR_FAIL_NULL_V(p_tile_map_layer, Vector<Vector2i>());
  3527. Ref<TileSet> tile_set = p_tile_map_layer->get_tile_set();
  3528. ERR_FAIL_COND_V(tile_set.is_null(), Vector<Vector2i>());
  3529. if (tile_set->get_tile_shape() == TileSet::TILE_SHAPE_SQUARE) {
  3530. return Geometry2D::bresenham_line(p_from_cell, p_to_cell);
  3531. } else {
  3532. // Adapt the bresenham line algorithm to half-offset shapes.
  3533. // See this blog post: http://zvold.blogspot.com/2010/01/bresenhams-line-drawing-algorithm-on_26.html
  3534. Vector<Point2i> points;
  3535. bool transposed = tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL;
  3536. p_from_cell = TileSet::transform_coords_layout(p_from_cell, tile_set->get_tile_offset_axis(), tile_set->get_tile_layout(), TileSet::TILE_LAYOUT_STACKED);
  3537. p_to_cell = TileSet::transform_coords_layout(p_to_cell, tile_set->get_tile_offset_axis(), tile_set->get_tile_layout(), TileSet::TILE_LAYOUT_STACKED);
  3538. if (transposed) {
  3539. SWAP(p_from_cell.x, p_from_cell.y);
  3540. SWAP(p_to_cell.x, p_to_cell.y);
  3541. }
  3542. Vector2i delta = p_to_cell - p_from_cell;
  3543. delta = Vector2i(2 * delta.x + ABS(p_to_cell.y % 2) - ABS(p_from_cell.y % 2), delta.y);
  3544. Vector2i sign = delta.sign();
  3545. Vector2i current = p_from_cell;
  3546. points.push_back(TileSet::transform_coords_layout(transposed ? Vector2i(current.y, current.x) : current, tile_set->get_tile_offset_axis(), TileSet::TILE_LAYOUT_STACKED, tile_set->get_tile_layout()));
  3547. int err = 0;
  3548. if (ABS(delta.y) < ABS(delta.x)) {
  3549. Vector2i err_step = 3 * delta.abs();
  3550. while (current != p_to_cell) {
  3551. err += err_step.y;
  3552. if (err > ABS(delta.x)) {
  3553. if (sign.x == 0) {
  3554. current += Vector2(sign.y, 0);
  3555. } else {
  3556. current += Vector2(bool(current.y % 2) ^ (sign.x < 0) ? sign.x : 0, sign.y);
  3557. }
  3558. err -= err_step.x;
  3559. } else {
  3560. current += Vector2i(sign.x, 0);
  3561. err += err_step.y;
  3562. }
  3563. points.push_back(TileSet::transform_coords_layout(transposed ? Vector2i(current.y, current.x) : current, tile_set->get_tile_offset_axis(), TileSet::TILE_LAYOUT_STACKED, tile_set->get_tile_layout()));
  3564. }
  3565. } else {
  3566. Vector2i err_step = delta.abs();
  3567. while (current != p_to_cell) {
  3568. err += err_step.x;
  3569. if (err > 0) {
  3570. if (sign.x == 0) {
  3571. current += Vector2(0, sign.y);
  3572. } else {
  3573. current += Vector2(bool(current.y % 2) ^ (sign.x < 0) ? sign.x : 0, sign.y);
  3574. }
  3575. err -= err_step.y;
  3576. } else {
  3577. if (sign.x == 0) {
  3578. current += Vector2(0, sign.y);
  3579. } else {
  3580. current += Vector2(bool(current.y % 2) ^ (sign.x > 0) ? -sign.x : 0, sign.y);
  3581. }
  3582. err += err_step.y;
  3583. }
  3584. points.push_back(TileSet::transform_coords_layout(transposed ? Vector2i(current.y, current.x) : current, tile_set->get_tile_offset_axis(), TileSet::TILE_LAYOUT_STACKED, tile_set->get_tile_layout()));
  3585. }
  3586. }
  3587. return points;
  3588. }
  3589. }
  3590. void TileMapLayerEditor::_tile_map_layer_changed() {
  3591. tile_map_layer_changed_needs_update = true;
  3592. }
  3593. void TileMapLayerEditor::_tab_changed(int p_tab_id) {
  3594. // Make the plugin edit the correct tilemap.
  3595. tabs_plugins[tabs_bar->get_current_tab()]->edit(edited_tile_map_layer_id);
  3596. // Update toolbar.
  3597. for (TileMapLayerSubEditorPlugin::TabData &tab_data : tabs_data) {
  3598. tab_data.toolbar->hide();
  3599. }
  3600. tabs_data[p_tab_id].toolbar->show();
  3601. // Update visible panel.
  3602. for (TileMapLayerSubEditorPlugin::TabData &tab_data : tabs_data) {
  3603. tab_data.panel->hide();
  3604. }
  3605. TileMapLayer *tile_map_layer = _get_edited_layer();
  3606. if (tile_map_layer) {
  3607. if (tile_map_layer->get_tile_set().is_valid()) {
  3608. tabs_data[tabs_bar->get_current_tab()].panel->show();
  3609. }
  3610. }
  3611. // Graphical update.
  3612. tabs_data[tabs_bar->get_current_tab()].panel->queue_redraw();
  3613. CanvasItemEditor::get_singleton()->update_viewport();
  3614. }
  3615. void TileMapLayerEditor::_layers_select_next_or_previous(bool p_next) {
  3616. const TileMapLayer *edited_layer = _get_edited_layer();
  3617. if (!edited_layer) {
  3618. return;
  3619. }
  3620. EditorNode *en = EditorNode::get_singleton();
  3621. Node *edited_scene_root = en->get_edited_scene();
  3622. ERR_FAIL_NULL(edited_scene_root);
  3623. TileMapLayer *new_selected_layer = nullptr;
  3624. int inc = p_next ? 1 : -1;
  3625. if (edited_layer->get_index_in_tile_map() >= 0) {
  3626. // Part of a TileMap.
  3627. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3628. new_selected_layer = Object::cast_to<TileMapLayer>(tile_map->get_child(Math::posmod(edited_layer->get_index_in_tile_map() + inc, tile_map->get_layers_count())));
  3629. } else {
  3630. // Individual layer.
  3631. _update_tile_map_layers_in_scene_list_cache();
  3632. int edited_index = -1;
  3633. for (int i = 0; i < tile_map_layers_in_scene_cache.size(); i++) {
  3634. if (tile_map_layers_in_scene_cache[i] == edited_layer) {
  3635. edited_index = i;
  3636. break;
  3637. }
  3638. }
  3639. new_selected_layer = tile_map_layers_in_scene_cache[Math::posmod(edited_index + inc, tile_map_layers_in_scene_cache.size())];
  3640. }
  3641. ERR_FAIL_NULL(new_selected_layer);
  3642. if (edited_layer->get_index_in_tile_map() < 0) {
  3643. // Only if not part of a TileMap.
  3644. en->edit_node(new_selected_layer);
  3645. en->get_editor_selection()->clear();
  3646. en->get_editor_selection()->add_node(new_selected_layer);
  3647. } else {
  3648. edit(new_selected_layer);
  3649. }
  3650. }
  3651. void TileMapLayerEditor::_move_tile_map_array_element(Object *p_undo_redo, Object *p_edited, const String &p_array_prefix, int p_from_index, int p_to_pos) {
  3652. EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  3653. ERR_FAIL_NULL(undo_redo_man);
  3654. TileMap *tile_map = Object::cast_to<TileMap>(p_edited);
  3655. if (!tile_map) {
  3656. return;
  3657. }
  3658. // Compute the array indices to save.
  3659. int begin = 0;
  3660. int end;
  3661. if (p_array_prefix == "layer_") {
  3662. end = tile_map->get_layers_count();
  3663. } else {
  3664. ERR_FAIL_MSG("Invalid array prefix for TileSet.");
  3665. }
  3666. if (p_from_index < 0) {
  3667. // Adding new.
  3668. if (p_to_pos >= 0) {
  3669. begin = p_to_pos;
  3670. } else {
  3671. end = 0; // Nothing to save when adding at the end.
  3672. }
  3673. } else if (p_to_pos < 0) {
  3674. // Removing.
  3675. begin = p_from_index;
  3676. } else {
  3677. // Moving.
  3678. begin = MIN(p_from_index, p_to_pos);
  3679. end = MIN(MAX(p_from_index, p_to_pos) + 1, end);
  3680. }
  3681. #define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));
  3682. // Save layers' properties.
  3683. if (p_from_index < 0) {
  3684. undo_redo_man->add_undo_method(tile_map, "remove_layer", p_to_pos < 0 ? tile_map->get_layers_count() : p_to_pos);
  3685. } else if (p_to_pos < 0) {
  3686. undo_redo_man->add_undo_method(tile_map, "add_layer", p_from_index);
  3687. }
  3688. List<PropertyInfo> properties;
  3689. tile_map->get_property_list(&properties);
  3690. for (PropertyInfo pi : properties) {
  3691. if (pi.name.begins_with(p_array_prefix)) {
  3692. String str = pi.name.trim_prefix(p_array_prefix);
  3693. int to_char_index = 0;
  3694. while (to_char_index < str.length()) {
  3695. if (!is_digit(str[to_char_index])) {
  3696. break;
  3697. }
  3698. to_char_index++;
  3699. }
  3700. if (to_char_index > 0) {
  3701. int array_index = str.left(to_char_index).to_int();
  3702. if (array_index >= begin && array_index < end) {
  3703. ADD_UNDO(tile_map, pi.name);
  3704. }
  3705. }
  3706. }
  3707. }
  3708. #undef ADD_UNDO
  3709. if (p_from_index < 0) {
  3710. undo_redo_man->add_do_method(tile_map, "add_layer", p_to_pos);
  3711. } else if (p_to_pos < 0) {
  3712. undo_redo_man->add_do_method(tile_map, "remove_layer", p_from_index);
  3713. } else {
  3714. undo_redo_man->add_do_method(tile_map, "move_layer", p_from_index, p_to_pos);
  3715. }
  3716. }
  3717. bool TileMapLayerEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  3718. if (ED_IS_SHORTCUT("tiles_editor/select_next_layer", p_event) && p_event->is_pressed()) {
  3719. _layers_select_next_or_previous(true);
  3720. return true;
  3721. }
  3722. if (ED_IS_SHORTCUT("tiles_editor/select_previous_layer", p_event) && p_event->is_pressed()) {
  3723. _layers_select_next_or_previous(false);
  3724. return true;
  3725. }
  3726. return tabs_plugins[tabs_bar->get_current_tab()]->forward_canvas_gui_input(p_event);
  3727. }
  3728. void TileMapLayerEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  3729. const TileMapLayer *edited_layer = _get_edited_layer();
  3730. if (!edited_layer) {
  3731. return;
  3732. }
  3733. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  3734. if (tile_set.is_null()) {
  3735. return;
  3736. }
  3737. if (!edited_layer->is_visible_in_tree()) {
  3738. return;
  3739. }
  3740. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  3741. Transform2D xform_inv = xform.affine_inverse();
  3742. Vector2i tile_shape_size = tile_set->get_tile_size();
  3743. // Draw tiles with invalid IDs in the grid.
  3744. TypedArray<Vector2i> used_cells = edited_layer->get_used_cells();
  3745. for (int i = 0; i < used_cells.size(); i++) {
  3746. Vector2i coords = used_cells[i];
  3747. int tile_source_id = edited_layer->get_cell_source_id(coords);
  3748. if (tile_source_id >= 0) {
  3749. Vector2i tile_atlas_coords = edited_layer->get_cell_atlas_coords(coords);
  3750. int tile_alternative_tile = edited_layer->get_cell_alternative_tile(coords);
  3751. TileSetSource *source = nullptr;
  3752. if (tile_set->has_source(tile_source_id)) {
  3753. source = *tile_set->get_source(tile_source_id);
  3754. }
  3755. if (!source || !source->has_tile(tile_atlas_coords) || !source->has_alternative_tile(tile_atlas_coords, tile_alternative_tile)) {
  3756. // Generate a random color from the hashed identifier of the tiles.
  3757. Array to_hash;
  3758. to_hash.push_back(tile_source_id);
  3759. to_hash.push_back(tile_atlas_coords);
  3760. to_hash.push_back(tile_alternative_tile);
  3761. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  3762. Color color;
  3763. color = color.from_hsv(
  3764. (float)((hash >> 24) & 0xFF) / 256.0,
  3765. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  3766. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  3767. 0.8);
  3768. // Display the warning pattern.
  3769. Transform2D tile_xform;
  3770. tile_xform.set_origin(tile_set->map_to_local(coords));
  3771. tile_xform.set_scale(tile_shape_size);
  3772. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, color, true, warning_pattern_texture);
  3773. // Draw the warning icon.
  3774. Vector2::Axis min_axis = missing_tile_texture->get_size().min_axis_index();
  3775. Vector2 icon_size;
  3776. icon_size[min_axis] = tile_set->get_tile_size()[min_axis] / 3;
  3777. icon_size[(min_axis + 1) % 2] = (icon_size[min_axis] * missing_tile_texture->get_size()[(min_axis + 1) % 2] / missing_tile_texture->get_size()[min_axis]);
  3778. Rect2 rect = Rect2(xform.xform(tile_set->map_to_local(coords)) - (icon_size * xform.get_scale() / 2), icon_size * xform.get_scale());
  3779. p_overlay->draw_texture_rect(missing_tile_texture, rect);
  3780. }
  3781. }
  3782. }
  3783. // Fading on the border.
  3784. const int fading = 5;
  3785. // Determine the drawn area.
  3786. Size2 screen_size = p_overlay->get_size();
  3787. Rect2i screen_rect;
  3788. screen_rect.position = tile_set->local_to_map(xform_inv.xform(Vector2()));
  3789. screen_rect.expand_to(tile_set->local_to_map(xform_inv.xform(Vector2(0, screen_size.height))));
  3790. screen_rect.expand_to(tile_set->local_to_map(xform_inv.xform(Vector2(screen_size.width, 0))));
  3791. screen_rect.expand_to(tile_set->local_to_map(xform_inv.xform(screen_size)));
  3792. screen_rect = screen_rect.grow(1);
  3793. Rect2i tilemap_used_rect = edited_layer->get_used_rect();
  3794. Rect2i displayed_rect = tilemap_used_rect.intersection(screen_rect);
  3795. displayed_rect = displayed_rect.grow(fading);
  3796. // Reduce the drawn area to avoid crashes if needed.
  3797. int max_size = 100;
  3798. if (displayed_rect.size.x > max_size) {
  3799. displayed_rect = displayed_rect.grow_individual(-(displayed_rect.size.x - max_size) / 2, 0, -(displayed_rect.size.x - max_size) / 2, 0);
  3800. }
  3801. if (displayed_rect.size.y > max_size) {
  3802. displayed_rect = displayed_rect.grow_individual(0, -(displayed_rect.size.y - max_size) / 2, 0, -(displayed_rect.size.y - max_size) / 2);
  3803. }
  3804. // Draw the grid.
  3805. bool display_grid = EDITOR_GET("editors/tiles_editor/display_grid");
  3806. if (display_grid) {
  3807. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  3808. for (int x = displayed_rect.position.x; x < (displayed_rect.position.x + displayed_rect.size.x); x++) {
  3809. for (int y = displayed_rect.position.y; y < (displayed_rect.position.y + displayed_rect.size.y); y++) {
  3810. Vector2i pos_in_rect = Vector2i(x, y) - displayed_rect.position;
  3811. // Fade out the border of the grid.
  3812. float left_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.x), 0.0f, 1.0f);
  3813. float right_opacity = CLAMP(Math::inverse_lerp((float)displayed_rect.size.x, (float)(displayed_rect.size.x - fading), (float)(pos_in_rect.x + 1)), 0.0f, 1.0f);
  3814. float top_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.y), 0.0f, 1.0f);
  3815. float bottom_opacity = CLAMP(Math::inverse_lerp((float)displayed_rect.size.y, (float)(displayed_rect.size.y - fading), (float)(pos_in_rect.y + 1)), 0.0f, 1.0f);
  3816. float opacity = CLAMP(MIN(left_opacity, MIN(right_opacity, MIN(top_opacity, bottom_opacity))) + 0.1, 0.0f, 1.0f);
  3817. Transform2D tile_xform;
  3818. tile_xform.set_origin(tile_set->map_to_local(Vector2(x, y)));
  3819. tile_xform.set_scale(tile_shape_size);
  3820. Color color = grid_color;
  3821. color.a = color.a * opacity;
  3822. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, color, false);
  3823. }
  3824. }
  3825. }
  3826. // Draw the IDs for debug.
  3827. /*Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  3828. for (int x = displayed_rect.position.x; x < (displayed_rect.position.x + displayed_rect.size.x); x++) {
  3829. for (int y = displayed_rect.position.y; y < (displayed_rect.position.y + displayed_rect.size.y); y++) {
  3830. p_overlay->draw_string(font, xform.xform(tile_set->map_to_local(Vector2(x, y))) + Vector2i(-tile_shape_size.x / 2, 0), vformat("%s", Vector2(x, y)));
  3831. }
  3832. }*/
  3833. // Draw the plugins.
  3834. tabs_plugins[tabs_bar->get_current_tab()]->forward_canvas_draw_over_viewport(p_overlay);
  3835. }
  3836. void TileMapLayerEditor::edit(Object *p_edited) {
  3837. if (p_edited && p_edited->get_instance_id() == edited_tile_map_layer_id) {
  3838. return;
  3839. }
  3840. _clear_all_layers_highlighting();
  3841. // Disconnect to changes.
  3842. TileMapLayer *tile_map_layer = _get_edited_layer();
  3843. if (tile_map_layer) {
  3844. tile_map_layer->disconnect(CoreStringName(changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed));
  3845. tile_map_layer->disconnect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed));
  3846. }
  3847. // Update the edited layer.
  3848. TileMapLayer *new_layer = Object::cast_to<TileMapLayer>(p_edited);
  3849. if (new_layer) {
  3850. // Change the edited object.
  3851. edited_tile_map_layer_id = new_layer->get_instance_id();
  3852. tile_map_layer = _get_edited_layer();
  3853. // Connect to changes.
  3854. if (!tile_map_layer->is_connected(CoreStringName(changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed))) {
  3855. tile_map_layer->connect(CoreStringName(changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed));
  3856. tile_map_layer->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed));
  3857. }
  3858. } else {
  3859. edited_tile_map_layer_id = ObjectID();
  3860. }
  3861. // Check if we are trying to use a MultiNodeEdit.
  3862. is_multi_node_edit = Object::cast_to<MultiNodeEdit>(p_edited);
  3863. // Call the plugins and update everything.
  3864. tabs_plugins[tabs_bar->get_current_tab()]->edit(edited_tile_map_layer_id);
  3865. _update_layers_selector();
  3866. _update_all_layers_highlighting();
  3867. _tile_map_layer_changed();
  3868. }
  3869. void TileMapLayerEditor::set_show_layer_selector(bool p_show_layer_selector) {
  3870. show_layers_selector = p_show_layer_selector;
  3871. _update_layers_selector();
  3872. }
  3873. TileMapLayerEditor::TileMapLayerEditor() {
  3874. set_process_internal(true);
  3875. // Shortcuts.
  3876. ED_SHORTCUT("tiles_editor/select_next_layer", TTR("Select Next Tile Map Layer"), Key::PAGEDOWN);
  3877. ED_SHORTCUT("tiles_editor/select_previous_layer", TTR("Select Previous Tile Map Layer"), Key::PAGEUP);
  3878. // TileMap editor plugins
  3879. tile_map_editor_plugins.push_back(memnew(TileMapLayerEditorTilesPlugin));
  3880. tile_map_editor_plugins.push_back(memnew(TileMapLayerEditorTerrainsPlugin));
  3881. // TabBar.
  3882. tabs_bar = memnew(TabBar);
  3883. tabs_bar->set_clip_tabs(false);
  3884. for (int plugin_index = 0; plugin_index < tile_map_editor_plugins.size(); plugin_index++) {
  3885. Vector<TileMapLayerSubEditorPlugin::TabData> tabs_vector = tile_map_editor_plugins[plugin_index]->get_tabs();
  3886. for (int tab_index = 0; tab_index < tabs_vector.size(); tab_index++) {
  3887. tabs_bar->add_tab(tabs_vector[tab_index].panel->get_name());
  3888. tabs_data.push_back(tabs_vector[tab_index]);
  3889. tabs_plugins.push_back(tile_map_editor_plugins[plugin_index]);
  3890. }
  3891. }
  3892. tabs_bar->connect("tab_changed", callable_mp(this, &TileMapLayerEditor::_tab_changed));
  3893. // --- TileMap toolbar ---
  3894. tile_map_toolbar = memnew(HFlowContainer);
  3895. tile_map_toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
  3896. add_child(tile_map_toolbar);
  3897. // Tabs.
  3898. tile_map_toolbar->add_child(tabs_bar);
  3899. // Tabs toolbars.
  3900. for (TileMapLayerSubEditorPlugin::TabData &tab_data : tabs_data) {
  3901. tab_data.toolbar->hide();
  3902. if (!tab_data.toolbar->get_parent()) {
  3903. tile_map_toolbar->add_child(tab_data.toolbar);
  3904. }
  3905. }
  3906. // Wide empty separation control. (like BoxContainer::add_spacer())
  3907. Control *c = memnew(Control);
  3908. c->set_mouse_filter(MOUSE_FILTER_PASS);
  3909. c->set_h_size_flags(SIZE_EXPAND_FILL);
  3910. tile_map_toolbar->add_child(c);
  3911. // Layer selector.
  3912. layer_selection_hbox = memnew(HBoxContainer);
  3913. tile_map_toolbar->add_child(layer_selection_hbox);
  3914. layers_selection_button = memnew(OptionButton);
  3915. layers_selection_button->set_custom_minimum_size(Size2(200, 0));
  3916. layers_selection_button->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  3917. layers_selection_button->set_tooltip_text(TTR("TileMap Layers"));
  3918. layers_selection_button->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditor::_layers_selection_item_selected));
  3919. layer_selection_hbox->add_child(layers_selection_button);
  3920. select_previous_layer = memnew(Button);
  3921. select_previous_layer->set_theme_type_variation("FlatButton");
  3922. select_previous_layer->set_tooltip_text(TTR("Select previous layer"));
  3923. select_previous_layer->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditor::_select_previous_layer_pressed));
  3924. layer_selection_hbox->add_child(select_previous_layer);
  3925. select_next_layer = memnew(Button);
  3926. select_next_layer->set_theme_type_variation("FlatButton");
  3927. select_next_layer->set_tooltip_text(TTR("Select next layer"));
  3928. select_next_layer->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditor::_select_next_layer_pressed));
  3929. layer_selection_hbox->add_child(select_next_layer);
  3930. select_all_layers = memnew(Button);
  3931. select_all_layers->set_theme_type_variation("FlatButton");
  3932. select_all_layers->set_text(TTR("Select all layers"));
  3933. select_all_layers->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditor::_select_all_layers_pressed));
  3934. select_all_layers->set_tooltip_text(TTR("Select all TileMapLayers in scene"));
  3935. layer_selection_hbox->add_child(select_all_layers);
  3936. // Highlighting selected layer.
  3937. toggle_highlight_selected_layer_button = memnew(Button);
  3938. toggle_highlight_selected_layer_button->set_theme_type_variation("FlatButton");
  3939. toggle_highlight_selected_layer_button->set_toggle_mode(true);
  3940. toggle_highlight_selected_layer_button->set_pressed(true);
  3941. toggle_highlight_selected_layer_button->connect(SceneStringName(toggled), callable_mp(this, &TileMapLayerEditor::_highlight_selected_layer_button_toggled));
  3942. toggle_highlight_selected_layer_button->set_tooltip_text(TTR("Highlight Selected TileMap Layer"));
  3943. tile_map_toolbar->add_child(toggle_highlight_selected_layer_button);
  3944. tile_map_toolbar->add_child(memnew(VSeparator));
  3945. // Grid toggle.
  3946. toggle_grid_button = memnew(Button);
  3947. toggle_grid_button->set_theme_type_variation("FlatButton");
  3948. toggle_grid_button->set_toggle_mode(true);
  3949. toggle_grid_button->set_tooltip_text(TTR("Toggle grid visibility."));
  3950. toggle_grid_button->connect(SceneStringName(toggled), callable_mp(this, &TileMapLayerEditor::_on_grid_toggled));
  3951. tile_map_toolbar->add_child(toggle_grid_button);
  3952. // Advanced settings menu button.
  3953. advanced_menu_button = memnew(MenuButton);
  3954. advanced_menu_button->set_flat(false);
  3955. advanced_menu_button->set_theme_type_variation("FlatButton");
  3956. advanced_menu_button->get_popup()->add_item(TTR("Automatically Replace Tiles with Proxies"), ADVANCED_MENU_REPLACE_WITH_PROXIES);
  3957. advanced_menu_button->get_popup()->add_item(TTR("Extract TileMap layers as individual TileMapLayer nodes"), ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS);
  3958. advanced_menu_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileMapLayerEditor::_advanced_menu_button_id_pressed));
  3959. tile_map_toolbar->add_child(advanced_menu_button);
  3960. // A label for editing errors.
  3961. cant_edit_label = memnew(Label);
  3962. cant_edit_label->set_h_size_flags(SIZE_EXPAND_FILL);
  3963. cant_edit_label->set_v_size_flags(SIZE_EXPAND_FILL);
  3964. cant_edit_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  3965. cant_edit_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  3966. cant_edit_label->hide();
  3967. add_child(cant_edit_label);
  3968. for (unsigned int tab_index = 0; tab_index < tabs_data.size(); tab_index++) {
  3969. add_child(tabs_data[tab_index].panel);
  3970. tabs_data[tab_index].panel->set_v_size_flags(SIZE_EXPAND_FILL);
  3971. tabs_data[tab_index].panel->set_visible(tab_index == 0);
  3972. tabs_data[tab_index].panel->set_h_size_flags(SIZE_EXPAND_FILL);
  3973. }
  3974. _tab_changed(0);
  3975. // Registers UndoRedo inspector callback.
  3976. EditorNode::get_editor_data().add_move_array_element_function(SNAME("TileMap"), callable_mp(this, &TileMapLayerEditor::_move_tile_map_array_element));
  3977. }
  3978. TileMapLayerEditor::~TileMapLayerEditor() {
  3979. for (int i = 0; i < tile_map_editor_plugins.size(); i++) {
  3980. memdelete(tile_map_editor_plugins[i]);
  3981. }
  3982. }