diff --git a/src/editor/controller/editor_controller.cpp b/src/editor/controller/editor_controller.cpp index 7f8e61e8d3b..483e841560e 100644 --- a/src/editor/controller/editor_controller.cpp +++ b/src/editor/controller/editor_controller.cpp @@ -1036,73 +1036,70 @@ void editor_controller::show_menu(const std::vector& items_arg, int xloc if(first_id == "EDITOR-LOAD-MRU-PLACEHOLDER") { active_menu_ = editor::LOAD_MRU; - context_manager_->expand_load_mru_menu(items); + context_manager_->expand_load_mru_menu(items, 0); } if(first_id == "editor-switch-map") { active_menu_ = editor::MAP; - context_manager_->expand_open_maps_menu(items); + context_manager_->expand_open_maps_menu(items, 0); } if(first_id == "editor-palette-groups") { active_menu_ = editor::PALETTE; - toolkit_->get_palette_manager()->active_palette().expand_palette_groups_menu(items); + toolkit_->get_palette_manager()->active_palette().expand_palette_groups_menu(items, 0); } if(first_id == "editor-switch-side") { active_menu_ = editor::SIDE; - context_manager_->expand_sides_menu(items); + context_manager_->expand_sides_menu(items, 0); } if(first_id == "editor-switch-area") { active_menu_ = editor::AREA; - context_manager_->expand_areas_menu(items); + context_manager_->expand_areas_menu(items, 0); } if(!items.empty() && items.front()["id"] == "editor-switch-time") { active_menu_ = editor::TIME; - context_manager_->expand_time_menu(items); + context_manager_->expand_time_menu(items, 0); } if(first_id == "editor-assign-local-time") { active_menu_ = editor::LOCAL_TIME; - context_manager_->expand_local_time_menu(items); + context_manager_->expand_local_time_menu(items, 0); } if(first_id == "menu-unit-facings") { active_menu_ = editor::UNIT_FACING; - items.erase(items.begin()); - - for(int dir = 0; dir != map_location::NDIRECTIONS; ++dir) { - items.emplace_back(config_of("label", map_location::write_translated_direction(map_location::DIRECTION(dir)))); - } + auto pos = items.erase(items.begin()); + int dir = 0; + std::generate_n(std::inserter>(items, pos), int(map_location::NDIRECTIONS), [&dir]() -> config { + return config_of("label", map_location::write_translated_direction(map_location::DIRECTION(dir++))); + }); } if(first_id == "editor-playlist") { active_menu_ = editor::MUSIC; - items.erase(items.begin()); - - for(const sound::music_track& track : music_tracks_) { - items.emplace_back(config_of("label", track.title().empty() ? track.id() : track.title())); - } + auto pos = items.erase(items.begin()); + std::transform(music_tracks_.begin(), music_tracks_.end(), std::inserter>(items, pos), [](const sound::music_track& track) -> config { + return config_of("label", track.title().empty() ? track.id() : track.title()); + }); } if(first_id == "editor-assign-schedule") { active_menu_ = editor::SCHEDULE; - - items.erase(items.begin()); - for(const auto& tod : tods_) { - items.emplace_back(config_of("label", tod.second.first)); - } + auto pos = items.erase(items.begin()); + std::transform(tods_.begin(), tods_.end(), std::inserter>(items, pos), [](const tods_map::value_type& tod) -> config { + return config_of("label", tod.second.first); + }); } if(first_id == "editor-assign-local-schedule") { active_menu_ = editor::LOCAL_SCHEDULE; - - items.erase(items.begin()); - for(const auto& tod : tods_) { - items.emplace_back(config_of("label", tod.second.first)); - } + auto pos = items.erase(items.begin()); + std::transform(tods_.begin(), tods_.end(), std::inserter>(items, pos), [](const tods_map::value_type& tod) -> config { + return config_of("label", tod.second.first); + }); } command_executor::show_menu(items, xloc, yloc, context_menu, disp); diff --git a/src/editor/map/context_manager.cpp b/src/editor/map/context_manager.cpp index 6bd4e293761..648003d70d1 100644 --- a/src/editor/map/context_manager.cpp +++ b/src/editor/map/context_manager.cpp @@ -293,182 +293,139 @@ void context_manager::new_scenario_dialog() } } -void context_manager::expand_open_maps_menu(std::vector& items) +void context_manager::expand_open_maps_menu(std::vector& items, int i) { - for(unsigned int i = 0; i < items.size(); ++i) { - if(items[i]["id"] != "editor-switch-map") { - continue; - } - - items.erase(items.begin() + i); - std::vector contexts; - for(size_t mci = 0; mci < map_contexts_.size(); ++mci) { - std::string filename = map_contexts_[mci]->get_filename(); - bool changed = map_contexts_[mci]->modified(); - bool pure_map = map_contexts_[mci]->is_pure_map(); - if(filename.empty()) { - if(pure_map) { - filename = _("(New Map)"); - } else { - filename = _("(New Scenario)"); - } + auto pos = items.erase(items.begin() + i); + std::vector contexts; + for(size_t mci = 0; mci < map_contexts_.size(); ++mci) { + std::string filename = map_contexts_[mci]->get_filename(); + bool changed = map_contexts_[mci]->modified(); + bool pure_map = map_contexts_[mci]->is_pure_map(); + if(filename.empty()) { + if(pure_map) { + filename = _("(New Map)"); + } else { + filename = _("(New Scenario)"); } - std::string label = "[" + std::to_string(mci) + "] " - + filename + (changed ? " [*]" : ""); - if(map_contexts_[mci]->is_embedded()) { - label += " (E)"; - } - contexts.emplace_back(config_of("label", label)); } - items.insert(items.begin() + i, contexts.begin(), contexts.end()); - break; + std::string label = "[" + std::to_string(mci) + "] " + + filename + (changed ? " [*]" : ""); + if(map_contexts_[mci]->is_embedded()) { + label += " (E)"; + } + contexts.emplace_back(config_of("label", label)); } + items.insert(pos, contexts.begin(), contexts.end()); } -void context_manager::expand_load_mru_menu(std::vector& items) +void context_manager::expand_load_mru_menu(std::vector& items, int i) { std::vector mru = preferences::editor::recent_files(); - for(unsigned int i = 0; i < items.size(); ++i) { - if(items[i]["id"] != "EDITOR-LOAD-MRU-PLACEHOLDER") { - continue; - } + auto pos = items.erase(items.begin() + i); - items.erase(items.begin() + i); - - if(mru.empty()) { - items.insert(items.begin() + i, config_of("label", _("No Recent Files"))); - continue; - } - - for(std::string& path : mru) { - // TODO: add proper leading ellipsization instead, since otherwise - // it'll be impossible to tell apart files with identical names and - // different parent paths. - path = filesystem::base_name(path); - } - - std::vector temp; - std::transform(mru.begin(), mru.end(), std::back_inserter(temp), [](const std::string& str) { - return config_of("label", str); - }); - - items.insert(items.begin() + i, temp.begin(), temp.end()); - break; + if(mru.empty()) { + items.insert(pos, config_of("label", _("No Recent Files"))); + return; } + for(std::string& path : mru) { + // TODO: add proper leading ellipsization instead, since otherwise + // it'll be impossible to tell apart files with identical names and + // different parent paths. + path = filesystem::base_name(path); + } + + std::vector temp; + std::transform(mru.begin(), mru.end(), std::back_inserter(temp), [](const std::string& str) { + return config_of("label", str); + }); + + items.insert(pos, temp.begin(), temp.end()); } -void context_manager::expand_areas_menu(std::vector& items) +void context_manager::expand_areas_menu(std::vector& items, int i) { tod_manager* tod = get_map_context().get_time_manager(); if(!tod) { return; } - for(unsigned int i = 0; i < items.size(); ++i) { - if(items[i]["id"] != "editor-switch-area") { - continue; + auto pos = items.erase(items.begin() + i); + std::vector area_entries; + + std::vector area_ids = tod->get_area_ids(); + + for(size_t mci = 0; mci < area_ids.size(); ++mci) { + + const std::string& area = area_ids[mci]; + std::stringstream label; + label << "[" << mci + 1 << "] "; + label << (area.empty() ? _("(Unnamed Area)") : area); + + if(mci == static_cast(get_map_context().get_active_area()) + && tod->get_area_by_index(mci) != get_map_context().get_map().selection()) { + label << " [*]"; } - items.erase(items.begin() + i); - std::vector area_entries; - - std::vector area_ids = tod->get_area_ids(); - - for(size_t mci = 0; mci < area_ids.size(); ++mci) { - - const std::string& area = area_ids[mci]; - std::stringstream label; - label << "[" << mci + 1 << "] "; - label << (area.empty() ? _("(Unnamed Area)") : area); - - if(mci == static_cast(get_map_context().get_active_area()) - && tod->get_area_by_index(mci) != get_map_context().get_map().selection()) { - label << " [*]"; - } - - area_entries.emplace_back(config_of("label", label.str())); - } - - items.insert(items.begin() + i, area_entries.begin(), area_entries.end()); - break; + area_entries.emplace_back(config_of("label", label.str())); } + + items.insert(pos, area_entries.begin(), area_entries.end()); } -void context_manager::expand_sides_menu(std::vector& items) +void context_manager::expand_sides_menu(std::vector& items, int i) { - for(unsigned int i = 0; i < items.size(); ++i) { - if(items[i]["id"] != "editor-switch-side") { - continue; - } + auto pos = items.erase(items.begin() + i); + std::vector contexts; - items.erase(items.begin() + i); - std::vector contexts; + for(size_t mci = 0; mci < get_map_context().get_teams().size(); ++mci) { - for(size_t mci = 0; mci < get_map_context().get_teams().size(); ++mci) { - - const team& t = get_map_context().get_teams()[mci]; - const std::string& teamname = t.user_team_name(); - std::stringstream label; - label << "[" << mci+1 << "] "; - label << (teamname.empty() ? _("(New Side)") : teamname); - contexts.emplace_back(config_of("label", label.str())); - } - - items.insert(items.begin() + i, contexts.begin(), contexts.end()); - break; + const team& t = get_map_context().get_teams()[mci]; + const std::string& teamname = t.user_team_name(); + std::stringstream label; + label << "[" << mci+1 << "] "; + label << (teamname.empty() ? _("(New Side)") : teamname); + contexts.emplace_back(config_of("label", label.str())); } + + items.insert(pos, contexts.begin(), contexts.end()); } -void context_manager::expand_time_menu(std::vector& items) +void context_manager::expand_time_menu(std::vector& items, int i) { - for(unsigned int i = 0; i < items.size(); ++i) { - if(items[i]["id"] != "editor-switch-time") { - continue; - } + auto pos = items.erase(items.begin() + i); + std::vector times; - items.erase(items.begin() + i); - std::vector times; + tod_manager* tod_m = get_map_context().get_time_manager(); - tod_manager* tod_m = get_map_context().get_time_manager(); + assert(tod_m != nullptr); - assert(tod_m != nullptr); - - for(const time_of_day& time : tod_m->times()) { - times.emplace_back(config_of - ("details", time.name) // Use 'details' field here since the image will take the first column - ("image", time.image) - ); - } - - items.insert(items.begin() + i, times.begin(), times.end()); - break; + for(const time_of_day& time : tod_m->times()) { + times.emplace_back(config_of + ("details", time.name) // Use 'details' field here since the image will take the first column + ("image", time.image) + ); } + + items.insert(pos, times.begin(), times.end()); } -void context_manager::expand_local_time_menu(std::vector& items) +void context_manager::expand_local_time_menu(std::vector& items, int i) { - for(unsigned int i = 0; i < items.size(); ++i) { - if(items[i]["id"] != "editor-assign-local-time") { - continue; - } + auto pos = items.erase(items.begin() + i); + std::vector times; - items.erase(items.begin() + i); - std::vector times; + tod_manager* tod_m = get_map_context().get_time_manager(); - tod_manager* tod_m = get_map_context().get_time_manager(); - - for(const time_of_day& time : tod_m->times(get_map_context().get_active_area())) { - times.emplace_back(config_of - ("details", time.name) // Use 'details' field here since the image will take the first column - ("image", time.image) - ); - } - - items.insert(items.begin() + i, times.begin(), times.end()); - break; + for(const time_of_day& time : tod_m->times(get_map_context().get_active_area())) { + times.emplace_back(config_of + ("details", time.name) // Use 'details' field here since the image will take the first column + ("image", time.image) + ); } + + items.insert(pos, times.begin(), times.end()); } void context_manager::apply_mask_dialog() diff --git a/src/editor/map/context_manager.hpp b/src/editor/map/context_manager.hpp index c9570be0e92..00eaa2fd1f5 100644 --- a/src/editor/map/context_manager.hpp +++ b/src/editor/map/context_manager.hpp @@ -105,22 +105,22 @@ public: void rename_area_dialog(); /** Menu expanding for open maps list */ - void expand_open_maps_menu(std::vector& items); + void expand_open_maps_menu(std::vector& items, int i); /** Menu expanding for most recent loaded list */ - void expand_load_mru_menu(std::vector& items); + void expand_load_mru_menu(std::vector& items, int i); /** Menu expanding for the map's player sides */ - void expand_sides_menu(std::vector& items); + void expand_sides_menu(std::vector& items, int i); /** Menu expanding for the map's defined areas */ - void expand_areas_menu(std::vector& items); + void expand_areas_menu(std::vector& items, int i); /** Menu expanding for the map's defined areas */ - void expand_time_menu(std::vector& items); + void expand_time_menu(std::vector& items, int i); /** Menu expanding for the map's defined areas */ - void expand_local_time_menu(std::vector& items); + void expand_local_time_menu(std::vector& items, int i); /** Display a load map dialog and process user input. */ void load_map_dialog(bool force_same_context = false); diff --git a/src/editor/palette/common_palette.hpp b/src/editor/palette/common_palette.hpp index d175d92c305..6eb6c41dbbf 100644 --- a/src/editor/palette/common_palette.hpp +++ b/src/editor/palette/common_palette.hpp @@ -75,8 +75,8 @@ public: virtual const std::vector& get_groups() const = 0; /** Menu expanding for palette group list */ - virtual void expand_palette_groups_menu(std::vector& items) = 0; - virtual void expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items) = 0; + virtual void expand_palette_groups_menu(std::vector& items, int i) = 0; + virtual void expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i) = 0; //item virtual int num_items() = 0; diff --git a/src/editor/palette/editor_palettes.cpp b/src/editor/palette/editor_palettes.cpp index 4d1ecd433de..6384535a1a1 100644 --- a/src/editor/palette/editor_palettes.cpp +++ b/src/editor/palette/editor_palettes.cpp @@ -42,48 +42,43 @@ template sdl_handler_vector editor_palette::handler_members(); template sdl_handler_vector editor_palette::handler_members(); template -void editor_palette::expand_palette_groups_menu(std::vector& items) +void editor_palette::expand_palette_groups_menu(std::vector& items, int i) { - for (unsigned int i = 0; i < items.size(); ++i) { - if (items[i]["id"] == "editor-palette-groups") { - items.erase(items.begin() + i); + auto pos = items.erase(items.begin() + i); - std::vector groups; - const std::vector& item_groups = get_groups(); + std::vector groups; + const std::vector& item_groups = get_groups(); - for (size_t mci = 0; mci < item_groups.size(); ++mci) { - std::string groupname = item_groups[mci].name; - if (groupname.empty()) { - groupname = _("(Unknown Group)"); - } - std::stringstream i_ss; - i_ss << item_groups[mci].icon; - if (mci == active_group_index()) { + for (size_t mci = 0; mci < item_groups.size(); ++mci) { + std::string groupname = item_groups[mci].name; + if (groupname.empty()) { + groupname = _("(Unknown Group)"); + } + std::stringstream i_ss; + i_ss << item_groups[mci].icon; + if (mci == active_group_index()) { - if (filesystem::file_exists(i_ss.str() + "_30-pressed.png" ) ) { - i_ss << "_30-pressed.png"; - } else { - i_ss << "_30.png~CS(70,70,0)"; - } - - } else { - i_ss << "_30.png"; - } - - groups.emplace_back(config_of - ("label", groupname) - ("icon", i_ss.str()) - ); + if (filesystem::file_exists(i_ss.str() + "_30-pressed.png" ) ) { + i_ss << "_30-pressed.png"; + } else { + i_ss << "_30.png~CS(70,70,0)"; } - items.insert(items.begin() + i, groups.begin(), groups.end()); - break; + } else { + i_ss << "_30.png"; } + + groups.emplace_back(config_of + ("label", groupname) + ("icon", i_ss.str()) + ); } + + items.insert(pos, groups.begin(), groups.end()); } -template void editor_palette::expand_palette_groups_menu(std::vector& items); -template void editor_palette::expand_palette_groups_menu(std::vector& items); -template void editor_palette::expand_palette_groups_menu(std::vector& items); +template void editor_palette::expand_palette_groups_menu(std::vector& items, int i); +template void editor_palette::expand_palette_groups_menu(std::vector& items, int i); +template void editor_palette::expand_palette_groups_menu(std::vector& items, int i); template bool editor_palette::scroll_up() @@ -104,7 +99,7 @@ template bool editor_palette::scroll_up(); template bool editor_palette::scroll_up(); template -void editor_palette::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items) +void editor_palette::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i) { const std::vector& item_groups = get_groups(); @@ -117,9 +112,9 @@ void editor_palette::expand_palette_groups_menu(std::vector< std::pair< st items.push_back(std::pair( img, groupname)); } } -template void editor_palette::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items); -template void editor_palette::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items); -template void editor_palette::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items); +template void editor_palette::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i); +template void editor_palette::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i); +template void editor_palette::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i); template bool editor_palette::can_scroll_up() diff --git a/src/editor/palette/editor_palettes.hpp b/src/editor/palette/editor_palettes.hpp index 3b4aa695a1b..bcb910202c8 100644 --- a/src/editor/palette/editor_palettes.hpp +++ b/src/editor/palette/editor_palettes.hpp @@ -63,8 +63,8 @@ public: size_t start_num(void) override { return items_start_; } /** Menu expanding for palette group list */ - void expand_palette_groups_menu(std::vector< std::pair >& items) override; - void expand_palette_groups_menu(std::vector& items) override; + void expand_palette_groups_menu(std::vector< std::pair >& items, int i) override; + void expand_palette_groups_menu(std::vector& items, int i) override; void set_group(size_t index) override; // int active_group(); diff --git a/src/editor/palette/empty_palette.hpp b/src/editor/palette/empty_palette.hpp index 03b6fa155f7..8a06df4d1d1 100644 --- a/src/editor/palette/empty_palette.hpp +++ b/src/editor/palette/empty_palette.hpp @@ -72,8 +72,8 @@ public: virtual const std::vector& get_groups() const override { return empty_; } /** Menu expanding for palette group list */ - virtual void expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& /*items*/) override {} - virtual void expand_palette_groups_menu(std::vector< config> & /*items*/) override {} + virtual void expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& /*items*/,int) override {} + virtual void expand_palette_groups_menu(std::vector< config> & /*items*/,int) override {} //item virtual int num_items() override {return 0;} diff --git a/src/editor/palette/location_palette.hpp b/src/editor/palette/location_palette.hpp index 8edd9387ff9..46ed77b31d3 100644 --- a/src/editor/palette/location_palette.hpp +++ b/src/editor/palette/location_palette.hpp @@ -42,8 +42,8 @@ public: size_t start_num(void) override { return items_start_; } /** Menu expanding for palette group list */ - void expand_palette_groups_menu(std::vector< std::pair >&) override {} - void expand_palette_groups_menu(std::vector&) override {} + void expand_palette_groups_menu(std::vector>&,int) override {} + void expand_palette_groups_menu(std::vector&,int) override {} virtual void set_group(size_t /*index*/) override {} virtual void next_group() override {}