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>
24 #include "preferences-dialog.h"
26 #include "glade-helpers.h"
27 #include "image-helpers.h"
28 #include "ucompose.hpp"
30 #include "Configuration.h"
33 #include "playerlist.h"
37 #include "GraphicsCache.h"
41 PreferencesDialog::PreferencesDialog(bool readonly)
43 d_readonly = readonly;
44 Glib::RefPtr<Gtk::Builder> xml
45 = Gtk::Builder::create_from_file(get_glade_path()
46 + "/preferences-dialog.ui");
48 xml->get_widget("dialog", dialog);
50 window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
52 xml->get_widget("commentator_checkbutton", commentator_checkbutton);
53 xml->get_widget("speed_scale", speed_scale);
54 xml->get_widget("play_music_checkbutton", play_music_checkbutton);
55 xml->get_widget("music_volume_scale", music_volume_scale);
56 xml->get_widget("music_volume_hbox", music_volume_hbox);
57 xml->get_widget("players_vbox", players_vbox);
59 GraphicsCache *gc = GraphicsCache::getInstance();
60 for (unsigned int i = 0; i < MAX_PLAYERS; i++)
62 Player *p = Playerlist::getInstance()->getPlayer(i);
65 if (p == Playerlist::getInstance()->getNeutral())
67 Gtk::HBox *player_hbox = new Gtk::HBox();
68 Gtk::Image *image = new Gtk::Image();
69 image->property_pixbuf() = gc->getShieldPic(2, p)->to_pixbuf();
70 Gtk::ComboBoxText *type = new Gtk::ComboBoxText();
71 type->signal_changed().connect
72 (sigc::bind(sigc::mem_fun
73 (this, &PreferencesDialog::on_type_changed), type));
74 Gtk::CheckButton *observe = new Gtk::CheckButton(_("Observe"));
75 observe->signal_toggled().connect
76 (sigc::bind(sigc::mem_fun
77 (this, &PreferencesDialog::on_observe_toggled), observe));
79 observe->set_active(p->isObservable());
80 type->append_text(_("Human"));
81 type->append_text(_("Computer"));
82 if (p->getType() == Player::HUMAN)
84 observe->set_sensitive(false);
89 if (p->isDead() || (Playerlist::getActiveplayer() == p &&
90 p->getType() != Player::HUMAN))
92 type->set_sensitive(false);
93 observe->set_sensitive(false);
96 type->set_sensitive(false);
97 player_hbox->pack_start(*manage(image), Gtk::PACK_SHRINK,
99 player_hbox->pack_start(*manage(type), Gtk::PACK_SHRINK, 10);
100 player_hbox->pack_start(*manage(observe), Gtk::PACK_SHRINK, 10);
101 //player_types.push_back(type);
102 player_types[p] = type;
103 player_observed[p] = observe;
104 //player_observed.push_back(observe);
105 Gtk::Label *player_name = new Gtk::Label(p->getName());
106 player_hbox->pack_start(*manage(player_name), Gtk::PACK_SHRINK, 10);
107 players_vbox->pack_start(*manage(player_hbox));
109 players_vbox->show_all_children();
110 commentator_checkbutton->signal_toggled().connect(
111 sigc::mem_fun(this, &PreferencesDialog::on_show_commentator_toggled));
112 speed_scale->set_value(Configuration::s_displaySpeedDelay);
113 speed_scale->signal_value_changed().connect(
114 sigc::mem_fun(this, &PreferencesDialog::on_speed_changed));
115 play_music_checkbutton->signal_toggled().connect(
116 sigc::mem_fun(this, &PreferencesDialog::on_play_music_toggled));
117 music_volume_scale->signal_value_changed().connect(
118 sigc::mem_fun(this, &PreferencesDialog::on_music_volume_changed));
120 commentator_checkbutton->set_active(Configuration::s_displayCommentator);
121 play_music_checkbutton->set_active(Configuration::s_musicenable);
122 music_volume_hbox->set_sensitive(Configuration::s_musicenable);
123 music_volume_scale->set_value(Configuration::s_musicvolume * 100.0 / 128);
127 PreferencesDialog::~PreferencesDialog()
131 void PreferencesDialog::set_parent_window(Gtk::Window &parent)
133 dialog->set_transient_for(parent);
134 //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
137 void PreferencesDialog::on_type_changed(Gtk::ComboBoxText *combo)
139 for (PlayerTypeMap::iterator it = player_types.begin();
140 it != player_types.end(); it++)
142 if (combo == (*it).second)
145 * if we're turning this player into a human,
146 * then we desensitize the associated checkbutton
147 * otherwise, we presume that we want to observe it
149 if (combo->get_active_text() == _("Human"))
151 player_observed[(*it).first]->set_active(true);
152 player_observed[(*it).first]->set_sensitive(false);
156 player_observed[(*it).first]->set_sensitive(true);
157 player_observed[(*it).first]->set_active(true);
163 void PreferencesDialog::on_observe_toggled(Gtk::CheckButton *button)
165 for (PlayerObserveMap::iterator it = player_observed.begin();
166 it != player_observed.end(); it++)
168 if (button == (*it).second)
169 (*it).first->setObservable(button->get_active());
173 void PreferencesDialog::hide()
178 void PreferencesDialog::run(Game *game)
180 static int width = -1;
181 static int height = -1;
183 if (width != -1 && height != -1)
184 dialog->set_default_size(width, height);
189 dialog->get_size(width, height);
190 Configuration::saveConfigurationFile(Configuration::configuration_file_path);
191 PlayerTypeMap::iterator j = player_types.begin();
192 for (; j != player_types.end(); ++j)
194 Player *p = (*j).first;
197 if (p == Playerlist::getInstance()->getNeutral())
199 if (p->getType() == Player::HUMAN) //changing human to:
201 if ((*j).second->get_active_text() == _("Human")) //human, no change
203 else //computer, change to easy
205 AI_Fast *new_player = new AI_Fast(*p);
206 Player *old_player = p;
207 Playerlist::getInstance()->swap(old_player, new_player);
208 //disconnect and connect game signals
209 game->addPlayer(new_player);
213 else //changing computer to:
215 if ((*j).second->get_active_text() == _("Human")) //human, change it
217 RealPlayer *new_player = new RealPlayer(*p);
218 Player *old_player = p;
219 Playerlist::getInstance()->swap(old_player, new_player);
220 //disconnect and connect game signals
221 game->addPlayer(new_player);
224 else //computer, no change
231 void PreferencesDialog::on_show_commentator_toggled()
233 Configuration::s_displayCommentator = commentator_checkbutton->get_active();
236 void PreferencesDialog::on_play_music_toggled()
238 bool play_music = play_music_checkbutton->get_active();
240 Configuration::s_musicenable = play_music;
244 Sound::getInstance()->enableBackground();
248 Sound::getInstance()->haltMusic();
249 Sound::getInstance()->disableBackground();
251 music_volume_hbox->set_sensitive(Configuration::s_musicenable);
254 void PreferencesDialog::on_speed_changed()
256 Configuration::s_displaySpeedDelay = int(speed_scale->get_value());
259 void PreferencesDialog::on_music_volume_changed()
261 int volume = int(music_volume_scale->get_value() / 100 * 128);
264 Mix_VolumeMusic(volume);
267 Configuration::s_musicvolume = volume;