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())
141 _LayerCounter::counter++;
145 synfig::Layer::create(const String &name)
147 if(!book().count(name))
149 return Layer::LooseHandle(new Layer_Mime(name));
152 Layer* layer(book()[name].factory());
153 return Layer::LooseHandle(layer);
156 synfig::Layer::~Layer()
158 _LayerCounter::counter--;
159 while(!dynamic_param_list_.empty())
161 remove_child(dynamic_param_list_.begin()->second.get());
162 dynamic_param_list_.erase(dynamic_param_list_.begin());
165 remove_from_all_groups();
167 parent_death_connect_.disconnect();
172 synfig::Layer::set_canvas(etl::loose_handle<Canvas> x)
176 parent_death_connect_.disconnect();
180 parent_death_connect_=x->signal_deleted().connect(
186 etl::loose_handle<synfig::Canvas>(0)
195 synfig::Layer::on_canvas_set()
199 etl::loose_handle<synfig::Canvas>
200 synfig::Layer::get_canvas()const
206 Layer::get_depth()const
210 return get_canvas()->get_depth(const_cast<synfig::Layer*>(this));
214 Layer::set_active(bool x)
221 signal_status_changed_();
226 Layer::set_description(const String& x)
231 signal_description_changed_();
236 Layer::connect_dynamic_param(const String& param, etl::loose_handle<ValueNode> value_node)
238 ValueNode::Handle previous(dynamic_param_list_[param]);
240 if(previous==value_node)
243 dynamic_param_list_[param]=ValueNode::Handle(value_node);
246 remove_child(previous.get());
248 add_child(value_node.get());
250 if(!value_node->is_exported() && get_canvas())
252 value_node->set_parent_canvas(get_canvas());
260 Layer::disconnect_dynamic_param(const String& param)
262 ValueNode::Handle previous(dynamic_param_list_[param]);
266 dynamic_param_list_.erase(param);
268 // fix 2353284: if two parameters in the same layer are
269 // connected to the same valuenode and we disconnect one of
270 // them, the parent-child relationship for the remaining
271 // connection was being deleted. now we search the parameter
272 // list to see if another parameter uses the same valuenode
273 DynamicParamList::const_iterator iter;
274 for (iter = dynamic_param_list().begin(); iter != dynamic_param_list().end(); iter++)
275 if (iter->second == previous)
277 if (iter == dynamic_param_list().end())
278 remove_child(previous.get());
288 dirty_time_=Time::end();
293 Layer::set_param(const String ¶m, const ValueBase &value)
295 if(param=="z_depth" && value.same_type_as(z_depth_))
297 z_depth_=value.get(z_depth_);
303 etl::handle<Transform>
304 Layer::get_transform()const
310 Layer::get_z_depth(const synfig::Time& t)const
312 if(!dynamic_param_list().count("z_depth"))
314 return (*dynamic_param_list().find("z_depth")->second)(t).get(Real());
318 Layer::simple_clone()const
320 if(!book().count(get_name())) return 0;
321 Handle ret = create(get_name()).get();
323 //ret->set_canvas(get_canvas());
324 ret->set_description(get_description());
325 ret->set_active(active());
326 ret->set_param_list(get_param_list());
327 for(DynamicParamList::const_iterator iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
328 ret->connect_dynamic_param(iter->first, iter->second);
333 Layer::clone(const GUID& deriv_guid) const
335 if(!book().count(get_name())) return 0;
337 //Layer *ret = book()[get_name()].factory();//create(get_name()).get();
338 Handle ret = create(get_name()).get();
341 //ret->set_canvas(get_canvas());
342 ret->set_description(get_description());
343 ret->set_active(active());
344 ret->set_guid(get_guid()^deriv_guid);
346 //ret->set_param_list(get_param_list());
347 // Process the parameter list so that
348 // we can duplicate any inline canvases
349 ParamList param_list(get_param_list());
350 for(ParamList::const_iterator iter(param_list.begin()); iter != param_list.end(); ++iter)
352 if(dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
354 // This parameter is a canvas. We need a close look.
355 Canvas::Handle canvas(iter->second.get(Canvas::Handle()));
356 if(canvas && canvas->is_inline())
358 // This parameter is an inline canvas! we need to clone it
359 // before we set it as a parameter.
360 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
361 ValueBase value(new_canvas);
362 ret->set_param(iter->first, value);
367 // This is a normal parameter,go ahead and set it.
368 ret->set_param(iter->first, iter->second);
371 // Duplicate the dynamic paramlist, but only the exported data nodes
372 DynamicParamList::const_iterator iter;
373 for(iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
375 // Make sure we clone inline canvases
376 if(iter->second->get_type()==ValueBase::TYPE_CANVAS)
378 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
379 if(canvas->is_inline())
381 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
382 ValueBase value(new_canvas);
383 ret->connect_dynamic_param(iter->first,ValueNode_Const::create(value));
388 if(iter->second->is_exported())
389 ret->connect_dynamic_param(iter->first,iter->second);
391 ret->connect_dynamic_param(iter->first,iter->second->clone(deriv_guid));
394 //ret->set_canvas(0);
400 Layer::reads_context() const
406 Layer::get_full_bounding_rect(Context context)const
409 return context.get_full_bounding_rect()|get_bounding_rect();
410 return context.get_full_bounding_rect();
414 Layer::get_bounding_rect()const
416 return Rect::full_plane();
420 Layer::set_param_list(const ParamList &list)
425 ParamList::const_iterator iter(list.begin());
426 for(;iter!=list.end();++iter)
428 if(!set_param(iter->first, iter->second))ret=false;
434 Layer::get_param_list()const
438 Vocab vocab(get_param_vocab());
440 Vocab::const_iterator iter=vocab.begin();
441 for(;iter!=vocab.end();++iter)
443 ret[iter->get_name()]=get_param(iter->get_name());
449 Layer::get_param(const String & param)const
452 return get_z_depth();
458 Layer::get_version()const
460 return get_param("version__").get(String());
464 Layer::set_version(const String &/*ver*/)
470 Layer::reset_version()
476 Layer::set_time(Context context, Time time)const
478 context.set_time(time);
483 Layer::set_time(Context context, Time time, const Point &pos)const
485 context.set_time(time,pos);
490 Layer::get_color(Context context, const Point &pos)const
492 return context.get_color(pos);
495 synfig::Layer::Handle
496 Layer::hit_check(synfig::Context context, const synfig::Point &pos)const
498 return context.hit_check(pos);
501 /* The default accelerated renderer
502 ** is anything but accelerated...
505 Layer::accelerated_render(Context context,Surface *surface,int /*quality*/, const RendDesc &renddesc, ProgressCallback *cb) const
507 handle<Target> target=surface_target(surface);
510 if(cb)cb->error(_("Unable to create surface target"));
513 RendDesc desc=renddesc;
514 target->set_rend_desc(&desc);
516 // When we render, we want to
517 // make sure that we are rendered too...
518 // Since the context iterator is for
519 // the layer after us, we need to back up.
520 // This could be considered a hack, as
521 // it is a possibility that we are indeed
522 // not the previous layer.
525 return render(context,target,desc,cb);
526 //return render_threaded(context,target,desc,cb,2);
530 Layer::get_name()const
532 return get_param("name__").get(String());
536 Layer::get_local_name()const
538 return get_param("local_name__").get(String());
543 Layer::get_param_vocab()const
547 ret.push_back(ParamDesc(z_depth_,"z_depth")
548 .set_local_name(_("Z Depth"))
549 .set_animation_only(true)
556 Layer::get_times_vfunc(Node::time_set &set) const
558 DynamicParamList::const_iterator i = dynamic_param_list_.begin(),
559 end = dynamic_param_list_.end();
563 const Node::time_set &tset = i->second->get_times();
564 set.insert(tset.begin(),tset.end());
570 Layer::add_to_group(const String&x)
575 remove_from_all_groups();
577 signal_added_to_group()(group_);
581 Layer::remove_from_group(const String&x)
584 remove_from_all_groups();
588 Layer::remove_from_all_groups()
592 signal_removed_from_group()(group_);
597 Layer::get_group()const
603 Layer::get_param_local_name(const String ¶m_name)const
605 ParamVocab vocab = get_param_vocab();
606 // loop to find the parameter in the parameter vocab - this gives us its local name
607 for (ParamVocab::iterator iter = vocab.begin(); iter != vocab.end(); iter++)
608 if (iter->get_name() == param_name)
609 return iter->get_local_name();