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