1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode.cpp
3 ** \brief Implementation of the "Placeholder" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 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"
40 #include "paramdesc.h"
43 #include "valuenode_const.h"
44 #include "valuenode_linear.h"
45 #include "valuenode_composite.h"
46 #include "valuenode_reference.h"
47 #include "valuenode_scale.h"
48 #include "valuenode_blinecalctangent.h"
49 #include "valuenode_blinecalcvertex.h"
50 #include "valuenode_blinecalcwidth.h"
51 #include "valuenode_blinereversetangent.h"
52 #include "valuenode_segcalctangent.h"
53 #include "valuenode_segcalcvertex.h"
54 #include "valuenode_repeat_gradient.h"
55 #include "valuenode_stripes.h"
56 #include "valuenode_range.h"
57 #include "valuenode_add.h"
58 #include "valuenode_subtract.h"
59 #include "valuenode_timedswap.h"
60 #include "valuenode_twotone.h"
61 #include "valuenode_bline.h"
62 #include "valuenode_dynamiclist.h"
63 #include "valuenode_radialcomposite.h"
64 #include "valuenode_gradientrotate.h"
65 #include "valuenode_sine.h"
66 #include "valuenode_cos.h"
67 #include "valuenode_atan2.h"
68 #include "valuenode_exp.h"
69 #include "valuenode_switch.h"
70 #include "valuenode_timeloop.h"
71 #include "valuenode_reciprocal.h"
72 #include "valuenode_duplicate.h"
73 #include "valuenode_integer.h"
74 #include "valuenode_step.h"
75 #include "valuenode_vectorangle.h"
76 #include "valuenode_vectorlength.h"
77 #include "valuenode_vectorx.h"
78 #include "valuenode_vectory.h"
79 #include "valuenode_gradientcolor.h"
85 /* === U S I N G =========================================================== */
89 using namespace synfig;
91 /* === M A C R O S ========================================================= */
93 /* === G L O B A L S ======================================================= */
95 static int value_node_count(0);
97 static LinkableValueNode::Book *book_;
100 ValueNode::LooseHandle
101 synfig::find_value_node(const GUID& guid)
103 return guid_cast<ValueNode>(guid);
106 /* === P R O C E D U R E S ================================================= */
108 /* === M E T H O D S ======================================================= */
111 ValueNode::subsys_init()
113 book_=new LinkableValueNode::Book();
115 #define ADD_VALUENODE(class,name,local,version) \
116 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create); \
117 (*book_)[name].check_type=&class::check_type; \
118 (*book_)[name].local_name=local; \
119 (*book_)[name].release_version=version
121 #define ADD_VALUENODE2(class,name,local,version) \
122 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create_from); \
123 (*book_)[name].check_type=&class::check_type; \
124 (*book_)[name].local_name=local; \
125 (*book_)[name].release_version=version
127 ADD_VALUENODE(ValueNode_Linear, "linear", _("Linear"), RELEASE_VERSION_0_61_06);
128 ADD_VALUENODE(ValueNode_Composite, "composite", _("Composite"), RELEASE_VERSION_0_61_06);
129 ADD_VALUENODE(ValueNode_RadialComposite, "radial_composite", _("Radial Composite"), RELEASE_VERSION_0_61_06);
130 ADD_VALUENODE(ValueNode_Reference, "reference", _("Reference"), RELEASE_VERSION_0_61_06);
131 ADD_VALUENODE(ValueNode_Scale, "scale", _("Scale"), RELEASE_VERSION_0_61_06);
132 ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent", _("Segment Tangent"), RELEASE_VERSION_0_61_06);
133 ADD_VALUENODE(ValueNode_SegCalcVertex, "segcalcvertex", _("Segment Vertex"), RELEASE_VERSION_0_61_06);
134 ADD_VALUENODE(ValueNode_Stripes, "stripes", _("Stripes"), RELEASE_VERSION_0_61_06);
135 ADD_VALUENODE(ValueNode_Subtract, "subtract", _("Subtract"), RELEASE_VERSION_0_61_06);
136 ADD_VALUENODE(ValueNode_TwoTone, "twotone", _("Two-Tone"), RELEASE_VERSION_0_61_06);
137 ADD_VALUENODE(ValueNode_BLine, "bline", _("BLine"), RELEASE_VERSION_0_61_06);
138 ADD_VALUENODE2(ValueNode_DynamicList, "dynamic_list", _("Dynamic List"), RELEASE_VERSION_0_61_06);
139 ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate", _("Gradient Rotate"), RELEASE_VERSION_0_61_06);
140 ADD_VALUENODE(ValueNode_Sine, "sine", _("Sine"), RELEASE_VERSION_0_61_06);
142 ADD_VALUENODE(ValueNode_TimedSwap, "timed_swap", _("Timed Swap"), RELEASE_VERSION_0_61_07); // SVN r610
143 ADD_VALUENODE(ValueNode_Repeat_Gradient, "repeat_gradient", _("Repeat Gradient"), RELEASE_VERSION_0_61_07); // SVN r666
144 ADD_VALUENODE(ValueNode_Exp, "exp", _("Exponential"), RELEASE_VERSION_0_61_07); // SVN r739
145 ADD_VALUENODE(ValueNode_Add, "add", _("Add"), RELEASE_VERSION_0_61_07); // SVN r742
146 ADD_VALUENODE(ValueNode_BLineCalcTangent, "blinecalctangent", _("BLine Tangent"), RELEASE_VERSION_0_61_07); // SVN r744
147 ADD_VALUENODE(ValueNode_BLineCalcVertex, "blinecalcvertex", _("BLine Vertex"), RELEASE_VERSION_0_61_07); // SVN r744
148 ADD_VALUENODE(ValueNode_Range, "range", _("Range"), RELEASE_VERSION_0_61_07); // SVN r776
150 ADD_VALUENODE(ValueNode_Switch, "switch", _("Switch"), RELEASE_VERSION_0_61_08); // SVN r923
151 ADD_VALUENODE(ValueNode_Cos, "cos", _("Cos"), RELEASE_VERSION_0_61_08); // SVN r1111
152 ADD_VALUENODE(ValueNode_Atan2, "atan2", _("aTan2"), RELEASE_VERSION_0_61_08); // SVN r1132
153 ADD_VALUENODE(ValueNode_BLineRevTangent, "blinerevtangent", _("Reverse Tangent"), RELEASE_VERSION_0_61_08); // SVN r1162
154 ADD_VALUENODE(ValueNode_TimeLoop, "timeloop", _("Time Loop"), RELEASE_VERSION_0_61_08); // SVN r1226
155 ADD_VALUENODE(ValueNode_Reciprocal, "reciprocal", _("Reciprocal"), RELEASE_VERSION_0_61_08); // SVN r1238
156 ADD_VALUENODE(ValueNode_Duplicate, "duplicate", _("Duplicate"), RELEASE_VERSION_0_61_08); // SVN r1267
157 ADD_VALUENODE(ValueNode_Integer, "fromint", _("From Integer"), RELEASE_VERSION_0_61_08); // SVN r1267
158 ADD_VALUENODE(ValueNode_Step, "step", _("Step"), RELEASE_VERSION_0_61_08); // SVN r1691
159 ADD_VALUENODE(ValueNode_BLineCalcWidth, "blinecalcwidth", _("BLine Width"), RELEASE_VERSION_0_61_08); // SVN r1694
161 ADD_VALUENODE(ValueNode_VectorAngle, "vectorangle", _("Vector Angle"), RELEASE_VERSION_0_61_09); // SVN r1880
162 ADD_VALUENODE(ValueNode_VectorLength, "vectorlength", _("Vector Length"), RELEASE_VERSION_0_61_09); // SVN r1881
163 ADD_VALUENODE(ValueNode_VectorX, "vectorx", _("Vector X"), RELEASE_VERSION_0_61_09); // SVN r1882
164 ADD_VALUENODE(ValueNode_VectorY, "vectory", _("Vector Y"), RELEASE_VERSION_0_61_09); // SVN r1882
165 ADD_VALUENODE(ValueNode_GradientColor, "gradientcolor", _("Gradient Color"), RELEASE_VERSION_0_61_09); // SVN r1885
168 #undef ADD_VALUENODE2
174 ValueNode::subsys_stop()
177 /* if(global_value_node_map.size() || value_node_count)
180 synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
182 if(global_value_node_map.size()!=value_node_count)
183 synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
185 GlobalValueNodeMap::iterator iter;
186 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
188 if(!iter->second->is_exported())
189 synfig::info(_("%s: count:%d name:%s type:%s"),
190 iter->first.get_string().c_str(),
191 iter->second->count(),
192 iter->second->get_name().c_str(),
193 ValueBase::type_local_name(iter->second->get_type()).c_str()
196 synfig::info(_("%s: id:%s count:%d name:%s type:%s"),
197 iter->first.get_string().c_str(),
198 iter->second->get_id().c_str(),
199 iter->second->count(),
200 iter->second->get_name().c_str(),
201 ValueBase::type_local_name(iter->second->get_type()).c_str()
209 ValueNode::ValueNode(ValueBase::Type type):type(type)
214 LinkableValueNode::Book&
215 LinkableValueNode::book()
220 LinkableValueNode::Handle
221 LinkableValueNode::create(const String &name, const ValueBase& x)
223 if(!book().count(name))
226 if (!check_type(name, x.get_type()) &&
227 // the Duplicate ValueNode is an exception - we don't want the
228 // user creating it for themselves, so check_type() fails for
229 // it even when it is valid
230 !(name == "duplicate" && x.get_type() == ValueBase::TYPE_REAL))
232 error(_("Bad type: ValueNode '%s' doesn't accept type '%s'"), book()[name].local_name.c_str(), ValueBase::type_local_name(x.get_type()).c_str());
236 return book()[name].factory(x);
240 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
242 if(!book().count(name) || !book()[name].check_type)
244 return book()[name].check_type(x);
248 LinkableValueNode::set_link(int i,ValueNode::Handle x)
250 ValueNode::Handle previous(get_link(i));
252 if(set_link_vfunc(i,x))
255 remove_child(previous.get());
258 if(!x->is_exported() && get_parent_canvas())
260 x->set_parent_canvas(get_parent_canvas());
268 ValueNode::LooseHandle
269 LinkableValueNode::get_link(int i)const
271 return get_link_vfunc(i);
275 LinkableValueNode::unlink_all()
277 for(int i=0;i<link_count();i++)
279 ValueNode::LooseHandle value_node(get_link(i));
281 value_node->parent_set.erase(this);
285 ValueNode::~ValueNode()
293 ValueNode::on_changed()
295 etl::loose_handle<Canvas> parent_canvas = get_parent_canvas();
297 do // signal to all the ancestor canvases
298 parent_canvas->signal_value_node_changed()(this);
299 while (parent_canvas = parent_canvas->parent());
300 else if(get_root_canvas())
301 get_root_canvas()->signal_value_node_changed()(this);
307 ValueNode::replace(etl::handle<ValueNode> x)
312 while(parent_set.size())
314 (*parent_set.begin())->add_child(x.get());
315 (*parent_set.begin())->remove_child(this);
316 //x->parent_set.insert(*parent_set.begin());
317 //parent_set.erase(parent_set.begin());
319 int r(RHandle(this).replace(x));
325 ValueNode::set_id(const String &x)
330 signal_id_changed_();
335 ValueNode::get_description(bool show_exported_name)const
337 String ret(_("ValueNode"));
339 if (dynamic_cast<const LinkableValueNode*>(this))
340 return (dynamic_cast<const LinkableValueNode*>(this))->get_description(-1, show_exported_name);
342 if (show_exported_name && !is_exported())
343 show_exported_name = false;
345 if (show_exported_name)
346 ret += strprintf(" (%s)", get_id().c_str());
351 ValueNodeList::ValueNodeList():
352 placeholder_count_(0)
357 ValueNodeList::count(const String &id)const
364 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
374 ValueNodeList::find(const String &id)
379 throw Exception::IDNotFound("Empty ID");
381 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
385 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
390 ValueNode::ConstHandle
391 ValueNodeList::find(const String &id)const
396 throw Exception::IDNotFound("Empty ID");
398 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
402 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
408 ValueNodeList::surefind(const String &id)
411 throw Exception::IDNotFound("Empty ID");
413 ValueNode::Handle value_node;
419 catch(Exception::IDNotFound)
421 value_node=PlaceholderValueNode::create();
422 value_node->set_id(id);
423 push_back(value_node);
424 placeholder_count_++;
431 ValueNodeList::erase(ValueNode::Handle value_node)
437 for(iter=begin();iter!=end();++iter)
438 if(value_node.get()==iter->get())
440 std::list<ValueNode::RHandle>::erase(iter);
441 if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
442 placeholder_count_--;
449 ValueNodeList::add(ValueNode::Handle value_node)
453 if(value_node->get_id().empty())
458 ValueNode::RHandle other_value_node=find(value_node->get_id());
459 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
461 other_value_node->replace(value_node);
462 placeholder_count_--;
468 catch(Exception::IDNotFound)
470 push_back(value_node);
478 ValueNodeList::audit()
482 for(next=begin(),iter=next++;iter!=end();iter=next++)
484 std::list<ValueNode::RHandle>::erase(iter);
489 PlaceholderValueNode::get_name()const
491 return "placeholder";
495 PlaceholderValueNode::get_local_name()const
497 return _("Placeholder");
501 PlaceholderValueNode::clone(const GUID& deriv_guid)const
503 ValueNode* ret(new PlaceholderValueNode());
504 ret->set_guid(get_guid()^deriv_guid);
508 PlaceholderValueNode::Handle
509 PlaceholderValueNode::create(ValueBase::Type type)
511 return new PlaceholderValueNode(type);
515 PlaceholderValueNode::operator()(Time /*t*/)const
521 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
527 LinkableValueNode::clone(const GUID& deriv_guid)const
530 ValueNode* x(find_value_node(get_guid()^deriv_guid).get());
536 LinkableValueNode *ret=create_new();
537 ret->set_guid(get_guid()^deriv_guid);
539 for(i=0;i<link_count();i++)
541 ValueNode::Handle link=get_link_vfunc(i);
542 if(!link->is_exported())
544 ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
546 value_node=link->clone(deriv_guid);
547 ret->set_link(i,value_node);
550 ret->set_link(i,link);
557 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
559 assert(is_exported());
562 if(x.get()==canvas_.get())
565 return canvas_->_get_relative_id(x)+':'+get_id();
569 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
571 canvas_=x; if(x) root_canvas_=x->get_root();
575 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
577 root_canvas_=x->get_root();
580 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
582 ValueNode::LooseHandle h;
584 int size = link_count();
586 //just add it to the set...
587 for(int i=0; i < size; ++i)
593 const Node::time_set &tset = h->get_times();
594 set.insert(tset.begin(),tset.end());
600 LinkableValueNode::get_description(int index, bool show_exported_name)const
606 if (show_exported_name && is_exported())
607 description += strprintf(" (%s)", get_id().c_str());
611 description = String(":") + link_local_name(index);
613 if (show_exported_name)
615 ValueNode::LooseHandle link(get_link(index));
616 if (link->is_exported())
617 description += strprintf(" (%s)", link->get_id().c_str());
621 const synfig::Node* node = this;
622 LinkableValueNode::ConstHandle parent_linkable_vn = 0;
624 // walk up through the valuenodes trying to find the layer at the top
625 while (!node->parent_set.empty() && !dynamic_cast<const Layer*>(node))
627 LinkableValueNode::ConstHandle linkable_value_node(dynamic_cast<const LinkableValueNode*>(node));
628 if (linkable_value_node)
631 int cnt = linkable_value_node->link_count();
632 for (int i = 0; i < cnt; i++)
633 if (linkable_value_node->get_link(i) == parent_linkable_vn)
635 link = String(":") + linkable_value_node->link_local_name(i);
639 description = linkable_value_node->get_local_name() + link + (parent_linkable_vn?">":"") + description;
641 node = *node->parent_set.begin();
642 parent_linkable_vn = linkable_value_node;
645 Layer::ConstHandle parent_layer(dynamic_cast<const Layer*>(node));
649 const Layer::DynamicParamList &dynamic_param_list(parent_layer->dynamic_param_list());
650 // loop to find the parameter in the dynamic parameter list - this gives us its name
651 for (Layer::DynamicParamList::const_iterator iter = dynamic_param_list.begin(); iter != dynamic_param_list.end(); iter++)
652 if (iter->second == parent_linkable_vn)
653 param = String(":") + parent_layer->get_param_local_name(iter->first);
654 description = strprintf("(%s)%s>%s",
655 parent_layer->get_non_empty_description().c_str(),
657 description.c_str());