1 // Copyright (C) 2009 Ben Asselstine
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.
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.
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
21 #include <sigc++/functors/mem_fun.h>
24 #include "tileset-explosion-picture-editor-dialog.h"
26 #include "glade-helpers.h"
27 #include "gui/image-helpers.h"
28 #include "ucompose.hpp"
31 #include "GraphicsCache.h"
32 #include "tile-preview-scene.h"
35 TilesetExplosionPictureEditorDialog::TilesetExplosionPictureEditorDialog(Tileset *tileset)
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");
42 xml->get_widget("dialog", dialog);
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());
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));
57 xml->get_widget("scene_image", scene_image);
59 if (d_tileset->getExplosionFilename().empty() == false)
60 selected_filename = d_tileset->getFile(d_tileset->getExplosionFilename());
64 TilesetExplosionPictureEditorDialog::~TilesetExplosionPictureEditorDialog()
68 void TilesetExplosionPictureEditorDialog::set_parent_window(Gtk::Window &parent)
70 dialog->set_transient_for(parent);
71 //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
74 int TilesetExplosionPictureEditorDialog::run()
77 int response = dialog->run();
82 void TilesetExplosionPictureEditorDialog::on_image_chosen()
84 selected_filename = explosion_filechooserbutton->get_filename();
85 if (selected_filename.empty())
88 show_explosion_image(selected_filename);
91 void TilesetExplosionPictureEditorDialog::on_large_toggled()
96 void TilesetExplosionPictureEditorDialog::on_small_toggled()
102 void TilesetExplosionPictureEditorDialog::update_panel()
104 if (selected_filename.empty() == false)
105 explosion_filechooserbutton->set_filename (selected_filename);
108 void TilesetExplosionPictureEditorDialog::show_explosion_image(std::string filename)
110 std::vector<PixMask* > tilestyle_images;
111 std::vector<PixMask*> base_tilestyles;
113 disassemble_row(File::getMiscFile("various/editor/tilestyles.png"), 17);
115 guint32 size = d_tileset->getTileSize();
116 std::vector<PixMask*>::iterator it;
117 for (it = base_tilestyles.begin(); it != base_tilestyles.end(); it++)
119 PixMask::scale(*it, size, size);
120 tilestyle_images.push_back(*it);
124 guint32 idx = d_tileset->getIndex(Tile::GRASS);
126 if (d_tileset->size() > 0)
127 grass = (*d_tileset)[idx];
129 if (large_explosion_radiobutton->get_active() == true)
137 s = new TilePreviewScene(grass, tilestyle_images, 6, 6, scene);
138 update_scene(s, filename);
140 else if (small_explosion_radiobutton->get_active() == true)
149 s = new TilePreviewScene(grass, tilestyle_images, 7, 7, scene);
150 update_scene(s, filename);
154 void TilesetExplosionPictureEditorDialog::update_scene(TilePreviewScene *scene,
155 std::string filename)
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
167 Glib::RefPtr<Gdk::Pixbuf> explosion;
168 if (small_explosion_radiobutton->get_active())
170 explosion = Gdk::Pixbuf::create_from_file(filename, d_tileset->getTileSize(), d_tileset->getTileSize(), false);
172 else if (large_explosion_radiobutton->get_active())
174 explosion = Gdk::Pixbuf::create_from_file(filename, d_tileset->getTileSize() * 2, d_tileset->getTileSize() * 2, false);
176 if (explosion == NULL)
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();