tiles_editor_plugin.cpp 20 KB

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