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_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"
67 /* === U S I N G =========================================================== */
71 using namespace synfig;
73 /* === M A C R O S ========================================================= */
75 /* === G L O B A L S ======================================================= */
77 static int value_node_count(0);
79 static LinkableValueNode::Book *book_;
82 ValueNode::LooseHandle
83 synfig::find_value_node(const GUID& guid)
85 return guid_cast<ValueNode>(guid);
88 /* === P R O C E D U R E S ================================================= */
90 /* === M E T H O D S ======================================================= */
93 ValueNode::subsys_init()
95 book_=new LinkableValueNode::Book();
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
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
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") );
130 #undef ADD_VALUENODE2
136 ValueNode::subsys_stop()
139 /* if(global_value_node_map.size() || value_node_count)
142 synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
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);
147 GlobalValueNodeMap::iterator iter;
148 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
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()
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()
171 ValueNode::ValueNode(ValueBase::Type type):type(type)
176 LinkableValueNode::Book&
177 LinkableValueNode::book()
182 LinkableValueNode::Handle
183 LinkableValueNode::create(const String &name, const ValueBase& x)
185 if(!book().count(name))
187 return book()[name].factory(x);
191 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
193 if(!book().count(name) || !book()[name].check_type)
195 return book()[name].check_type(x);
199 LinkableValueNode::set_link(int i,ValueNode::Handle x)
201 ValueNode::Handle previous(get_link(i));
203 if(set_link_vfunc(i,x))
206 remove_child(previous.get());
209 if(!x->is_exported() && get_parent_canvas())
211 x->set_parent_canvas(get_parent_canvas());
219 ValueNode::LooseHandle
220 LinkableValueNode::get_link(int i)const
222 return get_link_vfunc(i);
226 LinkableValueNode::unlink_all()
228 for(int i=0;i<link_count();i++)
230 ValueNode::LooseHandle value_node(get_link(i));
232 value_node->parent_set.erase(this);
236 ValueNode::~ValueNode()
246 ValueNode::on_changed()
248 etl::loose_handle<Canvas> parent_canvas = get_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);
260 ValueNode::replace(etl::handle<ValueNode> x)
265 while(parent_set.size())
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());
272 int r(RHandle(this).replace(x));
278 ValueNode::set_id(const String &x)
283 signal_id_changed_();
287 ValueNodeList::ValueNodeList():
288 placeholder_count_(0)
293 ValueNodeList::count(const String &id)const
300 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
309 ValueNodeList::find(const String &id)
314 throw Exception::IDNotFound("Empty ID");
316 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
319 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
324 ValueNode::ConstHandle
325 ValueNodeList::find(const String &id)const
330 throw Exception::IDNotFound("Empty ID");
332 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
335 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
341 ValueNodeList::surefind(const String &id)
344 throw Exception::IDNotFound("Empty ID");
346 ValueNode::Handle value_node;
352 catch(Exception::IDNotFound)
354 value_node=PlaceholderValueNode::create();
355 value_node->set_id(id);
356 push_back(value_node);
357 placeholder_count_++;
364 ValueNodeList::erase(ValueNode::Handle value_node)
370 for(iter=begin();iter!=end();++iter)
371 if(value_node.get()==iter->get())
373 std::list<ValueNode::RHandle>::erase(iter);
374 if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
375 placeholder_count_--;
382 ValueNodeList::add(ValueNode::Handle value_node)
386 if(value_node->get_id().empty())
391 ValueNode::RHandle other_value_node=find(value_node->get_id());
392 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
394 other_value_node->replace(value_node);
395 placeholder_count_--;
401 catch(Exception::IDNotFound)
403 push_back(value_node);
411 ValueNodeList::audit()
415 for(next=begin(),iter=next++;iter!=end();iter=next++)
417 std::list<ValueNode::RHandle>::erase(iter);
422 PlaceholderValueNode::get_name()const
424 return "placeholder";
428 PlaceholderValueNode::get_local_name()const
430 return _("Placeholder");
434 PlaceholderValueNode::clone(const GUID& deriv_guid)const
436 ValueNode* ret(new PlaceholderValueNode());
437 ret->set_guid(get_guid()^deriv_guid);
441 PlaceholderValueNode::Handle
442 PlaceholderValueNode::create(ValueBase::Type type)
444 return new PlaceholderValueNode(type);
448 PlaceholderValueNode::operator()(Time /*t*/)const
454 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
460 LinkableValueNode::clone(const GUID& deriv_guid)const
462 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
465 LinkableValueNode *ret=create_new();
466 ret->set_guid(get_guid()^deriv_guid);
468 for(i=0;i<link_count();i++)
470 ValueNode::Handle link=get_link_vfunc(i);
471 if(!link->is_exported())
473 ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
475 value_node=link->clone(deriv_guid);
476 ret->set_link(i,value_node);
479 ret->set_link(i,link);
486 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
488 assert(is_exported());
491 if(x.get()==canvas_.get())
494 return canvas_->_get_relative_id(x)+':'+get_id();
498 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
500 canvas_=x; if(x) root_canvas_=x->get_root();
504 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
506 root_canvas_=x->get_root();
509 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
511 ValueNode::LooseHandle h;
513 int size = link_count();
515 //just add it to the set...
516 for(int i=0; i < size; ++i)
522 const Node::time_set &tset = h->get_times();
523 set.insert(tset.begin(),tset.end());