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