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