tile_set_atlas_source_editor.cpp 131 KB

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