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