editor_delete_level_menu.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_level_menu.hpp"
  17. #include <physfs.h>
  18. #include <fmt/format.h>
  19. #include "editor/editor.hpp"
  20. #include "gui/dialog.hpp"
  21. #include "gui/menu_item.hpp"
  22. #include "supertux/level.hpp"
  23. #include "supertux/level_parser.hpp"
  24. #include "supertux/levelset.hpp"
  25. #include "supertux/menu/editor_level_select_menu.hpp"
  26. #include "supertux/menu/editor_levelset_select_menu.hpp"
  27. #include "util/file_system.hpp"
  28. EditorDeleteLevelMenu::EditorDeleteLevelMenu(World* world, Levelset* levelset,
  29. EditorLevelSelectMenu* level_select_menu, EditorLevelsetSelectMenu* levelset_select_menu) :
  30. m_levelset(levelset),
  31. m_level_full_paths(),
  32. m_level_names(),
  33. m_level_select_menu(level_select_menu),
  34. m_levelset_select_menu(levelset_select_menu)
  35. {
  36. for (int i = 0; i < levelset->get_num_levels(); i++)
  37. {
  38. std::string filename = levelset->get_level_filename(i);
  39. std::string fullpath = FileSystem::join(world->get_basedir(), filename);
  40. m_level_full_paths.push_back(fullpath);
  41. const std::string& level_name = LevelParser::get_level_name(fullpath);
  42. m_level_names.push_back(level_name);
  43. }
  44. refresh();
  45. }
  46. void
  47. EditorDeleteLevelMenu::refresh()
  48. {
  49. clear();
  50. add_label(_("Delete level"));
  51. add_hl();
  52. if (m_levelset->get_num_levels() == 0)
  53. {
  54. add_inactive(_("No levels available"));
  55. }
  56. for (size_t i = 0; i < m_level_names.size(); i++)
  57. {
  58. const std::string& level_name = m_level_names[i];
  59. if (!level_name.empty())
  60. add_entry(static_cast<int>(i), level_name);
  61. }
  62. add_hl();
  63. add_back(_("Back"));
  64. }
  65. void
  66. EditorDeleteLevelMenu::menu_action(MenuItem& item)
  67. {
  68. int id = item.get_id();
  69. // Cast to avoid compilation warning
  70. if (id >= 0 && id < static_cast<int>(m_level_full_paths.size()))
  71. {
  72. if (Editor::current()->is_level_loaded() && m_level_full_paths[id] == Editor::current()->get_level()->m_filename)
  73. Dialog::show_message(_("You cannot delete the level that you are editing!"));
  74. else
  75. {
  76. Dialog::show_confirmation(fmt::format(_("You are about to delete level \"{}\". Are you sure?"), m_level_names[id]), [this, id]()
  77. {
  78. PHYSFS_delete(m_level_full_paths[id].c_str());
  79. delete_item(id + 2);
  80. m_level_full_paths[id].clear();
  81. m_level_names[id].clear();
  82. refresh();
  83. m_level_select_menu->reload_menu();
  84. if (!Editor::current()->is_level_loaded())
  85. m_levelset_select_menu->reload_menu();
  86. });
  87. }
  88. }
  89. }
  90. /* EOF */