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