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 ======================================================= */
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) \
101 synfig::Layer::book() [synfig::String(class::name__)] = \
102 BookEntry(class::create, \
104 dgettext("synfig", class::local_name__), \
109 #define LAYER_ALIAS(class,alias) \
110 synfig::Layer::book()[synfig::String(alias)] = \
111 BookEntry(class::create, \
114 CATEGORY_DO_NOT_USE, \
118 INCLUDE_LAYER(Layer_SolidColor); LAYER_ALIAS(Layer_SolidColor, "solid_color");
119 INCLUDE_LAYER(Layer_PasteCanvas); LAYER_ALIAS(Layer_PasteCanvas, "paste_canvas");
120 INCLUDE_LAYER(Layer_Polygon); LAYER_ALIAS(Layer_Polygon, "Polygon");
121 INCLUDE_LAYER(Layer_MotionBlur); LAYER_ALIAS(Layer_MotionBlur, "motion_blur");
122 INCLUDE_LAYER(Layer_Duplicate);
136 /* === M E T H O D S ======================================================= */
141 dirty_time_(Time::end())
143 _LayerCounter::counter++;
147 synfig::Layer::create(const String &name)
149 if(!book().count(name))
151 return Layer::LooseHandle(new Layer_Mime(name));
154 Layer* layer(book()[name].factory());
155 return Layer::LooseHandle(layer);
158 synfig::Layer::~Layer()
160 _LayerCounter::counter--;
161 while(!dynamic_param_list_.empty())
163 remove_child(dynamic_param_list_.begin()->second.get());
164 dynamic_param_list_.erase(dynamic_param_list_.begin());
167 remove_from_all_groups();
169 parent_death_connect_.disconnect();
174 synfig::Layer::set_canvas(etl::loose_handle<Canvas> x)
178 parent_death_connect_.disconnect();
182 parent_death_connect_=x->signal_deleted().connect(
188 etl::loose_handle<synfig::Canvas>(0)
197 synfig::Layer::on_canvas_set()
201 etl::loose_handle<synfig::Canvas>
202 synfig::Layer::get_canvas()const
208 Layer::get_depth()const
212 return get_canvas()->get_depth(const_cast<synfig::Layer*>(this));
216 Layer::set_active(bool x)
223 signal_status_changed_();
228 Layer::set_description(const String& x)
233 signal_description_changed_();
238 Layer::connect_dynamic_param(const String& param, etl::loose_handle<ValueNode> value_node)
240 ValueNode::Handle previous(dynamic_param_list_[param]);
242 if(previous==value_node)
245 dynamic_param_list_[param]=ValueNode::Handle(value_node);
248 remove_child(previous.get());
250 add_child(value_node.get());
252 if(!value_node->is_exported() && get_canvas())
254 value_node->set_parent_canvas(get_canvas());
262 Layer::disconnect_dynamic_param(const String& param)
264 ValueNode::Handle previous(dynamic_param_list_[param]);
268 dynamic_param_list_.erase(param);
269 remove_child(previous.get());
278 dirty_time_=Time::end();
283 Layer::set_param(const String ¶m, const ValueBase &value)
285 if(param=="z_depth" && value.same_type_as(z_depth_))
287 z_depth_=value.get(z_depth_);
293 etl::handle<Transform>
294 Layer::get_transform()const
300 Layer::get_z_depth(const synfig::Time& t)const
302 if(!dynamic_param_list().count("z_depth"))
304 return (*dynamic_param_list().find("z_depth")->second)(t).get(Real());
308 Layer::simple_clone()const
310 if(!book().count(get_name())) return 0;
311 Handle ret = create(get_name()).get();
313 //ret->set_canvas(get_canvas());
314 ret->set_description(get_description());
315 ret->set_active(active());
316 ret->set_param_list(get_param_list());
317 for(DynamicParamList::const_iterator iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
318 ret->connect_dynamic_param(iter->first, iter->second);
323 Layer::clone(const GUID& deriv_guid) const
325 if(!book().count(get_name())) return 0;
327 //Layer *ret = book()[get_name()].factory();//create(get_name()).get();
328 Handle ret = create(get_name()).get();
331 //ret->set_canvas(get_canvas());
332 ret->set_description(get_description());
333 ret->set_active(active());
334 ret->set_guid(get_guid()^deriv_guid);
336 //ret->set_param_list(get_param_list());
337 // Process the parameter list so that
338 // we can duplicate any inline canvases
339 ParamList param_list(get_param_list());
340 for(ParamList::const_iterator iter(param_list.begin()); iter != param_list.end(); ++iter)
342 if(dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
344 // This parameter is a canvas. We need a close look.
345 Canvas::Handle canvas(iter->second.get(Canvas::Handle()));
346 if(canvas && canvas->is_inline())
348 // This parameter is an inline canvas! we need to clone it
349 // before we set it as a parameter.
350 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
351 ValueBase value(new_canvas);
352 ret->set_param(iter->first, value);
357 // This is a normal parameter,go ahead and set it.
358 ret->set_param(iter->first, iter->second);
361 // Duplicate the dynamic paramlist, but only the exported data nodes
362 DynamicParamList::const_iterator iter;
363 for(iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
365 // Make sure we clone inline canvases
366 if(iter->second->get_type()==ValueBase::TYPE_CANVAS)
368 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
369 if(canvas->is_inline())
371 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
372 ValueBase value(new_canvas);
373 ret->connect_dynamic_param(iter->first,ValueNode_Const::create(value));
378 if(iter->second->is_exported())
379 ret->connect_dynamic_param(iter->first,iter->second);
381 ret->connect_dynamic_param(iter->first,iter->second->clone(deriv_guid));
384 //ret->set_canvas(0);
390 Layer::reads_context() const
396 Layer::get_full_bounding_rect(Context context)const
399 return context.get_full_bounding_rect()|get_bounding_rect();
400 return context.get_full_bounding_rect();
404 Layer::get_bounding_rect()const
406 return Rect::full_plane();
410 Layer::set_param_list(const ParamList &list)
415 ParamList::const_iterator iter(list.begin());
416 for(;iter!=list.end();++iter)
418 if(!set_param(iter->first, iter->second))ret=false;
424 Layer::get_param_list()const
428 Vocab vocab(get_param_vocab());
430 Vocab::const_iterator iter=vocab.begin();
431 for(;iter!=vocab.end();++iter)
433 ret[iter->get_name()]=get_param(iter->get_name());
439 Layer::get_param(const String & param)const
442 return get_z_depth();
448 Layer::get_version()const
450 return get_param("version__").get(String());
454 Layer::set_version(const String &/*ver*/)
460 Layer::reset_version()
466 Layer::set_time(Context context, Time time)const
468 context.set_time(time);
473 Layer::set_time(Context context, Time time, const Point &pos)const
475 context.set_time(time,pos);
480 Layer::get_color(Context context, const Point &pos)const
482 return context.get_color(pos);
485 synfig::Layer::Handle
486 Layer::hit_check(synfig::Context context, const synfig::Point &pos)const
488 return context.hit_check(pos);
491 /* The default accelerated renderer
492 ** is anything but accelerated...
495 Layer::accelerated_render(Context context,Surface *surface,int /*quality*/, const RendDesc &renddesc, ProgressCallback *cb) const
497 handle<Target> target=surface_target(surface);
500 if(cb)cb->error(_("Unable to create surface target"));
503 RendDesc desc=renddesc;
504 target->set_rend_desc(&desc);
506 // When we render, we want to
507 // make sure that we are rendered too...
508 // Since the context iterator is for
509 // the layer after us, we need to back up.
510 // This could be considered a hack, as
511 // it is a possibility that we are indeed
512 // not the previous layer.
515 return render(context,target,desc,cb);
516 //return render_threaded(context,target,desc,cb,2);
520 Layer::get_name()const
522 return get_param("name__").get(String());
526 Layer::get_local_name()const
528 return get_param("local_name__").get(String());
533 Layer::get_param_vocab()const
537 ret.push_back(ParamDesc(z_depth_,"z_depth")
538 .set_local_name(_("Z Depth"))
539 .set_animation_only(true)
546 Layer::get_times_vfunc(Node::time_set &set) const
548 DynamicParamList::const_iterator i = dynamic_param_list_.begin(),
549 end = dynamic_param_list_.end();
553 const Node::time_set &tset = i->second->get_times();
554 set.insert(tset.begin(),tset.end());
560 Layer::add_to_group(const String&x)
565 remove_from_all_groups();
567 signal_added_to_group()(group_);
571 Layer::remove_from_group(const String&x)
574 remove_from_all_groups();
578 Layer::remove_from_all_groups()
582 signal_removed_from_group()(group_);
587 Layer::get_group()const
593 Layer::get_param_local_name(const String ¶m_name)const
595 ParamVocab vocab = get_param_vocab();
596 // loop to find the parameter in the parameter vocab - this gives us its local name
597 for (ParamVocab::iterator iter = vocab.begin(); iter != vocab.end(); iter++)
598 if (iter->get_name() == param_name)
599 return iter->get_local_name();