tile_map_editor.cpp 177 KB

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