editor_delete_levelset_menu.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SuperTux
  2. // Copyright (C) 2021 mrkubax10 <mrkubax10@onet.pl>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/menu/editor_delete_levelset_menu.hpp"
  17. #include <fmt/format.h>
  18. #include "editor/editor.hpp"
  19. #include "gui/dialog.hpp"
  20. #include "physfs/util.hpp"
  21. #include "supertux/menu/editor_levelset_select_menu.hpp"
  22. #include "supertux/world.hpp"
  23. #include "util/gettext.hpp"
  24. #include "util/log.hpp"
  25. EditorDeleteLevelsetMenu::EditorDeleteLevelsetMenu(EditorLevelsetSelectMenu* editor_levelset_select_menu) :
  26. m_editor_levelset_select_menu(editor_levelset_select_menu),
  27. m_world_names()
  28. {
  29. refresh();
  30. }
  31. void
  32. EditorDeleteLevelsetMenu::refresh()
  33. {
  34. clear();
  35. add_label(_("Delete World"));
  36. add_hl();
  37. unsigned int i = 0;
  38. std::vector<std::string>& contrib_worlds = m_editor_levelset_select_menu->get_contrib_worlds();
  39. for(std::string& level_world : contrib_worlds)
  40. {
  41. std::unique_ptr<World> world = World::from_directory(level_world);
  42. if (world->hide_from_contribs())
  43. {
  44. continue;
  45. }
  46. if (!world->is_levelset() && !world->is_worldmap())
  47. {
  48. log_warning << level_world << ": Unknown World type." << std::endl;
  49. continue;
  50. }
  51. auto title = world->get_title();
  52. if (title.empty())
  53. {
  54. continue;
  55. }
  56. m_world_names.push_back(title);
  57. add_entry(i++, title);
  58. }
  59. add_hl();
  60. add_back(_("Back"));
  61. }
  62. void
  63. EditorDeleteLevelsetMenu::menu_action(MenuItem& item)
  64. {
  65. int id = item.get_id();
  66. const auto& contrib_worlds = m_editor_levelset_select_menu->get_contrib_worlds();
  67. if (id >= 0)
  68. {
  69. if (Editor::is_active() && Editor::current()->get_world() && Editor::current()->get_world()->get_basedir() == contrib_worlds[id])
  70. Dialog::show_message(_("You cannot delete the world that you are editing"));
  71. else
  72. {
  73. Dialog::show_confirmation(fmt::format(_("You are about to delete world \"{}\". Are you sure?"), m_world_names[id]), [this, id, &contrib_worlds]()
  74. {
  75. physfsutil::remove_with_content(contrib_worlds[id]);
  76. m_editor_levelset_select_menu->reload_menu();
  77. refresh();
  78. });
  79. }
  80. }
  81. }
  82. /* EOF */