Fix manipulator crash when selecting profession from empty list

Also add ListColumn::hasSelection()

Fixes #1040
develop
lethosor 2016-12-11 21:16:21 -05:00
parent 697d35a583
commit c8b7dbd255
2 changed files with 21 additions and 6 deletions

@ -317,6 +317,18 @@ public:
return results[0]; return results[0];
} }
bool hasSelection()
{
for (auto item : list)
{
if (item.selected)
{
return true;
}
}
return false;
}
void clearSelection() void clearSelection()
{ {
for_each_(list, clear_fn); for_each_(list, clear_fn);

@ -954,6 +954,7 @@ public:
viewscreen_unitprofessionset(vector<UnitInfo*> &base_units, viewscreen_unitprofessionset(vector<UnitInfo*> &base_units,
bool filter_selected = true bool filter_selected = true
) )
:menu_options(-1) // default
{ {
menu_options.multiselect = false; menu_options.multiselect = false;
menu_options.auto_select = true; menu_options.auto_select = true;
@ -967,7 +968,7 @@ public:
std::string name = manager.templates[i].name; std::string name = manager.templates[i].name;
if (manager.templates[i].mask) if (manager.templates[i].mask)
name += " (mask)"; name += " (mask)";
ListEntry<size_t> elem(name, i+1); ListEntry<size_t> elem(name, i);
menu_options.add(elem); menu_options.add(elem);
} }
menu_options.filterDisplay(); menu_options.filterDisplay();
@ -1000,21 +1001,23 @@ public:
return; return;
} }
if (events->count(interface_key::SELECT)) if (events->count(interface_key::SELECT))
{
if (menu_options.hasSelection())
{ {
select_profession(menu_options.getFirstSelectedElem()); select_profession(menu_options.getFirstSelectedElem());
}
Screen::dismiss(this); Screen::dismiss(this);
return; return;
} }
} }
void select_profession(size_t selected) void select_profession(size_t selected)
{ {
if (selected > manager.templates.size()) if (manager.templates.empty() || selected >= manager.templates.size())
return; return;
ProfessionTemplate prof = manager.templates[selected - 1]; ProfessionTemplate prof = manager.templates[selected];
for (auto it = units.begin(); it != units.end(); ++it) for (UnitInfo *u : units)
{ {
UnitInfo* u = (*it);
if (!u || !u->unit || !u->allowEdit) continue; if (!u || !u->unit || !u->allowEdit) continue;
prof.apply(u); prof.apply(u);
} }