pentpal.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2004 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 
00021 #include <gtk/gtk.h>
00022 
00023 #include <libgimp/gimp.h>
00024 #include <libgimp/gimpui.h>
00025 
00026 #include "pentpal.h"
00027 
00028 #include "FileSystem.h"
00029 #include "Palette.h"
00030 #include "ConvertShape.h"
00031 #include "crusader/ConvertShapeCrusader.h"
00032 #include "u8/ConvertShapeU8.h"
00033 
00034 bool loadPalette(Pentagram::Palette * pal, const ConvertShapeFormat * format)
00035 {
00036         GimpRGB color;
00037         guchar palHack[2];
00038         palHack[1] = 255;
00039         bool newPal;
00040         const gchar * palName;
00041 
00042         for (int i = 0; i < 256; ++i)
00043         {
00044                 palHack[0] = i;
00045                 pal->native[i] = *((uint16 * ) palHack);
00046         }
00047 
00048         if (format == &PentagramShapeFormat)
00049         {
00050                 palName = "Pentagram";
00051         }
00052         else if (format == &U8SKFShapeFormat)
00053         {
00054                 palName = "U8SKF";
00055         }
00056         else if (format == &U8ShapeFormat)
00057         {
00058                 palName = "Ultima8";
00059         }
00060         else if (format == &U82DShapeFormat)
00061         {
00062                 palName = "Ultima8";
00063         }
00064         else if (format == &CrusaderShapeFormat)
00065         {
00066                 palName = "Crusader";
00067         }
00068         else if (format == &Crusader2DShapeFormat)
00069         {
00070                 palName = "Crusader";
00071         }
00072         else
00073         {
00074                 return false;
00075         }
00076 
00077         newPal = ! gimp_context_set_palette(palName);
00078         if (newPal)
00079         {
00080                 gint colors = 0;
00081                 if (gimp_palette_get_info(palName, &colors))
00082                 {
00083                         newPal = false;
00084                         if (colors != 256)
00085                         {
00086                                 newPal = true;
00087                                 if (!gimp_palette_delete(palName))
00088                                         return false;
00089                         }
00090                 }
00091         }
00092 
00093         if (newPal)
00094         {
00095                 if (g_ascii_strcasecmp(palName, gimp_palette_new(palName)))
00096                         return false;
00097                 if (!gimp_context_set_palette(palName))
00098                         return false;
00099 
00100                 IDataSource * ds = choosePalette();
00101                 if (ds)
00102                 {
00103                         gint j = 0;
00104                         ds->seek(4);
00105                         pal->load(*ds);
00106                         delete ds;
00107                         for (gint i = 0; i < 256; ++i)
00108                         {
00109                                 gimp_rgb_set_uchar(&color,
00110                                                         pal->palette[i * 3],
00111                                                         pal->palette[i * 3 + 1],
00112                                                         pal->palette[i * 3 + 2]);
00113                                 gimp_palette_add_entry(palName, "Untitled", &color, &j);
00114                                 assert (j == i);
00115                         }
00116                 }
00117                 else
00118                 {
00119                         return false;
00120                 }
00121         }
00122         else
00123         {
00124                 for (gint i = 0; i < 256; ++i)
00125                 {
00126                         gimp_palette_entry_get_color(palName, i, &color);
00127                         gimp_rgb_get_uchar(&color,
00128                                                 &(pal->palette[i * 3]),
00129                                                 &(pal->palette[i * 3 + 1]),
00130                                                 &(pal->palette[i * 3 + 2]));
00131                 }
00132         }
00133 
00134         return true;
00135 }
00136 
00137 static void file_sel_delete( GtkWidget *widget, GtkWidget **file_sel )
00138 {
00139         gtk_widget_destroy( *file_sel );
00140         *file_sel = NULL;
00141 }
00142 
00143 static void file_selected( GtkWidget *widget, gboolean *selected )
00144 {
00145         *selected = TRUE;
00146 }
00147 
00148 IDataSource * choosePalette()
00149 {
00150         FileSystem * filesys = FileSystem::get_instance();
00151         GtkWidget *file_sel;
00152         gchar *filename;
00153         gboolean selected = FALSE;
00154 
00155         file_sel = gtk_file_selection_new( "Choose a Palette" );
00156         gtk_window_set_modal( GTK_WINDOW( file_sel ), TRUE );
00157 
00158         gtk_signal_connect( GTK_OBJECT( file_sel ), "destroy",
00159                             GTK_SIGNAL_FUNC( file_sel_delete ), &file_sel );
00160 
00161         gtk_signal_connect( GTK_OBJECT( GTK_FILE_SELECTION( file_sel )->cancel_button ), "clicked", GTK_SIGNAL_FUNC( file_sel_delete ), &file_sel );
00162 
00163         gtk_signal_connect( GTK_OBJECT( GTK_FILE_SELECTION( file_sel )->ok_button ), "clicked", GTK_SIGNAL_FUNC( file_selected ), &selected );
00164 
00165         gtk_widget_show( file_sel );
00166 
00167         while( ( ! selected ) && ( file_sel ) )
00168                 gtk_main_iteration();
00169 
00170         /* canceled or window was closed */
00171         if( ! selected )
00172                 return 0;
00173 
00174         /* ok */
00175         filename = g_strdup( gtk_file_selection_get_filename( GTK_FILE_SELECTION( file_sel ) ) );
00176         gtk_widget_destroy( file_sel );
00177 
00178         IDataSource * ds = filesys->ReadFile(filename);
00179         return ds;
00180 }
00181 

Generated on Fri Jul 27 22:27:29 2007 for pentagram by  doxygen 1.4.7