Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00039
00040
00041
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
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
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& , 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
00107
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
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
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
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
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 }