Debian Codegraph @

editor/scene/2d/tiles/tile_set_atlas_source_editor.cpp

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