tile_map_layer_editor.cpp 186 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536
  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_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Sort")));
  435. select_tool_button->set_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("ToolSelect")));
  436. paint_tool_button->set_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Edit")));
  437. line_tool_button->set_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Line")));
  438. rect_tool_button->set_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Rectangle")));
  439. bucket_tool_button->set_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Bucket")));
  440. picker_button->set_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("ColorPick")));
  441. erase_button->set_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("Eraser")));
  442. random_tile_toggle->set_icon(tiles_bottom_panel->get_editor_theme_icon(SNAME("RandomNumberGenerator")));
  443. transform_button_rotate_left->set_icon(tiles_bottom_panel->get_editor_theme_icon("RotateLeft"));
  444. transform_button_rotate_right->set_icon(tiles_bottom_panel->get_editor_theme_icon("RotateRight"));
  445. transform_button_flip_h->set_icon(tiles_bottom_panel->get_editor_theme_icon("MirrorX"));
  446. transform_button_flip_v->set_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, selection_used_cells[i], selection_pattern);
  1210. cells_undo[coords] = TileMapCell(drag_modified[coords].source_id, drag_modified[coords].get_atlas_coords(), drag_modified[coords].alternative_tile);
  1211. coords = tile_set->map_pattern(top_left + offset, selection_used_cells[i], selection_pattern);
  1212. 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));
  1213. }
  1214. // Build the list of cells to do.
  1215. HashMap<Vector2i, TileMapCell> cells_do;
  1216. for (int i = 0; i < selection_used_cells.size(); i++) {
  1217. coords = tile_set->map_pattern(top_left, selection_used_cells[i], selection_pattern);
  1218. cells_do[coords] = TileMapCell();
  1219. }
  1220. for (int i = 0; i < selection_used_cells.size(); i++) {
  1221. coords = tile_set->map_pattern(top_left + offset, selection_used_cells[i], selection_pattern);
  1222. 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]));
  1223. }
  1224. // Move the tiles.
  1225. undo_redo->create_action(TTR("Move tiles"));
  1226. for (const KeyValue<Vector2i, TileMapCell> &E : cells_do) {
  1227. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1228. }
  1229. for (const KeyValue<Vector2i, TileMapCell> &E : cells_undo) {
  1230. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1231. }
  1232. // Update the selection.
  1233. undo_redo->add_undo_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  1234. tile_map_selection.clear();
  1235. for (int i = 0; i < selection_used_cells.size(); i++) {
  1236. coords = tile_set->map_pattern(top_left + offset, selection_used_cells[i], selection_pattern);
  1237. tile_map_selection.insert(coords);
  1238. }
  1239. undo_redo->add_do_method(this, "_set_tile_map_selection", _get_tile_map_selection());
  1240. undo_redo->commit_action();
  1241. }
  1242. } break;
  1243. case DRAG_TYPE_PICK: {
  1244. 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();
  1245. rect.size += Vector2i(1, 1);
  1246. int picked_source = -1;
  1247. TypedArray<Vector2i> coords_array;
  1248. for (int x = rect.position.x; x < rect.get_end().x; x++) {
  1249. for (int y = rect.position.y; y < rect.get_end().y; y++) {
  1250. Vector2i coords = Vector2i(x, y);
  1251. int source = edited_layer->get_cell_source_id(coords);
  1252. if (source != TileSet::INVALID_SOURCE) {
  1253. coords_array.push_back(coords);
  1254. if (picked_source == -1) {
  1255. picked_source = source;
  1256. } else if (picked_source != source) {
  1257. picked_source = -2;
  1258. }
  1259. }
  1260. }
  1261. }
  1262. if (picked_source >= 0) {
  1263. for (int i = 0; i < sources_list->get_item_count(); i++) {
  1264. if (int(sources_list->get_item_metadata(i)) == picked_source) {
  1265. sources_list->set_current(i);
  1266. TilesEditorUtils::get_singleton()->set_sources_lists_current(i);
  1267. break;
  1268. }
  1269. }
  1270. sources_list->ensure_current_is_visible();
  1271. }
  1272. Ref<TileMapPattern> new_selection_pattern = edited_layer->get_pattern(coords_array);
  1273. if (!new_selection_pattern->is_empty()) {
  1274. selection_pattern = new_selection_pattern;
  1275. _update_tileset_selection_from_selection_pattern();
  1276. }
  1277. picker_button->set_pressed(false);
  1278. } break;
  1279. case DRAG_TYPE_PAINT: {
  1280. undo_redo->create_action(TTR("Paint tiles"));
  1281. for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) {
  1282. 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));
  1283. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1284. }
  1285. undo_redo->commit_action(false);
  1286. } break;
  1287. case DRAG_TYPE_LINE: {
  1288. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(drag_start_mouse_pos, drag_start_mouse_pos, mpos, drag_erasing);
  1289. undo_redo->create_action(TTR("Paint tiles"));
  1290. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  1291. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  1292. continue;
  1293. }
  1294. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1295. 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));
  1296. }
  1297. undo_redo->commit_action();
  1298. } break;
  1299. case DRAG_TYPE_RECT: {
  1300. HashMap<Vector2i, TileMapCell> to_draw = _draw_rect(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing);
  1301. undo_redo->create_action(TTR("Paint tiles"));
  1302. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  1303. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  1304. continue;
  1305. }
  1306. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1307. 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));
  1308. }
  1309. undo_redo->commit_action();
  1310. } break;
  1311. case DRAG_TYPE_BUCKET: {
  1312. undo_redo->create_action(TTR("Paint tiles"));
  1313. for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) {
  1314. 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));
  1315. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  1316. }
  1317. undo_redo->commit_action(false);
  1318. } break;
  1319. case DRAG_TYPE_CLIPBOARD_PASTE: {
  1320. Vector2 mouse_offset = (Vector2(tile_map_clipboard->get_size()) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size();
  1321. undo_redo->create_action(TTR("Paste tiles"));
  1322. TypedArray<Vector2i> used_cells = tile_map_clipboard->get_used_cells();
  1323. for (int i = 0; i < used_cells.size(); i++) {
  1324. Vector2i coords = tile_set->map_pattern(tile_set->local_to_map(mpos - mouse_offset), used_cells[i], tile_map_clipboard);
  1325. 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]));
  1326. 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));
  1327. }
  1328. undo_redo->commit_action();
  1329. } break;
  1330. default:
  1331. break;
  1332. }
  1333. drag_type = DRAG_TYPE_NONE;
  1334. }
  1335. void TileMapLayerEditorTilesPlugin::_apply_transform(int p_type) {
  1336. if (selection_pattern.is_null() || selection_pattern->is_empty()) {
  1337. return;
  1338. }
  1339. Ref<TileMapPattern> transformed_pattern;
  1340. transformed_pattern.instantiate();
  1341. bool keep_shape = selection_pattern->get_size() == Vector2i(1, 1);
  1342. Vector2i size = selection_pattern->get_size();
  1343. for (int y = 0; y < size.y; y++) {
  1344. for (int x = 0; x < size.x; x++) {
  1345. Vector2i src_coords = Vector2i(x, y);
  1346. if (!selection_pattern->has_cell(src_coords)) {
  1347. continue;
  1348. }
  1349. Vector2i dst_coords;
  1350. if (keep_shape) {
  1351. dst_coords = src_coords;
  1352. } else if (p_type == TRANSFORM_ROTATE_LEFT) {
  1353. dst_coords = Vector2i(y, size.x - x - 1);
  1354. } else if (p_type == TRANSFORM_ROTATE_RIGHT) {
  1355. dst_coords = Vector2i(size.y - y - 1, x);
  1356. } else if (p_type == TRANSFORM_FLIP_H) {
  1357. dst_coords = Vector2i(size.x - x - 1, y);
  1358. } else if (p_type == TRANSFORM_FLIP_V) {
  1359. dst_coords = Vector2i(x, size.y - y - 1);
  1360. }
  1361. transformed_pattern->set_cell(dst_coords,
  1362. selection_pattern->get_cell_source_id(src_coords), selection_pattern->get_cell_atlas_coords(src_coords),
  1363. _get_transformed_alternative(selection_pattern->get_cell_alternative_tile(src_coords), p_type));
  1364. }
  1365. }
  1366. selection_pattern = transformed_pattern;
  1367. CanvasItemEditor::get_singleton()->update_viewport();
  1368. }
  1369. int TileMapLayerEditorTilesPlugin::_get_transformed_alternative(int p_alternative_id, int p_transform) {
  1370. bool transform_flip_h = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_H;
  1371. bool transform_flip_v = p_alternative_id & TileSetAtlasSource::TRANSFORM_FLIP_V;
  1372. bool transform_transpose = p_alternative_id & TileSetAtlasSource::TRANSFORM_TRANSPOSE;
  1373. switch (p_transform) {
  1374. case TRANSFORM_ROTATE_LEFT:
  1375. case TRANSFORM_ROTATE_RIGHT: {
  1376. // A matrix with every possible flip/transpose combination, sorted by what comes next when you rotate.
  1377. const LocalVector<bool> rotation_matrix = {
  1378. 0, 0, 0,
  1379. 0, 1, 1,
  1380. 1, 1, 0,
  1381. 1, 0, 1,
  1382. 1, 0, 0,
  1383. 0, 0, 1,
  1384. 0, 1, 0,
  1385. 1, 1, 1
  1386. };
  1387. for (int i = 0; i < 8; i++) {
  1388. if (transform_flip_h == rotation_matrix[i * 3] && transform_flip_v == rotation_matrix[i * 3 + 1] && transform_transpose == rotation_matrix[i * 3 + 2]) {
  1389. if (p_transform == TRANSFORM_ROTATE_LEFT) {
  1390. i = i / 4 * 4 + (i + 1) % 4;
  1391. } else {
  1392. i = i / 4 * 4 + Math::posmod(i - 1, 4);
  1393. }
  1394. transform_flip_h = rotation_matrix[i * 3];
  1395. transform_flip_v = rotation_matrix[i * 3 + 1];
  1396. transform_transpose = rotation_matrix[i * 3 + 2];
  1397. break;
  1398. }
  1399. }
  1400. } break;
  1401. case TRANSFORM_FLIP_H: {
  1402. transform_flip_h = !transform_flip_h;
  1403. } break;
  1404. case TRANSFORM_FLIP_V: {
  1405. transform_flip_v = !transform_flip_v;
  1406. } break;
  1407. }
  1408. return TileSetAtlasSource::alternative_no_transform(p_alternative_id) |
  1409. int(transform_flip_h) * TileSetAtlasSource::TRANSFORM_FLIP_H |
  1410. int(transform_flip_v) * TileSetAtlasSource::TRANSFORM_FLIP_V |
  1411. int(transform_transpose) * TileSetAtlasSource::TRANSFORM_TRANSPOSE;
  1412. }
  1413. void TileMapLayerEditorTilesPlugin::_update_fix_selected_and_hovered() {
  1414. TileMapLayer *edited_layer = _get_edited_layer();
  1415. if (!edited_layer) {
  1416. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1417. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1418. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1419. tile_set_selection.clear();
  1420. patterns_item_list->deselect_all();
  1421. tile_map_selection.clear();
  1422. selection_pattern.instantiate();
  1423. return;
  1424. }
  1425. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1426. if (tile_set.is_null()) {
  1427. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1428. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1429. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1430. tile_set_selection.clear();
  1431. patterns_item_list->deselect_all();
  1432. tile_map_selection.clear();
  1433. selection_pattern.instantiate();
  1434. return;
  1435. }
  1436. int source_index = sources_list->get_current();
  1437. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1438. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1439. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1440. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1441. tile_set_selection.clear();
  1442. patterns_item_list->deselect_all();
  1443. tile_map_selection.clear();
  1444. selection_pattern.instantiate();
  1445. return;
  1446. }
  1447. int source_id = sources_list->get_item_metadata(source_index);
  1448. // Clear hovered if needed.
  1449. if (source_id != hovered_tile.source_id ||
  1450. !tile_set->has_source(hovered_tile.source_id) ||
  1451. !tile_set->get_source(hovered_tile.source_id)->has_tile(hovered_tile.get_atlas_coords()) ||
  1452. !tile_set->get_source(hovered_tile.source_id)->has_alternative_tile(hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile)) {
  1453. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1454. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1455. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1456. }
  1457. // Cleanup tile set selection.
  1458. for (RBSet<TileMapCell>::Element *E = tile_set_selection.front(); E;) {
  1459. RBSet<TileMapCell>::Element *N = E->next();
  1460. const TileMapCell *selected = &(E->get());
  1461. if (!tile_set->has_source(selected->source_id) ||
  1462. !tile_set->get_source(selected->source_id)->has_tile(selected->get_atlas_coords()) ||
  1463. !tile_set->get_source(selected->source_id)->has_alternative_tile(selected->get_atlas_coords(), selected->alternative_tile)) {
  1464. tile_set_selection.erase(E);
  1465. }
  1466. E = N;
  1467. }
  1468. // Cleanup selection.
  1469. for (const KeyValue<Vector2i, TileMapCell> &E : selection_pattern->get_pattern()) {
  1470. const Vector2i key = E.key;
  1471. const TileMapCell &selected = E.value;
  1472. if (!tile_set->has_source(selected.source_id) ||
  1473. !tile_set->get_source(selected.source_id)->has_tile(selected.get_atlas_coords()) ||
  1474. !tile_set->get_source(selected.source_id)->has_alternative_tile(selected.get_atlas_coords(), selected.alternative_tile)) {
  1475. selection_pattern->remove_cell(key);
  1476. }
  1477. }
  1478. }
  1479. void TileMapLayerEditorTilesPlugin::_fix_invalid_tiles_in_tile_map_selection() {
  1480. TileMapLayer *edited_layer = _get_edited_layer();
  1481. if (!edited_layer) {
  1482. return;
  1483. }
  1484. RBSet<Vector2i> to_remove;
  1485. for (Vector2i selected : tile_map_selection) {
  1486. TileMapCell cell = edited_layer->get_cell(selected);
  1487. if (cell.source_id == TileSet::INVALID_SOURCE && cell.get_atlas_coords() == TileSetSource::INVALID_ATLAS_COORDS && cell.alternative_tile == TileSetAtlasSource::INVALID_TILE_ALTERNATIVE) {
  1488. to_remove.insert(selected);
  1489. }
  1490. }
  1491. for (Vector2i cell : to_remove) {
  1492. tile_map_selection.erase(cell);
  1493. }
  1494. }
  1495. void TileMapLayerEditorTilesPlugin::patterns_item_list_empty_clicked(const Vector2 &p_pos, MouseButton p_mouse_button_index) {
  1496. if (p_mouse_button_index == MouseButton::LEFT) {
  1497. _update_selection_pattern_from_tileset_pattern_selection();
  1498. }
  1499. }
  1500. void TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tilemap_selection() {
  1501. TileMapLayer *edited_layer = _get_edited_layer();
  1502. if (!edited_layer) {
  1503. return;
  1504. }
  1505. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1506. if (tile_set.is_null()) {
  1507. return;
  1508. }
  1509. selection_pattern.instantiate();
  1510. TypedArray<Vector2i> coords_array;
  1511. for (const Vector2i &E : tile_map_selection) {
  1512. coords_array.push_back(E);
  1513. }
  1514. selection_pattern = edited_layer->get_pattern(coords_array);
  1515. _update_transform_buttons();
  1516. }
  1517. void TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_tiles_selection() {
  1518. TileMapLayer *edited_layer = _get_edited_layer();
  1519. if (!edited_layer) {
  1520. return;
  1521. }
  1522. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1523. if (tile_set.is_null()) {
  1524. return;
  1525. }
  1526. // Clear the tilemap selection.
  1527. tile_map_selection.clear();
  1528. // Clear the selected pattern.
  1529. selection_pattern.instantiate();
  1530. // Group per source.
  1531. HashMap<int, List<const TileMapCell *>> per_source;
  1532. for (const TileMapCell &E : tile_set_selection) {
  1533. per_source[E.source_id].push_back(&(E));
  1534. }
  1535. int vertical_offset = 0;
  1536. for (const KeyValue<int, List<const TileMapCell *>> &E_source : per_source) {
  1537. // Per source.
  1538. List<const TileMapCell *> unorganized;
  1539. Rect2i encompassing_rect_coords;
  1540. HashMap<Vector2i, const TileMapCell *> organized_pattern;
  1541. TileSetSource *source = *tile_set->get_source(E_source.key);
  1542. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  1543. if (atlas_source) {
  1544. // Organize using coordinates.
  1545. for (const TileMapCell *current : E_source.value) {
  1546. if (current->alternative_tile == 0) {
  1547. organized_pattern[current->get_atlas_coords()] = current;
  1548. } else {
  1549. unorganized.push_back(current);
  1550. }
  1551. }
  1552. // Compute the encompassing rect for the organized pattern.
  1553. HashMap<Vector2i, const TileMapCell *>::Iterator E_cell = organized_pattern.begin();
  1554. if (E_cell) {
  1555. encompassing_rect_coords = Rect2i(E_cell->key, Vector2i(1, 1));
  1556. for (; E_cell; ++E_cell) {
  1557. encompassing_rect_coords.expand_to(E_cell->key + Vector2i(1, 1));
  1558. encompassing_rect_coords.expand_to(E_cell->key);
  1559. }
  1560. }
  1561. } else {
  1562. // Add everything unorganized.
  1563. for (const TileMapCell *cell : E_source.value) {
  1564. unorganized.push_back(cell);
  1565. }
  1566. }
  1567. // Now add everything to the output pattern.
  1568. for (const KeyValue<Vector2i, const TileMapCell *> &E_cell : organized_pattern) {
  1569. 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);
  1570. }
  1571. Vector2i organized_size = selection_pattern->get_size();
  1572. int unorganized_index = 0;
  1573. for (const TileMapCell *cell : unorganized) {
  1574. selection_pattern->set_cell(Vector2(organized_size.x + unorganized_index, vertical_offset), cell->source_id, cell->get_atlas_coords(), cell->alternative_tile);
  1575. unorganized_index++;
  1576. }
  1577. vertical_offset += MAX(organized_size.y, 1);
  1578. }
  1579. CanvasItemEditor::get_singleton()->update_viewport();
  1580. _update_transform_buttons();
  1581. }
  1582. void TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_pattern_selection() {
  1583. TileMapLayer *edited_layer = _get_edited_layer();
  1584. if (!edited_layer) {
  1585. return;
  1586. }
  1587. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1588. if (tile_set.is_null()) {
  1589. return;
  1590. }
  1591. // Clear the tilemap selection.
  1592. tile_map_selection.clear();
  1593. // Clear the selected pattern.
  1594. selection_pattern.instantiate();
  1595. if (patterns_item_list->get_selected_items().size() >= 1) {
  1596. selection_pattern = patterns_item_list->get_item_metadata(patterns_item_list->get_selected_items()[0]);
  1597. }
  1598. CanvasItemEditor::get_singleton()->update_viewport();
  1599. }
  1600. void TileMapLayerEditorTilesPlugin::_update_tileset_selection_from_selection_pattern() {
  1601. tile_set_selection.clear();
  1602. TypedArray<Vector2i> used_cells = selection_pattern->get_used_cells();
  1603. for (int i = 0; i < used_cells.size(); i++) {
  1604. Vector2i coords = used_cells[i];
  1605. if (selection_pattern->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  1606. 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)));
  1607. }
  1608. }
  1609. _update_source_display();
  1610. tile_atlas_control->queue_redraw();
  1611. alternative_tiles_control->queue_redraw();
  1612. _update_transform_buttons();
  1613. }
  1614. void TileMapLayerEditorTilesPlugin::_tile_atlas_control_draw() {
  1615. TileMapLayer *edited_layer = _get_edited_layer();
  1616. if (!edited_layer) {
  1617. return;
  1618. }
  1619. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1620. if (tile_set.is_null()) {
  1621. return;
  1622. }
  1623. int source_index = sources_list->get_current();
  1624. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1625. return;
  1626. }
  1627. int source_id = sources_list->get_item_metadata(source_index);
  1628. if (!tile_set->has_source(source_id)) {
  1629. return;
  1630. }
  1631. TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  1632. if (!atlas) {
  1633. return;
  1634. }
  1635. // Draw the selection.
  1636. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  1637. 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);
  1638. for (const TileMapCell &E : tile_set_selection) {
  1639. int16_t untransformed_alternative_id = E.alternative_tile & TileSetAtlasSource::UNTRANSFORM_MASK;
  1640. if (E.source_id == source_id && untransformed_alternative_id == 0) {
  1641. for (int frame = 0; frame < atlas->get_tile_animation_frames_count(E.get_atlas_coords()); frame++) {
  1642. Color color = selection_color;
  1643. if (frame > 0) {
  1644. color.a *= 0.3;
  1645. }
  1646. TilesEditorUtils::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(E.get_atlas_coords(), frame), color);
  1647. }
  1648. }
  1649. }
  1650. // Draw the hovered tile.
  1651. if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile == 0 && !tile_set_dragging_selection) {
  1652. for (int frame = 0; frame < atlas->get_tile_animation_frames_count(hovered_tile.get_atlas_coords()); frame++) {
  1653. Color color = Color(1.0, 0.8, 0.0, frame == 0 ? 0.6 : 0.3);
  1654. TilesEditorUtils::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(hovered_tile.get_atlas_coords(), frame), color);
  1655. }
  1656. }
  1657. // Draw the selection rect.
  1658. if (tile_set_dragging_selection) {
  1659. Vector2i start_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_set_drag_start_mouse_pos, true);
  1660. Vector2i end_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1661. Rect2i region = Rect2i(start_tile, end_tile - start_tile).abs();
  1662. region.size += Vector2i(1, 1);
  1663. RBSet<Vector2i> to_draw;
  1664. for (int x = region.position.x; x < region.get_end().x; x++) {
  1665. for (int y = region.position.y; y < region.get_end().y; y++) {
  1666. Vector2i tile = atlas->get_tile_at_coords(Vector2i(x, y));
  1667. if (tile != TileSetSource::INVALID_ATLAS_COORDS) {
  1668. to_draw.insert(tile);
  1669. }
  1670. }
  1671. }
  1672. for (const Vector2i &E : to_draw) {
  1673. TilesEditorUtils::draw_selection_rect(tile_atlas_control, atlas->get_tile_texture_region(E));
  1674. }
  1675. }
  1676. }
  1677. void TileMapLayerEditorTilesPlugin::_tile_atlas_control_mouse_exited() {
  1678. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1679. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1680. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1681. tile_atlas_control->queue_redraw();
  1682. }
  1683. void TileMapLayerEditorTilesPlugin::_tile_atlas_control_gui_input(const Ref<InputEvent> &p_event) {
  1684. TileMapLayer *edited_layer = _get_edited_layer();
  1685. if (!edited_layer) {
  1686. return;
  1687. }
  1688. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1689. if (tile_set.is_null()) {
  1690. return;
  1691. }
  1692. int source_index = sources_list->get_current();
  1693. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1694. return;
  1695. }
  1696. int source_id = sources_list->get_item_metadata(source_index);
  1697. if (!tile_set->has_source(source_id)) {
  1698. return;
  1699. }
  1700. TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  1701. if (!atlas) {
  1702. return;
  1703. }
  1704. // Update the hovered tile
  1705. hovered_tile.source_id = source_id;
  1706. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1707. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1708. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  1709. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1710. coords = atlas->get_tile_at_coords(coords);
  1711. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1712. hovered_tile.set_atlas_coords(coords);
  1713. hovered_tile.alternative_tile = 0;
  1714. }
  1715. }
  1716. Ref<InputEventMouseMotion> mm = p_event;
  1717. if (mm.is_valid()) {
  1718. tile_atlas_control->queue_redraw();
  1719. alternative_tiles_control->queue_redraw();
  1720. }
  1721. Ref<InputEventMouseButton> mb = p_event;
  1722. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  1723. if (mb->is_pressed()) { // Pressed
  1724. tile_set_dragging_selection = true;
  1725. tile_set_drag_start_mouse_pos = tile_atlas_control->get_local_mouse_position();
  1726. if (!mb->is_shift_pressed()) {
  1727. tile_set_selection.clear();
  1728. }
  1729. if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile == 0) {
  1730. if (mb->is_shift_pressed() && tile_set_selection.has(TileMapCell(source_id, hovered_tile.get_atlas_coords(), 0))) {
  1731. tile_set_selection.erase(TileMapCell(source_id, hovered_tile.get_atlas_coords(), 0));
  1732. } else {
  1733. tile_set_selection.insert(TileMapCell(source_id, hovered_tile.get_atlas_coords(), 0));
  1734. }
  1735. }
  1736. _update_selection_pattern_from_tileset_tiles_selection();
  1737. } else { // Released
  1738. if (tile_set_dragging_selection) {
  1739. if (!mb->is_shift_pressed()) {
  1740. tile_set_selection.clear();
  1741. }
  1742. // Compute the covered area.
  1743. Vector2i start_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_set_drag_start_mouse_pos, true);
  1744. Vector2i end_tile = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1745. if (start_tile != TileSetSource::INVALID_ATLAS_COORDS && end_tile != TileSetSource::INVALID_ATLAS_COORDS) {
  1746. Rect2i region = Rect2i(start_tile, end_tile - start_tile).abs();
  1747. region.size += Vector2i(1, 1);
  1748. // To update the selection, we copy the selected/not selected status of the tiles we drag from.
  1749. Vector2i start_coords = atlas->get_tile_at_coords(start_tile);
  1750. if (mb->is_shift_pressed() && start_coords != TileSetSource::INVALID_ATLAS_COORDS && !tile_set_selection.has(TileMapCell(source_id, start_coords, 0))) {
  1751. // Remove from the selection.
  1752. for (int x = region.position.x; x < region.get_end().x; x++) {
  1753. for (int y = region.position.y; y < region.get_end().y; y++) {
  1754. Vector2i tile_coords = atlas->get_tile_at_coords(Vector2i(x, y));
  1755. if (tile_coords != TileSetSource::INVALID_ATLAS_COORDS && tile_set_selection.has(TileMapCell(source_id, tile_coords, 0))) {
  1756. tile_set_selection.erase(TileMapCell(source_id, tile_coords, 0));
  1757. }
  1758. }
  1759. }
  1760. } else {
  1761. // Insert in the selection.
  1762. for (int x = region.position.x; x < region.get_end().x; x++) {
  1763. for (int y = region.position.y; y < region.get_end().y; y++) {
  1764. Vector2i tile_coords = atlas->get_tile_at_coords(Vector2i(x, y));
  1765. if (tile_coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1766. tile_set_selection.insert(TileMapCell(source_id, tile_coords, 0));
  1767. }
  1768. }
  1769. }
  1770. }
  1771. }
  1772. _update_selection_pattern_from_tileset_tiles_selection();
  1773. }
  1774. tile_set_dragging_selection = false;
  1775. }
  1776. tile_atlas_control->queue_redraw();
  1777. }
  1778. }
  1779. void TileMapLayerEditorTilesPlugin::_tile_alternatives_control_draw() {
  1780. TileMapLayer *edited_layer = _get_edited_layer();
  1781. if (!edited_layer) {
  1782. return;
  1783. }
  1784. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1785. if (tile_set.is_null()) {
  1786. return;
  1787. }
  1788. int source_index = sources_list->get_current();
  1789. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1790. return;
  1791. }
  1792. int source_id = sources_list->get_item_metadata(source_index);
  1793. if (!tile_set->has_source(source_id)) {
  1794. return;
  1795. }
  1796. TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  1797. if (!atlas) {
  1798. return;
  1799. }
  1800. // Draw the selection.
  1801. for (const TileMapCell &E : tile_set_selection) {
  1802. int16_t untransformed_alternative_id = E.alternative_tile & TileSetAtlasSource::UNTRANSFORM_MASK;
  1803. if (E.source_id == source_id && E.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && untransformed_alternative_id > 0) {
  1804. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(E.get_atlas_coords(), untransformed_alternative_id);
  1805. if (rect != Rect2i()) {
  1806. TilesEditorUtils::draw_selection_rect(alternative_tiles_control, rect);
  1807. }
  1808. }
  1809. }
  1810. // Draw hovered tile.
  1811. if (hovered_tile.get_atlas_coords() != TileSetSource::INVALID_ATLAS_COORDS && hovered_tile.alternative_tile > 0) {
  1812. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile);
  1813. if (rect != Rect2i()) {
  1814. TilesEditorUtils::draw_selection_rect(alternative_tiles_control, rect, Color(1.0, 0.8, 0.0, 0.5));
  1815. }
  1816. }
  1817. }
  1818. void TileMapLayerEditorTilesPlugin::_tile_alternatives_control_mouse_exited() {
  1819. hovered_tile.source_id = TileSet::INVALID_SOURCE;
  1820. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1821. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1822. alternative_tiles_control->queue_redraw();
  1823. }
  1824. void TileMapLayerEditorTilesPlugin::_tile_alternatives_control_gui_input(const Ref<InputEvent> &p_event) {
  1825. TileMapLayer *edited_layer = _get_edited_layer();
  1826. if (!edited_layer) {
  1827. return;
  1828. }
  1829. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  1830. if (tile_set.is_null()) {
  1831. return;
  1832. }
  1833. int source_index = sources_list->get_current();
  1834. if (source_index < 0 || source_index >= sources_list->get_item_count()) {
  1835. return;
  1836. }
  1837. int source_id = sources_list->get_item_metadata(source_index);
  1838. if (!tile_set->has_source(source_id)) {
  1839. return;
  1840. }
  1841. TileSetAtlasSource *atlas = Object::cast_to<TileSetAtlasSource>(*tile_set->get_source(source_id));
  1842. if (!atlas) {
  1843. return;
  1844. }
  1845. // Update the hovered tile
  1846. hovered_tile.source_id = source_id;
  1847. hovered_tile.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS);
  1848. hovered_tile.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1849. Vector3i alternative_coords = tile_atlas_view->get_alternative_tile_at_pos(alternative_tiles_control->get_local_mouse_position());
  1850. Vector2i coords = Vector2i(alternative_coords.x, alternative_coords.y);
  1851. int alternative = alternative_coords.z;
  1852. if (coords != TileSetSource::INVALID_ATLAS_COORDS && alternative != TileSetSource::INVALID_TILE_ALTERNATIVE) {
  1853. hovered_tile.set_atlas_coords(coords);
  1854. hovered_tile.alternative_tile = alternative;
  1855. }
  1856. Ref<InputEventMouseMotion> mm = p_event;
  1857. if (mm.is_valid()) {
  1858. tile_atlas_control->queue_redraw();
  1859. alternative_tiles_control->queue_redraw();
  1860. }
  1861. Ref<InputEventMouseButton> mb = p_event;
  1862. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  1863. if (mb->is_pressed()) { // Pressed
  1864. // Left click pressed.
  1865. if (!mb->is_shift_pressed()) {
  1866. tile_set_selection.clear();
  1867. }
  1868. if (coords != TileSetSource::INVALID_ATLAS_COORDS && alternative != TileSetAtlasSource::INVALID_TILE_ALTERNATIVE) {
  1869. if (mb->is_shift_pressed() && tile_set_selection.has(TileMapCell(source_id, hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile))) {
  1870. tile_set_selection.erase(TileMapCell(source_id, hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile));
  1871. } else {
  1872. tile_set_selection.insert(TileMapCell(source_id, hovered_tile.get_atlas_coords(), hovered_tile.alternative_tile));
  1873. }
  1874. }
  1875. _update_selection_pattern_from_tileset_tiles_selection();
  1876. }
  1877. tile_atlas_control->queue_redraw();
  1878. alternative_tiles_control->queue_redraw();
  1879. }
  1880. }
  1881. void TileMapLayerEditorTilesPlugin::_set_tile_map_selection(const TypedArray<Vector2i> &p_selection) {
  1882. tile_map_selection.clear();
  1883. for (int i = 0; i < p_selection.size(); i++) {
  1884. tile_map_selection.insert(p_selection[i]);
  1885. }
  1886. _update_selection_pattern_from_tilemap_selection();
  1887. _update_tileset_selection_from_selection_pattern();
  1888. CanvasItemEditor::get_singleton()->update_viewport();
  1889. }
  1890. TypedArray<Vector2i> TileMapLayerEditorTilesPlugin::_get_tile_map_selection() const {
  1891. TypedArray<Vector2i> output;
  1892. for (const Vector2i &E : tile_map_selection) {
  1893. output.push_back(E);
  1894. }
  1895. return output;
  1896. }
  1897. void TileMapLayerEditorTilesPlugin::_set_source_sort(int p_sort) {
  1898. for (int i = 0; i != TilesEditorUtils::SOURCE_SORT_MAX; i++) {
  1899. source_sort_button->get_popup()->set_item_checked(i, (i == (int)p_sort));
  1900. }
  1901. TilesEditorUtils::get_singleton()->set_sorting_option(p_sort);
  1902. _update_tile_set_sources_list();
  1903. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "tile_source_sort", p_sort);
  1904. }
  1905. void TileMapLayerEditorTilesPlugin::_bind_methods() {
  1906. ClassDB::bind_method(D_METHOD("_scene_thumbnail_done"), &TileMapLayerEditorTilesPlugin::_scene_thumbnail_done);
  1907. ClassDB::bind_method(D_METHOD("_set_tile_map_selection", "selection"), &TileMapLayerEditorTilesPlugin::_set_tile_map_selection);
  1908. ClassDB::bind_method(D_METHOD("_get_tile_map_selection"), &TileMapLayerEditorTilesPlugin::_get_tile_map_selection);
  1909. }
  1910. void TileMapLayerEditorTilesPlugin::edit(ObjectID p_tile_map_layer_id) {
  1911. _stop_dragging(); // Avoids staying in a wrong drag state.
  1912. // Disable sort button if the tileset is read-only
  1913. TileMapLayer *edited_layer = _get_edited_layer();
  1914. Ref<TileSet> tile_set;
  1915. if (edited_layer) {
  1916. tile_set = edited_layer->get_tile_set();
  1917. if (tile_set.is_valid()) {
  1918. source_sort_button->set_disabled(EditorNode::get_singleton()->is_resource_read_only(tile_set));
  1919. }
  1920. }
  1921. TileMapLayer *new_tile_map_layer = Object::cast_to<TileMapLayer>(ObjectDB::get_instance(edited_tile_map_layer_id));
  1922. Ref<TileSet> new_tile_set;
  1923. if (new_tile_map_layer) {
  1924. new_tile_set = new_tile_map_layer->get_tile_set();
  1925. }
  1926. if (tile_set.is_valid() && tile_set != new_tile_set) {
  1927. // Clear the selection.
  1928. tile_set_selection.clear();
  1929. patterns_item_list->deselect_all();
  1930. tile_map_selection.clear();
  1931. selection_pattern.instantiate();
  1932. }
  1933. edited_tile_map_layer_id = p_tile_map_layer_id;
  1934. }
  1935. TileMapLayerEditorTilesPlugin::TileMapLayerEditorTilesPlugin() {
  1936. CanvasItemEditor::get_singleton()
  1937. ->get_viewport_control()
  1938. ->connect(SceneStringName(mouse_exited), callable_mp(this, &TileMapLayerEditorTilesPlugin::_mouse_exited_viewport));
  1939. // --- Initialize references ---
  1940. tile_map_clipboard.instantiate();
  1941. selection_pattern.instantiate();
  1942. erase_pattern.instantiate();
  1943. erase_pattern->set_cell(Vector2i(0, 0), TileSet::INVALID_SOURCE, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
  1944. // --- Toolbar ---
  1945. toolbar = memnew(HBoxContainer);
  1946. HBoxContainer *tilemap_tiles_tools_buttons = memnew(HBoxContainer);
  1947. tool_buttons_group.instantiate();
  1948. select_tool_button = memnew(Button);
  1949. select_tool_button->set_theme_type_variation("FlatButton");
  1950. select_tool_button->set_toggle_mode(true);
  1951. select_tool_button->set_button_group(tool_buttons_group);
  1952. select_tool_button->set_shortcut(ED_SHORTCUT("tiles_editor/selection_tool", TTR("Selection"), Key::S));
  1953. select_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1954. tilemap_tiles_tools_buttons->add_child(select_tool_button);
  1955. viewport_shortcut_buttons.push_back(select_tool_button);
  1956. paint_tool_button = memnew(Button);
  1957. paint_tool_button->set_theme_type_variation("FlatButton");
  1958. paint_tool_button->set_toggle_mode(true);
  1959. paint_tool_button->set_button_group(tool_buttons_group);
  1960. paint_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/paint_tool"));
  1961. paint_tool_button->set_tooltip_text(TTR("Shift: Draw line.") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Shift: Draw rectangle."));
  1962. paint_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1963. tilemap_tiles_tools_buttons->add_child(paint_tool_button);
  1964. viewport_shortcut_buttons.push_back(paint_tool_button);
  1965. line_tool_button = memnew(Button);
  1966. line_tool_button->set_theme_type_variation("FlatButton");
  1967. line_tool_button->set_toggle_mode(true);
  1968. line_tool_button->set_button_group(tool_buttons_group);
  1969. // TRANSLATORS: This refers to the line tool in the tilemap editor.
  1970. line_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/line_tool"));
  1971. line_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1972. tilemap_tiles_tools_buttons->add_child(line_tool_button);
  1973. viewport_shortcut_buttons.push_back(line_tool_button);
  1974. rect_tool_button = memnew(Button);
  1975. rect_tool_button->set_theme_type_variation("FlatButton");
  1976. rect_tool_button->set_toggle_mode(true);
  1977. rect_tool_button->set_button_group(tool_buttons_group);
  1978. rect_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/rect_tool"));
  1979. rect_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1980. tilemap_tiles_tools_buttons->add_child(rect_tool_button);
  1981. viewport_shortcut_buttons.push_back(rect_tool_button);
  1982. bucket_tool_button = memnew(Button);
  1983. bucket_tool_button->set_theme_type_variation("FlatButton");
  1984. bucket_tool_button->set_toggle_mode(true);
  1985. bucket_tool_button->set_button_group(tool_buttons_group);
  1986. bucket_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/bucket_tool"));
  1987. bucket_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_toolbar));
  1988. tilemap_tiles_tools_buttons->add_child(bucket_tool_button);
  1989. toolbar->add_child(tilemap_tiles_tools_buttons);
  1990. viewport_shortcut_buttons.push_back(bucket_tool_button);
  1991. // -- TileMap tool settings --
  1992. tools_settings = memnew(HBoxContainer);
  1993. toolbar->add_child(tools_settings);
  1994. tools_settings_vsep = memnew(VSeparator);
  1995. tools_settings->add_child(tools_settings_vsep);
  1996. // Picker
  1997. picker_button = memnew(Button);
  1998. picker_button->set_theme_type_variation("FlatButton");
  1999. picker_button->set_toggle_mode(true);
  2000. picker_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/picker"));
  2001. 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;
  2002. picker_button->set_tooltip_text(vformat(TTR("Alternatively hold %s with other tools to pick tile."), find_keycode_name(key)));
  2003. picker_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  2004. tools_settings->add_child(picker_button);
  2005. viewport_shortcut_buttons.push_back(picker_button);
  2006. // Erase button.
  2007. erase_button = memnew(Button);
  2008. erase_button->set_theme_type_variation("FlatButton");
  2009. erase_button->set_toggle_mode(true);
  2010. erase_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/eraser"));
  2011. erase_button->set_tooltip_text(TTR("Alternatively use RMB to erase tiles."));
  2012. erase_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  2013. tools_settings->add_child(erase_button);
  2014. viewport_shortcut_buttons.push_back(erase_button);
  2015. // Transform toolbar.
  2016. transform_toolbar = memnew(HBoxContainer);
  2017. tools_settings->add_child(transform_toolbar);
  2018. transform_toolbar->add_child(memnew(VSeparator));
  2019. transform_button_rotate_left = memnew(Button);
  2020. transform_button_rotate_left->set_theme_type_variation("FlatButton");
  2021. transform_button_rotate_left->set_shortcut(ED_SHORTCUT("tiles_editor/rotate_tile_left", TTR("Rotate Tile Left"), Key::Z));
  2022. transform_toolbar->add_child(transform_button_rotate_left);
  2023. transform_button_rotate_left->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_ROTATE_LEFT));
  2024. viewport_shortcut_buttons.push_back(transform_button_rotate_left);
  2025. transform_button_rotate_right = memnew(Button);
  2026. transform_button_rotate_right->set_theme_type_variation("FlatButton");
  2027. transform_button_rotate_right->set_shortcut(ED_SHORTCUT("tiles_editor/rotate_tile_right", TTR("Rotate Tile Right"), Key::X));
  2028. transform_toolbar->add_child(transform_button_rotate_right);
  2029. transform_button_rotate_right->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_ROTATE_RIGHT));
  2030. viewport_shortcut_buttons.push_back(transform_button_rotate_right);
  2031. transform_button_flip_h = memnew(Button);
  2032. transform_button_flip_h->set_theme_type_variation("FlatButton");
  2033. transform_button_flip_h->set_shortcut(ED_SHORTCUT("tiles_editor/flip_tile_horizontal", TTR("Flip Tile Horizontally"), Key::C));
  2034. transform_toolbar->add_child(transform_button_flip_h);
  2035. transform_button_flip_h->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_FLIP_H));
  2036. viewport_shortcut_buttons.push_back(transform_button_flip_h);
  2037. transform_button_flip_v = memnew(Button);
  2038. transform_button_flip_v->set_theme_type_variation("FlatButton");
  2039. transform_button_flip_v->set_shortcut(ED_SHORTCUT("tiles_editor/flip_tile_vertical", TTR("Flip Tile Vertically"), Key::V));
  2040. transform_toolbar->add_child(transform_button_flip_v);
  2041. transform_button_flip_v->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_apply_transform).bind(TRANSFORM_FLIP_V));
  2042. viewport_shortcut_buttons.push_back(transform_button_flip_v);
  2043. // Separator 2.
  2044. tools_settings_vsep_2 = memnew(VSeparator);
  2045. tools_settings->add_child(tools_settings_vsep_2);
  2046. // Continuous checkbox.
  2047. bucket_contiguous_checkbox = memnew(CheckBox);
  2048. bucket_contiguous_checkbox->set_flat(true);
  2049. bucket_contiguous_checkbox->set_text(TTR("Contiguous"));
  2050. bucket_contiguous_checkbox->set_pressed(true);
  2051. tools_settings->add_child(bucket_contiguous_checkbox);
  2052. // Random tile checkbox.
  2053. random_tile_toggle = memnew(Button);
  2054. random_tile_toggle->set_theme_type_variation("FlatButton");
  2055. random_tile_toggle->set_toggle_mode(true);
  2056. random_tile_toggle->set_tooltip_text(TTR("Place Random Tile"));
  2057. random_tile_toggle->connect(SceneStringName(toggled), callable_mp(this, &TileMapLayerEditorTilesPlugin::_on_random_tile_checkbox_toggled));
  2058. tools_settings->add_child(random_tile_toggle);
  2059. // Random tile scattering.
  2060. scatter_controls_container = memnew(HBoxContainer);
  2061. scatter_label = memnew(Label);
  2062. scatter_label->set_tooltip_text(TTR("Modifies the chance of painting nothing instead of a randomly selected tile."));
  2063. scatter_label->set_text(TTR("Scattering:"));
  2064. scatter_controls_container->add_child(scatter_label);
  2065. scatter_spinbox = memnew(SpinBox);
  2066. scatter_spinbox->set_min(0.0);
  2067. scatter_spinbox->set_max(1000);
  2068. scatter_spinbox->set_step(0.001);
  2069. scatter_spinbox->set_tooltip_text(TTR("Modifies the chance of painting nothing instead of a randomly selected tile."));
  2070. scatter_spinbox->get_line_edit()->add_theme_constant_override("minimum_character_width", 4);
  2071. scatter_spinbox->connect(SceneStringName(value_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_on_scattering_spinbox_changed));
  2072. scatter_controls_container->add_child(scatter_spinbox);
  2073. tools_settings->add_child(scatter_controls_container);
  2074. _on_random_tile_checkbox_toggled(false);
  2075. // Default tool.
  2076. paint_tool_button->set_pressed(true);
  2077. _update_toolbar();
  2078. // --- Bottom panel tiles ---
  2079. tiles_bottom_panel = memnew(VBoxContainer);
  2080. // FIXME: This can trigger theme updates when the nodes that we want to update are not yet available.
  2081. // The toolbar should be extracted to a dedicated control and theme updates should be handled through
  2082. // the notification.
  2083. tiles_bottom_panel->connect(SceneStringName(theme_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_theme));
  2084. tiles_bottom_panel->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_stop_dragging));
  2085. tiles_bottom_panel->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tab_changed));
  2086. tiles_bottom_panel->set_name(TTR("Tiles"));
  2087. missing_source_label = memnew(Label);
  2088. missing_source_label->set_text(TTR("This TileMap's TileSet has no Tile Source configured. Go to the TileSet bottom panel to add one."));
  2089. missing_source_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2090. missing_source_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2091. missing_source_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2092. missing_source_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  2093. missing_source_label->hide();
  2094. tiles_bottom_panel->add_child(missing_source_label);
  2095. atlas_sources_split_container = memnew(HSplitContainer);
  2096. atlas_sources_split_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2097. atlas_sources_split_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2098. tiles_bottom_panel->add_child(atlas_sources_split_container);
  2099. VBoxContainer *split_container_left_side = memnew(VBoxContainer);
  2100. split_container_left_side->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2101. split_container_left_side->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2102. split_container_left_side->set_stretch_ratio(0.25);
  2103. split_container_left_side->set_custom_minimum_size(Size2(70, 0) * EDSCALE);
  2104. atlas_sources_split_container->add_child(split_container_left_side);
  2105. HBoxContainer *sources_bottom_actions = memnew(HBoxContainer);
  2106. sources_bottom_actions->set_alignment(HBoxContainer::ALIGNMENT_END);
  2107. source_sort_button = memnew(MenuButton);
  2108. source_sort_button->set_flat(false);
  2109. source_sort_button->set_theme_type_variation("FlatMenuButton");
  2110. source_sort_button->set_tooltip_text(TTR("Sort sources"));
  2111. PopupMenu *p = source_sort_button->get_popup();
  2112. p->connect(SceneStringName(id_pressed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_set_source_sort));
  2113. p->add_radio_check_item(TTR("Sort by ID (Ascending)"), TilesEditorUtils::SOURCE_SORT_ID);
  2114. p->add_radio_check_item(TTR("Sort by ID (Descending)"), TilesEditorUtils::SOURCE_SORT_ID_REVERSE);
  2115. p->add_radio_check_item(TTR("Sort by Name (Ascending)"), TilesEditorUtils::SOURCE_SORT_NAME);
  2116. p->add_radio_check_item(TTR("Sort by Name (Descending)"), TilesEditorUtils::SOURCE_SORT_NAME_REVERSE);
  2117. p->set_item_checked(TilesEditorUtils::SOURCE_SORT_ID, true);
  2118. sources_bottom_actions->add_child(source_sort_button);
  2119. sources_list = memnew(ItemList);
  2120. sources_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  2121. sources_list->set_fixed_icon_size(Size2(60, 60) * EDSCALE);
  2122. sources_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2123. sources_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2124. sources_list->set_stretch_ratio(0.25);
  2125. sources_list->set_custom_minimum_size(Size2(70, 0) * EDSCALE);
  2126. sources_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  2127. sources_list->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_source_display).unbind(1));
  2128. sources_list->connect(SceneStringName(item_selected), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_sources_lists_current));
  2129. sources_list->connect("item_activated", callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::display_tile_set_editor_panel).unbind(1));
  2130. sources_list->connect(SceneStringName(visibility_changed), callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::synchronize_sources_list).bind(sources_list, source_sort_button));
  2131. sources_list->add_user_signal(MethodInfo("sort_request"));
  2132. sources_list->connect("sort_request", callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_tile_set_sources_list));
  2133. split_container_left_side->add_child(sources_list);
  2134. split_container_left_side->add_child(sources_bottom_actions);
  2135. // Tile atlas source.
  2136. tile_atlas_view = memnew(TileAtlasView);
  2137. tile_atlas_view->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2138. tile_atlas_view->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2139. tile_atlas_view->set_texture_grid_visible(false);
  2140. tile_atlas_view->set_tile_shape_grid_visible(false);
  2141. tile_atlas_view->connect("transform_changed", callable_mp(TilesEditorUtils::get_singleton(), &TilesEditorUtils::set_atlas_view_transform));
  2142. atlas_sources_split_container->add_child(tile_atlas_view);
  2143. tile_atlas_control = memnew(Control);
  2144. tile_atlas_control->connect(SceneStringName(draw), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_atlas_control_draw));
  2145. tile_atlas_control->connect(SceneStringName(mouse_exited), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_atlas_control_mouse_exited));
  2146. tile_atlas_control->connect(SceneStringName(gui_input), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_atlas_control_gui_input));
  2147. tile_atlas_view->add_control_over_atlas_tiles(tile_atlas_control);
  2148. alternative_tiles_control = memnew(Control);
  2149. alternative_tiles_control->connect(SceneStringName(draw), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_alternatives_control_draw));
  2150. alternative_tiles_control->connect(SceneStringName(mouse_exited), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_alternatives_control_mouse_exited));
  2151. alternative_tiles_control->connect(SceneStringName(gui_input), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tile_alternatives_control_gui_input));
  2152. tile_atlas_view->add_control_over_alternative_tiles(alternative_tiles_control);
  2153. // Scenes collection source.
  2154. scene_tiles_list = memnew(ItemList);
  2155. scene_tiles_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  2156. scene_tiles_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2157. scene_tiles_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2158. scene_tiles_list->set_select_mode(ItemList::SELECT_MULTI);
  2159. scene_tiles_list->connect("multi_selected", callable_mp(this, &TileMapLayerEditorTilesPlugin::_scenes_list_multi_selected));
  2160. scene_tiles_list->connect("empty_clicked", callable_mp(this, &TileMapLayerEditorTilesPlugin::_scenes_list_lmb_empty_clicked));
  2161. scene_tiles_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  2162. atlas_sources_split_container->add_child(scene_tiles_list);
  2163. // Invalid source label.
  2164. invalid_source_label = memnew(Label);
  2165. invalid_source_label->set_text(TTR("Invalid source selected."));
  2166. invalid_source_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2167. invalid_source_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2168. invalid_source_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2169. invalid_source_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  2170. invalid_source_label->hide();
  2171. atlas_sources_split_container->add_child(invalid_source_label);
  2172. // --- Bottom panel patterns ---
  2173. patterns_bottom_panel = memnew(VBoxContainer);
  2174. patterns_bottom_panel->set_name(TTR("Patterns"));
  2175. patterns_bottom_panel->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditorTilesPlugin::_tab_changed));
  2176. int thumbnail_size = 64;
  2177. patterns_item_list = memnew(ItemList);
  2178. patterns_item_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  2179. patterns_item_list->set_max_columns(0);
  2180. patterns_item_list->set_icon_mode(ItemList::ICON_MODE_TOP);
  2181. patterns_item_list->set_fixed_column_width(thumbnail_size * 3 / 2);
  2182. patterns_item_list->set_max_text_lines(2);
  2183. patterns_item_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size));
  2184. patterns_item_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2185. patterns_item_list->connect(SceneStringName(gui_input), callable_mp(this, &TileMapLayerEditorTilesPlugin::_patterns_item_list_gui_input));
  2186. patterns_item_list->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_pattern_selection).unbind(1));
  2187. patterns_item_list->connect("item_activated", callable_mp(this, &TileMapLayerEditorTilesPlugin::_update_selection_pattern_from_tileset_pattern_selection).unbind(1));
  2188. patterns_item_list->connect("empty_clicked", callable_mp(this, &TileMapLayerEditorTilesPlugin::patterns_item_list_empty_clicked));
  2189. patterns_bottom_panel->add_child(patterns_item_list);
  2190. patterns_help_label = memnew(Label);
  2191. patterns_help_label->set_text(TTR("Drag and drop or paste a TileMap selection here to store a pattern."));
  2192. patterns_help_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2193. patterns_help_label->set_anchors_and_offsets_preset(Control::PRESET_CENTER);
  2194. patterns_item_list->add_child(patterns_help_label);
  2195. // Update.
  2196. _update_source_display();
  2197. }
  2198. TileMapLayerEditorTilesPlugin::~TileMapLayerEditorTilesPlugin() {
  2199. }
  2200. void TileMapLayerEditorTerrainsPlugin::tile_set_changed() {
  2201. _update_terrains_cache();
  2202. _update_terrains_tree();
  2203. _update_tiles_list();
  2204. }
  2205. void TileMapLayerEditorTerrainsPlugin::_update_toolbar() {
  2206. // Hide all settings.
  2207. for (int i = 0; i < tools_settings->get_child_count(); i++) {
  2208. Object::cast_to<CanvasItem>(tools_settings->get_child(i))->hide();
  2209. }
  2210. // Show only the correct settings.
  2211. if (tool_buttons_group->get_pressed_button() != bucket_tool_button) {
  2212. tools_settings_vsep->show();
  2213. picker_button->show();
  2214. erase_button->show();
  2215. } else {
  2216. tools_settings_vsep->show();
  2217. picker_button->show();
  2218. erase_button->show();
  2219. tools_settings_vsep_2->show();
  2220. bucket_contiguous_checkbox->show();
  2221. }
  2222. }
  2223. Vector<TileMapLayerSubEditorPlugin::TabData> TileMapLayerEditorTerrainsPlugin::get_tabs() const {
  2224. Vector<TileMapLayerSubEditorPlugin::TabData> tabs;
  2225. tabs.push_back({ toolbar, main_vbox_container });
  2226. return tabs;
  2227. }
  2228. 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 {
  2229. TileMapLayer *edited_layer = _get_edited_layer();
  2230. if (!edited_layer) {
  2231. return HashMap<Vector2i, TileMapCell>();
  2232. }
  2233. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2234. if (tile_set.is_null()) {
  2235. return HashMap<Vector2i, TileMapCell>();
  2236. }
  2237. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output;
  2238. if (p_connect) {
  2239. terrain_fill_output = edited_layer->terrain_fill_connect(p_to_paint, p_terrain_set, p_terrain, false);
  2240. } else {
  2241. terrain_fill_output = edited_layer->terrain_fill_path(p_to_paint, p_terrain_set, p_terrain, false);
  2242. }
  2243. // Make the painted path a set for faster lookups
  2244. HashSet<Vector2i> painted_set;
  2245. for (Vector2i coords : p_to_paint) {
  2246. painted_set.insert(coords);
  2247. }
  2248. HashMap<Vector2i, TileMapCell> output;
  2249. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  2250. if (painted_set.has(kv.key)) {
  2251. // Paint a random tile with the correct terrain for the painted path.
  2252. output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2253. } else {
  2254. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2255. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2256. TileMapCell cell = edited_layer->get_cell(kv.key);
  2257. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2258. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2259. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2260. if (atlas_source) {
  2261. // Get tile data.
  2262. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2263. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2264. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2265. }
  2266. }
  2267. }
  2268. if (in_map_terrain_pattern != kv.value) {
  2269. output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2270. }
  2271. }
  2272. }
  2273. return output;
  2274. }
  2275. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_terrain_pattern(const Vector<Vector2i> &p_to_paint, int p_terrain_set, TileSet::TerrainsPattern p_terrains_pattern) const {
  2276. TileMapLayer *edited_layer = _get_edited_layer();
  2277. if (!edited_layer) {
  2278. return HashMap<Vector2i, TileMapCell>();
  2279. }
  2280. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2281. if (tile_set.is_null()) {
  2282. return HashMap<Vector2i, TileMapCell>();
  2283. }
  2284. HashMap<Vector2i, TileSet::TerrainsPattern> terrain_fill_output = edited_layer->terrain_fill_pattern(p_to_paint, p_terrain_set, p_terrains_pattern, false);
  2285. // Make the painted path a set for faster lookups
  2286. HashSet<Vector2i> painted_set;
  2287. for (Vector2i coords : p_to_paint) {
  2288. painted_set.insert(coords);
  2289. }
  2290. HashMap<Vector2i, TileMapCell> output;
  2291. for (const KeyValue<Vector2i, TileSet::TerrainsPattern> &kv : terrain_fill_output) {
  2292. if (painted_set.has(kv.key)) {
  2293. // Paint a random tile with the correct terrain for the painted path.
  2294. output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2295. } else {
  2296. // Avoids updating the painted path from the output if the new pattern is the same as before.
  2297. TileSet::TerrainsPattern in_map_terrain_pattern = TileSet::TerrainsPattern(*tile_set, p_terrain_set);
  2298. TileMapCell cell = edited_layer->get_cell(kv.key);
  2299. if (cell.source_id != TileSet::INVALID_SOURCE) {
  2300. TileSetSource *source = *tile_set->get_source(cell.source_id);
  2301. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  2302. if (atlas_source) {
  2303. // Get tile data.
  2304. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2305. if (tile_data && tile_data->get_terrain_set() == p_terrain_set) {
  2306. in_map_terrain_pattern = tile_data->get_terrains_pattern();
  2307. }
  2308. }
  2309. }
  2310. if (in_map_terrain_pattern != kv.value) {
  2311. output[kv.key] = tile_set->get_random_tile_from_terrains_pattern(p_terrain_set, kv.value);
  2312. }
  2313. }
  2314. }
  2315. return output;
  2316. }
  2317. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_line(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase) {
  2318. TileMapLayer *edited_layer = _get_edited_layer();
  2319. if (!edited_layer) {
  2320. return HashMap<Vector2i, TileMapCell>();
  2321. }
  2322. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2323. if (tile_set.is_null()) {
  2324. return HashMap<Vector2i, TileMapCell>();
  2325. }
  2326. if (p_erase) {
  2327. 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));
  2328. } else {
  2329. if (selected_type == SELECTED_TYPE_CONNECT) {
  2330. return _draw_terrain_path_or_connect(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, selected_terrain, true);
  2331. } else if (selected_type == SELECTED_TYPE_PATH) {
  2332. return _draw_terrain_path_or_connect(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, selected_terrain, false);
  2333. } else { // SELECTED_TYPE_PATTERN
  2334. return _draw_terrain_pattern(TileMapLayerEditor::get_line(edited_layer, p_start_cell, p_end_cell), selected_terrain_set, selected_terrains_pattern);
  2335. }
  2336. }
  2337. }
  2338. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_rect(Vector2i p_start_cell, Vector2i p_end_cell, bool p_erase) {
  2339. TileMapLayer *edited_layer = _get_edited_layer();
  2340. if (!edited_layer) {
  2341. return HashMap<Vector2i, TileMapCell>();
  2342. }
  2343. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2344. if (tile_set.is_null()) {
  2345. return HashMap<Vector2i, TileMapCell>();
  2346. }
  2347. Rect2i rect;
  2348. rect.set_position(p_start_cell);
  2349. rect.set_end(p_end_cell);
  2350. rect = rect.abs();
  2351. Vector<Vector2i> to_draw;
  2352. for (int x = rect.position.x; x <= rect.get_end().x; x++) {
  2353. for (int y = rect.position.y; y <= rect.get_end().y; y++) {
  2354. to_draw.append(Vector2i(x, y));
  2355. }
  2356. }
  2357. if (p_erase) {
  2358. return _draw_terrain_pattern(to_draw, selected_terrain_set, TileSet::TerrainsPattern(*tile_set, selected_terrain_set));
  2359. } else {
  2360. if (selected_type == SELECTED_TYPE_CONNECT || selected_type == SELECTED_TYPE_PATH) {
  2361. return _draw_terrain_path_or_connect(to_draw, selected_terrain_set, selected_terrain, true);
  2362. } else { // SELECTED_TYPE_PATTERN
  2363. return _draw_terrain_pattern(to_draw, selected_terrain_set, selected_terrains_pattern);
  2364. }
  2365. }
  2366. }
  2367. RBSet<Vector2i> TileMapLayerEditorTerrainsPlugin::_get_cells_for_bucket_fill(Vector2i p_coords, bool p_contiguous) {
  2368. const TileMapLayer *edited_layer = _get_edited_layer();
  2369. if (!edited_layer) {
  2370. return RBSet<Vector2i>();
  2371. }
  2372. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2373. if (tile_set.is_null()) {
  2374. return RBSet<Vector2i>();
  2375. }
  2376. TileMapCell source_cell = edited_layer->get_cell(p_coords);
  2377. TileSet::TerrainsPattern source_pattern(*tile_set, selected_terrain_set);
  2378. if (source_cell.source_id != TileSet::INVALID_SOURCE) {
  2379. TileData *tile_data = nullptr;
  2380. Ref<TileSetSource> source = tile_set->get_source(source_cell.source_id);
  2381. Ref<TileSetAtlasSource> atlas_source = source;
  2382. if (atlas_source.is_valid()) {
  2383. tile_data = atlas_source->get_tile_data(source_cell.get_atlas_coords(), source_cell.alternative_tile);
  2384. }
  2385. if (!tile_data) {
  2386. return RBSet<Vector2i>();
  2387. }
  2388. source_pattern = tile_data->get_terrains_pattern();
  2389. }
  2390. // If we are filling empty tiles, compute the tilemap boundaries.
  2391. Rect2i boundaries;
  2392. if (source_cell.source_id == TileSet::INVALID_SOURCE) {
  2393. boundaries = edited_layer->get_used_rect();
  2394. }
  2395. RBSet<Vector2i> output;
  2396. if (p_contiguous) {
  2397. // Replace continuous tiles like the source.
  2398. RBSet<Vector2i> already_checked;
  2399. List<Vector2i> to_check;
  2400. to_check.push_back(p_coords);
  2401. while (!to_check.is_empty()) {
  2402. Vector2i coords = to_check.back()->get();
  2403. to_check.pop_back();
  2404. if (!already_checked.has(coords)) {
  2405. // Get the candidate cell pattern.
  2406. TileSet::TerrainsPattern candidate_pattern(*tile_set, selected_terrain_set);
  2407. if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  2408. TileData *tile_data = nullptr;
  2409. Ref<TileSetSource> source = tile_set->get_source(edited_layer->get_cell_source_id(coords));
  2410. Ref<TileSetAtlasSource> atlas_source = source;
  2411. if (atlas_source.is_valid()) {
  2412. tile_data = atlas_source->get_tile_data(edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords));
  2413. }
  2414. if (tile_data) {
  2415. candidate_pattern = tile_data->get_terrains_pattern();
  2416. }
  2417. }
  2418. // Draw.
  2419. if (candidate_pattern == source_pattern && (!source_pattern.is_erase_pattern() || boundaries.has_point(coords))) {
  2420. output.insert(coords);
  2421. // Get surrounding tiles (handles different tile shapes).
  2422. TypedArray<Vector2i> around = tile_set->get_surrounding_cells(coords);
  2423. for (int i = 0; i < around.size(); i++) {
  2424. to_check.push_back(around[i]);
  2425. }
  2426. }
  2427. already_checked.insert(coords);
  2428. }
  2429. }
  2430. } else {
  2431. // Replace all tiles like the source.
  2432. TypedArray<Vector2i> to_check;
  2433. if (source_cell.source_id == TileSet::INVALID_SOURCE) {
  2434. Rect2i rect = edited_layer->get_used_rect();
  2435. if (!rect.has_area()) {
  2436. rect = Rect2i(p_coords, Vector2i(1, 1));
  2437. }
  2438. for (int x = boundaries.position.x; x < boundaries.get_end().x; x++) {
  2439. for (int y = boundaries.position.y; y < boundaries.get_end().y; y++) {
  2440. to_check.append(Vector2i(x, y));
  2441. }
  2442. }
  2443. } else {
  2444. to_check = edited_layer->get_used_cells();
  2445. }
  2446. for (int i = 0; i < to_check.size(); i++) {
  2447. Vector2i coords = to_check[i];
  2448. // Get the candidate cell pattern.
  2449. TileSet::TerrainsPattern candidate_pattern;
  2450. if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  2451. TileData *tile_data = nullptr;
  2452. Ref<TileSetSource> source = tile_set->get_source(edited_layer->get_cell_source_id(coords));
  2453. Ref<TileSetAtlasSource> atlas_source = source;
  2454. if (atlas_source.is_valid()) {
  2455. tile_data = atlas_source->get_tile_data(edited_layer->get_cell_atlas_coords(coords), edited_layer->get_cell_alternative_tile(coords));
  2456. }
  2457. if (tile_data) {
  2458. candidate_pattern = tile_data->get_terrains_pattern();
  2459. }
  2460. }
  2461. // Draw.
  2462. if (candidate_pattern == source_pattern && (!source_pattern.is_erase_pattern() || boundaries.has_point(coords))) {
  2463. output.insert(coords);
  2464. }
  2465. }
  2466. }
  2467. return output;
  2468. }
  2469. HashMap<Vector2i, TileMapCell> TileMapLayerEditorTerrainsPlugin::_draw_bucket_fill(Vector2i p_coords, bool p_contiguous, bool p_erase) {
  2470. const TileMapLayer *edited_layer = _get_edited_layer();
  2471. if (!edited_layer) {
  2472. return HashMap<Vector2i, TileMapCell>();
  2473. }
  2474. const Ref<TileSet> &tile_set = edited_layer->get_tile_set();
  2475. if (tile_set.is_null()) {
  2476. return HashMap<Vector2i, TileMapCell>();
  2477. }
  2478. RBSet<Vector2i> cells_to_draw = _get_cells_for_bucket_fill(p_coords, p_contiguous);
  2479. Vector<Vector2i> cells_to_draw_as_vector;
  2480. for (Vector2i cell : cells_to_draw) {
  2481. cells_to_draw_as_vector.append(cell);
  2482. }
  2483. if (p_erase) {
  2484. return _draw_terrain_pattern(cells_to_draw_as_vector, selected_terrain_set, TileSet::TerrainsPattern(*tile_set, selected_terrain_set));
  2485. } else {
  2486. if (selected_type == SELECTED_TYPE_CONNECT || selected_type == SELECTED_TYPE_PATH) {
  2487. return _draw_terrain_path_or_connect(cells_to_draw_as_vector, selected_terrain_set, selected_terrain, true);
  2488. } else { // SELECTED_TYPE_PATTERN
  2489. return _draw_terrain_pattern(cells_to_draw_as_vector, selected_terrain_set, selected_terrains_pattern);
  2490. }
  2491. }
  2492. }
  2493. void TileMapLayerEditorTerrainsPlugin::_stop_dragging() {
  2494. TileMapLayer *edited_layer = _get_edited_layer();
  2495. if (!edited_layer) {
  2496. return;
  2497. }
  2498. const Ref<TileSet> &tile_set = edited_layer->get_tile_set();
  2499. if (tile_set.is_null()) {
  2500. return;
  2501. }
  2502. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  2503. Vector2 mpos = xform.affine_inverse().xform(CanvasItemEditor::get_singleton()->get_viewport_control()->get_local_mouse_position());
  2504. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2505. switch (drag_type) {
  2506. case DRAG_TYPE_PICK: {
  2507. Vector2i coords = tile_set->local_to_map(mpos);
  2508. TileMapCell cell = edited_layer->get_cell(coords);
  2509. TileData *tile_data = nullptr;
  2510. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  2511. Ref<TileSetAtlasSource> atlas_source = source;
  2512. if (atlas_source.is_valid()) {
  2513. tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  2514. }
  2515. if (tile_data) {
  2516. TileSet::TerrainsPattern terrains_pattern = tile_data->get_terrains_pattern();
  2517. // Find the tree item for the right terrain set.
  2518. bool need_tree_item_switch = true;
  2519. TreeItem *tree_item = terrains_tree->get_selected();
  2520. int new_terrain_set = -1;
  2521. if (tree_item) {
  2522. Dictionary metadata_dict = tree_item->get_metadata(0);
  2523. if (metadata_dict.has("terrain_set") && metadata_dict.has("terrain_id")) {
  2524. int terrain_set = metadata_dict["terrain_set"];
  2525. int terrain_id = metadata_dict["terrain_id"];
  2526. if (per_terrain_terrains_patterns[terrain_set][terrain_id].has(terrains_pattern)) {
  2527. new_terrain_set = terrain_set;
  2528. need_tree_item_switch = false;
  2529. }
  2530. }
  2531. }
  2532. if (need_tree_item_switch) {
  2533. for (tree_item = terrains_tree->get_root()->get_first_child(); tree_item; tree_item = tree_item->get_next_visible()) {
  2534. Dictionary metadata_dict = tree_item->get_metadata(0);
  2535. if (metadata_dict.has("terrain_set") && metadata_dict.has("terrain_id")) {
  2536. int terrain_set = metadata_dict["terrain_set"];
  2537. int terrain_id = metadata_dict["terrain_id"];
  2538. if (per_terrain_terrains_patterns[terrain_set][terrain_id].has(terrains_pattern)) {
  2539. // Found
  2540. new_terrain_set = terrain_set;
  2541. tree_item->select(0);
  2542. _update_tiles_list();
  2543. break;
  2544. }
  2545. }
  2546. }
  2547. }
  2548. // Find the list item for the given tile.
  2549. if (tree_item) {
  2550. for (int i = 0; i < terrains_tile_list->get_item_count(); i++) {
  2551. Dictionary metadata_dict = terrains_tile_list->get_item_metadata(i);
  2552. if (int(metadata_dict["type"]) == SELECTED_TYPE_PATTERN) {
  2553. TileSet::TerrainsPattern in_meta_terrains_pattern(*tile_set, new_terrain_set);
  2554. in_meta_terrains_pattern.from_array(metadata_dict["terrains_pattern"]);
  2555. if (in_meta_terrains_pattern == terrains_pattern) {
  2556. terrains_tile_list->select(i);
  2557. break;
  2558. }
  2559. }
  2560. }
  2561. } else {
  2562. ERR_PRINT("Terrain tile not found.");
  2563. }
  2564. }
  2565. picker_button->set_pressed(false);
  2566. } break;
  2567. case DRAG_TYPE_PAINT: {
  2568. undo_redo->create_action(TTR("Paint terrain"));
  2569. for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) {
  2570. 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));
  2571. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2572. }
  2573. undo_redo->commit_action(false);
  2574. } break;
  2575. case DRAG_TYPE_LINE: {
  2576. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing);
  2577. undo_redo->create_action(TTR("Paint terrain"));
  2578. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2579. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  2580. continue;
  2581. }
  2582. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2583. 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));
  2584. }
  2585. undo_redo->commit_action();
  2586. } break;
  2587. case DRAG_TYPE_RECT: {
  2588. HashMap<Vector2i, TileMapCell> to_draw = _draw_rect(tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos), drag_erasing);
  2589. undo_redo->create_action(TTR("Paint terrain"));
  2590. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2591. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  2592. continue;
  2593. }
  2594. undo_redo->add_do_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2595. 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));
  2596. }
  2597. undo_redo->commit_action();
  2598. } break;
  2599. case DRAG_TYPE_BUCKET: {
  2600. undo_redo->create_action(TTR("Paint terrain"));
  2601. for (const KeyValue<Vector2i, TileMapCell> &E : drag_modified) {
  2602. 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));
  2603. undo_redo->add_undo_method(edited_layer, "set_cell", E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2604. }
  2605. undo_redo->commit_action(false);
  2606. } break;
  2607. default:
  2608. break;
  2609. }
  2610. drag_type = DRAG_TYPE_NONE;
  2611. }
  2612. void TileMapLayerEditorTerrainsPlugin::_mouse_exited_viewport() {
  2613. has_mouse = false;
  2614. CanvasItemEditor::get_singleton()->update_viewport();
  2615. }
  2616. void TileMapLayerEditorTerrainsPlugin::_update_selection() {
  2617. const TileMapLayer *edited_layer = _get_edited_layer();
  2618. if (!edited_layer) {
  2619. return;
  2620. }
  2621. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2622. if (tile_set.is_null()) {
  2623. return;
  2624. }
  2625. // Get the selected terrain.
  2626. selected_terrain_set = -1;
  2627. selected_terrains_pattern = TileSet::TerrainsPattern();
  2628. TreeItem *selected_tree_item = terrains_tree->get_selected();
  2629. if (selected_tree_item && selected_tree_item->get_metadata(0)) {
  2630. Dictionary metadata_dict = selected_tree_item->get_metadata(0);
  2631. // Selected terrain
  2632. selected_terrain_set = metadata_dict["terrain_set"];
  2633. selected_terrain = metadata_dict["terrain_id"];
  2634. // Selected mode/terrain pattern
  2635. if (erase_button->is_pressed()) {
  2636. selected_type = SELECTED_TYPE_PATTERN;
  2637. selected_terrains_pattern = TileSet::TerrainsPattern(*tile_set, selected_terrain_set);
  2638. } else if (terrains_tile_list->is_anything_selected()) {
  2639. metadata_dict = terrains_tile_list->get_item_metadata(terrains_tile_list->get_selected_items()[0]);
  2640. if (int(metadata_dict["type"]) == SELECTED_TYPE_CONNECT) {
  2641. selected_type = SELECTED_TYPE_CONNECT;
  2642. } else if (int(metadata_dict["type"]) == SELECTED_TYPE_PATH) {
  2643. selected_type = SELECTED_TYPE_PATH;
  2644. } else if (int(metadata_dict["type"]) == SELECTED_TYPE_PATTERN) {
  2645. selected_type = SELECTED_TYPE_PATTERN;
  2646. selected_terrains_pattern = TileSet::TerrainsPattern(*tile_set, selected_terrain_set);
  2647. selected_terrains_pattern.from_array(metadata_dict["terrains_pattern"]);
  2648. } else {
  2649. ERR_FAIL();
  2650. }
  2651. }
  2652. }
  2653. }
  2654. bool TileMapLayerEditorTerrainsPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  2655. if (!main_vbox_container->is_visible_in_tree()) {
  2656. // If the bottom editor is not visible, we ignore inputs.
  2657. return false;
  2658. }
  2659. if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
  2660. return false;
  2661. }
  2662. TileMapLayer *edited_layer = _get_edited_layer();
  2663. if (!edited_layer) {
  2664. return false;
  2665. }
  2666. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2667. if (tile_set.is_null()) {
  2668. return false;
  2669. }
  2670. _update_selection();
  2671. Ref<InputEventKey> k = p_event;
  2672. if (k.is_valid() && k->is_pressed() && !k->is_echo()) {
  2673. for (BaseButton *b : viewport_shortcut_buttons) {
  2674. if (b->get_shortcut().is_valid() && b->get_shortcut()->matches_event(p_event)) {
  2675. b->set_pressed(b->get_button_group().is_valid() || !b->is_pressed());
  2676. return true;
  2677. }
  2678. }
  2679. }
  2680. Ref<InputEventMouseMotion> mm = p_event;
  2681. if (mm.is_valid()) {
  2682. has_mouse = true;
  2683. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  2684. Vector2 mpos = xform.affine_inverse().xform(mm->get_position());
  2685. switch (drag_type) {
  2686. case DRAG_TYPE_PAINT: {
  2687. if (selected_terrain_set >= 0) {
  2688. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos), drag_erasing);
  2689. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2690. if (!drag_modified.has(E.key)) {
  2691. drag_modified[E.key] = edited_layer->get_cell(E.key);
  2692. }
  2693. edited_layer->set_cell(E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2694. }
  2695. }
  2696. } break;
  2697. default:
  2698. break;
  2699. }
  2700. drag_last_mouse_pos = mpos;
  2701. CanvasItemEditor::get_singleton()->update_viewport();
  2702. return true;
  2703. }
  2704. Ref<InputEventMouseButton> mb = p_event;
  2705. if (mb.is_valid()) {
  2706. has_mouse = true;
  2707. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  2708. Vector2 mpos = xform.affine_inverse().xform(mb->get_position());
  2709. if (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT) {
  2710. if (mb->is_pressed()) {
  2711. // Pressed
  2712. if (erase_button->is_pressed() || mb->get_button_index() == MouseButton::RIGHT) {
  2713. drag_erasing = true;
  2714. }
  2715. if (picker_button->is_pressed()) {
  2716. drag_type = DRAG_TYPE_PICK;
  2717. } else {
  2718. // Paint otherwise.
  2719. 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)) {
  2720. if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) {
  2721. return true;
  2722. }
  2723. drag_type = DRAG_TYPE_PAINT;
  2724. drag_start_mouse_pos = mpos;
  2725. drag_modified.clear();
  2726. Vector2i cell = tile_set->local_to_map(mpos);
  2727. HashMap<Vector2i, TileMapCell> to_draw = _draw_line(cell, cell, drag_erasing);
  2728. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2729. drag_modified[E.key] = edited_layer->get_cell(E.key);
  2730. edited_layer->set_cell(E.key, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2731. }
  2732. } 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))) {
  2733. if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) {
  2734. return true;
  2735. }
  2736. drag_type = DRAG_TYPE_LINE;
  2737. drag_start_mouse_pos = mpos;
  2738. drag_modified.clear();
  2739. } 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))) {
  2740. if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) {
  2741. return true;
  2742. }
  2743. drag_type = DRAG_TYPE_RECT;
  2744. drag_start_mouse_pos = mpos;
  2745. drag_modified.clear();
  2746. } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button) {
  2747. if (selected_terrain_set < 0 || selected_terrain < 0 || (selected_type == SELECTED_TYPE_PATTERN && !selected_terrains_pattern.is_valid())) {
  2748. return true;
  2749. }
  2750. drag_type = DRAG_TYPE_BUCKET;
  2751. drag_start_mouse_pos = mpos;
  2752. drag_modified.clear();
  2753. Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_last_mouse_pos), tile_set->local_to_map(mpos));
  2754. for (int i = 0; i < line.size(); i++) {
  2755. if (!drag_modified.has(line[i])) {
  2756. HashMap<Vector2i, TileMapCell> to_draw = _draw_bucket_fill(line[i], bucket_contiguous_checkbox->is_pressed(), drag_erasing);
  2757. for (const KeyValue<Vector2i, TileMapCell> &E : to_draw) {
  2758. if (!drag_erasing && E.value.source_id == TileSet::INVALID_SOURCE) {
  2759. continue;
  2760. }
  2761. Vector2i coords = E.key;
  2762. if (!drag_modified.has(coords)) {
  2763. drag_modified.insert(coords, edited_layer->get_cell(coords));
  2764. }
  2765. edited_layer->set_cell(coords, E.value.source_id, E.value.get_atlas_coords(), E.value.alternative_tile);
  2766. }
  2767. }
  2768. }
  2769. }
  2770. }
  2771. } else {
  2772. // Released
  2773. _stop_dragging();
  2774. drag_erasing = false;
  2775. }
  2776. CanvasItemEditor::get_singleton()->update_viewport();
  2777. return true;
  2778. }
  2779. drag_last_mouse_pos = mpos;
  2780. }
  2781. return false;
  2782. }
  2783. void TileMapLayerEditorTerrainsPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) {
  2784. const TileMapLayer *edited_layer = _get_edited_layer();
  2785. if (!edited_layer) {
  2786. return;
  2787. }
  2788. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2789. if (tile_set.is_null()) {
  2790. return;
  2791. }
  2792. if (!edited_layer->is_visible_in_tree()) {
  2793. return;
  2794. }
  2795. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  2796. Vector2 mpos = edited_layer->get_local_mouse_position();
  2797. Vector2i tile_shape_size = tile_set->get_tile_size();
  2798. // Handle the preview of the tiles to be placed.
  2799. if (main_vbox_container->is_visible_in_tree() && has_mouse) { // Only if the tilemap editor is opened and the viewport is hovered.
  2800. RBSet<Vector2i> preview;
  2801. Rect2i drawn_grid_rect;
  2802. if (drag_type == DRAG_TYPE_PICK) {
  2803. // Draw the area being picked.
  2804. Vector2i coords = tile_set->local_to_map(mpos);
  2805. if (edited_layer->get_cell_source_id(coords) != TileSet::INVALID_SOURCE) {
  2806. Transform2D tile_xform;
  2807. tile_xform.set_origin(tile_set->map_to_local(coords));
  2808. tile_xform.set_scale(tile_shape_size);
  2809. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0), false);
  2810. }
  2811. } 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))) {
  2812. bool expand_grid = false;
  2813. if (tool_buttons_group->get_pressed_button() == paint_tool_button && drag_type == DRAG_TYPE_NONE) {
  2814. // Preview for a single tile.
  2815. preview.insert(tile_set->local_to_map(mpos));
  2816. expand_grid = true;
  2817. } else if (tool_buttons_group->get_pressed_button() == line_tool_button || drag_type == DRAG_TYPE_LINE) {
  2818. if (drag_type == DRAG_TYPE_NONE) {
  2819. // Preview for a single tile.
  2820. preview.insert(tile_set->local_to_map(mpos));
  2821. } else if (drag_type == DRAG_TYPE_LINE) {
  2822. // Preview for a line.
  2823. Vector<Vector2i> line = TileMapLayerEditor::get_line(edited_layer, tile_set->local_to_map(drag_start_mouse_pos), tile_set->local_to_map(mpos));
  2824. for (int i = 0; i < line.size(); i++) {
  2825. preview.insert(line[i]);
  2826. }
  2827. expand_grid = true;
  2828. }
  2829. } else if (drag_type == DRAG_TYPE_RECT) {
  2830. // Preview for a rect.
  2831. Rect2i rect;
  2832. rect.set_position(tile_set->local_to_map(drag_start_mouse_pos));
  2833. rect.set_end(tile_set->local_to_map(mpos));
  2834. rect = rect.abs();
  2835. HashMap<Vector2i, TileSet::TerrainsPattern> to_draw;
  2836. for (int x = rect.position.x; x <= rect.get_end().x; x++) {
  2837. for (int y = rect.position.y; y <= rect.get_end().y; y++) {
  2838. preview.insert(Vector2i(x, y));
  2839. }
  2840. }
  2841. expand_grid = true;
  2842. } else if (tool_buttons_group->get_pressed_button() == bucket_tool_button && drag_type == DRAG_TYPE_NONE) {
  2843. // Preview for a fill.
  2844. preview = _get_cells_for_bucket_fill(tile_set->local_to_map(mpos), bucket_contiguous_checkbox->is_pressed());
  2845. }
  2846. // Expand the grid if needed
  2847. if (expand_grid && !preview.is_empty()) {
  2848. drawn_grid_rect = Rect2i(preview.front()->get(), Vector2i(1, 1));
  2849. for (const Vector2i &E : preview) {
  2850. drawn_grid_rect.expand_to(E);
  2851. }
  2852. }
  2853. }
  2854. if (!preview.is_empty()) {
  2855. const int fading = 5;
  2856. // Draw the lines of the grid behind the preview.
  2857. bool display_grid = EDITOR_GET("editors/tiles_editor/display_grid");
  2858. if (display_grid) {
  2859. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  2860. if (drawn_grid_rect.size.x > 0 && drawn_grid_rect.size.y > 0) {
  2861. drawn_grid_rect = drawn_grid_rect.grow(fading);
  2862. for (int x = drawn_grid_rect.position.x; x < (drawn_grid_rect.position.x + drawn_grid_rect.size.x); x++) {
  2863. for (int y = drawn_grid_rect.position.y; y < (drawn_grid_rect.position.y + drawn_grid_rect.size.y); y++) {
  2864. Vector2i pos_in_rect = Vector2i(x, y) - drawn_grid_rect.position;
  2865. // Fade out the border of the grid.
  2866. float left_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.x), 0.0f, 1.0f);
  2867. 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);
  2868. float top_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.y), 0.0f, 1.0f);
  2869. 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);
  2870. float opacity = CLAMP(MIN(left_opacity, MIN(right_opacity, MIN(top_opacity, bottom_opacity))) + 0.1, 0.0f, 1.0f);
  2871. Transform2D tile_xform;
  2872. tile_xform.set_origin(tile_set->map_to_local(Vector2(x, y)));
  2873. tile_xform.set_scale(tile_shape_size);
  2874. Color color = grid_color;
  2875. color.a = color.a * opacity;
  2876. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, color, false);
  2877. }
  2878. }
  2879. }
  2880. }
  2881. // Draw the preview.
  2882. for (const Vector2i &E : preview) {
  2883. Transform2D tile_xform;
  2884. tile_xform.set_origin(tile_set->map_to_local(E));
  2885. tile_xform.set_scale(tile_set->get_tile_size());
  2886. if (drag_erasing || erase_button->is_pressed()) {
  2887. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(0.0, 0.0, 0.0, 0.5), true);
  2888. } else {
  2889. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, Color(1.0, 1.0, 1.0, 0.5), true);
  2890. }
  2891. }
  2892. }
  2893. }
  2894. }
  2895. void TileMapLayerEditorTerrainsPlugin::_update_terrains_cache() {
  2896. const TileMapLayer *edited_layer = _get_edited_layer();
  2897. if (!edited_layer) {
  2898. return;
  2899. }
  2900. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2901. if (tile_set.is_null()) {
  2902. return;
  2903. }
  2904. // Organizes tiles into structures.
  2905. per_terrain_terrains_patterns.resize(tile_set->get_terrain_sets_count());
  2906. for (int i = 0; i < tile_set->get_terrain_sets_count(); i++) {
  2907. per_terrain_terrains_patterns[i].resize(tile_set->get_terrains_count(i));
  2908. for (RBSet<TileSet::TerrainsPattern> &pattern : per_terrain_terrains_patterns[i]) {
  2909. pattern.clear();
  2910. }
  2911. }
  2912. for (int source_index = 0; source_index < tile_set->get_source_count(); source_index++) {
  2913. int source_id = tile_set->get_source_id(source_index);
  2914. Ref<TileSetSource> source = tile_set->get_source(source_id);
  2915. Ref<TileSetAtlasSource> atlas_source = source;
  2916. if (atlas_source.is_valid()) {
  2917. for (int tile_index = 0; tile_index < source->get_tiles_count(); tile_index++) {
  2918. Vector2i tile_id = source->get_tile_id(tile_index);
  2919. for (int alternative_index = 0; alternative_index < source->get_alternative_tiles_count(tile_id); alternative_index++) {
  2920. int alternative_id = source->get_alternative_tile_id(tile_id, alternative_index);
  2921. TileData *tile_data = atlas_source->get_tile_data(tile_id, alternative_id);
  2922. int terrain_set = tile_data->get_terrain_set();
  2923. if (terrain_set >= 0) {
  2924. ERR_FAIL_INDEX(terrain_set, (int)per_terrain_terrains_patterns.size());
  2925. TileMapCell cell;
  2926. cell.source_id = source_id;
  2927. cell.set_atlas_coords(tile_id);
  2928. cell.alternative_tile = alternative_id;
  2929. TileSet::TerrainsPattern terrains_pattern = tile_data->get_terrains_pattern();
  2930. // Terrain center bit
  2931. int terrain = terrains_pattern.get_terrain();
  2932. if (terrain >= 0 && terrain < (int)per_terrain_terrains_patterns[terrain_set].size()) {
  2933. per_terrain_terrains_patterns[terrain_set][terrain].insert(terrains_pattern);
  2934. }
  2935. // Terrain bits.
  2936. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  2937. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  2938. if (tile_set->is_valid_terrain_peering_bit(terrain_set, bit)) {
  2939. terrain = terrains_pattern.get_terrain_peering_bit(bit);
  2940. if (terrain >= 0 && terrain < (int)per_terrain_terrains_patterns[terrain_set].size()) {
  2941. per_terrain_terrains_patterns[terrain_set][terrain].insert(terrains_pattern);
  2942. }
  2943. }
  2944. }
  2945. }
  2946. }
  2947. }
  2948. }
  2949. }
  2950. }
  2951. void TileMapLayerEditorTerrainsPlugin::_update_terrains_tree() {
  2952. terrains_tree->clear();
  2953. terrains_tree->create_item();
  2954. const TileMapLayer *edited_layer = _get_edited_layer();
  2955. ERR_FAIL_NULL(edited_layer);
  2956. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2957. if (tile_set.is_null()) {
  2958. return;
  2959. }
  2960. // Fill in the terrain list.
  2961. Vector<Vector<Ref<Texture2D>>> icons = tile_set->generate_terrains_icons(Size2(16, 16) * EDSCALE);
  2962. for (int terrain_set_index = 0; terrain_set_index < tile_set->get_terrain_sets_count(); terrain_set_index++) {
  2963. // Add an item for the terrain set.
  2964. TreeItem *terrain_set_tree_item = terrains_tree->create_item();
  2965. String matches;
  2966. if (tile_set->get_terrain_set_mode(terrain_set_index) == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
  2967. terrain_set_tree_item->set_icon(0, main_vbox_container->get_editor_theme_icon(SNAME("TerrainMatchCornersAndSides")));
  2968. matches = String(TTR("Matches Corners and Sides"));
  2969. } else if (tile_set->get_terrain_set_mode(terrain_set_index) == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
  2970. terrain_set_tree_item->set_icon(0, main_vbox_container->get_editor_theme_icon(SNAME("TerrainMatchCorners")));
  2971. matches = String(TTR("Matches Corners Only"));
  2972. } else {
  2973. terrain_set_tree_item->set_icon(0, main_vbox_container->get_editor_theme_icon(SNAME("TerrainMatchSides")));
  2974. matches = String(TTR("Matches Sides Only"));
  2975. }
  2976. terrain_set_tree_item->set_text(0, vformat(TTR("Terrain Set %d (%s)"), terrain_set_index, matches));
  2977. terrain_set_tree_item->set_selectable(0, false);
  2978. for (int terrain_index = 0; terrain_index < tile_set->get_terrains_count(terrain_set_index); terrain_index++) {
  2979. // Add the item to the terrain list.
  2980. TreeItem *terrain_tree_item = terrains_tree->create_item(terrain_set_tree_item);
  2981. terrain_tree_item->set_text(0, tile_set->get_terrain_name(terrain_set_index, terrain_index));
  2982. terrain_tree_item->set_icon_max_width(0, 32 * EDSCALE);
  2983. terrain_tree_item->set_icon(0, icons[terrain_set_index][terrain_index]);
  2984. Dictionary metadata_dict;
  2985. metadata_dict["terrain_set"] = terrain_set_index;
  2986. metadata_dict["terrain_id"] = terrain_index;
  2987. terrain_tree_item->set_metadata(0, metadata_dict);
  2988. }
  2989. }
  2990. }
  2991. void TileMapLayerEditorTerrainsPlugin::_update_tiles_list() {
  2992. terrains_tile_list->clear();
  2993. const TileMapLayer *edited_layer = _get_edited_layer();
  2994. if (!edited_layer) {
  2995. return;
  2996. }
  2997. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  2998. if (tile_set.is_null()) {
  2999. return;
  3000. }
  3001. TreeItem *selected_tree_item = terrains_tree->get_selected();
  3002. if (selected_tree_item && selected_tree_item->get_metadata(0)) {
  3003. Dictionary metadata_dict = selected_tree_item->get_metadata(0);
  3004. int sel_terrain_set = metadata_dict["terrain_set"];
  3005. int sel_terrain_id = metadata_dict["terrain_id"];
  3006. ERR_FAIL_INDEX(sel_terrain_set, tile_set->get_terrain_sets_count());
  3007. ERR_FAIL_INDEX(sel_terrain_id, tile_set->get_terrains_count(sel_terrain_set));
  3008. // Add the two first generic modes
  3009. int item_index = terrains_tile_list->add_icon_item(main_vbox_container->get_editor_theme_icon(SNAME("TerrainConnect")));
  3010. 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."));
  3011. Dictionary list_metadata_dict;
  3012. list_metadata_dict["type"] = SELECTED_TYPE_CONNECT;
  3013. terrains_tile_list->set_item_metadata(item_index, list_metadata_dict);
  3014. item_index = terrains_tile_list->add_icon_item(main_vbox_container->get_editor_theme_icon(SNAME("TerrainPath")));
  3015. 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."));
  3016. list_metadata_dict = Dictionary();
  3017. list_metadata_dict["type"] = SELECTED_TYPE_PATH;
  3018. terrains_tile_list->set_item_metadata(item_index, list_metadata_dict);
  3019. // Sort the items in a map by the number of corresponding terrains.
  3020. RBMap<int, RBSet<TileSet::TerrainsPattern>> sorted;
  3021. for (const TileSet::TerrainsPattern &E : per_terrain_terrains_patterns[sel_terrain_set][sel_terrain_id]) {
  3022. // Count the number of matching sides/terrains.
  3023. int count = 0;
  3024. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  3025. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  3026. if (tile_set->is_valid_terrain_peering_bit(sel_terrain_set, bit) && E.get_terrain_peering_bit(bit) == sel_terrain_id) {
  3027. count++;
  3028. }
  3029. }
  3030. sorted[count].insert(E);
  3031. }
  3032. for (RBMap<int, RBSet<TileSet::TerrainsPattern>>::Element *E_set = sorted.back(); E_set; E_set = E_set->prev()) {
  3033. for (const TileSet::TerrainsPattern &E : E_set->get()) {
  3034. TileSet::TerrainsPattern terrains_pattern = E;
  3035. // Get the icon.
  3036. Ref<Texture2D> icon;
  3037. Rect2 region;
  3038. bool transpose = false;
  3039. double max_probability = -1.0;
  3040. for (const TileMapCell &cell : tile_set->get_tiles_for_terrains_pattern(sel_terrain_set, terrains_pattern)) {
  3041. Ref<TileSetSource> source = tile_set->get_source(cell.source_id);
  3042. Ref<TileSetAtlasSource> atlas_source = source;
  3043. if (atlas_source.is_valid()) {
  3044. TileData *tile_data = atlas_source->get_tile_data(cell.get_atlas_coords(), cell.alternative_tile);
  3045. if (tile_data->get_probability() > max_probability) {
  3046. icon = atlas_source->get_texture();
  3047. region = atlas_source->get_tile_texture_region(cell.get_atlas_coords());
  3048. if (tile_data->get_flip_h()) {
  3049. region.position.x += region.size.x;
  3050. region.size.x = -region.size.x;
  3051. }
  3052. if (tile_data->get_flip_v()) {
  3053. region.position.y += region.size.y;
  3054. region.size.y = -region.size.y;
  3055. }
  3056. transpose = tile_data->get_transpose();
  3057. max_probability = tile_data->get_probability();
  3058. }
  3059. }
  3060. }
  3061. // Create the ItemList's item.
  3062. item_index = terrains_tile_list->add_item("");
  3063. terrains_tile_list->set_item_icon(item_index, icon);
  3064. terrains_tile_list->set_item_icon_region(item_index, region);
  3065. terrains_tile_list->set_item_icon_transposed(item_index, transpose);
  3066. list_metadata_dict = Dictionary();
  3067. list_metadata_dict["type"] = SELECTED_TYPE_PATTERN;
  3068. list_metadata_dict["terrains_pattern"] = terrains_pattern.as_array();
  3069. terrains_tile_list->set_item_metadata(item_index, list_metadata_dict);
  3070. }
  3071. }
  3072. if (terrains_tile_list->get_item_count() > 0) {
  3073. terrains_tile_list->select(0);
  3074. }
  3075. }
  3076. }
  3077. void TileMapLayerEditorTerrainsPlugin::_update_theme() {
  3078. paint_tool_button->set_icon(main_vbox_container->get_editor_theme_icon(SNAME("Edit")));
  3079. line_tool_button->set_icon(main_vbox_container->get_editor_theme_icon(SNAME("Line")));
  3080. rect_tool_button->set_icon(main_vbox_container->get_editor_theme_icon(SNAME("Rectangle")));
  3081. bucket_tool_button->set_icon(main_vbox_container->get_editor_theme_icon(SNAME("Bucket")));
  3082. picker_button->set_icon(main_vbox_container->get_editor_theme_icon(SNAME("ColorPick")));
  3083. erase_button->set_icon(main_vbox_container->get_editor_theme_icon(SNAME("Eraser")));
  3084. _update_tiles_list();
  3085. }
  3086. void TileMapLayerEditorTerrainsPlugin::edit(ObjectID p_edited_tile_map_layer_id) {
  3087. _stop_dragging(); // Avoids staying in a wrong drag state.
  3088. if (edited_tile_map_layer_id != p_edited_tile_map_layer_id) {
  3089. edited_tile_map_layer_id = p_edited_tile_map_layer_id;
  3090. // Clear the selection.
  3091. _update_terrains_cache();
  3092. _update_terrains_tree();
  3093. _update_tiles_list();
  3094. }
  3095. }
  3096. TileMapLayerEditorTerrainsPlugin::TileMapLayerEditorTerrainsPlugin() {
  3097. main_vbox_container = memnew(VBoxContainer);
  3098. // FIXME: This can trigger theme updates when the nodes that we want to update are not yet available.
  3099. // The toolbar should be extracted to a dedicated control and theme updates should be handled through
  3100. // the notification.
  3101. main_vbox_container->connect(SceneStringName(theme_changed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_theme));
  3102. main_vbox_container->set_name(TTR("Terrains"));
  3103. HSplitContainer *tilemap_tab_terrains = memnew(HSplitContainer);
  3104. tilemap_tab_terrains->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  3105. tilemap_tab_terrains->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  3106. main_vbox_container->add_child(tilemap_tab_terrains);
  3107. terrains_tree = memnew(Tree);
  3108. terrains_tree->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  3109. terrains_tree->set_stretch_ratio(0.25);
  3110. terrains_tree->set_custom_minimum_size(Size2(70, 0) * EDSCALE);
  3111. terrains_tree->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  3112. terrains_tree->set_hide_root(true);
  3113. terrains_tree->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_tiles_list));
  3114. tilemap_tab_terrains->add_child(terrains_tree);
  3115. terrains_tile_list = memnew(ItemList);
  3116. terrains_tile_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  3117. terrains_tile_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  3118. terrains_tile_list->set_max_columns(0);
  3119. terrains_tile_list->set_same_column_width(true);
  3120. terrains_tile_list->set_fixed_icon_size(Size2(32, 32) * EDSCALE);
  3121. terrains_tile_list->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
  3122. tilemap_tab_terrains->add_child(terrains_tile_list);
  3123. // --- Toolbar ---
  3124. toolbar = memnew(HBoxContainer);
  3125. HBoxContainer *tilemap_tiles_tools_buttons = memnew(HBoxContainer);
  3126. tool_buttons_group.instantiate();
  3127. paint_tool_button = memnew(Button);
  3128. paint_tool_button->set_theme_type_variation("FlatButton");
  3129. paint_tool_button->set_toggle_mode(true);
  3130. paint_tool_button->set_button_group(tool_buttons_group);
  3131. paint_tool_button->set_pressed(true);
  3132. paint_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/paint_tool"));
  3133. paint_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar));
  3134. tilemap_tiles_tools_buttons->add_child(paint_tool_button);
  3135. viewport_shortcut_buttons.push_back(paint_tool_button);
  3136. line_tool_button = memnew(Button);
  3137. line_tool_button->set_theme_type_variation("FlatButton");
  3138. line_tool_button->set_toggle_mode(true);
  3139. line_tool_button->set_button_group(tool_buttons_group);
  3140. line_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/line_tool"));
  3141. line_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar));
  3142. tilemap_tiles_tools_buttons->add_child(line_tool_button);
  3143. viewport_shortcut_buttons.push_back(line_tool_button);
  3144. rect_tool_button = memnew(Button);
  3145. rect_tool_button->set_theme_type_variation("FlatButton");
  3146. rect_tool_button->set_toggle_mode(true);
  3147. rect_tool_button->set_button_group(tool_buttons_group);
  3148. rect_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/rect_tool"));
  3149. rect_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar));
  3150. tilemap_tiles_tools_buttons->add_child(rect_tool_button);
  3151. viewport_shortcut_buttons.push_back(rect_tool_button);
  3152. bucket_tool_button = memnew(Button);
  3153. bucket_tool_button->set_theme_type_variation("FlatButton");
  3154. bucket_tool_button->set_toggle_mode(true);
  3155. bucket_tool_button->set_button_group(tool_buttons_group);
  3156. bucket_tool_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/bucket_tool"));
  3157. bucket_tool_button->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditorTerrainsPlugin::_update_toolbar));
  3158. tilemap_tiles_tools_buttons->add_child(bucket_tool_button);
  3159. viewport_shortcut_buttons.push_back(bucket_tool_button);
  3160. toolbar->add_child(tilemap_tiles_tools_buttons);
  3161. // -- TileMap tool settings --
  3162. tools_settings = memnew(HBoxContainer);
  3163. toolbar->add_child(tools_settings);
  3164. tools_settings_vsep = memnew(VSeparator);
  3165. tools_settings->add_child(tools_settings_vsep);
  3166. // Picker
  3167. picker_button = memnew(Button);
  3168. picker_button->set_theme_type_variation("FlatButton");
  3169. picker_button->set_toggle_mode(true);
  3170. picker_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/picker"));
  3171. picker_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  3172. tools_settings->add_child(picker_button);
  3173. viewport_shortcut_buttons.push_back(picker_button);
  3174. // Erase button.
  3175. erase_button = memnew(Button);
  3176. erase_button->set_theme_type_variation("FlatButton");
  3177. erase_button->set_toggle_mode(true);
  3178. erase_button->set_shortcut(ED_GET_SHORTCUT("tiles_editor/eraser"));
  3179. erase_button->connect(SceneStringName(pressed), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
  3180. tools_settings->add_child(erase_button);
  3181. viewport_shortcut_buttons.push_back(erase_button);
  3182. // Separator 2.
  3183. tools_settings_vsep_2 = memnew(VSeparator);
  3184. tools_settings->add_child(tools_settings_vsep_2);
  3185. // Continuous checkbox.
  3186. bucket_contiguous_checkbox = memnew(CheckBox);
  3187. bucket_contiguous_checkbox->set_flat(true);
  3188. bucket_contiguous_checkbox->set_text(TTR("Contiguous"));
  3189. bucket_contiguous_checkbox->set_pressed(true);
  3190. tools_settings->add_child(bucket_contiguous_checkbox);
  3191. }
  3192. TileMapLayerEditorTerrainsPlugin::~TileMapLayerEditorTerrainsPlugin() {
  3193. }
  3194. TileMapLayer *TileMapLayerEditor::_get_edited_layer() const {
  3195. return Object::cast_to<TileMapLayer>(ObjectDB::get_instance(edited_tile_map_layer_id));
  3196. }
  3197. void TileMapLayerEditor::_find_tile_map_layers_in_scene(Node *p_current, const Node *p_owner, Vector<TileMapLayer *> &r_list) const {
  3198. ERR_FAIL_COND(!p_current || !p_owner);
  3199. if (p_current != p_owner) {
  3200. if (!p_current->get_owner()) {
  3201. return;
  3202. }
  3203. if (p_current->get_owner() != p_owner && !p_owner->is_editable_instance(p_current->get_owner())) {
  3204. return;
  3205. }
  3206. }
  3207. TileMapLayer *layer = Object::cast_to<TileMapLayer>(p_current);
  3208. if (layer) {
  3209. r_list.append(layer);
  3210. }
  3211. for (int i = 0; i < p_current->get_child_count(); i++) {
  3212. Node *child = p_current->get_child(i);
  3213. _find_tile_map_layers_in_scene(child, p_owner, r_list);
  3214. }
  3215. }
  3216. void TileMapLayerEditor::_update_tile_map_layers_in_scene_list_cache() {
  3217. if (!layers_in_scene_list_cache_needs_update) {
  3218. return;
  3219. }
  3220. EditorNode *en = EditorNode::get_singleton();
  3221. Node *edited_scene_root = en->get_edited_scene();
  3222. if (!edited_scene_root) {
  3223. return;
  3224. }
  3225. tile_map_layers_in_scene_cache.clear();
  3226. _find_tile_map_layers_in_scene(edited_scene_root, edited_scene_root, tile_map_layers_in_scene_cache);
  3227. layers_in_scene_list_cache_needs_update = false;
  3228. }
  3229. void TileMapLayerEditor::_node_change(Node *p_node) {
  3230. if (!layers_in_scene_list_cache_needs_update && p_node->is_part_of_edited_scene() && Object::cast_to<TileMapLayer>(p_node)) {
  3231. layers_in_scene_list_cache_needs_update = true;
  3232. }
  3233. }
  3234. void TileMapLayerEditor::_notification(int p_what) {
  3235. switch (p_what) {
  3236. case NOTIFICATION_ENTER_TREE: {
  3237. get_tree()->connect("node_added", callable_mp(this, &TileMapLayerEditor::_node_change));
  3238. get_tree()->connect("node_removed", callable_mp(this, &TileMapLayerEditor::_node_change));
  3239. } break;
  3240. case NOTIFICATION_EXIT_TREE: {
  3241. get_tree()->disconnect("node_added", callable_mp(this, &TileMapLayerEditor::_node_change));
  3242. get_tree()->disconnect("node_removed", callable_mp(this, &TileMapLayerEditor::_node_change));
  3243. } break;
  3244. case NOTIFICATION_THEME_CHANGED: {
  3245. missing_tile_texture = get_editor_theme_icon(SNAME("StatusWarning"));
  3246. warning_pattern_texture = get_editor_theme_icon(SNAME("WarningPattern"));
  3247. advanced_menu_button->set_icon(get_editor_theme_icon(SNAME("Tools")));
  3248. select_previous_layer->set_icon(get_editor_theme_icon(SNAME("MoveUp")));
  3249. select_next_layer->set_icon(get_editor_theme_icon(SNAME("MoveDown")));
  3250. select_all_layers->set_icon(get_editor_theme_icon(SNAME("FileList")));
  3251. toggle_grid_button->set_icon(get_editor_theme_icon(SNAME("Grid")));
  3252. toggle_highlight_selected_layer_button->set_icon(get_editor_theme_icon(SNAME("TileMapHighlightSelected")));
  3253. } break;
  3254. case NOTIFICATION_INTERNAL_PROCESS: {
  3255. if (is_visible_in_tree() && tile_map_layer_changed_needs_update) {
  3256. _update_bottom_panel();
  3257. _update_layers_selector();
  3258. tabs_plugins[tabs_bar->get_current_tab()]->tile_set_changed();
  3259. CanvasItemEditor::get_singleton()->update_viewport();
  3260. tile_map_layer_changed_needs_update = false;
  3261. }
  3262. } break;
  3263. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  3264. if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/tiles_editor")) {
  3265. toggle_grid_button->set_pressed_no_signal(EDITOR_GET("editors/tiles_editor/display_grid"));
  3266. toggle_highlight_selected_layer_button->set_pressed_no_signal(EDITOR_GET("editors/tiles_editor/highlight_selected_layer"));
  3267. }
  3268. } break;
  3269. }
  3270. }
  3271. void TileMapLayerEditor::_on_grid_toggled(bool p_pressed) {
  3272. EditorSettings::get_singleton()->set("editors/tiles_editor/display_grid", p_pressed);
  3273. CanvasItemEditor::get_singleton()->update_viewport();
  3274. }
  3275. void TileMapLayerEditor::_select_previous_layer_pressed() {
  3276. _layers_select_next_or_previous(false);
  3277. }
  3278. void TileMapLayerEditor::_select_next_layer_pressed() {
  3279. _layers_select_next_or_previous(true);
  3280. }
  3281. void TileMapLayerEditor::_select_all_layers_pressed() {
  3282. EditorNode *en = EditorNode::get_singleton();
  3283. Node *edited_scene_root = en->get_edited_scene();
  3284. ERR_FAIL_NULL(edited_scene_root);
  3285. en->get_editor_selection()->clear();
  3286. if (tile_map_layers_in_scene_cache.size() == 1) {
  3287. en->edit_node(tile_map_layers_in_scene_cache[0]);
  3288. en->get_editor_selection()->add_node(tile_map_layers_in_scene_cache[0]);
  3289. } else {
  3290. _update_tile_map_layers_in_scene_list_cache();
  3291. Ref<MultiNodeEdit> multi_node_edit = memnew(MultiNodeEdit);
  3292. for (TileMapLayer *layer : tile_map_layers_in_scene_cache) {
  3293. multi_node_edit->add_node(edited_scene_root->get_path_to(layer));
  3294. en->get_editor_selection()->add_node(layer);
  3295. }
  3296. en->push_item(multi_node_edit.ptr());
  3297. }
  3298. }
  3299. void TileMapLayerEditor::_layers_selection_item_selected(int p_index) {
  3300. TileMapLayer *edited_layer = _get_edited_layer();
  3301. ERR_FAIL_NULL(edited_layer);
  3302. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3303. ERR_FAIL_NULL(tile_map);
  3304. TileMapLayer *new_edited = Object::cast_to<TileMapLayer>(tile_map->get_child(p_index));
  3305. edit(new_edited);
  3306. }
  3307. void TileMapLayerEditor::_update_layers_selector() {
  3308. const TileMapLayer *edited_layer = _get_edited_layer();
  3309. // Update the selector.
  3310. layers_selection_button->clear();
  3311. layers_selection_button->hide();
  3312. select_all_layers->show();
  3313. select_next_layer->set_disabled(false);
  3314. select_previous_layer->set_disabled(false);
  3315. advanced_menu_button->get_popup()->set_item_disabled(ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS, true);
  3316. if (edited_layer) {
  3317. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3318. if (tile_map && edited_layer->get_index_in_tile_map() >= 0) {
  3319. // Build the list of layers.
  3320. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3321. const TileMapLayer *layer = Object::cast_to<TileMapLayer>(tile_map->get_child(i));
  3322. if (layer) {
  3323. int index = layers_selection_button->get_item_count();
  3324. layers_selection_button->add_item(layer->get_name());
  3325. layers_selection_button->set_item_metadata(index, layer->get_name());
  3326. if (edited_layer == layer) {
  3327. layers_selection_button->select(index);
  3328. }
  3329. }
  3330. }
  3331. // Disable selector if there's no layer to select.
  3332. layers_selection_button->set_disabled(false);
  3333. if (layers_selection_button->get_item_count() == 0) {
  3334. layers_selection_button->set_disabled(true);
  3335. layers_selection_button->set_text(TTR("No Layers"));
  3336. }
  3337. // Disable next/previous if there's one or less layers.
  3338. if (layers_selection_button->get_item_count() <= 1) {
  3339. select_next_layer->set_disabled(true);
  3340. select_previous_layer->set_disabled(true);
  3341. }
  3342. layers_selection_button->show();
  3343. select_all_layers->hide();
  3344. // Enable the "extract as TileMapLayer" option only if we are editing a TleMap.
  3345. advanced_menu_button->get_popup()->set_item_disabled(ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS, false);
  3346. }
  3347. } else {
  3348. select_all_layers->hide();
  3349. select_next_layer->set_disabled(true);
  3350. select_previous_layer->set_disabled(true);
  3351. }
  3352. }
  3353. void TileMapLayerEditor::_clear_all_layers_highlighting() {
  3354. // Note: This function might be removed if we remove the TileMap node at some point.
  3355. // All processing could be done in _update_all_layers_highlighting otherwise.
  3356. TileMapLayer *edited_layer = _get_edited_layer();
  3357. // Use default mode.
  3358. if (edited_layer && edited_layer->get_index_in_tile_map() >= 0) {
  3359. // For the TileMap node.
  3360. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3361. if (tile_map) {
  3362. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3363. TileMapLayer *layer = Object::cast_to<TileMapLayer>(tile_map->get_child(i));
  3364. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT);
  3365. }
  3366. }
  3367. } else {
  3368. // For other TileMapLayer nodes.
  3369. _update_tile_map_layers_in_scene_list_cache();
  3370. for (TileMapLayer *layer : tile_map_layers_in_scene_cache) {
  3371. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT);
  3372. }
  3373. }
  3374. }
  3375. void TileMapLayerEditor::_update_all_layers_highlighting() {
  3376. EditorNode *en = EditorNode::get_singleton();
  3377. Node *edited_scene_root = en->get_edited_scene();
  3378. if (!edited_scene_root) {
  3379. return;
  3380. }
  3381. // Get selected layer.
  3382. TileMapLayer *edited_layer = _get_edited_layer();
  3383. bool highlight_selected_layer = EDITOR_GET("editors/tiles_editor/highlight_selected_layer");
  3384. if (edited_layer && highlight_selected_layer) {
  3385. int edited_z_index = edited_layer->get_z_index();
  3386. if (edited_layer->get_index_in_tile_map() >= 0) {
  3387. // For the TileMap node.
  3388. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3389. ERR_FAIL_NULL(tile_map);
  3390. bool passed = false;
  3391. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3392. TileMapLayer *layer = Object::cast_to<TileMapLayer>(tile_map->get_child(i));
  3393. if (layer == edited_layer) {
  3394. passed = true;
  3395. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT);
  3396. } else {
  3397. if (passed || layer->get_z_index() > edited_z_index) {
  3398. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_ABOVE);
  3399. } else {
  3400. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_BELOW);
  3401. }
  3402. }
  3403. }
  3404. } else {
  3405. // Update highlight mode for independent layers.
  3406. _update_tile_map_layers_in_scene_list_cache();
  3407. bool passed = false;
  3408. for (TileMapLayer *layer : tile_map_layers_in_scene_cache) {
  3409. if (layer == edited_layer) {
  3410. passed = true;
  3411. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_DEFAULT);
  3412. } else {
  3413. if (passed || layer->get_z_index() > edited_z_index) {
  3414. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_ABOVE);
  3415. } else {
  3416. layer->set_highlight_mode(TileMapLayer::HIGHLIGHT_MODE_BELOW);
  3417. }
  3418. }
  3419. }
  3420. }
  3421. }
  3422. }
  3423. void TileMapLayerEditor::_highlight_selected_layer_button_toggled(bool p_pressed) {
  3424. TileMapLayer *edited_layer = _get_edited_layer();
  3425. if (!edited_layer) {
  3426. return;
  3427. }
  3428. EditorSettings::get_singleton()->set("editors/tiles_editor/highlight_selected_layer", p_pressed);
  3429. if (p_pressed) {
  3430. _update_all_layers_highlighting();
  3431. } else {
  3432. _clear_all_layers_highlighting();
  3433. }
  3434. }
  3435. void TileMapLayerEditor::_advanced_menu_button_id_pressed(int p_id) {
  3436. TileMapLayer *edited_layer = _get_edited_layer();
  3437. if (!edited_layer) {
  3438. return;
  3439. }
  3440. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  3441. if (tile_set.is_null()) {
  3442. return;
  3443. }
  3444. if (p_id == ADVANCED_MENU_REPLACE_WITH_PROXIES) { // Replace Tile Proxies
  3445. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3446. undo_redo->create_action(TTR("Replace Tiles with Proxies"));
  3447. TypedArray<Vector2i> used_cells = edited_layer->get_used_cells();
  3448. for (int i = 0; i < used_cells.size(); i++) {
  3449. Vector2i cell_coords = used_cells[i];
  3450. TileMapCell from = edited_layer->get_cell(cell_coords);
  3451. Array to_array = tile_set->map_tile_proxy(from.source_id, from.get_atlas_coords(), from.alternative_tile);
  3452. TileMapCell to;
  3453. to.source_id = to_array[0];
  3454. to.set_atlas_coords(to_array[1]);
  3455. to.alternative_tile = to_array[2];
  3456. if (from != to) {
  3457. undo_redo->add_do_method(edited_layer, "set_cell", cell_coords, to.source_id, to.get_atlas_coords(), to.alternative_tile);
  3458. undo_redo->add_undo_method(edited_layer, "set_cell", cell_coords, from.source_id, from.get_atlas_coords(), from.alternative_tile);
  3459. }
  3460. }
  3461. undo_redo->commit_action();
  3462. } else if (p_id == ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS) { // Transform internal TileMap layers into TileMapLayers.
  3463. ERR_FAIL_COND(edited_layer->get_index_in_tile_map() < 0);
  3464. EditorNode *en = EditorNode::get_singleton();
  3465. Node *edited_scene_root = en->get_edited_scene();
  3466. ERR_FAIL_NULL(edited_scene_root);
  3467. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3468. undo_redo->create_action(TTR("Extract TileMap layers as individual TileMapLayer nodes"));
  3469. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3470. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3471. undo_redo->add_do_method(tile_map, "remove_layer", 0);
  3472. }
  3473. for (int i = 0; i < tile_map->get_layers_count(); i++) {
  3474. TileMapLayer *new_layer = tile_map->duplicate_layer_from_internal(i);
  3475. undo_redo->add_do_method(tile_map, "add_child", new_layer);
  3476. undo_redo->add_do_method(new_layer, "set_owner", edited_scene_root);
  3477. undo_redo->add_do_property(new_layer, "tile_set", tile_map->get_tileset()); // Workaround for a bug: #89947.
  3478. undo_redo->add_undo_method(tile_map, "remove_child", new_layer);
  3479. undo_redo->add_do_reference(new_layer);
  3480. }
  3481. List<PropertyInfo> prop_list;
  3482. tile_map->get_property_list(&prop_list);
  3483. for (PropertyInfo &prop : prop_list) {
  3484. undo_redo->add_undo_property(tile_map, prop.name, tile_map->get(prop.name));
  3485. }
  3486. undo_redo->commit_action();
  3487. }
  3488. }
  3489. void TileMapLayerEditor::_update_bottom_panel() {
  3490. const TileMapLayer *edited_layer = _get_edited_layer();
  3491. Ref<TileSet> tile_set;
  3492. if (edited_layer) {
  3493. tile_set = edited_layer->get_tile_set();
  3494. }
  3495. // Update state labels.
  3496. if (is_multi_node_edit) {
  3497. cant_edit_label->set_text(TTR("Can't edit multiple layers at once."));
  3498. cant_edit_label->show();
  3499. } else if (!edited_layer) {
  3500. cant_edit_label->set_text(TTR("The selected TileMap has no layer to edit."));
  3501. cant_edit_label->show();
  3502. } else if (!edited_layer->is_enabled() || !edited_layer->is_visible_in_tree()) {
  3503. cant_edit_label->set_text(TTR("The edited layer is disabled or invisible"));
  3504. cant_edit_label->show();
  3505. } else if (tile_set.is_null()) {
  3506. 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."));
  3507. cant_edit_label->show();
  3508. } else {
  3509. cant_edit_label->hide();
  3510. }
  3511. // Update tabs visibility.
  3512. for (int i = 0; i < int(tabs_data.size()); i++) {
  3513. TileMapLayerSubEditorPlugin::TabData &tab_data = tabs_data[i];
  3514. if (i == tabs_bar->get_current_tab()) {
  3515. tab_data.panel->set_visible(!cant_edit_label->is_visible());
  3516. } else {
  3517. tab_data.panel->hide();
  3518. }
  3519. }
  3520. }
  3521. Vector<Vector2i> TileMapLayerEditor::get_line(const TileMapLayer *p_tile_map_layer, Vector2i p_from_cell, Vector2i p_to_cell) {
  3522. ERR_FAIL_NULL_V(p_tile_map_layer, Vector<Vector2i>());
  3523. Ref<TileSet> tile_set = p_tile_map_layer->get_tile_set();
  3524. ERR_FAIL_COND_V(tile_set.is_null(), Vector<Vector2i>());
  3525. if (tile_set->get_tile_shape() == TileSet::TILE_SHAPE_SQUARE) {
  3526. return Geometry2D::bresenham_line(p_from_cell, p_to_cell);
  3527. } else {
  3528. // Adapt the bresenham line algorithm to half-offset shapes.
  3529. // See this blog post: http://zvold.blogspot.com/2010/01/bresenhams-line-drawing-algorithm-on_26.html
  3530. Vector<Point2i> points;
  3531. bool transposed = tile_set->get_tile_offset_axis() == TileSet::TILE_OFFSET_AXIS_VERTICAL;
  3532. 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);
  3533. 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);
  3534. if (transposed) {
  3535. SWAP(p_from_cell.x, p_from_cell.y);
  3536. SWAP(p_to_cell.x, p_to_cell.y);
  3537. }
  3538. Vector2i delta = p_to_cell - p_from_cell;
  3539. delta = Vector2i(2 * delta.x + ABS(p_to_cell.y % 2) - ABS(p_from_cell.y % 2), delta.y);
  3540. Vector2i sign = delta.sign();
  3541. Vector2i current = p_from_cell;
  3542. 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()));
  3543. int err = 0;
  3544. if (ABS(delta.y) < ABS(delta.x)) {
  3545. Vector2i err_step = 3 * delta.abs();
  3546. while (current != p_to_cell) {
  3547. err += err_step.y;
  3548. if (err > ABS(delta.x)) {
  3549. if (sign.x == 0) {
  3550. current += Vector2(sign.y, 0);
  3551. } else {
  3552. current += Vector2(bool(current.y % 2) ^ (sign.x < 0) ? sign.x : 0, sign.y);
  3553. }
  3554. err -= err_step.x;
  3555. } else {
  3556. current += Vector2i(sign.x, 0);
  3557. err += err_step.y;
  3558. }
  3559. 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()));
  3560. }
  3561. } else {
  3562. Vector2i err_step = delta.abs();
  3563. while (current != p_to_cell) {
  3564. err += err_step.x;
  3565. if (err > 0) {
  3566. if (sign.x == 0) {
  3567. current += Vector2(0, sign.y);
  3568. } else {
  3569. current += Vector2(bool(current.y % 2) ^ (sign.x < 0) ? sign.x : 0, sign.y);
  3570. }
  3571. err -= err_step.y;
  3572. } else {
  3573. if (sign.x == 0) {
  3574. current += Vector2(0, sign.y);
  3575. } else {
  3576. current += Vector2(bool(current.y % 2) ^ (sign.x > 0) ? -sign.x : 0, sign.y);
  3577. }
  3578. err += err_step.y;
  3579. }
  3580. 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()));
  3581. }
  3582. }
  3583. return points;
  3584. }
  3585. }
  3586. void TileMapLayerEditor::_tile_map_layer_changed() {
  3587. tile_map_layer_changed_needs_update = true;
  3588. }
  3589. void TileMapLayerEditor::_tab_changed(int p_tab_id) {
  3590. // Make the plugin edit the correct tilemap.
  3591. tabs_plugins[tabs_bar->get_current_tab()]->edit(edited_tile_map_layer_id);
  3592. // Update toolbar.
  3593. for (TileMapLayerSubEditorPlugin::TabData &tab_data : tabs_data) {
  3594. tab_data.toolbar->hide();
  3595. }
  3596. tabs_data[p_tab_id].toolbar->show();
  3597. // Update visible panel.
  3598. for (TileMapLayerSubEditorPlugin::TabData &tab_data : tabs_data) {
  3599. tab_data.panel->hide();
  3600. }
  3601. TileMapLayer *tile_map_layer = _get_edited_layer();
  3602. if (tile_map_layer) {
  3603. if (tile_map_layer->get_tile_set().is_valid()) {
  3604. tabs_data[tabs_bar->get_current_tab()].panel->show();
  3605. }
  3606. }
  3607. // Graphical update.
  3608. tabs_data[tabs_bar->get_current_tab()].panel->queue_redraw();
  3609. CanvasItemEditor::get_singleton()->update_viewport();
  3610. }
  3611. void TileMapLayerEditor::_layers_select_next_or_previous(bool p_next) {
  3612. const TileMapLayer *edited_layer = _get_edited_layer();
  3613. if (!edited_layer) {
  3614. return;
  3615. }
  3616. EditorNode *en = EditorNode::get_singleton();
  3617. Node *edited_scene_root = en->get_edited_scene();
  3618. ERR_FAIL_NULL(edited_scene_root);
  3619. TileMapLayer *new_selected_layer = nullptr;
  3620. int inc = p_next ? 1 : -1;
  3621. if (edited_layer->get_index_in_tile_map() >= 0) {
  3622. // Part of a TileMap.
  3623. TileMap *tile_map = Object::cast_to<TileMap>(edited_layer->get_parent());
  3624. 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())));
  3625. } else {
  3626. // Individual layer.
  3627. _update_tile_map_layers_in_scene_list_cache();
  3628. int edited_index = -1;
  3629. for (int i = 0; i < tile_map_layers_in_scene_cache.size(); i++) {
  3630. if (tile_map_layers_in_scene_cache[i] == edited_layer) {
  3631. edited_index = i;
  3632. break;
  3633. }
  3634. }
  3635. new_selected_layer = tile_map_layers_in_scene_cache[Math::posmod(edited_index + inc, tile_map_layers_in_scene_cache.size())];
  3636. }
  3637. ERR_FAIL_NULL(new_selected_layer);
  3638. if (edited_layer->get_index_in_tile_map() < 0) {
  3639. // Only if not part of a TileMap.
  3640. en->edit_node(new_selected_layer);
  3641. en->get_editor_selection()->clear();
  3642. en->get_editor_selection()->add_node(new_selected_layer);
  3643. } else {
  3644. edit(new_selected_layer);
  3645. }
  3646. }
  3647. 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) {
  3648. EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  3649. ERR_FAIL_NULL(undo_redo_man);
  3650. TileMap *tile_map = Object::cast_to<TileMap>(p_edited);
  3651. if (!tile_map) {
  3652. return;
  3653. }
  3654. // Compute the array indices to save.
  3655. int begin = 0;
  3656. int end;
  3657. if (p_array_prefix == "layer_") {
  3658. end = tile_map->get_layers_count();
  3659. } else {
  3660. ERR_FAIL_MSG("Invalid array prefix for TileSet.");
  3661. }
  3662. if (p_from_index < 0) {
  3663. // Adding new.
  3664. if (p_to_pos >= 0) {
  3665. begin = p_to_pos;
  3666. } else {
  3667. end = 0; // Nothing to save when adding at the end.
  3668. }
  3669. } else if (p_to_pos < 0) {
  3670. // Removing.
  3671. begin = p_from_index;
  3672. } else {
  3673. // Moving.
  3674. begin = MIN(p_from_index, p_to_pos);
  3675. end = MIN(MAX(p_from_index, p_to_pos) + 1, end);
  3676. }
  3677. #define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));
  3678. // Save layers' properties.
  3679. if (p_from_index < 0) {
  3680. undo_redo_man->add_undo_method(tile_map, "remove_layer", p_to_pos < 0 ? tile_map->get_layers_count() : p_to_pos);
  3681. } else if (p_to_pos < 0) {
  3682. undo_redo_man->add_undo_method(tile_map, "add_layer", p_from_index);
  3683. }
  3684. List<PropertyInfo> properties;
  3685. tile_map->get_property_list(&properties);
  3686. for (PropertyInfo pi : properties) {
  3687. if (pi.name.begins_with(p_array_prefix)) {
  3688. String str = pi.name.trim_prefix(p_array_prefix);
  3689. int to_char_index = 0;
  3690. while (to_char_index < str.length()) {
  3691. if (!is_digit(str[to_char_index])) {
  3692. break;
  3693. }
  3694. to_char_index++;
  3695. }
  3696. if (to_char_index > 0) {
  3697. int array_index = str.left(to_char_index).to_int();
  3698. if (array_index >= begin && array_index < end) {
  3699. ADD_UNDO(tile_map, pi.name);
  3700. }
  3701. }
  3702. }
  3703. }
  3704. #undef ADD_UNDO
  3705. if (p_from_index < 0) {
  3706. undo_redo_man->add_do_method(tile_map, "add_layer", p_to_pos);
  3707. } else if (p_to_pos < 0) {
  3708. undo_redo_man->add_do_method(tile_map, "remove_layer", p_from_index);
  3709. } else {
  3710. undo_redo_man->add_do_method(tile_map, "move_layer", p_from_index, p_to_pos);
  3711. }
  3712. }
  3713. bool TileMapLayerEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  3714. if (ED_IS_SHORTCUT("tiles_editor/select_next_layer", p_event) && p_event->is_pressed()) {
  3715. _layers_select_next_or_previous(true);
  3716. return true;
  3717. }
  3718. if (ED_IS_SHORTCUT("tiles_editor/select_previous_layer", p_event) && p_event->is_pressed()) {
  3719. _layers_select_next_or_previous(false);
  3720. return true;
  3721. }
  3722. return tabs_plugins[tabs_bar->get_current_tab()]->forward_canvas_gui_input(p_event);
  3723. }
  3724. void TileMapLayerEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  3725. const TileMapLayer *edited_layer = _get_edited_layer();
  3726. if (!edited_layer) {
  3727. return;
  3728. }
  3729. Ref<TileSet> tile_set = edited_layer->get_tile_set();
  3730. if (tile_set.is_null()) {
  3731. return;
  3732. }
  3733. if (!edited_layer->is_visible_in_tree()) {
  3734. return;
  3735. }
  3736. Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * edited_layer->get_global_transform_with_canvas();
  3737. Transform2D xform_inv = xform.affine_inverse();
  3738. Vector2i tile_shape_size = tile_set->get_tile_size();
  3739. // Draw tiles with invalid IDs in the grid.
  3740. TypedArray<Vector2i> used_cells = edited_layer->get_used_cells();
  3741. for (int i = 0; i < used_cells.size(); i++) {
  3742. Vector2i coords = used_cells[i];
  3743. int tile_source_id = edited_layer->get_cell_source_id(coords);
  3744. if (tile_source_id >= 0) {
  3745. Vector2i tile_atlas_coords = edited_layer->get_cell_atlas_coords(coords);
  3746. int tile_alternative_tile = edited_layer->get_cell_alternative_tile(coords);
  3747. TileSetSource *source = nullptr;
  3748. if (tile_set->has_source(tile_source_id)) {
  3749. source = *tile_set->get_source(tile_source_id);
  3750. }
  3751. if (!source || !source->has_tile(tile_atlas_coords) || !source->has_alternative_tile(tile_atlas_coords, tile_alternative_tile)) {
  3752. // Generate a random color from the hashed identifier of the tiles.
  3753. Array to_hash;
  3754. to_hash.push_back(tile_source_id);
  3755. to_hash.push_back(tile_atlas_coords);
  3756. to_hash.push_back(tile_alternative_tile);
  3757. uint32_t hash = RandomPCG(to_hash.hash()).rand();
  3758. Color color;
  3759. color = color.from_hsv(
  3760. (float)((hash >> 24) & 0xFF) / 256.0,
  3761. Math::lerp(0.5, 1.0, (float)((hash >> 16) & 0xFF) / 256.0),
  3762. Math::lerp(0.5, 1.0, (float)((hash >> 8) & 0xFF) / 256.0),
  3763. 0.8);
  3764. // Display the warning pattern.
  3765. Transform2D tile_xform;
  3766. tile_xform.set_origin(tile_set->map_to_local(coords));
  3767. tile_xform.set_scale(tile_shape_size);
  3768. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, color, true, warning_pattern_texture);
  3769. // Draw the warning icon.
  3770. Vector2::Axis min_axis = missing_tile_texture->get_size().min_axis_index();
  3771. Vector2 icon_size;
  3772. icon_size[min_axis] = tile_set->get_tile_size()[min_axis] / 3;
  3773. 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]);
  3774. Rect2 rect = Rect2(xform.xform(tile_set->map_to_local(coords)) - (icon_size * xform.get_scale() / 2), icon_size * xform.get_scale());
  3775. p_overlay->draw_texture_rect(missing_tile_texture, rect);
  3776. }
  3777. }
  3778. }
  3779. // Fading on the border.
  3780. const int fading = 5;
  3781. // Determine the drawn area.
  3782. Size2 screen_size = p_overlay->get_size();
  3783. Rect2i screen_rect;
  3784. screen_rect.position = tile_set->local_to_map(xform_inv.xform(Vector2()));
  3785. screen_rect.expand_to(tile_set->local_to_map(xform_inv.xform(Vector2(0, screen_size.height))));
  3786. screen_rect.expand_to(tile_set->local_to_map(xform_inv.xform(Vector2(screen_size.width, 0))));
  3787. screen_rect.expand_to(tile_set->local_to_map(xform_inv.xform(screen_size)));
  3788. screen_rect = screen_rect.grow(1);
  3789. Rect2i tilemap_used_rect = edited_layer->get_used_rect();
  3790. Rect2i displayed_rect = tilemap_used_rect.intersection(screen_rect);
  3791. displayed_rect = displayed_rect.grow(fading);
  3792. // Reduce the drawn area to avoid crashes if needed.
  3793. int max_size = 100;
  3794. if (displayed_rect.size.x > max_size) {
  3795. displayed_rect = displayed_rect.grow_individual(-(displayed_rect.size.x - max_size) / 2, 0, -(displayed_rect.size.x - max_size) / 2, 0);
  3796. }
  3797. if (displayed_rect.size.y > max_size) {
  3798. displayed_rect = displayed_rect.grow_individual(0, -(displayed_rect.size.y - max_size) / 2, 0, -(displayed_rect.size.y - max_size) / 2);
  3799. }
  3800. // Draw the grid.
  3801. bool display_grid = EDITOR_GET("editors/tiles_editor/display_grid");
  3802. if (display_grid) {
  3803. Color grid_color = EDITOR_GET("editors/tiles_editor/grid_color");
  3804. for (int x = displayed_rect.position.x; x < (displayed_rect.position.x + displayed_rect.size.x); x++) {
  3805. for (int y = displayed_rect.position.y; y < (displayed_rect.position.y + displayed_rect.size.y); y++) {
  3806. Vector2i pos_in_rect = Vector2i(x, y) - displayed_rect.position;
  3807. // Fade out the border of the grid.
  3808. float left_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.x), 0.0f, 1.0f);
  3809. 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);
  3810. float top_opacity = CLAMP(Math::inverse_lerp(0.0f, (float)fading, (float)pos_in_rect.y), 0.0f, 1.0f);
  3811. 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);
  3812. float opacity = CLAMP(MIN(left_opacity, MIN(right_opacity, MIN(top_opacity, bottom_opacity))) + 0.1, 0.0f, 1.0f);
  3813. Transform2D tile_xform;
  3814. tile_xform.set_origin(tile_set->map_to_local(Vector2(x, y)));
  3815. tile_xform.set_scale(tile_shape_size);
  3816. Color color = grid_color;
  3817. color.a = color.a * opacity;
  3818. tile_set->draw_tile_shape(p_overlay, xform * tile_xform, color, false);
  3819. }
  3820. }
  3821. }
  3822. // Draw the IDs for debug.
  3823. /*Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  3824. for (int x = displayed_rect.position.x; x < (displayed_rect.position.x + displayed_rect.size.x); x++) {
  3825. for (int y = displayed_rect.position.y; y < (displayed_rect.position.y + displayed_rect.size.y); y++) {
  3826. 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)));
  3827. }
  3828. }*/
  3829. // Draw the plugins.
  3830. tabs_plugins[tabs_bar->get_current_tab()]->forward_canvas_draw_over_viewport(p_overlay);
  3831. }
  3832. void TileMapLayerEditor::edit(Object *p_edited) {
  3833. if (p_edited && p_edited->get_instance_id() == edited_tile_map_layer_id) {
  3834. return;
  3835. }
  3836. _clear_all_layers_highlighting();
  3837. // Disconnect to changes.
  3838. TileMapLayer *tile_map_layer = _get_edited_layer();
  3839. if (tile_map_layer) {
  3840. tile_map_layer->disconnect(CoreStringName(changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed));
  3841. tile_map_layer->disconnect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed));
  3842. }
  3843. // Update the edited layer.
  3844. TileMapLayer *new_layer = Object::cast_to<TileMapLayer>(p_edited);
  3845. if (new_layer) {
  3846. // Change the edited object.
  3847. edited_tile_map_layer_id = new_layer->get_instance_id();
  3848. tile_map_layer = _get_edited_layer();
  3849. // Connect to changes.
  3850. if (!tile_map_layer->is_connected(CoreStringName(changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed))) {
  3851. tile_map_layer->connect(CoreStringName(changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed));
  3852. tile_map_layer->connect(SceneStringName(visibility_changed), callable_mp(this, &TileMapLayerEditor::_tile_map_layer_changed));
  3853. }
  3854. } else {
  3855. edited_tile_map_layer_id = ObjectID();
  3856. }
  3857. // Check if we are trying to use a MultiNodeEdit.
  3858. is_multi_node_edit = Object::cast_to<MultiNodeEdit>(p_edited);
  3859. // Call the plugins and update everything.
  3860. tabs_plugins[tabs_bar->get_current_tab()]->edit(edited_tile_map_layer_id);
  3861. _update_layers_selector();
  3862. _update_all_layers_highlighting();
  3863. _tile_map_layer_changed();
  3864. }
  3865. void TileMapLayerEditor::set_show_layer_selector(bool p_show_layer_selector) {
  3866. show_layers_selector = p_show_layer_selector;
  3867. _update_layers_selector();
  3868. }
  3869. TileMapLayerEditor::TileMapLayerEditor() {
  3870. set_process_internal(true);
  3871. // Shortcuts.
  3872. ED_SHORTCUT("tiles_editor/select_next_layer", TTR("Select Next Tile Map Layer"), Key::PAGEDOWN);
  3873. ED_SHORTCUT("tiles_editor/select_previous_layer", TTR("Select Previous Tile Map Layer"), Key::PAGEUP);
  3874. // TileMap editor plugins
  3875. tile_map_editor_plugins.push_back(memnew(TileMapLayerEditorTilesPlugin));
  3876. tile_map_editor_plugins.push_back(memnew(TileMapLayerEditorTerrainsPlugin));
  3877. // TabBar.
  3878. tabs_bar = memnew(TabBar);
  3879. tabs_bar->set_clip_tabs(false);
  3880. for (int plugin_index = 0; plugin_index < tile_map_editor_plugins.size(); plugin_index++) {
  3881. Vector<TileMapLayerSubEditorPlugin::TabData> tabs_vector = tile_map_editor_plugins[plugin_index]->get_tabs();
  3882. for (int tab_index = 0; tab_index < tabs_vector.size(); tab_index++) {
  3883. tabs_bar->add_tab(tabs_vector[tab_index].panel->get_name());
  3884. tabs_data.push_back(tabs_vector[tab_index]);
  3885. tabs_plugins.push_back(tile_map_editor_plugins[plugin_index]);
  3886. }
  3887. }
  3888. tabs_bar->connect("tab_changed", callable_mp(this, &TileMapLayerEditor::_tab_changed));
  3889. // --- TileMap toolbar ---
  3890. tile_map_toolbar = memnew(HFlowContainer);
  3891. tile_map_toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
  3892. add_child(tile_map_toolbar);
  3893. // Tabs.
  3894. tile_map_toolbar->add_child(tabs_bar);
  3895. // Tabs toolbars.
  3896. for (TileMapLayerSubEditorPlugin::TabData &tab_data : tabs_data) {
  3897. tab_data.toolbar->hide();
  3898. if (!tab_data.toolbar->get_parent()) {
  3899. tile_map_toolbar->add_child(tab_data.toolbar);
  3900. }
  3901. }
  3902. // Wide empty separation control. (like BoxContainer::add_spacer())
  3903. Control *c = memnew(Control);
  3904. c->set_mouse_filter(MOUSE_FILTER_PASS);
  3905. c->set_h_size_flags(SIZE_EXPAND_FILL);
  3906. tile_map_toolbar->add_child(c);
  3907. // Layer selector.
  3908. layer_selection_hbox = memnew(HBoxContainer);
  3909. tile_map_toolbar->add_child(layer_selection_hbox);
  3910. layers_selection_button = memnew(OptionButton);
  3911. layers_selection_button->set_custom_minimum_size(Size2(200, 0));
  3912. layers_selection_button->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  3913. layers_selection_button->set_tooltip_text(TTR("TileMap Layers"));
  3914. layers_selection_button->connect(SceneStringName(item_selected), callable_mp(this, &TileMapLayerEditor::_layers_selection_item_selected));
  3915. layer_selection_hbox->add_child(layers_selection_button);
  3916. select_previous_layer = memnew(Button);
  3917. select_previous_layer->set_theme_type_variation("FlatButton");
  3918. select_previous_layer->set_tooltip_text(TTR("Select previous layer"));
  3919. select_previous_layer->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditor::_select_previous_layer_pressed));
  3920. layer_selection_hbox->add_child(select_previous_layer);
  3921. select_next_layer = memnew(Button);
  3922. select_next_layer->set_theme_type_variation("FlatButton");
  3923. select_next_layer->set_tooltip_text(TTR("Select next layer"));
  3924. select_next_layer->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditor::_select_next_layer_pressed));
  3925. layer_selection_hbox->add_child(select_next_layer);
  3926. select_all_layers = memnew(Button);
  3927. select_all_layers->set_theme_type_variation("FlatButton");
  3928. select_all_layers->set_text(TTR("Select all layers"));
  3929. select_all_layers->connect(SceneStringName(pressed), callable_mp(this, &TileMapLayerEditor::_select_all_layers_pressed));
  3930. select_all_layers->set_tooltip_text(TTR("Select all TileMapLayers in scene"));
  3931. layer_selection_hbox->add_child(select_all_layers);
  3932. // Highlighting selected layer.
  3933. toggle_highlight_selected_layer_button = memnew(Button);
  3934. toggle_highlight_selected_layer_button->set_theme_type_variation("FlatButton");
  3935. toggle_highlight_selected_layer_button->set_toggle_mode(true);
  3936. toggle_highlight_selected_layer_button->set_pressed(true);
  3937. toggle_highlight_selected_layer_button->connect(SceneStringName(toggled), callable_mp(this, &TileMapLayerEditor::_highlight_selected_layer_button_toggled));
  3938. toggle_highlight_selected_layer_button->set_tooltip_text(TTR("Highlight Selected TileMap Layer"));
  3939. tile_map_toolbar->add_child(toggle_highlight_selected_layer_button);
  3940. tile_map_toolbar->add_child(memnew(VSeparator));
  3941. // Grid toggle.
  3942. toggle_grid_button = memnew(Button);
  3943. toggle_grid_button->set_theme_type_variation("FlatButton");
  3944. toggle_grid_button->set_toggle_mode(true);
  3945. toggle_grid_button->set_tooltip_text(TTR("Toggle grid visibility."));
  3946. toggle_grid_button->connect(SceneStringName(toggled), callable_mp(this, &TileMapLayerEditor::_on_grid_toggled));
  3947. tile_map_toolbar->add_child(toggle_grid_button);
  3948. // Advanced settings menu button.
  3949. advanced_menu_button = memnew(MenuButton);
  3950. advanced_menu_button->set_flat(false);
  3951. advanced_menu_button->set_theme_type_variation("FlatButton");
  3952. advanced_menu_button->get_popup()->add_item(TTR("Automatically Replace Tiles with Proxies"), ADVANCED_MENU_REPLACE_WITH_PROXIES);
  3953. advanced_menu_button->get_popup()->add_item(TTR("Extract TileMap layers as individual TileMapLayer nodes"), ADVANCED_MENU_EXTRACT_TILE_MAP_LAYERS);
  3954. advanced_menu_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TileMapLayerEditor::_advanced_menu_button_id_pressed));
  3955. tile_map_toolbar->add_child(advanced_menu_button);
  3956. // A label for editing errors.
  3957. cant_edit_label = memnew(Label);
  3958. cant_edit_label->set_h_size_flags(SIZE_EXPAND_FILL);
  3959. cant_edit_label->set_v_size_flags(SIZE_EXPAND_FILL);
  3960. cant_edit_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  3961. cant_edit_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  3962. cant_edit_label->hide();
  3963. add_child(cant_edit_label);
  3964. for (unsigned int tab_index = 0; tab_index < tabs_data.size(); tab_index++) {
  3965. add_child(tabs_data[tab_index].panel);
  3966. tabs_data[tab_index].panel->set_v_size_flags(SIZE_EXPAND_FILL);
  3967. tabs_data[tab_index].panel->set_visible(tab_index == 0);
  3968. tabs_data[tab_index].panel->set_h_size_flags(SIZE_EXPAND_FILL);
  3969. }
  3970. _tab_changed(0);
  3971. // Registers UndoRedo inspector callback.
  3972. EditorNode::get_editor_data().add_move_array_element_function(SNAME("TileMap"), callable_mp(this, &TileMapLayerEditor::_move_tile_map_array_element));
  3973. }
  3974. TileMapLayerEditor::~TileMapLayerEditor() {
  3975. for (int i = 0; i < tile_map_editor_plugins.size(); i++) {
  3976. memdelete(tile_map_editor_plugins[i]);
  3977. }
  3978. }