tile_map_layer_editor.cpp 186 KB

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