initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / masked-image-editor-dialog.cpp
1 //  Copyright (C) 2009 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 #include <stdlib.h>
23
24 #include "masked-image-editor-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "gui/image-helpers.h"
28 #include "ucompose.hpp"
29 #include "defs.h"
30 #include "File.h"
31 #include "shieldsetlist.h"
32 #include "GraphicsCache.h"
33
34
35 MaskedImageEditorDialog::MaskedImageEditorDialog(std::string filename)
36 {
37     Glib::RefPtr<Gtk::Builder> xml
38         = Gtk::Builder::create_from_file(get_glade_path()
39                                     + "/masked-image-editor-dialog.ui");
40
41     xml->get_widget("dialog", dialog);
42
43     xml->get_widget("filechooserbutton", filechooserbutton);
44     xml->get_widget("image_white", image_white);
45     xml->get_widget("image_green", image_green);
46     xml->get_widget("image_yellow", image_yellow);
47     xml->get_widget("image_light_blue", image_light_blue);
48     xml->get_widget("image_red", image_red);
49     xml->get_widget("image_dark_blue", image_dark_blue);
50     xml->get_widget("image_orange", image_orange);
51     xml->get_widget("image_black", image_black);
52     xml->get_widget("image_neutral", image_neutral);
53     show_image(filename);
54     target_filename = filename;
55     update_panel();
56     filechooserbutton->signal_selection_changed().connect
57        (sigc::mem_fun(*this, &MaskedImageEditorDialog::on_image_chosen));
58
59 }
60
61 MaskedImageEditorDialog::~MaskedImageEditorDialog()
62 {
63   delete dialog;
64 }
65 void MaskedImageEditorDialog::set_parent_window(Gtk::Window &parent)
66 {
67     dialog->set_transient_for(parent);
68     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
69 }
70
71 int MaskedImageEditorDialog::run()
72 {
73     dialog->show_all();
74     int response = dialog->run();
75     if (response == Gtk::RESPONSE_ACCEPT)
76       target_filename = "";
77
78     return response;
79 }
80
81 void MaskedImageEditorDialog::on_image_chosen()
82 {
83   std::string selected_filename = filechooserbutton->get_filename();
84   if (selected_filename.empty())
85     return;
86
87   target_filename = selected_filename;
88   show_image(selected_filename);
89 }
90
91
92 void MaskedImageEditorDialog::update_panel()
93 {
94   if (target_filename != "")
95     filechooserbutton->set_filename (target_filename);
96 }
97
98 void MaskedImageEditorDialog::show_image(std::string filename)
99 {
100   if (filename == "")
101     return;
102   std::vector<PixMask*> half = disassemble_row(filename, 2);
103   for (unsigned int i = Shield::WHITE; i <= Shield::NEUTRAL; i++)
104     {
105       Gtk::Image *image = NULL;
106       switch (i)
107         {
108         case Shield::WHITE: image = image_white; break;
109         case Shield::GREEN: image = image_green; break;
110         case Shield::YELLOW: image = image_yellow; break;
111         case Shield::LIGHT_BLUE: image = image_light_blue; break;
112         case Shield::RED: image = image_red; break;
113         case Shield::DARK_BLUE: image = image_dark_blue; break;
114         case Shield::ORANGE: image = image_orange; break;
115         case Shield::BLACK: image = image_black; break;
116         case Shield::NEUTRAL: image = image_neutral; break;
117         default : break;
118         }
119
120       Gdk::Color colour = Shieldsetlist::getInstance()->getColor(1, i);
121       PixMask *army_image = GraphicsCache::applyMask(half[0],  half[1],
122                                                      colour, false);
123       image->property_pixbuf() = army_image->to_pixbuf();
124       delete army_image;
125     }
126   delete half[0];
127   delete half[1];
128 }