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_repeat_gradient.h"
48 #include "valuenode_stripes.h"
49 #include "valuenode_add.h"
50 #include "valuenode_subtract.h"
51 #include "valuenode_timedswap.h"
52 #include "valuenode_twotone.h"
53 #include "valuenode_bline.h"
54 #include "valuenode_dynamiclist.h"
55 #include "valuenode_radialcomposite.h"
56 #include "valuenode_gradientrotate.h"
57 #include "valuenode_sine.h"
58 #include "valuenode_exp.h"
64 /* === U S I N G =========================================================== */
68 using namespace synfig;
70 /* === M A C R O S ========================================================= */
72 /* === G L O B A L S ======================================================= */
74 static int value_node_count(0);
76 static LinkableValueNode::Book *book_;
79 ValueNode::LooseHandle
80 synfig::find_value_node(const GUID& guid)
82 return guid_cast<ValueNode>(guid);
85 /* === P R O C E D U R E S ================================================= */
87 /* === M E T H O D S ======================================================= */
90 ValueNode::subsys_init()
92 book_=new LinkableValueNode::Book();
94 #define ADD_VALUENODE(class,name,local) \
95 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create); \
96 (*book_)[name].check_type=&class::check_type; \
97 (*book_)[name].local_name=local
99 #define ADD_VALUENODE2(class,name,local) \
100 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create_from); \
101 (*book_)[name].check_type=&class::check_type; \
102 (*book_)[name].local_name=local
104 ADD_VALUENODE(ValueNode_Linear, "linear", _("Linear") );
105 ADD_VALUENODE(ValueNode_Composite, "composite", _("Composite") );
106 ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite", _("Radial Composite") );
107 ADD_VALUENODE(ValueNode_Reference, "reference", _("Reference") );
108 ADD_VALUENODE(ValueNode_Repeat_Gradient,"repeat_gradient", _("Repeat Gradient") );
109 ADD_VALUENODE(ValueNode_Scale, "scale", _("Scale") );
110 ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent", _("Segment Tangent") );
111 ADD_VALUENODE(ValueNode_SegCalcVertex, "segcalcvertex", _("Segment Vertex") );
112 ADD_VALUENODE(ValueNode_Stripes, "stripes", _("Stripes") );
113 ADD_VALUENODE(ValueNode_Add, "add", _("Add") );
114 ADD_VALUENODE(ValueNode_Subtract, "subtract", _("Subtract") );
115 ADD_VALUENODE(ValueNode_TimedSwap, "timed_swap", _("Timed Swap") );
116 ADD_VALUENODE(ValueNode_TwoTone, "twotone", _("Two-Tone") );
117 ADD_VALUENODE(ValueNode_BLine, "bline", _("BLine") );
118 ADD_VALUENODE2(ValueNode_DynamicList, "dynamic_list", _("Dynamic List") );
119 ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate", _("Gradient Rotate") );
120 ADD_VALUENODE(ValueNode_Sine, "sine", _("Sine") );
121 ADD_VALUENODE(ValueNode_Exp, "exp", _("Exponential") );
124 #undef ADD_VALUENODE2
130 ValueNode::subsys_stop()
133 /* if(global_value_node_map.size() || value_node_count)
136 synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
138 if(global_value_node_map.size()!=value_node_count)
139 synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
141 GlobalValueNodeMap::iterator iter;
142 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
144 if(!iter->second->is_exported())
145 synfig::info("%s: count:%d name:%s type:%s",
146 iter->first.get_string().c_str(),
147 iter->second->count(),
148 iter->second->get_name().c_str(),
149 ValueBase::type_name(iter->second->get_type()).c_str()
152 synfig::info("%s: id:%s count:%d name:%s type:%s",
153 iter->first.get_string().c_str(),
154 iter->second->get_id().c_str(),
155 iter->second->count(),
156 iter->second->get_name().c_str(),
157 ValueBase::type_name(iter->second->get_type()).c_str()
165 ValueNode::ValueNode(ValueBase::Type type):type(type)
170 LinkableValueNode::Book&
171 LinkableValueNode::book()
176 LinkableValueNode::Handle
177 LinkableValueNode::create(const String &name, const ValueBase& x)
179 if(!book().count(name))
181 return book()[name].factory(x);
185 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
187 if(!book().count(name) || !book()[name].check_type)
189 return book()[name].check_type(x);
193 LinkableValueNode::set_link(int i,ValueNode::Handle x)
195 ValueNode::Handle previous(get_link(i));
197 if(set_link_vfunc(i,x))
200 remove_child(previous.get());
203 if(!x->is_exported() && get_parent_canvas())
205 x->set_parent_canvas(get_parent_canvas());
213 ValueNode::LooseHandle
214 LinkableValueNode::get_link(int i)const
216 return get_link_vfunc(i);
220 LinkableValueNode::unlink_all()
222 for(int i=0;i<link_count();i++)
224 ValueNode::LooseHandle value_node(get_link(i));
226 value_node->parent_set.erase(this);
230 ValueNode::~ValueNode()
240 ValueNode::on_changed()
242 etl::loose_handle<Canvas> parent_canvas = get_parent_canvas();
244 do // signal to all the ancestor canvases
245 parent_canvas->signal_value_node_changed()(this);
246 while (parent_canvas = parent_canvas->parent());
247 else if(get_root_canvas())
248 get_root_canvas()->signal_value_node_changed()(this);
254 ValueNode::replace(etl::handle<ValueNode> x)
259 while(parent_set.size())
261 (*parent_set.begin())->add_child(x.get());
262 (*parent_set.begin())->remove_child(this);
263 //x->parent_set.insert(*parent_set.begin());
264 //parent_set.erase(parent_set.begin());
266 int r(RHandle(this).replace(x));
272 ValueNode::set_id(const String &x)
277 signal_id_changed_();
281 ValueNodeList::ValueNodeList():
282 placeholder_count_(0)
287 ValueNodeList::count(const String &id)const
294 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
303 ValueNodeList::find(const String &id)
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);
318 ValueNode::ConstHandle
319 ValueNodeList::find(const String &id)const
324 throw Exception::IDNotFound("Empty ID");
326 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
329 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
335 ValueNodeList::surefind(const String &id)
338 throw Exception::IDNotFound("Empty ID");
340 ValueNode::Handle value_node;
346 catch(Exception::IDNotFound)
348 value_node=PlaceholderValueNode::create();
349 value_node->set_id(id);
350 push_back(value_node);
351 placeholder_count_++;
358 ValueNodeList::erase(ValueNode::Handle value_node)
364 for(iter=begin();iter!=end();++iter)
365 if(value_node.get()==iter->get())
367 std::list<ValueNode::RHandle>::erase(iter);
368 if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
369 placeholder_count_--;
376 ValueNodeList::add(ValueNode::Handle value_node)
380 if(value_node->get_id().empty())
385 ValueNode::RHandle other_value_node=find(value_node->get_id());
386 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
388 other_value_node->replace(value_node);
389 placeholder_count_--;
395 catch(Exception::IDNotFound)
397 push_back(value_node);
405 ValueNodeList::audit()
409 for(next=begin(),iter=next++;iter!=end();iter=next++)
411 std::list<ValueNode::RHandle>::erase(iter);
416 PlaceholderValueNode::get_name()const
418 return "placeholder";
422 PlaceholderValueNode::get_local_name()const
424 return _("Placeholder");
428 PlaceholderValueNode::clone(const GUID& deriv_guid)const
430 ValueNode* ret(new PlaceholderValueNode());
431 ret->set_guid(get_guid()^deriv_guid);
435 PlaceholderValueNode::Handle
436 PlaceholderValueNode::create(ValueBase::Type type)
438 return new PlaceholderValueNode(type);
442 PlaceholderValueNode::operator()(Time /*t*/)const
448 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
454 LinkableValueNode::clone(const GUID& deriv_guid)const
456 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
459 LinkableValueNode *ret=create_new();
460 ret->set_guid(get_guid()^deriv_guid);
462 for(i=0;i<link_count();i++)
464 ValueNode::Handle link=get_link_vfunc(i);
465 if(!link->is_exported())
467 ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
469 value_node=link->clone(deriv_guid);
470 ret->set_link(i,value_node);
473 ret->set_link(i,link);
480 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
482 assert(is_exported());
485 if(x.get()==canvas_.get())
488 return canvas_->_get_relative_id(x)+':'+get_id();
492 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
494 canvas_=x; if(x) root_canvas_=x->get_root();
498 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
500 root_canvas_=x->get_root();
503 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
505 ValueNode::LooseHandle h;
507 int size = link_count();
509 //just add it to the set...
510 for(int i=0; i < size; ++i)
516 const Node::time_set &tset = h->get_times();
517 set.insert(tset.begin(),tset.end());