tiles_editor_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*************************************************************************/
  2. /* tiles_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "tiles_editor_plugin.h"
  31. #include "core/os/mutex.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/plugins/canvas_item_editor_plugin.h"
  35. #include "scene/2d/tile_map.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/button.h"
  38. #include "scene/gui/control.h"
  39. #include "scene/gui/separator.h"
  40. #include "scene/resources/tile_set.h"
  41. #include "tile_set_editor.h"
  42. TilesEditorPlugin *TilesEditorPlugin::singleton = nullptr;
  43. void TilesEditorPlugin::_preview_frame_started() {
  44. RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast<TilesEditorPlugin *>(this), &TilesEditorPlugin::_pattern_preview_done));
  45. }
  46. void TilesEditorPlugin::_pattern_preview_done() {
  47. pattern_preview_done.post();
  48. }
  49. void TilesEditorPlugin::_thread_func(void *ud) {
  50. TilesEditorPlugin *te = static_cast<TilesEditorPlugin *>(ud);
  51. te->_thread();
  52. }
  53. void TilesEditorPlugin::_thread() {
  54. pattern_thread_exited.clear();
  55. while (!pattern_thread_exit.is_set()) {
  56. pattern_preview_sem.wait();
  57. pattern_preview_mutex.lock();
  58. if (pattern_preview_queue.size()) {
  59. QueueItem item = pattern_preview_queue.front()->get();
  60. pattern_preview_queue.pop_front();
  61. pattern_preview_mutex.unlock();
  62. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  63. thumbnail_size *= EDSCALE;
  64. Vector2 thumbnail_size2 = Vector2(thumbnail_size, thumbnail_size);
  65. if (item.pattern.is_valid() && !item.pattern->is_empty()) {
  66. // Generate the pattern preview
  67. SubViewport *viewport = memnew(SubViewport);
  68. viewport->set_size(thumbnail_size2);
  69. viewport->set_disable_input(true);
  70. viewport->set_transparent_background(true);
  71. viewport->set_update_mode(SubViewport::UPDATE_ONCE);
  72. TileMap *tile_map = memnew(TileMap);
  73. tile_map->set_tileset(item.tile_set);
  74. tile_map->set_pattern(0, Vector2(), item.pattern);
  75. viewport->add_child(tile_map);
  76. TypedArray<Vector2i> used_cells = tile_map->get_used_cells(0);
  77. Rect2 encompassing_rect = Rect2();
  78. encompassing_rect.set_position(tile_map->map_to_world(used_cells[0]));
  79. for (int i = 0; i < used_cells.size(); i++) {
  80. Vector2i cell = used_cells[i];
  81. Vector2 world_pos = tile_map->map_to_world(cell);
  82. encompassing_rect.expand_to(world_pos);
  83. // Texture.
  84. Ref<TileSetAtlasSource> atlas_source = tile_set->get_source(tile_map->get_cell_source_id(0, cell));
  85. if (atlas_source.is_valid()) {
  86. Vector2i coords = tile_map->get_cell_atlas_coords(0, cell);
  87. int alternative = tile_map->get_cell_alternative_tile(0, cell);
  88. Vector2 center = world_pos - atlas_source->get_tile_effective_texture_offset(coords, alternative);
  89. encompassing_rect.expand_to(center - atlas_source->get_tile_texture_region(coords).size / 2);
  90. encompassing_rect.expand_to(center + atlas_source->get_tile_texture_region(coords).size / 2);
  91. }
  92. }
  93. Vector2 scale = thumbnail_size2 / MAX(encompassing_rect.size.x, encompassing_rect.size.y);
  94. tile_map->set_scale(scale);
  95. tile_map->set_position(-(scale * encompassing_rect.get_center()) + thumbnail_size2 / 2);
  96. // Add the viewport at the last moment to avoid rendering too early.
  97. EditorNode::get_singleton()->add_child(viewport);
  98. RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast<TilesEditorPlugin *>(this), &TilesEditorPlugin::_preview_frame_started), Object::CONNECT_ONESHOT);
  99. pattern_preview_done.wait();
  100. Ref<Image> image = viewport->get_texture()->get_image();
  101. // Find the index for the given pattern. TODO: optimize.
  102. Variant args[] = { item.pattern, ImageTexture::create_from_image(image) };
  103. const Variant *args_ptr[] = { &args[0], &args[1] };
  104. Variant r;
  105. Callable::CallError error;
  106. item.callback.callp(args_ptr, 2, r, error);
  107. viewport->queue_delete();
  108. } else {
  109. pattern_preview_mutex.unlock();
  110. }
  111. }
  112. }
  113. pattern_thread_exited.set();
  114. }
  115. void TilesEditorPlugin::_tile_map_changed() {
  116. tile_map_changed_needs_update = true;
  117. }
  118. void TilesEditorPlugin::_update_editors() {
  119. // If tile_map is not edited, we change the edited only if we are not editing a tile_set.
  120. tileset_editor->edit(tile_set);
  121. TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  122. if (tile_map) {
  123. tilemap_editor->edit(tile_map);
  124. } else {
  125. tilemap_editor->edit(nullptr);
  126. }
  127. // Update the viewport.
  128. CanvasItemEditor::get_singleton()->update_viewport();
  129. // Make sure the tile set editor is visible if we have one assigned.
  130. tileset_editor_button->set_visible(is_visible && tile_set.is_valid());
  131. // Update visibility of bottom panel buttons.
  132. if (tileset_editor_button->is_pressed() && !tile_set.is_valid()) {
  133. if (tile_map) {
  134. EditorNode::get_singleton()->make_bottom_panel_item_visible(tilemap_editor);
  135. } else {
  136. EditorNode::get_singleton()->hide_bottom_panel();
  137. }
  138. }
  139. }
  140. void TilesEditorPlugin::_notification(int p_what) {
  141. switch (p_what) {
  142. case NOTIFICATION_INTERNAL_PROCESS: {
  143. if (tile_map_changed_needs_update) {
  144. TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  145. if (tile_map) {
  146. tile_set = tile_map->get_tileset();
  147. }
  148. _update_editors();
  149. tile_map_changed_needs_update = false;
  150. }
  151. } break;
  152. }
  153. }
  154. void TilesEditorPlugin::make_visible(bool p_visible) {
  155. is_visible = p_visible;
  156. if (is_visible) {
  157. // Disable and hide invalid editors.
  158. TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  159. tileset_editor_button->set_visible(tile_set.is_valid());
  160. tilemap_editor_button->set_visible(tile_map);
  161. if (tile_map && !is_editing_tile_set) {
  162. EditorNode::get_singleton()->make_bottom_panel_item_visible(tilemap_editor);
  163. } else {
  164. EditorNode::get_singleton()->make_bottom_panel_item_visible(tileset_editor);
  165. }
  166. } else {
  167. tileset_editor_button->hide();
  168. tilemap_editor_button->hide();
  169. EditorNode::get_singleton()->hide_bottom_panel();
  170. }
  171. }
  172. void TilesEditorPlugin::queue_pattern_preview(Ref<TileSet> p_tile_set, Ref<TileMapPattern> p_pattern, Callable p_callback) {
  173. ERR_FAIL_COND(!p_tile_set.is_valid());
  174. ERR_FAIL_COND(!p_pattern.is_valid());
  175. {
  176. MutexLock lock(pattern_preview_mutex);
  177. pattern_preview_queue.push_back({ p_tile_set, p_pattern, p_callback });
  178. }
  179. pattern_preview_sem.post();
  180. }
  181. void TilesEditorPlugin::set_sources_lists_current(int p_current) {
  182. atlas_sources_lists_current = p_current;
  183. }
  184. void TilesEditorPlugin::synchronize_sources_list(Object *p_current_list, Object *p_current_sort_button) {
  185. ItemList *item_list = Object::cast_to<ItemList>(p_current_list);
  186. MenuButton *sorting_button = Object::cast_to<MenuButton>(p_current_sort_button);
  187. ERR_FAIL_COND(!item_list);
  188. ERR_FAIL_COND(!sorting_button);
  189. if (sorting_button->is_visible_in_tree()) {
  190. for (int i = 0; i != SOURCE_SORT_MAX; i++) {
  191. sorting_button->get_popup()->set_item_checked(i, (i == (int)source_sort));
  192. }
  193. }
  194. if (item_list->is_visible_in_tree()) {
  195. // Make sure the selection is not overwritten after sorting.
  196. int atlas_sources_lists_current_mem = atlas_sources_lists_current;
  197. item_list->emit_signal(SNAME("sort_request"));
  198. atlas_sources_lists_current = atlas_sources_lists_current_mem;
  199. if (atlas_sources_lists_current < 0 || atlas_sources_lists_current >= item_list->get_item_count()) {
  200. item_list->deselect_all();
  201. } else {
  202. item_list->set_current(atlas_sources_lists_current);
  203. item_list->ensure_current_is_visible();
  204. item_list->emit_signal(SNAME("item_selected"), atlas_sources_lists_current);
  205. }
  206. }
  207. }
  208. void TilesEditorPlugin::set_atlas_view_transform(float p_zoom, Vector2 p_scroll) {
  209. atlas_view_zoom = p_zoom;
  210. atlas_view_scroll = p_scroll;
  211. }
  212. void TilesEditorPlugin::synchronize_atlas_view(Object *p_current) {
  213. TileAtlasView *tile_atlas_view = Object::cast_to<TileAtlasView>(p_current);
  214. ERR_FAIL_COND(!tile_atlas_view);
  215. if (tile_atlas_view->is_visible_in_tree()) {
  216. tile_atlas_view->set_transform(atlas_view_zoom, atlas_view_scroll);
  217. }
  218. }
  219. void TilesEditorPlugin::set_sorting_option(int p_option) {
  220. source_sort = p_option;
  221. }
  222. List<int> TilesEditorPlugin::get_sorted_sources(const Ref<TileSet> tile_set) const {
  223. SourceNameComparator::tile_set = tile_set;
  224. List<int> source_ids;
  225. for (int i = 0; i < tile_set->get_source_count(); i++) {
  226. source_ids.push_back(tile_set->get_source_id(i));
  227. }
  228. switch (source_sort) {
  229. case SOURCE_SORT_ID_REVERSE:
  230. // Already sorted.
  231. source_ids.reverse();
  232. break;
  233. case SOURCE_SORT_NAME:
  234. source_ids.sort_custom<SourceNameComparator>();
  235. break;
  236. case SOURCE_SORT_NAME_REVERSE:
  237. source_ids.sort_custom<SourceNameComparator>();
  238. source_ids.reverse();
  239. break;
  240. default: // SOURCE_SORT_ID
  241. break;
  242. }
  243. SourceNameComparator::tile_set.unref();
  244. return source_ids;
  245. }
  246. Ref<TileSet> TilesEditorPlugin::SourceNameComparator::tile_set;
  247. bool TilesEditorPlugin::SourceNameComparator::operator()(const int &p_a, const int &p_b) const {
  248. String name_a;
  249. String name_b;
  250. {
  251. TileSetSource *source = *tile_set->get_source(p_a);
  252. if (!source->get_name().is_empty()) {
  253. name_a = source->get_name();
  254. }
  255. TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
  256. if (atlas_source) {
  257. Ref<Texture2D> texture = atlas_source->get_texture();
  258. if (name_a.is_empty() && texture.is_valid()) {
  259. name_a = texture->get_path().get_file();
  260. }
  261. }
  262. if (name_a.is_empty()) {
  263. name_a = itos(p_a);
  264. }
  265. }
  266. {
  267. TileSetSource *source = *tile_set->get_source(p_b);
  268. if (!source->get_name().is_empty()) {
  269. name_b = source->get_name();
  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. if (name_b.is_empty()) {
  279. name_b = itos(p_b);
  280. }
  281. }
  282. return NaturalNoCaseComparator()(name_a, name_b);
  283. }
  284. void TilesEditorPlugin::edit(Object *p_object) {
  285. // Disconnect to changes.
  286. TileMap *tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  287. if (tile_map) {
  288. tile_map->disconnect("changed", callable_mp(this, &TilesEditorPlugin::_tile_map_changed));
  289. }
  290. // Update edited objects.
  291. tile_set = Ref<TileSet>();
  292. is_editing_tile_set = false;
  293. if (p_object) {
  294. if (p_object->is_class("TileMap")) {
  295. tile_map_id = p_object->get_instance_id();
  296. tile_map = Object::cast_to<TileMap>(ObjectDB::get_instance(tile_map_id));
  297. tile_set = tile_map->get_tileset();
  298. EditorNode::get_singleton()->make_bottom_panel_item_visible(tilemap_editor);
  299. } else if (p_object->is_class("TileSet")) {
  300. tile_set = Ref<TileSet>(p_object);
  301. if (tile_map) {
  302. if (tile_map->get_tileset() != tile_set || !tile_map->is_inside_tree()) {
  303. tile_map = nullptr;
  304. tile_map_id = ObjectID();
  305. }
  306. }
  307. is_editing_tile_set = true;
  308. EditorNode::get_singleton()->make_bottom_panel_item_visible(tileset_editor);
  309. }
  310. }
  311. // Update the editors.
  312. _update_editors();
  313. // Add change listener.
  314. if (tile_map) {
  315. tile_map->connect("changed", callable_mp(this, &TilesEditorPlugin::_tile_map_changed));
  316. }
  317. }
  318. bool TilesEditorPlugin::handles(Object *p_object) const {
  319. return p_object->is_class("TileMap") || p_object->is_class("TileSet");
  320. }
  321. TilesEditorPlugin::TilesEditorPlugin() {
  322. set_process_internal(true);
  323. // Update the singleton.
  324. singleton = this;
  325. // Tileset editor.
  326. tileset_editor = memnew(TileSetEditor);
  327. tileset_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  328. tileset_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  329. tileset_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  330. tileset_editor->hide();
  331. // Tilemap editor.
  332. tilemap_editor = memnew(TileMapEditor);
  333. tilemap_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  334. tilemap_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  335. tilemap_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  336. tilemap_editor->hide();
  337. // Pattern preview generation thread.
  338. pattern_preview_thread.start(_thread_func, this);
  339. // Bottom buttons.
  340. tileset_editor_button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileSet"), tileset_editor);
  341. tileset_editor_button->hide();
  342. tilemap_editor_button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("TileMap"), tilemap_editor);
  343. tilemap_editor_button->hide();
  344. // Initialization.
  345. _update_editors();
  346. }
  347. TilesEditorPlugin::~TilesEditorPlugin() {
  348. if (pattern_preview_thread.is_started()) {
  349. pattern_thread_exit.set();
  350. pattern_preview_sem.post();
  351. while (!pattern_thread_exited.is_set()) {
  352. OS::get_singleton()->delay_usec(10000);
  353. RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server
  354. }
  355. pattern_preview_thread.wait_to_finish();
  356. }
  357. }