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_subtract.h"
50 #include "valuenode_timedswap.h"
51 #include "valuenode_twotone.h"
52 #include "valuenode_bline.h"
53 #include "valuenode_dynamiclist.h"
54 #include "valuenode_radialcomposite.h"
55 #include "valuenode_gradientrotate.h"
56 #include "valuenode_sine.h"
62 /* === U S I N G =========================================================== */
66 using namespace synfig;
68 /* === M A C R O S ========================================================= */
70 /* === G L O B A L S ======================================================= */
72 static int value_node_count(0);
74 static LinkableValueNode::Book *book_;
77 ValueNode::LooseHandle
78 synfig::find_value_node(const GUID& guid)
80 return guid_cast<ValueNode>(guid);
83 /* === P R O C E D U R E S ================================================= */
85 /* === M E T H O D S ======================================================= */
88 ValueNode::subsys_init()
90 book_=new LinkableValueNode::Book();
92 #define ADD_VALUENODE(class,name,local) \
93 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create); \
94 (*book_)[name].check_type=&class::check_type; \
95 (*book_)[name].local_name=local
97 #define ADD_VALUENODE2(class,name,local) \
98 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create_from); \
99 (*book_)[name].check_type=&class::check_type; \
100 (*book_)[name].local_name=local
102 ADD_VALUENODE(ValueNode_Linear, "linear", _("Linear") );
103 ADD_VALUENODE(ValueNode_Composite, "composite", _("Composite") );
104 ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite", _("Radial Composite") );
105 ADD_VALUENODE(ValueNode_Reference, "reference", _("Reference") );
106 ADD_VALUENODE(ValueNode_Repeat_Gradient,"repeat_gradient", _("Repeat Gradient") );
107 ADD_VALUENODE(ValueNode_Scale, "scale", _("Scale") );
108 ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent", _("Segment Tangent") );
109 ADD_VALUENODE(ValueNode_SegCalcVertex, "segcalcvertex", _("Segment Vertex") );
110 ADD_VALUENODE(ValueNode_Stripes, "stripes", _("Stripes") );
111 ADD_VALUENODE(ValueNode_Subtract, "subtract", _("Subtract") );
112 ADD_VALUENODE(ValueNode_TimedSwap, "timed_swap", _("Timed Swap") );
113 ADD_VALUENODE(ValueNode_TwoTone, "twotone", _("Two-Tone") );
114 ADD_VALUENODE(ValueNode_BLine, "bline", _("BLine") );
115 ADD_VALUENODE2(ValueNode_DynamicList, "dynamic_list", _("Dynamic List") );
116 ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate", _("Gradient Rotate") );
117 ADD_VALUENODE(ValueNode_Sine, "sine", _("Sine") );
120 #undef ADD_VALUENODE2
126 ValueNode::subsys_stop()
129 /* if(global_value_node_map.size() || value_node_count)
132 synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
134 if(global_value_node_map.size()!=value_node_count)
135 synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
137 GlobalValueNodeMap::iterator iter;
138 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
140 if(!iter->second->is_exported())
141 synfig::info("%s: count:%d name:%s type:%s",
142 iter->first.get_string().c_str(),
143 iter->second->count(),
144 iter->second->get_name().c_str(),
145 ValueBase::type_name(iter->second->get_type()).c_str()
148 synfig::info("%s: id:%s count:%d name:%s type:%s",
149 iter->first.get_string().c_str(),
150 iter->second->get_id().c_str(),
151 iter->second->count(),
152 iter->second->get_name().c_str(),
153 ValueBase::type_name(iter->second->get_type()).c_str()
161 ValueNode::ValueNode(ValueBase::Type type):type(type)
166 LinkableValueNode::Book&
167 LinkableValueNode::book()
172 LinkableValueNode::Handle
173 LinkableValueNode::create(const String &name, const ValueBase& x)
175 if(!book().count(name))
177 return book()[name].factory(x);
181 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
183 if(!book().count(name) || !book()[name].check_type)
185 return book()[name].check_type(x);
189 LinkableValueNode::set_link(int i,ValueNode::Handle x)
191 ValueNode::Handle previous(get_link(i));
193 if(set_link_vfunc(i,x))
196 remove_child(previous.get());
199 if(!x->is_exported() && get_parent_canvas())
201 x->set_parent_canvas(get_parent_canvas());
209 ValueNode::LooseHandle
210 LinkableValueNode::get_link(int i)const
212 return get_link_vfunc(i);
216 LinkableValueNode::unlink_all()
218 for(int i=0;i<link_count();i++)
220 ValueNode::LooseHandle value_node(get_link(i));
222 value_node->parent_set.erase(this);
226 ValueNode::~ValueNode()
236 ValueNode::on_changed()
238 etl::loose_handle<Canvas> parent_canvas = get_parent_canvas();
240 do // signal to all the ancestor canvases
241 parent_canvas->signal_value_node_changed()(this);
242 while (parent_canvas = parent_canvas->parent());
243 else if(get_root_canvas())
244 get_root_canvas()->signal_value_node_changed()(this);
250 ValueNode::replace(etl::handle<ValueNode> x)
255 while(parent_set.size())
257 (*parent_set.begin())->add_child(x.get());
258 (*parent_set.begin())->remove_child(this);
259 //x->parent_set.insert(*parent_set.begin());
260 //parent_set.erase(parent_set.begin());
262 int r(RHandle(this).replace(x));
268 ValueNode::set_id(const String &x)
273 signal_id_changed_();
277 ValueNodeList::ValueNodeList():
278 placeholder_count_(0)
283 ValueNodeList::count(const String &id)const
290 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
299 ValueNodeList::find(const String &id)
304 throw Exception::IDNotFound("Empty ID");
306 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
309 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
314 ValueNode::ConstHandle
315 ValueNodeList::find(const String &id)const
320 throw Exception::IDNotFound("Empty ID");
322 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
325 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
331 ValueNodeList::surefind(const String &id)
334 throw Exception::IDNotFound("Empty ID");
336 ValueNode::Handle value_node;
342 catch(Exception::IDNotFound)
344 value_node=PlaceholderValueNode::create();
345 value_node->set_id(id);
346 push_back(value_node);
347 placeholder_count_++;
354 ValueNodeList::erase(ValueNode::Handle value_node)
360 for(iter=begin();iter!=end();++iter)
361 if(value_node.get()==iter->get())
363 std::list<ValueNode::RHandle>::erase(iter);
364 if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
365 placeholder_count_--;
372 ValueNodeList::add(ValueNode::Handle value_node)
376 if(value_node->get_id().empty())
381 ValueNode::RHandle other_value_node=find(value_node->get_id());
382 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
384 other_value_node->replace(value_node);
385 placeholder_count_--;
391 catch(Exception::IDNotFound)
393 push_back(value_node);
401 ValueNodeList::audit()
405 for(next=begin(),iter=next++;iter!=end();iter=next++)
407 std::list<ValueNode::RHandle>::erase(iter);
412 PlaceholderValueNode::get_name()const
414 return "placeholder";
418 PlaceholderValueNode::get_local_name()const
420 return _("Placeholder");
424 PlaceholderValueNode::clone(const GUID& deriv_guid)const
426 ValueNode* ret(new PlaceholderValueNode());
427 ret->set_guid(get_guid()^deriv_guid);
431 PlaceholderValueNode::Handle
432 PlaceholderValueNode::create(ValueBase::Type type)
434 return new PlaceholderValueNode(type);
438 PlaceholderValueNode::operator()(Time /*t*/)const
444 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
450 LinkableValueNode::clone(const GUID& deriv_guid)const
452 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
455 LinkableValueNode *ret=create_new();
456 ret->set_guid(get_guid()^deriv_guid);
458 for(i=0;i<link_count();i++)
460 ValueNode::Handle link=get_link_vfunc(i);
461 if(!link->is_exported())
463 ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
465 value_node=link->clone(deriv_guid);
466 ret->set_link(i,value_node);
469 ret->set_link(i,link);
476 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
478 assert(is_exported());
481 if(x.get()==canvas_.get())
484 return canvas_->_get_relative_id(x)+':'+get_id();
488 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
490 canvas_=x; if(x) root_canvas_=x->get_root();
494 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
496 root_canvas_=x->get_root();
499 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
501 ValueNode::LooseHandle h;
503 int size = link_count();
505 //just add it to the set...
506 for(int i=0; i < size; ++i)
512 const Node::time_set &tset = h->get_times();
513 set.insert(tset.begin(),tset.end());