gui/dialogs/unit_create.cpp

Go to the documentation of this file.
00001 /* $Id: unit_create.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Ignacio R. Morelle <shadowm2006@gmail.com>
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_create.hpp"
00019 
00020 #include "gui/auxiliary/log.hpp"
00021 #include "gui/dialogs/helper.hpp"
00022 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00023 #include "gui/widgets/list.hpp"
00024 #else
00025 #include "gui/widgets/listbox.hpp"
00026 #endif
00027 #include "gui/widgets/settings.hpp"
00028 #include "gui/widgets/toggle_button.hpp"
00029 #include "gui/widgets/window.hpp"
00030 #include "foreach.hpp"
00031 #include "unit_types.hpp"
00032 
00033 namespace {
00034     static std::string last_chosen_type_id = "";
00035     static unit_race::GENDER last_gender = unit_race::MALE;
00036 
00037     /**
00038      * Helper function for updating the male/female checkboxes.
00039      * It's not a private member of class gui2::tunit_create so
00040      * we don't have to expose a forward-declaration of ttoggle_button
00041      * in the interface.
00042      */
00043     void update_male_female_toggles(gui2::ttoggle_button& male, gui2::ttoggle_button& female, unit_race::GENDER choice)
00044     {
00045         male.set_value(choice == unit_race::MALE);
00046         female.set_value(choice == unit_race::FEMALE);
00047     }
00048 }
00049 
00050 namespace gui2 {
00051 
00052 /*WIKI
00053  * @page = GUIWindowDefinitionWML
00054  * @order = 2_unit_create
00055  *
00056  * == Unit create ==
00057  *
00058  * This shows the debug-mode dialog to create new units on the map.
00059  *
00060  * @begin{table}{dialog_widgets}
00061  *
00062  * male_toggle & & toggle_button & m &
00063  *         Option button to select the "male" gender for created units. $
00064  *
00065  * female_toggle & & toggle_button & m &
00066  *         Option button to select the "female" gender for created units. $
00067  *
00068  * unit_type_list & & listbox & m &
00069  *         Listbox displaying existing unit types sorted by name and race. $
00070  *
00071  * -unit_type & & control & m &
00072  *         Widget which shows the unit type name label. $
00073  *
00074  * -race & & control & m &
00075  *         Widget which shows the unit race name label. $
00076  *
00077  * @end{table}
00078  */
00079 
00080 REGISTER_DIALOG(unit_create)
00081 
00082 tunit_create::tunit_create()
00083     : gender_(last_gender)
00084     , choice_(last_chosen_type_id)
00085     , type_ids_()
00086 {
00087 }
00088 
00089 void tunit_create::pre_show(CVideo& /*video*/, twindow& window)
00090 {
00091     ttoggle_button& male_toggle = find_widget<ttoggle_button>(
00092             &window, "male_toggle", false);
00093     ttoggle_button& female_toggle = find_widget<ttoggle_button>(
00094             &window, "female_toggle", false);
00095     tlistbox& list = find_widget<tlistbox>(&window, "unit_type_list", false);
00096 
00097     male_toggle.set_callback_state_change(
00098         dialog_callback<tunit_create, &tunit_create::gender_toggle_callback>
00099     );
00100     female_toggle.set_callback_state_change(
00101         dialog_callback<tunit_create, &tunit_create::gender_toggle_callback>
00102     );
00103     update_male_female_toggles(male_toggle, female_toggle, gender_);
00104     list.clear();
00105 
00106     // We use this container to "map" unit_type ids to list subscripts
00107     // later, so it ought to be empty before proceeding.
00108     type_ids_.clear();
00109 
00110     std::vector< std::string > type_labels, race_labels;
00111 
00112     foreach (const unit_type_data::unit_type_map::value_type &i, unit_types.types())
00113     {
00114         unit_types.find(i.first, unit_type::HELP_INDEX);
00115 
00116         // And so we map an unit_type id to a list subscript. Ugh.
00117         type_ids_.push_back(i.first);
00118 
00119         std::string race_label;
00120 
00121         if (const unit_race *r = unit_types.find_race(i.second.race())) {
00122             race_label = r->plural_name();
00123         }
00124 
00125         std::map< std::string, string_map > row_data;
00126         string_map column;
00127 
00128         column["label"] = i.second.type_name();
00129         row_data.insert(std::make_pair("unit_type", column));
00130         column["label"] = race_label;
00131         row_data.insert(std::make_pair("race", column));
00132 
00133         list.add_row(row_data);
00134 
00135         // Select the previous choice, if any.
00136         if(choice_.empty() != true && choice_ == i.first) {
00137             list.select_row(list.get_item_count() - 1);
00138         }
00139     }
00140 
00141     if(type_ids_.empty()) {
00142         ERR_GUI_G << "no unit types found for unit create dialog; not good\n";
00143     }
00144 }
00145 
00146 void tunit_create::post_show(twindow& window)
00147 {
00148     ttoggle_button& female_toggle = find_widget<ttoggle_button>(
00149             &window, "female_toggle", false);
00150     tlistbox& list = find_widget<tlistbox>(&window, "unit_type_list", false);
00151 
00152     choice_ = "";
00153 
00154     if(get_retval() != twindow::OK) {
00155         return;
00156     }
00157 
00158     const int selected_row = list.get_selected_row();
00159     if(selected_row < 0) {
00160         return;
00161     }
00162     else if(static_cast<size_t>(selected_row) >= type_ids_.size()) {
00163         // FIXME: maybe assert?
00164         ERR_GUI_G << "unit create dialog has more list items than known unit types; not good\n";
00165         return;
00166     }
00167 
00168     last_chosen_type_id = choice_ =
00169         type_ids_[static_cast<size_t>(selected_row)];
00170     last_gender = gender_ =
00171         female_toggle.get_value() ? unit_race::FEMALE : unit_race::MALE;
00172 }
00173 
00174 void tunit_create::gender_toggle_callback(twindow& window)
00175 {
00176     ttoggle_button& male_toggle = find_widget<ttoggle_button>(
00177             &window, "male_toggle", false);
00178     ttoggle_button& female_toggle = find_widget<ttoggle_button>(
00179             &window, "female_toggle", false);
00180 
00181     // Ye olde ugly hack for the lack of radio buttons.
00182 
00183     if(gender_ == unit_race::MALE) {
00184         gender_ = female_toggle.get_value() ? unit_race::FEMALE : unit_race::MALE;
00185     }
00186     else {
00187         gender_ = male_toggle.get_value() ? unit_race::MALE : unit_race::FEMALE;
00188     }
00189 
00190     update_male_female_toggles(male_toggle, female_toggle, gender_);
00191 }
00192 
00193 }
 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