tiles_editor_plugin.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. #include "tiles_editor_plugin.h"
  31. #include "tile_set_editor.h"
  32. #include "core/os/mutex.h"
  33. #include "editor/editor_interface.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/plugins/canvas_item_editor_plugin.h"
  38. #include "scene/2d/tile_map.h"
  39. #include "scene/gui/box_container.h"
  40. #include "scene/gui/button.h"
  41. #include "scene/gui/control.h"
  42. #include "scene/gui/separator.h"
  43. #include "scene/resources/image_texture.h"
  44. #include "scene/resources/tile_set.h"
  45. TilesEditorPlugin *TilesEditorPlugin::singleton = nullptr;
  46. void TilesEditorPlugin::_preview_frame_started() {
  47. RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast<TilesEditorPlugin *>(this), &TilesEditorPlugin::_pattern_preview_done));
  48. }
  49. void TilesEditorPlugin::_pattern_preview_done() {
  50. pattern_preview_done.post();
  51. }
  52. void TilesEditorPlugin::_thread_func(void *ud) {
  53. TilesEditorPlugin *te = static_cast<TilesEditorPlugin *>(ud);
  54. set_current_thread_safe_for_nodes(true);
  55. te->_thread();
  56. }
  57. void TilesEditorPlugin::_thread() {
  58. pattern_thread_exited.clear();
  59. while (!pattern_thread_exit.is_set()) {
  60. pattern_preview_sem.wait();
  61. pattern_preview_mutex.lock();
  62. if (pattern_preview_queue.size() == 0) {
  63. pattern_preview_mutex.unlock();
  64. } else {
  65. QueueItem item = pattern_preview_queue.front()->get();
  66. pattern_preview_queue.pop_front();
  67. pattern_preview_mutex.unlock();
  68. int thumbnail_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");
  69. thumbnail_size *= EDSCALE;
  70. Vector2 thumbnail_size2 = Vector2(thumbnail_size, thumbnail_size);
  71. if (item.pattern.is_valid() && !item.pattern->is_empty()) {
  72. // Generate the pattern preview
  73. SubViewport *viewport = memnew(SubViewport);
  74. viewport->set_size(thumbnail_size2);
  75. viewport->set_disable_input(true);
  76. viewport->set_transparent_background(true);
  77. viewport->set_update_mode(SubViewport::UPDATE_ONCE);
  78. TileMap *tile_map = memnew(TileMap);
  79. tile_map->set_tileset(item.tile_set);
  80. tile_map->set_pattern(0, Vector2(), item.pattern);
  81. viewport->add_child(tile_map);
  82. TypedArray<Vector2i> used_cells = tile_map->get_used_cells(0);
  83. Rect2 encompassing_rect;
  84. encompassing_rect.set_position(tile_map->map_to_local(used_cells[0]));
  85. for (int i = 0; i < used_cells.size(); i++) {
  86. Vector2i cell = used_cells[i];
  87. Vector2 world_pos = tile_map->map_to_local(cell);
  88. encompassing_rect.expand_to(world_pos);
  89. // Texture.
  90. Ref<TileSetAtlasSource> atlas_source = item.tile_set->get_source(tile_map->get_cell_source_id(0, cell));
  91. if (atlas_source.is_valid()) {
  92. Vector2i coords = tile_map->get_cell_atlas_coords(0, cell);
  93. int alternative = tile_map->get_cell_alternative_tile(0, cell);
  94. if (atlas_source->has_tile(coords) && atlas_source->has_alternative_tile(coords, alternative)) {
  95. Vector2 center = world_pos - atlas_source->get_tile_data(coords, alternative)->get_texture_origin();
  96. encompassing_rect.expand_to(center - atlas_source->get_tile_texture_region(coords).size / 2);
  97. encompassing_rect.expand_to(center + atlas_source->get_tile_texture_region(coords).size / 2);
  98. }
  99. }
  100. }
  101. Vector2 scale = thumbnail_size2 / MAX(encompassing_rect.size.x, encompassing_rect.size.y);
  102. tile_map->set_scale(scale);
  103. tile_map->set_position(-(scale * encompassing_rect.get_center()) + thumbnail_size2 / 2);
  104. // Add the viewport at the last moment to avoid rendering too early.
  105. EditorNode::get_singleton()->call_deferred("add_child", viewport);
  106. RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast<TilesEditorPlugin *>(this), &TilesEditorPlugin::_preview_frame_started), Object::CONNECT_ONE_SHOT);
  107. pattern_preview_done.wait();
  108. Ref<Image> image = viewport->get_texture()->get_image();
  109. // Find the index for the given pattern. TODO: optimize.
  110. Variant args[] = { item.pattern, ImageTexture::create_from_image(image) };
  111. const Variant *args_ptr[] = { &args[0], &args[1] };
  112. Variant r;
  113. Callable::CallError error;
  114. item.callback.callp(args_ptr, 2, r, error);
  115. viewport->queue_free();
  116. }
  117. }
  118. }
  119. pattern_thread_exited.set();
  120. }
  121. void TilesEditorPlugin::_tile_map_changed() {
  122. tile_map_changed_needs_update = true;
  123. }
  124. void TilesEditorPlugin::_update_editors() {
  125. // If tile_map is not edited, we change the edited only if we are not editing a tile_set.
  126. if (tile_set.is_valid()) {
  127. tileset_editor->edit(tile_set);
  128. }
  129. TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  130. if (tile_map) {
  131. tilemap_editor->edit(tile_map);
  132. } else {
  133. tilemap_editor->edit(nullptr);
  134. }
  135. // Update the viewport.
  136. CanvasItemEditor::get_singleton()->update_viewport();
  137. // Make sure the tile set editor is visible if we have one assigned.
  138. tileset_editor_button->set_visible(is_visible && tile_set.is_valid());
  139. tilemap_editor_button->set_visible(is_visible && tile_map);
  140. // Update visibility of bottom panel buttons.
  141. if (tileset_editor_button->is_pressed() && !tile_set.is_valid()) {
  142. if (tile_map) {
  143. EditorNode::get_singleton()->make_bottom_panel_item_visible(tilemap_editor);
  144. } else {
  145. EditorNode::get_singleton()->hide_bottom_panel();
  146. }
  147. }
  148. }
  149. void TilesEditorPlugin::_notification(int p_what) {
  150. switch (p_what) {
  151. case NOTIFICATION_INTERNAL_PROCESS: {
  152. if (tile_map_changed_needs_update) {
  153. TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  154. if (tile_map) {
  155. tile_set = tile_map->get_tileset();
  156. }
  157. _update_editors();
  158. tile_map_changed_needs_update = false;
  159. }
  160. } break;
  161. }
  162. }
  163. void TilesEditorPlugin::make_visible(bool p_visible) {
  164. if (p_visible || is_tile_map_selected()) {
  165. // Disable and hide invalid editors.
  166. TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  167. tileset_editor_button->set_visible(tile_set.is_valid());
  168. tilemap_editor_button->set_visible(tile_map);
  169. if (tile_map && (!is_editing_tile_set || !p_visible)) {
  170. EditorNode::get_singleton()->make_bottom_panel_item_visible(tilemap_editor);
  171. } else {
  172. EditorNode::get_singleton()->make_bottom_panel_item_visible(tileset_editor);
  173. }
  174. is_visible = true;
  175. } else {
  176. tileset_editor_button->hide();
  177. tilemap_editor_button->hide();
  178. EditorNode::get_singleton()->hide_bottom_panel();
  179. is_visible = false;
  180. }
  181. }
  182. bool TilesEditorPlugin::is_tile_map_selected() {
  183. TypedArray<Node> selection = EditorInterface::get_singleton()->get_selection()->get_selected_nodes();
  184. if (selection.size() == 1 && Object::cast_to<TileMap>(selection[0])) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. void TilesEditorPlugin::queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback) {
  190. ERR_FAIL_COND(!p_tile_set.is_valid());
  191. ERR_FAIL_COND(!p_pattern.is_valid());
  192. {
  193. MutexLock lock(pattern_preview_mutex);
  194. pattern_preview_queue.push_back({ p_tile_set, p_pattern, p_callback });
  195. }
  196. pattern_preview_sem.post();
  197. }
  198. void TilesEditorPlugin::set_sources_lists_current(int p_current) {
  199. atlas_sources_lists_current = p_current;
  200. }
  201. void TilesEditorPlugin::synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button) {
  202. ItemList *item_list = Object::cast_to<ItemList>(p_current_list);
  203. MenuButton *sorting_button = Object::cast_to<MenuButton>(p_current_sort_button);
  204. ERR_FAIL_COND(!item_list);
  205. ERR_FAIL_COND(!sorting_button);
  206. if (sorting_button->is_visible_in_tree()) {
  207. for (int i = 0; i != SOURCE_SORT_MAX; i++) {
  208. sorting_button->get_popup()->set_item_checked(i, (i == (int)source_sort));
  209. }
  210. }
  211. if (item_list->is_visible_in_tree()) {
  212. // Make sure the selection is not overwritten after sorting.
  213. int atlas_sources_lists_current_mem = atlas_sources_lists_current;
  214. item_list->emit_signal(SNAME("sort_request"));
  215. atlas_sources_lists_current = atlas_sources_lists_current_mem;
  216. if (atlas_sources_lists_current < 0 || atlas_sources_lists_current >= item_list->get_item_count()) {
  217. item_list->deselect_all();
  218. } else {
  219. item_list->set_current(atlas_sources_lists_current);
  220. item_list->ensure_current_is_visible();
  221. item_list->emit_signal(SNAME("item_selected"), atlas_sources_lists_current);
  222. }
  223. }
  224. }
  225. void TilesEditorPlugin::set_atlas_view_transform(float p_zoom, Vector2 p_scroll) {
  226. atlas_view_zoom = p_zoom;
  227. atlas_view_scroll = p_scroll;
  228. }
  229. void TilesEditorPlugin::synchronize_atlas_view(Object *p_current) {
  230. TileAtlasView *tile_atlas_view = Object::cast_to<TileAtlasView>(p_current);
  231. ERR_FAIL_COND(!tile_atlas_view);
  232. if (tile_atlas_view->is_visible_in_tree()) {
  233. tile_atlas_view->set_transform(atlas_view_zoom, atlas_view_scroll);
  234. }
  235. }
  236. void TilesEditorPlugin::set_sorting_option(int p_option) {
  237. source_sort = p_option;
  238. }
  239. List<int> TilesEditorPlugin::get_sorted_sources(const Ref<TileSet> p_tile_set) const {
  240. SourceNameComparator::tile_set = p_tile_set;
  241. List<int> source_ids;
  242. for (int i = 0; i < p_tile_set->get_source_count(); i++) {
  243. source_ids.push_back(p_tile_set->get_source_id(i));
  244. }
  245. switch (source_sort) {
  246. case SOURCE_SORT_ID_REVERSE:
  247. // Already sorted.
  248. source_ids.reverse();
  249. break;
  250. case SOURCE_SORT_NAME:
  251. source_ids.sort_custom<SourceNameComparator>();
  252. break;
  253. case SOURCE_SORT_NAME_REVERSE:
  254. source_ids.sort_custom<SourceNameComparator>();
  255. source_ids.reverse();
  256. break;
  257. default: // SOURCE_SORT_ID
  258. break;
  259. }
  260. SourceNameComparator::tile_set.unref();
  261. return source_ids;
  262. }
  263. Ref<TileSet> TilesEditorPlugin::SourceNameComparator::tile_set;
  264. bool TilesEditorPlugin::SourceNameComparator::operator()(const int &p_a, const int &p_b) const {
  265. String name_a;
  266. String name_b;
  267. {
  268. TileSetSource *source = *tile_set->get_source(p_a);
  269. if (!source->get_name().is_empty()) {
  270. name_a = source->get_name();
  271. }
  272. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  273. if (atlas_source) {
  274. Ref<Texture2D> texture = atlas_source->get_texture();
  275. if (name_a.is_empty() && texture.is_valid()) {
  276. name_a = texture->get_path().get_file();
  277. }
  278. }
  279. if (name_a.is_empty()) {
  280. name_a = itos(p_a);
  281. }
  282. }
  283. {
  284. TileSetSource *source = *tile_set->get_source(p_b);
  285. if (!source->get_name().is_empty()) {
  286. name_b = source->get_name();
  287. }
  288. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  289. if (atlas_source) {
  290. Ref<Texture2D> texture = atlas_source->get_texture();
  291. if (name_b.is_empty() && texture.is_valid()) {
  292. name_b = texture->get_path().get_file();
  293. }
  294. }
  295. if (name_b.is_empty()) {
  296. name_b = itos(p_b);
  297. }
  298. }
  299. return NaturalNoCaseComparator()(name_a, name_b);
  300. }
  301. void TilesEditorPlugin::edit(Object *p_object) {
  302. // Disconnect to changes.
  303. TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  304. if (tile_map) {
  305. tile_map->disconnect("changed", callable_mp(this, &TilesEditorPlugin::_tile_map_changed));
  306. }
  307. // Update edited objects.
  308. tile_set = Ref<TileSet>();
  309. is_editing_tile_set = false;
  310. if (p_object) {
  311. if (p_object->is_class("TileMap")) {
  312. tile_map_id = p_object->get_instance_id();
  313. tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  314. tile_set = tile_map->get_tileset();
  315. EditorNode::get_singleton()->make_bottom_panel_item_visible(tilemap_editor);
  316. } else if (p_object->is_class("TileSet")) {
  317. tile_set = Ref<TileSet>(p_object);
  318. if (tile_map) {
  319. if (tile_map->get_tileset() != tile_set || !tile_map->is_inside_tree() || !is_tile_map_selected()) {
  320. tile_map = nullptr;
  321. tile_map_id = ObjectID();
  322. }
  323. }
  324. is_editing_tile_set = true;
  325. }
  326. }
  327. // Update the editors.
  328. _update_editors();
  329. // If the tileset is being edited, the visibility function must be called
  330. // here after _update_editors has been called.
  331. if (is_editing_tile_set) {
  332. EditorNode::get_singleton()->make_bottom_panel_item_visible(tileset_editor);
  333. }
  334. // Add change listener.
  335. if (tile_map) {
  336. tile_map->connect("changed", callable_mp(this, &TilesEditorPlugin::_tile_map_changed));
  337. }
  338. }
  339. bool TilesEditorPlugin::handles(Object *p_object) const {
  340. return p_object->is_class("TileMap") || p_object->is_class("TileSet");
  341. }
  342. void TilesEditorPlugin::draw_selection_rect(CanvasItem *p_ci, const Rect2 &p_rect, const Color &p_color) {
  343. real_t scale = p_ci->get_global_transform().get_scale().x * 0.5;
  344. p_ci->draw_set_transform(p_rect.position, 0, Vector2(1, 1) / scale);
  345. RS::get_singleton()->canvas_item_add_nine_patch(
  346. p_ci->get_canvas_item(), Rect2(Vector2(), p_rect.size * scale), Rect2(), EditorNode::get_singleton()->get_gui_base()->get_theme_icon(SNAME("TileSelection"), SNAME("EditorIcons"))->get_rid(),
  347. Vector2(2, 2), Vector2(2, 2), RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, false, p_color);
  348. p_ci->draw_set_transform_matrix(Transform2D());
  349. }
  350. TilesEditorPlugin::TilesEditorPlugin() {
  351. set_process_internal(true);
  352. // Update the singleton.
  353. singleton = this;
  354. // Tileset editor.
  355. tileset_editor = memnew(TileSetEditor);
  356. tileset_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  357. tileset_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  358. tileset_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  359. tileset_editor->hide();
  360. // Tilemap editor.
  361. tilemap_editor = memnew(TileMapEditor);
  362. tilemap_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  363. tilemap_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  364. tilemap_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  365. tilemap_editor->hide();
  366. // Pattern preview generation thread.
  367. pattern_preview_thread.start(_thread_func, this);
  368. // Bottom buttons.
  369. tileset_editor_button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileSet"), tileset_editor);
  370. tileset_editor_button->hide();
  371. tilemap_editor_button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileMap"), tilemap_editor);
  372. tilemap_editor_button->hide();
  373. // Initialization.
  374. _update_editors();
  375. }
  376. TilesEditorPlugin::~TilesEditorPlugin() {
  377. if (pattern_preview_thread.is_started()) {
  378. pattern_thread_exit.set();
  379. pattern_preview_sem.post();
  380. while (!pattern_thread_exited.is_set()) {
  381. OS::get_singleton()->delay_usec(10000);
  382. RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server
  383. }
  384. pattern_preview_thread.wait_to_finish();
  385. }
  386. }