Add a 'cos' convert type, to go with 'sine'.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode.cpp
3 **      \brief Template File
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
41 #include "valuenode_const.h"
42 #include "valuenode_linear.h"
43 #include "valuenode_composite.h"
44 #include "valuenode_reference.h"
45 #include "valuenode_scale.h"
46 #include "valuenode_blinecalctangent.h"
47 #include "valuenode_blinecalcvertex.h"
48 #include "valuenode_segcalctangent.h"
49 #include "valuenode_segcalcvertex.h"
50 #include "valuenode_repeat_gradient.h"
51 #include "valuenode_stripes.h"
52 #include "valuenode_range.h"
53 #include "valuenode_add.h"
54 #include "valuenode_subtract.h"
55 #include "valuenode_timedswap.h"
56 #include "valuenode_twotone.h"
57 #include "valuenode_bline.h"
58 #include "valuenode_dynamiclist.h"
59 #include "valuenode_radialcomposite.h"
60 #include "valuenode_gradientrotate.h"
61 #include "valuenode_sine.h"
62 #include "valuenode_cos.h"
63 #include "valuenode_exp.h"
64 #include "valuenode_switch.h"
65
66 #include "layer.h"
67
68 #endif
69
70 /* === U S I N G =========================================================== */
71
72 using namespace std;
73 using namespace etl;
74 using namespace synfig;
75
76 /* === M A C R O S ========================================================= */
77
78 /* === G L O B A L S ======================================================= */
79
80 static int value_node_count(0);
81
82 static LinkableValueNode::Book *book_;
83
84
85 ValueNode::LooseHandle
86 synfig::find_value_node(const GUID& guid)
87 {
88         return guid_cast<ValueNode>(guid);
89 }
90
91 /* === P R O C E D U R E S ================================================= */
92
93 /* === M E T H O D S ======================================================= */
94
95 bool
96 ValueNode::subsys_init()
97 {
98         book_=new LinkableValueNode::Book();
99
100 #define ADD_VALUENODE(class,name,local)                                                                                                                 \
101         (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create);            \
102         (*book_)[name].check_type=&class::check_type;                                                                                           \
103         (*book_)[name].local_name=local
104
105 #define ADD_VALUENODE2(class,name,local)                                                                                                                \
106         (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create_from);       \
107         (*book_)[name].check_type=&class::check_type;                                                                                           \
108         (*book_)[name].local_name=local
109
110         ADD_VALUENODE(ValueNode_Linear,                 "linear",                       _("Linear")                             );
111         ADD_VALUENODE(ValueNode_Composite,              "composite",            _("Composite")                  );
112         ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite",     _("Radial Composite")   );
113         ADD_VALUENODE(ValueNode_Reference,              "reference",            _("Reference")                  );
114         ADD_VALUENODE(ValueNode_Repeat_Gradient,"repeat_gradient",      _("Repeat Gradient")    );
115         ADD_VALUENODE(ValueNode_Scale,                  "scale",                        _("Scale")                              );
116         ADD_VALUENODE(ValueNode_BLineCalcTangent,"blinecalctangent",_("BLine Tangent")          );
117         ADD_VALUENODE(ValueNode_BLineCalcVertex,"blinecalcvertex",      _("BLine Vertex")               );
118         ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent",       _("Segment Tangent")    );
119         ADD_VALUENODE(ValueNode_SegCalcVertex,  "segcalcvertex",        _("Segment Vertex")             );
120         ADD_VALUENODE(ValueNode_Stripes,                "stripes",                      _("Stripes")                    );
121         ADD_VALUENODE(ValueNode_Range,                  "range",                        _("Range")                              );
122         ADD_VALUENODE(ValueNode_Add,                    "add",                          _("Add")                                );
123         ADD_VALUENODE(ValueNode_Subtract,               "subtract",                     _("Subtract")                   );
124         ADD_VALUENODE(ValueNode_TimedSwap,              "timed_swap",           _("Timed Swap")                 );
125         ADD_VALUENODE(ValueNode_TwoTone,                "twotone",                      _("Two-Tone")                   );
126         ADD_VALUENODE(ValueNode_BLine,                  "bline",                        _("BLine")                              );
127         ADD_VALUENODE2(ValueNode_DynamicList,   "dynamic_list",         _("Dynamic List")               );
128         ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate",      _("Gradient Rotate")    );
129         ADD_VALUENODE(ValueNode_Sine,                   "sine",                         _("Sine")                               );
130         ADD_VALUENODE(ValueNode_Cos,                    "cos",                          _("Cos")                                );
131         ADD_VALUENODE(ValueNode_Exp,                    "exp",                          _("Exponential")                );
132         ADD_VALUENODE(ValueNode_Switch,                 "switch",                       _("Switch")                             );
133
134 #undef ADD_VALUENODE
135 #undef ADD_VALUENODE2
136
137         return true;
138 }
139
140 bool
141 ValueNode::subsys_stop()
142 {
143         delete book_;
144 /*      if(global_value_node_map.size() || value_node_count)
145         {
146                 if(value_node_count)
147                         synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
148
149                 if(global_value_node_map.size()!=value_node_count)
150                         synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
151
152                 GlobalValueNodeMap::iterator iter;
153                 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
154                 {
155                         if(!iter->second->is_exported())
156                                 synfig::info("%s: count:%d name:%s type:%s",
157                                         iter->first.get_string().c_str(),
158                                         iter->second->count(),
159                                         iter->second->get_name().c_str(),
160                                         ValueBase::type_name(iter->second->get_type()).c_str()
161                                 );
162                         else
163                                 synfig::info("%s: id:%s count:%d name:%s type:%s",
164                                         iter->first.get_string().c_str(),
165                                         iter->second->get_id().c_str(),
166                                         iter->second->count(),
167                                         iter->second->get_name().c_str(),
168                                         ValueBase::type_name(iter->second->get_type()).c_str()
169                                 );
170                 }
171         }
172 */
173         return true;
174 }
175
176 ValueNode::ValueNode(ValueBase::Type type):type(type)
177 {
178         value_node_count++;
179 }
180
181 LinkableValueNode::Book&
182 LinkableValueNode::book()
183 {
184         return *book_;
185 }
186
187 LinkableValueNode::Handle
188 LinkableValueNode::create(const String &name, const ValueBase& x)
189 {
190         if(!book().count(name))
191                 return 0;
192         return book()[name].factory(x);
193 }
194
195 bool
196 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
197 {
198         if(!book().count(name) || !book()[name].check_type)
199                 return false;
200         return book()[name].check_type(x);
201 }
202
203 bool
204 LinkableValueNode::set_link(int i,ValueNode::Handle x)
205 {
206         ValueNode::Handle previous(get_link(i));
207
208         if(set_link_vfunc(i,x))
209         {
210                 if(previous)
211                         remove_child(previous.get());
212                 add_child(x.get());
213
214                 if(!x->is_exported() && get_parent_canvas())
215                 {
216                         x->set_parent_canvas(get_parent_canvas());
217                 }
218                 changed();
219                 return true;
220         }
221         return false;
222 }
223
224 ValueNode::LooseHandle
225 LinkableValueNode::get_link(int i)const
226 {
227         return get_link_vfunc(i);
228 }
229
230 void
231 LinkableValueNode::unlink_all()
232 {
233         for(int i=0;i<link_count();i++)
234         {
235                 ValueNode::LooseHandle value_node(get_link(i));
236                 if(value_node)
237                         value_node->parent_set.erase(this);
238         }
239 }
240
241 ValueNode::~ValueNode()
242 {
243         value_node_count--;
244
245         begin_delete();
246
247         //DEBUGPOINT();
248 }
249
250 void
251 ValueNode::on_changed()
252 {
253         etl::loose_handle<Canvas> parent_canvas = get_parent_canvas();
254         if(parent_canvas)
255                 do                                              // signal to all the ancestor canvases
256                         parent_canvas->signal_value_node_changed()(this);
257                 while (parent_canvas = parent_canvas->parent());
258         else if(get_root_canvas())
259                 get_root_canvas()->signal_value_node_changed()(this);
260
261         Node::on_changed();
262 }
263
264 int
265 ValueNode::replace(etl::handle<ValueNode> x)
266 {
267         if(x.get()==this)
268                 return 0;
269
270         while(parent_set.size())
271         {
272                 (*parent_set.begin())->add_child(x.get());
273                 (*parent_set.begin())->remove_child(this);
274                 //x->parent_set.insert(*parent_set.begin());
275                 //parent_set.erase(parent_set.begin());
276         }
277         int r(RHandle(this).replace(x));
278         x->changed();
279         return r;
280 }
281
282 void
283 ValueNode::set_id(const String &x)
284 {
285         if(name!=x)
286         {
287                 name=x;
288                 signal_id_changed_();
289         }
290 }
291
292 ValueNodeList::ValueNodeList():
293         placeholder_count_(0)
294 {
295 }
296
297 bool
298 ValueNodeList::count(const String &id)const
299 {
300         const_iterator iter;
301
302         if(id.empty())
303                 return false;
304
305         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
306
307         if(iter==end())
308                 return false;
309
310         return true;
311 }
312
313 ValueNode::Handle
314 ValueNodeList::find(const String &id)
315 {
316         iterator iter;
317
318         if(id.empty())
319                 throw Exception::IDNotFound("Empty ID");
320
321         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
322
323         if(iter==end())
324                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
325
326         return *iter;
327 }
328
329 ValueNode::ConstHandle
330 ValueNodeList::find(const String &id)const
331 {
332         const_iterator iter;
333
334         if(id.empty())
335                 throw Exception::IDNotFound("Empty ID");
336
337         for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
338
339         if(iter==end())
340                 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
341
342         return *iter;
343 }
344
345 ValueNode::Handle
346 ValueNodeList::surefind(const String &id)
347 {
348         if(id.empty())
349                 throw Exception::IDNotFound("Empty ID");
350
351         ValueNode::Handle value_node;
352
353         try
354         {
355                 value_node=find(id);
356         }
357         catch(Exception::IDNotFound)
358         {
359                 value_node=PlaceholderValueNode::create();
360                 value_node->set_id(id);
361                 push_back(value_node);
362                 placeholder_count_++;
363         }
364
365         return value_node;
366 }
367
368 bool
369 ValueNodeList::erase(ValueNode::Handle value_node)
370 {
371         assert(value_node);
372
373         iterator iter;
374
375         for(iter=begin();iter!=end();++iter)
376                 if(value_node.get()==iter->get())
377                 {
378                         std::list<ValueNode::RHandle>::erase(iter);
379                         if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
380                                 placeholder_count_--;
381                         return true;
382                 }
383         return false;
384 }
385
386 bool
387 ValueNodeList::add(ValueNode::Handle value_node)
388 {
389         if(!value_node)
390                 return false;
391         if(value_node->get_id().empty())
392                 return false;
393
394         try
395         {
396                 ValueNode::RHandle other_value_node=find(value_node->get_id());
397                 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
398                 {
399                         other_value_node->replace(value_node);
400                         placeholder_count_--;
401                         return true;
402                 }
403
404                 return false;
405         }
406         catch(Exception::IDNotFound)
407         {
408                 push_back(value_node);
409                 return true;
410         }
411
412         return false;
413 }
414
415 void
416 ValueNodeList::audit()
417 {
418         iterator iter,next;
419
420         for(next=begin(),iter=next++;iter!=end();iter=next++)
421                 if(iter->count()==1)
422                         std::list<ValueNode::RHandle>::erase(iter);
423 }
424
425
426 String
427 PlaceholderValueNode::get_name()const
428 {
429         return "placeholder";
430 }
431
432 String
433 PlaceholderValueNode::get_local_name()const
434 {
435         return _("Placeholder");
436 }
437
438 ValueNode*
439 PlaceholderValueNode::clone(const GUID& deriv_guid)const
440 {
441         ValueNode* ret(new PlaceholderValueNode());
442         ret->set_guid(get_guid()^deriv_guid);
443         return ret;
444 }
445
446 PlaceholderValueNode::Handle
447 PlaceholderValueNode::create(ValueBase::Type type)
448 {
449         return new PlaceholderValueNode(type);
450 }
451
452 ValueBase
453 PlaceholderValueNode::operator()(Time /*t*/)const
454 {
455         assert(0);
456         return ValueBase();
457 }
458
459 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
460         ValueNode(type)
461 {
462 }
463
464 ValueNode*
465 LinkableValueNode::clone(const GUID& deriv_guid)const
466 {
467         { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
468
469         int i;
470         LinkableValueNode *ret=create_new();
471         ret->set_guid(get_guid()^deriv_guid);
472
473         for(i=0;i<link_count();i++)
474         {
475                 ValueNode::Handle link=get_link_vfunc(i);
476                 if(!link->is_exported())
477                 {
478                         ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
479                         if(!value_node)
480                                 value_node=link->clone(deriv_guid);
481                         ret->set_link(i,value_node);
482                 }
483                 else
484                         ret->set_link(i,link);
485         }
486
487         return ret;
488 }
489
490 String
491 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
492 {
493         assert(is_exported());
494         assert(canvas_);
495
496         if(x.get()==canvas_.get())
497                 return get_id();
498
499         return canvas_->_get_relative_id(x)+':'+get_id();
500 }
501
502 void
503 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
504 {
505         canvas_=x; if(x) root_canvas_=x->get_root();
506 }
507
508 void
509 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
510 {
511         root_canvas_=x->get_root();
512 }
513
514 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
515 {
516         ValueNode::LooseHandle  h;
517
518         int size = link_count();
519
520         //just add it to the set...
521         for(int i=0; i < size; ++i)
522         {
523                 h = get_link(i);
524
525                 if(h)
526                 {
527                         const Node::time_set &tset = h->get_times();
528                         set.insert(tset.begin(),tset.end());
529                 }
530         }
531 }