initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / triumphs-dialog.cpp
1 //  Copyright (C) 2007, 2008, 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 <sigc++/functors/mem_fun.h>
21 #include <gtkmm.h>
22
23 #include "triumphs-dialog.h"
24
25 #include "glade-helpers.h"
26 #include "image-helpers.h"
27 #include "input-helpers.h"
28 #include "ucompose.hpp"
29 #include "defs.h"
30 #include "File.h"
31 #include "GameMap.h"
32 #include "GraphicsCache.h"
33 #include "armysetlist.h"
34 #include "playerlist.h"
35 #include "player.h"
36
37 TriumphsDialog::TriumphsDialog(Player *player)
38 {
39   d_player = player;
40   Glib::RefPtr<Gtk::Builder> xml
41     = Gtk::Builder::create_from_file(get_glade_path() + "/triumphs-dialog.ui");
42
43   xml->get_widget("dialog", dialog);
44   decorate(dialog);
45   window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
46
47   Gtk::HBox *contents;
48   xml->get_widget("outer_hbox", contents);
49   notebook = Gtk::manage(new Gtk::Notebook());
50   contents->pack_start(*notebook, true, true, 0);
51   fill_in_info();
52   //set the notebook to start off on the player's own page
53   notebook->set_current_page(d_player->getId());
54 }
55
56 TriumphsDialog::~TriumphsDialog()
57 {
58   delete dialog;
59 }
60 void TriumphsDialog::set_parent_window(Gtk::Window &parent)
61 {
62   dialog->set_transient_for(parent);
63   //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
64 }
65
66 void TriumphsDialog::hide()
67 {
68   dialog->hide();
69 }
70 void TriumphsDialog::run()
71 {
72   dialog->show_all();
73   dialog->run();
74 }
75
76 guint32 TriumphsDialog::tally(Player *p, Triumphs::TriumphType type)
77 {
78   Playerlist *pl = Playerlist::getInstance();
79   guint32 count = 0;
80   if (p == d_player)
81     {
82       // add up what the other players did to us
83       for (Playerlist::iterator it = pl->begin(); it != pl->end(); it++)
84         {
85           if ((*it) == Playerlist::getInstance()->getNeutral())
86             continue;
87           count += p->getTriumphs()->getTriumphTally(*it, type);
88         }
89     }
90   else
91     {
92       // add up what we did to that player
93       count = d_player->getTriumphs()->getTriumphTally(p, type);
94     }
95   return count;
96 }
97
98 void TriumphsDialog::fill_in_page(Player *p)
99 {
100   GraphicsCache *gc = GraphicsCache::getInstance();
101   //here we tally up the stats, make a vbox and append it as a new page
102   //tally it up differently when p == d_player
103         
104   guint32 count;
105   Glib::ustring s;
106   count = tally(p, Triumphs::TALLY_HERO);
107   if (p == d_player)
108     s = String::ucompose (ngettext("%1 hero earned fates worthy of legend!",
109                                    "%1 heroes earned fates worthy of legend!",
110                                    count), count);
111   else
112     s = String::ucompose (ngettext
113                           ("%1 so-called hero slaughtered without mercy!",
114                            "%1 so-called heroes slaughtered without mercy!",
115                            count), count);
116   Gtk::Label *hero_label = new Gtk::Label(s);
117
118   const ArmyProto *hero = NULL;
119   const Armysetlist* al = Armysetlist::getInstance();
120   //let's go find the hero army
121   for (unsigned int j = 0; j < al->getSize(p->getArmyset()); j++)
122     {
123       const ArmyProto *a = al->getArmy (p->getArmyset(), j);
124       if (a->isHero())
125         {
126           hero = a;
127           break;
128         }
129     }
130   Gtk::Image *hero_image = new Gtk::Image();
131   hero_image->property_pixbuf() = gc->getArmyPic(p->getArmyset(), hero->getTypeId(), p, NULL)->to_pixbuf();
132   Gtk::HBox *hero_hbox = new Gtk::HBox();
133   hero_hbox->pack_start(*manage(hero_image), Gtk::PACK_SHRINK, 10);
134   hero_hbox->pack_start(*manage(hero_label), Gtk::PACK_SHRINK, 10);
135
136   count = tally(p, Triumphs::TALLY_SHIP);
137   if (p == d_player)
138     s = String::ucompose (ngettext("%1 navy not currently in service!",
139                                    "%1 navies not currently in service!",
140                                    count), count);
141   else
142     s = String::ucompose (ngettext("%1 navy rests with the fishes!",
143                                    "%1 navies rest with the fishes!",
144                                    count), count);
145   Gtk::Label *ship_label = new Gtk::Label(s);
146   Gtk::Image *ship_image = new Gtk::Image ();
147   ship_image->property_pixbuf() = gc->getShipPic(p)->to_pixbuf();
148   Gtk::HBox *ship_hbox = new Gtk::HBox();
149   ship_hbox->pack_start(*manage(ship_image), Gtk::PACK_SHRINK, 10);
150   ship_hbox->pack_start(*manage(ship_label), Gtk::PACK_SHRINK, 10);
151
152   count = tally(p, Triumphs::TALLY_NORMAL);
153   if (p == d_player)
154     s = String::ucompose (ngettext("%1 army died to ensure final victory!",
155                                    "%1 armies died to ensure final victory!",
156                                    count), count);
157   else
158     s = String::ucompose (ngettext("%1 army smote like sheep!",
159                                    "%1 armies smote like sheep!",
160                                    count), count);
161   Gtk::Label *normal_label = new Gtk::Label(s);
162   Gtk::Image *normal_image = new Gtk::Image();
163   normal_image->property_pixbuf() = gc->getArmyPic(p->getArmyset(), 0, p, NULL)->to_pixbuf();
164   Gtk::HBox *normal_hbox = new Gtk::HBox();
165   normal_hbox->pack_start(*manage(normal_image), Gtk::PACK_SHRINK, 10);
166   normal_hbox->pack_start(*manage(normal_label), Gtk::PACK_SHRINK, 10);
167
168   count = tally(p, Triumphs::TALLY_SPECIAL);
169   if (p == d_player)
170     s = String::ucompose 
171       (ngettext ("%1 unnatural creature returned from whence it came!",
172                  "%1 unnatural creatures returned from whence they came!",
173                  count), count);
174   else
175     s = String::ucompose (ngettext ("%1 unnatural creature dispatched!",
176                                     "%1 unnatural creatures dispatched!",
177                                     count), count);
178   Gtk::Label *special_label = new Gtk::Label(s);
179   //let's go find a special army
180   const ArmyProto *special = NULL;
181   for (unsigned int j = 0; j < al->getSize(p->getArmyset()); j++)
182     {
183       const ArmyProto *a = al->getArmy (p->getArmyset(), j);
184       if (a->getAwardable())
185         {
186           special = a;
187           break;
188         }
189     }
190   Gtk::Image *special_image = new Gtk::Image();
191   special_image->property_pixbuf() = 
192     gc->getArmyPic(p->getArmyset(), special->getTypeId(), p, NULL)->to_pixbuf();
193   Gtk::HBox *special_hbox = new Gtk::HBox();
194   special_hbox->pack_start(*manage(special_image), Gtk::PACK_SHRINK, 10);
195   special_hbox->pack_start(*manage(special_label), Gtk::PACK_SHRINK, 10);
196
197   count = tally(p, Triumphs::TALLY_FLAG);
198   if (p == d_player)
199     s = String::ucompose (ngettext ("%1 standard betrayed by it's guardian!",
200                                     "%1 standards betrayed by it's guardian!",
201                                     count), count);
202   else
203     s = String::ucompose (ngettext 
204                           ("%1 standard wrested from a vanquished foe!",
205                            "%1 standards wrested from a vanquished foe!",
206                            count), count);
207   Gtk::Label *flag_label = new Gtk::Label(s);
208   Gtk::Image *flag_image = new Gtk::Image ();
209   flag_image->property_pixbuf() = gc->getPlantedStandardPic(p)->to_pixbuf();
210   Gtk::HBox *flag_hbox = new Gtk::HBox();
211   flag_hbox->pack_start(*manage(flag_image), Gtk::PACK_SHRINK, 10);
212   flag_hbox->pack_start(*manage(flag_label), Gtk::PACK_SHRINK, 10);
213
214   Gtk::VBox *contents = new Gtk::VBox();
215   contents->add(*manage(normal_hbox));
216   contents->add(*manage(special_hbox));
217   contents->add(*manage(hero_hbox));
218   contents->add(*manage(ship_hbox));
219   contents->add(*manage(flag_hbox));
220   Gtk::Image *shield_image = new Gtk::Image();
221   shield_image->property_pixbuf() = gc->getShieldPic(2, p)->to_pixbuf();
222   notebook->append_page (*manage(contents), *manage(shield_image));
223 }
224
225 void TriumphsDialog::fill_in_info()
226 {
227   for (unsigned int i = 0; i < MAX_PLAYERS; i++)
228     {
229       Player *p = Playerlist::getInstance()->getPlayer(i);
230       if (p == NULL)
231         continue;
232       if (p == Playerlist::getInstance()->getNeutral())
233         continue;
234       fill_in_page(p);
235     }
236 }