Use macros for the get static and set static members
[synfig.git] / synfig-core / src / synfig / layer.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer.cpp
3 **      \brief Layer class implementation
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "canvas.h"
34 #include "layer.h"
35 #include "render.h"
36 #include "value.h"
37 #include "layer_bitmap.h"
38 #include "layer_mime.h"
39 #include "context.h"
40 #include "paramdesc.h"
41
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"
47
48 #include "valuenode_const.h"
49
50 #include "transform.h"
51 #include "rect.h"
52 #include "guid.h"
53
54 #include <sigc++/adaptors/bind.h>
55 #endif
56
57 /* === U S I N G =========================================================== */
58
59 using namespace etl;
60 using namespace std;
61 using namespace synfig;
62
63 /* === G L O B A L S ======================================================= */
64
65 static Layer::Book* _layer_book;
66
67 struct _LayerCounter
68 {
69         static int counter;
70         ~_LayerCounter()
71         {
72                 if(counter)
73                         synfig::error("%d layers not yet deleted!",counter);
74         }
75 } _layer_counter;
76
77 int _LayerCounter::counter(0);
78
79 /* === P R O C E D U R E S ================================================= */
80
81 Layer::Book&
82 Layer::book()
83 {
84         return *_layer_book;
85 }
86
87 void
88 Layer::register_in_book(const BookEntry &entry)
89 {
90         book()[entry.name]=entry;
91 }
92
93 bool
94 Layer::subsys_init()
95 {
96         _layer_book=new Book();
97
98 #define INCLUDE_LAYER(class)                                                                    \
99         synfig::Layer::book() [synfig::String(class::name__)] =         \
100                 BookEntry(class::create,                                                                \
101                                   class::name__,                                                                \
102                                   dgettext("synfig", class::local_name__),              \
103                                   class::category__,                                                    \
104                                   class::cvs_id__,                                                              \
105                                   class::version__)
106
107 #define LAYER_ALIAS(class,alias)                                                                \
108         synfig::Layer::book()[synfig::String(alias)] =                          \
109                 BookEntry(class::create,                                                                \
110                                   alias,                                                                                \
111                                   alias,                                                                                \
112                                   CATEGORY_DO_NOT_USE,                                                  \
113                                   class::cvs_id__,                                                              \
114                                   class::version__)
115
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);
121
122 #undef INCLUDE_LAYER
123
124         return true;
125 }
126
127 bool
128 Layer::subsys_stop()
129 {
130         delete _layer_book;
131         return true;
132 }
133
134 /* === M E T H O D S ======================================================= */
135
136 Layer::Layer():
137         active_(true),
138         z_depth_(0.0f),
139         dirty_time_(Time::end())
140 {
141         _LayerCounter::counter++;
142 }
143
144 Layer::LooseHandle
145 synfig::Layer::create(const String &name)
146 {
147         if(!book().count(name))
148         {
149                 return Layer::LooseHandle(new Layer_Mime(name));
150         }
151
152         Layer* layer(book()[name].factory());
153         return Layer::LooseHandle(layer);
154 }
155
156 synfig::Layer::~Layer()
157 {
158         _LayerCounter::counter--;
159         while(!dynamic_param_list_.empty())
160         {
161                 remove_child(dynamic_param_list_.begin()->second.get());
162                 dynamic_param_list_.erase(dynamic_param_list_.begin());
163         }
164
165         remove_from_all_groups();
166
167         parent_death_connect_.disconnect();
168         begin_delete();
169 }
170
171 void
172 synfig::Layer::set_canvas(etl::loose_handle<Canvas> x)
173 {
174         if(canvas_!=x)
175         {
176                 parent_death_connect_.disconnect();
177                 canvas_=x;
178                 if(x)
179                 {
180                         parent_death_connect_=x->signal_deleted().connect(
181                                 sigc::bind(
182                                         sigc::mem_fun(
183                                                 *this,
184                                                 &Layer::set_canvas
185                                         ),
186                                         etl::loose_handle<synfig::Canvas>(0)
187                                 )
188                         );
189                 }
190                 on_canvas_set();
191         }
192 }
193
194 void
195 synfig::Layer::on_canvas_set()
196 {
197 }
198
199 etl::loose_handle<synfig::Canvas>
200 synfig::Layer::get_canvas()const
201 {
202         return canvas_;
203 }
204
205 int
206 Layer::get_depth()const
207 {
208         if(!get_canvas())
209                 return -1;
210         return get_canvas()->get_depth(const_cast<synfig::Layer*>(this));
211 }
212
213 void
214 Layer::set_active(bool x)
215 {
216         if(active_!=x)
217         {
218                 active_=x;
219
220                 Node::on_changed();
221                 signal_status_changed_();
222         }
223 }
224
225 void
226 Layer::set_description(const String& x)
227 {
228         if(description_!=x)
229         {
230                 description_=x;
231                 signal_description_changed_();
232         }
233 }
234
235 bool
236 Layer::connect_dynamic_param(const String& param, etl::loose_handle<ValueNode> value_node)
237 {
238         ValueNode::Handle previous(dynamic_param_list_[param]);
239
240         if(previous==value_node)
241                 return true;
242
243         dynamic_param_list_[param]=ValueNode::Handle(value_node);
244
245         if(previous)
246                 remove_child(previous.get());
247
248         add_child(value_node.get());
249
250         if(!value_node->is_exported() && get_canvas())
251         {
252                 value_node->set_parent_canvas(get_canvas());
253         }
254
255         changed();
256         return true;
257 }
258
259 bool
260 Layer::disconnect_dynamic_param(const String& param)
261 {
262         ValueNode::Handle previous(dynamic_param_list_[param]);
263
264         if(previous)
265         {
266                 dynamic_param_list_.erase(param);
267
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)
276                                 break;
277                 if (iter == dynamic_param_list().end())
278                         remove_child(previous.get());
279
280                 changed();
281         }
282         return true;
283 }
284
285 void
286 Layer::on_changed()
287 {
288         dirty_time_=Time::end();
289         Node::on_changed();
290 }
291
292 bool
293 Layer::set_param(const String &param, const ValueBase &value)
294 {
295         if(param=="z_depth" && value.same_type_as(z_depth_))
296         {
297                 z_depth_=value.get(z_depth_);
298                 set_param_static(param, value.get_static());
299                 return true;
300         }
301         return false;
302 }
303
304 bool
305 Layer::set_param_static(const String &param, const bool x)
306 {
307         SET_STATIC(z_depth,x)
308
309         return false;
310 }
311
312
313 bool
314 Layer::get_param_static(const String &param) const
315 {
316         GET_STATIC(z_depth);
317
318         return false;
319 }
320
321
322 etl::handle<Transform>
323 Layer::get_transform()const
324 {
325         return 0;
326 }
327
328 float
329 Layer::get_z_depth(const synfig::Time& t)const
330 {
331         if(!dynamic_param_list().count("z_depth"))
332                 return z_depth_;
333         return (*dynamic_param_list().find("z_depth")->second)(t).get(Real());
334 }
335
336 Layer::Handle
337 Layer::simple_clone()const
338 {
339         if(!book().count(get_name())) return 0;
340         Handle ret = create(get_name()).get();
341         ret->group_=group_;
342         //ret->set_canvas(get_canvas());
343         ret->set_description(get_description());
344         ret->set_active(active());
345         ret->set_param_list(get_param_list());
346         for(DynamicParamList::const_iterator iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
347                 ret->connect_dynamic_param(iter->first, iter->second);
348         return ret;
349 }
350
351 Layer::Handle
352 Layer::clone(const GUID& deriv_guid) const
353 {
354         if(!book().count(get_name())) return 0;
355
356         //Layer *ret = book()[get_name()].factory();//create(get_name()).get();
357         Handle ret = create(get_name()).get();
358
359         ret->group_=group_;
360         //ret->set_canvas(get_canvas());
361         ret->set_description(get_description());
362         ret->set_active(active());
363         ret->set_guid(get_guid()^deriv_guid);
364
365         //ret->set_param_list(get_param_list());
366         // Process the parameter list so that
367         // we can duplicate any inline canvases
368         ParamList param_list(get_param_list());
369         for(ParamList::const_iterator iter(param_list.begin()); iter != param_list.end(); ++iter)
370         {
371                 if(dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
372                 {
373                         // This parameter is a canvas.  We need a close look.
374                         Canvas::Handle canvas(iter->second.get(Canvas::Handle()));
375                         if(canvas && canvas->is_inline())
376                         {
377                                 // This parameter is an inline canvas! we need to clone it
378                                 // before we set it as a parameter.
379                                 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
380                                 ValueBase value(new_canvas);
381                                 ret->set_param(iter->first, value);
382                                 continue;
383                         }
384                 }
385
386                 // This is a normal parameter,go ahead and set it.
387                 ret->set_param(iter->first, iter->second);
388         }
389
390         // Duplicate the dynamic paramlist, but only the exported data nodes
391         DynamicParamList::const_iterator iter;
392         for(iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
393         {
394                 // Make sure we clone inline canvases
395                 if(iter->second->get_type()==ValueBase::TYPE_CANVAS)
396                 {
397                         Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
398                         if(canvas->is_inline())
399                         {
400                                 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
401                                 ValueBase value(new_canvas);
402                                 ret->connect_dynamic_param(iter->first,ValueNode_Const::create(value));
403                                 continue;
404                         }
405                 }
406
407                 if(iter->second->is_exported())
408                         ret->connect_dynamic_param(iter->first,iter->second);
409                 else
410                         ret->connect_dynamic_param(iter->first,iter->second->clone(deriv_guid));
411         }
412
413         //ret->set_canvas(0);
414
415         return ret;
416 }
417
418 bool
419 Layer::reads_context() const
420 {
421         return false;
422 }
423
424 Rect
425 Layer::get_full_bounding_rect(Context context)const
426 {
427         if(active())
428                 return context.get_full_bounding_rect()|get_bounding_rect();
429         return context.get_full_bounding_rect();
430 }
431
432 Rect
433 Layer::get_bounding_rect()const
434 {
435         return Rect::full_plane();
436 }
437
438 bool
439 Layer::set_param_list(const ParamList &list)
440 {
441         bool ret=true;
442         if(!list.size())
443                 return false;
444         ParamList::const_iterator iter(list.begin());
445         for(;iter!=list.end();++iter)
446         {
447                 if(!set_param(iter->first, iter->second))ret=false;
448         }
449         return ret;
450 }
451
452 Layer::ParamList
453 Layer::get_param_list()const
454 {
455         ParamList ret;
456
457         Vocab vocab(get_param_vocab());
458
459         Vocab::const_iterator iter=vocab.begin();
460         for(;iter!=vocab.end();++iter)
461         {
462                 ret[iter->get_name()]=get_param(iter->get_name());
463         }
464         return ret;
465 }
466
467 ValueBase
468 Layer::get_param(const String & param)const
469 {
470         if(param=="z_depth")
471         {
472                 synfig::ValueBase ret(get_z_depth());
473                 ret.set_static(z_depth_static);
474                 return ret;
475         }
476         return ValueBase();
477 }
478
479 String
480 Layer::get_version()const
481 {
482         return get_param("version__").get(String());
483 }
484
485 bool
486 Layer::set_version(const String &/*ver*/)
487 {
488         return false;
489 }
490
491 void
492 Layer::reset_version()
493 {
494 }
495
496
497 void
498 Layer::set_time(Context context, Time time)const
499 {
500         context.set_time(time);
501         dirty_time_=time;
502 }
503
504 void
505 Layer::set_time(Context context, Time time, const Point &pos)const
506 {
507         context.set_time(time,pos);
508         dirty_time_=time;
509 }
510
511 Color
512 Layer::get_color(Context context, const Point &pos)const
513 {
514         return context.get_color(pos);
515 }
516
517 synfig::Layer::Handle
518 Layer::hit_check(synfig::Context context, const synfig::Point &pos)const
519 {
520         return context.hit_check(pos);
521 }
522
523 /*      The default accelerated renderer
524 **      is anything but accelerated...
525 */
526 bool
527 Layer::accelerated_render(Context context,Surface *surface,int /*quality*/, const RendDesc &renddesc, ProgressCallback *cb)  const
528 {
529         handle<Target> target=surface_target(surface);
530         if(!target)
531         {
532                 if(cb)cb->error(_("Unable to create surface target"));
533                 return false;
534         }
535         RendDesc desc=renddesc;
536         target->set_rend_desc(&desc);
537
538         // When we render, we want to
539         // make sure that we are rendered too...
540         // Since the context iterator is for
541         // the layer after us, we need to back up.
542         // This could be considered a hack, as
543         // it is a possibility that we are indeed
544         // not the previous layer.
545         --context;
546
547         return render(context,target,desc,cb);
548         //return render_threaded(context,target,desc,cb,2);
549 }
550
551 String
552 Layer::get_name()const
553 {
554         return get_param("name__").get(String());
555 }
556
557 String
558 Layer::get_local_name()const
559 {
560         return get_param("local_name__").get(String());
561 }
562
563
564 Layer::Vocab
565 Layer::get_param_vocab()const
566 {
567         Layer::Vocab ret;
568
569         ret.push_back(ParamDesc(z_depth_,"z_depth")
570                 .set_local_name(_("Z Depth"))
571                 .set_animation_only(true)
572         );
573
574         return ret;
575 }
576
577 void
578 Layer::get_times_vfunc(Node::time_set &set) const
579 {
580         DynamicParamList::const_iterator        i = dynamic_param_list_.begin(),
581                                                                                 end = dynamic_param_list_.end();
582
583         for(; i != end; ++i)
584         {
585                 const Node::time_set &tset = i->second->get_times();
586                 set.insert(tset.begin(),tset.end());
587         }
588 }
589
590
591 void
592 Layer::add_to_group(const String&x)
593 {
594         if(x==group_)
595                 return;
596         if(!group_.empty())
597                 remove_from_all_groups();
598         group_=x;
599         signal_added_to_group()(group_);
600 }
601
602 void
603 Layer::remove_from_group(const String&x)
604 {
605         if(group_==x)
606                 remove_from_all_groups();
607 }
608
609 void
610 Layer::remove_from_all_groups()
611 {
612         if(group_.empty())
613                 return;
614         signal_removed_from_group()(group_);
615         group_.clear();
616 }
617
618 String
619 Layer::get_group()const
620 {
621         return group_;
622 }
623
624 const String
625 Layer::get_param_local_name(const String &param_name)const
626 {
627         ParamVocab vocab = get_param_vocab();
628         // loop to find the parameter in the parameter vocab - this gives us its local name
629         for (ParamVocab::iterator iter = vocab.begin(); iter != vocab.end(); iter++)
630                 if (iter->get_name() == param_name)
631                         return iter->get_local_name();
632         return String();
633 }