Rename z_depth_static to z_depth__static to make proper use of the macros.
[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         z_depth__static(false)
141 {
142         _LayerCounter::counter++;
143 }
144
145 Layer::LooseHandle
146 synfig::Layer::create(const String &name)
147 {
148         if(!book().count(name))
149         {
150                 return Layer::LooseHandle(new Layer_Mime(name));
151         }
152
153         Layer* layer(book()[name].factory());
154         return Layer::LooseHandle(layer);
155 }
156
157 synfig::Layer::~Layer()
158 {
159         _LayerCounter::counter--;
160         while(!dynamic_param_list_.empty())
161         {
162                 remove_child(dynamic_param_list_.begin()->second.get());
163                 dynamic_param_list_.erase(dynamic_param_list_.begin());
164         }
165
166         remove_from_all_groups();
167
168         parent_death_connect_.disconnect();
169         begin_delete();
170 }
171
172 void
173 synfig::Layer::set_canvas(etl::loose_handle<Canvas> x)
174 {
175         if(canvas_!=x)
176         {
177                 parent_death_connect_.disconnect();
178                 canvas_=x;
179                 if(x)
180                 {
181                         parent_death_connect_=x->signal_deleted().connect(
182                                 sigc::bind(
183                                         sigc::mem_fun(
184                                                 *this,
185                                                 &Layer::set_canvas
186                                         ),
187                                         etl::loose_handle<synfig::Canvas>(0)
188                                 )
189                         );
190                 }
191                 on_canvas_set();
192         }
193 }
194
195 void
196 synfig::Layer::on_canvas_set()
197 {
198 }
199
200 etl::loose_handle<synfig::Canvas>
201 synfig::Layer::get_canvas()const
202 {
203         return canvas_;
204 }
205
206 int
207 Layer::get_depth()const
208 {
209         if(!get_canvas())
210                 return -1;
211         return get_canvas()->get_depth(const_cast<synfig::Layer*>(this));
212 }
213
214 void
215 Layer::set_active(bool x)
216 {
217         if(active_!=x)
218         {
219                 active_=x;
220
221                 Node::on_changed();
222                 signal_status_changed_();
223         }
224 }
225
226 void
227 Layer::set_description(const String& x)
228 {
229         if(description_!=x)
230         {
231                 description_=x;
232                 signal_description_changed_();
233         }
234 }
235
236 bool
237 Layer::connect_dynamic_param(const String& param, etl::loose_handle<ValueNode> value_node)
238 {
239         ValueNode::Handle previous(dynamic_param_list_[param]);
240
241         if(previous==value_node)
242                 return true;
243
244         dynamic_param_list_[param]=ValueNode::Handle(value_node);
245
246         if(previous)
247                 remove_child(previous.get());
248
249         add_child(value_node.get());
250
251         if(!value_node->is_exported() && get_canvas())
252         {
253                 value_node->set_parent_canvas(get_canvas());
254         }
255
256         changed();
257         return true;
258 }
259
260 bool
261 Layer::disconnect_dynamic_param(const String& param)
262 {
263         ValueNode::Handle previous(dynamic_param_list_[param]);
264
265         if(previous)
266         {
267                 dynamic_param_list_.erase(param);
268
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)
277                                 break;
278                 if (iter == dynamic_param_list().end())
279                         remove_child(previous.get());
280
281                 changed();
282         }
283         return true;
284 }
285
286 void
287 Layer::on_changed()
288 {
289         dirty_time_=Time::end();
290         Node::on_changed();
291 }
292
293 bool
294 Layer::set_param(const String &param, const ValueBase &value)
295 {
296         IMPORT_AS(z_depth_,"z_depth")
297         return false;
298 }
299
300 bool
301 Layer::set_param_static(const String &param, const bool x)
302 {
303         SET_STATIC(z_depth_,x)
304
305         return false;
306 }
307
308
309 bool
310 Layer::get_param_static(const String &param) const
311 {
312         GET_STATIC(z_depth_);
313
314         return false;
315 }
316
317
318 etl::handle<Transform>
319 Layer::get_transform()const
320 {
321         return 0;
322 }
323
324 float
325 Layer::get_z_depth(const synfig::Time& t)const
326 {
327         if(!dynamic_param_list().count("z_depth"))
328                 return z_depth_;
329         return (*dynamic_param_list().find("z_depth")->second)(t).get(Real());
330 }
331
332 Layer::Handle
333 Layer::simple_clone()const
334 {
335         if(!book().count(get_name())) return 0;
336         Handle ret = create(get_name()).get();
337         ret->group_=group_;
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);
344         return ret;
345 }
346
347 Layer::Handle
348 Layer::clone(const GUID& deriv_guid) const
349 {
350         if(!book().count(get_name())) return 0;
351
352         //Layer *ret = book()[get_name()].factory();//create(get_name()).get();
353         Handle ret = create(get_name()).get();
354
355         ret->group_=group_;
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);
360
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)
366         {
367                 if(dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
368                 {
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())
372                         {
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);
378                                 continue;
379                         }
380                 }
381
382                 // This is a normal parameter,go ahead and set it.
383                 ret->set_param(iter->first, iter->second);
384         }
385
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)
389         {
390                 // Make sure we clone inline canvases
391                 if(iter->second->get_type()==ValueBase::TYPE_CANVAS)
392                 {
393                         Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
394                         if(canvas->is_inline())
395                         {
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));
399                                 continue;
400                         }
401                 }
402
403                 if(iter->second->is_exported())
404                         ret->connect_dynamic_param(iter->first,iter->second);
405                 else
406                         ret->connect_dynamic_param(iter->first,iter->second->clone(deriv_guid));
407         }
408
409         //ret->set_canvas(0);
410
411         return ret;
412 }
413
414 bool
415 Layer::reads_context() const
416 {
417         return false;
418 }
419
420 Rect
421 Layer::get_full_bounding_rect(Context context)const
422 {
423         if(active())
424                 return context.get_full_bounding_rect()|get_bounding_rect();
425         return context.get_full_bounding_rect();
426 }
427
428 Rect
429 Layer::get_bounding_rect()const
430 {
431         return Rect::full_plane();
432 }
433
434 bool
435 Layer::set_param_list(const ParamList &list)
436 {
437         bool ret=true;
438         if(!list.size())
439                 return false;
440         ParamList::const_iterator iter(list.begin());
441         for(;iter!=list.end();++iter)
442         {
443                 if(!set_param(iter->first, iter->second))ret=false;
444         }
445         return ret;
446 }
447
448 Layer::ParamList
449 Layer::get_param_list()const
450 {
451         ParamList ret;
452
453         Vocab vocab(get_param_vocab());
454
455         Vocab::const_iterator iter=vocab.begin();
456         for(;iter!=vocab.end();++iter)
457         {
458                 ret[iter->get_name()]=get_param(iter->get_name());
459         }
460         return ret;
461 }
462
463 ValueBase
464 Layer::get_param(const String & param)const
465 {
466         if(param=="z_depth")
467         {
468                 synfig::ValueBase ret(get_z_depth());
469                 ret.set_static(z_depth__static);
470                 return ret;
471         }
472         return ValueBase();
473 }
474
475 String
476 Layer::get_version()const
477 {
478         return get_param("version__").get(String());
479 }
480
481 bool
482 Layer::set_version(const String &/*ver*/)
483 {
484         return false;
485 }
486
487 void
488 Layer::reset_version()
489 {
490 }
491
492
493 void
494 Layer::set_time(Context context, Time time)const
495 {
496         context.set_time(time);
497         dirty_time_=time;
498 }
499
500 void
501 Layer::set_time(Context context, Time time, const Point &pos)const
502 {
503         context.set_time(time,pos);
504         dirty_time_=time;
505 }
506
507 Color
508 Layer::get_color(Context context, const Point &pos)const
509 {
510         return context.get_color(pos);
511 }
512
513 synfig::Layer::Handle
514 Layer::hit_check(synfig::Context context, const synfig::Point &pos)const
515 {
516         return context.hit_check(pos);
517 }
518
519 /*      The default accelerated renderer
520 **      is anything but accelerated...
521 */
522 bool
523 Layer::accelerated_render(Context context,Surface *surface,int /*quality*/, const RendDesc &renddesc, ProgressCallback *cb)  const
524 {
525         handle<Target> target=surface_target(surface);
526         if(!target)
527         {
528                 if(cb)cb->error(_("Unable to create surface target"));
529                 return false;
530         }
531         RendDesc desc=renddesc;
532         target->set_rend_desc(&desc);
533
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.
541         --context;
542
543         return render(context,target,desc,cb);
544         //return render_threaded(context,target,desc,cb,2);
545 }
546
547 String
548 Layer::get_name()const
549 {
550         return get_param("name__").get(String());
551 }
552
553 String
554 Layer::get_local_name()const
555 {
556         return get_param("local_name__").get(String());
557 }
558
559
560 Layer::Vocab
561 Layer::get_param_vocab()const
562 {
563         Layer::Vocab ret;
564
565         ret.push_back(ParamDesc(z_depth_,"z_depth")
566                 .set_local_name(_("Z Depth"))
567                 .set_animation_only(true)
568         );
569
570         return ret;
571 }
572
573 void
574 Layer::get_times_vfunc(Node::time_set &set) const
575 {
576         DynamicParamList::const_iterator        i = dynamic_param_list_.begin(),
577                                                                                 end = dynamic_param_list_.end();
578
579         for(; i != end; ++i)
580         {
581                 const Node::time_set &tset = i->second->get_times();
582                 set.insert(tset.begin(),tset.end());
583         }
584 }
585
586
587 void
588 Layer::add_to_group(const String&x)
589 {
590         if(x==group_)
591                 return;
592         if(!group_.empty())
593                 remove_from_all_groups();
594         group_=x;
595         signal_added_to_group()(group_);
596 }
597
598 void
599 Layer::remove_from_group(const String&x)
600 {
601         if(group_==x)
602                 remove_from_all_groups();
603 }
604
605 void
606 Layer::remove_from_all_groups()
607 {
608         if(group_.empty())
609                 return;
610         signal_removed_from_group()(group_);
611         group_.clear();
612 }
613
614 String
615 Layer::get_group()const
616 {
617         return group_;
618 }
619
620 const String
621 Layer::get_param_local_name(const String &param_name)const
622 {
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();
628         return String();
629 }