The Battle for Wesnoth  1.19.2+dev
unit_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
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 
20 #include "gui/widgets/listbox.hpp"
21 #include "gui/widgets/image.hpp"
23 #include "gui/widgets/window.hpp"
24 #include "display.hpp"
25 #include "font/text_formatting.hpp"
26 #include "formatter.hpp"
27 #include "units/map.hpp"
28 #include "units/ptr.hpp"
29 #include "units/unit.hpp"
30 
31 #include <functional>
32 
33 static lg::log_domain log_display("display");
34 #define LOG_DP LOG_STREAM(info, log_display)
35 
36 namespace gui2::dialogs
37 {
38 
39 REGISTER_DIALOG(unit_list)
40 
42  : modal_dialog(window_id())
43  , unit_list_(unit_list)
44  , scroll_to_(scroll_to)
45 {
46 }
47 
48 static std::string format_level_string(const int level)
49 {
50  std::string lvl = std::to_string(level);
51 
52  if(level < 1) {
53  return "<span color='#969696'>" + lvl + "</span>";
54  } else if(level == 1) {
55  return lvl;
56  } else if(level == 2) {
57  return "<b>" + lvl + "</b>";
58  } else { // level must be > 2
59  return"<b><span color='#ffffff'>" + lvl + "</span></b>";
60  }
61 
62 }
63 
64 static std::string format_if_leader(unit_const_ptr u, const std::string& str)
65 {
66  return (*u).can_recruit() ? "<span color='#cdad00'>" + str + "</span>" : str;
67 }
68 
70 {
71  const int moves_left = (*u).movement_left();
72  const int moves_max = (*u).total_movement();
73 
74  std::string color = "#00ff00";
75 
76  if(moves_left == 0) {
77  color = "#ff0000";
78  } else if(moves_left < moves_max) {
79  color = "#ffff00";
80  }
81 
82  return formatter() << "<span color='" << color << "'>" << moves_left << "/" << moves_max << "</span>";
83 }
84 
86 {
87  listbox& list = find_widget<listbox>(&window, "units_list", false);
88 
90 
91  list.clear();
92 
93  window.keyboard_capture(&list);
94 
95  for(const unit_const_ptr& unit : unit_list_) {
96  widget_data row_data;
97  widget_item column;
98 
99  column["use_markup"] = "true";
100 
101  column["label"] = format_if_leader(unit, unit->type_name());
102  row_data.emplace("unit_type", column);
103 
104  const std::string& name = !unit->name().empty() ? format_if_leader(unit, unit->name().str()) : font::unicode_en_dash;
105  column["label"] = name;
106  row_data.emplace("unit_name", column);
107 
108  column["label"] = format_movement_string(unit);
109  row_data.emplace("unit_moves", column);
110 
111  std::stringstream hp_str;
112  hp_str << font::span_color(unit->hp_color()) << unit->hitpoints() << "/" << unit->max_hitpoints() << "</span>";
113 
114  column["label"] = hp_str.str();
115  row_data.emplace("unit_hp", column);
116 
117  column["label"] = format_level_string(unit->level());
118  row_data.emplace("unit_level", column);
119 
120  std::stringstream exp_str;
121  exp_str << font::span_color(unit->xp_color());
122  if(unit->can_advance()) {
123  exp_str << unit->experience() << "/" << unit->max_experience();
124  } else {
125  exp_str << font::unicode_en_dash;
126  }
127  exp_str << "</span>";
128 
129  column["label"] = exp_str.str();
130  row_data.emplace("unit_experience", column);
131 
132  column["label"] = utils::join(unit->trait_names(), ", ");
133  row_data.emplace("unit_traits", column);
134 
135  grid* row_grid = &list.add_row(row_data);
136 
137  // NOTE: this needs to be done *after* the row is added
138  // TODO: show custom statuses
140  find_widget<image>(row_grid, "unit_status_petrified", false).set_visible(widget::visibility::invisible);
141  }
142 
144  find_widget<image>(row_grid, "unit_status_poisoned", false).set_visible(widget::visibility::invisible);
145  }
146 
148  find_widget<image>(row_grid, "unit_status_slowed", false).set_visible(widget::visibility::invisible);
149  }
150 
151  if(!unit->invisible(unit->get_location(), false)) {
152  find_widget<image>(row_grid, "unit_status_invisible", false).set_visible(widget::visibility::invisible);
153  }
154  }
155 
156  list.register_translatable_sorting_option(0, [this](const int i) { return unit_list_[i]->type_name().str(); });
157  list.register_translatable_sorting_option(1, [this](const int i) { return unit_list_[i]->name().str(); });
158  list.register_sorting_option(2, [this](const int i) { return unit_list_[i]->movement_left(); });
159  list.register_sorting_option(3, [this](const int i) { return unit_list_[i]->hitpoints(); });
160  list.register_sorting_option(4, [this](const int i) {
161  const unit& u = *unit_list_[i];
162  return std::tuple(u.level(), -static_cast<int>(u.experience_to_advance()));
163  });
164  list.register_sorting_option(5, [this](const int i) { return unit_list_[i]->experience(); });
165  list.register_translatable_sorting_option(6, [this](const int i) {
166  return !unit_list_[i]->trait_names().empty() ? unit_list_[i]->trait_names().front().str() : ""; });
167 
169 }
170 
172 {
173  const int selected_row
174  = find_widget<listbox>(get_window(), "units_list", false).get_selected_row();
175 
176  if(selected_row == -1) {
177  return;
178  }
179 
180  find_widget<unit_preview_pane>(get_window(), "unit_details", false)
181  .set_displayed_unit(*unit_list_[selected_row].get());
182 }
183 
185 {
186  if(get_retval() == retval::OK) {
187  const int selected_row = find_widget<listbox>(&window, "units_list", false).get_selected_row();
188 
189  scroll_to_ = unit_list_[selected_row]->get_location();
190  }
191 }
192 
194 {
195  std::vector<unit_const_ptr> unit_list;
196  map_location scroll_to;
197 
198  const unit_map& units = gui.get_units();
199  for(unit_map::const_iterator i = units.begin(); i != units.end(); ++i) {
200  if(i->side() != gui.viewing_side()) {
201  continue;
202  }
203 
204  unit_list.push_back(i.get_shared_ptr());
205  }
206 
207  if(unit_list::execute(unit_list, scroll_to)) {
208  gui.scroll_to_tile(scroll_to, display::WARP);
209  gui.select_hex(scroll_to);
210  }
211 }
212 
213 } // namespace dialogs
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:88
std::ostringstream wrapper.
Definition: formatter.hpp:40
Abstract base class for all modal dialogs.
int get_retval() const
Returns the cached window exit code.
window * get_window()
Returns a pointer to the dialog's window.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: unit_list.cpp:85
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Definition: unit_list.cpp:184
map_location & scroll_to_
Definition: unit_list.hpp:50
std::vector< unit_const_ptr > & unit_list_
Definition: unit_list.hpp:48
void list_item_clicked()
Callbacks.
Definition: unit_list.cpp:171
static bool execute(std::vector< unit_const_ptr > &units, map_location &scroll_to)
Definition: unit_list.hpp:37
Base container class.
Definition: grid.hpp:32
The listbox class.
Definition: listbox.hpp:43
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:59
void register_translatable_sorting_option(const int col, translatable_sorter_func_t f)
Registers a special sorting function specifically for translatable values.
Definition: listbox.cpp:613
void register_sorting_option(const int col, const Func &f)
Definition: listbox.hpp:260
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:118
void set_visible(const visibility visible)
Definition: widget.cpp:469
@ invisible
The user set the widget invisible, that means:
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
void keyboard_capture(widget *widget)
Definition: window.cpp:1221
bool empty() const
Definition: tstring.hpp:186
const std::string & str() const
Definition: tstring.hpp:190
Container associating units to locations.
Definition: map.hpp:98
unit_iterator end()
Definition: map.hpp:428
unit_iterator begin()
Definition: map.hpp:418
This class represents a single unit of a specific type.
Definition: unit.hpp:133
map_display and display: classes which take care of displaying the map and game-data on the screen.
std::size_t i
Definition: function.cpp:968
bool invisible(const map_location &loc, bool see_all=true) const
Definition: unit.cpp:2636
int max_hitpoints() const
The max number of hitpoints this unit can have.
Definition: unit.hpp:505
int level() const
The current level of this unit.
Definition: unit.hpp:559
const t_string & type_name() const
Gets the translatable name of this unit's type.
Definition: unit.hpp:369
int hitpoints() const
The current number of hitpoints this unit has.
Definition: unit.hpp:499
bool get_state(const std::string &state) const
Check if the unit is affected by a status effect.
Definition: unit.cpp:1349
int experience() const
The current number of experience points this unit has.
Definition: unit.hpp:523
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:541
int max_experience() const
The max number of experience points this unit can have.
Definition: unit.hpp:529
const t_string & name() const
Gets this unit's translatable display name.
Definition: unit.hpp:403
@ STATE_SLOWED
Definition: unit.hpp:860
@ STATE_PETRIFIED
The unit is poisoned - it loses health each turn.
Definition: unit.hpp:862
@ STATE_POISONED
The unit is slowed - it moves slower and does less damage.
Definition: unit.hpp:861
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:1178
color_t hp_color() const
Color for this unit's current hitpoints.
Definition: unit.cpp:1124
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1389
const std::vector< t_string > & trait_names() const
Gets the names of the currently registered traits.
Definition: unit.hpp:1092
This file contains the window object, this object is a top level container which has the event manage...
CURSOR_TYPE get()
Definition: cursor.cpp:216
const std::string unicode_en_dash
Definition: constants.cpp:43
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
static std::string format_level_string(const int level)
Definition: unit_list.cpp:48
static std::string format_if_leader(unit_const_ptr u, const std::string &str)
Definition: unit_list.cpp:64
static std::string format_movement_string(unit_const_ptr u)
Definition: unit_list.cpp:69
REGISTER_DIALOG(editor_edit_unit)
void show_unit_list(display &gui)
Definition: unit_list.cpp:193
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
General purpose widgets.
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
int moves_left
Definition: pathfind.cpp:156
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
Encapsulates the map of the game.
Definition: location.hpp:38
static lg::log_domain log_display("display")