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