Eliminate pointless loops in editor menu generation

This commit is contained in:
Celtic Minstrel 2017-04-21 18:52:49 -04:00
parent d18b94d65d
commit 2e988809e8
8 changed files with 161 additions and 212 deletions

View file

@ -1036,73 +1036,70 @@ void editor_controller::show_menu(const std::vector<config>& items_arg, int xloc
if(first_id == "EDITOR-LOAD-MRU-PLACEHOLDER") { if(first_id == "EDITOR-LOAD-MRU-PLACEHOLDER") {
active_menu_ = editor::LOAD_MRU; 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") { if(first_id == "editor-switch-map") {
active_menu_ = editor::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") { if(first_id == "editor-palette-groups") {
active_menu_ = editor::PALETTE; 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") { if(first_id == "editor-switch-side") {
active_menu_ = editor::SIDE; active_menu_ = editor::SIDE;
context_manager_->expand_sides_menu(items); context_manager_->expand_sides_menu(items, 0);
} }
if(first_id == "editor-switch-area") { if(first_id == "editor-switch-area") {
active_menu_ = editor::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") { if(!items.empty() && items.front()["id"] == "editor-switch-time") {
active_menu_ = editor::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") { if(first_id == "editor-assign-local-time") {
active_menu_ = editor::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") { if(first_id == "menu-unit-facings") {
active_menu_ = editor::UNIT_FACING; active_menu_ = editor::UNIT_FACING;
items.erase(items.begin()); auto pos = items.erase(items.begin());
int dir = 0;
for(int dir = 0; dir != map_location::NDIRECTIONS; ++dir) { std::generate_n(std::inserter<std::vector<config>>(items, pos), int(map_location::NDIRECTIONS), [&dir]() -> config {
items.emplace_back(config_of("label", map_location::write_translated_direction(map_location::DIRECTION(dir)))); return config_of("label", map_location::write_translated_direction(map_location::DIRECTION(dir++)));
} });
} }
if(first_id == "editor-playlist") { if(first_id == "editor-playlist") {
active_menu_ = editor::MUSIC; active_menu_ = editor::MUSIC;
items.erase(items.begin()); auto pos = items.erase(items.begin());
std::transform(music_tracks_.begin(), music_tracks_.end(), std::inserter<std::vector<config>>(items, pos), [](const sound::music_track& track) -> config {
for(const sound::music_track& track : music_tracks_) { return config_of("label", track.title().empty() ? track.id() : track.title());
items.emplace_back(config_of("label", track.title().empty() ? track.id() : track.title())); });
}
} }
if(first_id == "editor-assign-schedule") { if(first_id == "editor-assign-schedule") {
active_menu_ = editor::SCHEDULE; active_menu_ = editor::SCHEDULE;
auto pos = items.erase(items.begin());
items.erase(items.begin()); std::transform(tods_.begin(), tods_.end(), std::inserter<std::vector<config>>(items, pos), [](const tods_map::value_type& tod) -> config {
for(const auto& tod : tods_) { return config_of("label", tod.second.first);
items.emplace_back(config_of("label", tod.second.first)); });
}
} }
if(first_id == "editor-assign-local-schedule") { if(first_id == "editor-assign-local-schedule") {
active_menu_ = editor::LOCAL_SCHEDULE; active_menu_ = editor::LOCAL_SCHEDULE;
auto pos = items.erase(items.begin());
items.erase(items.begin()); std::transform(tods_.begin(), tods_.end(), std::inserter<std::vector<config>>(items, pos), [](const tods_map::value_type& tod) -> config {
for(const auto& tod : tods_) { return config_of("label", tod.second.first);
items.emplace_back(config_of("label", tod.second.first)); });
}
} }
command_executor::show_menu(items, xloc, yloc, context_menu, disp); command_executor::show_menu(items, xloc, yloc, context_menu, disp);

View file

@ -293,14 +293,9 @@ void context_manager::new_scenario_dialog()
} }
} }
void context_manager::expand_open_maps_menu(std::vector<config>& items) void context_manager::expand_open_maps_menu(std::vector<config>& items, int i)
{ {
for(unsigned int i = 0; i < items.size(); ++i) { auto pos = items.erase(items.begin() + i);
if(items[i]["id"] != "editor-switch-map") {
continue;
}
items.erase(items.begin() + i);
std::vector<config> contexts; std::vector<config> contexts;
for(size_t mci = 0; mci < map_contexts_.size(); ++mci) { for(size_t mci = 0; mci < map_contexts_.size(); ++mci) {
std::string filename = map_contexts_[mci]->get_filename(); std::string filename = map_contexts_[mci]->get_filename();
@ -320,25 +315,18 @@ void context_manager::expand_open_maps_menu(std::vector<config>& items)
} }
contexts.emplace_back(config_of("label", label)); contexts.emplace_back(config_of("label", label));
} }
items.insert(items.begin() + i, contexts.begin(), contexts.end()); items.insert(pos, contexts.begin(), contexts.end());
break;
}
} }
void context_manager::expand_load_mru_menu(std::vector<config>& items) void context_manager::expand_load_mru_menu(std::vector<config>& items, int i)
{ {
std::vector<std::string> mru = preferences::editor::recent_files(); std::vector<std::string> mru = preferences::editor::recent_files();
for(unsigned int i = 0; i < items.size(); ++i) { auto pos = items.erase(items.begin() + i);
if(items[i]["id"] != "EDITOR-LOAD-MRU-PLACEHOLDER") {
continue;
}
items.erase(items.begin() + i);
if(mru.empty()) { if(mru.empty()) {
items.insert(items.begin() + i, config_of("label", _("No Recent Files"))); items.insert(pos, config_of("label", _("No Recent Files")));
continue; return;
} }
for(std::string& path : mru) { for(std::string& path : mru) {
@ -353,25 +341,17 @@ void context_manager::expand_load_mru_menu(std::vector<config>& items)
return config_of("label", str); return config_of("label", str);
}); });
items.insert(items.begin() + i, temp.begin(), temp.end()); items.insert(pos, temp.begin(), temp.end());
break;
}
} }
void context_manager::expand_areas_menu(std::vector<config>& items) void context_manager::expand_areas_menu(std::vector<config>& items, int i)
{ {
tod_manager* tod = get_map_context().get_time_manager(); tod_manager* tod = get_map_context().get_time_manager();
if(!tod) { if(!tod) {
return; return;
} }
for(unsigned int i = 0; i < items.size(); ++i) { auto pos = items.erase(items.begin() + i);
if(items[i]["id"] != "editor-switch-area") {
continue;
}
items.erase(items.begin() + i);
std::vector<config> area_entries; std::vector<config> area_entries;
std::vector<std::string> area_ids = tod->get_area_ids(); std::vector<std::string> area_ids = tod->get_area_ids();
@ -391,19 +371,12 @@ void context_manager::expand_areas_menu(std::vector<config>& items)
area_entries.emplace_back(config_of("label", label.str())); area_entries.emplace_back(config_of("label", label.str()));
} }
items.insert(items.begin() + i, area_entries.begin(), area_entries.end()); items.insert(pos, area_entries.begin(), area_entries.end());
break;
}
} }
void context_manager::expand_sides_menu(std::vector<config>& items) void context_manager::expand_sides_menu(std::vector<config>& items, int i)
{ {
for(unsigned int i = 0; i < items.size(); ++i) { auto pos = items.erase(items.begin() + i);
if(items[i]["id"] != "editor-switch-side") {
continue;
}
items.erase(items.begin() + i);
std::vector<config> contexts; std::vector<config> 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) {
@ -416,19 +389,12 @@ void context_manager::expand_sides_menu(std::vector<config>& items)
contexts.emplace_back(config_of("label", label.str())); contexts.emplace_back(config_of("label", label.str()));
} }
items.insert(items.begin() + i, contexts.begin(), contexts.end()); items.insert(pos, contexts.begin(), contexts.end());
break;
}
} }
void context_manager::expand_time_menu(std::vector<config>& items) void context_manager::expand_time_menu(std::vector<config>& items, int i)
{ {
for(unsigned int i = 0; i < items.size(); ++i) { auto pos = items.erase(items.begin() + i);
if(items[i]["id"] != "editor-switch-time") {
continue;
}
items.erase(items.begin() + i);
std::vector<config> times; std::vector<config> times;
tod_manager* tod_m = get_map_context().get_time_manager(); tod_manager* tod_m = get_map_context().get_time_manager();
@ -442,19 +408,12 @@ void context_manager::expand_time_menu(std::vector<config>& items)
); );
} }
items.insert(items.begin() + i, times.begin(), times.end()); items.insert(pos, times.begin(), times.end());
break;
}
} }
void context_manager::expand_local_time_menu(std::vector<config>& items) void context_manager::expand_local_time_menu(std::vector<config>& items, int i)
{ {
for(unsigned int i = 0; i < items.size(); ++i) { auto pos = items.erase(items.begin() + i);
if(items[i]["id"] != "editor-assign-local-time") {
continue;
}
items.erase(items.begin() + i);
std::vector<config> times; std::vector<config> times;
tod_manager* tod_m = get_map_context().get_time_manager(); tod_manager* tod_m = get_map_context().get_time_manager();
@ -466,9 +425,7 @@ void context_manager::expand_local_time_menu(std::vector<config>& items)
); );
} }
items.insert(items.begin() + i, times.begin(), times.end()); items.insert(pos, times.begin(), times.end());
break;
}
} }
void context_manager::apply_mask_dialog() void context_manager::apply_mask_dialog()

View file

@ -105,22 +105,22 @@ public:
void rename_area_dialog(); void rename_area_dialog();
/** Menu expanding for open maps list */ /** Menu expanding for open maps list */
void expand_open_maps_menu(std::vector<config>& items); void expand_open_maps_menu(std::vector<config>& items, int i);
/** Menu expanding for most recent loaded list */ /** Menu expanding for most recent loaded list */
void expand_load_mru_menu(std::vector<config>& items); void expand_load_mru_menu(std::vector<config>& items, int i);
/** Menu expanding for the map's player sides */ /** Menu expanding for the map's player sides */
void expand_sides_menu(std::vector<config>& items); void expand_sides_menu(std::vector<config>& items, int i);
/** Menu expanding for the map's defined areas */ /** Menu expanding for the map's defined areas */
void expand_areas_menu(std::vector<config>& items); void expand_areas_menu(std::vector<config>& items, int i);
/** Menu expanding for the map's defined areas */ /** Menu expanding for the map's defined areas */
void expand_time_menu(std::vector<config>& items); void expand_time_menu(std::vector<config>& items, int i);
/** Menu expanding for the map's defined areas */ /** Menu expanding for the map's defined areas */
void expand_local_time_menu(std::vector<config>& items); void expand_local_time_menu(std::vector<config>& items, int i);
/** Display a load map dialog and process user input. */ /** Display a load map dialog and process user input. */
void load_map_dialog(bool force_same_context = false); void load_map_dialog(bool force_same_context = false);

View file

@ -75,8 +75,8 @@ public:
virtual const std::vector<item_group>& get_groups() const = 0; virtual const std::vector<item_group>& get_groups() const = 0;
/** Menu expanding for palette group list */ /** Menu expanding for palette group list */
virtual void expand_palette_groups_menu(std::vector<config>& items) = 0; virtual void expand_palette_groups_menu(std::vector<config>& items, int i) = 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< std::pair< std::string, std::string> >& items, int i) = 0;
//item //item
virtual int num_items() = 0; virtual int num_items() = 0;

View file

@ -42,11 +42,9 @@ template sdl_handler_vector editor_palette<unit_type>::handler_members();
template sdl_handler_vector editor_palette<overlay>::handler_members(); template sdl_handler_vector editor_palette<overlay>::handler_members();
template<class Item> template<class Item>
void editor_palette<Item>::expand_palette_groups_menu(std::vector<config>& items) void editor_palette<Item>::expand_palette_groups_menu(std::vector<config>& items, int i)
{ {
for (unsigned int i = 0; i < items.size(); ++i) { auto pos = items.erase(items.begin() + i);
if (items[i]["id"] == "editor-palette-groups") {
items.erase(items.begin() + i);
std::vector<config> groups; std::vector<config> groups;
const std::vector<item_group>& item_groups = get_groups(); const std::vector<item_group>& item_groups = get_groups();
@ -76,14 +74,11 @@ void editor_palette<Item>::expand_palette_groups_menu(std::vector<config>& items
); );
} }
items.insert(items.begin() + i, groups.begin(), groups.end()); items.insert(pos, groups.begin(), groups.end());
break;
}
}
} }
template void editor_palette<t_translation::terrain_code>::expand_palette_groups_menu(std::vector<config>& items); template void editor_palette<t_translation::terrain_code>::expand_palette_groups_menu(std::vector<config>& items, int i);
template void editor_palette<unit_type>::expand_palette_groups_menu(std::vector<config>& items); template void editor_palette<unit_type>::expand_palette_groups_menu(std::vector<config>& items, int i);
template void editor_palette<overlay>::expand_palette_groups_menu(std::vector<config>& items); template void editor_palette<overlay>::expand_palette_groups_menu(std::vector<config>& items, int i);
template<class Item> template<class Item>
bool editor_palette<Item>::scroll_up() bool editor_palette<Item>::scroll_up()
@ -104,7 +99,7 @@ template bool editor_palette<unit_type>::scroll_up();
template bool editor_palette<overlay>::scroll_up(); template bool editor_palette<overlay>::scroll_up();
template<class Item> template<class Item>
void editor_palette<Item>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items) void editor_palette<Item>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i)
{ {
const std::vector<item_group>& item_groups = get_groups(); const std::vector<item_group>& item_groups = get_groups();
@ -117,9 +112,9 @@ void editor_palette<Item>::expand_palette_groups_menu(std::vector< std::pair< st
items.push_back(std::pair<std::string, std::string>( img, groupname)); items.push_back(std::pair<std::string, std::string>( img, groupname));
} }
} }
template void editor_palette<t_translation::terrain_code>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items); template void editor_palette<t_translation::terrain_code>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i);
template void editor_palette<unit_type>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items); template void editor_palette<unit_type>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i);
template void editor_palette<overlay>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items); template void editor_palette<overlay>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i);
template<class Item> template<class Item>
bool editor_palette<Item>::can_scroll_up() bool editor_palette<Item>::can_scroll_up()

View file

@ -63,8 +63,8 @@ public:
size_t start_num(void) override { return items_start_; } size_t start_num(void) override { return items_start_; }
/** Menu expanding for palette group list */ /** Menu expanding for palette group list */
void expand_palette_groups_menu(std::vector< std::pair<std::string, std::string> >& items) override; void expand_palette_groups_menu(std::vector< std::pair<std::string, std::string> >& items, int i) override;
void expand_palette_groups_menu(std::vector<config>& items) override; void expand_palette_groups_menu(std::vector<config>& items, int i) override;
void set_group(size_t index) override; void set_group(size_t index) override;
// int active_group(); // int active_group();

View file

@ -72,8 +72,8 @@ public:
virtual const std::vector<item_group>& get_groups() const override { return empty_; } virtual const std::vector<item_group>& get_groups() const override { return empty_; }
/** Menu expanding for palette group list */ /** 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< std::pair< std::string, std::string> >& /*items*/,int) override {}
virtual void expand_palette_groups_menu(std::vector< config> & /*items*/) override {} virtual void expand_palette_groups_menu(std::vector< config> & /*items*/,int) override {}
//item //item
virtual int num_items() override {return 0;} virtual int num_items() override {return 0;}

View file

@ -42,8 +42,8 @@ public:
size_t start_num(void) override { return items_start_; } size_t start_num(void) override { return items_start_; }
/** Menu expanding for palette group list */ /** Menu expanding for palette group list */
void expand_palette_groups_menu(std::vector< std::pair<std::string, std::string> >&) override {} void expand_palette_groups_menu(std::vector<std::pair<std::string, std::string>>&,int) override {}
void expand_palette_groups_menu(std::vector<config>&) override {} void expand_palette_groups_menu(std::vector<config>&,int) override {}
virtual void set_group(size_t /*index*/) override {} virtual void set_group(size_t /*index*/) override {}
virtual void next_group() override {} virtual void next_group() override {}