1 // Copyright (C) 2007 Ole Laursen
2 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
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.
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.
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
22 #include <sigc++/functors/mem_fun.h>
25 #include "buy-production-dialog.h"
27 #include "glade-helpers.h"
28 #include "image-helpers.h"
29 #include "input-helpers.h"
30 #include "ucompose.hpp"
34 #include "GraphicsCache.h"
35 #include "armysetlist.h"
36 #include "playerlist.h"
39 BuyProductionDialog::BuyProductionDialog(City *c)
42 GraphicsCache *gc = GraphicsCache::getInstance();
44 selected_army = NO_ARMY_SELECTED;
46 Glib::RefPtr<Gtk::Builder> xml
47 = Gtk::Builder::create_from_file(get_glade_path()
48 + "/buy-production-dialog.ui");
50 xml->get_widget("dialog", dialog);
52 window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
53 dialog->set_icon_from_file(File::getMiscFile("various/castle_icon.png"));
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);
59 Gtk::Table *toggles_table;
60 xml->get_widget("production_toggles_table", toggles_table);
62 const Armysetlist* al = Armysetlist::getInstance();
64 Player *p = Playerlist::getInstance()->getActiveplayer();
65 // fill in purchasable armies
66 for (unsigned int j = 0; j < al->getSize(p->getArmyset()); j++)
68 const ArmyProto *a = al->getArmy (p->getArmyset(), j);
69 if (a->getNewProductionCost() > 0)
70 purchasables.push_back(a);
73 // fill in production options
74 const int no_columns = 4;
75 for (unsigned int i = 0; i < purchasables.size(); ++i)
77 Gtk::ToggleButton *toggle = manage(new Gtk::ToggleButton);
79 bool greyed_out = false;
80 if (city->hasProductionBase(purchasables[i]) == true)
82 else if ((int)purchasables[i]->getNewProductionCost() >
83 c->getOwner()->getGold())
85 Glib::RefPtr<Gdk::Pixbuf> pix
86 = gc->getArmyPic(p->getArmyset(), purchasables[i]->getTypeId(), p,
87 NULL, greyed_out)->to_pixbuf();
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);
99 toggle->signal_toggled().connect(
100 sigc::bind(sigc::mem_fun(this, &BuyProductionDialog::on_production_toggled),
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),
107 toggle->signal_button_release_event().connect(
108 sigc::bind(sigc::mem_fun(*this, &BuyProductionDialog::on_production_button_event),
112 ignore_toggles = false;
113 production_toggles[0]->set_active(true);
116 BuyProductionDialog::~BuyProductionDialog()
119 delete army_info_tip;
122 void BuyProductionDialog::set_parent_window(Gtk::Window &parent)
124 dialog->set_transient_for(parent);
125 //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
128 void BuyProductionDialog::hide()
133 void BuyProductionDialog::run()
136 int response = dialog->run();
138 if (response != Gtk::RESPONSE_ACCEPT)
139 selected_army = NO_ARMY_SELECTED;
142 void BuyProductionDialog::on_production_toggled(Gtk::ToggleButton *toggle)
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])
153 production_toggles[i]->set_active(toggle == production_toggles[i]);
155 ignore_toggles = false;
157 fill_in_production_info();
158 set_buy_button_state();
161 void BuyProductionDialog::fill_in_production_info()
163 Glib::ustring s1, s2;
165 if (selected_army == -1)
167 s1 = _("No production");
173 const ArmyProto *a = army_id_to_army();
175 // fill in first column
178 s1 += String::ucompose(_("Duration: %1"), a->getProduction());
180 // note to translators: %1 is melee strength, %2 is ranged strength
181 s1 += String::ucompose(_("Strength: %1"),
184 // fill in second column
185 s2 += String::ucompose(_("Cost: %1"), a->getNewProductionCost());
187 s2 += String::ucompose(_("Moves: %1"), a->getMaxMoves());
189 s2 += String::ucompose(_("Upkeep: %1"), a->getUpkeep());
192 production_info_label1->set_markup("<i>" + s1 + "</i>");
193 production_info_label2->set_markup("<i>" + s2 + "</i>");
196 void BuyProductionDialog::set_buy_button_state()
200 if (selected_army == NO_ARMY_SELECTED)
204 int gold = city->getOwner()->getGold();
205 const ArmyProto *a = army_id_to_army();
207 if (int(a->getNewProductionCost()) > gold ||
208 city->hasProductionBase(selected_army,
209 city->getOwner()->getArmyset()))
213 buy_button->set_sensitive(can_buy);
216 const ArmyProto *BuyProductionDialog::army_id_to_army()
218 return purchasables[selected_army];
221 bool BuyProductionDialog::on_production_button_event(GdkEventButton *e, Gtk::ToggleButton *toggle)
223 MouseButtonEvent event = to_input_event(e);
224 if (event.button == MouseButtonEvent::RIGHT_BUTTON
225 && event.state == MouseButtonEvent::PRESSED) {
227 for (unsigned int i = 0; i < production_toggles.size(); ++i) {
228 if (toggle == production_toggles[i])
233 const ArmyProto *army = purchasables[slot];
238 delete army_info_tip;
239 army_info_tip = new ArmyInfoTip(toggle, army);
243 else if (event.button == MouseButtonEvent::RIGHT_BUTTON
244 && event.state == MouseButtonEvent::RELEASED) {
248 delete army_info_tip;
249 army_info_tip = NULL;