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_segcalctangent.h"
46 #include "valuenode_segcalcvertex.h"
47 #include "valuenode_stripes.h"
48 #include "valuenode_subtract.h"
49 #include "valuenode_timedswap.h"
50 #include "valuenode_twotone.h"
51 #include "valuenode_bline.h"
52 #include "valuenode_dynamiclist.h"
53 #include "valuenode_radialcomposite.h"
54 #include "valuenode_gradientrotate.h"
55 #include "valuenode_sine.h"
61 /* === U S I N G =========================================================== */
65 using namespace synfig;
67 /* === M A C R O S ========================================================= */
69 /* === G L O B A L S ======================================================= */
71 static int value_node_count(0);
73 static LinkableValueNode::Book *book_;
76 ValueNode::LooseHandle
77 synfig::find_value_node(const GUID& guid)
79 return guid_cast<ValueNode>(guid);
82 /* === P R O C E D U R E S ================================================= */
84 /* === M E T H O D S ======================================================= */
87 ValueNode::subsys_init()
89 book_=new LinkableValueNode::Book();
91 #define ADD_VALUENODE(c,n,l) (*book_)[n].factory=reinterpret_cast<LinkableValueNode::Factory>(&c::create); (*book_)[n].check_type=&c::check_type;(*book_)[n].local_name=l
92 #define ADD_VALUENODE2(c,n,l) (*book_)[n].factory=reinterpret_cast<LinkableValueNode::Factory>(&c::create_from); (*book_)[n].check_type=&c::check_type;(*book_)[n].local_name=l
94 ADD_VALUENODE(ValueNode_Linear, "linear", _("Linear") );
95 ADD_VALUENODE(ValueNode_Composite, "composite", _("Composite") );
96 ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite", _("Radial Composite") );
97 ADD_VALUENODE(ValueNode_Reference, "reference", _("Reference") );
98 ADD_VALUENODE(ValueNode_Scale, "scale", _("Scale") );
99 ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent", _("Segment Tangent") );
100 ADD_VALUENODE(ValueNode_SegCalcVertex, "segcalcvertex", _("Segment Vertex") );
101 ADD_VALUENODE(ValueNode_Stripes, "stripes", _("Stripes") );
102 ADD_VALUENODE(ValueNode_Subtract, "subtract", _("Subtract") );
103 //ADD_VALUENODE(ValueNode_TimedSwap, "timed_swap", _("Timed Swap") );
104 ADD_VALUENODE(ValueNode_TwoTone, "twotone", _("Two-Tone") );
105 ADD_VALUENODE(ValueNode_BLine, "bline", _("BLine") );
106 ADD_VALUENODE2(ValueNode_DynamicList, "dynamic_list", _("Dynamic List") );
107 ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate", _("Gradient Rotate") );
108 ADD_VALUENODE(ValueNode_Sine, "sine", _("Sine") );
111 #undef ADD_VALUENODE2
117 ValueNode::subsys_stop()
120 /* if(global_value_node_map.size() || value_node_count)
123 synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
125 if(global_value_node_map.size()!=value_node_count)
126 synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
128 GlobalValueNodeMap::iterator iter;
129 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
131 if(!iter->second->is_exported())
132 synfig::info("%s: count:%d name:%s type:%s",
133 iter->first.get_string().c_str(),
134 iter->second->count(),
135 iter->second->get_name().c_str(),
136 ValueBase::type_name(iter->second->get_type()).c_str()
139 synfig::info("%s: id:%s count:%d name:%s type:%s",
140 iter->first.get_string().c_str(),
141 iter->second->get_id().c_str(),
142 iter->second->count(),
143 iter->second->get_name().c_str(),
144 ValueBase::type_name(iter->second->get_type()).c_str()
152 ValueNode::ValueNode(ValueBase::Type type):type(type)
157 LinkableValueNode::Book&
158 LinkableValueNode::book()
163 LinkableValueNode::Handle
164 LinkableValueNode::create(const String &name, const ValueBase& x)
166 if(!book().count(name))
168 return book()[name].factory(x);
172 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
174 if(!book().count(name) || !book()[name].check_type)
176 return book()[name].check_type(x);
180 LinkableValueNode::set_link(int i,ValueNode::Handle x)
182 ValueNode::Handle previous(get_link(i));
184 if(set_link_vfunc(i,x))
187 remove_child(previous.get());
190 if(!x->is_exported() && get_parent_canvas())
192 x->set_parent_canvas(get_parent_canvas());
200 ValueNode::LooseHandle
201 LinkableValueNode::get_link(int i)const
203 return get_link_vfunc(i);
207 LinkableValueNode::unlink_all()
209 for(int i=0;i<link_count();i++)
211 ValueNode::LooseHandle value_node(get_link(i));
213 value_node->parent_set.erase(this);
217 ValueNode::~ValueNode()
227 ValueNode::on_changed()
229 if(get_parent_canvas())
230 get_parent_canvas()->signal_value_node_changed()(this);
231 else if(get_root_canvas() && get_parent_canvas())
232 get_root_canvas()->signal_value_node_changed()(this);
238 ValueNode::replace(etl::handle<ValueNode> x)
243 while(parent_set.size())
245 (*parent_set.begin())->add_child(x.get());
246 (*parent_set.begin())->remove_child(this);
247 //x->parent_set.insert(*parent_set.begin());
248 //parent_set.erase(parent_set.begin());
250 int r(RHandle(this).replace(x));
256 ValueNode::set_id(const String &x)
261 signal_id_changed_();
265 ValueNodeList::ValueNodeList():
266 placeholder_count_(0)
271 ValueNodeList::count(const String &id)const
278 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
287 ValueNodeList::find(const String &id)
292 throw Exception::IDNotFound("Empty ID");
294 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
297 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
302 ValueNode::ConstHandle
303 ValueNodeList::find(const String &id)const
308 throw Exception::IDNotFound("Empty ID");
310 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
313 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
319 ValueNodeList::surefind(const String &id)
322 throw Exception::IDNotFound("Empty ID");
324 ValueNode::Handle value_node;
330 catch(Exception::IDNotFound)
332 value_node=PlaceholderValueNode::create();
333 value_node->set_id(id);
334 push_back(value_node);
335 placeholder_count_++;
342 ValueNodeList::erase(ValueNode::Handle value_node)
348 for(iter=begin();iter!=end();++iter)
349 if(value_node.get()==iter->get())
351 std::list<ValueNode::RHandle>::erase(iter);
352 if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
353 placeholder_count_--;
360 ValueNodeList::add(ValueNode::Handle value_node)
364 if(value_node->get_id().empty())
369 ValueNode::RHandle other_value_node=find(value_node->get_id());
370 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
372 other_value_node->replace(value_node);
373 placeholder_count_--;
379 catch(Exception::IDNotFound)
381 push_back(value_node);
389 ValueNodeList::audit()
393 for(next=begin(),iter=next++;iter!=end();iter=next++)
395 std::list<ValueNode::RHandle>::erase(iter);
400 PlaceholderValueNode::get_name()const
402 return "placeholder";
406 PlaceholderValueNode::get_local_name()const
408 return _("Placeholder");
412 PlaceholderValueNode::clone(const GUID& deriv_guid)const
414 ValueNode* ret(new PlaceholderValueNode());
415 ret->set_guid(get_guid()^deriv_guid);
419 PlaceholderValueNode::Handle
420 PlaceholderValueNode::create(ValueBase::Type type)
422 return new PlaceholderValueNode(type);
426 PlaceholderValueNode::operator()(Time t)const
432 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
438 LinkableValueNode::clone(const GUID& deriv_guid)const
440 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
443 LinkableValueNode *ret=create_new();
444 ret->set_guid(get_guid()^deriv_guid);
446 for(i=0;i<link_count();i++)
448 ValueNode::Handle link=get_link_vfunc(i);
449 if(!link->is_exported())
451 ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
453 value_node=link->clone(deriv_guid);
454 ret->set_link(i,value_node);
457 ret->set_link(i,link);
464 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
466 assert(is_exported());
469 if(x.get()==canvas_.get())
472 return canvas_->_get_relative_id(x)+':'+get_id();
476 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
478 canvas_=x; if(x) root_canvas_=x->get_root();
482 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
484 root_canvas_=x->get_root();
487 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
489 ValueNode::LooseHandle h;
491 int size = link_count();
493 //just add it to the set...
494 for(int i=0; i < size; ++i)
500 const Node::time_set &tset = h->get_times();
501 set.insert(tset.begin(),tset.end());