1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
25 #define SYNFIG_NO_ANGLE
27 //#define HAS_HASH_MAP 1
36 #include "valuenode.h"
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_add.h"
52 #include "valuenode_subtract.h"
53 #include "valuenode_timedswap.h"
54 #include "valuenode_twotone.h"
55 #include "valuenode_bline.h"
56 #include "valuenode_dynamiclist.h"
57 #include "valuenode_radialcomposite.h"
58 #include "valuenode_gradientrotate.h"
59 #include "valuenode_sine.h"
60 #include "valuenode_exp.h"
66 /* === U S I N G =========================================================== */
70 using namespace synfig;
72 /* === M A C R O S ========================================================= */
74 /* === G L O B A L S ======================================================= */
76 static int value_node_count(0);
78 static LinkableValueNode::Book *book_;
81 ValueNode::LooseHandle
82 synfig::find_value_node(const GUID& guid)
84 return guid_cast<ValueNode>(guid);
87 /* === P R O C E D U R E S ================================================= */
89 /* === M E T H O D S ======================================================= */
92 ValueNode::subsys_init()
94 book_=new LinkableValueNode::Book();
96 #define ADD_VALUENODE(class,name,local) \
97 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create); \
98 (*book_)[name].check_type=&class::check_type; \
99 (*book_)[name].local_name=local
101 #define ADD_VALUENODE2(class,name,local) \
102 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create_from); \
103 (*book_)[name].check_type=&class::check_type; \
104 (*book_)[name].local_name=local
106 ADD_VALUENODE(ValueNode_Linear, "linear", _("Linear") );
107 ADD_VALUENODE(ValueNode_Composite, "composite", _("Composite") );
108 ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite", _("Radial Composite") );
109 ADD_VALUENODE(ValueNode_Reference, "reference", _("Reference") );
110 ADD_VALUENODE(ValueNode_Repeat_Gradient,"repeat_gradient", _("Repeat Gradient") );
111 ADD_VALUENODE(ValueNode_Scale, "scale", _("Scale") );
112 ADD_VALUENODE(ValueNode_BLineCalcTangent,"blinecalctangent",_("BLine Tangent") );
113 ADD_VALUENODE(ValueNode_BLineCalcVertex,"blinecalcvertex", _("BLine Vertex") );
114 ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent", _("Segment Tangent") );
115 ADD_VALUENODE(ValueNode_SegCalcVertex, "segcalcvertex", _("Segment Vertex") );
116 ADD_VALUENODE(ValueNode_Stripes, "stripes", _("Stripes") );
117 ADD_VALUENODE(ValueNode_Add, "add", _("Add") );
118 ADD_VALUENODE(ValueNode_Subtract, "subtract", _("Subtract") );
119 ADD_VALUENODE(ValueNode_TimedSwap, "timed_swap", _("Timed Swap") );
120 ADD_VALUENODE(ValueNode_TwoTone, "twotone", _("Two-Tone") );
121 ADD_VALUENODE(ValueNode_BLine, "bline", _("BLine") );
122 ADD_VALUENODE2(ValueNode_DynamicList, "dynamic_list", _("Dynamic List") );
123 ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate", _("Gradient Rotate") );
124 ADD_VALUENODE(ValueNode_Sine, "sine", _("Sine") );
125 ADD_VALUENODE(ValueNode_Exp, "exp", _("Exponential") );
128 #undef ADD_VALUENODE2
134 ValueNode::subsys_stop()
137 /* if(global_value_node_map.size() || value_node_count)
140 synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
142 if(global_value_node_map.size()!=value_node_count)
143 synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
145 GlobalValueNodeMap::iterator iter;
146 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
148 if(!iter->second->is_exported())
149 synfig::info("%s: count:%d name:%s type:%s",
150 iter->first.get_string().c_str(),
151 iter->second->count(),
152 iter->second->get_name().c_str(),
153 ValueBase::type_name(iter->second->get_type()).c_str()
156 synfig::info("%s: id:%s count:%d name:%s type:%s",
157 iter->first.get_string().c_str(),
158 iter->second->get_id().c_str(),
159 iter->second->count(),
160 iter->second->get_name().c_str(),
161 ValueBase::type_name(iter->second->get_type()).c_str()
169 ValueNode::ValueNode(ValueBase::Type type):type(type)
174 LinkableValueNode::Book&
175 LinkableValueNode::book()
180 LinkableValueNode::Handle
181 LinkableValueNode::create(const String &name, const ValueBase& x)
183 if(!book().count(name))
185 return book()[name].factory(x);
189 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
191 if(!book().count(name) || !book()[name].check_type)
193 return book()[name].check_type(x);
197 LinkableValueNode::set_link(int i,ValueNode::Handle x)
199 ValueNode::Handle previous(get_link(i));
201 if(set_link_vfunc(i,x))
204 remove_child(previous.get());
207 if(!x->is_exported() && get_parent_canvas())
209 x->set_parent_canvas(get_parent_canvas());
217 ValueNode::LooseHandle
218 LinkableValueNode::get_link(int i)const
220 return get_link_vfunc(i);
224 LinkableValueNode::unlink_all()
226 for(int i=0;i<link_count();i++)
228 ValueNode::LooseHandle value_node(get_link(i));
230 value_node->parent_set.erase(this);
234 ValueNode::~ValueNode()
244 ValueNode::on_changed()
246 etl::loose_handle<Canvas> parent_canvas = get_parent_canvas();
248 do // signal to all the ancestor canvases
249 parent_canvas->signal_value_node_changed()(this);
250 while (parent_canvas = parent_canvas->parent());
251 else if(get_root_canvas())
252 get_root_canvas()->signal_value_node_changed()(this);
258 ValueNode::replace(etl::handle<ValueNode> x)
263 while(parent_set.size())
265 (*parent_set.begin())->add_child(x.get());
266 (*parent_set.begin())->remove_child(this);
267 //x->parent_set.insert(*parent_set.begin());
268 //parent_set.erase(parent_set.begin());
270 int r(RHandle(this).replace(x));
276 ValueNode::set_id(const String &x)
281 signal_id_changed_();
285 ValueNodeList::ValueNodeList():
286 placeholder_count_(0)
291 ValueNodeList::count(const String &id)const
298 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
307 ValueNodeList::find(const String &id)
312 throw Exception::IDNotFound("Empty ID");
314 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
317 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
322 ValueNode::ConstHandle
323 ValueNodeList::find(const String &id)const
328 throw Exception::IDNotFound("Empty ID");
330 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
333 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
339 ValueNodeList::surefind(const String &id)
342 throw Exception::IDNotFound("Empty ID");
344 ValueNode::Handle value_node;
350 catch(Exception::IDNotFound)
352 value_node=PlaceholderValueNode::create();
353 value_node->set_id(id);
354 push_back(value_node);
355 placeholder_count_++;
362 ValueNodeList::erase(ValueNode::Handle value_node)
368 for(iter=begin();iter!=end();++iter)
369 if(value_node.get()==iter->get())
371 std::list<ValueNode::RHandle>::erase(iter);
372 if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
373 placeholder_count_--;
380 ValueNodeList::add(ValueNode::Handle value_node)
384 if(value_node->get_id().empty())
389 ValueNode::RHandle other_value_node=find(value_node->get_id());
390 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
392 other_value_node->replace(value_node);
393 placeholder_count_--;
399 catch(Exception::IDNotFound)
401 push_back(value_node);
409 ValueNodeList::audit()
413 for(next=begin(),iter=next++;iter!=end();iter=next++)
415 std::list<ValueNode::RHandle>::erase(iter);
420 PlaceholderValueNode::get_name()const
422 return "placeholder";
426 PlaceholderValueNode::get_local_name()const
428 return _("Placeholder");
432 PlaceholderValueNode::clone(const GUID& deriv_guid)const
434 ValueNode* ret(new PlaceholderValueNode());
435 ret->set_guid(get_guid()^deriv_guid);
439 PlaceholderValueNode::Handle
440 PlaceholderValueNode::create(ValueBase::Type type)
442 return new PlaceholderValueNode(type);
446 PlaceholderValueNode::operator()(Time /*t*/)const
452 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
458 LinkableValueNode::clone(const GUID& deriv_guid)const
460 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
463 LinkableValueNode *ret=create_new();
464 ret->set_guid(get_guid()^deriv_guid);
466 for(i=0;i<link_count();i++)
468 ValueNode::Handle link=get_link_vfunc(i);
469 if(!link->is_exported())
471 ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
473 value_node=link->clone(deriv_guid);
474 ret->set_link(i,value_node);
477 ret->set_link(i,link);
484 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
486 assert(is_exported());
489 if(x.get()==canvas_.get())
492 return canvas_->_get_relative_id(x)+':'+get_id();
496 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
498 canvas_=x; if(x) root_canvas_=x->get_root();
502 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
504 root_canvas_=x->get_root();
507 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
509 ValueNode::LooseHandle h;
511 int size = link_count();
513 //just add it to the set...
514 for(int i=0; i < size; ++i)
520 const Node::time_set &tset = h->get_times();
521 set.insert(tset.begin(),tset.end());