1 //  Copyright (C) 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 <ostream>
20 #include <iostream>
21 #include <sstream>
22 
23 #include <sigc++/functors/mem_fun.h>
24 
25 #include "army-chooser-button.h"
26 
27 #include "player.h"
28 #include "armysetlist.h"
29 
30 #define method(x) sigc::mem_fun(*this, &ArmyChooserButton::x)
31 
ArmyChooserButton(Gtk::Window & w,Glib::RefPtr<Gtk::Builder> xml,Glib::ustring widget,Player * p,SelectArmyDialog::Mode mode)32 ArmyChooserButton::ArmyChooserButton(Gtk::Window &w,
33                                      Glib::RefPtr<Gtk::Builder> xml,
34                                      Glib::ustring widget,
35                                      Player *p, SelectArmyDialog::Mode mode)
36 :d_window (w), d_player (p), d_mode (mode), d_selected (false),
37     d_selected_army_type (0)
38 {
39   xml->get_widget (widget, d_button);
40   d_button->signal_clicked ().connect(method (on_button_pressed));
41   update_button ();
42 }
43 
on_button_pressed()44 void ArmyChooserButton::on_button_pressed ()
45 {
46   int pre_selected = -1;
47   if (d_selected)
48     pre_selected = d_selected_army_type;
49   SelectArmyDialog d (d_window, d_mode, d_player, pre_selected);
50   d.run ();
51   const ArmyProto *a = d.get_selected_army ();
52   if (a)
53     {
54       d_selected = true;
55       d_selected_army_type = a->getId ();
56     }
57   update_button ();
58   army_selected.emit (a);
59 }
60 
update_button()61 void ArmyChooserButton::update_button ()
62 {
63   Glib::ustring name = "";
64   if (d_selected)
65     {
66       ArmyProto *a =
67         Armysetlist::getInstance ()->getArmy (d_player->getArmyset (),
68                                               d_selected_army_type);
69       if (a)
70         name = a->getName ();
71     }
72 
73   if (name != "")
74     d_button->set_label (name);
75   else
76     d_button->set_label (_("No army type selected"));
77 }
78 
select(guint32 selected)79 void ArmyChooserButton::select (guint32 selected)
80 {
81   d_selected = true;
82   d_selected_army_type = selected;
83   update_button ();
84 }
85 
clear_selected_army()86 void ArmyChooserButton::clear_selected_army ()
87 {
88   d_selected = false;
89   d_selected_army_type = 0;
90   update_button ();
91 }
92