The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
unit_recruit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 by 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 #include "gui/dialogs/helper.hpp"
19 #include "gui/widgets/button.hpp"
20 #include "gui/widgets/image.hpp"
21 #include "gui/widgets/label.hpp"
22 #ifdef GUI2_EXPERIMENTAL_LISTBOX
23 #include "gui/widgets/list.hpp"
24 #else
25 #include "gui/widgets/listbox.hpp"
26 #endif
28 #include "gui/widgets/settings.hpp"
29 #include "gui/widgets/window.hpp"
30 #include "gettext.hpp"
31 #include "help/help.hpp"
32 #include "marked-up_text.hpp"
33 #include "play_controller.hpp"
34 #include "resources.hpp"
35 #include "team.hpp"
36 #include "units/types.hpp"
37 #include "whiteboard/manager.hpp"
38 
39 #include "utils/functional.hpp"
40 
41 namespace gui2
42 {
43 
44 REGISTER_DIALOG(unit_recruit)
45 
46 tunit_recruit::tunit_recruit(std::vector<const unit_type*>& recruit_list, team& team)
47  : recruit_list_(recruit_list)
48  , team_(team)
49  , selected_index_(0)
50 {
51 }
52 
53 static std::string can_afford_unit(const std::string& text, const bool can_afford)
54 {
55  return can_afford ? text : "<span color='#ff0000'>" + text + "</span>";
56 }
57 
59 {
60  tlistbox& list = find_widget<tlistbox>(&window, "recruit_list", false);
61 
62 #ifdef GUI2_EXPERIMENTAL_LISTBOX
65  *this,
66  std::ref(window)));
67 #else
69  dialog_callback<tunit_recruit, &tunit_recruit::list_item_clicked>);
70 #endif
71 
73  find_widget<tbutton>(&window, "show_help", false),
74  std::bind(&tunit_recruit::show_help, this, std::ref(window)));
75 
76  for(const auto& recruit : recruit_list_)
77  {
78  std::map<std::string, string_map> row_data;
80 
81  std::string image_string = recruit->image() + "~RC(" + recruit->flag_rgb() + ">"
83 
84  int wb_gold = 0;
86  if(const boost::shared_ptr<wb::manager>& whiteb = resources::controller->get_whiteboard()) {
87  wb::future_map future; // So gold takes into account planned spending
88  wb_gold = whiteb->get_spent_gold_for(team_.side());
89  }
90  }
91 
92  const bool can_afford = recruit->cost() <= team_.gold() - wb_gold;
93 
94  const std::string cost_string = std::to_string(recruit->cost());
95 
96  column["label"] = image_string;
97  row_data.insert(std::make_pair("unit_image", column));
98 
99  column["label"] = can_afford_unit(recruit->type_name(), can_afford);
100  column["use_markup"] = "true";
101  row_data.insert(std::make_pair("unit_type", column));
102 
103  column["label"] = can_afford_unit(cost_string, can_afford);
104  column["use_markup"] = "true";
105  row_data.insert(std::make_pair("unit_cost", column));
106 
107  list.add_row(row_data);
108  }
109 
110  list_item_clicked(window);
111 }
112 
114 {
115  const int selected_row
116  = find_widget<tlistbox>(&window, "recruit_list", false).get_selected_row();
117 
118  if(selected_row == -1) {
119  return;
120  }
121 
122  find_widget<tunit_preview_pane>(&window, "recruit_details", false)
123  .set_displayed_type(recruit_list_[selected_row]);
124 }
125 
127 {
128  help::show_help(window.video(), "recruit_and_recall");
129 }
130 
132 {
133  if(get_retval() == twindow::OK) {
134  selected_index_ = find_widget<tlistbox>(&window, "recruit_list", false)
135  .get_selected_row();
136  }
137 }
138 
139 } // namespace gui2
play_controller * controller
Definition: resources.cpp:21
void show_help(CVideo &video, const std::string &show_topic, int xloc, int yloc)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:117
void post_show(twindow &window)
Actions to be taken after the window has been shown.
int get_retval() const
Definition: dialog.hpp:161
CVideo & video()
Definition: window.hpp:411
std::vector< const unit_type * > & recruit_list_
void connect_signal_notify_modified(tdispatcher &dispatcher, const tsignal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.hpp:725
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(label_settings)
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
STL namespace.
-file sdl_utils.hpp
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
static std::string can_afford_unit(const std::string &text, const bool can_afford)
Dialog is closed with ok button.
Definition: window.hpp:125
This file contains the settings handling of the widget library.
void 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:74
Applies the planned unit map for the duration of the struct's life.
Definition: manager.hpp:249
void pre_show(twindow &window)
Inherited from tdialog.
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
void show_help(twindow &window)
The listbox class.
Definition: listbox.hpp:39
int side() const
Definition: team.hpp:193
int gold() const
Definition: team.hpp:194
GLenum GLint ref
Definition: glew.h:1813
void set_callback_value_change(const std::function< void(twidget &)> &callback)
Definition: listbox.hpp:225
static std::string get_side_color_index(int side)
Definition: team.cpp:840
void list_item_clicked(twindow &window)
GLsizei const GLcharARB ** string
Definition: glew.h:4503