Converted Recruit dialog to GUI2

This commit is contained in:
Charles Dang 2016-03-09 19:52:25 +11:00
parent 2b46bb7e46
commit 42816df6f6
7 changed files with 441 additions and 33 deletions

View file

@ -0,0 +1,234 @@
#textdomain wesnoth-lib
###
### Definition of the window to recruit units
###
#define _GUI_RECRUIT_LIST
[listbox]
id = "recruit_list"
definition = "default"
[list_definition]
[row]
[column]
vertical_grow = "true"
horizontal_grow = "true"
[toggle_panel]
definition = "default"
return_value_id = "ok"
[grid]
[row]
[column]
grow_factor = 1
horizontal_grow = "true"
border = "all"
border_size = 5
[image]
id = "unit_image"
definition = "default"
linked_group = "image"
[/image]
[/column]
[column]
grow_factor = 0
horizontal_grow = "true"
[grid]
[row]
[column]
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
id = "unit_type"
definition = "default"
linked_group = "type"
[/label]
[/column]
[/row]
[row]
[column]
horizontal_alignment = "left"
[grid]
[row]
grow_factor = 1
[column]
border = "left,top,bottom"
border_size = 5
horizontal_alignment = "left"
[image]
id = "gold_icon"
definition = "default"
label = "themes/gold.png"
[/image]
[/column]
[column]
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
id = "unit_cost"
definition = "default"
[/label]
[/column]
[/row]
[/grid]
[/column]
[/row]
[/grid]
[/column]
[/row]
[/grid]
[/toggle_panel]
[/column]
[/row]
[/list_definition]
[/listbox]
#enddef
[window]
id = "unit_recruit"
description = "Unut recruit dialog."
[resolution]
definition = "default"
automatic_placement = "true"
vertical_placement = "center"
horizontal_placement = "center"
maximum_height = 600
[linked_group]
id = "image"
fixed_width = "true"
[/linked_group]
[linked_group]
id = "type"
fixed_width = "true"
[/linked_group]
[tooltip]
id = "tooltip_large"
[/tooltip]
[helptip]
id = "tooltip_large"
[/helptip]
[grid]
[row]
grow_factor = 0
[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
definition = "title"
label = _ "Recruit Unit"
[/label]
[/column]
[/row]
[row]
grow_factor = 1
[column]
horizontal_grow = "true"
[grid]
[row]
[column]
horizontal_grow = "true"
vertical_grow = "true"
border = "all"
border_size = 5
[unit_preview_pane]
definition = "minimal"
id = "recruit_details"
[/unit_preview_pane]
[/column]
[column]
horizontal_alignment = "right"
border = "all"
border_size = 5
{_GUI_RECRUIT_LIST}
[/column]
[/row]
[/grid]
[/column]
[/row]
[row]
grow_factor = 0
[column]
grow_factor = 0
horizontal_grow = "true"
[grid]
[row]
grow_factor=0
[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_alignment = "left"
[button]
id = "show_help"
definition = "default"
label = _ "Help"
[/button]
[/column]
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_alignment = "right"
[button]
id = "ok"
definition = "default"
label = _ "Recruit"
[/button]
[/column]
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_alignment = "right"
[button]
id = "cancel"
definition = "default"
label = _ "Cancel"
[/button]
[/column]
[/row]
[/grid]
[/column]
[/row]
[/grid]
[/resolution]
[/window]
#undef _GUI_RECRUIT_LIST
#undef _GUI_DETAILS_SECTION

View file

@ -737,6 +737,8 @@
<Unit filename="../../src/gui/dialogs/unit_attack.hpp" />
<Unit filename="../../src/gui/dialogs/unit_create.cpp" />
<Unit filename="../../src/gui/dialogs/unit_create.hpp" />
<Unit filename="../../src/gui/dialogs/unit_recruit.cpp" />
<Unit filename="../../src/gui/dialogs/unit_recruit.hpp" />
<Unit filename="../../src/gui/dialogs/wml_error.cpp" />
<Unit filename="../../src/gui/dialogs/wml_error.hpp" />
<Unit filename="../../src/gui/dialogs/wml_message.cpp" />

View file

@ -950,6 +950,7 @@ set(wesnoth-main_SRC
gui/dialogs/transient_message.cpp
gui/dialogs/unit_attack.cpp
gui/dialogs/unit_create.cpp
gui/dialogs/unit_recruit.cpp
gui/dialogs/wml_error.cpp
gui/dialogs/wml_message.cpp
halo.cpp

View file

@ -468,6 +468,7 @@ wesnoth_sources = Split("""
gui/dialogs/transient_message.cpp
gui/dialogs/unit_attack.cpp
gui/dialogs/unit_create.cpp
gui/dialogs/unit_recruit.cpp
gui/dialogs/wml_error.cpp
gui/dialogs/wml_message.cpp
gui/lib/types/point.cpp

View file

@ -0,0 +1,141 @@
/*
Copyright (C) 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/dialogs/helper.hpp"
#include "gui/dialogs/unit_recruit.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/image.hpp"
#include "gui/widgets/label.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/unit_preview_pane.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "gettext.hpp"
#include "help/help.hpp"
#include "marked-up_text.hpp"
#include "play_controller.hpp"
#include "resources.hpp"
#include "team.hpp"
#include "unit_types.hpp"
#include "whiteboard/manager.hpp"
#include "utils/foreach.tpp"
#include <boost/bind.hpp>
namespace gui2
{
REGISTER_DIALOG(unit_recruit)
tunit_recruit::tunit_recruit(std::vector<const unit_type*>& recruit_list, team& team)
: recruit_list_(recruit_list)
, team_(team)
, selected_index_(0)
{
}
static std::string can_afford_unit(const std::string& text, const bool can_afford)
{
return can_afford ? text : "<span color='#ff0000'>" + text + "</span>";
}
void tunit_recruit::pre_show(CVideo& /*video*/, twindow& window)
{
tlistbox& list = find_widget<tlistbox>(&window, "recruit_list", false);
#ifdef GUI2_EXPERIMENTAL_LISTBOX
connect_signal_notify_modified(*list,
boost::bind(&tunit_recruit::list_item_clicked,
*this,
boost::ref(window)));
#else
list.set_callback_value_change(
dialog_callback<tunit_recruit, &tunit_recruit::list_item_clicked>);
#endif
connect_signal_mouse_left_click(
find_widget<tbutton>(&window, "show_help", false),
boost::bind(&tunit_recruit::show_help, this, boost::ref(window)));
FOREACH(const AUTO& recruit, recruit_list_)
{
std::map<std::string, string_map> row_data;
string_map column;
std::string image_string = recruit->image() + "~RC(" + recruit->flag_rgb() + ">"
+ team::get_side_color_index(team_.side()) + ")";
int wb_gold = 0;
if(resources::controller) {
if(const boost::shared_ptr<wb::manager>& whiteb = resources::controller->get_whiteboard()) {
wb::future_map future; // So gold takes into account planned spending
wb_gold = whiteb->get_spent_gold_for(team_.side());
}
}
const bool can_afford = recruit->cost() < team_.gold() - wb_gold;
const std::string cost_string = lexical_cast<std::string>(recruit->cost());
column["label"] = image_string;
row_data.insert(std::make_pair("unit_image", column));
column["label"] = can_afford_unit(recruit->type_name(), can_afford);
column["use_markup"] = "true";
row_data.insert(std::make_pair("unit_type", column));
column["label"] = can_afford_unit(cost_string, can_afford);
column["use_markup"] = "true";
row_data.insert(std::make_pair("unit_cost", column));
list.add_row(row_data);
}
list_item_clicked(window);
}
void tunit_recruit::list_item_clicked(twindow& window)
{
const int selected_row
= find_widget<tlistbox>(&window, "recruit_list", false).get_selected_row();
if(selected_row == -1) {
return;
}
find_widget<tunit_preview_pane>(&window, "recruit_details", false)
.set_displayed_type(recruit_list_[selected_row]);
}
void tunit_recruit::show_help(twindow& window)
{
help::show_help(window.video(), "recruit_and_recall");
}
void tunit_recruit::post_show(twindow& window)
{
if(get_retval() == twindow::OK) {
selected_index_ = find_widget<tlistbox>(&window, "recruit_list", false)
.get_selected_row();
}
}
} // namespace gui2

View file

@ -0,0 +1,54 @@
/*
Copyright (C) 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef GUI_DIALOGS_UNIT_RECRUIT_HPP_INCLUDED
#define GUI_DIALOGS_UNIT_RECRUIT_HPP_INCLUDED
#include "gui/dialogs/dialog.hpp"
#include "team.hpp"
#include "unit_types.hpp"
namespace gui2 {
class tunit_recruit : public tdialog
{
public:
tunit_recruit(std::vector<const unit_type*>& recruit_list, team& team);
int get_selected_index() const
{
return selected_index_;
}
private:
/** Inherited from tdialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const;
/** Inherited from tdialog. */
void pre_show(CVideo& video, twindow& window);
void post_show(twindow& window);
void list_item_clicked(twindow& window);
void show_help(twindow& window);
std::vector<const unit_type*>& recruit_list_;
team& team_;
int selected_index_;
};
} // namespace gui2
#endif /* ! GUI_DIALOGS_UNIT_RECRUIT_HPP_INCLUDED */

View file

@ -52,6 +52,7 @@
#include "gui/dialogs/simple_item_selector.hpp"
#include "gui/dialogs/edit_text.hpp"
#include "gui/dialogs/unit_create.hpp"
#include "gui/dialogs/unit_recruit.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "help/help.hpp"
@ -522,59 +523,33 @@ bool menu_handler::has_friends() const
void menu_handler::recruit(int side_num, const map_location &last_hex)
{
team &current_team = teams()[side_num - 1];
std::vector<const unit_type*> sample_units;
gui_->draw(); //clear the old menu
std::vector<std::string> item_keys;
std::vector<std::string> items;
std::set<std::string> recruits = actions::get_recruits(side_num, last_hex);
for(std::set<std::string>::const_iterator it = recruits.begin(); it != recruits.end(); ++it) {
const unit_type *type = unit_types.find(*it);
const unit_type* type = unit_types.find(*it);
if (!type) {
ERR_NG << "could not find unit '" << *it << "'" << std::endl;
return;
}
item_keys.push_back(*it);
char prefix;
int wb_gold = 0;
if (const boost::shared_ptr<wb::manager> & whiteb = pc_.get_whiteboard())
{ wb::future_map future; // so gold takes into account planned spending
wb_gold = whiteb->get_spent_gold_for(side_num);
//display units that we can't afford to recruit in red
} // end planned unit map scope
prefix = (type->cost() > current_team.gold() - wb_gold
? font::BAD_TEXT : font::NULL_MARKUP);
std::stringstream description;
description << font::IMAGE << type->image();
#ifndef LOW_MEM
description << "~RC(" << type->flag_rgb() << '>'
<< team::get_side_color_index(side_num) << ')';
#endif
description << COLUMN_SEPARATOR << font::LARGE_TEXT << prefix << type->type_name() << "\n"
<< prefix << type->cost() << " " << translation::sngettext("unit^Gold", "Gold", type->cost());
items.push_back(description.str());
sample_units.push_back(type);
}
if(sample_units.empty()) {
gui2::show_transient_message(gui_->video(),"",_("You have no units available to recruit."));
gui2::show_transient_message(gui_->video(), "", _("You have no units available to recruit."));
return;
}
int recruit_res = dialogs::recruit_dialog(*gui_, sample_units, items, side_num, get_title_suffix(side_num));
gui2::tunit_recruit dlg(sample_units, teams()[side_num - 1]);
if(recruit_res != -1) {
do_recruit(item_keys[recruit_res], side_num, last_hex);
dlg.show(gui_->video());
if(dlg.get_retval() == gui2::twindow::OK) {
do_recruit(sample_units[dlg.get_selected_index()]->type_name(), side_num, last_hex);
}
}