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