editor_levelset_select_menu.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SuperTux
  2. // Copyright (C) 2015 Hume2 <teratux.mail@gmail.com>
  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_level_select_menu.hpp"
  17. #include <physfs.h>
  18. #include <sstream>
  19. #include <fmt/format.h>
  20. #include "editor/editor.hpp"
  21. #include "gui/menu_item.hpp"
  22. #include "gui/menu_manager.hpp"
  23. #include "physfs/util.hpp"
  24. #include "supertux/levelset.hpp"
  25. #include "supertux/menu/editor_levelset_select_menu.hpp"
  26. #include "supertux/menu/editor_delete_levelset_menu.hpp"
  27. #include "supertux/menu/menu_storage.hpp"
  28. #include "supertux/world.hpp"
  29. #include "util/file_system.hpp"
  30. #include "util/gettext.hpp"
  31. #include "util/log.hpp"
  32. EditorLevelsetSelectMenu::EditorLevelsetSelectMenu() :
  33. m_contrib_worlds()
  34. {
  35. initialize();
  36. }
  37. EditorLevelsetSelectMenu::~EditorLevelsetSelectMenu()
  38. {
  39. auto editor = Editor::current();
  40. if (editor == nullptr) {
  41. return;
  42. }
  43. if (!editor->is_level_loaded() && !editor->m_reload_request) {
  44. editor->m_quit_request = true;
  45. } else {
  46. editor->m_reactivate_request = true;
  47. }
  48. }
  49. void
  50. EditorLevelsetSelectMenu::initialize()
  51. {
  52. Editor::current()->m_deactivate_request = true;
  53. m_contrib_worlds.clear();
  54. // Generating contrib levels list by making use of Level Subset
  55. std::vector<std::string> level_worlds;
  56. physfsutil::enumerate_files("levels", [&level_worlds](const auto& filename) {
  57. std::string filepath = FileSystem::join("levels", filename);
  58. if (physfsutil::is_directory(filepath))
  59. {
  60. level_worlds.push_back(filepath);
  61. }
  62. });
  63. add_label(_("Choose World"));
  64. add_hl();
  65. int i = 0;
  66. for (const auto& level_world : level_worlds)
  67. {
  68. try
  69. {
  70. std::unique_ptr<World> world = World::from_directory(level_world);
  71. if (world->hide_from_contribs())
  72. {
  73. continue;
  74. }
  75. if (!world->is_levelset() && !world->is_worldmap())
  76. {
  77. log_warning << level_world << ": Unknown World type." << std::endl;
  78. continue;
  79. }
  80. auto title = world->get_title();
  81. if (title.empty())
  82. {
  83. continue;
  84. }
  85. auto levelset = std::unique_ptr<Levelset>(
  86. new Levelset(level_world, /* recursively = */ true));
  87. int level_count = levelset->get_num_levels();
  88. std::ostringstream level_title;
  89. level_title << title << " ("
  90. << fmt::format(fmt::runtime(__("{} level", "{} levels", level_count)), level_count)
  91. << ")";
  92. add_entry(i++, level_title.str());
  93. m_contrib_worlds.push_back(level_world);
  94. }
  95. catch(std::exception& e)
  96. {
  97. log_info << "Couldn't parse levelset info for '" << level_world << "': "
  98. << e.what() << std::endl;
  99. }
  100. }
  101. add_hl();
  102. add_submenu(_("Create World"), MenuStorage::EDITOR_NEW_LEVELSET_MENU);
  103. add_entry(-3, _("Delete World"));
  104. add_back(_("Back"),-2);
  105. }
  106. void EditorLevelsetSelectMenu::reload_menu()
  107. {
  108. clear();
  109. initialize();
  110. }
  111. void
  112. EditorLevelsetSelectMenu::menu_action(MenuItem& item)
  113. {
  114. if (item.get_id() >= 0)
  115. {
  116. std::unique_ptr<Menu> menu = std::unique_ptr<Menu>(new EditorLevelSelectMenu(
  117. World::from_directory(m_contrib_worlds[item.get_id()]), this));
  118. MenuManager::instance().push_menu(std::move(menu));
  119. }
  120. else if (item.get_id() == -3)
  121. {
  122. std::unique_ptr<EditorDeleteLevelsetMenu> menu = std::make_unique<EditorDeleteLevelsetMenu>(this);
  123. MenuManager::instance().push_menu(std::move(menu));
  124. }
  125. }
  126. /* EOF */