1 /* === S Y N F I G ========================================================= */
3 ** \brief Layer class implementation
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 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 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
37 #include "layer_bitmap.h"
38 #include "layer_mime.h"
40 #include "paramdesc.h"
42 #include "layer_solidcolor.h"
43 #include "layer_polygon.h"
44 #include "layer_pastecanvas.h"
45 #include "layer_motionblur.h"
46 #include "layer_duplicate.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) \
99 synfig::Layer::book() [synfig::String(class::name__)] = \
100 BookEntry(class::create, \
102 dgettext("synfig", class::local_name__), \
107 #define LAYER_ALIAS(class,alias) \
108 synfig::Layer::book()[synfig::String(alias)] = \
109 BookEntry(class::create, \
112 CATEGORY_DO_NOT_USE, \
116 INCLUDE_LAYER(Layer_SolidColor); LAYER_ALIAS(Layer_SolidColor, "solid_color");
117 INCLUDE_LAYER(Layer_PasteCanvas); LAYER_ALIAS(Layer_PasteCanvas, "paste_canvas");
118 INCLUDE_LAYER(Layer_Polygon); LAYER_ALIAS(Layer_Polygon, "Polygon");
119 INCLUDE_LAYER(Layer_MotionBlur); LAYER_ALIAS(Layer_MotionBlur, "motion_blur");
120 INCLUDE_LAYER(Layer_Duplicate);
134 /* === M E T H O D S ======================================================= */
139 dirty_time_(Time::end()),
140 z_depth_static(false)
142 _LayerCounter::counter++;
146 synfig::Layer::create(const String &name)
148 if(!book().count(name))
150 return Layer::LooseHandle(new Layer_Mime(name));
153 Layer* layer(book()[name].factory());
154 return Layer::LooseHandle(layer);
157 synfig::Layer::~Layer()
159 _LayerCounter::counter--;
160 while(!dynamic_param_list_.empty())
162 remove_child(dynamic_param_list_.begin()->second.get());
163 dynamic_param_list_.erase(dynamic_param_list_.begin());
166 remove_from_all_groups();
168 parent_death_connect_.disconnect();
173 synfig::Layer::set_canvas(etl::loose_handle<Canvas> x)
177 parent_death_connect_.disconnect();
181 parent_death_connect_=x->signal_deleted().connect(
187 etl::loose_handle<synfig::Canvas>(0)
196 synfig::Layer::on_canvas_set()
200 etl::loose_handle<synfig::Canvas>
201 synfig::Layer::get_canvas()const
207 Layer::get_depth()const
211 return get_canvas()->get_depth(const_cast<synfig::Layer*>(this));
215 Layer::set_active(bool x)
222 signal_status_changed_();
227 Layer::set_description(const String& x)
232 signal_description_changed_();
237 Layer::connect_dynamic_param(const String& param, etl::loose_handle<ValueNode> value_node)
239 ValueNode::Handle previous(dynamic_param_list_[param]);
241 if(previous==value_node)
244 dynamic_param_list_[param]=ValueNode::Handle(value_node);
247 remove_child(previous.get());
249 add_child(value_node.get());
251 if(!value_node->is_exported() && get_canvas())
253 value_node->set_parent_canvas(get_canvas());
261 Layer::disconnect_dynamic_param(const String& param)
263 ValueNode::Handle previous(dynamic_param_list_[param]);
267 dynamic_param_list_.erase(param);
269 // fix 2353284: if two parameters in the same layer are
270 // connected to the same valuenode and we disconnect one of
271 // them, the parent-child relationship for the remaining
272 // connection was being deleted. now we search the parameter
273 // list to see if another parameter uses the same valuenode
274 DynamicParamList::const_iterator iter;
275 for (iter = dynamic_param_list().begin(); iter != dynamic_param_list().end(); iter++)
276 if (iter->second == previous)
278 if (iter == dynamic_param_list().end())
279 remove_child(previous.get());
289 dirty_time_=Time::end();
294 Layer::set_param(const String ¶m, const ValueBase &value)
301 Layer::set_param_static(const String ¶m, const bool x)
303 SET_STATIC(z_depth,x)
310 Layer::get_param_static(const String ¶m) const
318 etl::handle<Transform>
319 Layer::get_transform()const
325 Layer::get_z_depth(const synfig::Time& t)const
327 if(!dynamic_param_list().count("z_depth"))
329 return (*dynamic_param_list().find("z_depth")->second)(t).get(Real());
333 Layer::simple_clone()const
335 if(!book().count(get_name())) return 0;
336 Handle ret = create(get_name()).get();
338 //ret->set_canvas(get_canvas());
339 ret->set_description(get_description());
340 ret->set_active(active());
341 ret->set_param_list(get_param_list());
342 for(DynamicParamList::const_iterator iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
343 ret->connect_dynamic_param(iter->first, iter->second);
348 Layer::clone(const GUID& deriv_guid) const
350 if(!book().count(get_name())) return 0;
352 //Layer *ret = book()[get_name()].factory();//create(get_name()).get();
353 Handle ret = create(get_name()).get();
356 //ret->set_canvas(get_canvas());
357 ret->set_description(get_description());
358 ret->set_active(active());
359 ret->set_guid(get_guid()^deriv_guid);
361 //ret->set_param_list(get_param_list());
362 // Process the parameter list so that
363 // we can duplicate any inline canvases
364 ParamList param_list(get_param_list());
365 for(ParamList::const_iterator iter(param_list.begin()); iter != param_list.end(); ++iter)
367 if(dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
369 // This parameter is a canvas. We need a close look.
370 Canvas::Handle canvas(iter->second.get(Canvas::Handle()));
371 if(canvas && canvas->is_inline())
373 // This parameter is an inline canvas! we need to clone it
374 // before we set it as a parameter.
375 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
376 ValueBase value(new_canvas);
377 ret->set_param(iter->first, value);
382 // This is a normal parameter,go ahead and set it.
383 ret->set_param(iter->first, iter->second);
386 // Duplicate the dynamic paramlist, but only the exported data nodes
387 DynamicParamList::const_iterator iter;
388 for(iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
390 // Make sure we clone inline canvases
391 if(iter->second->get_type()==ValueBase::TYPE_CANVAS)
393 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
394 if(canvas->is_inline())
396 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
397 ValueBase value(new_canvas);
398 ret->connect_dynamic_param(iter->first,ValueNode_Const::create(value));
403 if(iter->second->is_exported())
404 ret->connect_dynamic_param(iter->first,iter->second);
406 ret->connect_dynamic_param(iter->first,iter->second->clone(deriv_guid));
409 //ret->set_canvas(0);
415 Layer::reads_context() const
421 Layer::get_full_bounding_rect(Context context)const
424 return context.get_full_bounding_rect()|get_bounding_rect();
425 return context.get_full_bounding_rect();
429 Layer::get_bounding_rect()const
431 return Rect::full_plane();
435 Layer::set_param_list(const ParamList &list)
440 ParamList::const_iterator iter(list.begin());
441 for(;iter!=list.end();++iter)
443 if(!set_param(iter->first, iter->second))ret=false;
449 Layer::get_param_list()const
453 Vocab vocab(get_param_vocab());
455 Vocab::const_iterator iter=vocab.begin();
456 for(;iter!=vocab.end();++iter)
458 ret[iter->get_name()]=get_param(iter->get_name());
464 Layer::get_param(const String & param)const
468 synfig::ValueBase ret(get_z_depth());
469 ret.set_static(z_depth_static);
476 Layer::get_version()const
478 return get_param("version__").get(String());
482 Layer::set_version(const String &/*ver*/)
488 Layer::reset_version()
494 Layer::set_time(Context context, Time time)const
496 context.set_time(time);
501 Layer::set_time(Context context, Time time, const Point &pos)const
503 context.set_time(time,pos);
508 Layer::get_color(Context context, const Point &pos)const
510 return context.get_color(pos);
513 synfig::Layer::Handle
514 Layer::hit_check(synfig::Context context, const synfig::Point &pos)const
516 return context.hit_check(pos);
519 /* The default accelerated renderer
520 ** is anything but accelerated...
523 Layer::accelerated_render(Context context,Surface *surface,int /*quality*/, const RendDesc &renddesc, ProgressCallback *cb) const
525 handle<Target> target=surface_target(surface);
528 if(cb)cb->error(_("Unable to create surface target"));
531 RendDesc desc=renddesc;
532 target->set_rend_desc(&desc);
534 // When we render, we want to
535 // make sure that we are rendered too...
536 // Since the context iterator is for
537 // the layer after us, we need to back up.
538 // This could be considered a hack, as
539 // it is a possibility that we are indeed
540 // not the previous layer.
543 return render(context,target,desc,cb);
544 //return render_threaded(context,target,desc,cb,2);
548 Layer::get_name()const
550 return get_param("name__").get(String());
554 Layer::get_local_name()const
556 return get_param("local_name__").get(String());
561 Layer::get_param_vocab()const
565 ret.push_back(ParamDesc(z_depth,"z_depth")
566 .set_local_name(_("Z Depth"))
567 .set_animation_only(true)
574 Layer::get_times_vfunc(Node::time_set &set) const
576 DynamicParamList::const_iterator i = dynamic_param_list_.begin(),
577 end = dynamic_param_list_.end();
581 const Node::time_set &tset = i->second->get_times();
582 set.insert(tset.begin(),tset.end());
588 Layer::add_to_group(const String&x)
593 remove_from_all_groups();
595 signal_added_to_group()(group_);
599 Layer::remove_from_group(const String&x)
602 remove_from_all_groups();
606 Layer::remove_from_all_groups()
610 signal_removed_from_group()(group_);
615 Layer::get_group()const
621 Layer::get_param_local_name(const String ¶m_name)const
623 ParamVocab vocab = get_param_vocab();
624 // loop to find the parameter in the parameter vocab - this gives us its local name
625 for (ParamVocab::iterator iter = vocab.begin(); iter != vocab.end(); iter++)
626 if (iter->get_name() == param_name)
627 return iter->get_local_name();