gui/dialogs/unit_attack.cpp

Go to the documentation of this file.
00001 /* $Id: unit_attack.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2010 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "gui/dialogs/unit_attack.hpp"
00019 
00020 #include "gui/widgets/image.hpp"
00021 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00022 #include "gui/widgets/list.hpp"
00023 #else
00024 #include "gui/widgets/listbox.hpp"
00025 #endif
00026 #include "gui/widgets/settings.hpp"
00027 #include "gui/widgets/window.hpp"
00028 #include "unit.hpp"
00029 
00030 namespace gui2 {
00031 
00032 /*WIKI
00033  * @page = GUIWindowDefinitionWML
00034  * @order = 2_unit_attack
00035  *
00036  * == Unit attack ==
00037  *
00038  * This shows the dialog for attacking units.
00039  *
00040  * @begin{table}{dialog_widgets}
00041  * attacker_portrait & & image   & o & Shows the portrait of the attacking unit. $
00042  * attacker_icon     & & image   & o & Shows the icon of the attacking unit. $
00043  * attacker_name     & & control & o & Shows the name of the attacking unit. $
00044  *
00045  * defender_portrait & & image   & o & Shows the portrait of the defending unit. $
00046  * defender_icon     & & image   & o & Shows the icon of the defending unit. $
00047  * defender_name     & & control & o & Shows the name of the defending unit. $
00048  *
00049  *
00050  * weapon_list       & & listbox & m & The list with weapons to choos from. $
00051  * -attacker_weapon  & & control & o & The weapon for the attacker to use. $
00052  * -defender_weapon  & & control & o & The weapon for the defender to use. $
00053  *
00054  * @end{table}
00055  */
00056 
00057 REGISTER_DIALOG(unit_attack)
00058 
00059 tunit_attack::tunit_attack(
00060           const unit_map::iterator& attacker_itor
00061         , const unit_map::iterator& defender_itor
00062         , const std::vector<battle_context>& weapons
00063         , const int best_weapon)
00064     : selected_weapon_(-1)
00065     , attacker_itor_(attacker_itor)
00066     , defender_itor_(defender_itor)
00067     , weapons_(weapons)
00068     , best_weapon_(best_weapon)
00069 {
00070 }
00071 
00072 template<class T>
00073 static void set_label(
00074           twindow& window
00075         , const std::string& id
00076         , const std::string& label)
00077 {
00078     T* widget = find_widget<T>(&window, id, false, false);
00079     if(widget) {
00080         widget->set_label(label);
00081     }
00082 }
00083 
00084 static void set_attacker_info(twindow& w, unit& u)
00085 {
00086     set_label<timage>(w, "attacker_portrait", u.absolute_image());
00087     set_label<timage>(w, "attacker_icon", u.absolute_image());
00088     set_label<tcontrol>(w, "attacker_name", u.name());
00089 }
00090 
00091 static void set_defender_info(twindow& w, unit& u)
00092 {
00093     set_label<timage>(w, "defender_portrait", u.absolute_image());
00094     set_label<timage>(w, "defender_icon", u.absolute_image());
00095     set_label<tcontrol>(w, "defender_name", u.name());
00096 }
00097 
00098 static void set_weapon_info(twindow& window
00099         , const std::vector<battle_context>& weapons
00100         , const int best_weapon)
00101 {
00102     tlistbox& weapon_list =
00103             find_widget<tlistbox>(&window, "weapon_list", false);
00104     window.keyboard_capture(&weapon_list);
00105 
00106     const config empty;
00107     attack_type no_weapon(empty);
00108 
00109     foreach(const battle_context& weapon, weapons) {
00110         const battle_context_unit_stats& attacker =
00111                 weapon.get_attacker_stats();
00112 
00113         const battle_context_unit_stats& defender =
00114                 weapon.get_defender_stats();
00115 
00116         const attack_type& attacker_weapon = attack_type(*attacker.weapon);
00117         const attack_type& defender_weapon = attack_type(
00118                 defender.weapon ? *defender.weapon : no_weapon);
00119 
00120         std::map<std::string, string_map> data;
00121         string_map item;
00122 
00123         item["label"] = attacker_weapon.name();
00124         data.insert(std::make_pair("attacker_weapon", item));
00125 
00126         item["label"] = defender_weapon.name();
00127         data.insert(std::make_pair("defender_weapon", item));
00128 
00129         weapon_list.add_row(data);
00130 
00131     }
00132 
00133     assert(best_weapon < static_cast<int>(weapon_list.get_item_count()));
00134     weapon_list.select_row(best_weapon);
00135 }
00136 
00137 void tunit_attack::pre_show(CVideo& /*video*/, twindow& window)
00138 {
00139     set_attacker_info(window, *attacker_itor_);
00140     set_defender_info(window, *defender_itor_);
00141 
00142     selected_weapon_ = -1;
00143     set_weapon_info(window, weapons_, best_weapon_);
00144 }
00145 
00146 void tunit_attack::post_show(twindow& window)
00147 {
00148     if(get_retval() == twindow::OK) {
00149         selected_weapon_ = find_widget<tlistbox>(&window, "weapon_list", false)
00150                 .get_selected_row();
00151     }
00152 }
00153 
00154 } // namespace gui2
00155 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:59 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs