tile_map_editor.cpp 177 KB

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