tile_map_layer_editor.cpp 187 KB

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