tile_set_atlas_source_editor.cpp 142 KB

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