initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / tileset-explosion-picture-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 "tileset-explosion-picture-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 "GraphicsCache.h"
32 #include "tile-preview-scene.h"
33
34
35 TilesetExplosionPictureEditorDialog::TilesetExplosionPictureEditorDialog(Tileset *tileset)
36 {
37   selected_filename = "";
38     Glib::RefPtr<Gtk::Builder> xml
39         = Gtk::Builder::create_from_file(get_glade_path()
40                                     + "/tileset-explosion-picture-editor-dialog.ui");
41
42     xml->get_widget("dialog", dialog);
43     d_tileset = tileset;
44
45     xml->get_widget("explosion_filechooserbutton", explosion_filechooserbutton);
46     explosion_filechooserbutton->signal_selection_changed().connect
47        (sigc::mem_fun(*this, &TilesetExplosionPictureEditorDialog::on_image_chosen));
48     explosion_filechooserbutton->set_current_folder(Glib::get_home_dir());
49
50     xml->get_widget("large_explosion_radiobutton", large_explosion_radiobutton);
51     large_explosion_radiobutton->signal_toggled().connect
52       (sigc::mem_fun(*this, &TilesetExplosionPictureEditorDialog::on_large_toggled));
53     xml->get_widget("small_explosion_radiobutton", small_explosion_radiobutton);
54     small_explosion_radiobutton->signal_toggled().connect
55       (sigc::mem_fun(*this, &TilesetExplosionPictureEditorDialog::on_small_toggled));
56
57     xml->get_widget("scene_image", scene_image);
58     
59     if (d_tileset->getExplosionFilename().empty() == false)
60       selected_filename = d_tileset->getFile(d_tileset->getExplosionFilename());
61     on_large_toggled();
62 }
63
64 TilesetExplosionPictureEditorDialog::~TilesetExplosionPictureEditorDialog()
65 {
66   delete dialog;
67 }
68 void TilesetExplosionPictureEditorDialog::set_parent_window(Gtk::Window &parent)
69 {
70     dialog->set_transient_for(parent);
71     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
72 }
73
74 int TilesetExplosionPictureEditorDialog::run()
75 {
76     dialog->show_all();
77     int response = dialog->run();
78
79     return response;
80 }
81
82 void TilesetExplosionPictureEditorDialog::on_image_chosen()
83 {
84   selected_filename = explosion_filechooserbutton->get_filename();
85   if (selected_filename.empty())
86     return;
87
88   show_explosion_image(selected_filename);
89 }
90
91 void TilesetExplosionPictureEditorDialog::on_large_toggled()
92 {
93   update_panel();
94 }
95
96 void TilesetExplosionPictureEditorDialog::on_small_toggled()
97 {
98   update_panel();
99 }
100
101
102 void TilesetExplosionPictureEditorDialog::update_panel()
103 {
104   if (selected_filename.empty() == false)
105     explosion_filechooserbutton->set_filename (selected_filename);
106 }
107
108 void TilesetExplosionPictureEditorDialog::show_explosion_image(std::string filename)
109 {
110   std::vector<PixMask* > tilestyle_images;
111   std::vector<PixMask*> base_tilestyles;
112   base_tilestyles = 
113     disassemble_row(File::getMiscFile("various/editor/tilestyles.png"), 17);
114
115   guint32 size = d_tileset->getTileSize();
116   std::vector<PixMask*>::iterator it;
117   for (it = base_tilestyles.begin(); it != base_tilestyles.end(); it++)
118     {
119       PixMask::scale(*it, size, size);
120       tilestyle_images.push_back(*it);
121     }
122   TilePreviewScene *s;
123   std::string scene;
124   guint32 idx = d_tileset->getIndex(Tile::GRASS);
125   Tile *grass = NULL;
126   if (d_tileset->size() > 0)
127     grass = (*d_tileset)[idx];
128   scene.clear();
129   if (large_explosion_radiobutton->get_active() == true)
130     {
131       scene += "aaaaaa";
132       scene += "aaaaaa";
133       scene += "aaaaaa";
134       scene += "aaaaaa";
135       scene += "aaaaaa";
136       scene += "aaaaaa";
137       s = new TilePreviewScene(grass, tilestyle_images, 6, 6, scene);
138       update_scene(s, filename);
139     }
140   else if (small_explosion_radiobutton->get_active() == true)
141     {
142       scene += "aaaaaaa";
143       scene += "aaaaaaa";
144       scene += "aaaaaaa";
145       scene += "aaaaaaa";
146       scene += "aaaaaaa";
147       scene += "aaaaaaa";
148       scene += "aaaaaaa";
149       s = new TilePreviewScene(grass, tilestyle_images, 7, 7, scene);
150       update_scene(s, filename);
151     }
152 }
153
154 void TilesetExplosionPictureEditorDialog::update_scene(TilePreviewScene *scene,
155                                                        std::string filename)
156 {
157   if (!scene)
158     return;
159   if (filename == "")
160     return;
161
162   Glib::RefPtr<Gdk::Pixbuf> scene_pixbuf;
163   scene_pixbuf = scene->renderScene(d_tileset->getTileSize());
164   //center the explosion image on the pixbuf
165   //but the large explosion is scaled first
166
167   Glib::RefPtr<Gdk::Pixbuf> explosion;
168   if (small_explosion_radiobutton->get_active())
169     {
170       explosion = Gdk::Pixbuf::create_from_file(filename, d_tileset->getTileSize(), d_tileset->getTileSize(), false);
171     }
172   else if (large_explosion_radiobutton->get_active())
173     {
174       explosion = Gdk::Pixbuf::create_from_file(filename, d_tileset->getTileSize() * 2, d_tileset->getTileSize() * 2, false);
175     }
176   if (explosion == NULL)
177     return;
178
179   int i = (scene_pixbuf->get_width() - explosion->get_width()) / 2;
180   int j = (scene_pixbuf->get_height() - explosion->get_height()) / 2;
181   explosion->composite (scene_pixbuf, i, j, explosion->get_width(), explosion->get_height(), i, j, 1, 1, Gdk::INTERP_NEAREST, 255);
182   scene_image->property_pixbuf() = scene_pixbuf;
183   scene_image->show_all();
184 }