initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / buy-production-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 #include <assert.h>
24
25 #include "buy-production-dialog.h"
26
27 #include "glade-helpers.h"
28 #include "image-helpers.h"
29 #include "input-helpers.h"
30 #include "ucompose.hpp"
31 #include "defs.h"
32 #include "army.h"
33 #include "city.h"
34 #include "GraphicsCache.h"
35 #include "armysetlist.h"
36 #include "playerlist.h"
37 #include "File.h"
38
39 BuyProductionDialog::BuyProductionDialog(City *c)
40 {
41   army_info_tip = NULL;
42     GraphicsCache *gc = GraphicsCache::getInstance();
43     city = c;
44     selected_army = NO_ARMY_SELECTED;
45     
46     Glib::RefPtr<Gtk::Builder> xml
47         = Gtk::Builder::create_from_file(get_glade_path()
48                                     + "/buy-production-dialog.ui");
49
50     xml->get_widget("dialog", dialog);
51     decorate(dialog);
52     window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
53     dialog->set_icon_from_file(File::getMiscFile("various/castle_icon.png"));
54     
55     xml->get_widget("production_info_label1", production_info_label1);
56     xml->get_widget("production_info_label2", production_info_label2);
57     xml->get_widget("buy_button", buy_button);
58
59     Gtk::Table *toggles_table; 
60     xml->get_widget("production_toggles_table", toggles_table);
61     
62     const Armysetlist* al = Armysetlist::getInstance();
63
64     Player *p = Playerlist::getInstance()->getActiveplayer();
65     // fill in purchasable armies
66     for (unsigned int j = 0; j < al->getSize(p->getArmyset()); j++)
67       {
68         const ArmyProto *a = al->getArmy (p->getArmyset(), j);
69         if (a->getNewProductionCost() > 0)
70           purchasables.push_back(a);
71       }
72
73     // fill in production options
74     const int no_columns = 4;
75     for (unsigned int i = 0; i < purchasables.size(); ++i)
76     {
77         Gtk::ToggleButton *toggle = manage(new Gtk::ToggleButton);
78         
79         bool greyed_out = false;
80         if (city->hasProductionBase(purchasables[i]) == true)
81           greyed_out = true;
82         else if ((int)purchasables[i]->getNewProductionCost() > 
83                  c->getOwner()->getGold())
84           greyed_out = true;
85         Glib::RefPtr<Gdk::Pixbuf> pix
86             = gc->getArmyPic(p->getArmyset(), purchasables[i]->getTypeId(), p, 
87                              NULL, greyed_out)->to_pixbuf();
88         
89         Gtk::Image *image = new Gtk::Image();
90         image->property_pixbuf() = pix;
91         toggle->add(*manage(image));
92         production_toggles.push_back(toggle);
93         int x = i % no_columns;
94         int y = i / no_columns;
95         toggles_table->attach(*toggle, x, x + 1, y, y + 1,
96                               Gtk::SHRINK, Gtk::SHRINK);
97         toggle->show_all();
98
99         toggle->signal_toggled().connect(
100             sigc::bind(sigc::mem_fun(this, &BuyProductionDialog::on_production_toggled),
101                        toggle));
102         toggle->add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
103         toggle->signal_button_press_event().connect(
104             sigc::bind(sigc::mem_fun(*this, &BuyProductionDialog::on_production_button_event),
105                        toggle), false);
106         
107         toggle->signal_button_release_event().connect(
108             sigc::bind(sigc::mem_fun(*this, &BuyProductionDialog::on_production_button_event),
109                        toggle), false);
110     }
111
112     ignore_toggles = false;
113     production_toggles[0]->set_active(true);
114 }
115
116 BuyProductionDialog::~BuyProductionDialog()
117 {
118   if (army_info_tip)
119     delete army_info_tip;
120   delete dialog;
121 }
122 void BuyProductionDialog::set_parent_window(Gtk::Window &parent)
123 {
124     dialog->set_transient_for(parent);
125     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
126 }
127
128 void BuyProductionDialog::hide()
129 {
130   dialog->hide();
131 }
132
133 void BuyProductionDialog::run()
134 {
135     dialog->show();
136     int response = dialog->run();
137
138     if (response != Gtk::RESPONSE_ACCEPT)
139         selected_army = NO_ARMY_SELECTED;
140 }
141
142 void BuyProductionDialog::on_production_toggled(Gtk::ToggleButton *toggle)
143 {
144     if (ignore_toggles)
145         return;
146     
147     selected_army = NO_ARMY_SELECTED;
148     ignore_toggles = true;
149     for (unsigned int i = 0; i < production_toggles.size(); ++i) {
150         if (toggle == production_toggles[i])
151             selected_army = i;
152         
153         production_toggles[i]->set_active(toggle == production_toggles[i]);
154     }
155     ignore_toggles = false;
156
157     fill_in_production_info();
158     set_buy_button_state();
159 }
160
161 void BuyProductionDialog::fill_in_production_info()
162 {
163     Glib::ustring s1, s2;
164     
165     if (selected_army == -1)
166     {
167         s1 = _("No production");
168         s1 += "\n\n\n";
169         s2 = "\n\n\n";
170     }
171     else
172     {
173         const ArmyProto *a = army_id_to_army();
174
175         // fill in first column
176         s1 += a->getName();
177         s1 += "\n";
178         s1 += String::ucompose(_("Duration: %1"), a->getProduction());
179         s1 += "\n";
180         // note to translators: %1 is melee strength, %2 is ranged strength
181         s1 += String::ucompose(_("Strength: %1"),
182                               a->getStrength());
183         
184         // fill in second column
185         s2 += String::ucompose(_("Cost: %1"), a->getNewProductionCost());
186         s2 += "\n";
187         s2 += String::ucompose(_("Moves: %1"), a->getMaxMoves());
188         s2 += "\n";
189         s2 += String::ucompose(_("Upkeep: %1"), a->getUpkeep());
190     }
191     
192     production_info_label1->set_markup("<i>" + s1 + "</i>");
193     production_info_label2->set_markup("<i>" + s2 + "</i>");
194 }
195
196 void BuyProductionDialog::set_buy_button_state()
197 {
198     bool can_buy = true;
199     
200     if (selected_army == NO_ARMY_SELECTED)
201         can_buy = false;
202     else
203     {
204         int gold = city->getOwner()->getGold();
205         const ArmyProto *a = army_id_to_army();
206         
207         if (int(a->getNewProductionCost()) > gold ||
208             city->hasProductionBase(selected_army, 
209                                     city->getOwner()->getArmyset()))
210             can_buy = false;
211     }
212     
213     buy_button->set_sensitive(can_buy);
214 }
215
216 const ArmyProto *BuyProductionDialog::army_id_to_army()
217 {
218     return purchasables[selected_army];
219 }
220
221 bool BuyProductionDialog::on_production_button_event(GdkEventButton *e, Gtk::ToggleButton *toggle)
222 {
223     MouseButtonEvent event = to_input_event(e);
224     if (event.button == MouseButtonEvent::RIGHT_BUTTON
225         && event.state == MouseButtonEvent::PRESSED) {
226         int slot = -1;
227         for (unsigned int i = 0; i < production_toggles.size(); ++i) {
228             if (toggle == production_toggles[i])
229                 slot = i;
230         }
231         assert(slot != -1);
232
233         const ArmyProto *army = purchasables[slot];
234
235         if (army)
236           {
237             if (army_info_tip)
238               delete army_info_tip;
239             army_info_tip = new ArmyInfoTip(toggle, army);
240           }
241         return true;
242     }
243     else if (event.button == MouseButtonEvent::RIGHT_BUTTON
244              && event.state == MouseButtonEvent::RELEASED) {
245         {
246           if (army_info_tip)
247             {
248               delete army_info_tip;
249               army_info_tip = NULL;
250             }
251         }
252         return true;
253     }
254     
255     return false;
256 }
257