1/**************************************************************************/
2/* tiles_editor_plugin.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 "tiles_editor_plugin.h"
32
33#include "tile_set_editor.h"
34
35#include "core/os/mutex.h"
36
37#include "editor/editor_interface.h"
38#include "editor/editor_node.h"
39#include "editor/editor_scale.h"
40#include "editor/editor_settings.h"
41#include "editor/editor_string_names.h"
42#include "editor/plugins/canvas_item_editor_plugin.h"
43
44#include "scene/2d/tile_map.h"
45#include "scene/gui/box_container.h"
46#include "scene/gui/button.h"
47#include "scene/gui/control.h"
48#include "scene/gui/separator.h"
49#include "scene/resources/image_texture.h"
50#include "scene/resources/tile_set.h"
51
52TilesEditorUtils *TilesEditorUtils::singleton = nullptr;
53TileMapEditorPlugin *tile_map_plugin_singleton = nullptr;
54TileSetEditorPlugin *tile_set_plugin_singleton = nullptr;
55
56void TilesEditorUtils::_preview_frame_started() {
57 RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast<TilesEditorUtils *>(this), &TilesEditorUtils::_pattern_preview_done));
58}
59
60void TilesEditorUtils::_pattern_preview_done() {
61 pattern_preview_done.post();
62}
63
64void TilesEditorUtils::_thread_func(void *ud) {
65 TilesEditorUtils *te = static_cast<TilesEditorUtils *>(ud);
66 set_current_thread_safe_for_nodes(true);
67 te->_thread();
68}
69
70void TilesEditorUtils::_thread() {
71 pattern_thread_exited.clear();
72 while (!pattern_thread_exit.is_set()) {
73 pattern_preview_sem.wait();
74
75 pattern_preview_mutex.lock();
76 if (pattern_preview_queue.size() == 0) {
77 pattern_preview_mutex.unlock();
78 } else {
79 QueueItem item = pattern_preview_queue.front()->get();
80 pattern_preview_queue.pop_front();
81 pattern_preview_mutex.unlock();
82
83 int thumbnail_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");
84 thumbnail_size *= EDSCALE;
85 Vector2 thumbnail_size2 = Vector2(thumbnail_size, thumbnail_size);
86
87 if (item.pattern.is_valid() && !item.pattern->is_empty()) {
88 // Generate the pattern preview
89 SubViewport *viewport = memnew(SubViewport);
90 viewport->set_size(thumbnail_size2);
91 viewport->set_disable_input(true);
92 viewport->set_transparent_background(true);
93 viewport->set_update_mode(SubViewport::UPDATE_ONCE);
94
95 TileMap *tile_map = memnew(TileMap);
96 tile_map->set_tileset(item.tile_set);
97 tile_map->set_pattern(0, Vector2(), item.pattern);
98 viewport->add_child(tile_map);
99
100 TypedArray<Vector2i> used_cells = tile_map->get_used_cells(0);
101
102 Rect2 encompassing_rect;
103 encompassing_rect.set_position(tile_map->map_to_local(used_cells[0]));
104 for (int i = 0; i < used_cells.size(); i++) {
105 Vector2i cell = used_cells[i];
106 Vector2 world_pos = tile_map->map_to_local(cell);
107 encompassing_rect.expand_to(world_pos);
108
109 // Texture.
110 Ref<TileSetAtlasSource> atlas_source = item.tile_set->get_source(tile_map->get_cell_source_id(0, cell));
111 if (atlas_source.is_valid()) {
112 Vector2i coords = tile_map->get_cell_atlas_coords(0, cell);
113 int alternative = tile_map->get_cell_alternative_tile(0, cell);
114
115 if (atlas_source->has_tile(coords) && atlas_source->has_alternative_tile(coords, alternative)) {
116 Vector2 center = world_pos - atlas_source->get_tile_data(coords, alternative)->get_texture_origin();
117 encompassing_rect.expand_to(center - atlas_source->get_tile_texture_region(coords).size / 2);
118 encompassing_rect.expand_to(center + atlas_source->get_tile_texture_region(coords).size / 2);
119 }
120 }
121 }
122
123 Vector2 scale = thumbnail_size2 / MAX(encompassing_rect.size.x, encompassing_rect.size.y);
124 tile_map->set_scale(scale);
125 tile_map->set_position(-(scale * encompassing_rect.get_center()) + thumbnail_size2 / 2);
126
127 // Add the viewport at the last moment to avoid rendering too early.
128 EditorNode::get_singleton()->call_deferred("add_child", viewport);
129
130 RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast<TilesEditorUtils *>(this), &TilesEditorUtils::_preview_frame_started), Object::CONNECT_ONE_SHOT);
131
132 pattern_preview_done.wait();
133
134 Ref<Image> image = viewport->get_texture()->get_image();
135
136 // Find the index for the given pattern. TODO: optimize.
137 Variant args[] = { item.pattern, ImageTexture::create_from_image(image) };
138 const Variant *args_ptr[] = { &args[0], &args[1] };
139 Variant r;
140 Callable::CallError error;
141 item.callback.callp(args_ptr, 2, r, error);
142
143 viewport->queue_free();
144 }
145 }
146 }
147 pattern_thread_exited.set();
148}
149
150void TilesEditorUtils::queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback) {
151 ERR_FAIL_COND(!p_tile_set.is_valid());
152 ERR_FAIL_COND(!p_pattern.is_valid());
153 {
154 MutexLock lock(pattern_preview_mutex);
155 pattern_preview_queue.push_back({ p_tile_set, p_pattern, p_callback });
156 }
157 pattern_preview_sem.post();
158}
159
160void TilesEditorUtils::set_sources_lists_current(int p_current) {
161 atlas_sources_lists_current = p_current;
162}
163
164void TilesEditorUtils::synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button) {
165 ItemList *item_list = Object::cast_to<ItemList>(p_current_list);
166 MenuButton *sorting_button = Object::cast_to<MenuButton>(p_current_sort_button);
167 ERR_FAIL_NULL(item_list);
168 ERR_FAIL_NULL(sorting_button);
169
170 if (sorting_button->is_visible_in_tree()) {
171 for (int i = 0; i != SOURCE_SORT_MAX; i++) {
172 sorting_button->get_popup()->set_item_checked(i, (i == (int)source_sort));
173 }
174 }
175
176 if (item_list->is_visible_in_tree()) {
177 // Make sure the selection is not overwritten after sorting.
178 int atlas_sources_lists_current_mem = atlas_sources_lists_current;
179 item_list->emit_signal(SNAME("sort_request"));
180 atlas_sources_lists_current = atlas_sources_lists_current_mem;
181
182 if (atlas_sources_lists_current < 0 || atlas_sources_lists_current >= item_list->get_item_count()) {
183 item_list->deselect_all();
184 } else {
185 item_list->set_current(atlas_sources_lists_current);
186 item_list->ensure_current_is_visible();
187 item_list->emit_signal(SNAME("item_selected"), atlas_sources_lists_current);
188 }
189 }
190}
191
192void TilesEditorUtils::set_atlas_view_transform(float p_zoom, Vector2 p_scroll) {
193 atlas_view_zoom = p_zoom;
194 atlas_view_scroll = p_scroll;
195}
196
197void TilesEditorUtils::synchronize_atlas_view(Object *p_current) {
198 TileAtlasView *tile_atlas_view = Object::cast_to<TileAtlasView>(p_current);
199 ERR_FAIL_NULL(tile_atlas_view);
200
201 if (tile_atlas_view->is_visible_in_tree()) {
202 tile_atlas_view->set_transform(atlas_view_zoom, atlas_view_scroll);
203 }
204}
205
206void TilesEditorUtils::set_sorting_option(int p_option) {
207 source_sort = p_option;
208}
209
210List<int> TilesEditorUtils::get_sorted_sources(const Ref<TileSet> p_tile_set) const {
211 SourceNameComparator::tile_set = p_tile_set;
212 List<int> source_ids;
213
214 for (int i = 0; i < p_tile_set->get_source_count(); i++) {
215 source_ids.push_back(p_tile_set->get_source_id(i));
216 }
217
218 switch (source_sort) {
219 case SOURCE_SORT_ID_REVERSE:
220 // Already sorted.
221 source_ids.reverse();
222 break;
223 case SOURCE_SORT_NAME:
224 source_ids.sort_custom<SourceNameComparator>();
225 break;
226 case SOURCE_SORT_NAME_REVERSE:
227 source_ids.sort_custom<SourceNameComparator>();
228 source_ids.reverse();
229 break;
230 default: // SOURCE_SORT_ID
231 break;
232 }
233
234 SourceNameComparator::tile_set.unref();
235 return source_ids;
236}
237
238Ref<TileSet> TilesEditorUtils::SourceNameComparator::tile_set;
239
240bool TilesEditorUtils::SourceNameComparator::operator()(const int &p_a, const int &p_b) const {
241 String name_a;
242 String name_b;
243
244 {
245 TileSetSource *source = *tile_set->get_source(p_a);
246
247 if (!source->get_name().is_empty()) {
248 name_a = source->get_name();
249 }
250
251 TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
252 if (atlas_source) {
253 Ref<Texture2D> texture = atlas_source->get_texture();
254 if (name_a.is_empty() && texture.is_valid()) {
255 name_a = texture->get_path().get_file();
256 }
257 }
258
259 if (name_a.is_empty()) {
260 name_a = itos(p_a);
261 }
262 }
263
264 {
265 TileSetSource *source = *tile_set->get_source(p_b);
266
267 if (!source->get_name().is_empty()) {
268 name_b = source->get_name();
269 }
270
271 TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
272 if (atlas_source) {
273 Ref<Texture2D> texture = atlas_source->get_texture();
274 if (name_b.is_empty() && texture.is_valid()) {
275 name_b = texture->get_path().get_file();
276 }
277 }
278
279 if (name_b.is_empty()) {
280 name_b = itos(p_b);
281 }
282 }
283
284 return NaturalNoCaseComparator()(name_a, name_b);
285}
286
287void TilesEditorUtils::display_tile_set_editor_panel() {
288 tile_map_plugin_singleton->hide_editor();
289 tile_set_plugin_singleton->make_visible(true);
290}
291
292void TilesEditorUtils::draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color) {
293 Ref<Texture2D> selection_texture = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("TileSelection"), EditorStringName(EditorIcons));
294
295 real_t scale = p_ci->get_global_transform().get_scale().x * 0.5;
296 p_ci->draw_set_transform(p_rect.position, 0, Vector2(1, 1) / scale);
297 RS::get_singleton()->canvas_item_add_nine_patch(
298 p_ci->get_canvas_item(), Rect2(Vector2(), p_rect.size * scale), Rect2(), selection_texture->get_rid(),
299 Vector2(2, 2), Vector2(2, 2), RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, false, p_color);
300 p_ci->draw_set_transform_matrix(Transform2D());
301}
302
303TilesEditorUtils::TilesEditorUtils() {
304 singleton = this;
305 // Pattern preview generation thread.
306 pattern_preview_thread.start(_thread_func, this);
307}
308
309TilesEditorUtils::~TilesEditorUtils() {
310 if (pattern_preview_thread.is_started()) {
311 pattern_thread_exit.set();
312 pattern_preview_sem.post();
313 while (!pattern_thread_exited.is_set()) {
314 OS::get_singleton()->delay_usec(10000);
315 RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server
316 }
317 pattern_preview_thread.wait_to_finish();
318 }
319 singleton = nullptr;
320}
321
322void TileMapEditorPlugin::_tile_map_changed() {
323 if (tile_map_changed_needs_update) {
324 return;
325 }
326 tile_map_changed_needs_update = true;
327 callable_mp(this, &TileMapEditorPlugin::_update_tile_map).call_deferred();
328}
329
330void TileMapEditorPlugin::_update_tile_map() {
331 TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
332 if (tile_map) {
333 Ref<TileSet> tile_set = tile_map->get_tileset();
334 if (tile_set.is_valid() && edited_tileset != tile_set->get_instance_id()) {
335 tile_set_plugin_singleton->edit(tile_map->get_tileset().ptr());
336 tile_set_plugin_singleton->make_visible(true);
337 edited_tileset = tile_set->get_instance_id();
338 } else if (tile_set.is_null()) {
339 tile_set_plugin_singleton->edit(nullptr);
340 tile_set_plugin_singleton->make_visible(false);
341 edited_tileset = ObjectID();
342 }
343 }
344 tile_map_changed_needs_update = false;
345}
346
347void TileMapEditorPlugin::_notification(int p_notification) {
348 if (p_notification == NOTIFICATION_EXIT_TREE) {
349 get_tree()->queue_delete(TilesEditorUtils::get_singleton());
350 }
351}
352
353void TileMapEditorPlugin::edit(Object *p_object) {
354 TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
355 if (tile_map) {
356 tile_map->disconnect("changed", callable_mp(this, &TileMapEditorPlugin::_tile_map_changed));
357 }
358
359 tile_map = Object::cast_to<TileMap>(p_object);
360 if (tile_map) {
361 tile_map_id = tile_map->get_instance_id();
362 } else {
363 tile_map_id = ObjectID();
364 }
365
366 editor->edit(tile_map);
367 if (tile_map) {
368 tile_map->connect("changed", callable_mp(this, &TileMapEditorPlugin::_tile_map_changed));
369
370 if (tile_map->get_tileset().is_valid()) {
371 tile_set_plugin_singleton->edit(tile_map->get_tileset().ptr());
372 tile_set_plugin_singleton->make_visible(true);
373 edited_tileset = tile_map->get_tileset()->get_instance_id();
374 }
375 } else if (edited_tileset.is_valid()) {
376 // Hide the TileSet editor, unless another TileSet is being edited.
377 if (tile_set_plugin_singleton->get_edited_tileset() == edited_tileset) {
378 tile_set_plugin_singleton->edit(nullptr);
379 tile_set_plugin_singleton->make_visible(false);
380 }
381 edited_tileset = ObjectID();
382 }
383}
384
385bool TileMapEditorPlugin::handles(Object *p_object) const {
386 return Object::cast_to<TileMap>(p_object) != nullptr;
387}
388
389void TileMapEditorPlugin::make_visible(bool p_visible) {
390 if (p_visible) {
391 button->show();
392 EditorNode::get_singleton()->make_bottom_panel_item_visible(editor);
393 } else {
394 button->hide();
395 if (editor->is_visible_in_tree()) {
396 EditorNode::get_singleton()->hide_bottom_panel();
397 }
398 }
399}
400
401bool TileMapEditorPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
402 return editor->forward_canvas_gui_input(p_event);
403}
404
405void TileMapEditorPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) {
406 editor->forward_canvas_draw_over_viewport(p_overlay);
407}
408
409void TileMapEditorPlugin::hide_editor() {
410 if (editor->is_visible_in_tree()) {
411 EditorNode::get_singleton()->hide_bottom_panel();
412 }
413}
414
415bool TileMapEditorPlugin::is_editor_visible() const {
416 return editor->is_visible_in_tree();
417}
418
419TileMapEditorPlugin::TileMapEditorPlugin() {
420 memnew(TilesEditorUtils);
421 tile_map_plugin_singleton = this;
422
423 editor = memnew(TileMapEditor);
424 editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
425 editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
426 editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
427 editor->hide();
428
429 button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileMap"), editor);
430 button->hide();
431}
432
433TileMapEditorPlugin::~TileMapEditorPlugin() {
434 tile_map_plugin_singleton = nullptr;
435}
436
437void TileSetEditorPlugin::edit(Object *p_object) {
438 editor->edit(Ref<TileSet>(p_object));
439 if (p_object) {
440 edited_tileset = p_object->get_instance_id();
441 } else {
442 edited_tileset = ObjectID();
443 }
444}
445
446bool TileSetEditorPlugin::handles(Object *p_object) const {
447 return Object::cast_to<TileSet>(p_object) != nullptr;
448}
449
450void TileSetEditorPlugin::make_visible(bool p_visible) {
451 if (p_visible) {
452 button->show();
453 if (!tile_map_plugin_singleton->is_editor_visible()) {
454 EditorNode::get_singleton()->make_bottom_panel_item_visible(editor);
455 }
456 } else {
457 button->hide();
458 if (editor->is_visible_in_tree()) {
459 EditorNode::get_singleton()->hide_bottom_panel();
460 }
461 }
462}
463
464ObjectID TileSetEditorPlugin::get_edited_tileset() const {
465 return edited_tileset;
466}
467
468TileSetEditorPlugin::TileSetEditorPlugin() {
469 DEV_ASSERT(tile_map_plugin_singleton);
470 tile_set_plugin_singleton = this;
471
472 editor = memnew(TileSetEditor);
473 editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
474 editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
475 editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
476 editor->hide();
477
478 button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileSet"), editor);
479 button->hide();
480}
481
482TileSetEditorPlugin::~TileSetEditorPlugin() {
483 tile_set_plugin_singleton = nullptr;
484}
485