1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Import Image" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44 #include <synfig/canvas.h>
48 using namespace synfig;
52 /* === M A C R O S ========================================================= */
54 /* === G L O B A L S ======================================================= */
56 SYNFIG_LAYER_INIT(Import);
57 SYNFIG_LAYER_SET_NAME(Import,"import");
58 SYNFIG_LAYER_SET_LOCAL_NAME(Import,N_("Import Image"));
59 SYNFIG_LAYER_SET_CATEGORY(Import,N_("Other"));
60 SYNFIG_LAYER_SET_VERSION(Import,"0.1");
61 SYNFIG_LAYER_SET_CVS_ID(Import,"$Id$");
63 /* === P R O C E D U R E S ================================================= */
65 /* === M E T H O D S ======================================================= */
77 Import::on_canvas_set()
79 if(get_canvas())set_param("filename",filename);
83 Import::set_param(const String & param, const ValueBase &value)
87 if(param=="filename" && value.same_type_as(filename))
91 filename=value.get(filename);
97 String newfilename=value.get(string());
98 String filename_with_path;
100 // Get rid of any %20 crap
103 while((n=newfilename.find("%20"))!=String::npos)
104 newfilename.replace(n,3," ");
107 //if(get_canvas()->get_file_path()==dirname(newfilename))
109 // synfig::info("Image seems to be in local directory. Adjusting path...");
110 // newfilename=basename(newfilename);
114 if(is_absolute_path(newfilename))
116 string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
117 while(basename(curpath)==".")curpath=dirname(curpath);
119 newfilename=relative_path(curpath,newfilename);
120 synfig::info("basename(curpath)=%s, Path adjusted to %s",basename(curpath).c_str(),newfilename.c_str());
125 filename=newfilename;
127 if(newfilename.empty())
129 filename=newfilename;
135 // If we are already loaded, don't reload
136 if(filename==newfilename && importer)
138 synfig::warning(strprintf(_("Filename seems to already be set to \"%s\" (%s)"),filename.c_str(),newfilename.c_str()));
142 assert(get_canvas());
144 if(is_absolute_path(newfilename))
145 filename_with_path=newfilename;
147 filename_with_path=get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR+newfilename;
149 handle<Importer> newimporter;
151 newimporter=Importer::open(absolute_path(filename_with_path));
155 newimporter=Importer::open(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR+basename(newfilename));
158 synfig::error(strprintf("Unable to create an importer object with file \"%s\"",filename_with_path.c_str()));
165 if(!newimporter->get_frame(surface,Time(0)))
167 synfig::warning(strprintf("Unable to get frame from \"%s\"",filename_with_path.c_str()));
170 importer=newimporter;
171 filename=newfilename;
172 abs_filename=absolute_path(filename_with_path);
176 } catch(...) { set_amount(0); return false; }
178 return Layer_Bitmap::set_param(param,value);
182 Import::get_param(const String & param)const
188 if(param=="filename")
190 string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
191 return relative_path(curpath,abs_filename);
200 return Layer_Bitmap::get_param(param);
204 Import::get_param_vocab()const
206 Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
208 ret.push_back(ParamDesc("filename")
209 .set_local_name(_("Filename"))
210 .set_description(_("File to import"))
211 .set_hint("filename")
213 ret.push_back(ParamDesc("time_offset")
214 .set_local_name(_("Time Offset"))
221 Import::set_time(Context context, Time time)const
223 if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
224 //else surface.clear();
225 context.set_time(time);
229 Import::set_time(Context context, Time time, const Point &pos)const
231 if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
232 //else surface.clear();
233 context.set_time(time,pos);