| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  tiles_editor_plugin.cpp                                              */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                      https://godotengine.org                          */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-01-03 21:27:34 +01:00
										 |  |  | /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "tiles_editor_plugin.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | #include "core/os/mutex.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | #include "editor/editor_node.h"
 | 
					
						
							|  |  |  | #include "editor/editor_scale.h"
 | 
					
						
							|  |  |  | #include "editor/plugins/canvas_item_editor_plugin.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "scene/2d/tile_map.h"
 | 
					
						
							|  |  |  | #include "scene/gui/box_container.h"
 | 
					
						
							|  |  |  | #include "scene/gui/button.h"
 | 
					
						
							|  |  |  | #include "scene/gui/control.h"
 | 
					
						
							|  |  |  | #include "scene/gui/separator.h"
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | #include "scene/resources/tile_set.h"
 | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "tile_set_editor.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | TilesEditorPlugin *TilesEditorPlugin::singleton = nullptr; | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 02:43:03 -04:00
										 |  |  | void TilesEditorPlugin::_preview_frame_started() { | 
					
						
							|  |  |  | 	RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast<TilesEditorPlugin *>(this), &TilesEditorPlugin::_pattern_preview_done)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 01:59:38 -04:00
										 |  |  | void TilesEditorPlugin::_pattern_preview_done() { | 
					
						
							| 
									
										
										
										
											2021-11-05 02:43:03 -04:00
										 |  |  | 	pattern_preview_done.post(); | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::_thread_func(void *ud) { | 
					
						
							|  |  |  | 	TilesEditorPlugin *te = (TilesEditorPlugin *)ud; | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | 	te->_thread(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::_thread() { | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | 	pattern_thread_exited.clear(); | 
					
						
							|  |  |  | 	while (!pattern_thread_exit.is_set()) { | 
					
						
							|  |  |  | 		pattern_preview_sem.wait(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pattern_preview_mutex.lock(); | 
					
						
							|  |  |  | 		if (pattern_preview_queue.size()) { | 
					
						
							|  |  |  | 			QueueItem item = pattern_preview_queue.front()->get(); | 
					
						
							|  |  |  | 			pattern_preview_queue.pop_front(); | 
					
						
							|  |  |  | 			pattern_preview_mutex.unlock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); | 
					
						
							|  |  |  | 			thumbnail_size *= EDSCALE; | 
					
						
							|  |  |  | 			Vector2 thumbnail_size2 = Vector2(thumbnail_size, thumbnail_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (item.pattern.is_valid() && !item.pattern->is_empty()) { | 
					
						
							|  |  |  | 				// Generate the pattern preview
 | 
					
						
							|  |  |  | 				SubViewport *viewport = memnew(SubViewport); | 
					
						
							|  |  |  | 				viewport->set_size(thumbnail_size2); | 
					
						
							|  |  |  | 				viewport->set_disable_input(true); | 
					
						
							|  |  |  | 				viewport->set_transparent_background(true); | 
					
						
							|  |  |  | 				viewport->set_update_mode(SubViewport::UPDATE_ONCE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				TileMap *tile_map = memnew(TileMap); | 
					
						
							|  |  |  | 				tile_map->set_tileset(item.tile_set); | 
					
						
							|  |  |  | 				tile_map->set_pattern(0, Vector2(), item.pattern); | 
					
						
							|  |  |  | 				viewport->add_child(tile_map); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				TypedArray<Vector2i> used_cells = tile_map->get_used_cells(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Rect2 encompassing_rect = Rect2(); | 
					
						
							|  |  |  | 				encompassing_rect.set_position(tile_map->map_to_world(used_cells[0])); | 
					
						
							|  |  |  | 				for (int i = 0; i < used_cells.size(); i++) { | 
					
						
							|  |  |  | 					Vector2i cell = used_cells[i]; | 
					
						
							|  |  |  | 					Vector2 world_pos = tile_map->map_to_world(cell); | 
					
						
							|  |  |  | 					encompassing_rect.expand_to(world_pos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// Texture.
 | 
					
						
							|  |  |  | 					Ref<TileSetAtlasSource> atlas_source = tile_set->get_source(tile_map->get_cell_source_id(0, cell)); | 
					
						
							|  |  |  | 					if (atlas_source.is_valid()) { | 
					
						
							|  |  |  | 						Vector2i coords = tile_map->get_cell_atlas_coords(0, cell); | 
					
						
							|  |  |  | 						int alternative = tile_map->get_cell_alternative_tile(0, cell); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						Vector2 center = world_pos - atlas_source->get_tile_effective_texture_offset(coords, alternative); | 
					
						
							|  |  |  | 						encompassing_rect.expand_to(center - atlas_source->get_tile_texture_region(coords).size / 2); | 
					
						
							|  |  |  | 						encompassing_rect.expand_to(center + atlas_source->get_tile_texture_region(coords).size / 2); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Vector2 scale = thumbnail_size2 / MAX(encompassing_rect.size.x, encompassing_rect.size.y); | 
					
						
							|  |  |  | 				tile_map->set_scale(scale); | 
					
						
							|  |  |  | 				tile_map->set_position(-(scale * encompassing_rect.get_center()) + thumbnail_size2 / 2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Fix various typos
Found via `codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,expct,fave,findn,gird,hist,inh,inout,leapyear,lod,nd,numer,ois,ony,paket,ro,seeked,sinc,switchs,te,uint,varn,vew`
											
										 
											2022-01-02 13:47:52 -05:00
										 |  |  | 				// Add the viewport at the last moment to avoid rendering too early.
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | 				EditorNode::get_singleton()->add_child(viewport); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 02:43:03 -04:00
										 |  |  | 				RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast<TilesEditorPlugin *>(this), &TilesEditorPlugin::_preview_frame_started), Vector<Variant>(), Object::CONNECT_ONESHOT); | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 02:43:03 -04:00
										 |  |  | 				pattern_preview_done.wait(); | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Ref<Image> image = viewport->get_texture()->get_image(); | 
					
						
							|  |  |  | 				Ref<ImageTexture> image_texture; | 
					
						
							|  |  |  | 				image_texture.instantiate(); | 
					
						
							|  |  |  | 				image_texture->create_from_image(image); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// Find the index for the given pattern. TODO: optimize.
 | 
					
						
							|  |  |  | 				Variant args[] = { item.pattern, image_texture }; | 
					
						
							|  |  |  | 				const Variant *args_ptr[] = { &args[0], &args[1] }; | 
					
						
							|  |  |  | 				Variant r; | 
					
						
							|  |  |  | 				Callable::CallError error; | 
					
						
							|  |  |  | 				item.callback.call(args_ptr, 2, r, error); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				viewport->queue_delete(); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				pattern_preview_mutex.unlock(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	pattern_thread_exited.set(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::_tile_map_changed() { | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	tile_map_changed_needs_update = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::_update_editors() { | 
					
						
							|  |  |  | 	// If tile_map is not edited, we change the edited only if we are not editing a tile_set.
 | 
					
						
							|  |  |  | 	tileset_editor->edit(tile_set); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id)); | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 	if (tile_map) { | 
					
						
							|  |  |  | 		tilemap_editor->edit(tile_map); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		tilemap_editor->edit(nullptr); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Update the viewport.
 | 
					
						
							|  |  |  | 	CanvasItemEditor::get_singleton()->update_viewport(); | 
					
						
							| 
									
										
										
										
											2022-01-02 13:29:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Update visibility of bottom panel buttons.
 | 
					
						
							|  |  |  | 	if (tileset_editor_button->is_pressed() && !tile_set.is_valid()) { | 
					
						
							|  |  |  | 		if (tile_map) { | 
					
						
							|  |  |  | 			editor_node->make_bottom_panel_item_visible(tilemap_editor); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			editor_node->hide_bottom_panel(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::_notification(int p_what) { | 
					
						
							|  |  |  | 	switch (p_what) { | 
					
						
							|  |  |  | 		case NOTIFICATION_INTERNAL_PROCESS: { | 
					
						
							|  |  |  | 			if (tile_map_changed_needs_update) { | 
					
						
							|  |  |  | 				TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id)); | 
					
						
							|  |  |  | 				if (tile_map) { | 
					
						
							|  |  |  | 					tile_set = tile_map->get_tileset(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				_update_editors(); | 
					
						
							|  |  |  | 				tile_map_changed_needs_update = false; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TilesEditorPlugin::make_visible(bool p_visible) { | 
					
						
							|  |  |  | 	if (p_visible) { | 
					
						
							|  |  |  | 		// Disable and hide invalid editors.
 | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 		TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id)); | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 		tileset_editor_button->set_visible(tile_set.is_valid()); | 
					
						
							|  |  |  | 		tilemap_editor_button->set_visible(tile_map); | 
					
						
							|  |  |  | 		if (tile_map) { | 
					
						
							|  |  |  | 			editor_node->make_bottom_panel_item_visible(tilemap_editor); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 			editor_node->make_bottom_panel_item_visible(tileset_editor); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 		tileset_editor_button->hide(); | 
					
						
							|  |  |  | 		tilemap_editor_button->hide(); | 
					
						
							|  |  |  | 		editor_node->hide_bottom_panel(); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback) { | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!p_tile_set.is_valid()); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!p_pattern.is_valid()); | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		MutexLock lock(pattern_preview_mutex); | 
					
						
							|  |  |  | 		pattern_preview_queue.push_back({ p_tile_set, p_pattern, p_callback }); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	pattern_preview_sem.post(); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::set_sources_lists_current(int p_current) { | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	atlas_sources_lists_current = p_current; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 02:27:37 +01:00
										 |  |  | void TilesEditorPlugin::synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button) { | 
					
						
							|  |  |  | 	ItemList *item_list = Object::cast_to<ItemList>(p_current_list); | 
					
						
							|  |  |  | 	MenuButton *sorting_button = Object::cast_to<MenuButton>(p_current_sort_button); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	ERR_FAIL_COND(!item_list); | 
					
						
							| 
									
										
										
										
											2021-11-10 02:27:37 +01:00
										 |  |  | 	ERR_FAIL_COND(!sorting_button); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (sorting_button->is_visible_in_tree()) { | 
					
						
							|  |  |  | 		for (int i = 0; i != SOURCE_SORT_MAX; i++) { | 
					
						
							|  |  |  | 			sorting_button->get_popup()->set_item_checked(i, (i == (int)source_sort)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (item_list->is_visible_in_tree()) { | 
					
						
							|  |  |  | 		if (atlas_sources_lists_current < 0 || atlas_sources_lists_current >= item_list->get_item_count()) { | 
					
						
							|  |  |  | 			item_list->deselect_all(); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2021-11-10 02:27:37 +01:00
										 |  |  | 			// Make sure the selection is not overwritten after sorting.
 | 
					
						
							|  |  |  | 			int atlas_sources_lists_current_mem = atlas_sources_lists_current; | 
					
						
							|  |  |  | 			item_list->emit_signal(SNAME("sort_request")); | 
					
						
							|  |  |  | 			atlas_sources_lists_current = atlas_sources_lists_current_mem; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 			item_list->set_current(atlas_sources_lists_current); | 
					
						
							| 
									
										
										
										
											2021-11-10 02:27:37 +01:00
										 |  |  | 			item_list->ensure_current_is_visible(); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			item_list->emit_signal(SNAME("item_selected"), atlas_sources_lists_current); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::set_atlas_view_transform(float p_zoom, Vector2 p_scroll) { | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	atlas_view_zoom = p_zoom; | 
					
						
							|  |  |  | 	atlas_view_scroll = p_scroll; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::synchronize_atlas_view(Object *p_current) { | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	TileAtlasView *tile_atlas_view = Object::cast_to<TileAtlasView>(p_current); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!tile_atlas_view); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (tile_atlas_view->is_visible_in_tree()) { | 
					
						
							| 
									
										
										
										
											2021-09-23 20:43:43 +05:45
										 |  |  | 		tile_atlas_view->set_transform(atlas_view_zoom, atlas_view_scroll); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 02:27:37 +01:00
										 |  |  | void TilesEditorPlugin::set_sorting_option(int p_option) { | 
					
						
							|  |  |  | 	source_sort = p_option; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | List<int> TilesEditorPlugin::get_sorted_sources(const Ref<TileSet> tile_set) const { | 
					
						
							|  |  |  | 	SourceNameComparator::tile_set = tile_set; | 
					
						
							|  |  |  | 	List<int> source_ids; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < tile_set->get_source_count(); i++) { | 
					
						
							|  |  |  | 		source_ids.push_back(tile_set->get_source_id(i)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (source_sort) { | 
					
						
							|  |  |  | 		case SOURCE_SORT_ID_REVERSE: | 
					
						
							|  |  |  | 			// Already sorted.
 | 
					
						
							|  |  |  | 			source_ids.reverse(); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case SOURCE_SORT_NAME: | 
					
						
							|  |  |  | 			source_ids.sort_custom<SourceNameComparator>(); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case SOURCE_SORT_NAME_REVERSE: | 
					
						
							|  |  |  | 			source_ids.sort_custom<SourceNameComparator>(); | 
					
						
							|  |  |  | 			source_ids.reverse(); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		default: // SOURCE_SORT_ID
 | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	SourceNameComparator::tile_set.unref(); | 
					
						
							|  |  |  | 	return source_ids; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Ref<TileSet> TilesEditorPlugin::SourceNameComparator::tile_set; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool TilesEditorPlugin::SourceNameComparator::operator()(const int &p_a, const int &p_b) const { | 
					
						
							|  |  |  | 	String name_a; | 
					
						
							|  |  |  | 	String name_b; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		TileSetSource *source = *tile_set->get_source(p_a); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!source->get_name().is_empty()) { | 
					
						
							|  |  |  | 			name_a = source->get_name(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); | 
					
						
							|  |  |  | 		if (atlas_source) { | 
					
						
							|  |  |  | 			Ref<Texture2D> texture = atlas_source->get_texture(); | 
					
						
							|  |  |  | 			if (name_a.is_empty() && texture.is_valid()) { | 
					
						
							|  |  |  | 				name_a = texture->get_path().get_file(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (name_a.is_empty()) { | 
					
						
							|  |  |  | 			name_a = itos(p_a); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		TileSetSource *source = *tile_set->get_source(p_b); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!source->get_name().is_empty()) { | 
					
						
							|  |  |  | 			name_b = source->get_name(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source); | 
					
						
							|  |  |  | 		if (atlas_source) { | 
					
						
							|  |  |  | 			Ref<Texture2D> texture = atlas_source->get_texture(); | 
					
						
							|  |  |  | 			if (name_b.is_empty() && texture.is_valid()) { | 
					
						
							|  |  |  | 				name_b = texture->get_path().get_file(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (name_b.is_empty()) { | 
					
						
							|  |  |  | 			name_b = itos(p_b); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return NaturalNoCaseComparator()(name_a, name_b); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | void TilesEditorPlugin::edit(Object *p_object) { | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	// Disconnect to changes.
 | 
					
						
							|  |  |  | 	TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id)); | 
					
						
							|  |  |  | 	if (tile_map) { | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 		tile_map->disconnect("changed", callable_mp(this, &TilesEditorPlugin::_tile_map_changed)); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Update edited objects.
 | 
					
						
							|  |  |  | 	tile_set = Ref<TileSet>(); | 
					
						
							|  |  |  | 	if (p_object) { | 
					
						
							|  |  |  | 		if (p_object->is_class("TileMap")) { | 
					
						
							|  |  |  | 			tile_map_id = p_object->get_instance_id(); | 
					
						
							|  |  |  | 			tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id)); | 
					
						
							|  |  |  | 			tile_set = tile_map->get_tileset(); | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 			editor_node->make_bottom_panel_item_visible(tilemap_editor); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 		} else if (p_object->is_class("TileSet")) { | 
					
						
							|  |  |  | 			tile_set = Ref<TileSet>(p_object); | 
					
						
							|  |  |  | 			if (tile_map) { | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 				if (tile_map->get_tileset() != tile_set || !tile_map->is_inside_tree()) { | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 					tile_map = nullptr; | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 					tile_map_id = ObjectID(); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 			editor_node->make_bottom_panel_item_visible(tileset_editor); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Update the editors.
 | 
					
						
							|  |  |  | 	_update_editors(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Add change listener.
 | 
					
						
							|  |  |  | 	if (tile_map) { | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 		tile_map->connect("changed", callable_mp(this, &TilesEditorPlugin::_tile_map_changed)); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | bool TilesEditorPlugin::handles(Object *p_object) const { | 
					
						
							|  |  |  | 	return p_object->is_class("TileMap") || p_object->is_class("TileSet"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TilesEditorPlugin::TilesEditorPlugin(EditorNode *p_node) { | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	set_process_internal(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Update the singleton.
 | 
					
						
							|  |  |  | 	singleton = this; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 	editor_node = p_node; | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Tileset editor.
 | 
					
						
							|  |  |  | 	tileset_editor = memnew(TileSetEditor); | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 	tileset_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL); | 
					
						
							|  |  |  | 	tileset_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL); | 
					
						
							|  |  |  | 	tileset_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE); | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	tileset_editor->hide(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 	// Tilemap editor.
 | 
					
						
							|  |  |  | 	tilemap_editor = memnew(TileMapEditor); | 
					
						
							|  |  |  | 	tilemap_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL); | 
					
						
							|  |  |  | 	tilemap_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL); | 
					
						
							|  |  |  | 	tilemap_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE); | 
					
						
							|  |  |  | 	tilemap_editor->hide(); | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Pattern preview generation thread.
 | 
					
						
							|  |  |  | 	pattern_preview_thread.start(_thread_func, this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | 	// Bottom buttons.
 | 
					
						
							|  |  |  | 	tileset_editor_button = p_node->add_bottom_panel_item(TTR("TileSet"), tileset_editor); | 
					
						
							|  |  |  | 	tileset_editor_button->hide(); | 
					
						
							|  |  |  | 	tilemap_editor_button = p_node->add_bottom_panel_item(TTR("TileMap"), tilemap_editor); | 
					
						
							|  |  |  | 	tilemap_editor_button->hide(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | 	// Initialization.
 | 
					
						
							|  |  |  | 	_update_editors(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:07:55 +02:00
										 |  |  | TilesEditorPlugin::~TilesEditorPlugin() { | 
					
						
							| 
									
										
										
										
											2021-09-29 17:48:27 +02:00
										 |  |  | 	if (pattern_preview_thread.is_started()) { | 
					
						
							|  |  |  | 		pattern_thread_exit.set(); | 
					
						
							|  |  |  | 		pattern_preview_sem.post(); | 
					
						
							|  |  |  | 		while (!pattern_thread_exited.is_set()) { | 
					
						
							|  |  |  | 			OS::get_singleton()->delay_usec(10000); | 
					
						
							|  |  |  | 			RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		pattern_preview_thread.wait_to_finish(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-07 15:41:39 +02:00
										 |  |  | } |