editor_sectors_menu.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_sectors_menu.hpp"
  17. #include "editor/editor.hpp"
  18. #include "gui/dialog.hpp"
  19. #include "gui/menu_item.hpp"
  20. #include "supertux/menu/menu_storage.hpp"
  21. #include "supertux/level.hpp"
  22. #include "supertux/sector.hpp"
  23. #include "supertux/sector_parser.hpp"
  24. #include "util/gettext.hpp"
  25. #include "util/log.hpp"
  26. EditorSectorsMenu::EditorSectorsMenu()
  27. {
  28. add_label(_("Choose Sector"));
  29. add_hl();
  30. int id = 0;
  31. for (const auto& sector : Editor::current()->get_level()->m_sectors) {
  32. add_entry(id, sector->get_name());
  33. id++;
  34. }
  35. add_hl();
  36. add_submenu(_("Sector Settings"), MenuStorage::EDITOR_SECTOR_MENU);
  37. add_entry(-2,_("Create Sector"));
  38. add_entry(-3,_("Delete Sector"));
  39. add_hl();
  40. add_entry(-4,_("Cancel"));
  41. }
  42. EditorSectorsMenu::~EditorSectorsMenu()
  43. {
  44. auto editor = Editor::current();
  45. if (editor == nullptr) {
  46. return;
  47. }
  48. editor->m_reactivate_request = true;
  49. }
  50. void
  51. EditorSectorsMenu::create_sector()
  52. {
  53. auto level = Editor::current()->get_level();
  54. auto new_sector = SectorParser::from_nothing(*level);
  55. if (!new_sector) {
  56. log_warning << "Failed to create a new sector." << std::endl;
  57. return;
  58. }
  59. // Find an unique name.
  60. std::string sector_name;
  61. int num = 2;
  62. do {
  63. sector_name = "sector" + std::to_string(num);
  64. num++;
  65. } while ( level->get_sector(sector_name) );
  66. new_sector->set_name(sector_name);
  67. level->add_sector(std::move(new_sector));
  68. Editor::current()->load_sector(sector_name);
  69. MenuManager::instance().clear_menu_stack();
  70. Editor::current()->m_reactivate_request = true;
  71. }
  72. void
  73. EditorSectorsMenu::delete_sector()
  74. {
  75. Level* level = Editor::current()->get_level();
  76. auto dialog = std::make_unique<Dialog>();
  77. // Do not delete sector when there would be no left.
  78. if (level->get_sector_count() < 2) {
  79. dialog->set_text(_("Each level must have at least one sector."));
  80. dialog->clear_buttons();
  81. dialog->add_cancel_button(_("Cancel"));
  82. } else {
  83. // Confirmation dialog.
  84. dialog->set_text(_("Do you really want to delete this sector?"));
  85. dialog->clear_buttons();
  86. dialog->add_cancel_button(_("Cancel"));
  87. dialog->add_button(_("Delete sector"), [] {
  88. MenuManager::instance().clear_menu_stack();
  89. Editor::current()->delete_current_sector();
  90. });
  91. }
  92. MenuManager::instance().set_dialog(std::move(dialog));
  93. }
  94. void
  95. EditorSectorsMenu::menu_action(MenuItem& item)
  96. {
  97. if (item.get_id() >= 0)
  98. {
  99. Level* level = Editor::current()->get_level();
  100. Sector* sector = level->get_sector(item.get_id());
  101. Editor::current()->load_sector(sector->get_name());
  102. MenuManager::instance().clear_menu_stack();
  103. }
  104. else
  105. {
  106. switch (item.get_id())
  107. {
  108. case -1:
  109. break;
  110. case -2:
  111. create_sector();
  112. break;
  113. case -3:
  114. delete_sector();
  115. break;
  116. case -4:
  117. MenuManager::instance().clear_menu_stack();
  118. break;
  119. }
  120. }
  121. }
  122. /* EOF */