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