tile_map_layer_editor.cpp 189 KB

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