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