initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / new-map-dialog.cpp
1 //  Copyright (C) 2007 Ole Laursen
2 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU Library General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
17 //  02110-1301, USA.
18
19 #include <config.h>
20
21 #include <assert.h>
22 #include <sigc++/functors/mem_fun.h>
23 #include <gtkmm.h>
24
25 #include "new-map-dialog.h"
26
27 #include "glade-helpers.h"
28 #include "defs.h"
29 #include "File.h"
30 #include "tileset.h"
31 #include "tilesetlist.h"
32 #include "armysetlist.h"
33 #include "citysetlist.h"
34 #include "shieldsetlist.h"
35 #include "ucompose.hpp"
36 #include "GameMap.h"
37
38
39 NewMapDialog::NewMapDialog()
40 {
41     map_set = false;
42     
43     Glib::RefPtr<Gtk::Builder> xml
44         = Gtk::Builder::create_from_file(get_glade_path() + "/new-map-dialog.ui");
45
46     xml->get_widget("dialog", dialog);
47
48     xml->get_widget("map_size_combobox", map_size_combobox);
49
50     xml->get_widget("custom_size_table", custom_size_table);
51     xml->get_widget("width_spinbutton", width_spinbutton);
52     xml->get_widget("height_spinbutton", height_spinbutton);
53     xml->get_widget("random_map_container", random_map_container);
54     xml->get_widget("grass_scale", grass_scale);
55     xml->get_widget("water_scale", water_scale);
56     xml->get_widget("swamp_scale", swamp_scale);
57     xml->get_widget("forest_scale", forest_scale);
58     xml->get_widget("hills_scale", hills_scale);
59     xml->get_widget("mountains_scale", mountains_scale);
60     xml->get_widget("cities_scale", cities_scale);
61     xml->get_widget("ruins_scale", ruins_scale);
62     xml->get_widget("temples_scale", temples_scale);
63     xml->get_widget("signposts_scale", signposts_scale);
64     xml->get_widget("accept_button", accept_button);
65
66     // fill in tile themes combobox
67     
68     guint32 counter = 0;
69     guint32 default_id = 0;
70     Gtk::Box *box;
71
72     //fill in tile sizes combobox
73     tile_size_combobox = manage(new Gtk::ComboBoxText);
74     std::list<guint32> sizes;
75     Tilesetlist::getInstance()->getSizes(sizes);
76     Citysetlist::getInstance()->getSizes(sizes);
77     Armysetlist::getInstance()->getSizes(sizes);
78     for (std::list<guint32>::iterator it = sizes.begin(); it != sizes.end();
79          it++)
80       {
81         Glib::ustring s = String::ucompose("%1x%1", *it);
82         tile_size_combobox->append_text(s);
83         if ((*it) == Tileset::getDefaultTileSize())
84           default_id = counter;
85         counter++;
86       }
87     tile_size_combobox->set_active(default_id);
88     xml->get_widget("tile_size_box", box);
89     box->pack_start(*tile_size_combobox, Gtk::PACK_SHRINK);
90     tile_size_combobox->signal_changed().connect
91       (sigc::mem_fun(*this, &NewMapDialog::on_tile_size_changed));
92
93     // make new tile themes combobox
94     tile_theme_combobox = manage(new Gtk::ComboBoxText);
95     xml->get_widget("tile_theme_box", box);
96     box->pack_start(*tile_theme_combobox, Gtk::PACK_SHRINK);
97
98     // make new army themes combobox
99     army_theme_combobox = manage(new Gtk::ComboBoxText);
100     xml->get_widget("army_theme_box", box);
101     box->pack_start(*army_theme_combobox, Gtk::PACK_SHRINK);
102
103     // make new city themes combobox
104     city_theme_combobox = manage(new Gtk::ComboBoxText);
105     xml->get_widget("city_theme_box", box);
106     box->pack_start(*city_theme_combobox, Gtk::PACK_SHRINK);
107
108     counter = 0;
109     default_id = 0;
110     shield_theme_combobox = manage(new Gtk::ComboBoxText);
111     Shieldsetlist *sl = Shieldsetlist::getInstance();
112     std::list<std::string> shield_themes = sl->getNames();
113     for (std::list<std::string>::iterator i = shield_themes.begin(),
114          end = shield_themes.end(); i != end; ++i)
115       {
116         if (*i == _("Default"))
117           default_id = counter;
118         shield_theme_combobox->append_text(Glib::filename_to_utf8(*i));
119         counter++;
120       }
121
122     shield_theme_combobox->set_active(default_id);
123
124     xml->get_widget("shield_theme_box", box);
125     box->pack_start(*shield_theme_combobox, Gtk::PACK_SHRINK);
126
127     on_tile_size_changed();
128
129     // create fill style combobox
130     fill_style_combobox = manage(new Gtk::ComboBoxText);
131
132     add_fill_style(Tile::GRASS);
133     add_fill_style(Tile::WATER);
134     add_fill_style(Tile::FOREST);
135     add_fill_style(Tile::HILLS);
136     add_fill_style(Tile::MOUNTAIN);
137     add_fill_style(Tile::SWAMP);
138
139     fill_style_combobox->append_text(_("Random"));
140     fill_style.push_back(-1);
141
142     Gtk::Alignment *alignment;
143     xml->get_widget("fill_style_alignment", alignment);
144     alignment->add(*fill_style_combobox);
145
146     fill_style_combobox->signal_changed().connect(
147                                                   sigc::mem_fun(*this, &NewMapDialog::on_fill_style_changed));
148     fill_style_combobox->set_active(6);
149
150     // map size
151     map_size_combobox->set_active(MAP_SIZE_NORMAL);
152     map_size_combobox->signal_changed().connect(
153                                                 sigc::mem_fun(*this, &NewMapDialog::on_map_size_changed));
154     grass_scale->set_value(78);
155     water_scale->set_value(7);
156     swamp_scale->set_value(2);
157     forest_scale->set_value(3);
158     hills_scale->set_value(5);
159     signposts_scale->set_value(20);
160     mountains_scale->set_value(5);
161     on_map_size_changed();
162         
163     width_spinbutton->set_value(MAP_SIZE_TINY_WIDTH);
164     height_spinbutton->set_value(MAP_SIZE_TINY_HEIGHT);
165 }
166
167 NewMapDialog::~NewMapDialog()
168 {
169   delete dialog;
170 }
171
172 void NewMapDialog::set_parent_window(Gtk::Window &parent)
173 {
174   dialog->set_transient_for(parent);
175 }
176
177 void NewMapDialog::run()
178 {
179   dialog->show_all();
180   int response = dialog->run();
181   if (response == Gtk::RESPONSE_ACCEPT) // accepted
182     {
183       switch (map_size_combobox->get_active_row_number()) {
184       case MAP_SIZE_SMALL:
185         map.width = MAP_SIZE_SMALL_WIDTH;
186         map.height = MAP_SIZE_SMALL_HEIGHT;
187         break;
188
189       case MAP_SIZE_TINY:
190         map.width = MAP_SIZE_TINY_WIDTH;
191         map.height = MAP_SIZE_TINY_HEIGHT;
192         break;
193
194       case MAP_SIZE_NORMAL:
195       default:
196         map.width = MAP_SIZE_NORMAL_WIDTH;
197         map.height = MAP_SIZE_NORMAL_HEIGHT;
198         break;
199
200       case MAP_SIZE_CUSTOM:
201         map.width = int(width_spinbutton->get_value());
202         map.height = int(height_spinbutton->get_value());
203         break;
204       }
205
206       int row = fill_style_combobox->get_active_row_number();
207       assert(row >= 0 && row < int(fill_style.size()));
208       map.fill_style = fill_style[row];
209
210       map.tileset = Tilesetlist::getInstance()->getTilesetDir
211         (Glib::filename_from_utf8(tile_theme_combobox->get_active_text()),
212          get_active_tile_size());
213
214       map.shieldset = Shieldsetlist::getInstance()->getShieldsetDir
215         (Glib::filename_from_utf8(shield_theme_combobox->get_active_text()));
216
217       map.cityset = Citysetlist::getInstance()->getCitysetDir
218         (Glib::filename_from_utf8(city_theme_combobox->get_active_text()),
219          get_active_tile_size());
220
221       map.armyset = Armysetlist::getInstance()->getArmysetDir
222         (Glib::filename_from_utf8(army_theme_combobox->get_active_text()),
223          get_active_tile_size());
224
225       if (map.fill_style == -1)
226         {
227           map.grass = int(grass_scale->get_value());
228           map.water = int(water_scale->get_value());
229           map.swamp = int(swamp_scale->get_value());
230           map.forest = int(forest_scale->get_value());
231           map.hills = int(hills_scale->get_value());
232           map.mountains = int(mountains_scale->get_value());
233           map.cities = int(cities_scale->get_value());
234           map.ruins = int(ruins_scale->get_value());
235           map.temples = int(temples_scale->get_value());
236           map.signposts = int(signposts_scale->get_value());
237         }
238
239       map_set = true;
240     }
241   else
242     map_set = false;
243 }
244
245 void NewMapDialog::on_fill_style_changed()
246 {
247   int row = fill_style_combobox->get_active_row_number();
248   assert(row >= 0 && row < int(fill_style.size()));
249   bool random_selected = fill_style[row] == -1;
250   random_map_container->set_sensitive(random_selected);
251 }
252
253 void NewMapDialog::on_map_size_changed()
254 {
255   switch (map_size_combobox->get_active_row_number()) {
256   case MAP_SIZE_SMALL:
257     cities_scale->set_value(15);
258     ruins_scale->set_value(20);
259     temples_scale->set_value(20);
260     custom_size_table->set_sensitive(false);
261     break;
262
263   case MAP_SIZE_TINY:
264     cities_scale->set_value(10);
265     ruins_scale->set_value(15);
266     temples_scale->set_value(15);
267     custom_size_table->set_sensitive(false);
268     break;
269
270   case MAP_SIZE_NORMAL:
271   default:
272     cities_scale->set_value(20);
273     ruins_scale->set_value(25);
274     temples_scale->set_value(25);
275     custom_size_table->set_sensitive(false);
276     break;
277   case MAP_SIZE_CUSTOM:
278     cities_scale->set_value(20);
279     ruins_scale->set_value(25);
280     temples_scale->set_value(25);
281     custom_size_table->set_sensitive(true);
282     break;
283   }
284 }
285
286 void NewMapDialog::add_fill_style(Tile::Type tile_type)
287 {
288   Tileset *tileset = GameMap::getInstance()->getTileset();
289   Tile *tile = (*tileset)[tileset->getIndex(tile_type)];
290   fill_style_combobox->append_text(tile->getName());
291   fill_style.push_back(tile_type);
292 }
293
294 guint32 NewMapDialog::get_active_tile_size()
295 {
296   return (guint32) atoi(tile_size_combobox->get_active_text().c_str());
297 }
298
299 void NewMapDialog::on_tile_size_changed()
300 {
301   guint32 default_id = 0;
302   guint32 counter = 0;
303
304   tile_theme_combobox->clear_items();
305   Tilesetlist *tl = Tilesetlist::getInstance();
306   std::list<std::string> tile_themes = tl->getNames(get_active_tile_size());
307   for (std::list<std::string>::iterator i = tile_themes.begin(),
308        end = tile_themes.end(); i != end; ++i)
309     {
310       if (*i == _("Default"))
311         default_id = counter;
312       tile_theme_combobox->append_text(Glib::filename_to_utf8(*i));
313       counter++;
314     }
315
316   tile_theme_combobox->set_active(default_id);
317   if (tile_theme_combobox->get_children().size() == 0)
318     accept_button->set_sensitive(false);
319
320   army_theme_combobox->clear_items();
321   Armysetlist *al = Armysetlist::getInstance();
322   std::list<std::string> army_themes = al->getNames(get_active_tile_size());
323   counter = 0;
324   default_id = 0;
325   for (std::list<std::string>::iterator i = army_themes.begin(),
326        end = army_themes.end(); i != end; ++i)
327     {
328       if (*i == _("Default"))
329         default_id = counter;
330       army_theme_combobox->append_text(Glib::filename_to_utf8(*i));
331       counter++;
332     }
333
334   army_theme_combobox->set_active(default_id);
335   if (army_theme_combobox->get_children().size() == 0)
336     accept_button->set_sensitive(false);
337
338   city_theme_combobox->clear_items();
339   Citysetlist *cl = Citysetlist::getInstance();
340   std::list<std::string> city_themes = cl->getNames(get_active_tile_size());
341   counter = 0;
342   default_id = 0;
343   for (std::list<std::string>::iterator i = city_themes.begin(),
344        end = city_themes.end(); i != end; ++i)
345     {
346       if (*i == _("Default"))
347         default_id = counter;
348       city_theme_combobox->append_text(Glib::filename_to_utf8(*i));
349       counter++;
350     }
351
352   city_theme_combobox->set_active(default_id);
353   if (city_theme_combobox->get_children().size() == 0)
354     accept_button->set_sensitive(false);
355 }
356