initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / switch-sets-dialog.cpp
1 //  Copyright (C) 2009 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #include <config.h>
19
20 #include <assert.h>
21 #include <sigc++/functors/mem_fun.h>
22 #include <gtkmm.h>
23
24 #include "switch-sets-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "defs.h"
28 #include "File.h"
29 #include "tileset.h"
30 #include "tilesetlist.h"
31 #include "armysetlist.h"
32 #include "citysetlist.h"
33 #include "shieldsetlist.h"
34 #include "ucompose.hpp"
35 #include "GameMap.h"
36 #include "ruinlist.h"
37 #include "citylist.h"
38 #include "armyset.h"
39 #include "shieldset.h"
40 #include "cityset.h"
41
42
43 SwitchSetsDialog::SwitchSetsDialog()
44 {
45     Glib::RefPtr<Gtk::Builder> xml
46         = Gtk::Builder::create_from_file(get_glade_path() + "/switch-sets-dialog.ui");
47
48     xml->get_widget("dialog", dialog);
49
50     xml->get_widget("accept_button", accept_button);
51
52     // fill in tile themes combobox
53     
54     guint32 counter = 0;
55     guint32 default_id = 0;
56     Gtk::Box *box;
57
58     //fill in tile sizes combobox
59     tile_size_combobox = manage(new Gtk::ComboBoxText);
60     std::list<guint32> sizes;
61     Tilesetlist::getInstance()->getSizes(sizes);
62     Citysetlist::getInstance()->getSizes(sizes);
63     Armysetlist::getInstance()->getSizes(sizes);
64     for (std::list<guint32>::iterator it = sizes.begin(); it != sizes.end();
65          it++)
66       {
67         Glib::ustring s = String::ucompose("%1x%1", *it);
68         tile_size_combobox->append_text(s);
69         if ((*it) == GameMap::getInstance()->getTileset()->getTileSize())
70           default_id = counter;
71         counter++;
72       }
73     tile_size_combobox->set_active(default_id);
74     xml->get_widget("tile_size_box", box);
75     box->pack_start(*tile_size_combobox, Gtk::PACK_SHRINK);
76     tile_size_combobox->signal_changed().connect
77       (sigc::mem_fun(*this, &SwitchSetsDialog::on_tile_size_changed));
78
79     // make new tile themes combobox
80     tile_theme_combobox = manage(new Gtk::ComboBoxText);
81     xml->get_widget("tile_theme_box", box);
82     box->pack_start(*tile_theme_combobox, Gtk::PACK_SHRINK);
83
84     // make new army themes combobox
85     army_theme_combobox = manage(new Gtk::ComboBoxText);
86     xml->get_widget("army_theme_box", box);
87     box->pack_start(*army_theme_combobox, Gtk::PACK_SHRINK);
88
89     // make new city themes combobox
90     city_theme_combobox = manage(new Gtk::ComboBoxText);
91     xml->get_widget("city_theme_box", box);
92     box->pack_start(*city_theme_combobox, Gtk::PACK_SHRINK);
93
94     counter = 0;
95     default_id = 0;
96     shield_theme_combobox = manage(new Gtk::ComboBoxText);
97     Shieldsetlist *sl = Shieldsetlist::getInstance();
98     std::list<std::string> shield_themes = sl->getNames();
99     for (std::list<std::string>::iterator i = shield_themes.begin(),
100          end = shield_themes.end(); i != end; ++i)
101       {
102         if (*i == GameMap::getInstance()->getShieldset()->getName())
103           default_id = counter;
104         shield_theme_combobox->append_text(Glib::filename_to_utf8(*i));
105         counter++;
106       }
107
108     shield_theme_combobox->set_active(default_id);
109
110     xml->get_widget("shield_theme_box", box);
111     box->pack_start(*shield_theme_combobox, Gtk::PACK_SHRINK);
112
113     on_tile_size_changed();
114
115 }
116
117 SwitchSetsDialog::~SwitchSetsDialog()
118 {
119   delete dialog;
120 }
121
122 void SwitchSetsDialog::set_parent_window(Gtk::Window &parent)
123 {
124   dialog->set_transient_for(parent);
125 }
126
127 guint32 SwitchSetsDialog::get_active_tile_size()
128 {
129   return (guint32) atoi(tile_size_combobox->get_active_text().c_str());
130 }
131
132 void SwitchSetsDialog::on_tile_size_changed()
133 {
134   guint32 default_id = 0;
135   guint32 counter = 0;
136
137   tile_theme_combobox->clear_items();
138   Tilesetlist *tl = Tilesetlist::getInstance();
139   std::list<std::string> tile_themes = tl->getNames(get_active_tile_size());
140   for (std::list<std::string>::iterator i = tile_themes.begin(),
141        end = tile_themes.end(); i != end; ++i)
142     {
143       if (*i == GameMap::getInstance()->getTileset()->getName())
144         default_id = counter;
145       tile_theme_combobox->append_text(Glib::filename_to_utf8(*i));
146       counter++;
147     }
148
149   tile_theme_combobox->set_active(default_id);
150   if (tile_theme_combobox->get_children().size() == 0)
151     accept_button->set_sensitive(false);
152
153   army_theme_combobox->clear_items();
154   Armysetlist *al = Armysetlist::getInstance();
155   std::list<std::string> army_themes = al->getNames(get_active_tile_size());
156   counter = 0;
157   default_id = 0;
158   int armyset = Playerlist::getActiveplayer()->getArmyset();
159   for (std::list<std::string>::iterator i = army_themes.begin(),
160        end = army_themes.end(); i != end; ++i)
161     {
162       if (*i == Armysetlist::getInstance()->getArmyset(armyset)->getName())
163         default_id = counter;
164       army_theme_combobox->append_text(Glib::filename_to_utf8(*i));
165       counter++;
166     }
167
168   army_theme_combobox->set_active(default_id);
169   if (army_theme_combobox->get_children().size() == 0)
170     accept_button->set_sensitive(false);
171
172   city_theme_combobox->clear_items();
173   Citysetlist *cl = Citysetlist::getInstance();
174   std::list<std::string> city_themes = cl->getNames(get_active_tile_size());
175   counter = 0;
176   default_id = 0;
177   Cityset *active = GameMap::getInstance()->getCityset();
178   for (std::list<std::string>::iterator i = city_themes.begin(),
179        end = city_themes.end(); i != end; ++i)
180     {
181       if (*i == active->getName())
182         default_id = counter;
183       //only append it if the tile widths are identical.
184       Cityset *cityset = 
185         cl->getCityset(cl->getCitysetDir(*i, get_active_tile_size()));
186       if (active->tileWidthsEqual(cityset) == true)
187         city_theme_combobox->append_text(Glib::filename_to_utf8(*i));
188       counter++;
189     }
190
191   city_theme_combobox->set_active(default_id);
192   if (city_theme_combobox->get_children().size() == 0)
193     accept_button->set_sensitive(false);
194 }
195
196 int SwitchSetsDialog::run()
197 {
198   dialog->show_all();
199   int response = dialog->run();
200   if (response != Gtk::RESPONSE_ACCEPT) // accepted
201     return response;
202   std::string subdir;
203   subdir = Tilesetlist::getInstance()->getTilesetDir
204     (Glib::filename_from_utf8(tile_theme_combobox->get_active_text()),
205      get_active_tile_size());
206   selected_tileset = Tilesetlist::getInstance()->getTileset(subdir);
207
208   subdir = Shieldsetlist::getInstance()->getShieldsetDir
209     (Glib::filename_from_utf8(shield_theme_combobox->get_active_text()));
210   selected_shieldset = Shieldsetlist::getInstance()->getShieldset(subdir);
211
212   subdir = Citysetlist::getInstance()->getCitysetDir
213     (Glib::filename_from_utf8(city_theme_combobox->get_active_text()),
214      get_active_tile_size());
215   selected_cityset = Citysetlist::getInstance()->getCityset(subdir);
216
217   subdir = Armysetlist::getInstance()->getArmysetDir
218     (Glib::filename_from_utf8(army_theme_combobox->get_active_text()),
219      get_active_tile_size());
220   selected_armyset = Armysetlist::getInstance()->getArmyset(subdir);
221
222   GameMap::getInstance()->switchArmysets(selected_armyset);
223   if (selected_cityset != GameMap::getInstance()->getCityset())
224     GameMap::getInstance()->switchCityset(selected_cityset);
225   if (selected_shieldset != GameMap::getInstance()->getShieldset())
226     GameMap::getInstance()->switchShieldset(selected_shieldset);
227   if (selected_tileset != GameMap::getInstance()->getTileset())
228     GameMap::getInstance()->switchTileset(selected_tileset);
229   return response;
230 }