The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
unit_preview_pane.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 The Battle for Wesnoth Project http://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 
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/image.hpp"
23 #include "gui/widgets/label.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "gui/widgets/window.hpp"
26 
27 #include "formula/string_utils.hpp"
28 #include "game_config.hpp"
29 #include "gettext.hpp"
30 #include "help/help.hpp"
31 #include "marked-up_text.hpp"
32 #include "play_controller.hpp"
33 #include "resources.hpp"
34 #include "team.hpp"
35 #include "units/types.hpp"
36 
37 #include "utils/functional.hpp"
38 
39 namespace gui2
40 {
41 
42 // ------------ WIDGET -----------{
43 
44 REGISTER_WIDGET(unit_preview_pane)
45 
46 void tunit_preview_pane::finalize_setup()
47 {
48  // Icons
49  icon_type_ = find_widget<timage>(this, "type_image" , false, false);
50  icon_race_ = find_widget<timage>(this, "type_race" , false, false);
51  icon_alignment_ = find_widget<timage>(this, "type_alignment", false, false);
52 
53  // Labels
54  label_name_ = find_widget<tlabel>(this, "type_name" , false, false);
55  label_level_ = find_widget<tlabel>(this, "type_level" , false, false);
56  label_details_ = find_widget<tlabel>(this, "type_details", false, false);
57 
58  // Profile button
59  button_profile_ = find_widget<tbutton>(this, "type_profile", false, false);
60 
61  if(button_profile_) {
62  connect_signal_mouse_left_click(*button_profile_,
64  }
65 }
66 
68 {
69  // Sets the current type id for the profile button callback to use
70  current_type_ = type->id();
71 
72  if(icon_type_) {
74 
76  tc = "~RC(" + type->flag_rgb() + ">" +
78  + ")";
79  }
80 
81  icon_type_->set_label((type->icon().empty() ? type->image() : type->icon()) + tc);
82  }
83 
84  if(label_name_) {
85  label_name_->set_label("<big>" + type->type_name() + "</big>");
87  }
88 
89  if(label_level_) {
90  utils::string_map symbols;
91  symbols["lvl"] = std::to_string(type->level());
92 
93  std::string l_str = vgettext("Lvl $lvl", symbols);
94 
95  label_level_->set_label("<b>" + l_str + "</b>");
97  }
98 
99  if(icon_race_) {
100  icon_race_->set_label("icons/unit-groups/race_" + type->race_id() + "_30.png");
101  icon_race_->set_tooltip(type->race()->name(type->genders().front()));
102  }
103 
104  if(icon_alignment_) {
105  const std::string& alignment_name = type->alignment().to_string();
106 
107  icon_alignment_->set_label("icons/alignments/alignment_" + alignment_name + "_30.png");
109  type->alignment(),
110  type->genders().front()));
111  }
112 
113  if(label_details_) {
114  std::stringstream str;
115  str << "<b>" << _("HP: ") << "</b>"
116  << "<span color='#21e100'>" << type->hitpoints() << "</span> ";
117 
118  str << "<b>" << _("XP: ") << "</b>"
119  << "<span color='#00a0e1'>" << type->experience_needed() << "</span> ";
120 
121  str << "<b>" << _("MP: ") << "</b>"
122  << type->movement() << "\n";
123 
124  str << " \n";
125 
126  // Print trait details
127  bool has_traits = false;
128  std::stringstream t_str;
129 
130  for(const auto& tr : type->possible_traits())
131  {
132  if(tr["availability"] != "musthave") continue;
133 
134  const std::string gender_string =
135  type->genders().front() == unit_race::FEMALE ? "female_name" : "male_name";
136 
138  if(name.empty()) {
139  name = tr["name"];
140  }
141 
142  if(!name.empty()) {
143  t_str << " " << name << "\n";
144  }
145 
146  has_traits = true;
147  }
148 
149  if(has_traits) {
150  str << "<b>" << _("Traits") << "</b>" << "\n";
151  str << t_str.str();
152  str << " \n";
153  }
154 
155  // Print ability details
156  if(!type->abilities().empty()) {
157  str << "<b>" << _("Abilities") << "</b>" << "\n";
158 
159  for(const auto& ab : type->abilities())
160  {
161  str << " " << ab << "\n";
162  }
163 
164  str << " \n";
165  }
166 
167  // Print attack details
168  if(!type->attacks().empty()) {
169  str << "<b>" << _("Attacks") << "</b>" << "\n";
170 
171  for(const auto& a : type->attacks())
172  {
173  str << "<span color='#f5e6c1'>" << a.damage()
174  << font::weapon_numbers_sep << a.num_attacks() << " " << a.name() << "</span>" << "\n";
175 
176  str << "<span color='#a69275'>" << " " << a.range()
177  << font::weapon_details_sep << a.type() << "</span>" << "\n";
178 
179  const std::string special = a.weapon_specials();
180  if (!special.empty()) {
181  str << "<span color='#a69275'>" << " " << special << "</span>" << "\n";
182  }
183 
184  const std::string accuracy_parry = a.accuracy_parry_description();
185  if(!accuracy_parry.empty()) {
186  str << "<span color='#a69275'>" << " " << accuracy_parry << "</span>" << "\n";
187  }
188  }
189  }
190 
191  label_details_->set_label(str.str());
193  }
194 }
195 
197 {
198  if(get_window()) {
200  }
201 }
202 
203 void tunit_preview_pane::set_active(const bool /*active*/)
204 {
205  /* DO NOTHING */
206 }
207 
209 {
210  return true;
211 }
212 
214 {
215  return ENABLED;
216 }
217 
219 {
220  static const std::string type = "unit_preview_pane";
221  return type;
222 }
223 
224 void tunit_preview_pane::set_self_active(const bool /*active*/)
225 {
226  /* DO NOTHING */
227 }
228 
229 // }---------- DEFINITION ---------{
230 
232  : tcontrol_definition(cfg)
233 {
234  DBG_GUI_P << "Parsing unit preview pane " << id << '\n';
235 
236  load_resolutions<tresolution>(cfg);
237 }
238 
240  : tresolution_definition_(cfg), grid()
241 {
242  state.push_back(tstate_definition(cfg.child("background")));
243  state.push_back(tstate_definition(cfg.child("foreground")));
244 
245  const config& child = cfg.child("grid");
246  VALIDATE(child, _("No grid defined."));
247 
248  grid = new tbuilder_grid(child);
249 }
250 
251 // }---------- BUILDER -----------{
252 
253 namespace implementation
254 {
255 
256 tbuilder_unit_preview_pane::tbuilder_unit_preview_pane(const config& cfg)
257  : tbuilder_control(cfg)
258 {
259 }
260 
262 {
263  tunit_preview_pane* widget = new tunit_preview_pane();
264 
265  init_control(widget);
266 
267  DBG_GUI_G << "Window builder: placed unit preview pane '" << id
268  << "' with definition '" << definition << "'.\n";
269 
271  = boost::dynamic_pointer_cast<
273 
274  assert(conf);
275 
276  widget->init_grid(conf->grid);
277  widget->finalize_setup();
278 
279  return widget;
280 }
281 
282 } // namespace implementation
283 
284 // }------------ END --------------
285 
286 } // namespace gui2
play_controller * controller
Definition: resources.cpp:21
#define DBG_GUI_P
Definition: log.hpp:69
std::string const & gender_string(unit_race::GENDER gender)
Definition: race.cpp:151
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1806
const t_string & type_name() const
The name of the unit in the current language setting.
Definition: types.hpp:112
const std::vector< t_string > & abilities() const
Definition: types.hpp:166
const std::string weapon_details_sep
int movement() const
Definition: types.hpp:128
const t_string & name(GENDER gender=MALE) const
Definition: race.hpp:34
virtual unsigned get_state() const override
See tcontrol::get_state.
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
tresolution_definition_ptr config()
Definition: control.hpp:299
This file contains the window object, this object is a top level container which has the event manage...
virtual void set_label(const t_string &label)
Definition: control.cpp:330
const unit_race * race() const
Never returns nullptr, but may point to the null race.
Definition: types.hpp:218
const std::string & image() const
Definition: types.hpp:138
void connect_signal_mouse_left_click(tdispatcher &dispatcher, const tsignal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.hpp:710
void set_displayed_type(const unit_type *type)
Displays the stats of a specified unit type.
-file sdl_utils.hpp
int level() const
Definition: types.hpp:126
void finalize_setup()
Initializes the interneral sub-widget pointers.
Base class of a resolution, contains the common keys for a resolution.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void init_control(tcontrol *control) const
Definition: control.cpp:675
std::string definition
Parameters for the control.
Definition: control.hpp:530
virtual bool get_active() const override
See tcontrol::get_active.
virtual void set_use_markup(bool use_markup)
Definition: control.cpp:342
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
const std::string & flag_rgb() const
Definition: types.hpp:147
std::map< std::string, t_string > string_map
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
const std::vector< unit_race::GENDER > & genders() const
The returned vector will not be empty, provided this has been built to the HELP_INDEXED status...
Definition: types.hpp:198
void init_grid(const boost::intrusive_ptr< tbuilder_grid > &grid_builder)
Initializes and builds the grid.
Definition: container.cpp:209
const std::string & icon() const
Definition: types.hpp:139
Contains the state info for a resolution.
std::string race_id() const
Returns the ID of this type's race without the need to build the type.
Definition: types.hpp:215
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
virtual void set_active(const bool active) override
See tcontrol::set_active.
void profile_button_callback()
Callback for the profile button.
config::const_child_itors possible_traits() const
Definition: types.hpp:182
virtual void set_self_active(const bool active) override
See tcontainer_::set_self_active.
std::string vgettext(const char *msgid, const utils::string_map &symbols)
const GLfloat * tc
Definition: glew.h:12749
GLuint const GLchar * name
Definition: glew.h:1782
int hitpoints() const
Definition: types.hpp:123
int experience_needed(bool with_acceleration=true) const
Definition: types.cpp:514
std::vector< tstate_definition > state
const std::string weapon_numbers_sep
config & child(const std::string &key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:658
std::vector< attack_type > attacks() const
Definition: types.cpp:484
Base class for all widgets.
Definition: widget.hpp:49
static std::string get_side_color_index(int side)
Definition: team.cpp:840
void show_unit_help(CVideo &video, const std::string &show_topic, bool has_variations, bool hidden, int xloc, int yloc)
Open the help browser, show unit with id unit_id.
Definition: help.cpp:127
twindow * get_window()
Get the parent window.
Definition: widget.cpp:116
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
static std::string alignment_description(ALIGNMENT align, unit_race::GENDER gender=unit_race::MALE)
#define DBG_GUI_G
Definition: log.hpp:41
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool empty() const
Definition: tstring.hpp:166
const std::string & id() const
The id for this unit_type.
Definition: types.hpp:115
Contains the implementation details for lexical_cast and shouldn't be used directly.