1 // Copyright (C) 2008, 2009, 2011, 2014, 2020 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 <gtkmm.h>
21 #include <sigc++/functors/mem_fun.h>
22
23 #include "diplomacy-dialog.h"
24 #include "diplomacy-report-dialog.h"
25
26 #include "defs.h"
27 #include "ImageCache.h"
28 #include "playerlist.h"
29 #include "player.h"
30 #include "font-size.h"
31
32 #define method(x) sigc::mem_fun(*this, &DiplomacyDialog::x)
33
DiplomacyDialog(Gtk::Window & parent,Player * player)34 DiplomacyDialog::DiplomacyDialog(Gtk::Window &parent, Player *player)
35 : LwDialog(parent, "diplomacy-dialog.ui")
36 {
37 ImageCache *gc = ImageCache::getInstance();
38 Playerlist *pl = Playerlist::getInstance();
39 d_player = player;
40 xml->get_widget("proposals_table", d_proposals_table);
41 xml->get_widget("offers_table", d_offers_table);
42 xml->get_widget("player_label", d_player_label);
43 xml->get_widget("player_shield_image", d_player_shield_image);
44 xml->get_widget("report_button", d_report_button);
45
46 d_report_button->signal_clicked().connect (method(on_report_clicked));
47
48 // put the shields across the top of the proposals table, minus our own
49 guint32 i = 0;
50 guint32 j = 0;
51 for (unsigned int k = 0; k < MAX_PLAYERS; k++)
52 {
53 Player *p = pl->getPlayer(k);
54 if (p == NULL)
55 continue;
56 if (pl->getNeutral() == p)
57 continue;
58 if (p == d_player)
59 continue;
60 Glib::RefPtr<Gdk::Pixbuf> pixbuf =
61 gc->getShieldPic(2, p, false,
62 FontSize::getInstance ()->get_height ())->to_pixbuf();
63 Gtk::Image *im = new Gtk::Image();
64 im->property_pixbuf() = pixbuf;
65 d_proposals_table->attach(*manage(im), i, 0, 1, 1);
66 i++;
67 }
68 d_proposals_table->set_column_spacing (16);
69
70 d_player_shield_image->property_pixbuf() =
71 gc->getShieldPic(2, d_player, false,
72 FontSize::getInstance ()->get_height ())->to_pixbuf();
73
74 d_player_label->set_text(d_player->getName());
75
76 //fill in diplomatic state
77 i = 0;
78 j = 0;
79 for (unsigned int k = 0; k < MAX_PLAYERS; k++)
80 {
81 Player *p = pl->getPlayer(k);
82 if (p == NULL)
83 continue;
84 if (pl->getNeutral() == p)
85 continue;
86 if (p == d_player)
87 continue;
88 if (p->isDead())
89 {
90 i++;
91 continue;
92 }
93 j = 0;
94 Player::DiplomaticState state = d_player->getDiplomaticState (p);
95 Glib::RefPtr<Gdk::Pixbuf> pixbuf=
96 gc->getDiplomacyPic(1, state,
97 FontSize::getInstance ()->get_height ())->to_pixbuf();
98 Gtk::Image *im = new Gtk::Image();
99 im->property_pixbuf() = pixbuf;
100 d_proposals_table->attach(*manage(im), i, j + 1, 1, 1);
101 Player::DiplomaticProposal proposal = p->getDiplomaticProposal (d_player);
102 if (proposal != Player::NO_PROPOSAL)
103 {
104 j = 1;
105 Glib::RefPtr<Gdk::Pixbuf> pixbuf2;
106 switch (proposal)
107 {
108 case Player::PROPOSE_PEACE:
109 pixbuf2 =
110 gc->getDiplomacyPic
111 (1, Player::AT_PEACE,
112 FontSize::getInstance ()->get_height ())->to_pixbuf();
113 break;
114 case Player::PROPOSE_WAR_IN_FIELD:
115 pixbuf2 =
116 gc->getDiplomacyPic
117 (1, Player::AT_WAR_IN_FIELD,
118 FontSize::getInstance ()->get_height ())->to_pixbuf();
119 break;
120 case Player::PROPOSE_WAR:
121 pixbuf2 =
122 gc->getDiplomacyPic
123 (1, Player::AT_WAR,
124 FontSize::getInstance ()->get_height ())->to_pixbuf();
125 break;
126 default:
127 continue;
128 }
129
130 Gtk::Image *im2 = manage(new Gtk::Image());
131 im2->property_pixbuf() = pixbuf2;
132 d_proposals_table->attach(*manage(im2), i , j + 1, 1, 1);
133 }
134 i++;
135 }
136
137
138 // fill in the togglebuttons
139 i = 0;
140 j = 0;
141 for (unsigned int k = 0; k < MAX_PLAYERS; k++)
142 {
143 Player *p = pl->getPlayer(k);
144 if (p == NULL)
145 continue;
146 if (pl->getNeutral() == p)
147 continue;
148 if (p == d_player)
149 continue;
150
151 //show the peace radio buttons
152 j = 0;
153 Gtk::RadioButton *radio1= manage(new Gtk::RadioButton);
154 Gtk::Image *im3 = new Gtk::Image();
155 im3->property_pixbuf() =
156 gc->getDiplomacyPic(1, Player::AT_PEACE,
157 FontSize::getInstance ()->get_height ())->to_pixbuf();
158 radio1->set_tooltip_text(_("Propose peace"));
159 radio1->add(*manage(im3));
160 radio1->set_mode(false);
161 Gtk::RadioButtonGroup group = radio1->get_group();
162 if (p->isDead())
163 radio1->set_sensitive(false);
164 else
165 radio1->set_active (d_player->getDiplomaticProposal(p) ==
166 Player::PROPOSE_PEACE);
167 radio1->signal_toggled().connect
168 (sigc::bind(method(on_proposal_toggled), radio1, p, Player::PROPOSE_PEACE));
169 d_offers_table->attach(*radio1, i, j ,1, 1);
170
171 j = 1;
172 Gtk::RadioButton *radio2= manage(new Gtk::RadioButton);
173 Gtk::Image *im4 = new Gtk::Image();
174 im4->property_pixbuf() =
175 gc->getDiplomacyPic(1, Player::AT_WAR_IN_FIELD,
176 FontSize::getInstance ()->get_height ())->to_pixbuf();
177 radio2->set_tooltip_text(_("Propose war on armies not in cities"));
178 radio2->add(*manage(im4));
179 radio2->set_mode(false);
180 radio2->set_group(group);
181 if (p->isDead())
182 radio2->set_sensitive(false);
183 else
184 radio2->set_active (d_player->getDiplomaticProposal(p) ==
185 Player::PROPOSE_WAR_IN_FIELD);
186 radio2->signal_toggled().connect
187 (sigc::bind(method(on_proposal_toggled), radio2, p,
188 Player::PROPOSE_WAR_IN_FIELD));
189 d_offers_table->attach(*radio2, i, j, 1 , 1);
190
191 j = 2;
192 Gtk::RadioButton *radio3= manage(new Gtk::RadioButton);
193 Gtk::Image *im5 = new Gtk::Image();
194 im5->property_pixbuf() =
195 gc->getDiplomacyPic(1, Player::AT_WAR,
196 FontSize::getInstance ()->get_height ())->to_pixbuf();
197 radio3->set_tooltip_text(_("Propose war"));
198 radio3->add(*manage(im5));
199 radio3->set_mode(false);
200 radio3->set_group(group);
201 if (p->isDead())
202 radio3->set_sensitive(false);
203 else
204 radio3->set_active (d_player->getDiplomaticProposal(p) ==
205 Player::PROPOSE_WAR);
206 radio3->signal_toggled().connect
207 (sigc::bind(method(on_proposal_toggled), radio3, p, Player::PROPOSE_WAR));
208 d_offers_table->attach(*radio3, i, j, 1, 1);
209 i++;
210 }
211 }
212
on_proposal_toggled(Gtk::ToggleButton * toggle,Player * player,Player::DiplomaticProposal proposal)213 void DiplomacyDialog::on_proposal_toggled (Gtk::ToggleButton *toggle,
214 Player *player,
215 Player::DiplomaticProposal proposal)
216 {
217 if (toggle->get_active() == true)
218 d_player->proposeDiplomacy (proposal, player);
219 }
220
on_report_clicked()221 void DiplomacyDialog::on_report_clicked()
222 {
223 DiplomacyReportDialog d(*dialog, d_player);
224 d.run_and_hide();
225 }
226