1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode.cpp
3 ** \brief Template File
5 ** $Id: valuenode.cpp,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
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) (*book_)[n].factory=reinterpret_cast<LinkableValueNode::Factory>(&c::create); (*book_)[n].check_type=&c::check_type;(*book_)[n].local_name=n
92 #define ADD_VALUENODE2(c,n) (*book_)[n].factory=reinterpret_cast<LinkableValueNode::Factory>(&c::create_from); (*book_)[n].check_type=&c::check_type;(*book_)[n].local_name=n
94 ADD_VALUENODE(ValueNode_Linear,"linear");
95 ADD_VALUENODE(ValueNode_Composite,"composite");
96 ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite");
97 ADD_VALUENODE(ValueNode_Reference,"reference");
98 ADD_VALUENODE(ValueNode_Scale,"scale");
99 ADD_VALUENODE(ValueNode_SegCalcTangent,"segcalctangent");
100 ADD_VALUENODE(ValueNode_SegCalcVertex,"segcalcvertex");
101 ADD_VALUENODE(ValueNode_Stripes,"stripes");
102 ADD_VALUENODE(ValueNode_Subtract,"subtract");
103 //ADD_VALUENODE(ValueNode_TimedSwap,"timed_swap");
104 ADD_VALUENODE(ValueNode_TwoTone,"twotone");
105 ADD_VALUENODE(ValueNode_BLine,"bline");
106 ADD_VALUENODE2(ValueNode_DynamicList,"dynamic_list");
107 ADD_VALUENODE(ValueNode_GradientRotate,"gradient_rotate");
108 ADD_VALUENODE(ValueNode_Sine,"sine");
115 ValueNode::subsys_stop()
118 /* if(global_value_node_map.size() || value_node_count)
121 synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
123 if(global_value_node_map.size()!=value_node_count)
124 synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
126 GlobalValueNodeMap::iterator iter;
127 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
129 if(!iter->second->is_exported())
130 synfig::info("%s: count:%d name:%s type:%s",
131 iter->first.get_string().c_str(),
132 iter->second->count(),
133 iter->second->get_name().c_str(),
134 ValueBase::type_name(iter->second->get_type()).c_str()
137 synfig::info("%s: id:%s count:%d name:%s type:%s",
138 iter->first.get_string().c_str(),
139 iter->second->get_id().c_str(),
140 iter->second->count(),
141 iter->second->get_name().c_str(),
142 ValueBase::type_name(iter->second->get_type()).c_str()
150 ValueNode::ValueNode(ValueBase::Type type):type(type)
155 LinkableValueNode::Book&
156 LinkableValueNode::book()
161 LinkableValueNode::Handle
162 LinkableValueNode::create(const String &name, const ValueBase& x)
164 if(!book().count(name))
166 return book()[name].factory(x);
170 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
172 if(!book().count(name) || !book()[name].check_type)
174 return book()[name].check_type(x);
178 LinkableValueNode::set_link(int i,ValueNode::Handle x)
180 ValueNode::Handle previous(get_link(i));
182 if(set_link_vfunc(i,x))
185 remove_child(previous.get());
188 if(!x->is_exported() && get_parent_canvas())
190 x->set_parent_canvas(get_parent_canvas());
198 ValueNode::LooseHandle
199 LinkableValueNode::get_link(int i)const
201 return get_link_vfunc(i);
205 LinkableValueNode::unlink_all()
207 for(int i=0;i<link_count();i++)
209 ValueNode::LooseHandle value_node(get_link(i));
211 value_node->parent_set.erase(this);
215 ValueNode::~ValueNode()
225 ValueNode::on_changed()
227 if(get_parent_canvas())
228 get_parent_canvas()->signal_value_node_changed()(this);
229 else if(get_root_canvas() && get_parent_canvas())
230 get_root_canvas()->signal_value_node_changed()(this);
236 ValueNode::replace(etl::handle<ValueNode> x)
241 while(parent_set.size())
243 (*parent_set.begin())->add_child(x.get());
244 (*parent_set.begin())->remove_child(this);
245 //x->parent_set.insert(*parent_set.begin());
246 //parent_set.erase(parent_set.begin());
248 int r(RHandle(this).replace(x));
254 ValueNode::set_id(const String &x)
259 signal_id_changed_();
263 ValueNodeList::ValueNodeList():
264 placeholder_count_(0)
269 ValueNodeList::count(const String &id)const
276 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
285 ValueNodeList::find(const String &id)
290 throw Exception::IDNotFound("Empty ID");
292 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
295 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
300 ValueNode::ConstHandle
301 ValueNodeList::find(const String &id)const
306 throw Exception::IDNotFound("Empty ID");
308 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter);
311 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
317 ValueNodeList::surefind(const String &id)
320 throw Exception::IDNotFound("Empty ID");
322 ValueNode::Handle value_node;
328 catch(Exception::IDNotFound)
330 value_node=PlaceholderValueNode::create();
331 value_node->set_id(id);
332 push_back(value_node);
333 placeholder_count_++;
340 ValueNodeList::erase(ValueNode::Handle value_node)
346 for(iter=begin();iter!=end();++iter)
347 if(value_node.get()==iter->get())
349 std::list<ValueNode::RHandle>::erase(iter);
350 if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
351 placeholder_count_--;
358 ValueNodeList::add(ValueNode::Handle value_node)
362 if(value_node->get_id().empty())
367 ValueNode::RHandle other_value_node=find(value_node->get_id());
368 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
370 other_value_node->replace(value_node);
371 placeholder_count_--;
377 catch(Exception::IDNotFound)
379 push_back(value_node);
387 ValueNodeList::audit()
391 for(next=begin(),iter=next++;iter!=end();iter=next++)
393 std::list<ValueNode::RHandle>::erase(iter);
398 PlaceholderValueNode::get_name()const
400 return "placeholder";
404 PlaceholderValueNode::get_local_name()const
406 return _("Placeholder");
410 PlaceholderValueNode::clone(const GUID& deriv_guid)const
412 ValueNode* ret(new PlaceholderValueNode());
413 ret->set_guid(get_guid()^deriv_guid);
417 PlaceholderValueNode::Handle
418 PlaceholderValueNode::create(ValueBase::Type type)
420 return new PlaceholderValueNode(type);
424 PlaceholderValueNode::operator()(Time t)const
430 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
436 LinkableValueNode::clone(const GUID& deriv_guid)const
438 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
441 LinkableValueNode *ret=create_new();
442 ret->set_guid(get_guid()^deriv_guid);
444 for(i=0;i<link_count();i++)
446 ValueNode::Handle link=get_link_vfunc(i);
447 if(!link->is_exported())
449 ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
451 value_node=link->clone(deriv_guid);
452 ret->set_link(i,value_node);
455 ret->set_link(i,link);
462 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
464 assert(is_exported());
467 if(x.get()==canvas_.get())
470 return canvas_->_get_relative_id(x)+':'+get_id();
474 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
476 canvas_=x; if(x) root_canvas_=x->get_root();
480 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
482 root_canvas_=x->get_root();
485 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
487 ValueNode::LooseHandle h;
489 int size = link_count();
491 //just add it to the set...
492 for(int i=0; i < size; ++i)
498 const Node::time_set &tset = h->get_times();
499 set.insert(tset.begin(),tset.end());