tile_set_atlas_source_editor.cpp 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_scale.h"
  34. #include "editor/progress_dialog.h"
  35. #include "scene/gui/box_container.h"
  36. #include "scene/gui/button.h"
  37. #include "scene/gui/control.h"
  38. #include "scene/gui/item_list.h"
  39. #include "scene/gui/separator.h"
  40. #include "scene/gui/split_container.h"
  41. #include "scene/gui/tab_container.h"
  42. #include "core/core_string_names.h"
  43. #include "core/math/geometry_2d.h"
  44. #include "core/os/keyboard.h"
  45. void TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::set_id(int p_id) {
  46. ERR_FAIL_COND(p_id < 0);
  47. if (source_id == p_id) {
  48. return;
  49. }
  50. ERR_FAIL_COND_MSG(tile_set->has_source(p_id), vformat("Cannot change TileSet atlas source ID. Another atlas source exists with id %d.", p_id));
  51. int previous_source = source_id;
  52. source_id = p_id; // source_id must be updated before, because it's used by the atlas source list update.
  53. tile_set->set_source_id(previous_source, p_id);
  54. emit_signal("changed", "id");
  55. }
  56. int TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::get_id() {
  57. return source_id;
  58. }
  59. bool TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::_set(const StringName &p_name, const Variant &p_value) {
  60. bool valid = false;
  61. tile_set_atlas_source->set(p_name, p_value, &valid);
  62. if (valid) {
  63. emit_signal("changed", String(p_name).utf8().get_data());
  64. }
  65. return valid;
  66. }
  67. bool TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::_get(const StringName &p_name, Variant &r_ret) const {
  68. if (!tile_set_atlas_source) {
  69. return false;
  70. }
  71. bool valid = false;
  72. r_ret = tile_set_atlas_source->get(p_name, &valid);
  73. return valid;
  74. }
  75. void TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::_get_property_list(List<PropertyInfo> *p_list) const {
  76. p_list->push_back(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"));
  77. p_list->push_back(PropertyInfo(Variant::VECTOR2I, "margins", PROPERTY_HINT_NONE, ""));
  78. p_list->push_back(PropertyInfo(Variant::VECTOR2I, "separation", PROPERTY_HINT_NONE, ""));
  79. p_list->push_back(PropertyInfo(Variant::VECTOR2I, "tile_size", PROPERTY_HINT_NONE, ""));
  80. }
  81. void TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::_bind_methods() {
  82. // -- Shape and layout --
  83. ClassDB::bind_method(D_METHOD("set_id", "id"), &TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::set_id);
  84. ClassDB::bind_method(D_METHOD("get_id"), &TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::get_id);
  85. ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id");
  86. ADD_SIGNAL(MethodInfo("changed", PropertyInfo(Variant::STRING, "what")));
  87. }
  88. void TileSetAtlasSourceEditor::TileSetAtlasSourceProxyObject::edit(Ref<TileSet> p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id) {
  89. ERR_FAIL_COND(!p_tile_set.is_valid());
  90. ERR_FAIL_COND(!p_tile_set_atlas_source);
  91. ERR_FAIL_COND(p_source_id < 0);
  92. ERR_FAIL_COND(p_tile_set->get_source(p_source_id) != p_tile_set_atlas_source);
  93. // Disconnect to changes.
  94. if (tile_set_atlas_source) {
  95. tile_set_atlas_source->disconnect(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed));
  96. }
  97. tile_set = p_tile_set;
  98. tile_set_atlas_source = p_tile_set_atlas_source;
  99. source_id = p_source_id;
  100. // Connect to changes.
  101. if (tile_set_atlas_source) {
  102. if (!tile_set_atlas_source->is_connected(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed))) {
  103. tile_set_atlas_source->connect(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed));
  104. }
  105. }
  106. notify_property_list_changed();
  107. }
  108. // -- Proxy object used by the tile inspector --
  109. bool TileSetAtlasSourceEditor::TileProxyObject::_set(const StringName &p_name, const Variant &p_value) {
  110. if (!tile_set_atlas_source) {
  111. return false;
  112. }
  113. if (tiles.size() == 1) {
  114. const Vector2i &coords = tiles.front()->get().tile;
  115. const int &alternative = tiles.front()->get().alternative;
  116. if (alternative == 0 && p_name == "atlas_coords") {
  117. Vector2i as_vector2i = Vector2i(p_value);
  118. ERR_FAIL_COND_V(!tile_set_atlas_source->can_move_tile_in_atlas(coords, as_vector2i), false);
  119. if (tiles_set_atlas_source_editor->selection.front()->get().tile == coords) {
  120. tiles_set_atlas_source_editor->selection.clear();
  121. tiles_set_atlas_source_editor->selection.insert({ as_vector2i, 0 });
  122. tiles_set_atlas_source_editor->_update_tile_id_label();
  123. }
  124. tile_set_atlas_source->move_tile_in_atlas(coords, as_vector2i);
  125. tiles.clear();
  126. tiles.insert({ as_vector2i, 0 });
  127. emit_signal("changed", "atlas_coords");
  128. return true;
  129. } else if (alternative == 0 && p_name == "size_in_atlas") {
  130. Vector2i as_vector2i = Vector2i(p_value);
  131. ERR_FAIL_COND_V(!tile_set_atlas_source->can_move_tile_in_atlas(coords, TileSetAtlasSource::INVALID_ATLAS_COORDS, as_vector2i), false);
  132. tile_set_atlas_source->move_tile_in_atlas(coords, TileSetAtlasSource::INVALID_ATLAS_COORDS, as_vector2i);
  133. emit_signal("changed", "size_in_atlas");
  134. return true;
  135. } else if (alternative > 0 && p_name == "alternative_id") {
  136. int as_int = int(p_value);
  137. ERR_FAIL_COND_V(as_int < 0, false);
  138. 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));
  139. if (tiles_set_atlas_source_editor->selection.front()->get().alternative == alternative) {
  140. tiles_set_atlas_source_editor->selection.clear();
  141. tiles_set_atlas_source_editor->selection.insert({ coords, as_int });
  142. }
  143. int previous_alternative_tile = alternative;
  144. tiles.clear();
  145. tiles.insert({ coords, as_int }); // tiles must be updated before.
  146. tile_set_atlas_source->set_alternative_tile_id(coords, previous_alternative_tile, as_int);
  147. emit_signal("changed", "alternative_id");
  148. return true;
  149. }
  150. }
  151. bool any_valid = false;
  152. for (Set<TileSelection>::Element *E = tiles.front(); E; E = E->next()) {
  153. const Vector2i &coords = E->get().tile;
  154. const int &alternative = E->get().alternative;
  155. bool valid = false;
  156. TileData *tile_data = Object::cast_to<TileData>(tile_set_atlas_source->get_tile_data(coords, alternative));
  157. ERR_FAIL_COND_V(!tile_data, false);
  158. tile_data->set(p_name, p_value, &valid);
  159. any_valid |= valid;
  160. }
  161. if (any_valid) {
  162. emit_signal("changed", String(p_name).utf8().get_data());
  163. }
  164. return any_valid;
  165. }
  166. bool TileSetAtlasSourceEditor::TileProxyObject::_get(const StringName &p_name, Variant &r_ret) const {
  167. if (!tile_set_atlas_source) {
  168. return false;
  169. }
  170. if (tiles.size() == 1) {
  171. const Vector2i &coords = tiles.front()->get().tile;
  172. const int &alternative = tiles.front()->get().alternative;
  173. if (alternative == 0 && p_name == "atlas_coords") {
  174. r_ret = coords;
  175. return true;
  176. } else if (alternative == 0 && p_name == "size_in_atlas") {
  177. r_ret = tile_set_atlas_source->get_tile_size_in_atlas(coords);
  178. return true;
  179. } else if (alternative > 0 && p_name == "alternative_id") {
  180. r_ret = alternative;
  181. return true;
  182. }
  183. }
  184. for (Set<TileSelection>::Element *E = tiles.front(); E; E = E->next()) {
  185. // Return the first tile with a property matching the name.
  186. // Note: It's a little bit annoying, but the behavior is the same the one in MultiNodeEdit.
  187. const Vector2i &coords = E->get().tile;
  188. const int &alternative = E->get().alternative;
  189. TileData *tile_data = Object::cast_to<TileData>(tile_set_atlas_source->get_tile_data(coords, alternative));
  190. ERR_FAIL_COND_V(!tile_data, false);
  191. bool valid = false;
  192. r_ret = tile_data->get(p_name, &valid);
  193. if (valid) {
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. void TileSetAtlasSourceEditor::TileProxyObject::_get_property_list(List<PropertyInfo> *p_list) const {
  200. if (!tile_set_atlas_source) {
  201. return;
  202. }
  203. if (tiles.size() == 1) {
  204. if (tiles.front()->get().alternative == 0) {
  205. p_list->push_back(PropertyInfo(Variant::VECTOR2I, "atlas_coords", PROPERTY_HINT_NONE, ""));
  206. p_list->push_back(PropertyInfo(Variant::VECTOR2I, "size_in_atlas", PROPERTY_HINT_NONE, ""));
  207. } else {
  208. p_list->push_back(PropertyInfo(Variant::INT, "alternative_id", PROPERTY_HINT_NONE, ""));
  209. }
  210. }
  211. // Get the list of properties common to all tiles (similar to what's done in MultiNodeEdit).
  212. struct PropertyId {
  213. int occurence_id = 0;
  214. String property;
  215. bool operator<(const PropertyId &p_other) const {
  216. return occurence_id == p_other.occurence_id ? property < p_other.property : occurence_id < p_other.occurence_id;
  217. }
  218. };
  219. struct PLData {
  220. int uses = 0;
  221. PropertyInfo property_info;
  222. };
  223. Map<PropertyId, PLData> usage;
  224. List<PLData *> data_list;
  225. for (Set<TileSelection>::Element *E = tiles.front(); E; E = E->next()) {
  226. const Vector2i &coords = E->get().tile;
  227. const int &alternative = E->get().alternative;
  228. TileData *tile_data = Object::cast_to<TileData>(tile_set_atlas_source->get_tile_data(coords, alternative));
  229. ERR_FAIL_COND(!tile_data);
  230. List<PropertyInfo> list;
  231. tile_data->get_property_list(&list);
  232. Map<String, int> counts; // Counts the number of time a property appears (useful for groups that may appear more than once)
  233. for (List<PropertyInfo>::Element *E_property = list.front(); E_property; E_property = E_property->next()) {
  234. const String &property_string = E_property->get().name;
  235. if (!tile_data->is_allowing_transform() && (property_string == "flip_h" || property_string == "flip_v" || property_string == "transpose")) {
  236. continue;
  237. }
  238. if (!counts.has(property_string)) {
  239. counts[property_string] = 1;
  240. } else {
  241. counts[property_string] += 1;
  242. }
  243. PropertyInfo stored_property_info = E_property->get();
  244. stored_property_info.usage |= PROPERTY_USAGE_STORAGE; // Ignore the storage flag in comparing properties.
  245. PropertyId id = { counts[property_string], property_string };
  246. if (!usage.has(id)) {
  247. usage[id] = { 1, stored_property_info };
  248. data_list.push_back(&usage[id]);
  249. } else if (usage[id].property_info == stored_property_info) {
  250. usage[id].uses += 1;
  251. }
  252. }
  253. }
  254. // Add only properties that are common to all tiles.
  255. for (List<PLData *>::Element *E = data_list.front(); E; E = E->next()) {
  256. if (E->get()->uses == tiles.size()) {
  257. p_list->push_back(E->get()->property_info);
  258. }
  259. }
  260. }
  261. void TileSetAtlasSourceEditor::TileProxyObject::edit(TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id, Set<TileSelection> p_tiles) {
  262. ERR_FAIL_COND(!p_tile_set_atlas_source);
  263. ERR_FAIL_COND(p_source_id < 0);
  264. ERR_FAIL_COND(p_tiles.is_empty());
  265. for (Set<TileSelection>::Element *E = p_tiles.front(); E; E = E->next()) {
  266. ERR_FAIL_COND(E->get().tile == TileSetAtlasSource::INVALID_ATLAS_COORDS);
  267. ERR_FAIL_COND(E->get().alternative < 0);
  268. }
  269. // Disconnect to changes.
  270. for (Set<TileSelection>::Element *E = tiles.front(); E; E = E->next()) {
  271. const Vector2i &coords = E->get().tile;
  272. const int &alternative = E->get().alternative;
  273. if (tile_set_atlas_source && tile_set_atlas_source->has_tile(coords) && tile_set_atlas_source->has_alternative_tile(coords, alternative)) {
  274. TileData *tile_data = Object::cast_to<TileData>(tile_set_atlas_source->get_tile_data(coords, alternative));
  275. if (tile_data->is_connected(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed))) {
  276. tile_data->disconnect(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed));
  277. }
  278. }
  279. }
  280. tile_set_atlas_source = p_tile_set_atlas_source;
  281. source_id = p_source_id;
  282. tiles = Set<TileSelection>(p_tiles);
  283. // Connect to changes.
  284. for (Set<TileSelection>::Element *E = p_tiles.front(); E; E = E->next()) {
  285. const Vector2i &coords = E->get().tile;
  286. const int &alternative = E->get().alternative;
  287. if (tile_set_atlas_source->has_tile(coords) && tile_set_atlas_source->has_alternative_tile(coords, alternative)) {
  288. TileData *tile_data = Object::cast_to<TileData>(tile_set_atlas_source->get_tile_data(coords, alternative));
  289. if (!tile_data->is_connected(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed))) {
  290. tile_data->connect(CoreStringNames::get_singleton()->property_list_changed, callable_mp((Object *)this, &Object::notify_property_list_changed));
  291. }
  292. }
  293. }
  294. notify_property_list_changed();
  295. }
  296. void TileSetAtlasSourceEditor::TileProxyObject::_bind_methods() {
  297. ADD_SIGNAL(MethodInfo("changed", PropertyInfo(Variant::STRING, "what")));
  298. }
  299. void TileSetAtlasSourceEditor::_inspector_property_selected(String p_property) {
  300. selected_property = p_property;
  301. _update_atlas_view();
  302. }
  303. void TileSetAtlasSourceEditor::_update_tile_id_label() {
  304. if (selection.size() == 1) {
  305. TileSelection selected = selection.front()->get();
  306. tool_tile_id_label->set_text(vformat("%d, %s, %d", tile_set_atlas_source_id, selected.tile, selected.alternative));
  307. tool_tile_id_label->set_tooltip(vformat(TTR("Selected tile:\nSource: %d\nAtlas coordinates: %s\nAlternative: %d"), tile_set_atlas_source_id, selected.tile, selected.alternative));
  308. tool_tile_id_label->show();
  309. } else {
  310. tool_tile_id_label->hide();
  311. }
  312. }
  313. void TileSetAtlasSourceEditor::_update_source_inspector() {
  314. // Update the proxy object.
  315. atlas_source_proxy_object->edit(tile_set, tile_set_atlas_source, tile_set_atlas_source_id);
  316. // Update the "clear outside texture" button.
  317. tool_advanced_menu_buttom->get_popup()->set_item_disabled(0, !tile_set_atlas_source->has_tiles_outside_texture());
  318. }
  319. void TileSetAtlasSourceEditor::_update_fix_selected_and_hovered_tiles() {
  320. // Fix selected.
  321. for (Set<TileSelection>::Element *E = selection.front(); E; E = E->next()) {
  322. TileSelection selected = E->get();
  323. if (!tile_set_atlas_source->has_tile(selected.tile) || !tile_set_atlas_source->has_alternative_tile(selected.tile, selected.alternative)) {
  324. selection.erase(E);
  325. }
  326. }
  327. // Fix hovered.
  328. if (!tile_set_atlas_source->has_tile(hovered_base_tile_coords)) {
  329. hovered_base_tile_coords = TileSetAtlasSource::INVALID_ATLAS_COORDS;
  330. }
  331. Vector2i coords = Vector2i(hovered_alternative_tile_coords.x, hovered_alternative_tile_coords.y);
  332. int alternative = hovered_alternative_tile_coords.z;
  333. if (!tile_set_atlas_source->has_tile(coords) || !tile_set_atlas_source->has_alternative_tile(coords, alternative)) {
  334. hovered_alternative_tile_coords = Vector3i(TileSetAtlasSource::INVALID_ATLAS_COORDS.x, TileSetAtlasSource::INVALID_ATLAS_COORDS.y, TileSetAtlasSource::INVALID_TILE_ALTERNATIVE);
  335. }
  336. }
  337. void TileSetAtlasSourceEditor::_update_tile_inspector() {
  338. bool has_atlas_tile_selected = (tools_button_group->get_pressed_button() == tool_select_button) && !selection.is_empty();
  339. // Update the proxy object.
  340. if (has_atlas_tile_selected) {
  341. tile_proxy_object->edit(tile_set_atlas_source, tile_set_atlas_source_id, selection);
  342. }
  343. // Update visibility.
  344. tile_inspector_label->set_visible(has_atlas_tile_selected);
  345. tile_inspector->set_visible(has_atlas_tile_selected);
  346. }
  347. void TileSetAtlasSourceEditor::_update_atlas_view() {
  348. // Update the atlas display.
  349. tile_atlas_view->set_atlas_source(*tile_set, tile_set_atlas_source, tile_set_atlas_source_id);
  350. // Create a bunch of buttons to add alternative tiles.
  351. for (int i = 0; i < alternative_tiles_control->get_child_count(); i++) {
  352. alternative_tiles_control->get_child(i)->queue_delete();
  353. }
  354. Vector2i pos;
  355. Vector2 texture_region_base_size = tile_set_atlas_source->get_texture_region_size();
  356. int texture_region_base_size_min = MIN(texture_region_base_size.x, texture_region_base_size.y);
  357. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  358. Vector2i tile_id = tile_set_atlas_source->get_tile_id(i);
  359. int alternative_count = tile_set_atlas_source->get_alternative_tiles_count(tile_id);
  360. if (alternative_count > 1) {
  361. // Compute the right extremity of alternative.
  362. int y_increment = 0;
  363. pos.x = 0;
  364. for (int j = 1; j < alternative_count; j++) {
  365. int alternative_id = tile_set_atlas_source->get_alternative_tile_id(tile_id, j);
  366. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(tile_id, alternative_id);
  367. pos.x = MAX(pos.x, rect.get_end().x);
  368. y_increment = MAX(y_increment, rect.size.y);
  369. }
  370. // Create and position the button.
  371. Button *button = memnew(Button);
  372. alternative_tiles_control->add_child(button);
  373. button->set_flat(true);
  374. button->set_icon(get_theme_icon("Add", "EditorIcons"));
  375. button->add_theme_style_override("normal", memnew(StyleBoxEmpty));
  376. button->add_theme_style_override("hover", memnew(StyleBoxEmpty));
  377. button->add_theme_style_override("focus", memnew(StyleBoxEmpty));
  378. button->add_theme_style_override("pressed", memnew(StyleBoxEmpty));
  379. button->connect("pressed", callable_mp(tile_set_atlas_source, &TileSetAtlasSource::create_alternative_tile), varray(tile_id, -1));
  380. 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)));
  381. button->set_expand_icon(true);
  382. pos.y += y_increment;
  383. }
  384. }
  385. tile_atlas_view->set_padding(Side::SIDE_RIGHT, texture_region_base_size_min);
  386. // Redraw everything.
  387. tile_atlas_control->update();
  388. tile_atlas_control_unscaled->update();
  389. alternative_tiles_control->update();
  390. alternative_tiles_control_unscaled->update();
  391. tile_atlas_view->update();
  392. // Synchronize atlas view.
  393. TilesEditor::get_singleton()->synchronize_atlas_view(tile_atlas_view);
  394. }
  395. void TileSetAtlasSourceEditor::_update_toolbar() {
  396. // Hide all settings.
  397. for (int i = 0; i < tool_settings->get_child_count(); i++) {
  398. Object::cast_to<CanvasItem>(tool_settings->get_child(i))->hide();
  399. }
  400. // SHow only the correct settings.
  401. if (tools_button_group->get_pressed_button() == tool_select_button) {
  402. } else if (tools_button_group->get_pressed_button() == tool_add_remove_button) {
  403. tool_settings_vsep->show();
  404. tools_settings_erase_button->show();
  405. } else if (tools_button_group->get_pressed_button() == tool_add_remove_rect_button) {
  406. tool_settings_vsep->show();
  407. tools_settings_erase_button->show();
  408. }
  409. }
  410. void TileSetAtlasSourceEditor::_tile_atlas_control_mouse_exited() {
  411. hovered_base_tile_coords = TileSetAtlasSource::INVALID_ATLAS_COORDS;
  412. tile_atlas_control->update();
  413. tile_atlas_control_unscaled->update();
  414. tile_atlas_view->update();
  415. }
  416. void TileSetAtlasSourceEditor::_tile_atlas_view_transform_changed() {
  417. tile_atlas_control->update();
  418. tile_atlas_control_unscaled->update();
  419. }
  420. void TileSetAtlasSourceEditor::_tile_atlas_control_gui_input(const Ref<InputEvent> &p_event) {
  421. // Update the hovered coords.
  422. hovered_base_tile_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  423. // Handle the event.
  424. Ref<InputEventMouseMotion> mm = p_event;
  425. if (mm.is_valid()) {
  426. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  427. Vector2i last_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_last_mouse_pos);
  428. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  429. Vector2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  430. if (drag_type == DRAG_TYPE_NONE) {
  431. if (selection.size() == 1) {
  432. // Change the cursor depending on the hovered thing.
  433. TileSelection selected = selection.front()->get();
  434. if (selected.tile != TileSetAtlasSource::INVALID_ATLAS_COORDS && selected.alternative == 0) {
  435. Vector2 mouse_local_pos = tile_atlas_control->get_local_mouse_position();
  436. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(selected.tile);
  437. Rect2 region = tile_set_atlas_source->get_tile_texture_region(selected.tile);
  438. Size2 zoomed_size = resize_handle->get_size() / tile_atlas_view->get_zoom();
  439. Rect2 rect = region.grow_individual(zoomed_size.x, zoomed_size.y, 0, 0);
  440. const Vector2i coords[] = { Vector2i(0, 0), Vector2i(1, 0), Vector2i(1, 1), Vector2i(0, 1) };
  441. const Vector2i directions[] = { Vector2i(0, -1), Vector2i(1, 0), Vector2i(0, 1), Vector2i(-1, 0) };
  442. CursorShape cursor_shape = CURSOR_ARROW;
  443. bool can_grow[4];
  444. for (int i = 0; i < 4; i++) {
  445. can_grow[i] = tile_set_atlas_source->can_move_tile_in_atlas(selected.tile, selected.tile + directions[i]);
  446. can_grow[i] |= (i % 2 == 0) ? size_in_atlas.y > 1 : size_in_atlas.x > 1;
  447. }
  448. for (int i = 0; i < 4; i++) {
  449. Vector2 pos = rect.position + Vector2(rect.size.x, rect.size.y) * coords[i];
  450. if (can_grow[i] && can_grow[(i + 3) % 4] && Rect2(pos, zoomed_size).has_point(mouse_local_pos)) {
  451. cursor_shape = (i % 2) ? CURSOR_BDIAGSIZE : CURSOR_FDIAGSIZE;
  452. }
  453. Vector2 next_pos = rect.position + Vector2(rect.size.x, rect.size.y) * coords[(i + 1) % 4];
  454. if (can_grow[i] && Rect2((pos + next_pos) / 2.0, zoomed_size).has_point(mouse_local_pos)) {
  455. cursor_shape = (i % 2) ? CURSOR_HSIZE : CURSOR_VSIZE;
  456. }
  457. }
  458. tile_atlas_control->set_default_cursor_shape(cursor_shape);
  459. }
  460. }
  461. } else if (drag_type == DRAG_TYPE_CREATE_BIG_TILE) {
  462. // Create big tile.
  463. new_base_tiles_coords = new_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  464. Rect2i new_rect = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  465. new_rect.size += Vector2i(1, 1);
  466. // Check if the new tile can fit in the new rect.
  467. if (tile_set_atlas_source->can_move_tile_in_atlas(drag_current_tile, new_rect.position, new_rect.size)) {
  468. // Move and resize the tile.
  469. tile_set_atlas_source->move_tile_in_atlas(drag_current_tile, new_rect.position, new_rect.size);
  470. drag_current_tile = new_rect.position;
  471. }
  472. } else if (drag_type == DRAG_TYPE_CREATE_TILES) {
  473. // Create tiles.
  474. last_base_tiles_coords = last_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  475. new_base_tiles_coords = new_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  476. Vector<Point2i> line = Geometry2D::bresenham_line(last_base_tiles_coords, new_base_tiles_coords);
  477. for (int i = 0; i < line.size(); i++) {
  478. if (tile_set_atlas_source->get_tile_at_coords(line[i]) == TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  479. tile_set_atlas_source->create_tile(line[i]);
  480. drag_modified_tiles.insert(line[i]);
  481. }
  482. }
  483. drag_last_mouse_pos = tile_atlas_control->get_local_mouse_position();
  484. } else if (drag_type == DRAG_TYPE_REMOVE_TILES) {
  485. // Remove tiles.
  486. last_base_tiles_coords = last_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  487. new_base_tiles_coords = new_base_tiles_coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  488. Vector<Point2i> line = Geometry2D::bresenham_line(last_base_tiles_coords, new_base_tiles_coords);
  489. for (int i = 0; i < line.size(); i++) {
  490. Vector2i base_tile_coords = tile_set_atlas_source->get_tile_at_coords(line[i]);
  491. if (base_tile_coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  492. drag_modified_tiles.insert(base_tile_coords);
  493. }
  494. }
  495. drag_last_mouse_pos = tile_atlas_control->get_local_mouse_position();
  496. } else if (drag_type == DRAG_TYPE_MOVE_TILE) {
  497. // Move tile.
  498. 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();
  499. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position() - mouse_offset);
  500. coords = coords.max(Vector2i(0, 0)).min(grid_size - Vector2i(1, 1));
  501. if (drag_current_tile != coords && tile_set_atlas_source->can_move_tile_in_atlas(drag_current_tile, coords)) {
  502. tile_set_atlas_source->move_tile_in_atlas(drag_current_tile, coords);
  503. selection.clear();
  504. selection.insert({ coords, 0 });
  505. drag_current_tile = coords;
  506. // Update only what's needed.
  507. tile_set_atlas_source_changed_needs_update = false;
  508. _update_tile_inspector();
  509. _update_atlas_view();
  510. _update_tile_id_label();
  511. }
  512. } else if (drag_type >= DRAG_TYPE_RESIZE_TOP_LEFT && drag_type <= DRAG_TYPE_RESIZE_LEFT) {
  513. // Resizing a tile.
  514. new_base_tiles_coords = new_base_tiles_coords.max(Vector2i(-1, -1)).min(grid_size);
  515. Rect2i old_rect = Rect2i(drag_current_tile, tile_set_atlas_source->get_tile_size_in_atlas(drag_current_tile));
  516. Rect2i new_rect = old_rect;
  517. if (drag_type == DRAG_TYPE_RESIZE_LEFT || drag_type == DRAG_TYPE_RESIZE_TOP_LEFT || drag_type == DRAG_TYPE_RESIZE_BOTTOM_LEFT) {
  518. new_rect.position.x = MIN(new_base_tiles_coords.x + 1, old_rect.get_end().x - 1);
  519. new_rect.size.x = old_rect.get_end().x - new_rect.position.x;
  520. }
  521. if (drag_type == DRAG_TYPE_RESIZE_TOP || drag_type == DRAG_TYPE_RESIZE_TOP_LEFT || drag_type == DRAG_TYPE_RESIZE_TOP_RIGHT) {
  522. new_rect.position.y = MIN(new_base_tiles_coords.y + 1, old_rect.get_end().y - 1);
  523. new_rect.size.y = old_rect.get_end().y - new_rect.position.y;
  524. }
  525. if (drag_type == DRAG_TYPE_RESIZE_RIGHT || drag_type == DRAG_TYPE_RESIZE_TOP_RIGHT || drag_type == DRAG_TYPE_RESIZE_BOTTOM_RIGHT) {
  526. new_rect.set_end(Vector2i(MAX(new_base_tiles_coords.x, old_rect.position.x + 1), new_rect.get_end().y));
  527. }
  528. if (drag_type == DRAG_TYPE_RESIZE_BOTTOM || drag_type == DRAG_TYPE_RESIZE_BOTTOM_LEFT || drag_type == DRAG_TYPE_RESIZE_BOTTOM_RIGHT) {
  529. new_rect.set_end(Vector2i(new_rect.get_end().x, MAX(new_base_tiles_coords.y, old_rect.position.y + 1)));
  530. }
  531. if (tile_set_atlas_source->can_move_tile_in_atlas(drag_current_tile, new_rect.position, new_rect.size)) {
  532. tile_set_atlas_source->move_tile_in_atlas(drag_current_tile, new_rect.position, new_rect.size);
  533. selection.clear();
  534. selection.insert({ new_rect.position, 0 });
  535. drag_current_tile = new_rect.position;
  536. // Update only what's needed.
  537. tile_set_atlas_source_changed_needs_update = false;
  538. _update_tile_inspector();
  539. _update_atlas_view();
  540. _update_tile_id_label();
  541. }
  542. }
  543. // Redraw for the hovered tile.
  544. tile_atlas_control->update();
  545. tile_atlas_control_unscaled->update();
  546. alternative_tiles_control->update();
  547. alternative_tiles_control_unscaled->update();
  548. tile_atlas_view->update();
  549. return;
  550. }
  551. Ref<InputEventMouseButton> mb = p_event;
  552. if (mb.is_valid()) {
  553. Vector2 mouse_local_pos = tile_atlas_control->get_local_mouse_position();
  554. if (mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  555. if (mb->is_pressed()) {
  556. // Left click pressed.
  557. if (tools_button_group->get_pressed_button() == tool_add_remove_button) {
  558. if (tools_settings_erase_button->is_pressed()) {
  559. // Remove tiles.
  560. // Setup the dragging info.
  561. drag_type = DRAG_TYPE_REMOVE_TILES;
  562. drag_start_mouse_pos = mouse_local_pos;
  563. drag_last_mouse_pos = drag_start_mouse_pos;
  564. // Remove a first tile.
  565. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  566. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  567. coords = tile_set_atlas_source->get_tile_at_coords(coords);
  568. }
  569. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  570. drag_modified_tiles.insert(coords);
  571. }
  572. } else {
  573. if (mb->is_shift_pressed()) {
  574. // Create a big tile.
  575. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(mouse_local_pos);
  576. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS && tile_set_atlas_source->get_tile_at_coords(coords) == TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  577. // Setup the dragging info, only if we start on an empty tile.
  578. drag_type = DRAG_TYPE_CREATE_BIG_TILE;
  579. drag_start_mouse_pos = mouse_local_pos;
  580. drag_last_mouse_pos = drag_start_mouse_pos;
  581. drag_current_tile = coords;
  582. // Create a tile.
  583. tile_set_atlas_source->create_tile(coords);
  584. }
  585. } else {
  586. // Create tiles.
  587. // Setup the dragging info.
  588. drag_type = DRAG_TYPE_CREATE_TILES;
  589. drag_start_mouse_pos = mouse_local_pos;
  590. drag_last_mouse_pos = drag_start_mouse_pos;
  591. // Create a first tile if needed.
  592. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  593. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS && tile_set_atlas_source->get_tile_at_coords(coords) == TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  594. tile_set_atlas_source->create_tile(coords);
  595. drag_modified_tiles.insert(coords);
  596. }
  597. }
  598. }
  599. } else if (tools_button_group->get_pressed_button() == tool_add_remove_rect_button) {
  600. if (tools_settings_erase_button->is_pressed()) {
  601. // Remove tiles using rect.
  602. // Setup the dragging info.
  603. drag_type = DRAG_TYPE_REMOVE_TILES_USING_RECT;
  604. drag_start_mouse_pos = mouse_local_pos;
  605. drag_last_mouse_pos = drag_start_mouse_pos;
  606. } else {
  607. if (mb->is_shift_pressed()) {
  608. // Create a big tile.
  609. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(mouse_local_pos);
  610. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS && tile_set_atlas_source->get_tile_at_coords(coords) == TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  611. // Setup the dragging info, only if we start on an empty tile.
  612. drag_type = DRAG_TYPE_CREATE_BIG_TILE;
  613. drag_start_mouse_pos = mouse_local_pos;
  614. drag_last_mouse_pos = drag_start_mouse_pos;
  615. drag_current_tile = coords;
  616. // Create a tile.
  617. tile_set_atlas_source->create_tile(coords);
  618. }
  619. } else {
  620. // Create tiles using rect.
  621. drag_type = DRAG_TYPE_CREATE_TILES_USING_RECT;
  622. drag_start_mouse_pos = mouse_local_pos;
  623. drag_last_mouse_pos = drag_start_mouse_pos;
  624. }
  625. }
  626. } else if (tools_button_group->get_pressed_button() == tool_select_button) {
  627. // Dragging a handle.
  628. drag_type = DRAG_TYPE_NONE;
  629. if (selection.size() == 1) {
  630. TileSelection selected = selection.front()->get();
  631. if (selected.tile != TileSetAtlasSource::INVALID_ATLAS_COORDS && selected.alternative == 0) {
  632. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(selected.tile);
  633. Rect2 region = tile_set_atlas_source->get_tile_texture_region(selected.tile);
  634. Size2 zoomed_size = resize_handle->get_size() / tile_atlas_view->get_zoom();
  635. Rect2 rect = region.grow_individual(zoomed_size.x, zoomed_size.y, 0, 0);
  636. const Vector2i coords[] = { Vector2i(0, 0), Vector2i(1, 0), Vector2i(1, 1), Vector2i(0, 1) };
  637. const Vector2i directions[] = { Vector2i(0, -1), Vector2i(1, 0), Vector2i(0, 1), Vector2i(-1, 0) };
  638. CursorShape cursor_shape = CURSOR_ARROW;
  639. bool can_grow[4];
  640. for (int i = 0; i < 4; i++) {
  641. can_grow[i] = tile_set_atlas_source->can_move_tile_in_atlas(selected.tile, selected.tile + directions[i]);
  642. can_grow[i] |= (i % 2 == 0) ? size_in_atlas.y > 1 : size_in_atlas.x > 1;
  643. }
  644. for (int i = 0; i < 4; i++) {
  645. Vector2 pos = rect.position + Vector2(rect.size.x, rect.size.y) * coords[i];
  646. if (can_grow[i] && can_grow[(i + 3) % 4] && Rect2(pos, zoomed_size).has_point(mouse_local_pos)) {
  647. drag_type = (DragType)((int)DRAG_TYPE_RESIZE_TOP_LEFT + i * 2);
  648. drag_start_mouse_pos = mouse_local_pos;
  649. drag_last_mouse_pos = drag_start_mouse_pos;
  650. drag_current_tile = selected.tile;
  651. drag_start_tile_shape = Rect2i(selected.tile, tile_set_atlas_source->get_tile_size_in_atlas(selected.tile));
  652. cursor_shape = (i % 2) ? CURSOR_BDIAGSIZE : CURSOR_FDIAGSIZE;
  653. }
  654. Vector2 next_pos = rect.position + Vector2(rect.size.x, rect.size.y) * coords[(i + 1) % 4];
  655. if (can_grow[i] && Rect2((pos + next_pos) / 2.0, zoomed_size).has_point(mouse_local_pos)) {
  656. drag_type = (DragType)((int)DRAG_TYPE_RESIZE_TOP + i * 2);
  657. drag_start_mouse_pos = mouse_local_pos;
  658. drag_last_mouse_pos = drag_start_mouse_pos;
  659. drag_current_tile = selected.tile;
  660. drag_start_tile_shape = Rect2i(selected.tile, tile_set_atlas_source->get_tile_size_in_atlas(selected.tile));
  661. cursor_shape = (i % 2) ? CURSOR_HSIZE : CURSOR_VSIZE;
  662. }
  663. }
  664. tile_atlas_control->set_default_cursor_shape(cursor_shape);
  665. }
  666. }
  667. // Selecting then dragging a tile.
  668. if (drag_type == DRAG_TYPE_NONE) {
  669. TileSelection selected = { TileSetAtlasSource::INVALID_ATLAS_COORDS, TileSetAtlasSource::INVALID_TILE_ALTERNATIVE };
  670. Vector2i coords = tile_atlas_view->get_atlas_tile_coords_at_pos(mouse_local_pos);
  671. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  672. coords = tile_set_atlas_source->get_tile_at_coords(coords);
  673. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  674. selected = { coords, 0 };
  675. }
  676. }
  677. bool shift = mb->is_shift_pressed();
  678. if (!shift && selection.size() == 1 && selected.tile != TileSetAtlasSource::INVALID_ATLAS_COORDS && selection.has(selected)) {
  679. // Start move dragging.
  680. drag_type = DRAG_TYPE_MOVE_TILE;
  681. drag_start_mouse_pos = mouse_local_pos;
  682. drag_last_mouse_pos = drag_start_mouse_pos;
  683. drag_current_tile = selected.tile;
  684. drag_start_tile_shape = Rect2i(selected.tile, tile_set_atlas_source->get_tile_size_in_atlas(selected.tile));
  685. tile_atlas_control->set_default_cursor_shape(CURSOR_MOVE);
  686. } else {
  687. // Start selection dragging.
  688. drag_type = DRAG_TYPE_RECT_SELECT;
  689. drag_start_mouse_pos = mouse_local_pos;
  690. drag_last_mouse_pos = drag_start_mouse_pos;
  691. }
  692. }
  693. }
  694. } else {
  695. // Left click released.
  696. _end_dragging();
  697. }
  698. tile_atlas_control->update();
  699. tile_atlas_control_unscaled->update();
  700. alternative_tiles_control->update();
  701. alternative_tiles_control_unscaled->update();
  702. tile_atlas_view->update();
  703. return;
  704. } else if (mb->get_button_index() == MOUSE_BUTTON_RIGHT) {
  705. if (mb->is_pressed()) {
  706. // Right click pressed.
  707. TileSelection selected = { tile_atlas_view->get_atlas_tile_coords_at_pos(mouse_local_pos), 0 };
  708. if (selected.tile != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  709. selected.tile = tile_set_atlas_source->get_tile_at_coords(selected.tile);
  710. }
  711. // Set the selection if needed.
  712. if (selection.size() <= 1) {
  713. if (selected.tile != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  714. undo_redo->create_action(TTR("Select tiles"));
  715. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  716. selection.clear();
  717. selection.insert(selected);
  718. undo_redo->add_do_method(this, "_set_selection_from_array", _get_selection_as_array());
  719. undo_redo->commit_action(false);
  720. _update_tile_inspector();
  721. _update_tile_id_label();
  722. }
  723. }
  724. // Pops up the correct menu, depending on whether we have a tile or not.
  725. if (selected.tile != TileSetAtlasSource::INVALID_ATLAS_COORDS && selection.has(selected)) {
  726. // We have a tile.
  727. menu_option_coords = selected.tile;
  728. menu_option_alternative = 0;
  729. base_tile_popup_menu->popup(Rect2i(get_global_mouse_position(), Size2i()));
  730. } else if (hovered_base_tile_coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  731. // We don't have a tile, but can create one.
  732. menu_option_coords = hovered_base_tile_coords;
  733. menu_option_alternative = TileSetAtlasSource::INVALID_TILE_ALTERNATIVE;
  734. empty_base_tile_popup_menu->popup(Rect2i(get_global_mouse_position(), Size2i()));
  735. }
  736. } else {
  737. // Right click released.
  738. _end_dragging();
  739. }
  740. tile_atlas_control->update();
  741. tile_atlas_control_unscaled->update();
  742. alternative_tiles_control->update();
  743. alternative_tiles_control_unscaled->update();
  744. tile_atlas_view->update();
  745. return;
  746. }
  747. }
  748. }
  749. void TileSetAtlasSourceEditor::_end_dragging() {
  750. switch (drag_type) {
  751. case DRAG_TYPE_CREATE_TILES:
  752. undo_redo->create_action(TTR("Create tiles"));
  753. for (Set<Vector2i>::Element *E = drag_modified_tiles.front(); E; E = E->next()) {
  754. undo_redo->add_do_method(tile_set_atlas_source, "create_tile", E->get());
  755. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", E->get());
  756. }
  757. undo_redo->commit_action(false);
  758. break;
  759. case DRAG_TYPE_CREATE_BIG_TILE:
  760. undo_redo->create_action(TTR("Create a tile"));
  761. 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));
  762. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", drag_current_tile);
  763. undo_redo->commit_action(false);
  764. break;
  765. case DRAG_TYPE_REMOVE_TILES: {
  766. List<PropertyInfo> list;
  767. tile_set_atlas_source->get_property_list(&list);
  768. Map<Vector2i, List<const PropertyInfo *>> per_tile = _group_properties_per_tiles(list, tile_set_atlas_source);
  769. undo_redo->create_action(TTR("Remove tiles"));
  770. for (Set<Vector2i>::Element *E = drag_modified_tiles.front(); E; E = E->next()) {
  771. Vector2i coords = E->get();
  772. undo_redo->add_do_method(tile_set_atlas_source, "remove_tile", coords);
  773. undo_redo->add_undo_method(tile_set_atlas_source, "create_tile", coords);
  774. if (per_tile.has(coords)) {
  775. for (List<const PropertyInfo *>::Element *E_property = per_tile[coords].front(); E_property; E_property = E_property->next()) {
  776. String property = E_property->get()->name;
  777. Variant value = tile_set_atlas_source->get(property);
  778. if (value.get_type() != Variant::NIL) {
  779. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  780. }
  781. }
  782. }
  783. }
  784. undo_redo->commit_action();
  785. } break;
  786. case DRAG_TYPE_CREATE_TILES_USING_RECT: {
  787. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  788. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  789. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  790. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  791. undo_redo->create_action(TTR("Create tiles"));
  792. for (int x = area.get_position().x; x < area.get_end().x; x++) {
  793. for (int y = area.get_position().y; y < area.get_end().y; y++) {
  794. Vector2i coords = Vector2i(x, y);
  795. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  796. undo_redo->add_do_method(tile_set_atlas_source, "create_tile", coords);
  797. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", coords);
  798. }
  799. }
  800. }
  801. undo_redo->commit_action();
  802. } break;
  803. case DRAG_TYPE_REMOVE_TILES_USING_RECT: {
  804. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  805. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  806. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  807. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  808. List<PropertyInfo> list;
  809. tile_set_atlas_source->get_property_list(&list);
  810. Map<Vector2i, List<const PropertyInfo *>> per_tile = _group_properties_per_tiles(list, tile_set_atlas_source);
  811. Set<Vector2i> to_delete;
  812. for (int x = area.get_position().x; x < area.get_end().x; x++) {
  813. for (int y = area.get_position().y; y < area.get_end().y; y++) {
  814. Vector2i coords = tile_set_atlas_source->get_tile_at_coords(Vector2i(x, y));
  815. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  816. to_delete.insert(coords);
  817. }
  818. }
  819. }
  820. undo_redo->create_action(TTR("Remove tiles"));
  821. undo_redo->add_do_method(this, "_set_selection_from_array", Array());
  822. for (Set<Vector2i>::Element *E = to_delete.front(); E; E = E->next()) {
  823. Vector2i coords = E->get();
  824. undo_redo->add_do_method(tile_set_atlas_source, "remove_tile", coords);
  825. undo_redo->add_undo_method(tile_set_atlas_source, "create_tile", coords);
  826. if (per_tile.has(coords)) {
  827. for (List<const PropertyInfo *>::Element *E_property = per_tile[coords].front(); E_property; E_property = E_property->next()) {
  828. String property = E_property->get()->name;
  829. Variant value = tile_set_atlas_source->get(property);
  830. if (value.get_type() != Variant::NIL) {
  831. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  832. }
  833. }
  834. }
  835. }
  836. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  837. undo_redo->commit_action();
  838. } break;
  839. case DRAG_TYPE_MOVE_TILE:
  840. if (drag_current_tile != drag_start_tile_shape.position) {
  841. undo_redo->create_action(TTR("Move a tile"));
  842. 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));
  843. undo_redo->add_do_method(this, "_set_selection_from_array", _get_selection_as_array());
  844. 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);
  845. Array array;
  846. array.push_back(drag_start_tile_shape.position);
  847. array.push_back(0);
  848. undo_redo->add_undo_method(this, "_set_selection_from_array", array);
  849. undo_redo->commit_action(false);
  850. }
  851. break;
  852. case DRAG_TYPE_RECT_SELECT: {
  853. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  854. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  855. ERR_FAIL_COND(start_base_tiles_coords == TileSetAtlasSource::INVALID_ATLAS_COORDS);
  856. ERR_FAIL_COND(new_base_tiles_coords == TileSetAtlasSource::INVALID_ATLAS_COORDS);
  857. Rect2i region = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  858. region.size += Vector2i(1, 1);
  859. undo_redo->create_action(TTR("Select tiles"));
  860. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  861. // Determine if we clear, then add or remove to the selection.
  862. bool add_to_selection = true;
  863. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  864. Vector2i coords = tile_set_atlas_source->get_tile_at_coords(start_base_tiles_coords);
  865. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  866. if (selection.has({ coords, 0 })) {
  867. add_to_selection = false;
  868. }
  869. }
  870. } else {
  871. selection.clear();
  872. }
  873. // Modify the selection.
  874. for (int x = region.position.x; x < region.get_end().x; x++) {
  875. for (int y = region.position.y; y < region.get_end().y; y++) {
  876. Vector2i coords = Vector2i(x, y);
  877. coords = tile_set_atlas_source->get_tile_at_coords(coords);
  878. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  879. if (add_to_selection && !selection.has({ coords, 0 })) {
  880. selection.insert({ coords, 0 });
  881. } else if (!add_to_selection && selection.has({ coords, 0 })) {
  882. selection.erase({ coords, 0 });
  883. }
  884. }
  885. }
  886. }
  887. _update_tile_inspector();
  888. _update_tile_id_label();
  889. undo_redo->add_do_method(this, "_set_selection_from_array", _get_selection_as_array());
  890. undo_redo->commit_action(false);
  891. break;
  892. }
  893. case DRAG_TYPE_RESIZE_TOP_LEFT:
  894. case DRAG_TYPE_RESIZE_TOP:
  895. case DRAG_TYPE_RESIZE_TOP_RIGHT:
  896. case DRAG_TYPE_RESIZE_RIGHT:
  897. case DRAG_TYPE_RESIZE_BOTTOM_RIGHT:
  898. case DRAG_TYPE_RESIZE_BOTTOM:
  899. case DRAG_TYPE_RESIZE_BOTTOM_LEFT:
  900. case DRAG_TYPE_RESIZE_LEFT:
  901. if (drag_start_tile_shape != Rect2i(drag_current_tile, tile_set_atlas_source->get_tile_size_in_atlas(drag_current_tile))) {
  902. undo_redo->create_action(TTR("Resize a tile"));
  903. 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));
  904. undo_redo->add_do_method(this, "_set_selection_from_array", _get_selection_as_array());
  905. 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);
  906. Array array;
  907. array.push_back(drag_start_tile_shape.position);
  908. array.push_back(0);
  909. undo_redo->add_undo_method(this, "_set_selection_from_array", array);
  910. undo_redo->commit_action(false);
  911. }
  912. break;
  913. default:
  914. break;
  915. }
  916. drag_modified_tiles.clear();
  917. drag_type = DRAG_TYPE_NONE;
  918. tile_atlas_control->set_default_cursor_shape(CURSOR_ARROW);
  919. }
  920. Map<Vector2i, List<const PropertyInfo *>> TileSetAtlasSourceEditor::_group_properties_per_tiles(const List<PropertyInfo> &r_list, const TileSetAtlasSource *p_atlas) {
  921. // Group properties per tile.
  922. Map<Vector2i, List<const PropertyInfo *>> per_tile;
  923. for (const List<PropertyInfo>::Element *E_property = r_list.front(); E_property; E_property = E_property->next()) {
  924. Vector<String> components = String(E_property->get().name).split("/", true, 1);
  925. if (components.size() >= 1) {
  926. Vector<String> coord_arr = components[0].split(":");
  927. if (coord_arr.size() == 2 && coord_arr[0].is_valid_integer() && coord_arr[1].is_valid_integer()) {
  928. Vector2i coords = Vector2i(coord_arr[0].to_int(), coord_arr[1].to_int());
  929. per_tile[coords].push_back(&(E_property->get()));
  930. }
  931. }
  932. }
  933. return per_tile;
  934. }
  935. void TileSetAtlasSourceEditor::_menu_option(int p_option) {
  936. switch (p_option) {
  937. case TILE_DELETE: {
  938. List<PropertyInfo> list;
  939. tile_set_atlas_source->get_property_list(&list);
  940. Map<Vector2i, List<const PropertyInfo *>> per_tile = _group_properties_per_tiles(list, tile_set_atlas_source);
  941. undo_redo->create_action(TTR("Remove tile"));
  942. // Remove tiles
  943. Set<Vector2i> removed;
  944. for (Set<TileSelection>::Element *E = selection.front(); E; E = E->next()) {
  945. TileSelection selected = E->get();
  946. if (selected.alternative == 0) {
  947. // Remove a tile.
  948. undo_redo->add_do_method(tile_set_atlas_source, "remove_tile", selected.tile);
  949. undo_redo->add_undo_method(tile_set_atlas_source, "create_tile", selected.tile);
  950. removed.insert(selected.tile);
  951. if (per_tile.has(selected.tile)) {
  952. for (List<const PropertyInfo *>::Element *E_property = per_tile[selected.tile].front(); E_property; E_property = E_property->next()) {
  953. String property = E_property->get()->name;
  954. Variant value = tile_set_atlas_source->get(property);
  955. if (value.get_type() != Variant::NIL) {
  956. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  957. }
  958. }
  959. }
  960. }
  961. }
  962. // Remove alternatives
  963. for (Set<TileSelection>::Element *E = selection.front(); E; E = E->next()) {
  964. TileSelection selected = E->get();
  965. if (selected.alternative > 0 && !removed.has(selected.tile)) {
  966. // Remove an alternative tile.
  967. undo_redo->add_do_method(tile_set_atlas_source, "remove_alternative_tile", selected.tile, selected.alternative);
  968. undo_redo->add_undo_method(tile_set_atlas_source, "create_alternative_tile", selected.tile, selected.alternative);
  969. if (per_tile.has(selected.tile)) {
  970. for (List<const PropertyInfo *>::Element *E_property = per_tile[selected.tile].front(); E_property; E_property = E_property->next()) {
  971. Vector<String> components = E_property->get()->name.split("/", true, 2);
  972. if (components.size() >= 2 && components[1].is_valid_integer() && components[1].to_int() == selected.alternative) {
  973. String property = E_property->get()->name;
  974. Variant value = tile_set_atlas_source->get(property);
  975. if (value.get_type() != Variant::NIL) {
  976. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  977. }
  978. }
  979. }
  980. }
  981. }
  982. }
  983. undo_redo->commit_action();
  984. _update_fix_selected_and_hovered_tiles();
  985. _update_tile_id_label();
  986. } break;
  987. case TILE_CREATE: {
  988. undo_redo->create_action(TTR("Create a tile"));
  989. undo_redo->add_do_method(tile_set_atlas_source, "create_tile", menu_option_coords);
  990. Array array;
  991. array.push_back(menu_option_coords);
  992. array.push_back(0);
  993. undo_redo->add_do_method(this, "_set_selection_from_array", array);
  994. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", menu_option_coords);
  995. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  996. undo_redo->commit_action();
  997. _update_tile_id_label();
  998. } break;
  999. case TILE_CREATE_ALTERNATIVE: {
  1000. undo_redo->create_action(TTR("Create tile alternatives"));
  1001. Array array;
  1002. for (Set<TileSelection>::Element *E = selection.front(); E; E = E->next()) {
  1003. if (E->get().alternative == 0) {
  1004. int next_id = tile_set_atlas_source->get_next_alternative_tile_id(E->get().tile);
  1005. undo_redo->add_do_method(tile_set_atlas_source, "create_alternative_tile", E->get().tile, next_id);
  1006. array.push_back(E->get().tile);
  1007. array.push_back(next_id);
  1008. undo_redo->add_undo_method(tile_set_atlas_source, "remove_alternative_tile", E->get().tile, next_id);
  1009. }
  1010. }
  1011. undo_redo->add_do_method(this, "_set_selection_from_array", array);
  1012. undo_redo->add_undo_method(this, "_set_selection_from_array", _get_selection_as_array());
  1013. undo_redo->commit_action();
  1014. _update_tile_id_label();
  1015. } break;
  1016. case ADVANCED_CLEANUP_TILES_OUTSIDE_TEXTURE: {
  1017. tile_set_atlas_source->clear_tiles_outside_texture();
  1018. } break;
  1019. case ADVANCED_AUTO_CREATE_TILES: {
  1020. _auto_create_tiles();
  1021. } break;
  1022. case ADVANCED_AUTO_REMOVE_TILES: {
  1023. _auto_remove_tiles();
  1024. } break;
  1025. }
  1026. }
  1027. void TileSetAtlasSourceEditor::_unhandled_key_input(const Ref<InputEvent> &p_event) {
  1028. // Check for shortcuts.
  1029. if (ED_IS_SHORTCUT("tiles_editor/delete_tile", p_event)) {
  1030. if (tools_button_group->get_pressed_button() == tool_select_button && !selection.is_empty()) {
  1031. _menu_option(TILE_DELETE);
  1032. accept_event();
  1033. }
  1034. }
  1035. }
  1036. void TileSetAtlasSourceEditor::_set_selection_from_array(Array p_selection) {
  1037. ERR_FAIL_COND((p_selection.size() % 2) != 0);
  1038. selection.clear();
  1039. for (int i = 0; i < p_selection.size() / 2; i++) {
  1040. TileSelection selected = { p_selection[i * 2], p_selection[i * 2 + 1] };
  1041. if (tile_set_atlas_source->has_tile(selected.tile) && tile_set_atlas_source->has_alternative_tile(selected.tile, selected.alternative)) {
  1042. selection.insert(selected);
  1043. }
  1044. }
  1045. _update_tile_inspector();
  1046. _update_tile_id_label();
  1047. _update_atlas_view();
  1048. }
  1049. Array TileSetAtlasSourceEditor::_get_selection_as_array() {
  1050. Array output;
  1051. for (Set<TileSelection>::Element *E = selection.front(); E; E = E->next()) {
  1052. output.push_back(E->get().tile);
  1053. output.push_back(E->get().alternative);
  1054. }
  1055. return output;
  1056. }
  1057. void TileSetAtlasSourceEditor::_tile_atlas_control_draw() {
  1058. // Draw the selected tile.
  1059. if (tools_button_group->get_pressed_button() == tool_select_button) {
  1060. for (Set<TileSelection>::Element *E = selection.front(); E; E = E->next()) {
  1061. TileSelection selected = E->get();
  1062. if (selected.alternative == 0) {
  1063. // Draw the rect.
  1064. Rect2 region = tile_set_atlas_source->get_tile_texture_region(selected.tile);
  1065. tile_atlas_control->draw_rect(region, Color(0.2, 0.2, 1.0), false);
  1066. }
  1067. }
  1068. if (selection.size() == 1) {
  1069. // Draw the resize handles (only when it's possible to expand).
  1070. TileSelection selected = selection.front()->get();
  1071. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(selected.tile);
  1072. Size2 zoomed_size = resize_handle->get_size() / tile_atlas_view->get_zoom();
  1073. Rect2 region = tile_set_atlas_source->get_tile_texture_region(selected.tile);
  1074. Rect2 rect = region.grow_individual(zoomed_size.x, zoomed_size.y, 0, 0);
  1075. Vector2i coords[] = { Vector2i(0, 0), Vector2i(1, 0), Vector2i(1, 1), Vector2i(0, 1) };
  1076. Vector2i directions[] = { Vector2i(0, -1), Vector2i(1, 0), Vector2i(0, 1), Vector2i(-1, 0) };
  1077. bool can_grow[4];
  1078. for (int i = 0; i < 4; i++) {
  1079. can_grow[i] = tile_set_atlas_source->can_move_tile_in_atlas(selected.tile, selected.tile + directions[i]);
  1080. can_grow[i] |= (i % 2 == 0) ? size_in_atlas.y > 1 : size_in_atlas.x > 1;
  1081. }
  1082. for (int i = 0; i < 4; i++) {
  1083. Vector2 pos = rect.position + Vector2(rect.size.x, rect.size.y) * coords[i];
  1084. if (can_grow[i] && can_grow[(i + 3) % 4]) {
  1085. tile_atlas_control->draw_texture_rect(resize_handle, Rect2(pos, zoomed_size), false);
  1086. } else {
  1087. tile_atlas_control->draw_texture_rect(resize_handle_disabled, Rect2(pos, zoomed_size), false);
  1088. }
  1089. Vector2 next_pos = rect.position + Vector2(rect.size.x, rect.size.y) * coords[(i + 1) % 4];
  1090. if (can_grow[i]) {
  1091. tile_atlas_control->draw_texture_rect(resize_handle, Rect2((pos + next_pos) / 2.0, zoomed_size), false);
  1092. } else {
  1093. tile_atlas_control->draw_texture_rect(resize_handle_disabled, Rect2((pos + next_pos) / 2.0, zoomed_size), false);
  1094. }
  1095. }
  1096. }
  1097. }
  1098. if (drag_type == DRAG_TYPE_REMOVE_TILES) {
  1099. // Draw the tiles to be removed.
  1100. for (Set<Vector2i>::Element *E = drag_modified_tiles.front(); E; E = E->next()) {
  1101. tile_atlas_control->draw_rect(tile_set_atlas_source->get_tile_texture_region(E->get()), Color(0.0, 0.0, 0.0), false);
  1102. }
  1103. } else if (drag_type == DRAG_TYPE_RECT_SELECT || drag_type == DRAG_TYPE_REMOVE_TILES_USING_RECT) {
  1104. // Draw tiles to be removed.
  1105. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  1106. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  1107. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  1108. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  1109. Color color = Color(0.0, 0.0, 0.0);
  1110. if (drag_type == DRAG_TYPE_RECT_SELECT) {
  1111. color = Color(0.5, 0.5, 1.0);
  1112. }
  1113. Set<Vector2i> to_paint;
  1114. for (int x = area.get_position().x; x < area.get_end().x; x++) {
  1115. for (int y = area.get_position().y; y < area.get_end().y; y++) {
  1116. Vector2i coords = tile_set_atlas_source->get_tile_at_coords(Vector2i(x, y));
  1117. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  1118. to_paint.insert(coords);
  1119. }
  1120. }
  1121. }
  1122. for (Set<Vector2i>::Element *E = to_paint.front(); E; E = E->next()) {
  1123. Vector2i coords = E->get();
  1124. tile_atlas_control->draw_rect(tile_set_atlas_source->get_tile_texture_region(coords), color, false);
  1125. }
  1126. } else if (drag_type == DRAG_TYPE_CREATE_TILES_USING_RECT) {
  1127. // Draw tiles to be created.
  1128. Vector2i margins = tile_set_atlas_source->get_margins();
  1129. Vector2i separation = tile_set_atlas_source->get_separation();
  1130. Vector2i tile_size = tile_set_atlas_source->get_texture_region_size();
  1131. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  1132. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  1133. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  1134. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  1135. for (int x = area.get_position().x; x < area.get_end().x; x++) {
  1136. for (int y = area.get_position().y; y < area.get_end().y; y++) {
  1137. Vector2i coords = Vector2i(x, y);
  1138. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  1139. Vector2i origin = margins + (coords * (tile_size + separation));
  1140. tile_atlas_control->draw_rect(Rect2i(origin, tile_size), Color(1.0, 1.0, 1.0), false);
  1141. }
  1142. }
  1143. }
  1144. }
  1145. // Draw the hovered tile.
  1146. if (drag_type == DRAG_TYPE_REMOVE_TILES_USING_RECT || drag_type == DRAG_TYPE_CREATE_TILES_USING_RECT) {
  1147. // Draw the rect.
  1148. Vector2i start_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(drag_start_mouse_pos);
  1149. Vector2i new_base_tiles_coords = tile_atlas_view->get_atlas_tile_coords_at_pos(tile_atlas_control->get_local_mouse_position());
  1150. Rect2i area = Rect2i(start_base_tiles_coords, new_base_tiles_coords - start_base_tiles_coords).abs();
  1151. area.set_end((area.get_end() + Vector2i(1, 1)).min(tile_set_atlas_source->get_atlas_grid_size()));
  1152. Vector2i margins = tile_set_atlas_source->get_margins();
  1153. Vector2i separation = tile_set_atlas_source->get_separation();
  1154. Vector2i tile_size = tile_set_atlas_source->get_texture_region_size();
  1155. Vector2i origin = margins + (area.position * (tile_size + separation));
  1156. tile_atlas_control->draw_rect(Rect2i(origin, area.size * tile_size), Color(1.0, 1.0, 1.0), false);
  1157. } else {
  1158. Vector2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  1159. 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) {
  1160. Vector2i hovered_tile = tile_set_atlas_source->get_tile_at_coords(hovered_base_tile_coords);
  1161. if (hovered_tile != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  1162. // Draw existing hovered tile.
  1163. tile_atlas_control->draw_rect(tile_set_atlas_source->get_tile_texture_region(hovered_tile), Color(1.0, 1.0, 1.0), false);
  1164. } else {
  1165. // Draw empty tile, only in add/remove tiles mode.
  1166. if (tools_button_group->get_pressed_button() == tool_add_remove_button || tools_button_group->get_pressed_button() == tool_add_remove_rect_button) {
  1167. Vector2i margins = tile_set_atlas_source->get_margins();
  1168. Vector2i separation = tile_set_atlas_source->get_separation();
  1169. Vector2i tile_size = tile_set_atlas_source->get_texture_region_size();
  1170. Vector2i origin = margins + (hovered_base_tile_coords * (tile_size + separation));
  1171. tile_atlas_control->draw_rect(Rect2i(origin, tile_size), Color(1.0, 1.0, 1.0), false);
  1172. }
  1173. }
  1174. }
  1175. }
  1176. }
  1177. void TileSetAtlasSourceEditor::_tile_atlas_control_unscaled_draw() {
  1178. // Draw the preview of the selected property.
  1179. TileDataEditor *tile_data_editor = TileSetEditor::get_singleton()->get_tile_data_editor(selected_property);
  1180. if (tile_data_editor && tile_inspector->is_visible_in_tree()) {
  1181. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  1182. Vector2i coords = tile_set_atlas_source->get_tile_id(i);
  1183. Rect2i texture_region = tile_set_atlas_source->get_tile_texture_region(coords);
  1184. Vector2i position = (texture_region.position + texture_region.get_end()) / 2 + tile_set_atlas_source->get_tile_effective_texture_offset(coords, 0);
  1185. Transform2D xform = tile_atlas_control->get_parent_control()->get_transform();
  1186. xform.translate(position);
  1187. tile_data_editor->draw_over_tile(tile_atlas_control_unscaled, xform, *tile_set, tile_set_atlas_source_id, coords, 0, selected_property);
  1188. }
  1189. }
  1190. }
  1191. void TileSetAtlasSourceEditor::_tile_alternatives_control_gui_input(const Ref<InputEvent> &p_event) {
  1192. // Update the hovered alternative tile.
  1193. hovered_alternative_tile_coords = tile_atlas_view->get_alternative_tile_at_pos(alternative_tiles_control->get_local_mouse_position());
  1194. Ref<InputEventMouseMotion> mm = p_event;
  1195. if (mm.is_valid()) {
  1196. tile_atlas_control->update();
  1197. tile_atlas_control_unscaled->update();
  1198. alternative_tiles_control->update();
  1199. alternative_tiles_control_unscaled->update();
  1200. }
  1201. Ref<InputEventMouseButton> mb = p_event;
  1202. if (mb.is_valid()) {
  1203. drag_type = DRAG_TYPE_NONE;
  1204. Vector2 mouse_local_pos = alternative_tiles_control->get_local_mouse_position();
  1205. if (mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  1206. if (mb->is_pressed()) {
  1207. // Left click pressed.
  1208. if (tools_button_group->get_pressed_button() == tool_select_button) {
  1209. Vector3 tile = tile_atlas_view->get_alternative_tile_at_pos(mouse_local_pos);
  1210. selection.clear();
  1211. TileSelection selected = { Vector2i(tile.x, tile.y), int(tile.z) };
  1212. if (selected.tile != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  1213. selection.insert(selected);
  1214. }
  1215. _update_tile_inspector();
  1216. _update_tile_id_label();
  1217. }
  1218. }
  1219. } else if (mb->get_button_index() == MOUSE_BUTTON_RIGHT) {
  1220. if (mb->is_pressed()) {
  1221. // Right click pressed
  1222. Vector3 tile = tile_atlas_view->get_alternative_tile_at_pos(mouse_local_pos);
  1223. selection.clear();
  1224. TileSelection selected = { Vector2i(tile.x, tile.y), int(tile.z) };
  1225. if (selected.tile != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  1226. selection.insert(selected);
  1227. }
  1228. _update_tile_inspector();
  1229. _update_tile_id_label();
  1230. if (selection.size() == 1) {
  1231. selected = selection.front()->get();
  1232. menu_option_coords = selected.tile;
  1233. menu_option_alternative = selected.alternative;
  1234. alternative_tile_popup_menu->popup(Rect2i(get_global_mouse_position(), Size2i()));
  1235. }
  1236. }
  1237. }
  1238. tile_atlas_control->update();
  1239. tile_atlas_control_unscaled->update();
  1240. alternative_tiles_control->update();
  1241. alternative_tiles_control_unscaled->update();
  1242. }
  1243. }
  1244. void TileSetAtlasSourceEditor::_tile_alternatives_control_mouse_exited() {
  1245. hovered_alternative_tile_coords = Vector3i(TileSetAtlasSource::INVALID_ATLAS_COORDS.x, TileSetAtlasSource::INVALID_ATLAS_COORDS.y, TileSetAtlasSource::INVALID_TILE_ALTERNATIVE);
  1246. tile_atlas_control->update();
  1247. tile_atlas_control_unscaled->update();
  1248. alternative_tiles_control->update();
  1249. alternative_tiles_control_unscaled->update();
  1250. }
  1251. void TileSetAtlasSourceEditor::_tile_alternatives_control_draw() {
  1252. // Update the hovered alternative tile.
  1253. if (tools_button_group->get_pressed_button() == tool_select_button) {
  1254. // Draw hovered tile.
  1255. Vector2i coords = Vector2(hovered_alternative_tile_coords.x, hovered_alternative_tile_coords.y);
  1256. if (coords != TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  1257. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(coords, hovered_alternative_tile_coords.z);
  1258. if (rect != Rect2i()) {
  1259. alternative_tiles_control->draw_rect(rect, Color(1.0, 1.0, 1.0), false);
  1260. }
  1261. }
  1262. // Draw selected tile.
  1263. for (Set<TileSelection>::Element *E = selection.front(); E; E = E->next()) {
  1264. TileSelection selected = E->get();
  1265. if (selected.alternative >= 1) {
  1266. Rect2i rect = tile_atlas_view->get_alternative_tile_rect(selected.tile, selected.alternative);
  1267. if (rect != Rect2i()) {
  1268. alternative_tiles_control->draw_rect(rect, Color(0.2, 0.2, 1.0), false);
  1269. }
  1270. }
  1271. }
  1272. }
  1273. }
  1274. void TileSetAtlasSourceEditor::_tile_alternatives_control_unscaled_draw() {
  1275. //TODO
  1276. }
  1277. void TileSetAtlasSourceEditor::_tile_set_atlas_source_changed() {
  1278. tile_set_atlas_source_changed_needs_update = true;
  1279. }
  1280. void TileSetAtlasSourceEditor::_atlas_source_proxy_object_changed(String p_what) {
  1281. if (p_what == "texture" && !atlas_source_proxy_object->get("texture").is_null()) {
  1282. confirm_auto_create_tiles->popup_centered();
  1283. } else if (p_what == "id") {
  1284. emit_signal("source_id_changed", atlas_source_proxy_object->get_id());
  1285. }
  1286. }
  1287. void TileSetAtlasSourceEditor::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, String p_property, Variant p_new_value) {
  1288. UndoRedo *undo_redo = Object::cast_to<UndoRedo>(p_undo_redo);
  1289. ERR_FAIL_COND(!undo_redo);
  1290. #define ADD_UNDO(obj, property) undo_redo->add_undo_property(obj, property, tile_data->get(property));
  1291. TileProxyObject *tile_data = Object::cast_to<TileProxyObject>(p_edited);
  1292. if (tile_data) {
  1293. Vector<String> components = String(p_property).split("/", true, 2);
  1294. if (components.size() == 2 && components[1] == "shapes_count") {
  1295. int layer_index = components[0].trim_prefix("physics_layer_").to_int();
  1296. int new_shapes_count = p_new_value;
  1297. int old_shapes_count = tile_data->get(vformat("physics_layer_%d/shapes_count", layer_index));
  1298. if (new_shapes_count < old_shapes_count) {
  1299. for (int i = new_shapes_count - 1; i < old_shapes_count; i++) {
  1300. ADD_UNDO(tile_data, vformat("physics_layer_%d/shape_%d/shape", layer_index, i));
  1301. ADD_UNDO(tile_data, vformat("physics_layer_%d/shape_%d/one_way", layer_index, i));
  1302. ADD_UNDO(tile_data, vformat("physics_layer_%d/shape_%d/one_way_margin", layer_index, i));
  1303. }
  1304. }
  1305. }
  1306. }
  1307. #undef ADD_UNDO
  1308. }
  1309. void TileSetAtlasSourceEditor::edit(Ref<TileSet> p_tile_set, TileSetAtlasSource *p_tile_set_atlas_source, int p_source_id) {
  1310. ERR_FAIL_COND(!p_tile_set.is_valid());
  1311. ERR_FAIL_COND(!p_tile_set_atlas_source);
  1312. ERR_FAIL_COND(p_source_id < 0);
  1313. ERR_FAIL_COND(p_tile_set->get_source(p_source_id) != p_tile_set_atlas_source);
  1314. if (p_tile_set == tile_set && p_tile_set_atlas_source == tile_set_atlas_source && p_source_id == tile_set_atlas_source_id) {
  1315. return;
  1316. }
  1317. // Remove listener for old objects.
  1318. if (tile_set_atlas_source) {
  1319. tile_set_atlas_source->disconnect("changed", callable_mp(this, &TileSetAtlasSourceEditor::_tile_set_atlas_source_changed));
  1320. }
  1321. // Clear the selection.
  1322. selection.clear();
  1323. // Change the edited object.
  1324. tile_set = p_tile_set;
  1325. tile_set_atlas_source = p_tile_set_atlas_source;
  1326. tile_set_atlas_source_id = p_source_id;
  1327. // Add the listener again.
  1328. if (tile_set_atlas_source) {
  1329. tile_set_atlas_source->connect("changed", callable_mp(this, &TileSetAtlasSourceEditor::_tile_set_atlas_source_changed));
  1330. }
  1331. // Update everything.
  1332. _update_source_inspector();
  1333. // Update the selected tile.
  1334. _update_fix_selected_and_hovered_tiles();
  1335. _update_tile_id_label();
  1336. _update_atlas_view();
  1337. _update_tile_inspector();
  1338. }
  1339. void TileSetAtlasSourceEditor::init_source() {
  1340. confirm_auto_create_tiles->popup_centered();
  1341. }
  1342. void TileSetAtlasSourceEditor::_auto_create_tiles() {
  1343. if (!tile_set_atlas_source) {
  1344. return;
  1345. }
  1346. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  1347. if (texture.is_valid()) {
  1348. Vector2i margins = tile_set_atlas_source->get_margins();
  1349. Vector2i separation = tile_set_atlas_source->get_separation();
  1350. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  1351. Size2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  1352. undo_redo->create_action(TTR("Create tiles in non-transparent texture regions"));
  1353. for (int y = 0; y < grid_size.y; y++) {
  1354. for (int x = 0; x < grid_size.x; x++) {
  1355. // Check if we have a tile at the coord
  1356. Vector2i coords = Vector2i(x, y);
  1357. if (tile_set_atlas_source->get_tile_at_coords(coords) == TileSetAtlasSource::INVALID_ATLAS_COORDS) {
  1358. // Check if the texture is empty at the given coords.
  1359. Rect2i region = Rect2i(margins + (coords * (texture_region_size + separation)), texture_region_size);
  1360. bool is_opaque = false;
  1361. for (int region_x = region.get_position().x; region_x < region.get_end().x; region_x++) {
  1362. for (int region_y = region.get_position().y; region_y < region.get_end().y; region_y++) {
  1363. if (texture->is_pixel_opaque(region_x, region_y)) {
  1364. is_opaque = true;
  1365. break;
  1366. }
  1367. }
  1368. if (is_opaque) {
  1369. break;
  1370. }
  1371. }
  1372. // If we do have opaque pixels, create a tile.
  1373. if (is_opaque) {
  1374. undo_redo->add_do_method(tile_set_atlas_source, "create_tile", coords);
  1375. undo_redo->add_undo_method(tile_set_atlas_source, "remove_tile", coords);
  1376. }
  1377. }
  1378. }
  1379. }
  1380. undo_redo->commit_action();
  1381. }
  1382. }
  1383. void TileSetAtlasSourceEditor::_auto_remove_tiles() {
  1384. if (!tile_set_atlas_source) {
  1385. return;
  1386. }
  1387. Ref<Texture2D> texture = tile_set_atlas_source->get_texture();
  1388. if (texture.is_valid()) {
  1389. Vector2i margins = tile_set_atlas_source->get_margins();
  1390. Vector2i separation = tile_set_atlas_source->get_separation();
  1391. Vector2i texture_region_size = tile_set_atlas_source->get_texture_region_size();
  1392. Vector2i grid_size = tile_set_atlas_source->get_atlas_grid_size();
  1393. undo_redo->create_action(TTR("Remove tiles in fully transparent texture regions"));
  1394. List<PropertyInfo> list;
  1395. tile_set_atlas_source->get_property_list(&list);
  1396. Map<Vector2i, List<const PropertyInfo *>> per_tile = _group_properties_per_tiles(list, tile_set_atlas_source);
  1397. for (int i = 0; i < tile_set_atlas_source->get_tiles_count(); i++) {
  1398. Vector2i coords = tile_set_atlas_source->get_tile_id(i);
  1399. Vector2i size_in_atlas = tile_set_atlas_source->get_tile_size_in_atlas(coords);
  1400. // Skip tiles outside texture.
  1401. if ((coords.x + size_in_atlas.x) > grid_size.x || (coords.y + size_in_atlas.y) > grid_size.y) {
  1402. continue;
  1403. }
  1404. // Check if the texture is empty at the given coords.
  1405. Rect2i region = Rect2i(margins + (coords * (texture_region_size + separation)), texture_region_size * size_in_atlas);
  1406. bool is_opaque = false;
  1407. for (int region_x = region.get_position().x; region_x < region.get_end().x; region_x++) {
  1408. for (int region_y = region.get_position().y; region_y < region.get_end().y; region_y++) {
  1409. if (texture->is_pixel_opaque(region_x, region_y)) {
  1410. is_opaque = true;
  1411. break;
  1412. }
  1413. }
  1414. if (is_opaque) {
  1415. break;
  1416. }
  1417. }
  1418. // If we do have opaque pixels, create a tile.
  1419. if (!is_opaque) {
  1420. undo_redo->add_do_method(tile_set_atlas_source, "remove_tile", coords);
  1421. undo_redo->add_undo_method(tile_set_atlas_source, "create_tile", coords);
  1422. if (per_tile.has(coords)) {
  1423. for (List<const PropertyInfo *>::Element *E_property = per_tile[coords].front(); E_property; E_property = E_property->next()) {
  1424. String property = E_property->get()->name;
  1425. Variant value = tile_set_atlas_source->get(property);
  1426. if (value.get_type() != Variant::NIL) {
  1427. undo_redo->add_undo_method(tile_set_atlas_source, "set", E_property->get()->name, value);
  1428. }
  1429. }
  1430. }
  1431. }
  1432. }
  1433. undo_redo->commit_action();
  1434. }
  1435. }
  1436. void TileSetAtlasSourceEditor::_notification(int p_what) {
  1437. switch (p_what) {
  1438. case NOTIFICATION_ENTER_TREE:
  1439. case NOTIFICATION_THEME_CHANGED:
  1440. tool_select_button->set_icon(get_theme_icon("ToolSelect", "EditorIcons"));
  1441. tool_add_remove_button->set_icon(get_theme_icon("EditAddRemove", "EditorIcons"));
  1442. tool_add_remove_rect_button->set_icon(get_theme_icon("RectangleAddRemove", "EditorIcons"));
  1443. tools_settings_erase_button->set_icon(get_theme_icon("Eraser", "EditorIcons"));
  1444. tool_advanced_menu_buttom->set_icon(get_theme_icon("Tools", "EditorIcons"));
  1445. resize_handle = get_theme_icon("EditorHandle", "EditorIcons");
  1446. resize_handle_disabled = get_theme_icon("EditorHandleDisabled", "EditorIcons");
  1447. break;
  1448. case NOTIFICATION_INTERNAL_PROCESS:
  1449. if (tile_set_atlas_source_changed_needs_update) {
  1450. // Update everything.
  1451. _update_source_inspector();
  1452. // Update the selected tile.
  1453. _update_fix_selected_and_hovered_tiles();
  1454. _update_tile_id_label();
  1455. _update_atlas_view();
  1456. _update_tile_inspector();
  1457. tile_set_atlas_source_changed_needs_update = false;
  1458. }
  1459. break;
  1460. default:
  1461. break;
  1462. }
  1463. }
  1464. void TileSetAtlasSourceEditor::_bind_methods() {
  1465. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &TileSetAtlasSourceEditor::_unhandled_key_input);
  1466. ClassDB::bind_method(D_METHOD("_set_selection_from_array"), &TileSetAtlasSourceEditor::_set_selection_from_array);
  1467. ADD_SIGNAL(MethodInfo("source_id_changed", PropertyInfo(Variant::INT, "source_id")));
  1468. }
  1469. TileSetAtlasSourceEditor::TileSetAtlasSourceEditor() {
  1470. set_process_unhandled_key_input(true);
  1471. set_process_internal(true);
  1472. // -- Right side --
  1473. HSplitContainer *split_container_right_side = memnew(HSplitContainer);
  1474. split_container_right_side->set_h_size_flags(SIZE_EXPAND_FILL);
  1475. add_child(split_container_right_side);
  1476. // Middle panel.
  1477. ScrollContainer *middle_panel = memnew(ScrollContainer);
  1478. middle_panel->set_enable_h_scroll(false);
  1479. middle_panel->set_custom_minimum_size(Size2i(200, 0) * EDSCALE);
  1480. split_container_right_side->add_child(middle_panel);
  1481. VBoxContainer *middle_vbox_container = memnew(VBoxContainer);
  1482. middle_vbox_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1483. middle_panel->add_child(middle_vbox_container);
  1484. // Tile inspector.
  1485. tile_inspector_label = memnew(Label);
  1486. tile_inspector_label->set_text(TTR("Tile properties:"));
  1487. tile_inspector_label->hide();
  1488. middle_vbox_container->add_child(tile_inspector_label);
  1489. tile_proxy_object = memnew(TileProxyObject(this));
  1490. tile_proxy_object->connect("changed", callable_mp(this, &TileSetAtlasSourceEditor::_update_atlas_view).unbind(1));
  1491. tile_inspector = memnew(EditorInspector);
  1492. tile_inspector->set_undo_redo(undo_redo);
  1493. tile_inspector->set_enable_v_scroll(false);
  1494. tile_inspector->edit(tile_proxy_object);
  1495. tile_inspector->set_use_folding(true);
  1496. tile_inspector->connect("property_selected", callable_mp(this, &TileSetAtlasSourceEditor::_inspector_property_selected));
  1497. middle_vbox_container->add_child(tile_inspector);
  1498. // Atlas source inspector.
  1499. atlas_source_inspector_label = memnew(Label);
  1500. atlas_source_inspector_label->set_text(TTR("Atlas properties:"));
  1501. middle_vbox_container->add_child(atlas_source_inspector_label);
  1502. atlas_source_proxy_object = memnew(TileSetAtlasSourceProxyObject());
  1503. atlas_source_proxy_object->connect("changed", callable_mp(this, &TileSetAtlasSourceEditor::_atlas_source_proxy_object_changed));
  1504. atlas_source_inspector = memnew(EditorInspector);
  1505. atlas_source_inspector->set_undo_redo(undo_redo);
  1506. atlas_source_inspector->set_enable_v_scroll(false);
  1507. atlas_source_inspector->edit(atlas_source_proxy_object);
  1508. middle_vbox_container->add_child(atlas_source_inspector);
  1509. // Right panel.
  1510. VBoxContainer *right_panel = memnew(VBoxContainer);
  1511. right_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  1512. right_panel->set_v_size_flags(SIZE_EXPAND_FILL);
  1513. split_container_right_side->add_child(right_panel);
  1514. // -- Dialogs --
  1515. confirm_auto_create_tiles = memnew(AcceptDialog);
  1516. confirm_auto_create_tiles->set_title(TTR("Create tiles automatically in non-transparent texture regions?"));
  1517. confirm_auto_create_tiles->set_text(TTR("The atlas's texture was modified.\nWould you like to automatically create tiles in the atlas?"));
  1518. confirm_auto_create_tiles->get_ok_button()->set_text(TTR("Yes"));
  1519. confirm_auto_create_tiles->add_cancel_button()->set_text(TTR("No"));
  1520. confirm_auto_create_tiles->connect("confirmed", callable_mp(this, &TileSetAtlasSourceEditor::_auto_create_tiles));
  1521. add_child(confirm_auto_create_tiles);
  1522. // -- Toolbox --
  1523. tools_button_group.instance();
  1524. toolbox = memnew(HBoxContainer);
  1525. right_panel->add_child(toolbox);
  1526. tool_select_button = memnew(Button);
  1527. tool_select_button->set_flat(true);
  1528. tool_select_button->set_toggle_mode(true);
  1529. tool_select_button->set_pressed(true);
  1530. tool_select_button->set_button_group(tools_button_group);
  1531. tool_select_button->set_tooltip(TTR("Select tiles"));
  1532. tool_select_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_fix_selected_and_hovered_tiles));
  1533. tool_select_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_tile_id_label));
  1534. tool_select_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_tile_inspector));
  1535. tool_select_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_atlas_view));
  1536. tool_select_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_toolbar));
  1537. toolbox->add_child(tool_select_button);
  1538. tool_add_remove_button = memnew(Button);
  1539. tool_add_remove_button->set_flat(true);
  1540. tool_add_remove_button->set_toggle_mode(true);
  1541. tool_add_remove_button->set_button_group(tools_button_group);
  1542. tool_add_remove_button->set_tooltip(TTR("Add/Remove tiles tool (use the shift key to create big tiles)"));
  1543. tool_add_remove_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_fix_selected_and_hovered_tiles));
  1544. tool_add_remove_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_tile_id_label));
  1545. tool_add_remove_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_tile_inspector));
  1546. tool_add_remove_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_atlas_view));
  1547. tool_add_remove_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_toolbar));
  1548. toolbox->add_child(tool_add_remove_button);
  1549. tool_add_remove_rect_button = memnew(Button);
  1550. tool_add_remove_rect_button->set_flat(true);
  1551. tool_add_remove_rect_button->set_toggle_mode(true);
  1552. tool_add_remove_rect_button->set_button_group(tools_button_group);
  1553. tool_add_remove_rect_button->set_tooltip(TTR("Add/Remove tiles rectangle tool (use the shift key to create big tiles)"));
  1554. tool_add_remove_rect_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_fix_selected_and_hovered_tiles));
  1555. tool_add_remove_rect_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_tile_id_label));
  1556. tool_add_remove_rect_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_tile_inspector));
  1557. tool_add_remove_rect_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_atlas_view));
  1558. tool_add_remove_rect_button->connect("pressed", callable_mp(this, &TileSetAtlasSourceEditor::_update_toolbar));
  1559. toolbox->add_child(tool_add_remove_rect_button);
  1560. // Tool settings.
  1561. tool_settings = memnew(HBoxContainer);
  1562. toolbox->add_child(tool_settings);
  1563. tool_settings_vsep = memnew(VSeparator);
  1564. tool_settings->add_child(tool_settings_vsep);
  1565. tools_settings_erase_button = memnew(Button);
  1566. tools_settings_erase_button->set_flat(true);
  1567. tools_settings_erase_button->set_toggle_mode(true);
  1568. tools_settings_erase_button->set_shortcut(ED_SHORTCUT("tiles_editor/eraser", "Eraser", KEY_E));
  1569. tools_settings_erase_button->set_shortcut_context(this);
  1570. tool_settings->add_child(tools_settings_erase_button);
  1571. VSeparator *tool_advanced_vsep = memnew(VSeparator);
  1572. toolbox->add_child(tool_advanced_vsep);
  1573. tool_advanced_menu_buttom = memnew(MenuButton);
  1574. tool_advanced_menu_buttom->set_flat(true);
  1575. tool_advanced_menu_buttom->get_popup()->add_item(TTR("Cleanup tiles outside texture"), ADVANCED_CLEANUP_TILES_OUTSIDE_TEXTURE);
  1576. tool_advanced_menu_buttom->get_popup()->set_item_disabled(0, true);
  1577. tool_advanced_menu_buttom->get_popup()->add_item(TTR("Create tiles in non-transparent texture regions."), ADVANCED_AUTO_CREATE_TILES);
  1578. tool_advanced_menu_buttom->get_popup()->add_item(TTR("Remove tiles in fully transparent texture regions."), ADVANCED_AUTO_REMOVE_TILES);
  1579. tool_advanced_menu_buttom->get_popup()->connect("id_pressed", callable_mp(this, &TileSetAtlasSourceEditor::_menu_option));
  1580. toolbox->add_child(tool_advanced_menu_buttom);
  1581. _update_toolbar();
  1582. // Right side of toolbar.
  1583. Control *middle_space = memnew(Control);
  1584. middle_space->set_h_size_flags(SIZE_EXPAND_FILL);
  1585. toolbox->add_child(middle_space);
  1586. tool_tile_id_label = memnew(Label);
  1587. tool_tile_id_label->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1588. toolbox->add_child(tool_tile_id_label);
  1589. _update_tile_id_label();
  1590. // Tile atlas view.
  1591. tile_atlas_view = memnew(TileAtlasView);
  1592. tile_atlas_view->set_h_size_flags(SIZE_EXPAND_FILL);
  1593. tile_atlas_view->set_v_size_flags(SIZE_EXPAND_FILL);
  1594. tile_atlas_view->connect("transform_changed", callable_mp(TilesEditor::get_singleton(), &TilesEditor::set_atlas_view_transform));
  1595. tile_atlas_view->connect("transform_changed", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_view_transform_changed).unbind(2));
  1596. right_panel->add_child(tile_atlas_view);
  1597. base_tile_popup_menu = memnew(PopupMenu);
  1598. base_tile_popup_menu->add_shortcut(ED_SHORTCUT("tiles_editor/delete", TTR("Delete"), KEY_DELETE), TILE_DELETE);
  1599. base_tile_popup_menu->add_item(TTR("Create an Alternative Tile"), TILE_CREATE_ALTERNATIVE);
  1600. base_tile_popup_menu->connect("id_pressed", callable_mp(this, &TileSetAtlasSourceEditor::_menu_option));
  1601. tile_atlas_view->add_child(base_tile_popup_menu);
  1602. empty_base_tile_popup_menu = memnew(PopupMenu);
  1603. empty_base_tile_popup_menu->add_item(TTR("Create a Tile"), TILE_CREATE);
  1604. empty_base_tile_popup_menu->connect("id_pressed", callable_mp(this, &TileSetAtlasSourceEditor::_menu_option));
  1605. tile_atlas_view->add_child(empty_base_tile_popup_menu);
  1606. tile_atlas_control = memnew(Control);
  1607. tile_atlas_control->connect("draw", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_control_draw));
  1608. tile_atlas_control->connect("mouse_exited", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_control_mouse_exited));
  1609. tile_atlas_control->connect("gui_input", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_control_gui_input));
  1610. tile_atlas_view->add_control_over_atlas_tiles(tile_atlas_control);
  1611. tile_atlas_control_unscaled = memnew(Control);
  1612. tile_atlas_control_unscaled->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  1613. tile_atlas_control_unscaled->connect("draw", callable_mp(this, &TileSetAtlasSourceEditor::_tile_atlas_control_unscaled_draw));
  1614. tile_atlas_view->add_control_over_atlas_tiles(tile_atlas_control_unscaled, false);
  1615. tile_atlas_control_unscaled->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  1616. alternative_tile_popup_menu = memnew(PopupMenu);
  1617. alternative_tile_popup_menu->add_shortcut(ED_SHORTCUT("tiles_editor/delete_tile", TTR("Delete"), KEY_DELETE), TILE_DELETE);
  1618. alternative_tile_popup_menu->connect("id_pressed", callable_mp(this, &TileSetAtlasSourceEditor::_menu_option));
  1619. tile_atlas_view->add_child(alternative_tile_popup_menu);
  1620. alternative_tiles_control = memnew(Control);
  1621. alternative_tiles_control->connect("draw", callable_mp(this, &TileSetAtlasSourceEditor::_tile_alternatives_control_draw));
  1622. alternative_tiles_control->connect("mouse_exited", callable_mp(this, &TileSetAtlasSourceEditor::_tile_alternatives_control_mouse_exited));
  1623. alternative_tiles_control->connect("gui_input", callable_mp(this, &TileSetAtlasSourceEditor::_tile_alternatives_control_gui_input));
  1624. tile_atlas_view->add_control_over_alternative_tiles(alternative_tiles_control);
  1625. alternative_tiles_control_unscaled = memnew(Control);
  1626. alternative_tiles_control_unscaled->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  1627. alternative_tiles_control_unscaled->connect("draw", callable_mp(this, &TileSetAtlasSourceEditor::_tile_alternatives_control_unscaled_draw));
  1628. tile_atlas_view->add_control_over_atlas_tiles(alternative_tiles_control_unscaled, false);
  1629. alternative_tiles_control_unscaled->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  1630. tile_atlas_view_missing_source_label = memnew(Label);
  1631. tile_atlas_view_missing_source_label->set_text(TTR("Add or select an atlas texture to the left panel."));
  1632. tile_atlas_view_missing_source_label->set_align(Label::ALIGN_CENTER);
  1633. tile_atlas_view_missing_source_label->set_valign(Label::VALIGN_CENTER);
  1634. tile_atlas_view_missing_source_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1635. tile_atlas_view_missing_source_label->set_v_size_flags(SIZE_EXPAND_FILL);
  1636. tile_atlas_view_missing_source_label->hide();
  1637. right_panel->add_child(tile_atlas_view_missing_source_label);
  1638. }
  1639. TileSetAtlasSourceEditor::~TileSetAtlasSourceEditor() {
  1640. memdelete(tile_proxy_object);
  1641. memdelete(atlas_source_proxy_object);
  1642. }