Avoid a warning from g++ 4.3.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode.cpp
3 **      \brief Implementation of the "Placeholder" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 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 #define SYNFIG_NO_ANGLE
27
28 //#define HAS_HASH_MAP 1
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include "valuenode.h"
38 #include "general.h"
39 #include "canvas.h"
40 #include "paramdesc.h"
41
42 #include "valuenode_const.h"
43 #include "valuenode_linear.h"
44 #include "valuenode_composite.h"
45 #include "valuenode_reference.h"
46 #include "valuenode_scale.h"
47 #include "valuenode_blinecalctangent.h"
48 #include "valuenode_blinecalcvertex.h"
49 #include "valuenode_blinereversetangent.h"
50 #include "valuenode_segcalctangent.h"
51 #include "valuenode_segcalcvertex.h"
52 #include "valuenode_repeat_gradient.h"
53 #include "valuenode_stripes.h"
54 #include "valuenode_range.h"
55 #include "valuenode_add.h"
56 #include "valuenode_subtract.h"
57 #include "valuenode_timedswap.h"
58 #include "valuenode_twotone.h"
59 #include "valuenode_bline.h"
60 #include "valuenode_dynamiclist.h"
61 #include "valuenode_radialcomposite.h"
62 #include "valuenode_gradientrotate.h"
63 #include "valuenode_sine.h"
64 #include "valuenode_cos.h"
65 #include "valuenode_atan2.h"
66 #include "valuenode_exp.h"
67 #include "valuenode_switch.h"
68 #include "valuenode_timeloop.h"
69 #include "valuenode_reciprocal.h"
70 #include "valuenode_duplicate.h"
71 #include "valuenode_integer.h"
72
73 #include "layer.h"
74
75 #endif
76
77 /* === U S I N G =========================================================== */
78
79 using namespace std;
80 using namespace etl;
81 using namespace synfig;
82
83 /* === M A C R O S ========================================================= */
84
85 /* === G L O B A L S ======================================================= */
86
87 static int value_node_count(0);
88
89 static LinkableValueNode::Book *book_;
90
91
92 ValueNode::LooseHandle
93 synfig::find_value_node(const GUID& guid)
94 {
95         return guid_cast<ValueNode>(guid);
96 }
97
98 /* === P R O C E D U R E S ================================================= */
99
100 /* === M E T H O D S ======================================================= */
101
102 bool
103 ValueNode::subsys_init()
104 {
105         book_=new LinkableValueNode::Book();
106
107 #define ADD_VALUENODE(class,name,local)                                                                                                                 \
108         (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create);            \
109         (*book_)[name].check_type=&class::check_type;                                                                                           \
110         (*book_)[name].local_name=local
111
112 #define ADD_VALUENODE2(class,name,local)                                                                                                                \
113         (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create_from);       \
114         (*book_)[name].check_type=&class::check_type;                                                                                           \
115         (*book_)[name].local_name=local
116
117         ADD_VALUENODE(ValueNode_Linear,                 "linear",                       _("Linear")                             );
118         ADD_VALUENODE(ValueNode_Composite,              "composite",            _("Composite")                  );
119         ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite",     _("Radial Composite")   );
120         ADD_VALUENODE(ValueNode_Reference,              "reference",            _("Reference")                  );
121         ADD_VALUENODE(ValueNode_Repeat_Gradient,"repeat_gradient",      _("Repeat Gradient")    );
122         ADD_VALUENODE(ValueNode_Scale,                  "scale",                        _("Scale")                              );
123         ADD_VALUENODE(ValueNode_BLineCalcTangent,"blinecalctangent",_("BLine Tangent")          );
124         ADD_VALUENODE(ValueNode_BLineCalcVertex,"blinecalcvertex",      _("BLine Vertex")               );
125         ADD_VALUENODE(ValueNode_BLineRevTangent,"blinerevtangent",      _("Reverse Tangent")    );
126         ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent",       _("Segment Tangent")    );
127         ADD_VALUENODE(ValueNode_SegCalcVertex,  "segcalcvertex",        _("Segment Vertex")             );
128         ADD_VALUENODE(ValueNode_Stripes,                "stripes",                      _("Stripes")                    );
129         ADD_VALUENODE(ValueNode_Range,                  "range",                        _("Range")                              );
130         ADD_VALUENODE(ValueNode_Add,                    "add",                          _("Add")                                );
131         ADD_VALUENODE(ValueNode_Subtract,               "subtract",                     _("Subtract")                   );
132         ADD_VALUENODE(ValueNode_TimedSwap,              "timed_swap",           _("Timed Swap")                 );
133         ADD_VALUENODE(ValueNode_TwoTone,                "twotone",                      _("Two-Tone")                   );
134         ADD_VALUENODE(ValueNode_BLine,                  "bline",                        _("BLine")                              );
135         ADD_VALUENODE2(ValueNode_DynamicList,   "dynamic_list",         _("Dynamic List")               );
136         ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate",      _("Gradient Rotate")    );
137         ADD_VALUENODE(ValueNode_Sine,                   "sine",                         _("Sine")                               );
138         ADD_VALUENODE(ValueNode_Cos,                    "cos",                          _("Cos")                                );
139         ADD_VALUENODE(ValueNode_Atan2,                  "atan2",                        _("aTan2")                              );
140         ADD_VALUENODE(ValueNode_Exp,                    "exp",                          _("Exponential")                );
141         ADD_VALUENODE(ValueNode_Switch,                 "switch",                       _("Switch")                             );
142         ADD_VALUENODE(ValueNode_TimeLoop,               "timeloop",                     _("Time Loop")                  );
143         ADD_VALUENODE(ValueNode_Reciprocal,             "reciprocal",           _("Reciprocal")                 );
144         ADD_VALUENODE(ValueNode_Duplicate,              "duplicate",            _("Duplicate")                  );
145         ADD_VALUENODE(ValueNode_Integer,                "fromint",                      _("From Integer")               );
146
147 #undef ADD_VALUENODE
148 #undef ADD_VALUENODE2
149
150         return true;
151 }
152
153 bool
154 ValueNode::subsys_stop()
155 {
156         delete book_;
157 /*      if(global_value_node_map.size() || value_node_count)
158         {
159                 if(value_node_count)
160                         synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
161
162                 if(global_value_node_map.size()!=value_node_count)
163                         synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
164
165                 GlobalValueNodeMap::iterator iter;
166                 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
167                 {
168                         if(!iter->second->is_exported())
169                                 synfig::info(_("%s: count:%d name:%s type:%s"),
170                                         iter->first.get_string().c_str(),
171                                         iter->second->count(),
172                                         iter->second->get_name().c_str(),
173                                         ValueBase::type_local_name(iter->second->get_type()).c_str()
174                                 );
175                         else
176                                 synfig::info(_("%s: id:%s count:%d name:%s type:%s"),
177                                         iter->first.get_string().c_str(),
178                                         iter->second->get_id().c_str(),
179                                         iter->second->count(),
180                                         iter->second->get_name().c_str(),
181                                         ValueBase::type_local_name(iter->second->get_type()).c_str()
182                                 );
183                 }
184         }
185 */
186         return true;
187 }
188
189 ValueNode::ValueNode(ValueBase::Type type):type(type)
190 {
191         value_node_count++;
192 }
193
194 LinkableValueNode::Book&
195 LinkableValueNode::book()
196 {
197         return *book_;
198 }
199
200 LinkableValueNode::Handle
201 LinkableValueNode::create(const String &name, const ValueBase& x)
202 {
203         if(!book().count(name))
204                 return 0;
205
206         if (!check_type(name, x.get_type()) &&
207                 // the Duplicate ValueNode is an exception - we don't want the
208                 // user creating it for themselves, so check_type() fails for
209                 // it even when it is valid
210                 !(name == "duplicate" && x.get_type() == ValueBase::TYPE_REAL))
211         {
212                 error(_("Bad type: ValueNode '%s' doesn't accept type '%s'"), book()[name].local_name.c_str(), ValueBase::type_local_name(x.get_type()).c_str());
213                 return 0;
214         }
215
216         return book()[name].factory(x);
217 }
218
219 bool
220 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
221 {
222         if(!book().count(name) || !book()[name].check_type)
223                 return false;
224         return book()[name].check_type(x);
225 }
226
227 bool
228 LinkableValueNode::set_link(int i,ValueNode::Handle x)
229 {
230         ValueNode::Handle previous(get_link(i));
231
232         if(set_link_vfunc(i,x))
233         {
234                 if(previous)
235                         remove_child(previous.get());
236                 add_child(x.get());
237
238                 if(!x->is_exported() && get_parent_canvas())
239                 {
240                         x->set_parent_canvas(get_parent_canvas());
241                 }
242                 changed();
243                 return true;
244         }
245         return false;
246 }
247
248 ValueNode::LooseHandle
249 LinkableValueNode::get_link(int i)const
250 {
251         return get_link_vfunc(i);
252 }
253
254 void
255 LinkableValueNode::unlink_all()
256 {
257         for(int i=0;i<link_count();i++)
258         {
259                 ValueNode::LooseHandle value_node(get_link(i));
260                 if(value_node)
261                         value_node->parent_set.erase(this);
262         }
263 }
264
265 ValueNode::~ValueNode()
266 {
267         value_node_count--;
268
269         begin_delete();
270 }
271
272 void
273 ValueNode::on_changed()
274 {
275         etl::loose_handle<Canvas> parent_canvas = get_parent_canvas();
276         if(parent_canvas)
277                 do                                              // signal to all the ancestor canvases
278                         parent_canvas->signal_value_node_changed()(this);
279                 while (parent_canvas = parent_canvas->parent());
280         else if(get_root_canvas())
281                 get_root_canvas()->signal_value_node_changed()(this);
282
283         Node::on_changed();
284 }
285
286 int
287 ValueNode::replace(etl::handle<ValueNode> x)
288 {
289         if(x.get()==this)
290                 return 0;
291
292         while(parent_set.size())
293         {
294                 (*parent_set.begin())->add_child(x.get());
295                 (*parent_set.begin())->remove_child(this);
296                 //x->parent_set.insert(*parent_set.begin());
297                 //parent_set.erase(parent_set.begin());
298         }
299         int r(RHandle(this).replace(x));
300         x->changed();
301         return r;
302 }
303
304 void
305 ValueNode::set_id(const String &x)
306 {
307         if(name!=x)
308         {
309                 name=x;
310                 signal_id_changed_();
311         }
312 }
313
314 String
315 ValueNode::get_description(bool show_exported_name)const
316 {
317         if (dynamic_cast<const LinkableValueNode*>(this))
318                 return (dynamic_cast<const LinkableValueNode*>(this))->get_description(-1, show_exported_name);
319
320         return "ValueNode";
321 }
322
323 ValueNodeList::ValueNodeList():
324         placeholder_count_(0)
325 {
326 }
327
328 bool
329 ValueNodeList::count(const String &id)const
330 {
331         const_iterator iter;
332
333         if(id.empty())
334                 return false;
335
336         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
337                 ;
338
339         if(iter==end())
340                 return false;
341
342         return true;
343 }
344
345 ValueNode::Handle
346 ValueNodeList::find(const String &id)
347 {
348         iterator iter;
349
350         if(id.empty())
351                 throw Exception::IDNotFound("Empty ID");
352
353         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
354                 ;
355
356         if(iter==end())
357                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
358
359         return *iter;
360 }
361
362 ValueNode::ConstHandle
363 ValueNodeList::find(const String &id)const
364 {
365         const_iterator iter;
366
367         if(id.empty())
368                 throw Exception::IDNotFound("Empty ID");
369
370         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
371                 ;
372
373         if(iter==end())
374                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
375
376         return *iter;
377 }
378
379 ValueNode::Handle
380 ValueNodeList::surefind(const String &id)
381 {
382         if(id.empty())
383                 throw Exception::IDNotFound("Empty ID");
384
385         ValueNode::Handle value_node;
386
387         try
388         {
389                 value_node=find(id);
390         }
391         catch(Exception::IDNotFound)
392         {
393                 value_node=PlaceholderValueNode::create();
394                 value_node->set_id(id);
395                 push_back(value_node);
396                 placeholder_count_++;
397         }
398
399         return value_node;
400 }
401
402 bool
403 ValueNodeList::erase(ValueNode::Handle value_node)
404 {
405         assert(value_node);
406
407         iterator iter;
408
409         for(iter=begin();iter!=end();++iter)
410                 if(value_node.get()==iter->get())
411                 {
412                         std::list<ValueNode::RHandle>::erase(iter);
413                         if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
414                                 placeholder_count_--;
415                         return true;
416                 }
417         return false;
418 }
419
420 bool
421 ValueNodeList::add(ValueNode::Handle value_node)
422 {
423         if(!value_node)
424                 return false;
425         if(value_node->get_id().empty())
426                 return false;
427
428         try
429         {
430                 ValueNode::RHandle other_value_node=find(value_node->get_id());
431                 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
432                 {
433                         other_value_node->replace(value_node);
434                         placeholder_count_--;
435                         return true;
436                 }
437
438                 return false;
439         }
440         catch(Exception::IDNotFound)
441         {
442                 push_back(value_node);
443                 return true;
444         }
445
446         return false;
447 }
448
449 void
450 ValueNodeList::audit()
451 {
452         iterator iter,next;
453
454         for(next=begin(),iter=next++;iter!=end();iter=next++)
455                 if(iter->count()==1)
456                         std::list<ValueNode::RHandle>::erase(iter);
457 }
458
459
460 String
461 PlaceholderValueNode::get_name()const
462 {
463         return "placeholder";
464 }
465
466 String
467 PlaceholderValueNode::get_local_name()const
468 {
469         return _("Placeholder");
470 }
471
472 ValueNode*
473 PlaceholderValueNode::clone(const GUID& deriv_guid)const
474 {
475         ValueNode* ret(new PlaceholderValueNode());
476         ret->set_guid(get_guid()^deriv_guid);
477         return ret;
478 }
479
480 PlaceholderValueNode::Handle
481 PlaceholderValueNode::create(ValueBase::Type type)
482 {
483         return new PlaceholderValueNode(type);
484 }
485
486 ValueBase
487 PlaceholderValueNode::operator()(Time /*t*/)const
488 {
489         assert(0);
490         return ValueBase();
491 }
492
493 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
494         ValueNode(type)
495 {
496 }
497
498 ValueNode*
499 LinkableValueNode::clone(const GUID& deriv_guid)const
500 {
501         // printf("%s:%d clone()\n", __FILE__, __LINE__);
502         { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)
503                                                                                                                                   {
504                                                                                                                                           printf("VALUENODE FOUND VALUENODE\n");
505                                                                                                                                           return x;
506                                                                                                                                   }}
507
508         int i;
509         LinkableValueNode *ret=create_new();
510         ret->set_guid(get_guid()^deriv_guid);
511
512         for(i=0;i<link_count();i++)
513         {
514                 ValueNode::Handle link=get_link_vfunc(i);
515                 if(!link->is_exported())
516                 {
517                         ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
518                         if(!value_node)
519                                 value_node=link->clone(deriv_guid);
520                         ret->set_link(i,value_node);
521                 }
522                 else
523                         ret->set_link(i,link);
524         }
525
526         return ret;
527 }
528
529 String
530 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
531 {
532         assert(is_exported());
533         assert(canvas_);
534
535         if(x.get()==canvas_.get())
536                 return get_id();
537
538         return canvas_->_get_relative_id(x)+':'+get_id();
539 }
540
541 void
542 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
543 {
544         canvas_=x; if(x) root_canvas_=x->get_root();
545 }
546
547 void
548 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
549 {
550         root_canvas_=x->get_root();
551 }
552
553 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
554 {
555         ValueNode::LooseHandle  h;
556
557         int size = link_count();
558
559         //just add it to the set...
560         for(int i=0; i < size; ++i)
561         {
562                 h = get_link(i);
563
564                 if(h)
565                 {
566                         const Node::time_set &tset = h->get_times();
567                         set.insert(tset.begin(),tset.end());
568                 }
569         }
570 }
571
572 String
573 LinkableValueNode::get_description(int index, bool show_exported_name)const
574 {
575         String description;
576
577         if (show_exported_name && !is_exported())
578                 show_exported_name = false;
579
580         if (index != -1)
581                 description = String(":") + link_local_name(index);
582
583         const synfig::Node* node = this;
584         LinkableValueNode::ConstHandle parent_linkable_vn = 0;
585
586         // walk up through the valuenodes trying to find the layer at the top
587         while (!node->parent_set.empty() && !dynamic_cast<const Layer*>(node))
588         {
589                 LinkableValueNode::ConstHandle linkable_value_node(dynamic_cast<const LinkableValueNode*>(node));
590                 if (linkable_value_node)
591                 {
592                         String link;
593                         int cnt = linkable_value_node->link_count();
594                         for (int i = 0; i < cnt; i++)
595                                 if (linkable_value_node->get_link(i) == parent_linkable_vn)
596                                 {
597                                         link = String(":") + linkable_value_node->link_local_name(i);
598                                         break;
599                                 }
600
601                         description = linkable_value_node->get_local_name() + link + (parent_linkable_vn?">":"") + description;
602                 }
603                 node = *node->parent_set.begin();
604                 parent_linkable_vn = linkable_value_node;
605         }
606
607         Layer::ConstHandle parent_layer(dynamic_cast<const Layer*>(node));
608         if(parent_layer)
609         {
610                 String param;
611                 const Layer::DynamicParamList &dynamic_param_list(parent_layer->dynamic_param_list());
612                 // loop to find the parameter in the dynamic parameter list - this gives us its name
613                 for (Layer::DynamicParamList::const_iterator iter = dynamic_param_list.begin(); iter != dynamic_param_list.end(); iter++)
614                         if (iter->second == parent_linkable_vn)
615                                 param = String(":") + parent_layer->get_param_local_name(iter->first);
616                 description = strprintf("(%s)%s>%s",
617                                                                 parent_layer->get_non_empty_description().c_str(),
618                                                                 param.c_str(),
619                                                                 description.c_str());
620         }
621
622         if (show_exported_name)
623                 description += strprintf(" (%s)", get_id().c_str());
624
625         return description;
626 }