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
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
26 #define SYNFIG_NO_ANGLE
28 //#define HAS_HASH_MAP 1
37 #include "valuenode.h"
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"
68 /* === U S I N G =========================================================== */
72 using namespace synfig;
74 /* === M A C R O S ========================================================= */
76 /* === G L O B A L S ======================================================= */
78 static int value_node_count(0);
80 static LinkableValueNode::Book *book_;
83 ValueNode::LooseHandle
84 synfig::find_value_node(const GUID& guid)
86 return guid_cast<ValueNode>(guid);
89 /* === P R O C E D U R E S ================================================= */
91 /* === M E T H O D S ======================================================= */
94 ValueNode::subsys_init()
96 book_=new LinkableValueNode::Book();
98 #define ADD_VALUENODE(class,name,local) \
99 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create); \
100 (*book_)[name].check_type=&class::check_type; \
101 (*book_)[name].local_name=local
103 #define ADD_VALUENODE2(class,name,local) \
104 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create_from); \
105 (*book_)[name].check_type=&class::check_type; \
106 (*book_)[name].local_name=local
108 ADD_VALUENODE(ValueNode_Linear, "linear", _("Linear") );
109 ADD_VALUENODE(ValueNode_Composite, "composite", _("Composite") );
110 ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite", _("Radial Composite") );
111 ADD_VALUENODE(ValueNode_Reference, "reference", _("Reference") );
112 ADD_VALUENODE(ValueNode_Repeat_Gradient,"repeat_gradient", _("Repeat Gradient") );
113 ADD_VALUENODE(ValueNode_Scale, "scale", _("Scale") );
114 ADD_VALUENODE(ValueNode_BLineCalcTangent,"blinecalctangent",_("BLine Tangent") );
115 ADD_VALUENODE(ValueNode_BLineCalcVertex,"blinecalcvertex", _("BLine Vertex") );
116 ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent", _("Segment Tangent") );
117 ADD_VALUENODE(ValueNode_SegCalcVertex, "segcalcvertex", _("Segment Vertex") );
118 ADD_VALUENODE(ValueNode_Stripes, "stripes", _("Stripes") );
119 ADD_VALUENODE(ValueNode_Range, "range", _("Range") );
120 ADD_VALUENODE(ValueNode_Add, "add", _("Add") );
121 ADD_VALUENODE(ValueNode_Subtract, "subtract", _("Subtract") );
122 ADD_VALUENODE(ValueNode_TimedSwap, "timed_swap", _("Timed Swap") );
123 ADD_VALUENODE(ValueNode_TwoTone, "twotone", _("Two-Tone") );
124 ADD_VALUENODE(ValueNode_BLine, "bline", _("BLine") );
125 ADD_VALUENODE2(ValueNode_DynamicList, "dynamic_list", _("Dynamic List") );
126 ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate", _("Gradient Rotate") );
127 ADD_VALUENODE(ValueNode_Sine, "sine", _("Sine") );
128 ADD_VALUENODE(ValueNode_Exp, "exp", _("Exponential") );
131 #undef ADD_VALUENODE2
137 ValueNode::subsys_stop()
140 /* if(global_value_node_map.size() || value_node_count)
143 synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
145 if(global_value_node_map.size()!=value_node_count)
146 synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
148 GlobalValueNodeMap::iterator iter;
149 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
151 if(!iter->second->is_exported())
152 synfig::info("%s: count:%d name:%s type:%s",
153 iter->first.get_string().c_str(),
154 iter->second->count(),
155 iter->second->get_name().c_str(),
156 ValueBase::type_name(iter->second->get_type()).c_str()
159 synfig::info("%s: id:%s count:%d name:%s type:%s",
160 iter->first.get_string().c_str(),
161 iter->second->get_id().c_str(),
162 iter->second->count(),
163 iter->second->get_name().c_str(),
164 ValueBase::type_name(iter->second->get_type()).c_str()
172 ValueNode::ValueNode(ValueBase::Type type):type(type)
177 LinkableValueNode::Book&
178 LinkableValueNode::book()
183 LinkableValueNode::Handle
184 LinkableValueNode::create(const String &name, const ValueBase& x)
186 if(!book().count(name))
188 return book()[name].factory(x);
192 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
194 if(!book().count(name) || !book()[name].check_type)
196 return book()[name].check_type(x);
200 LinkableValueNode::set_link(int i,ValueNode::Handle x)
202 ValueNode::Handle previous(get_link(i));
204 if(set_link_vfunc(i,x))
207 remove_child(previous.get());
210 if(!x->is_exported() && get_parent_canvas())
212 x->set_parent_canvas(get_parent_canvas());
220 ValueNode::LooseHandle
221 LinkableValueNode::get_link(int i)const
223 return get_link_vfunc(i);
227 LinkableValueNode::unlink_all()
229 for(int i=0;i<link_count();i++)
231 ValueNode::LooseHandle value_node(get_link(i));
233 value_node->parent_set.erase(this);
237 ValueNode::~ValueNode()
247 ValueNode::on_changed()
249 etl::loose_handle<Canvas> parent_canvas = get_parent_canvas();
251 do // signal to all the ancestor canvases
252 parent_canvas->signal_value_node_changed()(this);
253 while (parent_canvas = parent_canvas->parent());
254 else if(get_root_canvas())
255 get_root_canvas()->signal_value_node_changed()(this);
261 ValueNode::replace(etl::handle<ValueNode> x)
266 while(parent_set.size())
268 (*parent_set.begin())->add_child(x.get());
269 (*parent_set.begin())->remove_child(this);
270 //x->parent_set.insert(*parent_set.begin());
271 //parent_set.erase(parent_set.begin());
273 int r(RHandle(this).replace(x));
279 ValueNode::set_id(const String &x)
284 signal_id_changed_();
288 ValueNodeList::ValueNodeList():
289 placeholder_count_(0)
294 ValueNodeList::count(const String &id)const
301 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
310 ValueNodeList::find(const String &id)
315 throw Exception::IDNotFound("Empty ID");
317 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
320 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
325 ValueNode::ConstHandle
326 ValueNodeList::find(const String &id)const
331 throw Exception::IDNotFound("Empty ID");
333 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
336 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
342 ValueNodeList::surefind(const String &id)
345 throw Exception::IDNotFound("Empty ID");
347 ValueNode::Handle value_node;
353 catch(Exception::IDNotFound)
355 value_node=PlaceholderValueNode::create();
356 value_node->set_id(id);
357 push_back(value_node);
358 placeholder_count_++;
365 ValueNodeList::erase(ValueNode::Handle value_node)
371 for(iter=begin();iter!=end();++iter)
372 if(value_node.get()==iter->get())
374 std::list<ValueNode::RHandle>::erase(iter);
375 if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
376 placeholder_count_--;
383 ValueNodeList::add(ValueNode::Handle value_node)
387 if(value_node->get_id().empty())
392 ValueNode::RHandle other_value_node=find(value_node->get_id());
393 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
395 other_value_node->replace(value_node);
396 placeholder_count_--;
402 catch(Exception::IDNotFound)
404 push_back(value_node);
412 ValueNodeList::audit()
416 for(next=begin(),iter=next++;iter!=end();iter=next++)
418 std::list<ValueNode::RHandle>::erase(iter);
423 PlaceholderValueNode::get_name()const
425 return "placeholder";
429 PlaceholderValueNode::get_local_name()const
431 return _("Placeholder");
435 PlaceholderValueNode::clone(const GUID& deriv_guid)const
437 ValueNode* ret(new PlaceholderValueNode());
438 ret->set_guid(get_guid()^deriv_guid);
442 PlaceholderValueNode::Handle
443 PlaceholderValueNode::create(ValueBase::Type type)
445 return new PlaceholderValueNode(type);
449 PlaceholderValueNode::operator()(Time /*t*/)const
455 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
461 LinkableValueNode::clone(const GUID& deriv_guid)const
463 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
466 LinkableValueNode *ret=create_new();
467 ret->set_guid(get_guid()^deriv_guid);
469 for(i=0;i<link_count();i++)
471 ValueNode::Handle link=get_link_vfunc(i);
472 if(!link->is_exported())
474 ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
476 value_node=link->clone(deriv_guid);
477 ret->set_link(i,value_node);
480 ret->set_link(i,link);
487 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
489 assert(is_exported());
492 if(x.get()==canvas_.get())
495 return canvas_->_get_relative_id(x)+':'+get_id();
499 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
501 canvas_=x; if(x) root_canvas_=x->get_root();
505 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
507 root_canvas_=x->get_root();
510 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
512 ValueNode::LooseHandle h;
514 int size = link_count();
516 //just add it to the set...
517 for(int i=0; i < size; ++i)
523 const Node::time_set &tset = h->get_times();
524 set.insert(tset.begin(),tset.end());