The Battle for Wesnoth  1.19.10+dev
units_dialog.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024 - 2025
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "font/standard_colors.hpp"
20 #include "formatter.hpp"
21 #include "game_board.hpp"
22 #include "gettext.hpp"
24 #include "gui/dialogs/message.hpp"
26 #include "gui/widgets/listbox.hpp"
27 #include "gui/widgets/button.hpp"
28 #include "gui/widgets/label.hpp"
30 #include "gui/widgets/text_box.hpp"
33 #include "gui/widgets/window.hpp"
34 #include "help/help.hpp"
35 #include "log.hpp"
36 #include "replay_helper.hpp"
37 #include "play_controller.hpp"
38 #include "resources.hpp"
39 #include "serialization/markup.hpp"
40 #include "synced_context.hpp"
41 #include "team.hpp"
42 #include "units/helper.hpp"
43 #include "units/unit.hpp"
44 #include "units/ptr.hpp"
45 #include "units/types.hpp"
46 #include "utils/ci_searcher.hpp"
47 #include "whiteboard/manager.hpp"
48 
49 #include <functional>
50 #include <string>
51 
52 static lg::log_domain log_display("display");
53 #define LOG_DP LOG_STREAM(info, log_display)
54 
55 namespace gui2::dialogs
56 {
57 
58 namespace
59 {
60 // Index 2 is by-level
61 std::pair sort_default{ std::string{"unit_name"}, sort_order::type::ascending };
62 utils::optional<decltype(sort_default)> sort_last;
63 }
64 
65 REGISTER_DIALOG(units_dialog)
66 
68  : modal_dialog(window_id())
69  , selected_index_(-1)
70  , num_rows_(0)
71  , ok_label_(_("OK"))
72  , cancel_label_(_("Cancel"))
73  , show_rename_(false)
74  , show_dismiss_(false)
75  , show_variations_(false)
76  , sort_order_(sort_default)
77  , gender_(unit_race::GENDER::MALE)
78  , variation_()
79  , filter_options_()
80  , gender_toggle_()
81 {
82  for(widget* sort_toggle : find_widget<grid>("_header_grid")) {
83  // FIXME: only proper (non-spacer) header options are currently given an ID.
84  // We want the spacers to remain for sizing, but this is pretty fragile.
85  if(!sort_toggle->id().empty()) {
87  }
88  }
89 }
90 
91 namespace {
92 
93 template<typename T>
94 void dump_recall_list_to_console(const T& units)
95 {
96  log_scope2(log_display, "dump_recall_list_to_console()")
97 
98  LOG_DP << "size: " << units.size();
99 
100  std::size_t idx = 0;
101  for(const auto& u_ptr : units) {
102  LOG_DP << "\tunit[" << (idx++) << "]: " << u_ptr->id() << " name = '" << u_ptr->name() << "'";
103  }
104 }
105 
106 std::string get_title_suffix(int side_num)
107 {
108  if(!resources::gameboard) {
109  return "";
110  }
111 
112  unit_map& units = resources::gameboard->units();
113 
114  int controlled_recruiters = 0;
115  for(const auto& team : resources::gameboard->teams()) {
116  if(team.is_local_human() && !team.recruits().empty() && units.find_leader(team.side()) !=units.end()) {
117  ++controlled_recruiters;
118  }
119  }
120 
121  std::stringstream msg;
122  if(controlled_recruiters >= 2) {
124  if(leader != resources::gameboard->units().end() && !leader->name().empty()) {
125  msg << " (" << leader->name() << ")";
126  }
127  }
128 
129  return msg.str();
130 }
131 }
132 
134 {
135  text_box& filter = find_widget<text_box>("filter_box");
136  filter.on_modified([this](const auto& box) { filter_text_changed(box.text()); });
137 
138  listbox& list = find_widget<listbox>("main_list");
140 
142  find_widget<button>("show_help"),
143  std::bind(&units_dialog::show_help, this));
144 
146  add_to_keyboard_chain(&list);
147 
148  show_list(list);
149 
150  find_widget<label>("title").set_label(title_);
151  find_widget<button>("ok").set_label(ok_label_);
152  find_widget<button>("cancel").set_label(cancel_label_);
153  find_widget<button>("dismiss").set_visible(show_dismiss_);
154  find_widget<button>("rename").set_visible(show_rename_);
155  find_widget<grid>("variation_gender_grid").set_visible(show_variations_);
156 
157  // Gender and variation selectors
158  if(show_variations_) {
160  find_widget<menu_button>("variation_box"), [this](auto&&...) { update_variation(); });
161 
162  auto& group = get_toggle();
163  group.add_member(find_widget<toggle_button>("male_toggle", true, true), unit_race::MALE);
164  group.add_member(find_widget<toggle_button>("female_toggle", true, true), unit_race::FEMALE);
165 
168  [this](widget&, const unit_race::GENDER& gender) { update_gender(gender); });
169  }
170 
172 }
173 
175 {
176  if (num_rows_ == 0) {
177  return;
178  }
179 
180  for(std::size_t i = 0; i < num_rows_; i++) {
181  widget_data row_data;
182  widget_item column;
183  // generate tooltip for ith row
184  if (tooltip_gen_) {
185  column["tooltip"] = tooltip_gen_(i);
186  }
187 
188  // if custom filter text generator exists, use it to generate the filter text
189  if (filter_gen_) {
190  filter_options_.push_back(filter_gen_(i));
191  }
192 
193  std::vector<std::string> filter_keys;
194  for (const auto& [id, gen] : column_generators_) {
195  column["use_markup"] = "true";
196  // generate label for ith row and column with 'id'
197  column["label"] = gen(i);
198  if (!filter_gen_ && id != "unit_image") {
199  filter_keys.emplace_back(column["label"]);
200  }
201  row_data.emplace(id, column);
202  }
203 
204  if (!filter_gen_) {
205  filter_options_.push_back(filter_keys);
206  }
207  list.add_row(row_data);
208  }
209 
210  const auto [sorter_id, order] = sort_last.value_or(sort_order_);
211  list.set_active_sorter(sorter_id, order, true);
212  if (is_selected()) { // In this case, is an entry preselected by caller?
214  fire(event::NOTIFY_MODIFIED, *this, nullptr);
215  }
216 }
217 
218 void units_dialog::rename_unit(std::vector<unit_const_ptr>& unit_list)
219 {
220  listbox& list = find_widget<listbox>("main_list");
221 
223  if (selected_index_ == -1) {
224  return;
225  }
226 
227  unit& selected_unit = const_cast<unit&>(*unit_list[selected_index_]);
228 
229  std::string name = selected_unit.name();
230 
231  if(gui2::dialogs::edit_text::execute(_("Rename Unit"), _("Name:"), name)) {
232  selected_unit.rename(name);
233 
234  list.get_row_grid(selected_index_)->find_widget<label>("unit_name").set_label(name);
235 
238 
241  }
242 }
243 
244 void units_dialog::dismiss_unit(std::vector<unit_const_ptr>& unit_list, const team& team)
245 {
246  LOG_DP << "Recall list units:"; dump_recall_list_to_console(unit_list);
247 
248  listbox& list = find_widget<listbox>("main_list");
250  if (selected_index_ == -1) {
251  return;
252  }
253 
254  const unit& u = *unit_list[selected_index_].get();
255 
256  if(!u.dismissable()) {
258  return;
259  }
260 
261  // If the unit is of level > 1, or is close to advancing, we warn the player about it
262  std::stringstream message;
263  if(u.loyal()) {
264  message << _("This unit is loyal and requires no upkeep.") << " " << (u.gender() == unit_race::MALE
265  ? _("Do you really want to dismiss him?")
266  : _("Do you really want to dismiss her?"));
267 
268  } else if(u.level() > 1) {
269  message << _("This unit is an experienced one, having advanced levels.") << " " << (u.gender() == unit_race::MALE
270  ? _("Do you really want to dismiss him?")
271  : _("Do you really want to dismiss her?"));
272 
273  } else if(u.experience() > u.max_experience()/2) {
274  message << _("This unit is close to advancing a level.") << " " << (u.gender() == unit_race::MALE
275  ? _("Do you really want to dismiss him?")
276  : _("Do you really want to dismiss her?"));
277  }
278 
279  if(!message.str().empty()) {
280  const int res = gui2::show_message(_("Dismiss Unit"), message.str(), message::yes_no_buttons);
281  if(res != gui2::retval::OK) {
282  return;
283  }
284  }
285 
286  unit_list.erase(unit_list.begin() + selected_index_);
287 
288  // Remove the entry from the filter list
290 
291  // Remove the entry from the dialog list
293 
294  // This line can change selected_index_, so the erasing needs to be done before it
296 
297  assert(filter_options_.size() == list.get_item_count());
298 
299  LOG_DP << "Dismissing a unit, side = " << u.side() << ", id = '" << u.id() << "'";
300  LOG_DP << "That side's recall list:";
301  dump_recall_list_to_console(team.recall_list());
302 
303  // Find the unit in the recall list.
304  unit_const_ptr dismissed_unit = team.recall_list().find_if_matches_id(u.id());
305  assert(dismissed_unit);
306 
307  // Record the dismissal, then delete the unit.
308  synced_context::run_and_throw("disband", replay_helper::get_disband(dismissed_unit->id()));
309 
310  // Close the dialog if all units are dismissed
311  if(list.get_item_count() == 0) {
313  }
314 }
315 
317 {
318  if (!topic_id_.empty()) {
320  }
321 }
322 
324 {
325  selected_index_ = find_widget<listbox>("main_list").get_selected_row();
326 
327  if (selected_index_ == -1) {
328  return;
329  }
330 
331  fire(event::NOTIFY_MODIFIED, *this, nullptr);
332 }
333 
335 {
336  listbox& list = find_widget<listbox>("main_list");
337  if(const auto [sorter, order] = list.get_active_sorter(); sorter) {
338  sort_last.emplace(sorter->id(), order);
339  } else {
340  sort_last.reset();
341  }
342 
343  if(get_retval() == retval::OK) {
345  }
346 }
347 
348 void units_dialog::filter_text_changed(const std::string& text)
349 {
350  const std::size_t shown = find_widget<listbox>("main_list")
351  .filter_rows_by([this, match = translation::make_ci_matcher(text)](std::size_t row) {
352  return match(filter_options_[row]);
353  });
354 
355  // Disable rename and dismiss buttons if no units are shown
356  find_widget<button>("rename").set_active(shown > 0);
357  find_widget<button>("dismiss").set_active(shown > 0);
358 }
359 
361 {
362  gender_ = val;
363 
364  selected_index_ = find_widget<listbox>("main_list").get_selected_row();
365  if(selected_index_ == -1) {
366  return;
367  }
368 
369  fire(event::NOTIFY_MODIFIED, *this, nullptr);
370 }
371 
373 {
374  variation_ = find_widget<menu_button>("variation_box").get_value_config()["variation_id"].str();
375 
376  selected_index_ = find_widget<listbox>("main_list").get_selected_row();
377  if(selected_index_ == -1) {
378  return;
379  }
380 
381  fire(event::NOTIFY_MODIFIED, *this, nullptr);
382 }
383 
384 // } -------------------- BUILDERS -------------------- {
385 std::unique_ptr<units_dialog> units_dialog::build_create_dialog(const std::vector<const unit_type*>& types_list)
386 {
387  auto dlg = std::make_unique<units_dialog>();
388 
389  const auto type_gen = [](const auto& type) {
390  std::string type_name = type->type_name();
391  if(type_name != type->id()) {
392  type_name += " (" + type->id() + ")";
393  }
394  return type_name;
395  };
396 
397  const auto race_gen = [](const auto& type) {
398  return type->race()->plural_name();
399  };
400 
401  const auto populate_variations = [&dlg](const unit_type& ut) {
402  // Populate variations box
403  menu_button& var_box = dlg->find_widget<menu_button>("variation_box");
404  std::vector<config> var_box_values;
405  var_box_values.emplace_back("label", _("unit_variation^Default Variation"), "variation_id", "");
406 
407  const auto& uvars = ut.variation_types();
408 
409  var_box.set_active(!uvars.empty());
410 
411  unsigned n = 0, selection = 0;
412 
413  for(const auto& [uv_id, uv] : uvars) {
414  ++n;
415 
416  std::string uv_label;
417  if(!uv.variation_name().empty()) {
418  uv_label = uv.variation_name() + " (" + uv_id + ")";
419  } else if(!uv.type_name().empty() && uv.type_name() != ut.type_name()) {
420  uv_label = uv.type_name() + " (" + uv_id + ")";
421  } else {
422  uv_label = uv_id;
423  }
424 
425  var_box_values.emplace_back("label", uv_label, "variation_id", uv_id);
426 
427  if(uv_id == dlg->variation()) {
428  selection = n;
429  }
430  }
431 
432  // If we didn't find the variation selection again then the new selected
433  // unit type doesn't have that variation id.
434  if(!selection) {
435  dlg->clear_variation();
436  }
437 
438  var_box.set_values(var_box_values, selection);
439  };
440 
441  dlg->set_title(_("Create Unit"))
442  .set_ok_label(_("Create"))
443  .set_help_topic("..units")
444  .set_row_num(types_list.size())
445  .set_show_variations(true);
446 
447  // Listbox data
448  auto set_column = dlg->make_column_builder(types_list);
449 
450  set_column("unit_name", type_gen, sort_type::generator);
451  set_column("unit_details", race_gen, sort_type::generator);
452 
453  dlg->on_modified([populate_variations, &dlg, &types_list](std::size_t index) -> const auto& {
454  const unit_type* ut = types_list[index];
455 
456  if (dlg->is_selected() && (static_cast<int>(index) == dlg->get_selected_index())) {
457  dlg->get_toggle().set_member_states(dlg->gender());
458  } else {
459  dlg->get_toggle().set_members_enabled(
460  [ut](const unit_race::GENDER& gender) { return ut->has_gender_variation(gender); });
461  }
462 
463  populate_variations(*ut);
464 
465  const auto& g = dlg->gender();
466  if(ut->has_gender_variation(g)) {
467  ut = &ut->get_gender_unit_type(g);
468  }
469 
470  const auto& var = dlg->variation();
471  if(!var.empty()) {
472  ut = &ut->get_variation(var);
473  }
474 
475  return *ut;
476  });
477 
478  return dlg;
479 }
480 
481 std::unique_ptr<units_dialog> units_dialog::build_recruit_dialog(
482  const std::vector<const unit_type*>& recruit_list,
483  recruit_msgs_map& err_msgs_map,
484  const team& team
485 )
486 {
487  auto dlg = std::make_unique<units_dialog>();
488  auto set_column = dlg->make_column_builder(recruit_list);
489 
490  set_column("unit_image", [&](const auto& recruit) {
491  std::string image_string = recruit->icon();
492  if (image_string.empty()) {
493  image_string = recruit->image();
494  }
495  image_string += "~RC(" + recruit->flag_rgb() + ">" + team.color() + ")";
496  image_string += "~SCALE_INTO(72,72)";
497  // Does the unit have error message? If so, grey out image.
498  if (!err_msgs_map[recruit].empty()) {
499  image_string += "~GS()";
500  }
501  return image_string;
502  }, sort_type::none);
503 
504  set_column("unit_details", [&](const auto& recruit) {
505  // Does the unit have error message? If so, grey out text here.
506  bool recruitable = err_msgs_map[recruit].empty();
507  return unit_helper::maybe_inactive(recruit->type_name(), recruitable)
508  + '\n'
509  + unit_helper::format_cost_string(recruit->cost(), recruitable);
511 
512  dlg->set_title(_("Recruit Unit") + get_title_suffix(team.side()))
513  .set_ok_label(_("Recruit"))
514  .set_help_topic("recruit_and_recall")
515  .set_row_num(recruit_list.size());
516 
517  dlg->set_tooltip_generator([&](std::size_t index) {
518  // Show the error message in case of disabled units, if any.
519  return err_msgs_map[recruit_list[index]];
520  });
521 
522  dlg->on_modified([&recruit_list](std::size_t index) -> const auto& { return *recruit_list[index]; });
523 
524  return dlg;
525 }
526 
527 std::unique_ptr<units_dialog> units_dialog::build_unit_list_dialog(std::vector<unit_const_ptr>& unit_list)
528 {
529  auto dlg = std::make_unique<units_dialog>();
530  dlg->set_title(_("Unit List"))
531  .set_ok_label(_("Scroll To"))
532  .set_help_topic("..units")
533  .set_row_num(unit_list.size())
534  .set_show_rename(true);
535 
536  // Rename functionality
537  button& rename = dlg->find_widget<button>("rename");
538  connect_signal_mouse_left_click(rename, std::bind([&]() {
539  dlg->rename_unit(unit_list);
540  }));
541 
542  auto set_column = dlg->make_column_builder(unit_list);
543 
544  set_column("unit_name",
545  [](const auto& unit) {
546  return !unit->name().empty() ? unit->name().str() : font::unicode_en_dash;
548 
549  set_column("unit_details",
550  [](const auto& unit) {
551  return unit->type_name().str();
553 
554  set_column("unit_level",
555  [](const auto& unit) {
557  },
558  [](const auto& unit) {
559  return std::tuple(unit->level(), -static_cast<int>(unit->experience_to_advance()));
560  });
561 
562  set_column("unit_moves",
563  [](const auto& unit) {
565  },
566  [](const auto& unit) {
567  return unit->movement_left();
568  });
569 
570  set_column("unit_hp",
571  [](const auto& unit) {
573  },
574  [](const auto& unit) {
575  return unit->hitpoints();
576  });
577 
578  set_column("unit_xp",
579  [](const auto& unit) {
580  std::stringstream exp_str;
581  if(unit->can_advance()) {
582  exp_str << unit->experience() << "/" << unit->max_experience();
583  } else {
584  exp_str << font::unicode_en_dash;
585  }
586  return markup::span_color(unit->xp_color(), exp_str.str());
587  },
588  [](const auto& unit) {
589  // this allows 0/35, 0/100 etc to be sorted
590  // also sorts 23/35 before 0/35, after which 0/100 comes
591  return unit->experience() + unit->max_experience();
592  });
593 
594  set_column("unit_status", [](const auto& unit) {
595  // Status
596  if(unit->incapacitated()) {
597  return "misc/petrified.png";
598  }
599 
600  if(unit->poisoned()) {
601  return "misc/poisoned.png";
602  }
603 
604  if(unit->slowed()) {
605  return "misc/slowed.png";
606  }
607 
608  if(unit->invisible(unit->get_location(), false)) {
609  return "misc/invisible.png";
610  }
611 
612  return "";
613  }, sort_type::none);
614 
615  set_column("unit_traits", [](const auto& unit) {
616  return utils::join(unit->trait_names(), ", ");
618 
619  dlg->set_filter_generator([&unit_list](std::size_t index) {
620  const auto& unit = unit_list[index];
621  std::vector<std::string> filter_keys;
622 
623  filter_keys.emplace_back(unit->type_name());
624  filter_keys.emplace_back(!unit->name().empty() ? unit->name().str() : font::unicode_en_dash);
625  filter_keys.emplace_back(std::to_string(unit->level()));
626  filter_keys.emplace_back(unit_type::alignment_description(unit->alignment(), unit->gender()));
627 
628  if(const auto* race = unit->race()) {
629  filter_keys.emplace_back(race->name(unit->gender()));
630  filter_keys.emplace_back(race->plural_name());
631  }
632 
633  for(const std::string& trait : unit->trait_names()) {
634  filter_keys.emplace_back(trait);
635  }
636 
637  return filter_keys;
638  });
639 
640  dlg->on_modified([&unit_list, &rename](std::size_t index) -> const auto& {
641  auto& unit = unit_list[index];
642  rename.set_active(!unit->unrenamable());
643  return *unit;
644  });
645 
646  return dlg;
647 }
648 
649 std::unique_ptr<units_dialog> units_dialog::build_recall_dialog(
650  std::vector<unit_const_ptr>& recall_list,
651  const team& team)
652 {
653  int wb_gold = 0;
654  if(resources::controller && resources::controller->get_whiteboard()) {
655  wb::future_map future; // So gold takes into account planned spending
656  wb_gold = resources::controller->get_whiteboard()->get_spent_gold_for(team.side());
657  }
658 
659  // Lambda to check if a unit is recallable
660  const auto recallable = [wb_gold, &team](const unit& unit) {
661  // Note: Our callers apply [filter_recall], but leave it to us to apply cost-based filtering.
662  const int recall_cost = unit.recall_cost() > -1 ? unit.recall_cost() : team.recall_cost();
663  return recall_cost <= team.gold() - wb_gold;
664  };
665 
666  auto dlg = std::make_unique<units_dialog>();
667  dlg->set_title(_("Recall Unit") + get_title_suffix(team.side()))
668  .set_ok_label(_("Recall"))
669  .set_help_topic("recruit_and_recall")
670  .set_row_num(recall_list.size())
671  .set_show_rename(true)
672  .set_show_dismiss(true);
673 
674  // Rename functionality
675  button& rename = dlg->find_widget<button>("rename");
676  connect_signal_mouse_left_click(rename, std::bind([&]() {
677  dlg->rename_unit(recall_list);
678  }));
679 
680  // Dismiss functionality
681  button& dismiss = dlg->find_widget<button>("dismiss");
682  connect_signal_mouse_left_click(dismiss, std::bind([&]() {
683  dlg->dismiss_unit(recall_list, team);
684  }));
685 
686  auto set_column = dlg->make_column_builder(recall_list);
687 
688  set_column("unit_image", [recallable](const auto& unit) {
689  std::string mods = unit->image_mods();
690  if(unit->can_recruit()) { mods += "~BLIT(" + unit::leader_crown() + ")"; }
691  for(const std::string& overlay : unit->overlays()) {
692  mods += "~BLIT(" + overlay + ")";
693  }
694  if(!recallable(*unit)) { mods += "~GS()"; }
695  mods += "~SCALE_INTO(72,72)";
696  return unit->absolute_image() + mods;
697  }, sort_type::none);
698 
699  set_column("unit_name",
700  [recallable](const auto& unit) {
701  const std::string& name = !unit->name().empty() ? unit->name().str() : font::unicode_en_dash;
702  return unit_helper::maybe_inactive(name, recallable(*unit));
703  },
704  [](const auto& unit) {
705  return unit->name().str();
706  });
707 
708  set_column("unit_details",
709  [recallable, &team](const auto& unit) {
710  std::stringstream details;
711  details << unit_helper::maybe_inactive(unit->type_name().str(), recallable(*unit)) << '\n';
712  const int recall_cost = unit->recall_cost() == -1 ? team.recall_cost() : unit->recall_cost();
713  if (recallable(*unit)) {
715  } else {
716  details << unit_helper::format_cost_string(recall_cost, false);
717  }
718  return details.str();
719  },
720  [](const auto& unit) {
721  return unit->type_name().str();
722  });
723 
724  set_column("unit_moves",
725  [recallable](const auto& unit) {
727  unit->movement_left(), unit->total_movement(), recallable(*unit));
728  },
729  [](const auto& unit) {
730  return unit->movement_left();
731  });
732 
733  set_column("unit_level",
734  [recallable](const auto& unit) {
735  return unit_helper::format_level_string(unit->level(), recallable(*unit));
736  },
737  [](const auto& unit) {
738  return std::tuple(unit->level(), -static_cast<int>(unit->experience_to_advance()));
739  });
740 
741  set_column("unit_hp",
742  [recallable](const auto& unit) {
743  const color_t& col = recallable(*unit) ? unit->hp_color() : font::GRAY_COLOR;
744  return markup::span_color(col, unit->hitpoints(), "/", unit->max_hitpoints());
745  },
746  [](const auto& unit) {
747  return unit->hitpoints();
748  });
749 
750  set_column("unit_xp",
751  [recallable](const auto& unit) {
752  const color_t& col = recallable(*unit) ? unit->xp_color() : font::GRAY_COLOR;
753  if(unit->can_advance()) {
754  return markup::span_color(col, unit->experience(), "/", unit->max_experience());
755  } else {
757  }
758  },
759  [](const auto& unit) {
760  // this allows 0/35, 0/100 etc to be sorted
761  // also sorts 23/35 before 0/35, after which 0/100 comes
762  return unit->experience() + unit->max_experience();
763  });
764 
765  set_column("unit_traits",
766  [recallable](const auto& unit) {
767  std::string traits;
768  for(const std::string& trait : unit->trait_names()) {
769  traits += (traits.empty() ? "" : "\n") + trait;
770  }
771  return unit_helper::maybe_inactive((!traits.empty() ? traits : font::unicode_en_dash), recallable(*unit));
772  },
773  [](const auto& unit) {
774  return !unit->trait_names().empty() ? unit->trait_names().front().str() : "";
775  });
776 
777 
778  dlg->set_sort_by(std::pair("unit_level", sort_order::type::descending));
779 
780  dlg->set_tooltip_generator([recallable, wb_gold, &recall_list](std::size_t index) {
781  if(recallable(*recall_list[index])) {
782  return std::string();
783  }
784 
785  // Just set the tooltip on every single element in this row.
786  if(wb_gold > 0) {
787  return _("This unit cannot be recalled because you will not have enough gold at this point in your plan.");
788  } else {
789  return _("This unit cannot be recalled because you do not have enough gold.");
790  }
791  });
792 
793  dlg->set_filter_generator([&recall_list](std::size_t index) {
794  const auto& unit = recall_list[index];
795  std::vector<std::string> filter_keys;
796 
797  filter_keys.emplace_back(unit->type_name());
798  filter_keys.emplace_back(!unit->name().empty() ? unit->name().str() : font::unicode_en_dash);
799  filter_keys.emplace_back(std::to_string(unit->level()));
800  filter_keys.emplace_back(unit_type::alignment_description(unit->alignment(), unit->gender()));
801 
802  if(const auto* race = unit->race()) {
803  filter_keys.emplace_back(race->name(unit->gender()));
804  filter_keys.emplace_back(race->plural_name());
805  }
806 
807  for(const std::string& trait : unit->trait_names()) {
808  filter_keys.emplace_back(trait);
809  }
810 
811  return filter_keys;
812  });
813 
814  dlg->on_modified([&recall_list, &rename](std::size_t index) -> const auto& {
815  const auto& unit = recall_list[index];
816  rename.set_active(!unit->unrenamable());
817  return *unit;
818  });
819 
820  return dlg;
821 }
822 
823 
824 
825 } // namespace dialogs
double g
Definition: astarsearch.cpp:63
virtual const unit_map & units() const override
Definition: game_board.hpp:107
Simple push button.
Definition: button.hpp:36
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:64
Main class to show messages to the user.
Definition: message.hpp:36
@ yes_no_buttons
Shows a yes and no button.
Definition: message.hpp:81
Abstract base class for all modal dialogs.
units_dialog & set_tooltip_generator(const Generator &generator)
Sets the generator function for the tooltips.
void show_list(listbox &list)
std::pair< std::string, sort_order::type > sort_order_
std::function< std::vector< std::string >std::size_t)> filter_gen_
std::map< const unit_type *, t_string > recruit_msgs_map
units_dialog & set_ok_label(const std::string &ok_label)
std::vector< std::vector< std::string > > filter_options_
auto make_column_builder(const std::vector< Value > &list)
Creates a generator function which registers secondary generator and sorter functions for the list co...
std::function< std::string(std::size_t)> tooltip_gen_
virtual void post_show() override
Actions to be taken after the window has been shown.
void rename_unit(std::vector< unit_const_ptr > &unit_list)
units_dialog & set_help_topic(const std::string &topic_id)
units_dialog & set_show_variations(bool show=true)
group< unit_race::GENDER > & get_toggle()
void filter_text_changed(const std::string &text)
static std::unique_ptr< units_dialog > build_unit_list_dialog(std::vector< unit_const_ptr > &units_list)
units_dialog & set_row_num(const std::size_t row_num)
static std::unique_ptr< units_dialog > build_create_dialog(const std::vector< const unit_type * > &types_list)
void dismiss_unit(std::vector< unit_const_ptr > &unit_list, const team &team)
void on_modified(const Func &f)
Registers an function which will fired on NOTIFY_MODIFIED dialog events.
std::map< std::string_view, std::function< std::string(std::size_t)> > column_generators_
virtual void pre_show() override
Actions to be taken before showing the window.
void list_item_clicked()
Callbacks.
unit_race::GENDER gender() const
Gender choice from the user.
static std::unique_ptr< units_dialog > build_recruit_dialog(const std::vector< const unit_type * > &recruit_list, recruit_msgs_map &err_msgs_map, const team &team)
static std::unique_ptr< units_dialog > build_recall_dialog(std::vector< unit_const_ptr > &recall_list, const team &team)
void update_gender(const unit_race::GENDER val)
bool fire(const ui_event event, widget &target)
Fires an event which has no extra parameters.
Definition: dispatcher.cpp:74
void on_modified(const Func &func)
Sets a common callback function for all members.
Definition: group.hpp:121
void add_member(selectable_item *w, const T &value)
Adds a widget/value pair to the group map.
Definition: group.hpp:41
void set_member_states(const T &value)
Sets the toggle values for all widgets besides the one associated with the specified value to false.
Definition: group.hpp:110
The listbox class.
Definition: listbox.hpp:41
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:92
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:267
void set_active_sorter(std::string_view id, sort_order::type order, bool select_first=false)
Sorts the listbox by a pre-set sorting option.
Definition: listbox.cpp:626
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:280
std::pair< widget *, sort_order::type > get_active_sorter() const
Returns a widget pointer to the active sorter, along with its corresponding order.
Definition: listbox.cpp:640
void remove_row(const unsigned row, unsigned count=1)
Removes a row in the listbox.
Definition: listbox.cpp:112
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:305
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:159
void set_values(const std::vector<::config > &values, unsigned selected=0)
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: menu_button.cpp:74
virtual void set_label(const t_string &text)
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
Base class for all widgets.
Definition: widget.hpp:55
void set_visible(const visibility visible)
Definition: widget.cpp:479
@ invisible
The user set the widget invisible, that means:
T * find_widget(const std::string_view id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition: widget.hpp:747
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:395
void keyboard_capture(widget *widget)
Definition: window.cpp:1201
void invalidate_layout()
Updates the size of the window.
Definition: window.cpp:760
void add_to_keyboard_chain(widget *widget)
Adds the widget to the keyboard chain.
Definition: window.cpp:1207
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
int get_retval()
Definition: window.hpp:402
std::shared_ptr< wb::manager > get_whiteboard() const
unit_ptr find_if_matches_id(const std::string &unit_id)
Find a unit by id.
static config get_disband(const std::string &unit_id)
static bool run_and_throw(const std::string &commandname, const config &data, action_spectator &spectator=get_default_spectator())
bool empty() const
Definition: tstring.hpp:195
const std::string & str() const
Definition: tstring.hpp:199
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:75
const std::string & color() const
Definition: team.hpp:247
int side() const
Definition: team.hpp:180
int recall_cost() const
Definition: team.hpp:185
bool is_local_human() const
Definition: team.hpp:258
int gold() const
Definition: team.hpp:181
recall_list_manager & recall_list()
Definition: team.hpp:206
const std::set< std::string > & recruits() const
Definition: team.hpp:214
Container associating units to locations.
Definition: map.hpp:98
unit_iterator end()
Definition: map.hpp:428
unit_iterator find_leader(int side)
Definition: map.cpp:320
@ FEMALE
Definition: race.hpp:28
@ MALE
Definition: race.hpp:28
A single unit type that the player may recruit.
Definition: types.hpp:43
static std::string alignment_description(unit_alignments::type align, unit_race::GENDER gender=unit_race::MALE)
Implementation detail of unit_type::alignment_description.
Definition: types.cpp:841
This class represents a single unit of a specific type.
Definition: unit.hpp:133
std::size_t i
Definition: function.cpp:1030
static std::string _(const char *str)
Definition: gettext.hpp:97
bool invisible(const map_location &loc, bool see_all=true) const
Definition: unit.cpp:2577
int max_hitpoints() const
The max number of hitpoints this unit can have.
Definition: unit.hpp:521
unit_alignments::type alignment() const
The alignment of this unit.
Definition: unit.hpp:491
bool incapacitated() const
Check if the unit has been petrified.
Definition: unit.hpp:920
int level() const
The current level of this unit.
Definition: unit.hpp:575
const t_string & type_name() const
Gets the translatable name of this unit's type.
Definition: unit.hpp:369
bool unrenamable() const
Whether this unit can be renamed.
Definition: unit.hpp:436
int recall_cost() const
How much gold it costs to recall this unit, or -1 if the side's default recall cost is used.
Definition: unit.hpp:656
void rename(const std::string &name)
Attempts to rename this unit's translatable display name, taking the 'unrenamable' flag into account.
Definition: unit.hpp:424
int hitpoints() const
The current number of hitpoints this unit has.
Definition: unit.hpp:515
bool slowed() const
Check if the unit has been slowed.
Definition: unit.hpp:929
const unit_race * race() const
Gets this unit's race.
Definition: unit.hpp:509
int experience() const
The current number of experience points this unit has.
Definition: unit.hpp:539
t_string block_dismiss_message() const
A message of why this unit cannot be dismissed.
Definition: unit.hpp:460
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
Definition: unit.hpp:628
const std::string & id() const
Gets this unit's id.
Definition: unit.hpp:380
int side() const
The side this unit belongs to.
Definition: unit.hpp:343
bool dismissable() const
Whether this unit can be dismissed.
Definition: unit.hpp:454
bool poisoned() const
Check if the unit has been poisoned.
Definition: unit.hpp:911
unsigned int experience_to_advance() const
The number of experience points this unit needs to level up, or 0 if current XP > max XP.
Definition: unit.hpp:557
int max_experience() const
The max number of experience points this unit can have.
Definition: unit.hpp:545
unit_race::GENDER gender() const
The gender of this unit.
Definition: unit.hpp:481
const t_string & name() const
Gets this unit's translatable display name.
Definition: unit.hpp:403
bool can_advance() const
Checks whether this unit has any options to advance to.
Definition: unit.hpp:272
color_t xp_color() const
Color for this unit's XP.
Definition: unit.cpp:1223
color_t hp_color() const
Color for this unit's current hitpoints.
Definition: unit.cpp:1179
std::string image_mods() const
Gets an IPF string containing all IPF image mods.
Definition: unit.cpp:2735
const std::vector< std::string > & overlays() const
Get the unit's overlay images.
Definition: unit.hpp:1686
std::string absolute_image() const
The name of the file to game_display (used in menus).
Definition: unit.cpp:2554
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1413
int movement_left() const
Gets how far a unit can move, considering the incapacitated flag.
Definition: unit.hpp:1338
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1322
const std::vector< t_string > & trait_names() const
Gets the names of the currently registered traits.
Definition: unit.hpp:1107
bool loyal() const
Gets whether this unit is loyal - ie, it costs no upkeep.
Definition: unit.cpp:1710
This file contains the window object, this object is a top level container which has the event manage...
T end(const std::pair< T, T > &p)
Standard logging facilities (interface).
#define log_scope2(domain, description)
Definition: log.hpp:276
const color_t GRAY_COLOR
const std::string unicode_en_dash
Definition: constants.cpp:43
REGISTER_DIALOG(editor_edit_unit)
@ NOTIFY_MODIFIED
Definition: handler.hpp:158
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:189
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:163
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
std::map< std::string, t_string > widget_item
Definition: widget.hpp:33
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup)
Shows a transient message to the user.
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Definition: message.cpp:148
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
@ CANCEL
Dialog was closed with the CANCEL button.
Definition: retval.hpp:38
void show_help(const std::string &show_topic)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:140
std::string span_color(const color_t &color, Args &&... data)
Applies Pango markup to the input specifying its display color.
Definition: markup.hpp:110
game_board * gameboard
Definition: resources.cpp:20
play_controller * controller
Definition: resources.cpp:21
auto make_ci_matcher(std::string_view filter_text)
Returns a function which performs locale-aware case-insensitive search.
Definition: ci_searcher.hpp:24
std::string format_level_string(const int level, bool recallable)
Definition: helper.cpp:135
std::string format_movement_string(const int moves_left, const int moves_max, bool active)
Definition: helper.cpp:154
std::string maybe_inactive(const std::string &str, bool active)
Definition: helper.cpp:96
std::string format_cost_string(int unit_recall_cost, bool active)
Definition: helper.cpp:101
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
constexpr auto filter
Definition: ranges.hpp:38
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:61
Applies the planned unit map for the duration of the struct's life.
Definition: manager.hpp:253
static map_location::direction n
#define LOG_DP
static lg::log_domain log_display("display")