Skip to content

Commit

Permalink
Use std::vector for the list of focused units
Browse files Browse the repository at this point in the history
I would like to have some decent container to use in longturn#805 (if several units are
selected, I want the right click menu to apply to all of them, which will
require a container).

Change focus_units (=get_units_in_focus()) to be a std::vector<unit *>. This
has implications in a lot of places, which are also modified to use
std::vector.
  • Loading branch information
lmoureaux committed Aug 3, 2022
1 parent 30ae545 commit 1a04505
Show file tree
Hide file tree
Showing 23 changed files with 256 additions and 445 deletions.
23 changes: 6 additions & 17 deletions client/citydlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,6 @@ void unit_list_item::create_menu()

m_menu = new QMenu;

auto *units = unit_list_new();
unit_list_append(units, m_unit);

auto *activate_and_close_action = new QAction(_("Activate unit"), this);
connect(activate_and_close_action, &QAction::triggered, this,
&unit_list_item::activate_and_close_dialog);
Expand Down Expand Up @@ -693,35 +690,33 @@ void unit_list_item::create_menu()
m_menu->addAction(change_homecity_action);
}

if (units_can_load(units)) {
if (units_can_load({m_unit})) {
auto *load_action = new QAction(_("Load"), this);
connect(load_action, &QAction::triggered, this, &unit_list_item::load);
m_menu->addAction(load_action);
}

if (units_can_unload(units)) {
if (units_can_unload({m_unit})) {
auto *unload_action = new QAction(_("Unload"), this);
connect(unload_action, &QAction::triggered, this,
&unit_list_item::unload);
m_menu->addAction(unload_action);
}

if (units_are_occupied(units)) {
if (units_are_occupied({m_unit})) {
auto *unload_all_action =
new QAction(_("Unload All From Transporter"), this);
connect(unload_all_action, &QAction::triggered, this,
&unit_list_item::unload_all);
m_menu->addAction(unload_all_action);
}

if (units_can_upgrade(units)) {
if (units_can_upgrade({m_unit})) {
auto *upgrade_action = new QAction(_("Upgrade Unit"), this);
connect(upgrade_action, &QAction::triggered, this,
&unit_list_item::upgrade);
m_menu->addAction(upgrade_action);
}

unit_list_destroy(units);
}

/**
Expand All @@ -738,10 +733,7 @@ bool unit_list_item::can_issue_orders() const
void unit_list_item::disband()
{
if (can_issue_orders()) {
auto *units = unit_list_new();
unit_list_append(units, m_unit);
popup_disband_dialog(units);
unit_list_destroy(units);
popup_disband_dialog({m_unit});
}
}

Expand Down Expand Up @@ -781,10 +773,7 @@ void unit_list_item::unload_all()
void unit_list_item::upgrade()
{
if (can_issue_orders()) {
auto *units = unit_list_new();
unit_list_append(units, m_unit);
popup_upgrade_dialog(units);
unit_list_destroy(units);
popup_upgrade_dialog({m_unit});
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/client_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ void set_client_state(enum client_states newstate)

update_info_label();
unit_focus_update();
update_unit_info_label(nullptr);
update_unit_info_label({});

break;
}
Expand Down
15 changes: 5 additions & 10 deletions client/climisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
***********************************************************************/

#include <QBitArray>
#include <algorithm>
#include <cstdarg>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -1054,19 +1055,13 @@ void cityrep_buy(struct city *pcity)
/**
Returns TRUE if any of the units can do the connect activity.
*/
bool can_units_do_connect(struct unit_list *punits,
bool can_units_do_connect(const std::vector<unit *> &units,
enum unit_activity activity,
struct extra_type *tgt)
{
unit_list_iterate(punits, punit)
{
if (can_unit_do_connect(punit, activity, tgt)) {
return true;
}
}
unit_list_iterate_end;

return false;
return std::any_of(units.begin(), units.end(), [&](const auto unit) {
return can_unit_do_connect(unit, activity, tgt);
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion client/climisc.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct city *get_nearest_city(const struct unit *punit, int *sq_dist);

void cityrep_buy(struct city *pcity);

bool can_units_do_connect(struct unit_list *punits,
bool can_units_do_connect(const std::vector<unit *> &units,
enum unit_activity activity,
struct extra_type *tgt);

Expand Down
Loading

0 comments on commit 1a04505

Please sign in to comment.