tile_map_editor.cpp 175 KB

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