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