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 
17 #include "supertux/menu/editor_delete_level_menu.hpp"
18 #include <physfs.h>
19 #include "supertux/levelset.hpp"
20 #include "supertux/level_parser.hpp"
21 #include "supertux/level.hpp"
22 #include "supertux/menu/editor_level_select_menu.hpp"
23 #include "supertux/menu/editor_levelset_select_menu.hpp"
24 #include "util/file_system.hpp"
25 #include "editor/editor.hpp"
26 #include "gui/menu_item.hpp"
27 #include "gui/dialog.hpp"
EditorDeleteLevelMenu(std::unique_ptr<Levelset> & levelset,EditorLevelSelectMenu * level_select_menu,EditorLevelsetSelectMenu * levelset_select_menu)28 EditorDeleteLevelMenu::EditorDeleteLevelMenu(std::unique_ptr<Levelset>& levelset, EditorLevelSelectMenu* level_select_menu, EditorLevelsetSelectMenu* levelset_select_menu) :
29   m_level_full_paths(),
30   m_level_select_menu(level_select_menu),
31   m_levelset_select_menu(levelset_select_menu)
32 {
33   add_label(_("Delete level"));
34   add_hl();
35   for (int i = 0; i < levelset->get_num_levels(); i++)
36   {
37     std::string filename = levelset->get_level_filename(i);
38     std::string fullpath = FileSystem::join(Editor::current()->get_world()->get_basedir(),filename);
39     m_level_full_paths.push_back(fullpath);
40     add_entry(i, LevelParser::get_level_name(fullpath));
41   }
42   add_hl();
43   add_back(_("Back"));
44 }
45 void
menu_action(MenuItem & item)46 EditorDeleteLevelMenu::menu_action(MenuItem& item)
47 {
48   int id = item.get_id();
49   // Cast to avoid compilation warning
50   if (id >= 0 && id < static_cast<int>(m_level_full_paths.size()))
51   {
52     if (Editor::current()->is_level_loaded() && m_level_full_paths[id] == Editor::current()->get_level()->m_filename)
53       Dialog::show_message(_("You cannot delete level that you are editing!"));
54     else
55     {
56       PHYSFS_delete(m_level_full_paths[id].c_str());
57       delete_item(id + 2);
58       m_level_full_paths.erase(m_level_full_paths.begin() + id);
59       m_level_select_menu->reload_menu();
60       if (!Editor::current()->is_level_loaded())
61         m_levelset_select_menu->reload_menu();
62     }
63   }
64 }
65 /* EOF */
66