Remove debugging text and unneccesary test.
[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         if(iter==end())
339                 return false;
340
341         return true;
342 }
343
344 ValueNode::Handle
345 ValueNodeList::find(const String &id)
346 {
347         iterator iter;
348
349         if(id.empty())
350                 throw Exception::IDNotFound("Empty ID");
351
352         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
353
354         if(iter==end())
355                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
356
357         return *iter;
358 }
359
360 ValueNode::ConstHandle
361 ValueNodeList::find(const String &id)const
362 {
363         const_iterator iter;
364
365         if(id.empty())
366                 throw Exception::IDNotFound("Empty ID");
367
368         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
369
370         if(iter==end())
371                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
372
373         return *iter;
374 }
375
376 ValueNode::Handle
377 ValueNodeList::surefind(const String &id)
378 {
379         if(id.empty())
380                 throw Exception::IDNotFound("Empty ID");
381
382         ValueNode::Handle value_node;
383
384         try
385         {
386                 value_node=find(id);
387         }
388         catch(Exception::IDNotFound)
389         {
390                 value_node=PlaceholderValueNode::create();
391                 value_node->set_id(id);
392                 push_back(value_node);
393                 placeholder_count_++;
394         }
395
396         return value_node;
397 }
398
399 bool
400 ValueNodeList::erase(ValueNode::Handle value_node)
401 {
402         assert(value_node);
403
404         iterator iter;
405
406         for(iter=begin();iter!=end();++iter)
407                 if(value_node.get()==iter->get())
408                 {
409                         std::list<ValueNode::RHandle>::erase(iter);
410                         if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
411                                 placeholder_count_--;
412                         return true;
413                 }
414         return false;
415 }
416
417 bool
418 ValueNodeList::add(ValueNode::Handle value_node)
419 {
420         if(!value_node)
421                 return false;
422         if(value_node->get_id().empty())
423                 return false;
424
425         try
426         {
427                 ValueNode::RHandle other_value_node=find(value_node->get_id());
428                 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
429                 {
430                         other_value_node->replace(value_node);
431                         placeholder_count_--;
432                         return true;
433                 }
434
435                 return false;
436         }
437         catch(Exception::IDNotFound)
438         {
439                 push_back(value_node);
440                 return true;
441         }
442
443         return false;
444 }
445
446 void
447 ValueNodeList::audit()
448 {
449         iterator iter,next;
450
451         for(next=begin(),iter=next++;iter!=end();iter=next++)
452                 if(iter->count()==1)
453                         std::list<ValueNode::RHandle>::erase(iter);
454 }
455
456
457 String
458 PlaceholderValueNode::get_name()const
459 {
460         return "placeholder";
461 }
462
463 String
464 PlaceholderValueNode::get_local_name()const
465 {
466         return _("Placeholder");
467 }
468
469 ValueNode*
470 PlaceholderValueNode::clone(const GUID& deriv_guid)const
471 {
472         ValueNode* ret(new PlaceholderValueNode());
473         ret->set_guid(get_guid()^deriv_guid);
474         return ret;
475 }
476
477 PlaceholderValueNode::Handle
478 PlaceholderValueNode::create(ValueBase::Type type)
479 {
480         return new PlaceholderValueNode(type);
481 }
482
483 ValueBase
484 PlaceholderValueNode::operator()(Time /*t*/)const
485 {
486         assert(0);
487         return ValueBase();
488 }
489
490 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
491         ValueNode(type)
492 {
493 }
494
495 ValueNode*
496 LinkableValueNode::clone(const GUID& deriv_guid)const
497 {
498         // printf("%s:%d clone()\n", __FILE__, __LINE__);
499         { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)
500                                                                                                                                   {
501                                                                                                                                           printf("VALUENODE FOUND VALUENODE\n");
502                                                                                                                                           return x;
503                                                                                                                                   }}
504
505         int i;
506         LinkableValueNode *ret=create_new();
507         ret->set_guid(get_guid()^deriv_guid);
508
509         for(i=0;i<link_count();i++)
510         {
511                 ValueNode::Handle link=get_link_vfunc(i);
512                 if(!link->is_exported())
513                 {
514                         ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
515                         if(!value_node)
516                                 value_node=link->clone(deriv_guid);
517                         ret->set_link(i,value_node);
518                 }
519                 else
520                         ret->set_link(i,link);
521         }
522
523         return ret;
524 }
525
526 String
527 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
528 {
529         assert(is_exported());
530         assert(canvas_);
531
532         if(x.get()==canvas_.get())
533                 return get_id();
534
535         return canvas_->_get_relative_id(x)+':'+get_id();
536 }
537
538 void
539 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
540 {
541         canvas_=x; if(x) root_canvas_=x->get_root();
542 }
543
544 void
545 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
546 {
547         root_canvas_=x->get_root();
548 }
549
550 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
551 {
552         ValueNode::LooseHandle  h;
553
554         int size = link_count();
555
556         //just add it to the set...
557         for(int i=0; i < size; ++i)
558         {
559                 h = get_link(i);
560
561                 if(h)
562                 {
563                         const Node::time_set &tset = h->get_times();
564                         set.insert(tset.begin(),tset.end());
565                 }
566         }
567 }
568
569 String
570 LinkableValueNode::get_description(int index, bool show_exported_name)const
571 {
572         String description;
573
574         if (show_exported_name && !is_exported())
575                 show_exported_name = false;
576
577         if (index != -1)
578                 description = String(":") + link_local_name(index);
579
580         const synfig::Node* node = this;
581         LinkableValueNode::ConstHandle parent_linkable_vn = 0;
582
583         // walk up through the valuenodes trying to find the layer at the top
584         while (!node->parent_set.empty() && !dynamic_cast<const Layer*>(node))
585         {
586                 LinkableValueNode::ConstHandle linkable_value_node(dynamic_cast<const LinkableValueNode*>(node));
587                 if (linkable_value_node)
588                 {
589                         String link;
590                         int cnt = linkable_value_node->link_count();
591                         for (int i = 0; i < cnt; i++)
592                                 if (linkable_value_node->get_link(i) == parent_linkable_vn)
593                                 {
594                                         link = String(":") + linkable_value_node->link_local_name(i);
595                                         break;
596                                 }
597
598                         description = linkable_value_node->get_local_name() + link + (parent_linkable_vn?">":"") + description;
599                 }
600                 node = *node->parent_set.begin();
601                 parent_linkable_vn = linkable_value_node;
602         }
603
604         Layer::ConstHandle parent_layer(dynamic_cast<const Layer*>(node));
605         if(parent_layer)
606         {
607                 String param;
608                 const Layer::DynamicParamList &dynamic_param_list(parent_layer->dynamic_param_list());
609                 // loop to find the parameter in the dynamic parameter list - this gives us its name
610                 for (Layer::DynamicParamList::const_iterator iter = dynamic_param_list.begin(); iter != dynamic_param_list.end(); iter++)
611                         if (iter->second == parent_linkable_vn)
612                                 param = String(":") + parent_layer->get_param_local_name(iter->first);
613                 description = strprintf("(%s)%s>%s",
614                                                                 parent_layer->get_non_empty_description().c_str(),
615                                                                 param.c_str(),
616                                                                 description.c_str());
617         }
618
619         if (show_exported_name)
620                 description += strprintf(" (%s)", get_id().c_str());
621
622         return description;
623 }