initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / army-gains-level-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 <gtkmm.h>
22 #include <sigc++/functors/mem_fun.h>
23
24 #include "army-gains-level-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "image-helpers.h"
28 #include "ucompose.hpp"
29 #include "defs.h"
30 #include "army.h"
31 #include "GraphicsCache.h"
32 #include "hero.h"
33
34 //give a hero some more abilities
35 ArmyGainsLevelDialog::ArmyGainsLevelDialog(Hero *a, bool show_sight_stat)
36 {
37     GraphicsCache *gc = GraphicsCache::getInstance();
38     hero = a;
39     
40     Glib::RefPtr<Gtk::Builder> xml
41         = Gtk::Builder::create_from_file(get_glade_path()
42                                     + "/army-gains-level-dialog.ui");
43
44     xml->get_widget("dialog", dialog);
45     decorate(dialog);
46     window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
47     
48     Gtk::Image *image;
49     xml->get_widget("image", image);
50     image->property_pixbuf() = gc->getArmyPic(hero)->to_pixbuf();
51     Gtk::Image *hero_image;
52     xml->get_widget("hero_image", hero_image);
53     hero_image->property_pixbuf() = 
54       gc->getNewLevelPic(Playerlist::getActiveplayer(),
55                          dynamic_cast<Hero*>(a)->getGender())->to_pixbuf();
56     
57     Gtk::Label *label;
58     xml->get_widget("label", label);
59     Glib::ustring s;
60     s = String::ucompose(_("%1 has advanced to level %2!"), a->getName(),
61                          a->getLevel() + 1);
62     set_title(s);
63     s += "\n\n";
64     s += _("Choose an attribute to improve:");
65     label->set_text(s);
66
67     xml->get_widget("stats_vbox", stats_vbox);
68
69     add_item(Army::MOVES, _("Moves: %1"));
70     if (show_sight_stat == true)
71       add_item(Army::SIGHT, _("Sight: %1"));
72     if (a->getStat(Army::STRENGTH, false) < MAX_ARMY_STRENGTH)
73       add_item(Army::STRENGTH, _("Strength: %1"));
74
75     stat_items[0].radio->set_active(true);
76     on_stat_toggled();
77 }
78
79 ArmyGainsLevelDialog::~ArmyGainsLevelDialog()
80 {
81   delete dialog;
82 }
83
84 void ArmyGainsLevelDialog::set_parent_window(Gtk::Window &parent)
85 {
86     dialog->set_transient_for(parent);
87     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
88 }
89
90 void ArmyGainsLevelDialog::hide()
91 {
92   dialog->hide();
93 }
94
95 void ArmyGainsLevelDialog::run()
96 {
97     dialog->show_all();
98     dialog->run();
99 }
100
101 void ArmyGainsLevelDialog::add_item(Army::Stat stat, Glib::ustring desc)
102 {
103     StatItem item;
104     item.stat = stat;
105     item.desc = desc;
106     if (stat_items.empty())
107         item.radio = manage(new Gtk::RadioButton);
108     else
109     {
110         Gtk::RadioButton::Group group = stat_items[0].radio->get_group();
111         item.radio = manage(new Gtk::RadioButton(group));
112     }
113
114     stat_items.push_back(item);
115
116     item.radio->signal_toggled().connect(
117         sigc::mem_fun(this, &ArmyGainsLevelDialog::on_stat_toggled));
118
119     stats_vbox->pack_start(*item.radio, Gtk::PACK_SHRINK);
120 }
121
122 void ArmyGainsLevelDialog::on_stat_toggled()
123 {
124     for (unsigned int i = 0; i < stat_items.size(); ++i)
125         if (stat_items[i].radio->get_active())
126         {
127             selected_stat = stat_items[i].stat;
128             break;
129         }
130             
131     fill_in_descriptions();
132 }
133
134 void ArmyGainsLevelDialog::fill_in_descriptions()
135 {
136     for (unsigned int i = 0; i < stat_items.size(); ++i)
137     {
138         StatItem &item = stat_items[i];
139
140         int v = hero->getStat(item.stat, false);
141
142         if (item.radio->get_active())
143             v += hero->computeLevelGain(item.stat);
144             
145         item.radio->set_label(String::ucompose(item.desc, v));
146     }
147 }