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") {
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<std::vector<config>>(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<std::vector<config>>(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<std::vector<config>>(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<std::vector<config>>(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);

View file

@ -293,182 +293,139 @@ 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) {
if(items[i]["id"] != "editor-switch-map") {
continue;
}
items.erase(items.begin() + i);
std::vector<config> 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<config> 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<config>& items)
void context_manager::expand_load_mru_menu(std::vector<config>& items, int i)
{
std::vector<std::string> 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<config> 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<config> 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<config>& items)
void context_manager::expand_areas_menu(std::vector<config>& 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<config> area_entries;
std::vector<std::string> 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<size_t>(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<config> area_entries;
std::vector<std::string> 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<size_t>(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<config>& items)
void context_manager::expand_sides_menu(std::vector<config>& 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<config> contexts;
items.erase(items.begin() + i);
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) {
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<config>& items)
void context_manager::expand_time_menu(std::vector<config>& 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<config> times;
items.erase(items.begin() + i);
std::vector<config> 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<config>& items)
void context_manager::expand_local_time_menu(std::vector<config>& 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<config> times;
items.erase(items.begin() + i);
std::vector<config> 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()

View file

@ -105,22 +105,22 @@ public:
void rename_area_dialog();
/** 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 */
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 */
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 */
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 */
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 */
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. */
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;
/** 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< std::pair< std::string, std::string> >& 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, int i) = 0;
//item
virtual int num_items() = 0;

View file

@ -42,48 +42,43 @@ template sdl_handler_vector editor_palette<unit_type>::handler_members();
template sdl_handler_vector editor_palette<overlay>::handler_members();
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) {
if (items[i]["id"] == "editor-palette-groups") {
items.erase(items.begin() + i);
auto pos = items.erase(items.begin() + i);
std::vector<config> groups;
const std::vector<item_group>& item_groups = get_groups();
std::vector<config> groups;
const std::vector<item_group>& 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<t_translation::terrain_code>::expand_palette_groups_menu(std::vector<config>& items);
template void editor_palette<unit_type>::expand_palette_groups_menu(std::vector<config>& items);
template void editor_palette<overlay>::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, int i);
template void editor_palette<overlay>::expand_palette_groups_menu(std::vector<config>& items, int i);
template<class Item>
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<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();
@ -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));
}
}
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<unit_type>::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);
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, int i);
template void editor_palette<overlay>::expand_palette_groups_menu(std::vector< std::pair< std::string, std::string> >& items, int i);
template<class Item>
bool editor_palette<Item>::can_scroll_up()

View file

@ -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<std::string, std::string> >& items) override;
void expand_palette_groups_menu(std::vector<config>& 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, int i) override;
void set_group(size_t index) override;
// int active_group();

View file

@ -72,8 +72,8 @@ public:
virtual const std::vector<item_group>& 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;}

View file

@ -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<std::string, std::string> >&) override {}
void expand_palette_groups_menu(std::vector<config>&) override {}
void expand_palette_groups_menu(std::vector<std::pair<std::string, std::string>>&,int) override {}
void expand_palette_groups_menu(std::vector<config>&,int) override {}
virtual void set_group(size_t /*index*/) override {}
virtual void next_group() override {}