The Battle for Wesnoth  1.15.12+dev
unit_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #define GETTEXT_DOMAIN "wesnoth-lib"
15 
17 
19 #include "gui/core/log.hpp"
20 #include "gui/dialogs/message.hpp"
21 #include "gui/widgets/listbox.hpp"
22 #include "gui/widgets/settings.hpp"
23 #include "gui/widgets/button.hpp"
24 #include "gui/widgets/image.hpp"
25 #include "gui/widgets/label.hpp"
28 #include "gui/widgets/window.hpp"
29 #include "display.hpp"
30 #include "font/text_formatting.hpp"
31 #include "formatter.hpp"
32 #include "units/map.hpp"
33 #include "units/ptr.hpp"
34 #include "units/unit.hpp"
35 
36 #include <functional>
37 
38 static lg::log_domain log_display("display");
39 #define LOG_DP LOG_STREAM(info, log_display)
40 
41 namespace gui2::dialogs
42 {
43 
44 REGISTER_DIALOG(unit_list)
45 
47  : unit_list_(unit_list)
48  , scroll_to_(scroll_to)
49 {
50 }
51 
52 static std::string format_level_string(const int level)
53 {
54  std::string lvl = std::to_string(level);
55 
56  if(level < 1) {
57  return "<span color='#969696'>" + lvl + "</span>";
58  } else if(level == 1) {
59  return lvl;
60  } else if(level == 2) {
61  return "<b>" + lvl + "</b>";
62  } else { // level must be > 2
63  return"<b><span color='#ffffff'>" + lvl + "</span></b>";
64  }
65 
66 }
67 
68 static std::string format_if_leader(unit_const_ptr u, const std::string& str)
69 {
70  return (*u).can_recruit() ? "<span color='#cdad00'>" + str + "</span>" : str;
71 }
72 
74 {
75  const int moves_left = (*u).movement_left();
76  const int moves_max = (*u).total_movement();
77 
78  std::string color = "#00ff00";
79 
80  if(moves_left == 0) {
81  color = "#ff0000";
82  } else if(moves_left < moves_max) {
83  color = "#ffff00";
84  }
85 
86  return formatter() << "<span color='" << color << "'>" << moves_left << "/" << moves_max << "</span>";
87 }
88 
90 {
91  listbox& list = find_widget<listbox>(&window, "units_list", false);
92 
94 
95  list.clear();
96 
97  window.keyboard_capture(&list);
98 
99  for(const unit_const_ptr& unit : unit_list_) {
100  std::map<std::string, string_map> row_data;
101  string_map column;
102 
103  column["use_markup"] = "true";
104 
105  column["label"] = format_if_leader(unit, unit->type_name());
106  row_data.emplace("unit_type", column);
107 
108  const std::string& name = !unit->name().empty() ? format_if_leader(unit, unit->name().str()) : font::unicode_en_dash;
109  column["label"] = name;
110  row_data.emplace("unit_name", column);
111 
112  column["label"] = format_movement_string(unit);
113  row_data.emplace("unit_moves", column);
114 
115  std::stringstream hp_str;
116  hp_str << font::span_color(unit->hp_color()) << unit->hitpoints() << "/" << unit->max_hitpoints() << "</span>";
117 
118  column["label"] = hp_str.str();
119  row_data.emplace("unit_hp", column);
120 
121  column["label"] = format_level_string(unit->level());
122  row_data.emplace("unit_level", column);
123 
124  std::stringstream exp_str;
125  exp_str << font::span_color(unit->xp_color());
126  if(unit->can_advance()) {
127  exp_str << unit->experience() << "/" << unit->max_experience();
128  } else {
129  exp_str << font::unicode_en_dash;
130  }
131  exp_str << "</span>";
132 
133  column["label"] = exp_str.str();
134  row_data.emplace("unit_experience", column);
135 
136  column["label"] = utils::join(unit->trait_names(), ", ");
137  row_data.emplace("unit_traits", column);
138 
139  grid* row_grid = &list.add_row(row_data);
140 
141  // NOTE: this needs to be done *after* the row is added
142  // TODO: show custom statuses
144  find_widget<image>(row_grid, "unit_status_petrified", false).set_visible(widget::visibility::invisible);
145  }
146 
148  find_widget<image>(row_grid, "unit_status_poisoned", false).set_visible(widget::visibility::invisible);
149  }
150 
152  find_widget<image>(row_grid, "unit_status_slowed", false).set_visible(widget::visibility::invisible);
153  }
154 
155  if(!unit->invisible(unit->get_location(), false)) {
156  find_widget<image>(row_grid, "unit_status_invisible", false).set_visible(widget::visibility::invisible);
157  }
158  }
159 
160  list.register_translatable_sorting_option(0, [this](const int i) { return unit_list_[i]->type_name().str(); });
161  list.register_translatable_sorting_option(1, [this](const int i) { return unit_list_[i]->name().str(); });
162  list.register_sorting_option(2, [this](const int i) { return unit_list_[i]->movement_left(); });
163  list.register_sorting_option(3, [this](const int i) { return unit_list_[i]->hitpoints(); });
164  list.register_sorting_option(4, [this](const int i) {
165  const unit& u = *unit_list_[i];
166  return std::tuple(u.level(), -static_cast<int>(u.experience_to_advance()));
167  });
168  list.register_sorting_option(5, [this](const int i) { return unit_list_[i]->experience(); });
169  list.register_translatable_sorting_option(6, [this](const int i) {
170  return !unit_list_[i]->trait_names().empty() ? unit_list_[i]->trait_names().front().str() : ""; });
171 
173 }
174 
176 {
177  const int selected_row
178  = find_widget<listbox>(get_window(), "units_list", false).get_selected_row();
179 
180  if(selected_row == -1) {
181  return;
182  }
183 
184  find_widget<unit_preview_pane>(get_window(), "unit_details", false)
185  .set_displayed_unit(*unit_list_[selected_row].get());
186 }
187 
189 {
190  if(get_retval() == retval::OK) {
191  const int selected_row = find_widget<listbox>(&window, "units_list", false).get_selected_row();
192 
193  scroll_to_ = unit_list_[selected_row]->get_location();
194  }
195 }
196 
198 {
199  std::vector<unit_const_ptr> unit_list;
200  map_location scroll_to;
201 
202  const unit_map& units = gui.get_units();
203  for(unit_map::const_iterator i = units.begin(); i != units.end(); ++i) {
204  if(i->side() != gui.viewing_side()) {
205  continue;
206  }
207 
208  unit_list.push_back(i.get_shared_ptr());
209  }
210 
211  if(unit_list::execute(unit_list, scroll_to)) {
212  gui.scroll_to_tile(scroll_to, display::WARP);
213  gui.select_hex(scroll_to);
214  }
215 }
216 
217 } // namespace dialogs
Define the common log macros for the gui toolkit.
virtual void select_hex(map_location hex)
Definition: display.cpp:1619
void list_item_clicked()
Callbacks.
Definition: unit_list.cpp:175
unit_iterator end()
Definition: map.hpp:428
This class represents a single unit of a specific type.
Definition: unit.hpp:120
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Definition: unit_list.cpp:188
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:1274
int hitpoints() const
The current number of hitpoints this unit has.
Definition: unit.hpp:492
std::vector< unit_const_ptr > & unit_list_
Definition: unit_list.hpp:48
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:197
General purpose widgets.
unit_iterator begin()
Definition: map.hpp:418
The unit is slowed - it moves slower and does less damage.
Definition: unit.hpp:854
const unit_map & get_units() const
Definition: display.hpp:124
int viewing_side() const
Definition: display.hpp:106
The unit is poisoned - it loses health each turn.
Definition: unit.hpp:855
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:636
STL namespace.
static std::string format_movement_string(unit_const_ptr u)
Definition: unit_list.cpp:73
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:89
map_location & scroll_to_
Definition: unit_list.hpp:50
color_t hp_color() const
Color for this unit&#39;s current hitpoints.
Definition: unit.cpp:1045
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:534
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:26
The listbox class.
Definition: listbox.hpp:42
static std::string format_level_string(const int level)
Definition: unit_list.cpp:52
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:30
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:186
This file contains the settings handling of the widget library.
std::ostringstream wrapper.
Definition: formatter.hpp:38
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:126
color_t xp_color() const
Color for this unit&#39;s XP.
Definition: unit.cpp:1099
const t_string & name() const
Gets this unit&#39;s translatable display name.
Definition: unit.hpp:393
static std::string format_if_leader(unit_const_ptr u, const std::string &str)
Definition: unit_list.cpp:68
int max_experience() const
The max number of experience points this unit can have.
Definition: unit.hpp:522
int level() const
The current level of this unit.
Definition: unit.hpp:552
map_display and display: classes which take care of displaying the map and game-data on the screen...
int moves_left
Definition: pathfind.cpp:156
const std::vector< t_string > & trait_names() const
Gets the names of the currently registered traits.
Definition: unit.hpp:1074
const t_string & type_name() const
Gets the translatable name of this unit&#39;s type.
Definition: unit.hpp:359
Encapsulates the map of the game.
Definition: location.hpp:37
bool invisible(const map_location &loc, bool see_all=true) const
Definition: unit.cpp:2380
static lg::log_domain log_display("display")
std::size_t i
Definition: function.cpp:940
const std::string unicode_en_dash
Definition: constants.cpp:42
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:2202
int max_hitpoints() const
The max number of hitpoints this unit can have.
Definition: unit.hpp:498
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:67
static bool execute(std::vector< unit_const_ptr > &units, map_location &scroll_to)
Definition: unit_list.hpp:37
int get_retval() const
Returns the cached window exit code.
bool empty() const
Definition: tstring.hpp:186
int experience() const
The current number of experience points this unit has.
Definition: unit.hpp:516
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1348
bool can_advance() const
Checks whether this unit has any options to advance to.
Definition: unit.hpp:262
Container associating units to locations.
Definition: map.hpp:97
Dialog was closed with the OK button.
Definition: retval.hpp:34
void register_sorting_option(const int col, const Func &f)
Definition: listbox.hpp:271
const std::string & str() const
Definition: tstring.hpp:190
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
unit_list(std::vector< unit_const_ptr > &unit_list, map_location &scroll_to)
Definition: unit_list.cpp:46