1 //  Copyright (C) 2011, 2014, 2015, 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 #include <gtkmm.h>
20 
21 #include "game-button-box.h"
22 #include "builder-cache.h"
23 #include "File.h"
24 #include "game.h"
25 #include "GameScenario.h"
26 #include "playerlist.h"
27 #include "player.h"
28 #include "ImageCache.h"
29 #include "font-size.h"
30 
create()31 GameButtonBox * GameButtonBox::create()
32 {
33   Glib::ustring file = "game-button-box-large-screen.ui";
34   Glib::RefPtr<Gtk::Builder> xml = BuilderCache::get(file);
35 
36   GameButtonBox *box;
37   xml->get_widget_derived("box", box);
38   box->add_pictures_to_buttons();
39   return box;
40 }
41 
pad_image(Gtk::Image * image)42 void GameButtonBox::pad_image(Gtk::Image *image)
43 {
44   int padding = 3;
45   image->property_xpad() = padding;
46   image->property_ypad() = padding;
47 }
48 
add_picture_to_button(guint32 icontype,Gtk::Button * button)49 void GameButtonBox::add_picture_to_button (guint32 icontype, Gtk::Button *button)
50 {
51   Gtk::Image *image = new Gtk::Image();
52   PixMask *pixmask =
53     ImageCache::getInstance()->getGameButtonPic
54     (icontype, FontSize::getInstance ()->get_height ());
55   image->property_pixbuf() = pixmask->to_pixbuf();
56   pad_image(image);
57   button->add(*manage(image));
58 }
59 
add_pictures_to_buttons()60 void GameButtonBox::add_pictures_to_buttons()
61 {
62   add_picture_to_button (ImageCache::NEXT_MOVABLE_STACK, next_movable_button);
63   add_picture_to_button (ImageCache::CENTER_ON_STACK, center_button);
64   add_picture_to_button (ImageCache::DIPLOMACY_NO_PROPOSALS, diplomacy_button);
65   add_picture_to_button (ImageCache::STACK_DEFEND, defend_button);
66   add_picture_to_button (ImageCache::STACK_PARK, park_button);
67   add_picture_to_button (ImageCache::STACK_DESELECT, deselect_button);
68   add_picture_to_button (ImageCache::STACK_SEARCH, search_button);
69   add_picture_to_button (ImageCache::STACK_MOVE, move_button);
70   add_picture_to_button (ImageCache::MOVE_ALL_STACKS, move_all_button);
71   add_picture_to_button (ImageCache::END_TURN, end_turn_button);
72 }
73 
GameButtonBox(BaseObjectType * baseObject,const Glib::RefPtr<Gtk::Builder> & xml)74 GameButtonBox::GameButtonBox(BaseObjectType* baseObject, const Glib::RefPtr<Gtk::Builder> &xml)
75   : Gtk::Box(baseObject)
76 {
77   xml->get_widget("next_movable_button", next_movable_button);
78   xml->get_widget("center_button", center_button);
79   xml->get_widget("diplomacy_button", diplomacy_button);
80   xml->get_widget("defend_button", defend_button);
81   xml->get_widget("park_button", park_button);
82   xml->get_widget("deselect_button", deselect_button);
83   xml->get_widget("search_button", search_button);
84   xml->get_widget("move_button", move_button);
85   xml->get_widget("move_all_button", move_all_button);
86   xml->get_widget("end_turn_button", end_turn_button);
87 }
88 
drop_connections()89 void GameButtonBox::drop_connections()
90 {
91   std::list<sigc::connection>::iterator it = connections.begin();
92   for (; it != connections.end(); it++)
93     (*it).disconnect();
94   connections.clear();
95 }
96 
setup_signals(Game * game)97 void GameButtonBox::setup_signals(Game *game)
98 {
99   drop_connections();
100   setup_button(next_movable_button,
101                sigc::mem_fun(game, &Game::select_next_movable_stack),
102                game->can_select_next_movable_stack);
103   setup_button(defend_button,
104                sigc::mem_fun(game, &Game::defend_selected_stack),
105                game->can_defend_selected_stack);
106   setup_button(park_button,
107                sigc::mem_fun(game, &Game::park_selected_stack),
108                game->can_park_selected_stack);
109   setup_button(deselect_button,
110                sigc::mem_fun(game, &Game::deselect_selected_stack),
111                game->can_deselect_selected_stack);
112   setup_button(search_button,
113                sigc::mem_fun(game, &Game::search_selected_stack),
114                game->can_search_selected_stack);
115   setup_button(move_button,
116                sigc::mem_fun(game, &Game::move_selected_stack_along_path),
117                game->can_move_selected_stack_along_path);
118   setup_button(move_all_button,
119                sigc::mem_fun(game, &Game::move_all_stacks),
120                game->can_move_all_stacks);
121   setup_button(end_turn_button,
122                sigc::mem_fun(game, &Game::end_turn),
123                game->can_end_turn);
124   setup_button(center_button,
125                sigc::mem_fun(game, &Game::center_selected_stack),
126                game->can_center_selected_stack);
127   connections.push_back
128     (game->received_diplomatic_proposal.connect
129      (sigc::mem_fun(*this, &GameButtonBox::change_diplomacy_button_image)));
130   connections.push_back
131     (game->can_end_turn.connect
132      (sigc::mem_fun(*this, &GameButtonBox::update_diplomacy_button)));
133   connections.push_back
134     (diplomacy_button->signal_clicked().connect
135      (diplomacy_clicked, &sigc::signal<void>::emit));
136 }
137 
setup_button(Gtk::Button * button,sigc::slot<void> slot,sigc::signal<void,bool> & game_signal)138 void GameButtonBox::setup_button(Gtk::Button *button, sigc::slot<void> slot,
139                                  sigc::signal<void, bool> &game_signal)
140 {
141   connections.push_back (button->signal_clicked().connect(slot));
142   connections.push_back
143     (game_signal.connect(sigc::mem_fun(button, &Gtk::Widget::set_sensitive)));
144 }
145 
update_diplomacy_button(bool sensitive)146 void GameButtonBox::update_diplomacy_button (bool sensitive)
147 {
148   if (Playerlist::getActiveplayer()->getType() != Player::HUMAN)
149     {
150       diplomacy_button->set_sensitive (false);
151       return;
152     }
153   if (GameScenario::s_diplomacy == false)
154     {
155       diplomacy_button->set_sensitive (false);
156       return;
157     }
158   diplomacy_button->set_sensitive(sensitive);
159 }
160 
change_diplomacy_button_image(bool proposals_present)161 void GameButtonBox::change_diplomacy_button_image (bool proposals_present)
162 {
163   ImageCache *gc = ImageCache::getInstance();
164   /* switch up the image. */
165   if (proposals_present)
166     {
167       Gtk::Image *proposals_present_image = new Gtk::Image();
168       proposals_present_image->property_pixbuf() =
169         gc->getGameButtonPic (ImageCache::DIPLOMACY_NEW_PROPOSALS,
170                               FontSize::getInstance()->get_height())->to_pixbuf();
171       pad_image(proposals_present_image);
172       diplomacy_button->property_image() = proposals_present_image;
173     }
174   else
175     {
176       Gtk::Image *proposals_not_present_image = new Gtk::Image();
177       proposals_not_present_image->property_pixbuf() =
178         gc->getGameButtonPic (ImageCache::DIPLOMACY_NO_PROPOSALS,
179                               FontSize::getInstance()->get_height())->to_pixbuf();
180       pad_image(proposals_not_present_image);
181       diplomacy_button->property_image() = proposals_not_present_image;
182     }
183 }
184 
give_some_cheese()185 void GameButtonBox::give_some_cheese()
186 {
187   end_turn_button->set_sensitive(false);
188 }
189 
get_end_turn_button_sensitive()190 bool GameButtonBox::get_end_turn_button_sensitive()
191 {
192   return end_turn_button->get_sensitive();
193 }
194