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