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 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 ======================================================= */
26 #define SYNFIG_NO_ANGLE
39 #include "layer_bitmap.h"
40 #include "layer_mime.h"
42 #include "paramdesc.h"
44 #include "layer_solidcolor.h"
45 #include "layer_polygon.h"
46 #include "layer_pastecanvas.h"
47 #include "layer_motionblur.h"
48 #include "layer_duplicate.h"
50 #include "valuenode_const.h"
52 #include "transform.h"
56 #include <sigc++/adaptors/bind.h>
59 /* === U S I N G =========================================================== */
63 using namespace synfig;
65 /* === G L O B A L S ======================================================= */
67 static Layer::Book* _layer_book;
75 synfig::error("%d layers not yet deleted!",counter);
79 int _LayerCounter::counter(0);
81 /* === P R O C E D U R E S ================================================= */
90 Layer::register_in_book(const BookEntry &entry)
92 book()[entry.name]=entry;
98 _layer_book=new Book();
100 #define INCLUDE_LAYER(class) synfig::Layer::book()[synfig::String(class::name__)]=BookEntry(class::create,class::name__,dgettext("synfig",class::local_name__),dgettext("synfig",class::category__),class::cvs_id__,class::version__)
101 #define LAYER_ALIAS(class,alias) synfig::Layer::book()[synfig::String(alias)]=synfig::Layer::BookEntry(class::create,alias,alias,CATEGORY_DO_NOT_USE,class::cvs_id__,class::version__);
103 INCLUDE_LAYER(Layer_SolidColor); LAYER_ALIAS(Layer_SolidColor, "solid_color");
104 INCLUDE_LAYER(Layer_PasteCanvas); LAYER_ALIAS(Layer_PasteCanvas, "paste_canvas");
105 INCLUDE_LAYER(Layer_Polygon); LAYER_ALIAS(Layer_Polygon, "Polygon");
106 INCLUDE_LAYER(Layer_MotionBlur); LAYER_ALIAS(Layer_MotionBlur, "motion_blur");
107 INCLUDE_LAYER(Layer_Duplicate);
121 /* === M E T H O D S ======================================================= */
126 dirty_time_(Time::end())
128 _LayerCounter::counter++;
132 synfig::Layer::create(const String &name)
134 if(!book().count(name))
136 return Layer::LooseHandle(new Layer_Mime(name));
139 Layer* layer(book()[name].factory());
140 return Layer::LooseHandle(layer);
143 synfig::Layer::~Layer()
145 _LayerCounter::counter--;
146 while(!dynamic_param_list_.empty())
148 remove_child(dynamic_param_list_.begin()->second.get());
149 dynamic_param_list_.erase(dynamic_param_list_.begin());
152 remove_from_all_groups();
154 parent_death_connect_.disconnect();
159 synfig::Layer::set_canvas(etl::loose_handle<Canvas> x)
163 parent_death_connect_.disconnect();
167 parent_death_connect_=x->signal_deleted().connect(
173 etl::loose_handle<synfig::Canvas>(0)
182 synfig::Layer::on_canvas_set()
186 etl::loose_handle<synfig::Canvas>
187 synfig::Layer::get_canvas()const
193 Layer::get_depth()const
197 return get_canvas()->get_depth(const_cast<synfig::Layer*>(this));
201 Layer::set_active(bool x)
208 signal_status_changed_();
213 Layer::set_description(const String& x)
218 signal_description_changed_();
223 Layer::connect_dynamic_param(const String& param, etl::loose_handle<ValueNode> value_node)
225 ValueNode::Handle previous(dynamic_param_list_[param]);
227 if(previous==value_node)
230 dynamic_param_list_[param]=ValueNode::Handle(value_node);
233 remove_child(previous.get());
235 add_child(value_node.get());
237 if(!value_node->is_exported() && get_canvas())
239 value_node->set_parent_canvas(get_canvas());
247 Layer::disconnect_dynamic_param(const String& param)
249 ValueNode::Handle previous(dynamic_param_list_[param]);
253 dynamic_param_list_.erase(param);
254 remove_child(previous.get());
263 dirty_time_=Time::end();
269 Layer::set_param(const String ¶m, const ValueBase &value)
271 if(param=="z_depth" && value.same_type_as(z_depth_))
273 z_depth_=value.get(z_depth_);
279 etl::handle<Transform>
280 Layer::get_transform()const
286 Layer::get_z_depth(const synfig::Time& t)const
288 if(!dynamic_param_list().count("z_depth"))
290 return (*dynamic_param_list().find("z_depth")->second)(t).get(Real());
293 #ifdef THIS_CODE_IS_NOT_USED
295 Layer::simple_clone()const
297 if(!book().count(get_name())) return 0;
298 Layer *ret = create(get_name()).get();
299 ret->set_canvas(get_canvas());
300 ret->set_description(get_description());
301 ret->set_param_list(get_param_list());
304 #endif /* THIS_CODE_IS_NOT_USED */
307 Layer::clone(const GUID& deriv_guid) const
309 if(!book().count(get_name())) return 0;
311 //Layer *ret = book()[get_name()].factory();//create(get_name()).get();
312 Handle ret = create(get_name()).get();
315 //ret->set_canvas(get_canvas());
316 ret->set_description(get_description());
317 ret->set_active(active());
318 ret->set_guid(get_guid()^deriv_guid);
320 //ret->set_param_list(get_param_list());
321 // Process the parameter list so that
322 // we can duplicate any inline canvases
323 ParamList param_list(get_param_list());
324 for(ParamList::const_iterator iter(param_list.begin()); iter != param_list.end(); ++iter)
326 if(dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
329 // This parameter is a canvas. We need a close look.
330 Canvas::Handle canvas(iter->second.get(Canvas::Handle()));
331 if(canvas && canvas->is_inline())
333 // This parameter is an inline canvas! we need to clone it
334 // before we set it as a parameter.
335 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
336 ValueBase value(new_canvas);
337 ret->set_param(iter->first, value);
342 // This is a normal parameter,go ahead and set it.
343 ret->set_param(iter->first, iter->second);
346 // Duplicate the dynamic paramlist, but only the exported data nodes
347 DynamicParamList::const_iterator iter;
348 for(iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
350 // Make sure we clone inline canvases
351 if(iter->second->get_type()==ValueBase::TYPE_CANVAS)
353 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
354 if(canvas->is_inline())
356 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
357 ValueBase value(new_canvas);
358 ret->connect_dynamic_param(iter->first,ValueNode_Const::create(value));
363 if(iter->second->is_exported())
364 ret->connect_dynamic_param(iter->first,iter->second);
366 ret->connect_dynamic_param(iter->first,iter->second->clone(deriv_guid));
369 //ret->set_canvas(0);
375 Layer::get_full_bounding_rect(Context context)const
378 return context.get_full_bounding_rect()|get_bounding_rect();
379 return context.get_full_bounding_rect();
383 Layer::get_bounding_rect()const
385 return Rect::full_plane();
389 Layer::set_param_list(const ParamList &list)
394 ParamList::const_iterator iter(list.begin());
395 for(;iter!=list.end();++iter)
397 if(!set_param(iter->first, iter->second))ret=false;
403 Layer::get_param_list()const
407 Vocab vocab(get_param_vocab());
409 Vocab::const_iterator iter=vocab.begin();
410 for(;iter!=vocab.end();++iter)
412 ret[iter->get_name()]=get_param(iter->get_name());
418 Layer::get_param(const String & param)const
421 return get_z_depth();
427 Layer::get_version()const
429 return get_param("version__").get(String());
433 Layer::set_version(const String &/*ver*/)
439 Layer::reset_version()
445 Layer::set_time(Context context, Time time)const
447 context.set_time(time);
452 Layer::set_time(Context context, Time time, const Point &pos)const
454 context.set_time(time,pos);
459 Layer::get_color(Context context, const Point &pos)const
461 return context.get_color(pos);
464 synfig::Layer::Handle
465 Layer::hit_check(synfig::Context context, const synfig::Point &pos)const
467 return context.hit_check(pos);
470 /* The default accelerated renderer
471 ** is anything but accelerated...
474 Layer::accelerated_render(Context context,Surface *surface,int /*quality*/, const RendDesc &renddesc, ProgressCallback *cb) const
476 handle<Target> target=surface_target(surface);
479 if(cb)cb->error(_("Unable to create surface target"));
482 RendDesc desc=renddesc;
483 target->set_rend_desc(&desc);
485 // When we render, we want to
486 // make sure that we are rendered too...
487 // Since the context iterator is for
488 // the layer after us, we need to back up.
489 // This could be considered a hack, as
490 // it is a possibility that we are indeed
491 // not the previous layer.
494 return render(context,target,desc,cb);
495 //return render_threaded(context,target,desc,cb,2);
499 Layer::get_name()const
501 return get_param("name__").get(String());
505 Layer::get_local_name()const
507 return get_param("local_name__").get(String());
512 Layer::get_param_vocab()const
516 ret.push_back(ParamDesc(z_depth_,"z_depth")
517 .set_local_name(_("Z Depth"))
518 .set_animation_only(true)
525 Layer::get_times_vfunc(Node::time_set &set) const
527 DynamicParamList::const_iterator i = dynamic_param_list_.begin(),
528 end = dynamic_param_list_.end();
532 const Node::time_set &tset = i->second->get_times();
533 set.insert(tset.begin(),tset.end());
539 Layer::add_to_group(const String&x)
544 remove_from_all_groups();
546 signal_added_to_group()(group_);
550 Layer::remove_from_group(const String&x)
553 remove_from_all_groups();
557 Layer::remove_from_all_groups()
561 signal_removed_from_group()(group_);
566 Layer::get_group()const
572 Layer::get_param_local_name(const String ¶m_name)const
574 ParamVocab vocab = get_param_vocab();
575 // loop to find the parameter in the parameter vocab - this gives us its local name
576 for (ParamVocab::iterator iter = vocab.begin(); iter != vocab.end(); iter++)
577 if (iter->get_name() == param_name)
578 return iter->get_local_name();