tile_set_atlas_source_editor.cpp 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829
  1. /**************************************************************************/
  2. /* tile_set_atlas_source_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_set_atlas_source_editor.h"
  31. #include "tiles_editor_plugin.h"
  32. #include "editor/editor_inspector.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_undo_redo_manager.h"
  37. #include "editor/progress_dialog.h"
  38. #include "scene/gui/box_container.h"
  39. #include "scene/gui/button.h"
  40. #include "scene/gui/control.h"
  41. #include "scene/gui/item_list.h"
  42. #include "scene/gui/separator.h"
  43. #include "scene/gui/split_container.h"
  44. #include "scene/gui/tab_container.h"
  45. #include "core/core_string_names.h"
  46. #include "core/math/geometry_2d.h"
  47. #include "core/os/keyboard.h"
  48. void TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::set_id(int p_id) {
  49. ERR_FAIL_COND(p_id < 0);
  50. if (source_id == p_id) {
  51. return;
  52. }
  53. ERR_FAIL_COND_MSG(tile_set->has_source(p_id), vformat("Cannot change TileSet Atlas Source ID. Another source exists with id %d.", p_id));
  54. int previous_source = source_id;
  55. source_id = p_id; // source_id must be updated before, because it's used by the source list update.
  56. tile_set->set_source_id(previous_source, p_id);
  57. emit_signal(SNAME("changed"), "id");
  58. }
  59. int TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::get_id() const {
  60. return source_id;
  61. }
  62. bool TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::_set(const StringName &p_name, const Variant &p_value) {
  63. if (p_name == "id") {
  64. set_id(p_value);
  65. return true;
  66. }
  67. String name = p_name;
  68. if (name == "name") {
  69. // Use the resource_name property to store the source's name.
  70. name = "resource_name";
  71. }
  72. bool valid = false;
  73. tile_set_atlas_source->set(name, p_value, &valid);
  74. if (valid) {
  75. emit_signal(SNAME("changed"), String(name).utf8().get_data());
  76. }
  77. return valid;
  78. }
  79. bool TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::_get(const StringName &p_name, Variant &r_ret) const {
  80. if (!tile_set_atlas_source) {
  81. return false;
  82. }
  83. if (p_name == "id") {
  84. r_ret = get_id();
  85. return true;
  86. }
  87. String name = p_name;
  88. if (name == "name") {
  89. // Use the resource_name property to store the source's name.
  90. name = "resource_name";
  91. }
  92. bool valid = false;
  93. r_ret = tile_set_atlas_source->get(name, &valid);
  94. return valid;
  95. }
  96. void TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::_get_property_list(List<PropertyInfo> *p_list) const {
  97. p_list->push_back(PropertyInfo(Variant::NIL, TTR("Atlas"), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  98. p_list->push_back(PropertyInfo(Variant::INT, PNAME("id"), PROPERTY_HINT_RANGE, "0," + itos(INT_MAX) + ",1"));
  99. p_list->push_back(PropertyInfo(Variant::STRING, PNAME("name")));
  100. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("texture"), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"));
  101. p_list->push_back(PropertyInfo(Variant::VECTOR2I, PNAME("margins"), PROPERTY_HINT_NONE, "suffix:px"));
  102. p_list->push_back(PropertyInfo(Variant::VECTOR2I, PNAME("separation")));
  103. p_list->push_back(PropertyInfo(Variant::VECTOR2I, PNAME("texture_region_size"), PROPERTY_HINT_NONE, "suffix:px"));
  104. p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("use_texture_padding")));
  105. }
  106. void TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::_bind_methods() {
  107. ADD_SIGNAL(MethodInfo("changed", PropertyInfo(Variant::STRING, "what")));
  108. }
  109. void TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::edit(Ref<TileSet> p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id) {
  110. ERR_FAIL_COND(!p_tile_set_atlas_source);
  111. ERR_FAIL_COND(p_source_id < 0);
  112. ERR_FAIL_COND(p_tile_set.is_valid() && p_tile_set->get_source(p_source_id) != p_tile_set_atlas_source);
  113. if (p_tile_set == tile_set && p_tile_set_atlas_source == tile_set_atlas_source && p_source_id == source_id) {
  114. return;
  115. }
  116. // Disconnect to changes.
  117. if (tile_set_atlas_source) {
  118. tile_set_atlas_source->disconnect(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed));
  119. }
  120. tile_set = p_tile_set;
  121. tile_set_atlas_source = p_tile_set_atlas_source;
  122. source_id = p_source_id;
  123. // Connect to changes.
  124. if (tile_set_atlas_source) {
  125. if (!tile_set_atlas_source->is_connected(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed))) {
  126. tile_set_atlas_source->connect(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed));
  127. }
  128. }
  129. notify_property_list_changed();
  130. }
  131. // -- Proxy object used by the tile inspector --
  132. bool TileSetAtlasSourceEditor::AtlasTileProxyObject::_set(const StringName &p_name, const Variant &p_value) {
  133. if (!tile_set_atlas_source) {
  134. return false;
  135. }
  136. // ID and size related properties.
  137. if (tiles.size() == 1) {
  138. const Vector2i coords = tiles.front()->get().tile;
  139. const int &alternative = tiles.front()->get().alternative;
  140. if (alternative == 0) {
  141. Vector<String> components = String(p_name).split("/", true, 2);
  142. if (p_name == "atlas_coords") {
  143. Vector2i as_vector2i = Vector2i(p_value);
  144. bool has_room_for_tile = tile_set_atlas_source->has_room_for_tile(as_vector2i, tile_set_atlas_source->get_tile_size_in_atlas(coords), tile_set_atlas_source->get_tile_animation_columns(coords), tile_set_atlas_source->get_tile_animation_separation(coords), tile_set_atlas_source->get_tile_animation_frames_count(coords), coords);
  145. ERR_FAIL_COND_V_EDMSG(!has_room_for_tile, false, "Cannot move the tile, invalid coordinates or not enough room in the atlas for the tile and its animation frames.");
  146. if (tiles_set_atlas_source_editor->selection.front()->get().tile == coords) {
  147. tiles_set_atlas_source_editor->selection.clear();
  148. tiles_set_atlas_source_editor->selection.insert({ as_vector2i, 0 });
  149. tiles_set_atlas_source_editor->_update_tile_id_label();
  150. }
  151. tile_set_atlas_source->move_tile_in_atlas(coords, as_vector2i);
  152. tiles.clear();
  153. tiles.insert({ as_vector2i, 0 });
  154. emit_signal(SNAME("changed"), "atlas_coords");
  155. return true;
  156. } else if (p_name == "size_in_atlas") {
  157. Vector2i as_vector2i = Vector2i(p_value);
  158. bool has_room_for_tile = tile_set_atlas_source->has_room_for_tile(coords, as_vector2i, tile_set_atlas_source->get_tile_animation_columns(coords), tile_set_atlas_source->get_tile_animation_separation(coords), tile_set_atlas_source->get_tile_animation_frames_count(coords), coords);
  159. ERR_FAIL_COND_V_EDMSG(!has_room_for_tile, false, "Invalid size or not enough room in the atlas for the tile.");
  160. tile_set_atlas_source->move_tile_in_atlas(coords, TileSetSource::INVALID_ATLAS_COORDS, as_vector2i);
  161. emit_signal(SNAME("changed"), "size_in_atlas");
  162. return true;
  163. }
  164. } else if (alternative > 0) {
  165. if (p_name == "alternative_id") {
  166. int as_int = int(p_value);
  167. ERR_FAIL_COND_V(as_int < 0, false);
  168. ERR_FAIL_COND_V_MSG(tile_set_atlas_source->has_alternative_tile(coords, as_int), false, vformat("Cannot change alternative tile ID. Another alternative exists with id %d for tile at coords %s.", as_int, coords));
  169. if (tiles_set_atlas_source_editor->selection.front()->get().alternative == alternative) {
  170. tiles_set_atlas_source_editor->selection.clear();
  171. tiles_set_atlas_source_editor->selection.insert({ coords, as_int });
  172. }
  173. int previous_alternative_tile = alternative;
  174. tiles.clear();
  175. tiles.insert({ coords, as_int }); // tiles must be updated before.
  176. tile_set_atlas_source->set_alternative_tile_id(coords, previous_alternative_tile, as_int);
  177. emit_signal(SNAME("changed"), "alternative_id");
  178. return true;
  179. }
  180. }
  181. }
  182. // Animation.
  183. // Check if all tiles have an alternative_id of 0.
  184. bool all_alternatve_id_zero = true;
  185. for (TileSelection tile : tiles) {
  186. if (tile.alternative != 0) {
  187. all_alternatve_id_zero = false;
  188. break;
  189. }
  190. }
  191. if (all_alternatve_id_zero) {
  192. Vector<String> components = String(p_name).split("/", true, 2);
  193. if (p_name == "animation_columns") {
  194. for (TileSelection tile : tiles) {
  195. bool has_room_for_tile = tile_set_atlas_source->has_room_for_tile(tile.tile, tile_set_atlas_source->get_tile_size_in_atlas(tile.tile), p_value, tile_set_atlas_source->get_tile_animation_separation(tile.tile), tile_set_atlas_source->get_tile_animation_frames_count(tile.tile), tile.tile);
  196. if (!has_room_for_tile) {
  197. ERR_PRINT(vformat("Cannot change the number of columns to %s for tile animation. Not enough room in the atlas to layout %s frame(s).", p_value, tile_set_atlas_source->get_tile_animation_frames_count(tile.tile)));
  198. } else {
  199. tile_set_atlas_source->set_tile_animation_columns(tile.tile, p_value);
  200. }
  201. }
  202. emit_signal(SNAME("changed"), "animation_columns");
  203. return true;
  204. } else if (p_name == "animation_separation") {
  205. for (TileSelection tile : tiles) {
  206. bool has_room_for_tile = tile_set_atlas_source->has_room_for_tile(tile.tile, tile_set_atlas_source->get_tile_size_in_atlas(tile.tile), tile_set_atlas_source->get_tile_animation_columns(tile.tile), p_value, tile_set_atlas_source->get_tile_animation_frames_count(tile.tile), tile.tile);
  207. if (!has_room_for_tile) {
  208. ERR_PRINT(vformat("Cannot change separation between frames of the animation to %s. Not enough room in the atlas to layout %s frame(s).", p_value, tile_set_atlas_source->get_tile_animation_frames_count(tile.tile)));
  209. } else {
  210. tile_set_atlas_source->set_tile_animation_separation(tile.tile, p_value);
  211. }
  212. }
  213. emit_signal(SNAME("changed"), "animation_separation");
  214. return true;
  215. } else if (p_name == "animation_speed") {
  216. for (TileSelection tile : tiles) {
  217. tile_set_atlas_source->set_tile_animation_speed(tile.tile, p_value);
  218. }
  219. emit_signal(SNAME("changed"), "animation_speed");
  220. return true;
  221. } else if (p_name == "animation_frames_count") {
  222. for (TileSelection tile : tiles) {
  223. int frame_count = p_value;
  224. if (frame_count == 0) {
  225. frame_count = 1;
  226. }
  227. bool has_room_for_tile = tile_set_atlas_source->has_room_for_tile(tile.tile, tile_set_atlas_source->get_tile_size_in_atlas(tile.tile), tile_set_atlas_source->get_tile_animation_columns(tile.tile), tile_set_atlas_source->get_tile_animation_separation(tile.tile), frame_count, tile.tile);
  228. if (!has_room_for_tile) {
  229. ERR_PRINT(vformat("Cannot add frames to the animation, not enough room in the atlas to layout %s frames.", frame_count));
  230. } else {
  231. tile_set_atlas_source->set_tile_animation_frames_count(tile.tile, frame_count);
  232. }
  233. }
  234. notify_property_list_changed();
  235. emit_signal(SNAME("changed"), "animation_separation");
  236. return true;
  237. } else if (components.size() == 2 && components[0].begins_with("animation_frame_") && components[0].trim_prefix("animation_frame_").is_valid_int()) {
  238. for (TileSelection tile : tiles) {
  239. int frame = components[0].trim_prefix("animation_frame_").to_int();
  240. if (frame < 0 || frame >= tile_set_atlas_source->get_tile_animation_frames_count(tile.tile)) {
  241. ERR_PRINT(vformat("No tile animation frame with index %d", frame));
  242. } else {
  243. if (components[1] == "duration") {
  244. tile_set_atlas_source->set_tile_animation_frame_duration(tile.tile, frame, p_value);
  245. return true;
  246. }
  247. }
  248. }
  249. }
  250. }
  251. // Other properties.
  252. bool any_valid = false;
  253. for (const TileSelection &E : tiles) {
  254. const Vector2i &coords = E.tile;
  255. const int &alternative = E.alternative;
  256. bool valid = false;
  257. TileData *tile_data = tile_set_atlas_source->get_tile_data(coords, alternative);
  258. ERR_FAIL_COND_V(!tile_data, false);
  259. tile_data->set(p_name, p_value, &valid);
  260. any_valid |= valid;
  261. }
  262. if (any_valid) {
  263. emit_signal(SNAME("changed"), String(p_name).utf8().get_data());
  264. }
  265. return any_valid;
  266. }
  267. bool TileSetAtlasSourceEditor::AtlasTileProxyObject::_get(const StringName &p_name, Variant &r_ret) const {
  268. if (!tile_set_atlas_source) {
  269. return false;
  270. }
  271. // ID and size related properties.s
  272. if (tiles.size() == 1) {
  273. const Vector2i &coords = tiles.front()->get().tile;
  274. const int &alternative = tiles.front()->get().alternative;
  275. if (alternative == 0) {
  276. Vector<String> components = String(p_name).split("/", true, 2);
  277. if (p_name == "atlas_coords") {
  278. r_ret = coords;
  279. return true;
  280. } else if (p_name == "size_in_atlas") {
  281. r_ret = tile_set_atlas_source->get_tile_size_in_atlas(coords);
  282. return true;
  283. }
  284. } else if (alternative > 0) {
  285. if (p_name == "alternative_id") {
  286. r_ret = alternative;
  287. return true;
  288. }
  289. }
  290. }
  291. // Animation.
  292. // Check if all tiles have an alternative_id of 0.
  293. bool all_alternatve_id_zero = true;
  294. for (TileSelection tile : tiles) {
  295. if (tile.alternative != 0) {
  296. all_alternatve_id_zero = false;
  297. break;
  298. }
  299. }
  300. if (all_alternatve_id_zero) {
  301. const Vector2i &coords = tiles.front()->get().tile;
  302. Vector<String> components = String(p_name).split("/", true, 2);
  303. if (p_name == "animation_columns") {
  304. r_ret = tile_set_atlas_source->get_tile_animation_columns(coords);
  305. return true;
  306. } else if (p_name == "animation_separation") {
  307. r_ret = tile_set_atlas_source->get_tile_animation_separation(coords);
  308. return true;
  309. } else if (p_name == "animation_speed") {
  310. r_ret = tile_set_atlas_source->get_tile_animation_speed(coords);
  311. return true;
  312. } else if (p_name == "animation_frames_count") {
  313. r_ret = tile_set_atlas_source->get_tile_animation_frames_count(coords);
  314. return true;
  315. } else if (components.size() == 2 && components[0].begins_with("animation_frame_") && components[0].trim_prefix("animation_frame_").is_valid_int()) {
  316. int frame = components[0].trim_prefix("animation_frame_").to_int();
  317. if (components[1] == "duration") {
  318. if (frame < 0 || frame >= tile_set_atlas_source->get_tile_animation_frames_count(coords)) {
  319. return false;
  320. }
  321. r_ret = tile_set_atlas_source->get_tile_animation_frame_duration(coords, frame);
  322. return true;
  323. }
  324. }
  325. }
  326. for (const TileSelection &E : tiles) {
  327. // Return the first tile with a property matching the name.
  328. // Note: It's a little bit annoying, but the behavior is the same the one in MultiNodeEdit.
  329. const Vector2i &coords = E.tile;
  330. const int &alternative = E.alternative;
  331. TileData *tile_data = tile_set_atlas_source->get_tile_data(coords, alternative);
  332. ERR_FAIL_COND_V(!tile_data, false);
  333. bool valid = false;
  334. r_ret = tile_data->get(p_name, &valid);
  335. if (valid) {
  336. return true;
  337. }
  338. }
  339. return false;
  340. }
  341. void TileSetAtlasSourceEditor::AtlasTileProxyObject::_get_property_list(List<PropertyInfo> *p_list) const {
  342. if (!tile_set_atlas_source) {
  343. return;
  344. }
  345. // ID and size related properties.
  346. if (tiles.size() == 1) {
  347. if (tiles.front()->get().alternative == 0) {
  348. p_list->push_back(PropertyInfo(Variant::NIL, TTR("Base Tile"), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  349. p_list->push_back(PropertyInfo(Variant::VECTOR2I, PNAME("atlas_coords")));
  350. p_list->push_back(PropertyInfo(Variant::VECTOR2I, PNAME("size_in_atlas")));
  351. } else {
  352. p_list->push_back(PropertyInfo(Variant::NIL, TTR("Alternative Tile"), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  353. p_list->push_back(PropertyInfo(Variant::INT, PNAME("alternative_id")));
  354. }
  355. } else {
  356. p_list->push_back(PropertyInfo(Variant::NIL, TTR("Tiles"), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY));
  357. }
  358. // Animation.
  359. // Check if all tiles have an alternative_id of 0.
  360. bool all_alternatve_id_zero = true;
  361. for (TileSelection tile : tiles) {
  362. if (tile.alternative != 0) {
  363. all_alternatve_id_zero = false;
  364. break;
  365. }
  366. }
  367. if (all_alternatve_id_zero) {
  368. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Animation", "animation_"), PROPERTY_HINT_NONE, "animation_", PROPERTY_USAGE_GROUP));
  369. p_list->push_back(PropertyInfo(Variant::INT, PNAME("animation_columns")));
  370. p_list->push_back(PropertyInfo(Variant::VECTOR2I, PNAME("animation_separation")));
  371. p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("animation_speed")));
  372. p_list->push_back(PropertyInfo(Variant::INT, PNAME("animation_frames_count"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY, "Frames,animation_frame_"));
  373. // Not optimal, but returns value for the first tile. This is similar to what MultiNodeEdit does.
  374. if (tile_set_atlas_source->get_tile_animation_frames_count(tiles.front()->get().tile) == 1) {
  375. p_list->push_back(PropertyInfo(Variant::FLOAT, "animation_frame_0/duration", PROPERTY_HINT_NONE, "suffix:s", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY));
  376. } else {
  377. for (int i = 0; i < tile_set_atlas_source->get_tile_animation_frames_count(tiles.front()->get().tile); i++) {
  378. p_list->push_back(PropertyInfo(Variant::FLOAT, vformat("animation_frame_%d/%s", i, PNAME("duration")), PROPERTY_HINT_NONE, "suffix:s"));
  379. }
  380. }
  381. }
  382. // Get the list of properties common to all tiles (similar to what's done in MultiNodeEdit).
  383. struct PropertyId {
  384. int occurence_id = 0;
  385. String property;
  386. bool operator<(const PropertyId &p_other) const {
  387. return occurence_id == p_other.occurence_id ? property < p_other.property : occurence_id < p_other.occurence_id;
  388. }
  389. };
  390. struct PLData {
  391. int uses = 0;
  392. PropertyInfo property_info;
  393. };
  394. RBMap<PropertyId, PLData> usage;
  395. List<PLData *> data_list;
  396. for (const TileSelection &E : tiles) {
  397. const Vector2i &coords = E.tile;
  398. const int &alternative = E.alternative;
  399. TileData *tile_data = tile_set_atlas_source->get_tile_data(coords, alternative);
  400. ERR_FAIL_COND(!tile_data);
  401. List<PropertyInfo> list;
  402. tile_data->get_property_list(&list);
  403. HashMap<String, int> counts; // Counts the number of time a property appears (useful for groups that may appear more than once)
  404. for (List<PropertyInfo>::Element *E_property = list.front(); E_property; E_property = E_property->next()) {
  405. // Don't show category for TileData.
  406. if (E_property->get().usage & PROPERTY_USAGE_CATEGORY) {
  407. continue;
  408. }
  409. const String &property_string = E_property->get().name;
  410. if (!tile_data->is_allowing_transform() && (property_string == "flip_h" || property_string == "flip_v" || property_string == "transpose")) {
  411. continue;
  412. }
  413. if (!counts.has(property_string)) {
  414. counts[property_string] = 1;
  415. } else {
  416. counts[property_string] += 1;
  417. }
  418. PropertyInfo stored_property_info = E_property->get();
  419. stored_property_info.usage |= PROPERTY_USAGE_STORAGE; // Ignore the storage flag in comparing properties.
  420. PropertyId id = { counts[property_string], property_string };
  421. if (!usage.has(id)) {
  422. usage[id] = { 1, stored_property_info };
  423. data_list.push_back(&usage[id]);
  424. } else if (usage[id].property_info == stored_property_info) {
  425. usage[id].uses += 1;
  426. }
  427. }
  428. }
  429. // Add only properties that are common to all tiles.
  430. for (const PLData *E : data_list) {
  431. if (E->uses == tiles.size()) {
  432. p_list->push_back(E->property_info);
  433. }
  434. }
  435. }
  436. void TileSetAtlasSourceEditor::AtlasTileProxyObject::edit(TileSetAtlasSource *p_tile_set_atlas_source, RBSet<TileSelection> p_tiles) {
  437. ERR_FAIL_COND(!p_tile_set_atlas_source);
  438. ERR_FAIL_COND(p_tiles.is_empty());
  439. for (const TileSelection &E : p_tiles) {
  440. ERR_FAIL_COND(E.tile == TileSetSource::INVALID_ATLAS_COORDS);
  441. ERR_FAIL_COND(E.alternative < 0);
  442. }
  443. // Disconnect to changes.
  444. for (const TileSelection &E : tiles) {
  445. const Vector2i &coords = E.tile;
  446. const int &alternative = E.alternative;
  447. if (tile_set_atlas_source && tile_set_atlas_source->has_tile(coords) && tile_set_atlas_source->has_alternative_tile(coords, alternative)) {
  448. TileData *tile_data = tile_set_atlas_source->get_tile_data(coords, alternative);
  449. if (tile_data->is_connected(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed))) {
  450. tile_data->disconnect(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed));
  451. }
  452. }
  453. }
  454. tile_set_atlas_source = p_tile_set_atlas_source;
  455. tiles = RBSet<TileSelection>(p_tiles);
  456. // Connect to changes.
  457. for (const TileSelection &E : p_tiles) {
  458. const Vector2i &coords = E.tile;
  459. const int &alternative = E.alternative;
  460. if (tile_set_atlas_source->has_tile(coords) && tile_set_atlas_source->has_alternative_tile(coords, alternative)) {
  461. TileData *tile_data = tile_set_atlas_source->get_tile_data(coords, alternative);
  462. if (!tile_data->is_connected(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed))) {
  463. tile_data->connect(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed));
  464. }
  465. }
  466. }
  467. notify_property_list_changed();
  468. }
  469. void TileSetAtlasSourceEditor::AtlasTileProxyObject::_bind_methods() {
  470. ADD_SIGNAL(MethodInfo("changed", PropertyInfo(Variant::STRING, "what")));
  471. }
  472. void TileSetAtlasSourceEditor::_inspector_property_selected(String p_property) {
  473. selected_property = p_property;
  474. _update_atlas_view();
  475. _update_current_tile_data_editor();
  476. }
  477. void TileSetAtlasSourceEditor::_update_tile_id_label() {
  478. if (selection.size() == 1) {
  479. TileSelection selected = selection.front()->get();
  480. tool_tile_id_label->set_text(vformat("%d, %s, %d", tile_set_atlas_source_id, selected.tile, selected.alternative));
  481. tool_tile_id_label->set_tooltip_text(vformat(TTR("Selected tile:\nSource: %d\nAtlas coordinates: %s\nAlternative: %d"), tile_set_atlas_source_id, selected.tile, selected.alternative));
  482. tool_tile_id_label->show();
  483. } else {
  484. tool_tile_id_label->hide();
  485. }
  486. }
  487. void TileSetAtlasSourceEditor::_update_source_inspector() {
  488. // Update the proxy object.
  489. atlas_source_proxy_object->edit(tile_set, tile_set_atlas_source, tile_set_atlas_source_id);
  490. }
  491. void TileSetAtlasSourceEditor::_update_fix_selected_and_hovered_tiles() {
  492. // Fix selected.
  493. for (RBSet<TileSelection>::Element *E = selection.front(); E;) {
  494. RBSet<TileSelection>::Element *N = E->next();
  495. TileSelection selected = E->get();
  496. if (!tile_set_atlas_source->has_tile(selected.tile) || !tile_set_atlas_source->has_alternative_tile(selected.tile, selected.alternative)) {
  497. selection.erase(E);
  498. }
  499. E = N;
  500. }
  501. // Fix hovered.
  502. if (!tile_set_atlas_source->has_tile(hovered_base_tile_coords)) {
  503. hovered_base_tile_coords = TileSetSource::INVALID_ATLAS_COORDS;
  504. }
  505. Vector2i coords = Vector2i(hovered_alternative_tile_coords.x, hovered_alternative_tile_coords.y);
  506. int alternative = hovered_alternative_tile_coords.z;
  507. if (!tile_set_atlas_source->has_tile(coords) || !tile_set_atlas_source->has_alternative_tile(coords, alternative)) {
  508. hovered_alternative_tile_coords = Vector3i(TileSetSource::INVALID_ATLAS_COORDS.x, TileSetSource::INVALID_ATLAS_COORDS.y, TileSetSource::INVALID_TILE_ALTERNATIVE);
  509. }
  510. }
  511. void TileSetAtlasSourceEditor::_update_atlas_source_inspector() {
  512. // Update visibility.
  513. bool inspector_visible = tools_button_group->get_pressed_button() == tool_setup_atlas_source_button;
  514. atlas_source_inspector->set_visible(inspector_visible);
  515. atlas_source_inspector->set_read_only(read_only);
  516. }
  517. void TileSetAtlasSourceEditor::_update_tile_inspector() {
  518. // Update visibility.
  519. if (tools_button_group->get_pressed_button() == tool_select_button) {
  520. if (!selection.is_empty()) {
  521. tile_proxy_object->edit(tile_set_atlas_source, selection);
  522. }
  523. tile_inspector->set_visible(!selection.is_empty());
  524. tile_inspector_no_tile_selected_label->set_visible(selection.is_empty());
  525. } else {
  526. tile_inspector->hide();
  527. tile_inspector_no_tile_selected_label->hide();
  528. }
  529. tile_inspector->set_read_only(read_only);
  530. }
  531. void TileSetAtlasSourceEditor::_update_tile_data_editors() {
  532. String previously_selected;
  533. if (tile_data_editors_tree && tile_data_editors_tree->get_selected()) {
  534. previously_selected = tile_data_editors_tree->get_selected()->get_metadata(0);
  535. }
  536. tile_data_editors_tree->clear();
  537. if (tile_set.is_null()) {
  538. return;
  539. }
  540. TreeItem *root = tile_data_editors_tree->create_item();
  541. TreeItem *group;
  542. #define ADD_TILE_DATA_EDITOR_GROUP(text) \
  543. group = tile_data_editors_tree->create_item(root); \
  544. group->set_custom_bg_color(0, group_color); \
  545. group->set_selectable(0, false); \
  546. group->set_disable_folding(true); \
  547. group->set_text(0, text);
  548. TreeItem *item;
  549. #define ADD_TILE_DATA_EDITOR(parent, text, property) \
  550. item = tile_data_editors_tree->create_item(parent); \
  551. item->set_text(0, text); \
  552. item->set_metadata(0, property); \
  553. if (property == previously_selected) { \
  554. item->select(0); \
  555. }
  556. // Theming.
  557. tile_data_editors_tree->add_theme_constant_override("v_separation", 1);
  558. tile_data_editors_tree->add_theme_constant_override("h_separation", 3);
  559. Color group_color = get_theme_color(SNAME("prop_category"), SNAME("Editor"));
  560. // List of editors.
  561. // --- Rendering ---
  562. ADD_TILE_DATA_EDITOR_GROUP(TTR("Rendering"));
  563. ADD_TILE_DATA_EDITOR(group, TTR("Texture Origin"), "texture_origin");
  564. if (!tile_data_editors.has("texture_origin")) {
  565. TileDataTextureOriginEditor *tile_data_texture_origin_editor = memnew(TileDataTextureOriginEditor);
  566. tile_data_texture_origin_editor->hide();
  567. tile_data_texture_origin_editor->setup_property_editor(Variant::VECTOR2I, "texture_origin");
  568. tile_data_texture_origin_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  569. tile_data_texture_origin_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  570. tile_data_editors["texture_origin"] = tile_data_texture_origin_editor;
  571. }
  572. ADD_TILE_DATA_EDITOR(group, TTR("Modulate"), "modulate");
  573. if (!tile_data_editors.has("modulate")) {
  574. TileDataDefaultEditor *tile_data_modulate_editor = memnew(TileDataDefaultEditor());
  575. tile_data_modulate_editor->hide();
  576. tile_data_modulate_editor->setup_property_editor(Variant::COLOR, "modulate", "", Color(1.0, 1.0, 1.0, 1.0));
  577. tile_data_modulate_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  578. tile_data_modulate_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  579. tile_data_editors["modulate"] = tile_data_modulate_editor;
  580. }
  581. ADD_TILE_DATA_EDITOR(group, TTR("Z Index"), "z_index");
  582. if (!tile_data_editors.has("z_index")) {
  583. TileDataDefaultEditor *tile_data_z_index_editor = memnew(TileDataDefaultEditor());
  584. tile_data_z_index_editor->hide();
  585. tile_data_z_index_editor->setup_property_editor(Variant::INT, "z_index");
  586. tile_data_z_index_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  587. tile_data_z_index_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  588. tile_data_editors["z_index"] = tile_data_z_index_editor;
  589. }
  590. ADD_TILE_DATA_EDITOR(group, TTR("Y Sort Origin"), "y_sort_origin");
  591. if (!tile_data_editors.has("y_sort_origin")) {
  592. TileDataYSortEditor *tile_data_y_sort_editor = memnew(TileDataYSortEditor);
  593. tile_data_y_sort_editor->hide();
  594. tile_data_y_sort_editor->setup_property_editor(Variant::INT, "y_sort_origin");
  595. tile_data_y_sort_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  596. tile_data_y_sort_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  597. tile_data_editors["y_sort_origin"] = tile_data_y_sort_editor;
  598. }
  599. for (int i = 0; i < tile_set->get_occlusion_layers_count(); i++) {
  600. ADD_TILE_DATA_EDITOR(group, vformat(TTR("Occlusion Layer %d"), i), vformat("occlusion_layer_%d", i));
  601. if (!tile_data_editors.has(vformat("occlusion_layer_%d", i))) {
  602. TileDataOcclusionShapeEditor *tile_data_occlusion_shape_editor = memnew(TileDataOcclusionShapeEditor());
  603. tile_data_occlusion_shape_editor->hide();
  604. tile_data_occlusion_shape_editor->set_occlusion_layer(i);
  605. tile_data_occlusion_shape_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  606. tile_data_occlusion_shape_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  607. tile_data_editors[vformat("occlusion_layer_%d", i)] = tile_data_occlusion_shape_editor;
  608. }
  609. }
  610. for (int i = tile_set->get_occlusion_layers_count(); tile_data_editors.has(vformat("occlusion_layer_%d", i)); i++) {
  611. tile_data_editors[vformat("occlusion_layer_%d", i)]->queue_free();
  612. tile_data_editors.erase(vformat("occlusion_layer_%d", i));
  613. }
  614. // --- Rendering ---
  615. ADD_TILE_DATA_EDITOR(root, TTR("Terrains"), "terrain_set");
  616. if (!tile_data_editors.has("terrain_set")) {
  617. TileDataTerrainsEditor *tile_data_terrains_editor = memnew(TileDataTerrainsEditor);
  618. tile_data_terrains_editor->hide();
  619. tile_data_terrains_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  620. tile_data_terrains_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  621. tile_data_editors["terrain_set"] = tile_data_terrains_editor;
  622. }
  623. // --- Miscellaneous ---
  624. ADD_TILE_DATA_EDITOR(root, TTR("Probability"), "probability");
  625. if (!tile_data_editors.has("probability")) {
  626. TileDataDefaultEditor *tile_data_probability_editor = memnew(TileDataDefaultEditor());
  627. tile_data_probability_editor->hide();
  628. tile_data_probability_editor->setup_property_editor(Variant::FLOAT, "probability", "", 1.0);
  629. tile_data_probability_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  630. tile_data_probability_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  631. tile_data_editors["probability"] = tile_data_probability_editor;
  632. }
  633. // --- Physics ---
  634. ADD_TILE_DATA_EDITOR_GROUP(TTR("Physics"));
  635. for (int i = 0; i < tile_set->get_physics_layers_count(); i++) {
  636. ADD_TILE_DATA_EDITOR(group, vformat(TTR("Physics Layer %d"), i), vformat("physics_layer_%d", i));
  637. if (!tile_data_editors.has(vformat("physics_layer_%d", i))) {
  638. TileDataCollisionEditor *tile_data_collision_editor = memnew(TileDataCollisionEditor());
  639. tile_data_collision_editor->hide();
  640. tile_data_collision_editor->set_physics_layer(i);
  641. tile_data_collision_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  642. tile_data_collision_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  643. tile_data_editors[vformat("physics_layer_%d", i)] = tile_data_collision_editor;
  644. }
  645. }
  646. for (int i = tile_set->get_physics_layers_count(); tile_data_editors.has(vformat("physics_layer_%d", i)); i++) {
  647. tile_data_editors[vformat("physics_layer_%d", i)]->queue_free();
  648. tile_data_editors.erase(vformat("physics_layer_%d", i));
  649. }
  650. // --- Navigation ---
  651. ADD_TILE_DATA_EDITOR_GROUP(TTR("Navigation"));
  652. for (int i = 0; i < tile_set->get_navigation_layers_count(); i++) {
  653. ADD_TILE_DATA_EDITOR(group, vformat(TTR("Navigation Layer %d"), i), vformat("navigation_layer_%d", i));
  654. if (!tile_data_editors.has(vformat("navigation_layer_%d", i))) {
  655. TileDataNavigationEditor *tile_data_navigation_editor = memnew(TileDataNavigationEditor());
  656. tile_data_navigation_editor->hide();
  657. tile_data_navigation_editor->set_navigation_layer(i);
  658. tile_data_navigation_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  659. tile_data_navigation_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  660. tile_data_editors[vformat("navigation_layer_%d", i)] = tile_data_navigation_editor;
  661. }
  662. }
  663. for (int i = tile_set->get_navigation_layers_count(); tile_data_editors.has(vformat("navigation_layer_%d", i)); i++) {
  664. tile_data_editors[vformat("navigation_layer_%d", i)]->queue_free();
  665. tile_data_editors.erase(vformat("navigation_layer_%d", i));
  666. }
  667. // --- Custom Data ---
  668. ADD_TILE_DATA_EDITOR_GROUP(TTR("Custom Data"));
  669. for (int i = 0; i < tile_set->get_custom_data_layers_count(); i++) {
  670. String editor_name = vformat("custom_data_%d", i);
  671. String prop_name = tile_set->get_custom_data_layer_name(i);
  672. Variant::Type prop_type = tile_set->get_custom_data_layer_type(i);
  673. if (prop_name.is_empty()) {
  674. ADD_TILE_DATA_EDITOR(group, vformat(TTR("Custom Data %d"), i), editor_name);
  675. } else {
  676. ADD_TILE_DATA_EDITOR(group, prop_name, editor_name);
  677. }
  678. // If the type of the edited property has been changed, delete the
  679. // editor and create a new one.
  680. if (tile_data_editors.has(editor_name) && ((TileDataDefaultEditor *)tile_data_editors[editor_name])->get_property_type() != prop_type) {
  681. tile_data_editors[vformat("custom_data_%d", i)]->queue_free();
  682. tile_data_editors.erase(vformat("custom_data_%d", i));
  683. }
  684. if (!tile_data_editors.has(editor_name)) {
  685. TileDataDefaultEditor *tile_data_custom_data_editor = memnew(TileDataDefaultEditor());
  686. tile_data_custom_data_editor->hide();
  687. tile_data_custom_data_editor->setup_property_editor(prop_type, editor_name, prop_name);
  688. tile_data_custom_data_editor->connect("needs_redraw", callable_mp((CanvasItem *)tile_atlas_control_unscaled, &Control::queue_redraw));
  689. tile_data_custom_data_editor->connect("needs_redraw", callable_mp((CanvasItem *)alternative_tiles_control_unscaled, &Control::queue_redraw));
  690. tile_data_editors[editor_name] = tile_data_custom_data_editor;
  691. }
  692. }
  693. for (int i = tile_set->get_custom_data_layers_count(); tile_data_editors.has(vformat("custom_data_%d", i)); i++) {
  694. tile_data_editors[vformat("custom_data_%d", i)]->queue_free();
  695. tile_data_editors.erase(vformat("custom_data_%d", i));
  696. }
  697. #undef ADD_TILE_DATA_EDITOR_GROUP
  698. #undef ADD_TILE_DATA_EDITOR
  699. // Add tile data editors as children.
  700. for (KeyValue<String, TileDataEditor *> &E : tile_data_editors) {
  701. // Tile Data Editor.
  702. TileDataEditor *tile_data_editor = E.value;
  703. if (!tile_data_editor->is_inside_tree()) {
  704. tile_data_painting_editor_container->add_child(tile_data_editor);
  705. }
  706. tile_data_editor->set_tile_set(tile_set);
  707. // Toolbar.
  708. Control *toolbar = tile_data_editor->get_toolbar();
  709. if (!toolbar->is_inside_tree()) {
  710. tool_settings_tile_data_toolbar_container->add_child(toolbar);
  711. }
  712. toolbar->hide();
  713. }
  714. // Update visibility.
  715. bool is_visible = tools_button_group->get_pressed_button() == tool_paint_button;
  716. tile_data_editor_dropdown_button->set_visible(is_visible);
  717. if (tile_data_editors_tree->get_selected()) {
  718. tile_data_editor_dropdown_button->set_text(tile_data_editors_tree->get_selected()->get_text(0));
  719. } else {
  720. tile_data_editor_dropdown_button->set_text(TTR("Select a property editor"));
  721. }
  722. tile_data_editors_scroll->set_visible(is_visible);
  723. }
  724. void TileSetAtlasSourceEditor::_update_current_tile_data_editor() {
  725. // Find the property to use.
  726. String property;
  727. if (tools_button_group->get_pressed_button() == tool_select_button && tile_inspector->is_visible() && !tile_inspector->get_selected_path().is_empty()) {
  728. Vector<String> components = tile_inspector->get_selected_path().split("/");
  729. if (components.size() >= 1) {
  730. property = components[0];
  731. // Workaround for terrains as they don't have a common first component.
  732. if (property.begins_with("terrains_")) {
  733. property = "terrain_set";
  734. }
  735. }
  736. } else if (tools_button_group->get_pressed_button() == tool_paint_button && tile_data_editors_tree->get_selected()) {
  737. property = tile_data_editors_tree->get_selected()->get_metadata(0);
  738. tile_data_editor_dropdown_button->set_text(tile_data_editors_tree->get_selected()->get_text(0));
  739. }
  740. // Hide all editors but the current one.
  741. for (const KeyValue<String, TileDataEditor *> &E : tile_data_editors) {
  742. E.value->hide();
  743. E.value->get_toolbar()->hide();
  744. }
  745. if (tile_data_editors.has(property)) {
  746. current_tile_data_editor = tile_data_editors[property];
  747. } else {
  748. current_tile_data_editor = nullptr;
  749. }
  750. // Get the correct editor for the TileData's property.
  751. if (current_tile_data_editor) {
  752. current_tile_data_editor_toolbar = current_tile_data_editor->get_toolbar();
  753. current_property = property;
  754. current_tile_data_editor->set_visible(tools_button_group->get_pressed_button() == tool_paint_button);
  755. current_tile_data_editor_toolbar->set_visible(tools_button_group->get_pressed_button() == tool_paint_button);
  756. }
  757. }
  758. void TileSetAtlasSourceEditor::_tile_data_editor_dropdown_button_draw() {
  759. if (!has_theme_icon(SNAME("arrow"), SNAME("OptionButton"))) {
  760. return;
  761. }
  762. RID ci = tile_data_editor_dropdown_button->get_canvas_item();
  763. Ref<Texture2D> arrow = Control::get_theme_icon(SNAME("arrow"), SNAME("OptionButton"));
  764. Color clr = Color(1, 1, 1);
  765. if (get_theme_constant(SNAME("modulate_arrow"))) {
  766. switch (tile_data_editor_dropdown_button->get_draw_mode()) {
  767. case BaseButton::DRAW_PRESSED:
  768. clr = get_theme_color(SNAME("font_pressed_color"));
  769. break;
  770. case BaseButton::DRAW_HOVER:
  771. clr = get_theme_color(SNAME("font_hover_color"));
  772. break;
  773. case BaseButton::DRAW_DISABLED:
  774. clr = get_theme_color(SNAME("font_disabled_color"));
  775. break;
  776. default:
  777. if (tile_data_editor_dropdown_button->has_focus()) {
  778. clr = get_theme_color(SNAME("font_focus_color"));
  779. } else {
  780. clr = get_theme_color(SNAME("font_color"));
  781. }
  782. }
  783. }
  784. Size2 size = tile_data_editor_dropdown_button->get_size();
  785. Point2 ofs;
  786. if (is_layout_rtl()) {
  787. ofs = Point2(get_theme_constant(SNAME("arrow_margin"), SNAME("OptionButton")), int(Math::abs((size.height - arrow->get_height()) / 2)));
  788. } else {
  789. ofs = Point2(size.width - arrow->get_width() - get_theme_constant(SNAME("arrow_margin"), SNAME("OptionButton")), int(Math::abs((size.height - arrow->get_height()) / 2)));
  790. }
  791. arrow->draw(ci, ofs, clr);
  792. }
  793. void TileSetAtlasSourceEditor::_tile_data_editor_dropdown_button_pressed() {
  794. Size2 size = tile_data_editor_dropdown_button->get_size();
  795. tile_data_editors_popup->set_position(tile_data_editor_dropdown_button->get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y));
  796. tile_data_editors_popup->set_size(Size2(size.width, 0));
  797. tile_data_editors_popup->popup();
  798. }
  799. void TileSetAtlasSourceEditor::_tile_data_editors_tree_selected() {
  800. tile_data_editors_popup->call_deferred(SNAME("hide"));
  801. _update_current_tile_data_editor();
  802. tile_atlas_control->queue_redraw();
  803. tile_atlas_control_unscaled->queue_redraw();
  804. alternative_tiles_control->queue_redraw();
  805. alternative_tiles_control_unscaled->queue_redraw();
  806. }
  807. void TileSetAtlasSourceEditor::_update_atlas_view() {
  808. // Update the atlas display.
  809. tile_atlas_view->set_atlas_source(*tile_set, tile_set_atlas_source, tile_set_atlas_source_id);
  810. // Create a bunch of buttons to add alternative tiles.
  811. for (int i = 0; i < alternative_tiles_control->get_child_count(); i++) {
  812. alternative_tiles_control->get_child(i)->queue_free();
  813. }
  814. if (tile_set.is_null()) {
  815. return;
  816. }
  817. Vector2i pos;
  818. Vector2 texture_region_base_size = tile_set_atlas_source->get_texture_region_size();
  819. int texture_region_base_size_min = MIN(texture_region_base_size.x, texture_region_base_size.y);
  820. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  821. Vector2i tile_id = tile_set_atlas_source->get_tile_id(i);
  822. int alternative_count = tile_set_atlas_source->get_alternative_tiles_count(tile_id);
  823. if (alternative_count > 1) {
  824. // Compute the right extremity of alternative.
  825. int y_increment = 0;
  826. pos.x = 0;
  827. for (int j = 1; j < alternative_count; j++) {
  828. int alternative_id = tile_set_atlas_source->get_alternative_tile_id(tile_id, j);
  829. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(tile_id, alternative_id);
  830. pos.x = MAX(pos.x, rect.get_end().x);
  831. y_increment = MAX(y_increment, rect.size.y);
  832. }
  833. // Create and position the button.
  834. Button *button = memnew(Button);
  835. alternative_tiles_control->add_child(button);
  836. button->set_flat(true);
  837. button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  838. button->add_theme_style_override("normal", memnew(StyleBoxEmpty));
  839. button->add_theme_style_override("hover", memnew(StyleBoxEmpty));
  840. button->add_theme_style_override("focus", memnew(StyleBoxEmpty));
  841. button->add_theme_style_override("pressed", memnew(StyleBoxEmpty));
  842. button->connect("pressed", callable_mp(tile_set_atlas_source, &TileSetAtlasSource::create_alternative_tile).bind(tile_id, TileSetSource::INVALID_TILE_ALTERNATIVE));
  843. button->set_rect(Rect2(Vector2(pos.x, pos.y + (y_increment - texture_region_base_size.y) / 2.0), Vector2(texture_region_base_size_min, texture_region_base_size_min)));
  844. button->set_expand_icon(true);
  845. pos.y += y_increment;
  846. }
  847. }
  848. tile_atlas_view->set_padding(Side::SIDE_RIGHT, texture_region_base_size_min);
  849. // Redraw everything.
  850. tile_atlas_control->queue_redraw();
  851. tile_atlas_control_unscaled->queue_redraw();
  852. alternative_tiles_control->queue_redraw();
  853. alternative_tiles_control_unscaled->queue_redraw();
  854. tile_atlas_view->queue_redraw();
  855. // Synchronize atlas view.
  856. TilesEditorPlugin::get_singleton()->synchronize_atlas_view(tile_atlas_view);
  857. }
  858. void TileSetAtlasSourceEditor::_update_toolbar() {
  859. // Show the tools and settings.
  860. if (tools_button_group->get_pressed_button() == tool_setup_atlas_source_button) {
  861. if (current_tile_data_editor_toolbar) {
  862. current_tile_data_editor_toolbar->hide();
  863. }
  864. tools_settings_erase_button->show();
  865. tool_advanced_menu_button->show();
  866. } else if (tools_button_group->get_pressed_button() == tool_select_button) {
  867. if (current_tile_data_editor_toolbar) {
  868. current_tile_data_editor_toolbar->hide();
  869. }
  870. tools_settings_erase_button->hide();
  871. tool_advanced_menu_button->hide();
  872. } else if (tools_button_group->get_pressed_button() == tool_paint_button) {
  873. if (current_tile_data_editor_toolbar) {
  874. current_tile_data_editor_toolbar->show();
  875. }
  876. tools_settings_erase_button->hide();
  877. tool_advanced_menu_button->hide();
  878. }
  879. }
  880. void TileSetAtlasSourceEditor::_tile_atlas_control_mouse_exited() {
  881. hovered_base_tile_coords = TileSetSource::INVALID_ATLAS_COORDS;
  882. tile_atlas_control->queue_redraw();
  883. tile_atlas_control_unscaled->queue_redraw();
  884. tile_atlas_view->queue_redraw();
  885. }
  886. void TileSetAtlasSourceEditor::_tile_atlas_view_transform_changed() {
  887. tile_atlas_control->queue_redraw();
  888. tile_atlas_control_unscaled->queue_redraw();
  889. }
  890. void TileSetAtlasSourceEditor::_tile_atlas_control_gui_input(const Ref<InputEvent> &p_event) {
  891. // Update the hovered coords.
  892. hovered_base_tile_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  893. // Forward the event to the current tile data editor if we are in the painting mode.
  894. if (tools_button_group->get_pressed_button() == tool_paint_button) {
  895. if (current_tile_data_editor) {
  896. current_tile_data_editor->forward_painting_atlas_gui_input(tile_atlas_view, tile_set_atlas_source, p_event);
  897. }
  898. // Update only what's needed.
  899. tile_set_changed_needs_update = false;
  900. tile_atlas_control->queue_redraw();
  901. tile_atlas_control_unscaled->queue_redraw();
  902. alternative_tiles_control->queue_redraw();
  903. alternative_tiles_control_unscaled->queue_redraw();
  904. tile_atlas_view->queue_redraw();
  905. return;
  906. } else {
  907. // Handle the event.
  908. Ref<InputEventMouseMotion> mm = p_event;
  909. if (mm.is_valid()) {
  910. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos, true);
  911. Vector2i last_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_last_mouse_pos, true);
  912. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  913. Vector2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  914. if (drag_type == DRAG_TYPE_CREATE_BIG_TILE) {
  915. // Create big tile.
  916. new_base_tiles_coords = new_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  917. Rect2i new_rect = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  918. new_rect.size += Vector2i(1, 1);
  919. // Check if the new tile can fit in the new rect.
  920. if (tile_set_atlas_source->has_room_for_tile(new_rect.position, new_rect.size, tile_set_atlas_source->get_tile_animation_columns(drag_current_tile), tile_set_atlas_source->get_tile_animation_separation(drag_current_tile), tile_set_atlas_source->get_tile_animation_frames_count(drag_current_tile), drag_current_tile)) {
  921. // Move and resize the tile.
  922. tile_set_atlas_source->move_tile_in_atlas(drag_current_tile, new_rect.position, new_rect.size);
  923. drag_current_tile = new_rect.position;
  924. }
  925. } else if (drag_type == DRAG_TYPE_CREATE_TILES) {
  926. // Create tiles.
  927. last_base_tiles_coords = last_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  928. new_base_tiles_coords = new_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  929. Vector<Point2i> line = Geometry2D::bresenham_line(last_base_tiles_coords, new_base_tiles_coords);
  930. for (int i = 0; i < line.size(); i++) {
  931. if (tile_set_atlas_source->get_tile_at_coords(line[i]) == TileSetSource::INVALID_ATLAS_COORDS) {
  932. tile_set_atlas_source->create_tile(line[i]);
  933. drag_modified_tiles.insert(line[i]);
  934. }
  935. }
  936. drag_last_mouse_pos = tile_atlas_control->get_local_mouse_position();
  937. } else if (drag_type == DRAG_TYPE_REMOVE_TILES) {
  938. // Remove tiles.
  939. last_base_tiles_coords = last_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  940. new_base_tiles_coords = new_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  941. Vector<Point2i> line = Geometry2D::bresenham_line(last_base_tiles_coords, new_base_tiles_coords);
  942. for (int i = 0; i < line.size(); i++) {
  943. Vector2i base_tile_coords = tile_set_atlas_source->get_tile_at_coords(line[i]);
  944. if (base_tile_coords != TileSetSource::INVALID_ATLAS_COORDS) {
  945. drag_modified_tiles.insert(base_tile_coords);
  946. }
  947. }
  948. drag_last_mouse_pos = tile_atlas_control->get_local_mouse_position();
  949. } else if (drag_type == DRAG_TYPE_MOVE_TILE) {
  950. // Move tile.
  951. Vector2 mouse_offset = (Vector2(tile_set_atlas_source->get_tile_size_in_atlas(drag_current_tile)) / 2.0 - Vector2(0.5, 0.5)) * tile_set->get_tile_size();
  952. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position() - mouse_offset);
  953. if (drag_current_tile != coords && tile_set_atlas_source->has_room_for_tile(coords, tile_set_atlas_source->get_tile_size_in_atlas(drag_current_tile), tile_set_atlas_source->get_tile_animation_columns(drag_current_tile), tile_set_atlas_source->get_tile_animation_separation(drag_current_tile), tile_set_atlas_source->get_tile_animation_frames_count(drag_current_tile), drag_current_tile)) {
  954. tile_set_atlas_source->move_tile_in_atlas(drag_current_tile, coords);
  955. selection.clear();
  956. selection.insert({ coords, 0 });
  957. drag_current_tile = coords;
  958. // Update only what's needed.
  959. tile_set_changed_needs_update = false;
  960. _update_tile_inspector();
  961. _update_atlas_view();
  962. _update_tile_id_label();
  963. _update_current_tile_data_editor();
  964. }
  965. } else if (drag_type == DRAG_TYPE_MAY_POPUP_MENU) {
  966. if (Vector2(drag_start_mouse_pos).distance_to(tile_atlas_control->get_local_mouse_position()) > 5.0 * EDSCALE) {
  967. drag_type = DRAG_TYPE_NONE;
  968. }
  969. } else if (drag_type >= DRAG_TYPE_RESIZE_TOP_LEFT && drag_type <= DRAG_TYPE_RESIZE_LEFT) {
  970. // Resizing a tile.
  971. new_base_tiles_coords = new_base_tiles_coords.max(Vector2i(-1, -1)).min(grid_size);
  972. Rect2i old_rect = Rect2i(drag_current_tile, tile_set_atlas_source->get_tile_size_in_atlas(drag_current_tile));
  973. Rect2i new_rect = old_rect;
  974. if (drag_type == DRAG_TYPE_RESIZE_LEFT || drag_type == DRAG_TYPE_RESIZE_TOP_LEFT || drag_type == DRAG_TYPE_RESIZE_BOTTOM_LEFT) {
  975. new_rect.position.x = MIN(new_base_tiles_coords.x + 1, old_rect.get_end().x - 1);
  976. new_rect.size.x = old_rect.get_end().x - new_rect.position.x;
  977. }
  978. if (drag_type == DRAG_TYPE_RESIZE_TOP || drag_type == DRAG_TYPE_RESIZE_TOP_LEFT || drag_type == DRAG_TYPE_RESIZE_TOP_RIGHT) {
  979. new_rect.position.y = MIN(new_base_tiles_coords.y + 1, old_rect.get_end().y - 1);
  980. new_rect.size.y = old_rect.get_end().y - new_rect.position.y;
  981. }
  982. if (drag_type == DRAG_TYPE_RESIZE_RIGHT || drag_type == DRAG_TYPE_RESIZE_TOP_RIGHT || drag_type == DRAG_TYPE_RESIZE_BOTTOM_RIGHT) {
  983. new_rect.set_end(Vector2i(MAX(new_base_tiles_coords.x, old_rect.position.x + 1), new_rect.get_end().y));
  984. }
  985. if (drag_type == DRAG_TYPE_RESIZE_BOTTOM || drag_type == DRAG_TYPE_RESIZE_BOTTOM_LEFT || drag_type == DRAG_TYPE_RESIZE_BOTTOM_RIGHT) {
  986. new_rect.set_end(Vector2i(new_rect.get_end().x, MAX(new_base_tiles_coords.y, old_rect.position.y + 1)));
  987. }
  988. if (tile_set_atlas_source->has_room_for_tile(new_rect.position, new_rect.size, tile_set_atlas_source->get_tile_animation_columns(drag_current_tile), tile_set_atlas_source->get_tile_animation_separation(drag_current_tile), tile_set_atlas_source->get_tile_animation_frames_count(drag_current_tile), drag_current_tile)) {
  989. tile_set_atlas_source->move_tile_in_atlas(drag_current_tile, new_rect.position, new_rect.size);
  990. selection.clear();
  991. selection.insert({ new_rect.position, 0 });
  992. drag_current_tile = new_rect.position;
  993. // Update only what's needed.
  994. tile_set_changed_needs_update = false;
  995. _update_tile_inspector();
  996. _update_atlas_view();
  997. _update_tile_id_label();
  998. _update_current_tile_data_editor();
  999. }
  1000. }
  1001. // Redraw for the hovered tile.
  1002. tile_atlas_control->queue_redraw();
  1003. tile_atlas_control_unscaled->queue_redraw();
  1004. alternative_tiles_control->queue_redraw();
  1005. alternative_tiles_control_unscaled->queue_redraw();
  1006. tile_atlas_view->queue_redraw();
  1007. return;
  1008. }
  1009. Ref<InputEventMouseButton> mb = p_event;
  1010. if (mb.is_valid()) {
  1011. Vector2 mouse_local_pos = tile_atlas_control->get_local_mouse_position();
  1012. if (mb->get_button_index() == MouseButton::LEFT) {
  1013. if (mb->is_pressed()) {
  1014. // Left click pressed.
  1015. if (tools_button_group->get_pressed_button() == tool_setup_atlas_source_button) {
  1016. if (tools_settings_erase_button->is_pressed()) {
  1017. // Erasing
  1018. if (mb->is_ctrl_pressed() || mb->is_shift_pressed()) {
  1019. // Remove tiles using rect.
  1020. // Setup the dragging info.
  1021. drag_type = DRAG_TYPE_REMOVE_TILES_USING_RECT;
  1022. drag_start_mouse_pos = mouse_local_pos;
  1023. drag_last_mouse_pos = drag_start_mouse_pos;
  1024. } else {
  1025. // Remove tiles.
  1026. // Setup the dragging info.
  1027. drag_type = DRAG_TYPE_REMOVE_TILES;
  1028. drag_start_mouse_pos = mouse_local_pos;
  1029. drag_last_mouse_pos = drag_start_mouse_pos;
  1030. // Remove a first tile.
  1031. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  1032. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1033. coords = tile_set_atlas_source->get_tile_at_coords(coords);
  1034. }
  1035. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1036. drag_modified_tiles.insert(coords);
  1037. }
  1038. }
  1039. } else {
  1040. // Creating
  1041. if (mb->is_shift_pressed()) {
  1042. // Create a big tile.
  1043. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(mouse_local_pos);
  1044. if (coords != TileSetSource::INVALID_ATLAS_COORDS && tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
  1045. // Setup the dragging info, only if we start on an empty tile.
  1046. drag_type = DRAG_TYPE_CREATE_BIG_TILE;
  1047. drag_start_mouse_pos = mouse_local_pos;
  1048. drag_last_mouse_pos = drag_start_mouse_pos;
  1049. drag_current_tile = coords;
  1050. // Create a tile.
  1051. tile_set_atlas_source->create_tile(coords);
  1052. }
  1053. } else if (mb->is_ctrl_pressed()) {
  1054. // Create tiles using rect.
  1055. drag_type = DRAG_TYPE_CREATE_TILES_USING_RECT;
  1056. drag_start_mouse_pos = mouse_local_pos;
  1057. drag_last_mouse_pos = drag_start_mouse_pos;
  1058. } else {
  1059. // Create tiles.
  1060. // Setup the dragging info.
  1061. drag_type = DRAG_TYPE_CREATE_TILES;
  1062. drag_start_mouse_pos = mouse_local_pos;
  1063. drag_last_mouse_pos = drag_start_mouse_pos;
  1064. // Create a first tile if needed.
  1065. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  1066. if (coords != TileSetSource::INVALID_ATLAS_COORDS && tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
  1067. tile_set_atlas_source->create_tile(coords);
  1068. drag_modified_tiles.insert(coords);
  1069. }
  1070. }
  1071. }
  1072. } else if (tools_button_group->get_pressed_button() == tool_select_button) {
  1073. // Dragging a handle.
  1074. drag_type = DRAG_TYPE_NONE;
  1075. if (selection.size() == 1) {
  1076. TileSelection selected = selection.front()->get();
  1077. if (selected.tile != TileSetSource::INVALID_ATLAS_COORDS && selected.alternative == 0) {
  1078. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(selected.tile);
  1079. Rect2 region = tile_set_atlas_source->get_tile_texture_region(selected.tile);
  1080. Size2 zoomed_size = resize_handle->get_size() / tile_atlas_view->get_zoom();
  1081. Rect2 rect = region.grow_individual(zoomed_size.x, zoomed_size.y, 0, 0);
  1082. const Vector2i coords[] = { Vector2i(0, 0), Vector2i(1, 0), Vector2i(1, 1), Vector2i(0, 1) };
  1083. const Vector2i directions[] = { Vector2i(0, -1), Vector2i(1, 0), Vector2i(0, 1), Vector2i(-1, 0) };
  1084. bool can_grow[4];
  1085. for (int i = 0; i < 4; i++) {
  1086. can_grow[i] = tile_set_atlas_source->has_room_for_tile(selected.tile + directions[i], tile_set_atlas_source->get_tile_size_in_atlas(selected.tile), tile_set_atlas_source->get_tile_animation_columns(selected.tile), tile_set_atlas_source->get_tile_animation_separation(selected.tile), tile_set_atlas_source->get_tile_animation_frames_count(selected.tile), selected.tile);
  1087. can_grow[i] |= (i % 2 == 0) ? size_in_atlas.y > 1 : size_in_atlas.x > 1;
  1088. }
  1089. for (int i = 0; i < 4; i++) {
  1090. Vector2 pos = rect.position + rect.size * coords[i];
  1091. if (can_grow[i] && can_grow[(i + 3) % 4] && Rect2(pos, zoomed_size).has_point(mouse_local_pos)) {
  1092. drag_type = (DragType)((int)DRAG_TYPE_RESIZE_TOP_LEFT + i * 2);
  1093. drag_start_mouse_pos = mouse_local_pos;
  1094. drag_last_mouse_pos = drag_start_mouse_pos;
  1095. drag_current_tile = selected.tile;
  1096. drag_start_tile_shape = Rect2i(selected.tile, tile_set_atlas_source->get_tile_size_in_atlas(selected.tile));
  1097. }
  1098. Vector2 next_pos = rect.position + rect.size * coords[(i + 1) % 4];
  1099. if (can_grow[i] && Rect2((pos + next_pos) / 2.0, zoomed_size).has_point(mouse_local_pos)) {
  1100. drag_type = (DragType)((int)DRAG_TYPE_RESIZE_TOP + i * 2);
  1101. drag_start_mouse_pos = mouse_local_pos;
  1102. drag_last_mouse_pos = drag_start_mouse_pos;
  1103. drag_current_tile = selected.tile;
  1104. drag_start_tile_shape = Rect2i(selected.tile, tile_set_atlas_source->get_tile_size_in_atlas(selected.tile));
  1105. }
  1106. }
  1107. }
  1108. }
  1109. // Selecting then dragging a tile.
  1110. if (drag_type == DRAG_TYPE_NONE) {
  1111. TileSelection selected = { TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE };
  1112. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(mouse_local_pos);
  1113. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1114. coords = tile_set_atlas_source->get_tile_at_coords(coords);
  1115. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1116. selected = { coords, 0 };
  1117. }
  1118. }
  1119. bool shift = mb->is_shift_pressed();
  1120. if (!shift && selection.size() == 1 && selected.tile != TileSetSource::INVALID_ATLAS_COORDS && selection.has(selected)) {
  1121. // Start move dragging.
  1122. drag_type = DRAG_TYPE_MOVE_TILE;
  1123. drag_start_mouse_pos = mouse_local_pos;
  1124. drag_last_mouse_pos = drag_start_mouse_pos;
  1125. drag_current_tile = selected.tile;
  1126. drag_start_tile_shape = Rect2i(selected.tile, tile_set_atlas_source->get_tile_size_in_atlas(selected.tile));
  1127. } else {
  1128. // Start selection dragging.
  1129. drag_type = DRAG_TYPE_RECT_SELECT;
  1130. drag_start_mouse_pos = mouse_local_pos;
  1131. drag_last_mouse_pos = drag_start_mouse_pos;
  1132. }
  1133. }
  1134. }
  1135. } else {
  1136. // Left click released.
  1137. _end_dragging();
  1138. }
  1139. tile_atlas_control->queue_redraw();
  1140. tile_atlas_control_unscaled->queue_redraw();
  1141. alternative_tiles_control->queue_redraw();
  1142. alternative_tiles_control_unscaled->queue_redraw();
  1143. tile_atlas_view->queue_redraw();
  1144. return;
  1145. } else if (mb->get_button_index() == MouseButton::RIGHT) {
  1146. // Right click pressed.
  1147. if (mb->is_pressed()) {
  1148. drag_type = DRAG_TYPE_MAY_POPUP_MENU;
  1149. drag_start_mouse_pos = tile_atlas_control->get_local_mouse_position();
  1150. } else {
  1151. // Right click released.
  1152. _end_dragging();
  1153. }
  1154. tile_atlas_control->queue_redraw();
  1155. tile_atlas_control_unscaled->queue_redraw();
  1156. alternative_tiles_control->queue_redraw();
  1157. alternative_tiles_control_unscaled->queue_redraw();
  1158. tile_atlas_view->queue_redraw();
  1159. return;
  1160. }
  1161. }
  1162. }
  1163. }
  1164. void TileSetAtlasSourceEditor::_end_dragging() {
  1165. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1166. switch (drag_type) {
  1167. case DRAG_TYPE_CREATE_TILES:
  1168. undo_redo->create_action(TTR("Create tiles"));
  1169. for (const Vector2i &E : drag_modified_tiles) {
  1170. undo_redo->add_do_method(tile_set_atlas_source, "create_tile", E);
  1171. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", E);
  1172. }
  1173. undo_redo->commit_action(false);
  1174. break;
  1175. case DRAG_TYPE_CREATE_BIG_TILE:
  1176. undo_redo->create_action(TTR("Create a tile"));
  1177. undo_redo->add_do_method(tile_set_atlas_source, "create_tile", drag_current_tile, tile_set_atlas_source->get_tile_size_in_atlas(drag_current_tile));
  1178. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", drag_current_tile);
  1179. undo_redo->commit_action(false);
  1180. break;
  1181. case DRAG_TYPE_REMOVE_TILES: {
  1182. List<PropertyInfo> list;
  1183. tile_set_atlas_source->get_property_list(&list);
  1184. HashMap<Vector2i, List<const PropertyInfo *>> per_tile = _group_properties_per_tiles(list, tile_set_atlas_source);
  1185. undo_redo->create_action(TTR("Remove tiles"));
  1186. for (const Vector2i &E : drag_modified_tiles) {
  1187. Vector2i coords = E;
  1188. undo_redo->add_do_method(tile_set_atlas_source, "remove_tile", coords);
  1189. undo_redo->add_undo_method(tile_set_atlas_source, "create_tile", coords);
  1190. if (per_tile.has(coords)) {
  1191. for (List<const PropertyInfo *>::Element *E_property = per_tile[coords].front(); E_property; E_property = E_property->next()) {
  1192. String property = E_property->get()->name;
  1193. Variant value = tile_set_atlas_source->get(property);
  1194. if (value.get_type() != Variant::NIL) {
  1195. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  1196. }
  1197. }
  1198. }
  1199. }
  1200. undo_redo->commit_action();
  1201. } break;
  1202. case DRAG_TYPE_CREATE_TILES_USING_RECT: {
  1203. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos, true);
  1204. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1205. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  1206. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  1207. undo_redo->create_action(TTR("Create tiles"));
  1208. for (int x = area.get_position().x; x < area.get_end().x; x++) {
  1209. for (int y = area.get_position().y; y < area.get_end().y; y++) {
  1210. Vector2i coords = Vector2i(x, y);
  1211. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
  1212. undo_redo->add_do_method(tile_set_atlas_source, "create_tile", coords);
  1213. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", coords);
  1214. }
  1215. }
  1216. }
  1217. undo_redo->commit_action();
  1218. } break;
  1219. case DRAG_TYPE_REMOVE_TILES_USING_RECT: {
  1220. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos, true);
  1221. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1222. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  1223. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  1224. List<PropertyInfo> list;
  1225. tile_set_atlas_source->get_property_list(&list);
  1226. HashMap<Vector2i, List<const PropertyInfo *>> per_tile = _group_properties_per_tiles(list, tile_set_atlas_source);
  1227. RBSet<Vector2i> to_delete;
  1228. for (int x = area.get_position().x; x < area.get_end().x; x++) {
  1229. for (int y = area.get_position().y; y < area.get_end().y; y++) {
  1230. Vector2i coords = tile_set_atlas_source->get_tile_at_coords(Vector2i(x, y));
  1231. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1232. to_delete.insert(coords);
  1233. }
  1234. }
  1235. }
  1236. undo_redo->create_action(TTR("Remove tiles"));
  1237. undo_redo->add_do_method(this, "_set_selection_from_array", Array());
  1238. for (const Vector2i &E : to_delete) {
  1239. Vector2i coords = E;
  1240. undo_redo->add_do_method(tile_set_atlas_source, "remove_tile", coords);
  1241. undo_redo->add_undo_method(tile_set_atlas_source, "create_tile", coords);
  1242. if (per_tile.has(coords)) {
  1243. for (List<const PropertyInfo *>::Element *E_property = per_tile[coords].front(); E_property; E_property = E_property->next()) {
  1244. String property = E_property->get()->name;
  1245. Variant value = tile_set_atlas_source->get(property);
  1246. if (value.get_type() != Variant::NIL) {
  1247. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  1248. }
  1249. }
  1250. }
  1251. }
  1252. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  1253. undo_redo->commit_action();
  1254. } break;
  1255. case DRAG_TYPE_MOVE_TILE:
  1256. if (drag_current_tile != drag_start_tile_shape.position) {
  1257. undo_redo->create_action(TTR("Move a tile"));
  1258. undo_redo->add_do_method(tile_set_atlas_source, "move_tile_in_atlas", drag_start_tile_shape.position, drag_current_tile, tile_set_atlas_source->get_tile_size_in_atlas(drag_current_tile));
  1259. undo_redo->add_do_method(this, "_set_selection_from_array", _get_selection_as_array());
  1260. undo_redo->add_undo_method(tile_set_atlas_source, "move_tile_in_atlas", drag_current_tile, drag_start_tile_shape.position, drag_start_tile_shape.size);
  1261. Array array;
  1262. array.push_back(drag_start_tile_shape.position);
  1263. array.push_back(0);
  1264. undo_redo->add_undo_method(this, "_set_selection_from_array", array);
  1265. undo_redo->commit_action(false);
  1266. }
  1267. break;
  1268. case DRAG_TYPE_RECT_SELECT: {
  1269. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos, true);
  1270. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1271. ERR_FAIL_COND(start_base_tiles_coords == TileSetSource::INVALID_ATLAS_COORDS);
  1272. ERR_FAIL_COND(new_base_tiles_coords == TileSetSource::INVALID_ATLAS_COORDS);
  1273. Rect2i region = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  1274. region.size += Vector2i(1, 1);
  1275. undo_redo->create_action(TTR("Select tiles"));
  1276. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  1277. // Determine if we clear, then add or remove to the selection.
  1278. bool add_to_selection = true;
  1279. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  1280. Vector2i coords = tile_set_atlas_source->get_tile_at_coords(start_base_tiles_coords);
  1281. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1282. if (selection.has({ coords, 0 })) {
  1283. add_to_selection = false;
  1284. }
  1285. }
  1286. } else {
  1287. selection.clear();
  1288. }
  1289. // Modify the selection.
  1290. for (int x = region.position.x; x < region.get_end().x; x++) {
  1291. for (int y = region.position.y; y < region.get_end().y; y++) {
  1292. Vector2i coords = Vector2i(x, y);
  1293. coords = tile_set_atlas_source->get_tile_at_coords(coords);
  1294. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1295. if (add_to_selection && !selection.has({ coords, 0 })) {
  1296. selection.insert({ coords, 0 });
  1297. } else if (!add_to_selection && selection.has({ coords, 0 })) {
  1298. selection.erase({ coords, 0 });
  1299. }
  1300. }
  1301. }
  1302. }
  1303. _update_tile_inspector();
  1304. _update_tile_id_label();
  1305. _update_current_tile_data_editor();
  1306. undo_redo->add_do_method(this, "_set_selection_from_array", _get_selection_as_array());
  1307. undo_redo->commit_action(false);
  1308. } break;
  1309. case DRAG_TYPE_MAY_POPUP_MENU: {
  1310. Vector2 mouse_local_pos = tile_atlas_control->get_local_mouse_position();
  1311. TileSelection selected = { tile_atlas_view->get_atlas_tile_coords_at_pos(mouse_local_pos), 0 };
  1312. if (selected.tile != TileSetSource::INVALID_ATLAS_COORDS) {
  1313. selected.tile = tile_set_atlas_source->get_tile_at_coords(selected.tile);
  1314. }
  1315. // Set the selection if needed.
  1316. if (selection.size() <= 1) {
  1317. if (selected.tile != TileSetSource::INVALID_ATLAS_COORDS) {
  1318. undo_redo->create_action(TTR("Select tiles"));
  1319. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  1320. selection.clear();
  1321. selection.insert(selected);
  1322. undo_redo->add_do_method(this, "_set_selection_from_array", _get_selection_as_array());
  1323. undo_redo->commit_action(false);
  1324. _update_tile_inspector();
  1325. _update_tile_id_label();
  1326. _update_current_tile_data_editor();
  1327. }
  1328. }
  1329. // Pops up the correct menu, depending on whether we have a tile or not.
  1330. if (selected.tile != TileSetSource::INVALID_ATLAS_COORDS && selection.has(selected)) {
  1331. // We have a tile.
  1332. menu_option_coords = selected.tile;
  1333. menu_option_alternative = 0;
  1334. base_tile_popup_menu->popup(Rect2i(get_screen_transform().xform(get_local_mouse_position()), Size2i()));
  1335. } else if (hovered_base_tile_coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1336. // We don't have a tile, but can create one.
  1337. menu_option_coords = hovered_base_tile_coords;
  1338. menu_option_alternative = TileSetSource::INVALID_TILE_ALTERNATIVE;
  1339. empty_base_tile_popup_menu->popup(Rect2i(get_screen_transform().xform(get_local_mouse_position()), Size2i()));
  1340. }
  1341. } break;
  1342. case DRAG_TYPE_RESIZE_TOP_LEFT:
  1343. case DRAG_TYPE_RESIZE_TOP:
  1344. case DRAG_TYPE_RESIZE_TOP_RIGHT:
  1345. case DRAG_TYPE_RESIZE_RIGHT:
  1346. case DRAG_TYPE_RESIZE_BOTTOM_RIGHT:
  1347. case DRAG_TYPE_RESIZE_BOTTOM:
  1348. case DRAG_TYPE_RESIZE_BOTTOM_LEFT:
  1349. case DRAG_TYPE_RESIZE_LEFT:
  1350. if (drag_start_tile_shape != Rect2i(drag_current_tile, tile_set_atlas_source->get_tile_size_in_atlas(drag_current_tile))) {
  1351. undo_redo->create_action(TTR("Resize a tile"));
  1352. undo_redo->add_do_method(tile_set_atlas_source, "move_tile_in_atlas", drag_start_tile_shape.position, drag_current_tile, tile_set_atlas_source->get_tile_size_in_atlas(drag_current_tile));
  1353. undo_redo->add_do_method(this, "_set_selection_from_array", _get_selection_as_array());
  1354. undo_redo->add_undo_method(tile_set_atlas_source, "move_tile_in_atlas", drag_current_tile, drag_start_tile_shape.position, drag_start_tile_shape.size);
  1355. Array array;
  1356. array.push_back(drag_start_tile_shape.position);
  1357. array.push_back(0);
  1358. undo_redo->add_undo_method(this, "_set_selection_from_array", array);
  1359. undo_redo->commit_action(false);
  1360. }
  1361. break;
  1362. default:
  1363. break;
  1364. }
  1365. drag_modified_tiles.clear();
  1366. drag_type = DRAG_TYPE_NONE;
  1367. // Change mouse accordingly.
  1368. }
  1369. Control::CursorShape TileSetAtlasSourceEditor::get_cursor_shape(const Point2 &p_pos) const {
  1370. Control::CursorShape cursor_shape = get_default_cursor_shape();
  1371. if (drag_type == DRAG_TYPE_NONE) {
  1372. if (selection.size() == 1) {
  1373. // Change the cursor depending on the hovered thing.
  1374. TileSelection selected = selection.front()->get();
  1375. if (selected.tile != TileSetSource::INVALID_ATLAS_COORDS && selected.alternative == 0) {
  1376. Transform2D xform = tile_atlas_control->get_global_transform().affine_inverse() * get_global_transform();
  1377. Vector2 mouse_local_pos = xform.xform(p_pos);
  1378. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(selected.tile);
  1379. Rect2 region = tile_set_atlas_source->get_tile_texture_region(selected.tile);
  1380. Size2 zoomed_size = resize_handle->get_size() / tile_atlas_view->get_zoom();
  1381. Rect2 rect = region.grow_individual(zoomed_size.x, zoomed_size.y, 0, 0);
  1382. const Vector2i coords[] = { Vector2i(0, 0), Vector2i(1, 0), Vector2i(1, 1), Vector2i(0, 1) };
  1383. const Vector2i directions[] = { Vector2i(0, -1), Vector2i(1, 0), Vector2i(0, 1), Vector2i(-1, 0) };
  1384. bool can_grow[4];
  1385. for (int i = 0; i < 4; i++) {
  1386. can_grow[i] = tile_set_atlas_source->has_room_for_tile(selected.tile + directions[i], tile_set_atlas_source->get_tile_size_in_atlas(selected.tile), tile_set_atlas_source->get_tile_animation_columns(selected.tile), tile_set_atlas_source->get_tile_animation_separation(selected.tile), tile_set_atlas_source->get_tile_animation_frames_count(selected.tile), selected.tile);
  1387. can_grow[i] |= (i % 2 == 0) ? size_in_atlas.y > 1 : size_in_atlas.x > 1;
  1388. }
  1389. for (int i = 0; i < 4; i++) {
  1390. Vector2 pos = rect.position + rect.size * coords[i];
  1391. if (can_grow[i] && can_grow[(i + 3) % 4] && Rect2(pos, zoomed_size).has_point(mouse_local_pos)) {
  1392. cursor_shape = (i % 2) ? CURSOR_BDIAGSIZE : CURSOR_FDIAGSIZE;
  1393. }
  1394. Vector2 next_pos = rect.position + rect.size * coords[(i + 1) % 4];
  1395. if (can_grow[i] && Rect2((pos + next_pos) / 2.0, zoomed_size).has_point(mouse_local_pos)) {
  1396. cursor_shape = (i % 2) ? CURSOR_HSIZE : CURSOR_VSIZE;
  1397. }
  1398. }
  1399. }
  1400. }
  1401. } else {
  1402. switch (drag_type) {
  1403. case DRAG_TYPE_RESIZE_TOP_LEFT:
  1404. case DRAG_TYPE_RESIZE_BOTTOM_RIGHT:
  1405. cursor_shape = CURSOR_FDIAGSIZE;
  1406. break;
  1407. case DRAG_TYPE_RESIZE_TOP:
  1408. case DRAG_TYPE_RESIZE_BOTTOM:
  1409. cursor_shape = CURSOR_VSIZE;
  1410. break;
  1411. case DRAG_TYPE_RESIZE_TOP_RIGHT:
  1412. case DRAG_TYPE_RESIZE_BOTTOM_LEFT:
  1413. cursor_shape = CURSOR_BDIAGSIZE;
  1414. break;
  1415. case DRAG_TYPE_RESIZE_LEFT:
  1416. case DRAG_TYPE_RESIZE_RIGHT:
  1417. cursor_shape = CURSOR_HSIZE;
  1418. break;
  1419. case DRAG_TYPE_MOVE_TILE:
  1420. cursor_shape = CURSOR_MOVE;
  1421. break;
  1422. default:
  1423. break;
  1424. }
  1425. }
  1426. return cursor_shape;
  1427. }
  1428. HashMap<Vector2i, List<const PropertyInfo *>> TileSetAtlasSourceEditor::_group_properties_per_tiles(const List<PropertyInfo> &r_list, const TileSetAtlasSource *p_atlas) {
  1429. // Group properties per tile.
  1430. HashMap<Vector2i, List<const PropertyInfo *>> per_tile;
  1431. for (const List<PropertyInfo>::Element *E_property = r_list.front(); E_property; E_property = E_property->next()) {
  1432. Vector<String> components = String(E_property->get().name).split("/", true, 1);
  1433. if (components.size() >= 1) {
  1434. Vector<String> coord_arr = components[0].split(":");
  1435. if (coord_arr.size() == 2 && coord_arr[0].is_valid_int() && coord_arr[1].is_valid_int()) {
  1436. Vector2i coords = Vector2i(coord_arr[0].to_int(), coord_arr[1].to_int());
  1437. per_tile[coords].push_back(&(E_property->get()));
  1438. }
  1439. }
  1440. }
  1441. return per_tile;
  1442. }
  1443. void TileSetAtlasSourceEditor::_menu_option(int p_option) {
  1444. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1445. switch (p_option) {
  1446. case TILE_DELETE: {
  1447. List<PropertyInfo> list;
  1448. tile_set_atlas_source->get_property_list(&list);
  1449. HashMap<Vector2i, List<const PropertyInfo *>> per_tile = _group_properties_per_tiles(list, tile_set_atlas_source);
  1450. undo_redo->create_action(TTR("Remove tile"));
  1451. // Remove tiles
  1452. RBSet<Vector2i> removed;
  1453. for (const TileSelection &E : selection) {
  1454. TileSelection selected = E;
  1455. if (selected.alternative == 0) {
  1456. // Remove a tile.
  1457. undo_redo->add_do_method(tile_set_atlas_source, "remove_tile", selected.tile);
  1458. undo_redo->add_undo_method(tile_set_atlas_source, "create_tile", selected.tile);
  1459. removed.insert(selected.tile);
  1460. if (per_tile.has(selected.tile)) {
  1461. for (List<const PropertyInfo *>::Element *E_property = per_tile[selected.tile].front(); E_property; E_property = E_property->next()) {
  1462. String property = E_property->get()->name;
  1463. Variant value = tile_set_atlas_source->get(property);
  1464. if (value.get_type() != Variant::NIL) {
  1465. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  1466. }
  1467. }
  1468. }
  1469. }
  1470. }
  1471. // Remove alternatives
  1472. for (const TileSelection &E : selection) {
  1473. TileSelection selected = E;
  1474. if (selected.alternative > 0 && !removed.has(selected.tile)) {
  1475. // Remove an alternative tile.
  1476. undo_redo->add_do_method(tile_set_atlas_source, "remove_alternative_tile", selected.tile, selected.alternative);
  1477. undo_redo->add_undo_method(tile_set_atlas_source, "create_alternative_tile", selected.tile, selected.alternative);
  1478. if (per_tile.has(selected.tile)) {
  1479. for (List<const PropertyInfo *>::Element *E_property = per_tile[selected.tile].front(); E_property; E_property = E_property->next()) {
  1480. Vector<String> components = E_property->get()->name.split("/", true, 2);
  1481. if (components.size() >= 2 && components[1].is_valid_int() && components[1].to_int() == selected.alternative) {
  1482. String property = E_property->get()->name;
  1483. Variant value = tile_set_atlas_source->get(property);
  1484. if (value.get_type() != Variant::NIL) {
  1485. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  1486. }
  1487. }
  1488. }
  1489. }
  1490. }
  1491. }
  1492. undo_redo->commit_action();
  1493. _update_fix_selected_and_hovered_tiles();
  1494. _update_tile_id_label();
  1495. } break;
  1496. case TILE_CREATE: {
  1497. undo_redo->create_action(TTR("Create a tile"));
  1498. undo_redo->add_do_method(tile_set_atlas_source, "create_tile", menu_option_coords);
  1499. Array array;
  1500. array.push_back(menu_option_coords);
  1501. array.push_back(0);
  1502. undo_redo->add_do_method(this, "_set_selection_from_array", array);
  1503. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", menu_option_coords);
  1504. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  1505. undo_redo->commit_action();
  1506. _update_tile_id_label();
  1507. } break;
  1508. case TILE_CREATE_ALTERNATIVE: {
  1509. undo_redo->create_action(TTR("Create tile alternatives"));
  1510. Array array;
  1511. for (const TileSelection &E : selection) {
  1512. if (E.alternative == 0) {
  1513. int next_id = tile_set_atlas_source->get_next_alternative_tile_id(E.tile);
  1514. undo_redo->add_do_method(tile_set_atlas_source, "create_alternative_tile", E.tile, next_id);
  1515. array.push_back(E.tile);
  1516. array.push_back(next_id);
  1517. undo_redo->add_undo_method(tile_set_atlas_source, "remove_alternative_tile", E.tile, next_id);
  1518. }
  1519. }
  1520. undo_redo->add_do_method(this, "_set_selection_from_array", array);
  1521. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  1522. undo_redo->commit_action();
  1523. _update_tile_id_label();
  1524. } break;
  1525. case ADVANCED_AUTO_CREATE_TILES: {
  1526. _auto_create_tiles();
  1527. } break;
  1528. case ADVANCED_AUTO_REMOVE_TILES: {
  1529. _auto_remove_tiles();
  1530. } break;
  1531. }
  1532. }
  1533. void TileSetAtlasSourceEditor::_unhandled_key_input(const Ref<InputEvent> &p_event) {
  1534. // Check for shortcuts.
  1535. if (ED_IS_SHORTCUT("tiles_editor/delete_tile", p_event)) {
  1536. if (tools_button_group->get_pressed_button() == tool_select_button && !selection.is_empty()) {
  1537. _menu_option(TILE_DELETE);
  1538. accept_event();
  1539. }
  1540. }
  1541. }
  1542. void TileSetAtlasSourceEditor::_set_selection_from_array(Array p_selection) {
  1543. ERR_FAIL_COND((p_selection.size() % 2) != 0);
  1544. selection.clear();
  1545. for (int i = 0; i < p_selection.size() / 2; i++) {
  1546. TileSelection selected = { p_selection[i * 2], p_selection[i * 2 + 1] };
  1547. if (tile_set_atlas_source->has_tile(selected.tile) && tile_set_atlas_source->has_alternative_tile(selected.tile, selected.alternative)) {
  1548. selection.insert(selected);
  1549. }
  1550. }
  1551. _update_tile_inspector();
  1552. _update_tile_id_label();
  1553. _update_atlas_view();
  1554. _update_current_tile_data_editor();
  1555. }
  1556. Array TileSetAtlasSourceEditor::_get_selection_as_array() {
  1557. Array output;
  1558. for (const TileSelection &E : selection) {
  1559. output.push_back(E.tile);
  1560. output.push_back(E.alternative);
  1561. }
  1562. return output;
  1563. }
  1564. void TileSetAtlasSourceEditor::_tile_atlas_control_draw() {
  1565. // Draw the selected tile.
  1566. if (tools_button_group->get_pressed_button() == tool_select_button) {
  1567. for (const TileSelection &E : selection) {
  1568. TileSelection selected = E;
  1569. if (selected.alternative == 0) {
  1570. // Draw the rect.
  1571. for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(selected.tile); frame++) {
  1572. Color color = Color(0.0, 1.0, 0.0, frame == 0 ? 1.0 : 0.3);
  1573. Rect2 region = tile_set_atlas_source->get_tile_texture_region(selected.tile, frame);
  1574. TilesEditorPlugin::draw_selection_rect(tile_atlas_control, region, color);
  1575. }
  1576. }
  1577. }
  1578. if (selection.size() == 1) {
  1579. // Draw the resize handles (only when it's possible to expand).
  1580. TileSelection selected = selection.front()->get();
  1581. if (selected.alternative == 0) {
  1582. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(selected.tile);
  1583. Size2 zoomed_size = resize_handle->get_size() / tile_atlas_view->get_zoom();
  1584. Rect2 region = tile_set_atlas_source->get_tile_texture_region(selected.tile);
  1585. Rect2 rect = region.grow_individual(zoomed_size.x, zoomed_size.y, 0, 0);
  1586. const Vector2i coords[] = { Vector2i(0, 0), Vector2i(1, 0), Vector2i(1, 1), Vector2i(0, 1) };
  1587. const Vector2i directions[] = { Vector2i(0, -1), Vector2i(1, 0), Vector2i(0, 1), Vector2i(-1, 0) };
  1588. bool can_grow[4];
  1589. for (int i = 0; i < 4; i++) {
  1590. can_grow[i] = tile_set_atlas_source->has_room_for_tile(selected.tile + directions[i], tile_set_atlas_source->get_tile_size_in_atlas(selected.tile), tile_set_atlas_source->get_tile_animation_columns(selected.tile), tile_set_atlas_source->get_tile_animation_separation(selected.tile), tile_set_atlas_source->get_tile_animation_frames_count(selected.tile), selected.tile);
  1591. can_grow[i] |= (i % 2 == 0) ? size_in_atlas.y > 1 : size_in_atlas.x > 1;
  1592. }
  1593. for (int i = 0; i < 4; i++) {
  1594. Vector2 pos = rect.position + rect.size * coords[i];
  1595. if (can_grow[i] && can_grow[(i + 3) % 4]) {
  1596. tile_atlas_control->draw_texture_rect(resize_handle, Rect2(pos, zoomed_size), false);
  1597. } else {
  1598. tile_atlas_control->draw_texture_rect(resize_handle_disabled, Rect2(pos, zoomed_size), false);
  1599. }
  1600. Vector2 next_pos = rect.position + rect.size * coords[(i + 1) % 4];
  1601. if (can_grow[i]) {
  1602. tile_atlas_control->draw_texture_rect(resize_handle, Rect2((pos + next_pos) / 2.0, zoomed_size), false);
  1603. } else {
  1604. tile_atlas_control->draw_texture_rect(resize_handle_disabled, Rect2((pos + next_pos) / 2.0, zoomed_size), false);
  1605. }
  1606. }
  1607. }
  1608. }
  1609. }
  1610. if (drag_type == DRAG_TYPE_REMOVE_TILES) {
  1611. // Draw the tiles to be removed.
  1612. for (const Vector2i &E : drag_modified_tiles) {
  1613. for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(E); frame++) {
  1614. TilesEditorPlugin::draw_selection_rect(tile_atlas_control, tile_set_atlas_source->get_tile_texture_region(E, frame), Color(0.0, 0.0, 0.0));
  1615. }
  1616. }
  1617. } else if (drag_type == DRAG_TYPE_RECT_SELECT || drag_type == DRAG_TYPE_REMOVE_TILES_USING_RECT) {
  1618. // Draw tiles to be removed.
  1619. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos, true);
  1620. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1621. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  1622. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  1623. Color color = Color(0.0, 0.0, 0.0);
  1624. if (drag_type == DRAG_TYPE_RECT_SELECT) {
  1625. color = Color(1.0, 1.0, 0.0);
  1626. }
  1627. RBSet<Vector2i> to_paint;
  1628. for (int x = area.get_position().x; x < area.get_end().x; x++) {
  1629. for (int y = area.get_position().y; y < area.get_end().y; y++) {
  1630. Vector2i coords = tile_set_atlas_source->get_tile_at_coords(Vector2i(x, y));
  1631. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1632. to_paint.insert(coords);
  1633. }
  1634. }
  1635. }
  1636. for (const Vector2i &E : to_paint) {
  1637. Vector2i coords = E;
  1638. TilesEditorPlugin::draw_selection_rect(tile_atlas_control, tile_set_atlas_source->get_tile_texture_region(coords), color);
  1639. }
  1640. } else if (drag_type == DRAG_TYPE_CREATE_TILES_USING_RECT) {
  1641. // Draw tiles to be created.
  1642. Vector2i margins = tile_set_atlas_source->get_margins();
  1643. Vector2i separation = tile_set_atlas_source->get_separation();
  1644. Vector2i tile_size = tile_set_atlas_source->get_texture_region_size();
  1645. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos, true);
  1646. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1647. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  1648. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  1649. for (int x = area.get_position().x; x < area.get_end().x; x++) {
  1650. for (int y = area.get_position().y; y < area.get_end().y; y++) {
  1651. Vector2i coords = Vector2i(x, y);
  1652. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
  1653. Vector2i origin = margins + (coords * (tile_size + separation));
  1654. TilesEditorPlugin::draw_selection_rect(tile_atlas_control, Rect2i(origin, tile_size));
  1655. }
  1656. }
  1657. }
  1658. }
  1659. // Draw the hovered tile.
  1660. if (drag_type == DRAG_TYPE_REMOVE_TILES_USING_RECT || drag_type == DRAG_TYPE_CREATE_TILES_USING_RECT) {
  1661. // Draw the rect.
  1662. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos, true);
  1663. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position(), true);
  1664. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  1665. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  1666. Vector2i margins = tile_set_atlas_source->get_margins();
  1667. Vector2i separation = tile_set_atlas_source->get_separation();
  1668. Vector2i tile_size = tile_set_atlas_source->get_texture_region_size();
  1669. Vector2i origin = margins + (area.position * (tile_size + separation));
  1670. TilesEditorPlugin::draw_selection_rect(tile_atlas_control, Rect2i(origin, area.size * tile_size));
  1671. } else {
  1672. Vector2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  1673. if (hovered_base_tile_coords.x >= 0 && hovered_base_tile_coords.y >= 0 && hovered_base_tile_coords.x < grid_size.x && hovered_base_tile_coords.y < grid_size.y) {
  1674. Vector2i hovered_tile = tile_set_atlas_source->get_tile_at_coords(hovered_base_tile_coords);
  1675. if (hovered_tile != TileSetSource::INVALID_ATLAS_COORDS) {
  1676. // Draw existing hovered tile.
  1677. for (int frame = 0; frame < tile_set_atlas_source->get_tile_animation_frames_count(hovered_tile); frame++) {
  1678. Color color = Color(1.0, 0.8, 0.0, frame == 0 ? 0.6 : 0.3);
  1679. TilesEditorPlugin::draw_selection_rect(tile_atlas_control, tile_set_atlas_source->get_tile_texture_region(hovered_tile, frame), color);
  1680. }
  1681. } else {
  1682. // Draw empty tile, only in add/remove tiles mode.
  1683. if (tools_button_group->get_pressed_button() == tool_setup_atlas_source_button) {
  1684. Vector2i margins = tile_set_atlas_source->get_margins();
  1685. Vector2i separation = tile_set_atlas_source->get_separation();
  1686. Vector2i tile_size = tile_set_atlas_source->get_texture_region_size();
  1687. Vector2i origin = margins + (hovered_base_tile_coords * (tile_size + separation));
  1688. TilesEditorPlugin::draw_selection_rect(tile_atlas_control, Rect2i(origin, tile_size));
  1689. }
  1690. }
  1691. }
  1692. }
  1693. }
  1694. void TileSetAtlasSourceEditor::_tile_atlas_control_unscaled_draw() {
  1695. if (current_tile_data_editor) {
  1696. // Draw the preview of the selected property.
  1697. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  1698. Vector2i coords = tile_set_atlas_source->get_tile_id(i);
  1699. Rect2i texture_region = tile_set_atlas_source->get_tile_texture_region(coords);
  1700. Vector2i position = texture_region.get_center() + tile_set_atlas_source->get_tile_data(coords, 0)->get_texture_origin();
  1701. Transform2D xform = tile_atlas_control->get_parent_control()->get_transform();
  1702. xform.translate_local(position);
  1703. if (tools_button_group->get_pressed_button() == tool_select_button && selection.has({ coords, 0 })) {
  1704. continue;
  1705. }
  1706. TileMapCell cell;
  1707. cell.source_id = tile_set_atlas_source_id;
  1708. cell.set_atlas_coords(coords);
  1709. cell.alternative_tile = 0;
  1710. current_tile_data_editor->draw_over_tile(tile_atlas_control_unscaled, xform, cell);
  1711. }
  1712. // Draw the selection on top of other.
  1713. if (tools_button_group->get_pressed_button() == tool_select_button) {
  1714. for (const TileSelection &E : selection) {
  1715. if (E.alternative != 0) {
  1716. continue;
  1717. }
  1718. Rect2i texture_region = tile_set_atlas_source->get_tile_texture_region(E.tile);
  1719. Vector2i position = texture_region.get_center() + tile_set_atlas_source->get_tile_data(E.tile, 0)->get_texture_origin();
  1720. Transform2D xform = tile_atlas_control->get_parent_control()->get_transform();
  1721. xform.translate_local(position);
  1722. TileMapCell cell;
  1723. cell.source_id = tile_set_atlas_source_id;
  1724. cell.set_atlas_coords(E.tile);
  1725. cell.alternative_tile = 0;
  1726. current_tile_data_editor->draw_over_tile(tile_atlas_control_unscaled, xform, cell, true);
  1727. }
  1728. }
  1729. // Call the TileData's editor custom draw function.
  1730. if (tools_button_group->get_pressed_button() == tool_paint_button) {
  1731. Transform2D xform = tile_atlas_control->get_parent_control()->get_transform();
  1732. current_tile_data_editor->forward_draw_over_atlas(tile_atlas_view, tile_set_atlas_source, tile_atlas_control_unscaled, xform);
  1733. }
  1734. }
  1735. }
  1736. void TileSetAtlasSourceEditor::_tile_alternatives_control_gui_input(const Ref<InputEvent> &p_event) {
  1737. // Update the hovered alternative tile.
  1738. hovered_alternative_tile_coords = tile_atlas_view->get_alternative_tile_at_pos(alternative_tiles_control->get_local_mouse_position());
  1739. // Forward the event to the current tile data editor if we are in the painting mode.
  1740. if (tools_button_group->get_pressed_button() == tool_paint_button) {
  1741. if (current_tile_data_editor) {
  1742. current_tile_data_editor->forward_painting_alternatives_gui_input(tile_atlas_view, tile_set_atlas_source, p_event);
  1743. }
  1744. tile_atlas_control->queue_redraw();
  1745. tile_atlas_control_unscaled->queue_redraw();
  1746. alternative_tiles_control->queue_redraw();
  1747. alternative_tiles_control_unscaled->queue_redraw();
  1748. tile_atlas_view->queue_redraw();
  1749. return;
  1750. }
  1751. Ref<InputEventMouseMotion> mm = p_event;
  1752. if (mm.is_valid()) {
  1753. tile_atlas_control->queue_redraw();
  1754. tile_atlas_control_unscaled->queue_redraw();
  1755. alternative_tiles_control->queue_redraw();
  1756. alternative_tiles_control_unscaled->queue_redraw();
  1757. if (drag_type == DRAG_TYPE_MAY_POPUP_MENU) {
  1758. if (Vector2(drag_start_mouse_pos).distance_to(alternative_tiles_control->get_local_mouse_position()) > 5.0 * EDSCALE) {
  1759. drag_type = DRAG_TYPE_NONE;
  1760. }
  1761. }
  1762. }
  1763. Ref<InputEventMouseButton> mb = p_event;
  1764. if (mb.is_valid()) {
  1765. Vector2 mouse_local_pos = alternative_tiles_control->get_local_mouse_position();
  1766. if (mb->get_button_index() == MouseButton::LEFT) {
  1767. if (mb->is_pressed()) {
  1768. // Left click pressed.
  1769. if (tools_button_group->get_pressed_button() == tool_select_button) {
  1770. Vector3 tile = tile_atlas_view->get_alternative_tile_at_pos(mouse_local_pos);
  1771. selection.clear();
  1772. TileSelection selected = { Vector2i(tile.x, tile.y), int(tile.z) };
  1773. if (selected.tile != TileSetSource::INVALID_ATLAS_COORDS) {
  1774. selection.insert(selected);
  1775. }
  1776. _update_tile_inspector();
  1777. _update_tile_id_label();
  1778. }
  1779. }
  1780. } else if (mb->get_button_index() == MouseButton::RIGHT) {
  1781. if (mb->is_pressed()) {
  1782. drag_type = DRAG_TYPE_MAY_POPUP_MENU;
  1783. drag_start_mouse_pos = alternative_tiles_control->get_local_mouse_position();
  1784. } else {
  1785. if (drag_type == DRAG_TYPE_MAY_POPUP_MENU) {
  1786. // Right click released and wasn't dragged too far
  1787. Vector3 tile = tile_atlas_view->get_alternative_tile_at_pos(mouse_local_pos);
  1788. selection.clear();
  1789. TileSelection selected = { Vector2i(tile.x, tile.y), int(tile.z) };
  1790. if (selected.tile != TileSetSource::INVALID_ATLAS_COORDS) {
  1791. selection.insert(selected);
  1792. }
  1793. _update_tile_inspector();
  1794. _update_tile_id_label();
  1795. if (selection.size() == 1) {
  1796. selected = selection.front()->get();
  1797. menu_option_coords = selected.tile;
  1798. menu_option_alternative = selected.alternative;
  1799. alternative_tile_popup_menu->popup(Rect2i(get_screen_transform().xform(get_local_mouse_position()), Size2i()));
  1800. }
  1801. }
  1802. drag_type = DRAG_TYPE_NONE;
  1803. }
  1804. }
  1805. tile_atlas_control->queue_redraw();
  1806. tile_atlas_control_unscaled->queue_redraw();
  1807. alternative_tiles_control->queue_redraw();
  1808. alternative_tiles_control_unscaled->queue_redraw();
  1809. }
  1810. }
  1811. void TileSetAtlasSourceEditor::_tile_alternatives_control_mouse_exited() {
  1812. hovered_alternative_tile_coords = Vector3i(TileSetSource::INVALID_ATLAS_COORDS.x, TileSetSource::INVALID_ATLAS_COORDS.y, TileSetSource::INVALID_TILE_ALTERNATIVE);
  1813. tile_atlas_control->queue_redraw();
  1814. tile_atlas_control_unscaled->queue_redraw();
  1815. alternative_tiles_control->queue_redraw();
  1816. alternative_tiles_control_unscaled->queue_redraw();
  1817. }
  1818. void TileSetAtlasSourceEditor::_tile_alternatives_control_draw() {
  1819. // Update the hovered alternative tile.
  1820. if (tools_button_group->get_pressed_button() == tool_select_button) {
  1821. // Draw hovered tile.
  1822. Vector2i coords = Vector2(hovered_alternative_tile_coords.x, hovered_alternative_tile_coords.y);
  1823. if (coords != TileSetSource::INVALID_ATLAS_COORDS) {
  1824. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(coords, hovered_alternative_tile_coords.z);
  1825. if (rect != Rect2i()) {
  1826. TilesEditorPlugin::draw_selection_rect(alternative_tiles_control, rect, Color(1.0, 0.8, 0.0, 0.5));
  1827. }
  1828. }
  1829. // Draw selected tile.
  1830. for (const TileSelection &E : selection) {
  1831. TileSelection selected = E;
  1832. if (selected.alternative >= 1) {
  1833. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(selected.tile, selected.alternative);
  1834. if (rect != Rect2i()) {
  1835. TilesEditorPlugin::draw_selection_rect(alternative_tiles_control, rect);
  1836. }
  1837. }
  1838. }
  1839. }
  1840. }
  1841. void TileSetAtlasSourceEditor::_tile_alternatives_control_unscaled_draw() {
  1842. // Draw the preview of the selected property.
  1843. if (current_tile_data_editor) {
  1844. // Draw the preview of the currently selected property.
  1845. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  1846. Vector2i coords = tile_set_atlas_source->get_tile_id(i);
  1847. for (int j = 0; j < tile_set_atlas_source->get_alternative_tiles_count(coords); j++) {
  1848. int alternative_tile = tile_set_atlas_source->get_alternative_tile_id(coords, j);
  1849. if (alternative_tile == 0) {
  1850. continue;
  1851. }
  1852. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(coords, alternative_tile);
  1853. Vector2 position = rect.get_center() + tile_set_atlas_source->get_tile_data(coords, alternative_tile)->get_texture_origin();
  1854. Transform2D xform = alternative_tiles_control->get_parent_control()->get_transform();
  1855. xform.translate_local(position);
  1856. if (tools_button_group->get_pressed_button() == tool_select_button && selection.has({ coords, alternative_tile })) {
  1857. continue;
  1858. }
  1859. TileMapCell cell;
  1860. cell.source_id = tile_set_atlas_source_id;
  1861. cell.set_atlas_coords(coords);
  1862. cell.alternative_tile = alternative_tile;
  1863. current_tile_data_editor->draw_over_tile(alternative_tiles_control_unscaled, xform, cell);
  1864. }
  1865. }
  1866. // Draw the selection on top of other.
  1867. if (tools_button_group->get_pressed_button() == tool_select_button) {
  1868. for (const TileSelection &E : selection) {
  1869. if (E.alternative == 0) {
  1870. continue;
  1871. }
  1872. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(E.tile, E.alternative);
  1873. Vector2 position = rect.get_center() + tile_set_atlas_source->get_tile_data(E.tile, E.alternative)->get_texture_origin();
  1874. Transform2D xform = alternative_tiles_control->get_parent_control()->get_transform();
  1875. xform.translate_local(position);
  1876. TileMapCell cell;
  1877. cell.source_id = tile_set_atlas_source_id;
  1878. cell.set_atlas_coords(E.tile);
  1879. cell.alternative_tile = E.alternative;
  1880. current_tile_data_editor->draw_over_tile(alternative_tiles_control_unscaled, xform, cell, true);
  1881. }
  1882. }
  1883. // Call the TileData's editor custom draw function.
  1884. if (tools_button_group->get_pressed_button() == tool_paint_button) {
  1885. Transform2D xform = tile_atlas_control->get_parent_control()->get_transform();
  1886. current_tile_data_editor->forward_draw_over_alternatives(tile_atlas_view, tile_set_atlas_source, alternative_tiles_control_unscaled, xform);
  1887. }
  1888. }
  1889. }
  1890. void TileSetAtlasSourceEditor::_tile_set_changed() {
  1891. if (tile_set->get_source_count() == 0) {
  1892. // No sources, so nothing to do here anymore.
  1893. tile_set->disconnect("changed", callable_mp(this, &TileSetAtlasSourceEditor::_tile_set_changed));
  1894. tile_set = Ref<TileSet>();
  1895. return;
  1896. }
  1897. tile_set_changed_needs_update = true;
  1898. }
  1899. void TileSetAtlasSourceEditor::_tile_proxy_object_changed(String p_what) {
  1900. tile_set_changed_needs_update = false; // Avoid updating too many things.
  1901. _update_atlas_view();
  1902. }
  1903. void TileSetAtlasSourceEditor::_atlas_source_proxy_object_changed(String p_what) {
  1904. if (p_what == "texture" && !atlas_source_proxy_object->get("texture").is_null()) {
  1905. confirm_auto_create_tiles->popup_centered();
  1906. } else if (p_what == "id") {
  1907. emit_signal(SNAME("source_id_changed"), atlas_source_proxy_object->get_id());
  1908. }
  1909. }
  1910. void TileSetAtlasSourceEditor::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, String p_property, Variant p_new_value) {
  1911. EditorUndoRedoManager *undo_redo_man = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
  1912. ERR_FAIL_NULL(undo_redo_man);
  1913. #define ADD_UNDO(obj, property) undo_redo_man->add_undo_property(obj, property, obj->get(property));
  1914. AtlasTileProxyObject *tile_data_proxy = Object::cast_to<AtlasTileProxyObject>(p_edited);
  1915. if (tile_data_proxy) {
  1916. UndoRedo *internal_undo_redo = undo_redo_man->get_history_for_object(tile_data_proxy).undo_redo;
  1917. internal_undo_redo->start_force_keep_in_merge_ends();
  1918. Vector<String> components = String(p_property).split("/", true, 2);
  1919. if (components.size() == 2 && components[1] == "polygons_count") {
  1920. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  1921. int new_polygons_count = p_new_value;
  1922. int old_polygons_count = tile_data_proxy->get(vformat("physics_layer_%d/polygons_count", layer_index));
  1923. if (new_polygons_count < old_polygons_count) {
  1924. for (int i = new_polygons_count; i < old_polygons_count; i++) {
  1925. ADD_UNDO(tile_data_proxy, vformat("physics_layer_%d/polygon_%d/points", layer_index, i));
  1926. ADD_UNDO(tile_data_proxy, vformat("physics_layer_%d/polygon_%d/one_way", layer_index, i));
  1927. ADD_UNDO(tile_data_proxy, vformat("physics_layer_%d/polygon_%d/one_way_margin", layer_index, i));
  1928. }
  1929. }
  1930. } else if (p_property == "terrain_set") {
  1931. int current_terrain_set = tile_data_proxy->get("terrain_set");
  1932. ADD_UNDO(tile_data_proxy, "terrain");
  1933. for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) {
  1934. TileSet::CellNeighbor bit = TileSet::CellNeighbor(i);
  1935. if (tile_set->is_valid_terrain_peering_bit(current_terrain_set, bit)) {
  1936. ADD_UNDO(tile_data_proxy, "terrains_peering_bit/" + String(TileSet::CELL_NEIGHBOR_ENUM_TO_TEXT[i]));
  1937. }
  1938. }
  1939. }
  1940. internal_undo_redo->end_force_keep_in_merge_ends();
  1941. }
  1942. TileSetAtlasSourceProxyObject *atlas_source_proxy = Object::cast_to<TileSetAtlasSourceProxyObject>(p_edited);
  1943. if (atlas_source_proxy) {
  1944. TileSetAtlasSource *atlas_source = atlas_source_proxy->get_edited();
  1945. ERR_FAIL_COND(!atlas_source);
  1946. UndoRedo *internal_undo_redo = undo_redo_man->get_history_for_object(atlas_source).undo_redo;
  1947. internal_undo_redo->start_force_keep_in_merge_ends();
  1948. PackedVector2Array arr;
  1949. if (p_property == "texture") {
  1950. arr = atlas_source->get_tiles_to_be_removed_on_change(p_new_value, atlas_source->get_margins(), atlas_source->get_separation(), atlas_source->get_texture_region_size());
  1951. } else if (p_property == "margins") {
  1952. arr = atlas_source->get_tiles_to_be_removed_on_change(atlas_source->get_texture(), p_new_value, atlas_source->get_separation(), atlas_source->get_texture_region_size());
  1953. } else if (p_property == "separation") {
  1954. arr = atlas_source->get_tiles_to_be_removed_on_change(atlas_source->get_texture(), atlas_source->get_margins(), p_new_value, atlas_source->get_texture_region_size());
  1955. } else if (p_property == "texture_region_size") {
  1956. arr = atlas_source->get_tiles_to_be_removed_on_change(atlas_source->get_texture(), atlas_source->get_margins(), atlas_source->get_separation(), p_new_value);
  1957. }
  1958. if (!arr.is_empty()) {
  1959. // Get all properties assigned to a tile.
  1960. List<PropertyInfo> properties;
  1961. atlas_source->get_property_list(&properties);
  1962. for (int i = 0; i < arr.size(); i++) {
  1963. Vector2i coords = arr[i];
  1964. String prefix = vformat("%d:%d/", coords.x, coords.y);
  1965. for (PropertyInfo pi : properties) {
  1966. if (pi.name.begins_with(prefix)) {
  1967. ADD_UNDO(atlas_source, pi.name);
  1968. }
  1969. }
  1970. }
  1971. }
  1972. internal_undo_redo->end_force_keep_in_merge_ends();
  1973. }
  1974. #undef ADD_UNDO
  1975. }
  1976. void TileSetAtlasSourceEditor::edit(Ref<TileSet> p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id) {
  1977. ERR_FAIL_COND(!p_tile_set.is_valid());
  1978. ERR_FAIL_COND(!p_tile_set_atlas_source);
  1979. ERR_FAIL_COND(p_source_id < 0);
  1980. ERR_FAIL_COND(p_tile_set->get_source(p_source_id) != p_tile_set_atlas_source);
  1981. bool new_read_only_state = false;
  1982. if (p_tile_set.is_valid()) {
  1983. new_read_only_state = EditorNode::get_singleton()->is_resource_read_only(p_tile_set);
  1984. }
  1985. if (p_tile_set == tile_set && p_tile_set_atlas_source == tile_set_atlas_source && p_source_id == tile_set_atlas_source_id && new_read_only_state == read_only) {
  1986. return;
  1987. }
  1988. // Remove listener for old objects.
  1989. if (tile_set.is_valid()) {
  1990. tile_set->disconnect("changed", callable_mp(this, &TileSetAtlasSourceEditor::_tile_set_changed));
  1991. }
  1992. // Clear the selection.
  1993. selection.clear();
  1994. // Change the edited object.
  1995. tile_set = p_tile_set;
  1996. tile_set_atlas_source = p_tile_set_atlas_source;
  1997. tile_set_atlas_source_id = p_source_id;
  1998. // Read-only is off by default.
  1999. read_only = new_read_only_state;
  2000. if (tile_set.is_valid()) {
  2001. tile_set->connect("changed", callable_mp(this, &TileSetAtlasSourceEditor::_tile_set_changed));
  2002. }
  2003. if (read_only && tools_button_group->get_pressed_button() == tool_paint_button) {
  2004. tool_paint_button->set_pressed(false);
  2005. tool_setup_atlas_source_button->set_pressed(true);
  2006. }
  2007. // Disable buttons in read-only mode.
  2008. tool_paint_button->set_disabled(read_only);
  2009. tools_settings_erase_button->set_disabled(read_only);
  2010. tool_advanced_menu_button->set_disabled(read_only);
  2011. // Update everything.
  2012. _update_source_inspector();
  2013. // Update the selected tile.
  2014. _update_fix_selected_and_hovered_tiles();
  2015. _update_tile_id_label();
  2016. _update_atlas_view();
  2017. _update_atlas_source_inspector();
  2018. _update_tile_inspector();
  2019. _update_tile_data_editors();
  2020. _update_current_tile_data_editor();
  2021. }
  2022. void TileSetAtlasSourceEditor::init_source() {
  2023. confirm_auto_create_tiles->popup_centered();
  2024. }
  2025. void TileSetAtlasSourceEditor::_auto_create_tiles() {
  2026. if (!tile_set_atlas_source) {
  2027. return;
  2028. }
  2029. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  2030. if (texture.is_valid()) {
  2031. Vector2i margins = tile_set_atlas_source->get_margins();
  2032. Vector2i separation = tile_set_atlas_source->get_separation();
  2033. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  2034. Size2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  2035. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2036. undo_redo->create_action(TTR("Create tiles in non-transparent texture regions"));
  2037. for (int y = 0; y < grid_size.y; y++) {
  2038. for (int x = 0; x < grid_size.x; x++) {
  2039. // Check if we have a tile at the coord
  2040. Vector2i coords = Vector2i(x, y);
  2041. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetSource::INVALID_ATLAS_COORDS) {
  2042. // Check if the texture is empty at the given coords.
  2043. Rect2i region = Rect2i(margins + (coords * (texture_region_size + separation)), texture_region_size);
  2044. bool is_opaque = false;
  2045. for (int region_x = region.get_position().x; region_x < region.get_end().x; region_x++) {
  2046. for (int region_y = region.get_position().y; region_y < region.get_end().y; region_y++) {
  2047. if (texture->is_pixel_opaque(region_x, region_y)) {
  2048. is_opaque = true;
  2049. break;
  2050. }
  2051. }
  2052. if (is_opaque) {
  2053. break;
  2054. }
  2055. }
  2056. // If we do have opaque pixels, create a tile.
  2057. if (is_opaque) {
  2058. undo_redo->add_do_method(tile_set_atlas_source, "create_tile", coords);
  2059. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", coords);
  2060. }
  2061. }
  2062. }
  2063. }
  2064. undo_redo->commit_action();
  2065. }
  2066. }
  2067. void TileSetAtlasSourceEditor::_auto_remove_tiles() {
  2068. if (!tile_set_atlas_source) {
  2069. return;
  2070. }
  2071. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  2072. if (texture.is_valid()) {
  2073. Vector2i margins = tile_set_atlas_source->get_margins();
  2074. Vector2i separation = tile_set_atlas_source->get_separation();
  2075. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  2076. Vector2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  2077. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2078. undo_redo->create_action(TTR("Remove tiles in fully transparent texture regions"));
  2079. List<PropertyInfo> list;
  2080. tile_set_atlas_source->get_property_list(&list);
  2081. HashMap<Vector2i, List<const PropertyInfo *>> per_tile = _group_properties_per_tiles(list, tile_set_atlas_source);
  2082. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  2083. Vector2i coords = tile_set_atlas_source->get_tile_id(i);
  2084. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(coords);
  2085. // Skip tiles outside texture.
  2086. if ((coords.x + size_in_atlas.x) > grid_size.x || (coords.y + size_in_atlas.y) > grid_size.y) {
  2087. continue;
  2088. }
  2089. // Check if the texture is empty at the given coords.
  2090. Rect2i region = Rect2i(margins + (coords * (texture_region_size + separation)), texture_region_size * size_in_atlas);
  2091. bool is_opaque = false;
  2092. for (int region_x = region.get_position().x; region_x < region.get_end().x; region_x++) {
  2093. for (int region_y = region.get_position().y; region_y < region.get_end().y; region_y++) {
  2094. if (texture->is_pixel_opaque(region_x, region_y)) {
  2095. is_opaque = true;
  2096. break;
  2097. }
  2098. }
  2099. if (is_opaque) {
  2100. break;
  2101. }
  2102. }
  2103. // If we do have opaque pixels, create a tile.
  2104. if (!is_opaque) {
  2105. undo_redo->add_do_method(tile_set_atlas_source, "remove_tile", coords);
  2106. undo_redo->add_undo_method(tile_set_atlas_source, "create_tile", coords);
  2107. if (per_tile.has(coords)) {
  2108. for (List<const PropertyInfo *>::Element *E_property = per_tile[coords].front(); E_property; E_property = E_property->next()) {
  2109. String property = E_property->get()->name;
  2110. Variant value = tile_set_atlas_source->get(property);
  2111. if (value.get_type() != Variant::NIL) {
  2112. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  2113. }
  2114. }
  2115. }
  2116. }
  2117. }
  2118. undo_redo->commit_action();
  2119. }
  2120. }
  2121. void TileSetAtlasSourceEditor::_notification(int p_what) {
  2122. switch (p_what) {
  2123. case NOTIFICATION_ENTER_TREE:
  2124. case NOTIFICATION_THEME_CHANGED: {
  2125. tool_setup_atlas_source_button->set_icon(get_theme_icon(SNAME("Tools"), SNAME("EditorIcons")));
  2126. tool_select_button->set_icon(get_theme_icon(SNAME("ToolSelect"), SNAME("EditorIcons")));
  2127. tool_paint_button->set_icon(get_theme_icon(SNAME("CanvasItem"), SNAME("EditorIcons")));
  2128. tools_settings_erase_button->set_icon(get_theme_icon(SNAME("Eraser"), SNAME("EditorIcons")));
  2129. tool_advanced_menu_button->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  2130. resize_handle = get_theme_icon(SNAME("EditorHandle"), SNAME("EditorIcons"));
  2131. resize_handle_disabled = get_theme_icon(SNAME("EditorHandleDisabled"), SNAME("EditorIcons"));
  2132. } break;
  2133. case NOTIFICATION_INTERNAL_PROCESS: {
  2134. if (tile_set_changed_needs_update) {
  2135. // Read-only is off by default
  2136. read_only = false;
  2137. // Add the listener again and check for read-only status.
  2138. if (tile_set.is_valid()) {
  2139. read_only = EditorNode::get_singleton()->is_resource_read_only(tile_set);
  2140. }
  2141. // Disable buttons in read-only mode.
  2142. tool_paint_button->set_disabled(read_only);
  2143. tools_settings_erase_button->set_disabled(read_only);
  2144. tool_advanced_menu_button->set_disabled(read_only);
  2145. // Update everything.
  2146. _update_source_inspector();
  2147. // Update the selected tile.
  2148. _update_fix_selected_and_hovered_tiles();
  2149. _update_tile_id_label();
  2150. _update_atlas_view();
  2151. _update_atlas_source_inspector();
  2152. _update_tile_inspector();
  2153. _update_tile_data_editors();
  2154. _update_current_tile_data_editor();
  2155. tile_set_changed_needs_update = false;
  2156. }
  2157. } break;
  2158. case NOTIFICATION_EXIT_TREE: {
  2159. for (KeyValue<String, TileDataEditor *> &E : tile_data_editors) {
  2160. Control *toolbar = E.value->get_toolbar();
  2161. if (toolbar->get_parent() == tool_settings_tile_data_toolbar_container) {
  2162. tool_settings_tile_data_toolbar_container->remove_child(toolbar);
  2163. }
  2164. }
  2165. } break;
  2166. }
  2167. }
  2168. void TileSetAtlasSourceEditor::_bind_methods() {
  2169. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &TileSetAtlasSourceEditor::_unhandled_key_input);
  2170. ClassDB::bind_method(D_METHOD("_set_selection_from_array"), &TileSetAtlasSourceEditor::_set_selection_from_array);
  2171. ADD_SIGNAL(MethodInfo("source_id_changed", PropertyInfo(Variant::INT, "source_id")));
  2172. }
  2173. TileSetAtlasSourceEditor::TileSetAtlasSourceEditor() {
  2174. set_process_unhandled_key_input(true);
  2175. set_process_internal(true);
  2176. // Middle panel.
  2177. VBoxContainer *middle_vbox_container = memnew(VBoxContainer);
  2178. middle_vbox_container->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  2179. add_child(middle_vbox_container);
  2180. // -- Toolbox --
  2181. tools_button_group.instantiate();
  2182. tools_button_group->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_fix_selected_and_hovered_tiles).unbind(1));
  2183. tools_button_group->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_tile_id_label).unbind(1));
  2184. tools_button_group->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_atlas_source_inspector).unbind(1));
  2185. tools_button_group->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_tile_inspector).unbind(1));
  2186. tools_button_group->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_tile_data_editors).unbind(1));
  2187. tools_button_group->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_current_tile_data_editor).unbind(1));
  2188. tools_button_group->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_atlas_view).unbind(1));
  2189. tools_button_group->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_toolbar).unbind(1));
  2190. HBoxContainer *toolbox = memnew(HBoxContainer);
  2191. middle_vbox_container->add_child(toolbox);
  2192. tool_setup_atlas_source_button = memnew(Button);
  2193. tool_setup_atlas_source_button->set_text(TTR("Setup"));
  2194. tool_setup_atlas_source_button->set_flat(true);
  2195. tool_setup_atlas_source_button->set_toggle_mode(true);
  2196. tool_setup_atlas_source_button->set_pressed(true);
  2197. tool_setup_atlas_source_button->set_button_group(tools_button_group);
  2198. tool_setup_atlas_source_button->set_tooltip_text(TTR("Atlas setup. Add/Remove tiles tool (use the shift key to create big tiles, control for rectangle editing)."));
  2199. toolbox->add_child(tool_setup_atlas_source_button);
  2200. tool_select_button = memnew(Button);
  2201. tool_select_button->set_text(TTR("Select"));
  2202. tool_select_button->set_flat(true);
  2203. tool_select_button->set_toggle_mode(true);
  2204. tool_select_button->set_pressed(false);
  2205. tool_select_button->set_button_group(tools_button_group);
  2206. tool_select_button->set_tooltip_text(TTR("Select tiles."));
  2207. toolbox->add_child(tool_select_button);
  2208. tool_paint_button = memnew(Button);
  2209. tool_paint_button->set_text(TTR("Paint"));
  2210. tool_paint_button->set_flat(true);
  2211. tool_paint_button->set_toggle_mode(true);
  2212. tool_paint_button->set_button_group(tools_button_group);
  2213. tool_paint_button->set_tooltip_text(TTR("Paint properties."));
  2214. toolbox->add_child(tool_paint_button);
  2215. // Tile inspector.
  2216. tile_proxy_object = memnew(AtlasTileProxyObject(this));
  2217. tile_proxy_object->connect("changed", callable_mp(this, &TileSetAtlasSourceEditor::_tile_proxy_object_changed));
  2218. tile_inspector = memnew(EditorInspector);
  2219. tile_inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  2220. tile_inspector->set_show_categories(true);
  2221. tile_inspector->edit(tile_proxy_object);
  2222. tile_inspector->set_use_folding(true);
  2223. tile_inspector->connect("property_selected", callable_mp(this, &TileSetAtlasSourceEditor::_inspector_property_selected));
  2224. middle_vbox_container->add_child(tile_inspector);
  2225. tile_inspector_no_tile_selected_label = memnew(Label);
  2226. tile_inspector_no_tile_selected_label->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER);
  2227. tile_inspector_no_tile_selected_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2228. tile_inspector_no_tile_selected_label->set_text(TTR("No tiles selected."));
  2229. middle_vbox_container->add_child(tile_inspector_no_tile_selected_label);
  2230. // Property values palette.
  2231. tile_data_editors_scroll = memnew(ScrollContainer);
  2232. tile_data_editors_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  2233. tile_data_editors_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  2234. middle_vbox_container->add_child(tile_data_editors_scroll);
  2235. VBoxContainer *tile_data_editors_vbox = memnew(VBoxContainer);
  2236. tile_data_editors_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  2237. tile_data_editors_scroll->add_child(tile_data_editors_vbox);
  2238. tile_data_editors_popup = memnew(Popup);
  2239. tile_data_editors_label = memnew(Label);
  2240. tile_data_editors_label->set_text(TTR("Paint Properties:"));
  2241. tile_data_editors_label->set_theme_type_variation("HeaderSmall");
  2242. tile_data_editors_vbox->add_child(tile_data_editors_label);
  2243. tile_data_editor_dropdown_button = memnew(Button);
  2244. tile_data_editor_dropdown_button->connect("draw", callable_mp(this, &TileSetAtlasSourceEditor::_tile_data_editor_dropdown_button_draw));
  2245. tile_data_editor_dropdown_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_tile_data_editor_dropdown_button_pressed));
  2246. tile_data_editors_vbox->add_child(tile_data_editor_dropdown_button);
  2247. tile_data_editor_dropdown_button->add_child(tile_data_editors_popup);
  2248. tile_data_editors_tree = memnew(Tree);
  2249. tile_data_editors_tree->set_hide_root(true);
  2250. tile_data_editors_tree->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2251. tile_data_editors_tree->set_h_scroll_enabled(false);
  2252. tile_data_editors_tree->set_v_scroll_enabled(false);
  2253. tile_data_editors_tree->connect("item_selected", callable_mp(this, &TileSetAtlasSourceEditor::_tile_data_editors_tree_selected));
  2254. tile_data_editors_popup->add_child(tile_data_editors_tree);
  2255. tile_data_painting_editor_container = memnew(VBoxContainer);
  2256. tile_data_painting_editor_container->set_h_size_flags(SIZE_EXPAND_FILL);
  2257. tile_data_editors_vbox->add_child(tile_data_painting_editor_container);
  2258. // Atlas source inspector.
  2259. atlas_source_proxy_object = memnew(TileSetAtlasSourceProxyObject());
  2260. atlas_source_proxy_object->connect("changed", callable_mp(this, &TileSetAtlasSourceEditor::_atlas_source_proxy_object_changed));
  2261. atlas_source_inspector = memnew(EditorInspector);
  2262. atlas_source_inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  2263. atlas_source_inspector->set_show_categories(true);
  2264. atlas_source_inspector->edit(atlas_source_proxy_object);
  2265. middle_vbox_container->add_child(atlas_source_inspector);
  2266. // -- Right side --
  2267. VBoxContainer *right_vbox_container = memnew(VBoxContainer);
  2268. add_child(right_vbox_container);
  2269. // Tool settings.
  2270. tool_settings = memnew(HBoxContainer);
  2271. right_vbox_container->add_child(tool_settings);
  2272. tool_settings_tile_data_toolbar_container = memnew(HBoxContainer);
  2273. tool_settings->add_child(tool_settings_tile_data_toolbar_container);
  2274. tools_settings_erase_button = memnew(Button);
  2275. tools_settings_erase_button->set_flat(true);
  2276. tools_settings_erase_button->set_toggle_mode(true);
  2277. tools_settings_erase_button->set_shortcut(ED_SHORTCUT("tiles_editor/eraser", TTR("Eraser"), Key::E));
  2278. tools_settings_erase_button->set_shortcut_context(this);
  2279. tool_settings->add_child(tools_settings_erase_button);
  2280. tool_advanced_menu_button = memnew(MenuButton);
  2281. tool_advanced_menu_button->set_flat(true);
  2282. tool_advanced_menu_button->get_popup()->add_item(TTR("Create Tiles in Non-Transparent Texture Regions"), ADVANCED_AUTO_CREATE_TILES);
  2283. tool_advanced_menu_button->get_popup()->add_item(TTR("Remove Tiles in Fully Transparent Texture Regions"), ADVANCED_AUTO_REMOVE_TILES);
  2284. tool_advanced_menu_button->get_popup()->connect("id_pressed", callable_mp(this, &TileSetAtlasSourceEditor::_menu_option));
  2285. tool_settings->add_child(tool_advanced_menu_button);
  2286. _update_toolbar();
  2287. // Right side of toolbar.
  2288. Control *middle_space = memnew(Control);
  2289. middle_space->set_h_size_flags(SIZE_EXPAND_FILL);
  2290. tool_settings->add_child(middle_space);
  2291. tool_tile_id_label = memnew(Label);
  2292. tool_tile_id_label->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  2293. tool_settings->add_child(tool_tile_id_label);
  2294. _update_tile_id_label();
  2295. // Right panel.
  2296. VBoxContainer *right_panel = memnew(VBoxContainer);
  2297. right_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  2298. right_panel->set_v_size_flags(SIZE_EXPAND_FILL);
  2299. right_vbox_container->add_child(right_panel);
  2300. // Tile atlas view.
  2301. tile_atlas_view = memnew(TileAtlasView);
  2302. tile_atlas_view->set_h_size_flags(SIZE_EXPAND_FILL);
  2303. tile_atlas_view->set_v_size_flags(SIZE_EXPAND_FILL);
  2304. tile_atlas_view->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  2305. tile_atlas_view->connect("transform_changed", callable_mp(TilesEditorPlugin::get_singleton(), &TilesEditorPlugin::set_atlas_view_transform));
  2306. tile_atlas_view->connect("transform_changed", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_view_transform_changed).unbind(2));
  2307. right_panel->add_child(tile_atlas_view);
  2308. base_tile_popup_menu = memnew(PopupMenu);
  2309. base_tile_popup_menu->add_shortcut(ED_SHORTCUT("tiles_editor/delete", TTR("Delete"), Key::KEY_DELETE), TILE_DELETE);
  2310. base_tile_popup_menu->add_item(TTR("Create an Alternative Tile"), TILE_CREATE_ALTERNATIVE);
  2311. base_tile_popup_menu->connect("id_pressed", callable_mp(this, &TileSetAtlasSourceEditor::_menu_option));
  2312. tile_atlas_view->add_child(base_tile_popup_menu);
  2313. empty_base_tile_popup_menu = memnew(PopupMenu);
  2314. empty_base_tile_popup_menu->add_item(TTR("Create a Tile"), TILE_CREATE);
  2315. empty_base_tile_popup_menu->connect("id_pressed", callable_mp(this, &TileSetAtlasSourceEditor::_menu_option));
  2316. tile_atlas_view->add_child(empty_base_tile_popup_menu);
  2317. tile_atlas_control = memnew(Control);
  2318. tile_atlas_control->connect("draw", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_control_draw));
  2319. tile_atlas_control->connect("mouse_exited", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_control_mouse_exited));
  2320. tile_atlas_control->connect("gui_input", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_control_gui_input));
  2321. tile_atlas_view->add_control_over_atlas_tiles(tile_atlas_control);
  2322. tile_atlas_control_unscaled = memnew(Control);
  2323. tile_atlas_control_unscaled->connect("draw", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_control_unscaled_draw));
  2324. tile_atlas_view->add_control_over_atlas_tiles(tile_atlas_control_unscaled, false);
  2325. tile_atlas_control_unscaled->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  2326. alternative_tile_popup_menu = memnew(PopupMenu);
  2327. alternative_tile_popup_menu->add_shortcut(ED_SHORTCUT("tiles_editor/delete_tile", TTR("Delete"), Key::KEY_DELETE), TILE_DELETE);
  2328. alternative_tile_popup_menu->connect("id_pressed", callable_mp(this, &TileSetAtlasSourceEditor::_menu_option));
  2329. tile_atlas_view->add_child(alternative_tile_popup_menu);
  2330. alternative_tiles_control = memnew(Control);
  2331. alternative_tiles_control->connect("draw", callable_mp(this, &TileSetAtlasSourceEditor::_tile_alternatives_control_draw));
  2332. alternative_tiles_control->connect("mouse_exited", callable_mp(this, &TileSetAtlasSourceEditor::_tile_alternatives_control_mouse_exited));
  2333. alternative_tiles_control->connect("gui_input", callable_mp(this, &TileSetAtlasSourceEditor::_tile_alternatives_control_gui_input));
  2334. tile_atlas_view->add_control_over_alternative_tiles(alternative_tiles_control);
  2335. alternative_tiles_control_unscaled = memnew(Control);
  2336. alternative_tiles_control_unscaled->connect("draw", callable_mp(this, &TileSetAtlasSourceEditor::_tile_alternatives_control_unscaled_draw));
  2337. tile_atlas_view->add_control_over_alternative_tiles(alternative_tiles_control_unscaled, false);
  2338. alternative_tiles_control_unscaled->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  2339. EditorNode::get_singleton()->get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &TileSetAtlasSourceEditor::_undo_redo_inspector_callback));
  2340. // -- Dialogs --
  2341. confirm_auto_create_tiles = memnew(AcceptDialog);
  2342. confirm_auto_create_tiles->set_title(TTR("Auto Create Tiles in Non-Transparent Texture Regions?"));
  2343. confirm_auto_create_tiles->set_text(TTR("The atlas's texture was modified.\nWould you like to automatically create tiles in the atlas?"));
  2344. confirm_auto_create_tiles->set_ok_button_text(TTR("Yes"));
  2345. confirm_auto_create_tiles->add_cancel_button()->set_text(TTR("No"));
  2346. confirm_auto_create_tiles->connect("confirmed", callable_mp(this, &TileSetAtlasSourceEditor::_auto_create_tiles));
  2347. add_child(confirm_auto_create_tiles);
  2348. // Inspector plugin.
  2349. Ref<EditorInspectorPluginTileData> tile_data_inspector_plugin;
  2350. tile_data_inspector_plugin.instantiate();
  2351. EditorInspector::add_inspector_plugin(tile_data_inspector_plugin);
  2352. }
  2353. TileSetAtlasSourceEditor::~TileSetAtlasSourceEditor() {
  2354. memdelete(tile_proxy_object);
  2355. memdelete(atlas_source_proxy_object);
  2356. }
  2357. ////// EditorPropertyTilePolygon //////
  2358. void EditorPropertyTilePolygon::_add_focusable_children(Node *p_node) {
  2359. Control *control = Object::cast_to<Control>(p_node);
  2360. if (control && control->get_focus_mode() != Control::FOCUS_NONE) {
  2361. add_focusable(control);
  2362. }
  2363. for (int i = 0; i < p_node->get_child_count(); i++) {
  2364. _add_focusable_children(p_node->get_child(i));
  2365. }
  2366. }
  2367. void EditorPropertyTilePolygon::_polygons_changed() {
  2368. if (String(count_property).is_empty()) {
  2369. if (base_type == "OccluderPolygon2D") {
  2370. // Single OccluderPolygon2D.
  2371. Ref<OccluderPolygon2D> occluder;
  2372. if (generic_tile_polygon_editor->get_polygon_count() >= 1) {
  2373. occluder.instantiate();
  2374. occluder->set_polygon(generic_tile_polygon_editor->get_polygon(0));
  2375. }
  2376. emit_changed(get_edited_property(), occluder);
  2377. } else if (base_type == "NavigationPolygon") {
  2378. Ref<NavigationPolygon> navigation_polygon;
  2379. if (generic_tile_polygon_editor->get_polygon_count() >= 1) {
  2380. navigation_polygon.instantiate();
  2381. for (int i = 0; i < generic_tile_polygon_editor->get_polygon_count(); i++) {
  2382. Vector<Vector2> polygon = generic_tile_polygon_editor->get_polygon(i);
  2383. navigation_polygon->add_outline(polygon);
  2384. }
  2385. navigation_polygon->make_polygons_from_outlines();
  2386. }
  2387. emit_changed(get_edited_property(), navigation_polygon);
  2388. }
  2389. } else {
  2390. if (base_type.is_empty()) {
  2391. // Multiple array of vertices.
  2392. Vector<String> changed_properties;
  2393. Array values;
  2394. int count = generic_tile_polygon_editor->get_polygon_count();
  2395. changed_properties.push_back(count_property);
  2396. values.push_back(count);
  2397. for (int i = 0; i < count; i++) {
  2398. changed_properties.push_back(vformat(element_pattern, i));
  2399. values.push_back(generic_tile_polygon_editor->get_polygon(i));
  2400. }
  2401. emit_signal(SNAME("multiple_properties_changed"), changed_properties, values, false);
  2402. }
  2403. }
  2404. }
  2405. void EditorPropertyTilePolygon::update_property() {
  2406. TileSetAtlasSourceEditor::AtlasTileProxyObject *atlas_tile_proxy_object = Object::cast_to<TileSetAtlasSourceEditor::AtlasTileProxyObject>(get_edited_object());
  2407. ERR_FAIL_COND(!atlas_tile_proxy_object);
  2408. ERR_FAIL_COND(atlas_tile_proxy_object->get_edited_tiles().is_empty());
  2409. TileSetAtlasSource *tile_set_atlas_source = atlas_tile_proxy_object->get_edited_tile_set_atlas_source();
  2410. generic_tile_polygon_editor->set_tile_set(Ref<TileSet>(tile_set_atlas_source->get_tile_set()));
  2411. // Set the background
  2412. Vector2i coords = atlas_tile_proxy_object->get_edited_tiles().front()->get().tile;
  2413. int alternative = atlas_tile_proxy_object->get_edited_tiles().front()->get().alternative;
  2414. TileData *tile_data = tile_set_atlas_source->get_tile_data(coords, alternative);
  2415. generic_tile_polygon_editor->set_background(tile_set_atlas_source->get_texture(), tile_set_atlas_source->get_tile_texture_region(coords), tile_data->get_texture_origin(), tile_data->get_flip_h(), tile_data->get_flip_v(), tile_data->get_transpose(), tile_data->get_modulate());
  2416. // Reset the polygons.
  2417. generic_tile_polygon_editor->clear_polygons();
  2418. if (String(count_property).is_empty()) {
  2419. if (base_type == "OccluderPolygon2D") {
  2420. // Single OccluderPolygon2D.
  2421. Ref<OccluderPolygon2D> occluder = get_edited_property_value();
  2422. generic_tile_polygon_editor->clear_polygons();
  2423. if (occluder.is_valid()) {
  2424. generic_tile_polygon_editor->add_polygon(occluder->get_polygon());
  2425. }
  2426. } else if (base_type == "NavigationPolygon") {
  2427. // Single OccluderPolygon2D.
  2428. Ref<NavigationPolygon> navigation_polygon = get_edited_property_value();
  2429. generic_tile_polygon_editor->clear_polygons();
  2430. if (navigation_polygon.is_valid()) {
  2431. for (int i = 0; i < navigation_polygon->get_outline_count(); i++) {
  2432. generic_tile_polygon_editor->add_polygon(navigation_polygon->get_outline(i));
  2433. }
  2434. }
  2435. }
  2436. } else {
  2437. int count = get_edited_object()->get(count_property);
  2438. if (base_type.is_empty()) {
  2439. // Multiple array of vertices.
  2440. generic_tile_polygon_editor->clear_polygons();
  2441. for (int i = 0; i < count; i++) {
  2442. generic_tile_polygon_editor->add_polygon(get_edited_object()->get(vformat(element_pattern, i)));
  2443. }
  2444. }
  2445. }
  2446. }
  2447. void EditorPropertyTilePolygon::setup_single_mode(const StringName &p_property, const String &p_base_type) {
  2448. set_object_and_property(nullptr, p_property);
  2449. base_type = p_base_type;
  2450. generic_tile_polygon_editor->set_multiple_polygon_mode(false);
  2451. }
  2452. void EditorPropertyTilePolygon::setup_multiple_mode(const StringName &p_property, const StringName &p_count_property, const String &p_element_pattern, const String &p_base_type) {
  2453. set_object_and_property(nullptr, p_property);
  2454. count_property = p_count_property;
  2455. element_pattern = p_element_pattern;
  2456. base_type = p_base_type;
  2457. generic_tile_polygon_editor->set_multiple_polygon_mode(true);
  2458. }
  2459. EditorPropertyTilePolygon::EditorPropertyTilePolygon() {
  2460. // Setup the polygon editor.
  2461. generic_tile_polygon_editor = memnew(GenericTilePolygonEditor);
  2462. generic_tile_polygon_editor->set_use_undo_redo(false);
  2463. generic_tile_polygon_editor->clear_polygons();
  2464. add_child(generic_tile_polygon_editor);
  2465. generic_tile_polygon_editor->connect("polygons_changed", callable_mp(this, &EditorPropertyTilePolygon::_polygons_changed));
  2466. // Add all focussable children of generic_tile_polygon_editor as focussable.
  2467. _add_focusable_children(generic_tile_polygon_editor);
  2468. }
  2469. ////// EditorInspectorPluginTileData //////
  2470. bool EditorInspectorPluginTileData::can_handle(Object *p_object) {
  2471. return Object::cast_to<TileSetAtlasSourceEditor::AtlasTileProxyObject>(p_object) != nullptr;
  2472. }
  2473. bool EditorInspectorPluginTileData::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  2474. Vector<String> components = String(p_path).split("/", true, 2);
  2475. if (components.size() == 2 && components[0].begins_with("occlusion_layer_") && components[0].trim_prefix("occlusion_layer_").is_valid_int()) {
  2476. // Occlusion layers.
  2477. int layer_index = components[0].trim_prefix("occlusion_layer_").to_int();
  2478. ERR_FAIL_COND_V(layer_index < 0, false);
  2479. if (components[1] == "polygon") {
  2480. EditorPropertyTilePolygon *ep = memnew(EditorPropertyTilePolygon);
  2481. ep->setup_single_mode(p_path, "OccluderPolygon2D");
  2482. add_property_editor(p_path, ep);
  2483. return true;
  2484. }
  2485. } else if (components.size() >= 2 && components[0].begins_with("physics_layer_") && components[0].trim_prefix("physics_layer_").is_valid_int()) {
  2486. // Physics layers.
  2487. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  2488. ERR_FAIL_COND_V(layer_index < 0, false);
  2489. if (components[1] == "polygons_count") {
  2490. EditorPropertyTilePolygon *ep = memnew(EditorPropertyTilePolygon);
  2491. ep->setup_multiple_mode(vformat("physics_layer_%d/polygons", layer_index), vformat("physics_layer_%d/polygons_count", layer_index), vformat("physics_layer_%d/polygon_%%d/points", layer_index), "");
  2492. Vector<String> properties;
  2493. properties.push_back(p_path);
  2494. int count = p_object->get(vformat("physics_layer_%d/polygons_count", layer_index));
  2495. for (int i = 0; i < count; i++) {
  2496. properties.push_back(vformat(vformat("physics_layer_%d/polygon_%d/points", layer_index, i)));
  2497. }
  2498. add_property_editor_for_multiple_properties("Polygons", properties, ep);
  2499. return true;
  2500. } else if (components.size() == 3 && components[1].begins_with("polygon_") && components[1].trim_prefix("polygon_").is_valid_int()) {
  2501. int polygon_index = components[1].trim_prefix("polygon_").to_int();
  2502. ERR_FAIL_COND_V(polygon_index < 0, false);
  2503. if (components[2] == "points") {
  2504. return true;
  2505. }
  2506. }
  2507. } else if (components.size() == 2 && components[0].begins_with("navigation_layer_") && components[0].trim_prefix("navigation_layer_").is_valid_int()) {
  2508. // Navigation layers.
  2509. int layer_index = components[0].trim_prefix("navigation_layer_").to_int();
  2510. ERR_FAIL_COND_V(layer_index < 0, false);
  2511. if (components[1] == "polygon") {
  2512. EditorPropertyTilePolygon *ep = memnew(EditorPropertyTilePolygon);
  2513. ep->setup_single_mode(p_path, "NavigationPolygon");
  2514. add_property_editor(p_path, ep);
  2515. return true;
  2516. }
  2517. }
  2518. return false;
  2519. }