Set z depth static value to false by default in Layer class.
[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 /*      if(param=="z_depth" && value.same_type_as(z_depth_))
297         {
298                 z_depth_=value.get(z_depth_);
299                 set_param_static(param, value.get_static());
300                 return true;
301         }
302         */
303         IMPORT_AS(z_depth_,"z_depth")
304         return false;
305 }
306
307 bool
308 Layer::set_param_static(const String &param, const bool x)
309 {
310         SET_STATIC(z_depth,x)
311
312         return false;
313 }
314
315
316 bool
317 Layer::get_param_static(const String &param) const
318 {
319         GET_STATIC(z_depth);
320
321         return false;
322 }
323
324
325 etl::handle<Transform>
326 Layer::get_transform()const
327 {
328         return 0;
329 }
330
331 float
332 Layer::get_z_depth(const synfig::Time& t)const
333 {
334         if(!dynamic_param_list().count("z_depth"))
335                 return z_depth_;
336         return (*dynamic_param_list().find("z_depth")->second)(t).get(Real());
337 }
338
339 Layer::Handle
340 Layer::simple_clone()const
341 {
342         if(!book().count(get_name())) return 0;
343         Handle ret = create(get_name()).get();
344         ret->group_=group_;
345         //ret->set_canvas(get_canvas());
346         ret->set_description(get_description());
347         ret->set_active(active());
348         ret->set_param_list(get_param_list());
349         for(DynamicParamList::const_iterator iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
350                 ret->connect_dynamic_param(iter->first, iter->second);
351         return ret;
352 }
353
354 Layer::Handle
355 Layer::clone(const GUID& deriv_guid) const
356 {
357         if(!book().count(get_name())) return 0;
358
359         //Layer *ret = book()[get_name()].factory();//create(get_name()).get();
360         Handle ret = create(get_name()).get();
361
362         ret->group_=group_;
363         //ret->set_canvas(get_canvas());
364         ret->set_description(get_description());
365         ret->set_active(active());
366         ret->set_guid(get_guid()^deriv_guid);
367
368         //ret->set_param_list(get_param_list());
369         // Process the parameter list so that
370         // we can duplicate any inline canvases
371         ParamList param_list(get_param_list());
372         for(ParamList::const_iterator iter(param_list.begin()); iter != param_list.end(); ++iter)
373         {
374                 if(dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
375                 {
376                         // This parameter is a canvas.  We need a close look.
377                         Canvas::Handle canvas(iter->second.get(Canvas::Handle()));
378                         if(canvas && canvas->is_inline())
379                         {
380                                 // This parameter is an inline canvas! we need to clone it
381                                 // before we set it as a parameter.
382                                 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
383                                 ValueBase value(new_canvas);
384                                 ret->set_param(iter->first, value);
385                                 continue;
386                         }
387                 }
388
389                 // This is a normal parameter,go ahead and set it.
390                 ret->set_param(iter->first, iter->second);
391         }
392
393         // Duplicate the dynamic paramlist, but only the exported data nodes
394         DynamicParamList::const_iterator iter;
395         for(iter=dynamic_param_list().begin();iter!=dynamic_param_list().end();++iter)
396         {
397                 // Make sure we clone inline canvases
398                 if(iter->second->get_type()==ValueBase::TYPE_CANVAS)
399                 {
400                         Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
401                         if(canvas->is_inline())
402                         {
403                                 Canvas::Handle new_canvas(canvas->clone(deriv_guid));
404                                 ValueBase value(new_canvas);
405                                 ret->connect_dynamic_param(iter->first,ValueNode_Const::create(value));
406                                 continue;
407                         }
408                 }
409
410                 if(iter->second->is_exported())
411                         ret->connect_dynamic_param(iter->first,iter->second);
412                 else
413                         ret->connect_dynamic_param(iter->first,iter->second->clone(deriv_guid));
414         }
415
416         //ret->set_canvas(0);
417
418         return ret;
419 }
420
421 bool
422 Layer::reads_context() const
423 {
424         return false;
425 }
426
427 Rect
428 Layer::get_full_bounding_rect(Context context)const
429 {
430         if(active())
431                 return context.get_full_bounding_rect()|get_bounding_rect();
432         return context.get_full_bounding_rect();
433 }
434
435 Rect
436 Layer::get_bounding_rect()const
437 {
438         return Rect::full_plane();
439 }
440
441 bool
442 Layer::set_param_list(const ParamList &list)
443 {
444         bool ret=true;
445         if(!list.size())
446                 return false;
447         ParamList::const_iterator iter(list.begin());
448         for(;iter!=list.end();++iter)
449         {
450                 if(!set_param(iter->first, iter->second))ret=false;
451         }
452         return ret;
453 }
454
455 Layer::ParamList
456 Layer::get_param_list()const
457 {
458         ParamList ret;
459
460         Vocab vocab(get_param_vocab());
461
462         Vocab::const_iterator iter=vocab.begin();
463         for(;iter!=vocab.end();++iter)
464         {
465                 ret[iter->get_name()]=get_param(iter->get_name());
466         }
467         return ret;
468 }
469
470 ValueBase
471 Layer::get_param(const String & param)const
472 {
473         if(param=="z_depth")
474         {
475                 synfig::ValueBase ret(get_z_depth());
476                 ret.set_static(z_depth_static);
477                 return ret;
478         }
479         return ValueBase();
480 }
481
482 String
483 Layer::get_version()const
484 {
485         return get_param("version__").get(String());
486 }
487
488 bool
489 Layer::set_version(const String &/*ver*/)
490 {
491         return false;
492 }
493
494 void
495 Layer::reset_version()
496 {
497 }
498
499
500 void
501 Layer::set_time(Context context, Time time)const
502 {
503         context.set_time(time);
504         dirty_time_=time;
505 }
506
507 void
508 Layer::set_time(Context context, Time time, const Point &pos)const
509 {
510         context.set_time(time,pos);
511         dirty_time_=time;
512 }
513
514 Color
515 Layer::get_color(Context context, const Point &pos)const
516 {
517         return context.get_color(pos);
518 }
519
520 synfig::Layer::Handle
521 Layer::hit_check(synfig::Context context, const synfig::Point &pos)const
522 {
523         return context.hit_check(pos);
524 }
525
526 /*      The default accelerated renderer
527 **      is anything but accelerated...
528 */
529 bool
530 Layer::accelerated_render(Context context,Surface *surface,int /*quality*/, const RendDesc &renddesc, ProgressCallback *cb)  const
531 {
532         handle<Target> target=surface_target(surface);
533         if(!target)
534         {
535                 if(cb)cb->error(_("Unable to create surface target"));
536                 return false;
537         }
538         RendDesc desc=renddesc;
539         target->set_rend_desc(&desc);
540
541         // When we render, we want to
542         // make sure that we are rendered too...
543         // Since the context iterator is for
544         // the layer after us, we need to back up.
545         // This could be considered a hack, as
546         // it is a possibility that we are indeed
547         // not the previous layer.
548         --context;
549
550         return render(context,target,desc,cb);
551         //return render_threaded(context,target,desc,cb,2);
552 }
553
554 String
555 Layer::get_name()const
556 {
557         return get_param("name__").get(String());
558 }
559
560 String
561 Layer::get_local_name()const
562 {
563         return get_param("local_name__").get(String());
564 }
565
566
567 Layer::Vocab
568 Layer::get_param_vocab()const
569 {
570         Layer::Vocab ret;
571
572         ret.push_back(ParamDesc(z_depth_,"z_depth")
573                 .set_local_name(_("Z Depth"))
574                 .set_animation_only(true)
575         );
576
577         return ret;
578 }
579
580 void
581 Layer::get_times_vfunc(Node::time_set &set) const
582 {
583         DynamicParamList::const_iterator        i = dynamic_param_list_.begin(),
584                                                                                 end = dynamic_param_list_.end();
585
586         for(; i != end; ++i)
587         {
588                 const Node::time_set &tset = i->second->get_times();
589                 set.insert(tset.begin(),tset.end());
590         }
591 }
592
593
594 void
595 Layer::add_to_group(const String&x)
596 {
597         if(x==group_)
598                 return;
599         if(!group_.empty())
600                 remove_from_all_groups();
601         group_=x;
602         signal_added_to_group()(group_);
603 }
604
605 void
606 Layer::remove_from_group(const String&x)
607 {
608         if(group_==x)
609                 remove_from_all_groups();
610 }
611
612 void
613 Layer::remove_from_all_groups()
614 {
615         if(group_.empty())
616                 return;
617         signal_removed_from_group()(group_);
618         group_.clear();
619 }
620
621 String
622 Layer::get_group()const
623 {
624         return group_;
625 }
626
627 const String
628 Layer::get_param_local_name(const String &param_name)const
629 {
630         ParamVocab vocab = get_param_vocab();
631         // loop to find the parameter in the parameter vocab - this gives us its local name
632         for (ParamVocab::iterator iter = vocab.begin(); iter != vocab.end(); iter++)
633                 if (iter->get_name() == param_name)
634                         return iter->get_local_name();
635         return String();
636 }