1 /* === S Y N F I G ========================================================= */
3 ** \brief Layer class implementation
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
25 #define SYNFIG_NO_ANGLE
38 #include "layer_bitmap.h"
39 #include "layer_mime.h"
41 #include "paramdesc.h"
43 #include "layer_solidcolor.h"
44 #include "layer_polygon.h"
45 #include "layer_pastecanvas.h"
46 #include "layer_motionblur.h"
48 #include "valuenode_const.h"
50 #include "transform.h"
54 #include <sigc++/adaptors/bind.h>
57 /* === U S I N G =========================================================== */
61 using namespace synfig;
63 /* === G L O B A L S ======================================================= */
65 static Layer::Book* _layer_book;
73 synfig::error("%d layers not yet deleted!",counter);
77 int _LayerCounter::counter(0);
79 /* === P R O C E D U R E S ================================================= */
88 Layer::register_in_book(const BookEntry &entry)
90 book()[entry.name]=entry;
96 _layer_book=new Book();
98 #define INCLUDE_LAYER(class) synfig::Layer::book()[synfig::String(class::name__)]=BookEntry(class::create,class::name__,class::local_name__,class::category__,class::cvs_id__,class::version__)
99 #define LAYER_ALIAS(class,alias) synfig::Layer::book()[synfig::String(alias)]=synfig::Layer::BookEntry(class::create,alias,alias,_("Do Not Use"),class::cvs_id__,class::version__);
101 INCLUDE_LAYER(Layer_SolidColor); LAYER_ALIAS(Layer_SolidColor, "solid_color");
102 INCLUDE_LAYER(Layer_PasteCanvas); LAYER_ALIAS(Layer_PasteCanvas, "paste_canvas");
103 INCLUDE_LAYER(Layer_Polygon); LAYER_ALIAS(Layer_Polygon, "Polygon");
104 INCLUDE_LAYER(Layer_MotionBlur); LAYER_ALIAS(Layer_MotionBlur, "motion_blur");
118 /* === M E T H O D S ======================================================= */
123 dirty_time_(Time::end())
125 _LayerCounter::counter++;
129 synfig::Layer::create(const String &name)
131 if(!book().count(name))
133 return Layer::LooseHandle(new Layer_Mime(name));
136 Layer* layer(book()[name].factory());
137 return Layer::LooseHandle(layer);
140 synfig::Layer::~Layer()
142 _LayerCounter::counter--;
143 while(!dynamic_param_list_.empty())
145 remove_child(dynamic_param_list_.begin()->second.get());
146 dynamic_param_list_.erase(dynamic_param_list_.begin());
149 remove_from_all_groups();
151 parent_death_connect_.disconnect();
156 synfig::Layer::set_canvas(etl::loose_handle<Canvas> x)
160 parent_death_connect_.disconnect();
164 parent_death_connect_=x->signal_deleted().connect(
170 etl::loose_handle<synfig::Canvas>(0)
179 synfig::Layer::on_canvas_set()
183 etl::loose_handle<synfig::Canvas>
184 synfig::Layer::get_canvas()const
190 Layer::get_depth()const
194 return get_canvas()->get_depth(const_cast<synfig::Layer*>(this));
198 Layer::set_active(bool x)
205 signal_status_changed_();
210 Layer::set_description(const String& x)
215 signal_description_changed_();
220 Layer::connect_dynamic_param(const String& param, etl::loose_handle<ValueNode> value_node)
222 ValueNode::Handle previous(dynamic_param_list_[param]);
224 if(previous==value_node)
227 dynamic_param_list_[param]=ValueNode::Handle(value_node);
230 remove_child(previous.get());
232 add_child(value_node.get());
234 if(!value_node->is_exported() && get_canvas())
236 value_node->set_parent_canvas(get_canvas());
244 Layer::disconnect_dynamic_param(const String& param)
246 ValueNode::Handle previous(dynamic_param_list_[param]);
250 dynamic_param_list_.erase(param);
251 remove_child(previous.get());
260 dirty_time_=Time::end();
266 Layer::set_param(const String ¶m, const ValueBase &value)
268 if(param=="z_depth" && value.same_type_as(z_depth_))
270 z_depth_=value.get(z_depth_);
276 etl::handle<Transform>
277 Layer::get_transform()const
283 Layer::get_z_depth(const synfig::Time& t)const
285 if(!dynamic_param_list().count("z_depth"))
287 return (*dynamic_param_list().find("z_depth")->second)(t).get(Real());
291 Layer::simple_clone()const
293 if(!book().count(get_name())) return 0;
294 Layer *ret = create(get_name()).get();
295 ret->set_canvas(get_canvas());
296 ret->set_description(get_description());
297 ret->set_param_list(get_param_list());
302 Layer::clone(const GUID& deriv_guid) const
304 if(!book().count(get_name())) return 0;
306 //Layer *ret = book()[get_name()].factory();//create(get_name()).get();
307 Handle ret = create(get_name()).get();
310 //ret->set_canvas(get_canvas());
311 ret->set_description(get_description());
312 ret->set_active(active());
313 ret->set_guid(get_guid()^deriv_guid);
315 //ret->set_param_list(get_param_list());
316 // Process the parameter list so that
317 // we can duplicate any inline canvases
318 ParamList param_list(get_param_list());
319 for(ParamList::const_iterator iter(param_list.begin()); iter != param_list.end(); ++iter)
321 if(dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
324 // This parameter is a canvas. We need a close look.
325 Canvas::Handle canvas(iter->second.get(Canvas::Handle()));
326 if(canvas && canvas->is_inline())
328 // This parameter is an inline canvas! we need to clone it
329 // before we set it as a parameter.
330 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
331 ValueBase value(new_canvas);
332 ret->set_param(iter->first, value);
337 // This is a normal parameter,go ahead and set it.
338 ret->set_param(iter->first, iter->second);
341 // Duplicate the dynamic paramlist, but only the exported data nodes
342 DynamicParamList::const_iterator iter;
343 for(iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
345 // Make sure we clone inline canvases
346 if(iter->second->get_type()==ValueBase::TYPE_CANVAS)
348 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
349 if(canvas->is_inline())
351 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
352 ValueBase value(new_canvas);
353 ret->connect_dynamic_param(iter->first,ValueNode_Const::create(value));
358 if(iter->second->is_exported())
359 ret->connect_dynamic_param(iter->first,iter->second);
361 ret->connect_dynamic_param(iter->first,iter->second->clone(deriv_guid));
364 //ret->set_canvas(0);
370 Layer::get_full_bounding_rect(Context context)const
373 return context.get_full_bounding_rect()|get_bounding_rect();
374 return context.get_full_bounding_rect();
378 Layer::get_bounding_rect()const
380 return Rect::full_plane();
384 Layer::set_param_list(const ParamList &list)
389 ParamList::const_iterator iter(list.begin());
390 for(;iter!=list.end();++iter)
392 if(!set_param(iter->first, iter->second))ret=false;
398 Layer::get_param_list()const
402 Vocab vocab(get_param_vocab());
404 Vocab::const_iterator iter=vocab.begin();
405 for(;iter!=vocab.end();++iter)
407 ret[iter->get_name()]=get_param(iter->get_name());
413 Layer::get_param(const String & param)const
416 return get_z_depth();
422 Layer::get_version()const
424 return get_param("version__").get(String());
428 Layer::set_version(const String &/*ver*/)
434 Layer::reset_version()
440 Layer::set_time(Context context, Time time)const
442 context.set_time(time);
447 Layer::set_time(Context context, Time time, const Point &pos)const
449 context.set_time(time,pos);
454 Layer::get_color(Context context, const Point &pos)const
456 return context.get_color(pos);
459 synfig::Layer::Handle
460 Layer::hit_check(synfig::Context context, const synfig::Point &pos)const
462 return context.hit_check(pos);
465 /* The default accelerated renderer
466 ** is anything but accelerated...
469 Layer::accelerated_render(Context context,Surface *surface,int /*quality*/, const RendDesc &renddesc, ProgressCallback *cb) const
471 handle<Target> target=surface_target(surface);
474 if(cb)cb->error(_("Unable to create surface target"));
477 RendDesc desc=renddesc;
478 target->set_rend_desc(&desc);
480 // When we render, we want to
481 // make sure that we are rendered too...
482 // Since the context iterator is for
483 // the layer after us, we need to back up.
484 // This could be considered a hack, as
485 // it is a possibility that we are indeed
486 // not the previous layer.
489 return render(context,target,desc,cb);
490 //return render_threaded(context,target,desc,cb,2);
494 Layer::get_name()const
496 return get_param("name__").get(String());
500 Layer::get_local_name()const
502 return get_param("local_name__").get(String());
507 Layer::get_param_vocab()const
511 ret.push_back(ParamDesc(z_depth_,"z_depth")
512 .set_local_name(_("Z Depth"))
513 .set_animation_only(true)
520 Layer::get_times_vfunc(Node::time_set &set) const
522 DynamicParamList::const_iterator i = dynamic_param_list_.begin(),
523 end = dynamic_param_list_.end();
527 const Node::time_set &tset = i->second->get_times();
528 set.insert(tset.begin(),tset.end());
534 Layer::add_to_group(const String&x)
539 remove_from_all_groups();
541 signal_added_to_group()(group_);
545 Layer::remove_from_group(const String&x)
548 remove_from_all_groups();
552 Layer::remove_from_all_groups()
556 signal_removed_from_group()(group_);
561 Layer::get_group()const