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