tile_map_layer_editor.cpp 190 KB

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