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 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"
42 #include "valuenode_const.h"
43 #include "valuenode_linear.h"
44 #include "valuenode_composite.h"
45 #include "valuenode_reference.h"
46 #include "valuenode_scale.h"
47 #include "valuenode_blinecalctangent.h"
48 #include "valuenode_blinecalcvertex.h"
49 #include "valuenode_blinereversetangent.h"
50 #include "valuenode_segcalctangent.h"
51 #include "valuenode_segcalcvertex.h"
52 #include "valuenode_repeat_gradient.h"
53 #include "valuenode_stripes.h"
54 #include "valuenode_range.h"
55 #include "valuenode_add.h"
56 #include "valuenode_subtract.h"
57 #include "valuenode_timedswap.h"
58 #include "valuenode_twotone.h"
59 #include "valuenode_bline.h"
60 #include "valuenode_dynamiclist.h"
61 #include "valuenode_radialcomposite.h"
62 #include "valuenode_gradientrotate.h"
63 #include "valuenode_sine.h"
64 #include "valuenode_cos.h"
65 #include "valuenode_atan2.h"
66 #include "valuenode_exp.h"
67 #include "valuenode_switch.h"
68 #include "valuenode_timeloop.h"
69 #include "valuenode_reciprocal.h"
70 #include "valuenode_duplicate.h"
71 #include "valuenode_integer.h"
77 /* === U S I N G =========================================================== */
81 using namespace synfig;
83 /* === M A C R O S ========================================================= */
85 /* === G L O B A L S ======================================================= */
87 static int value_node_count(0);
89 static LinkableValueNode::Book *book_;
92 ValueNode::LooseHandle
93 synfig::find_value_node(const GUID& guid)
95 return guid_cast<ValueNode>(guid);
98 /* === P R O C E D U R E S ================================================= */
100 /* === M E T H O D S ======================================================= */
103 ValueNode::subsys_init()
105 book_=new LinkableValueNode::Book();
107 #define ADD_VALUENODE(class,name,local) \
108 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create); \
109 (*book_)[name].check_type=&class::check_type; \
110 (*book_)[name].local_name=local
112 #define ADD_VALUENODE2(class,name,local) \
113 (*book_)[name].factory=reinterpret_cast<LinkableValueNode::Factory>(&class::create_from); \
114 (*book_)[name].check_type=&class::check_type; \
115 (*book_)[name].local_name=local
117 ADD_VALUENODE(ValueNode_Linear, "linear", _("Linear") );
118 ADD_VALUENODE(ValueNode_Composite, "composite", _("Composite") );
119 ADD_VALUENODE(ValueNode_RadialComposite,"radial_composite", _("Radial Composite") );
120 ADD_VALUENODE(ValueNode_Reference, "reference", _("Reference") );
121 ADD_VALUENODE(ValueNode_Repeat_Gradient,"repeat_gradient", _("Repeat Gradient") );
122 ADD_VALUENODE(ValueNode_Scale, "scale", _("Scale") );
123 ADD_VALUENODE(ValueNode_BLineCalcTangent,"blinecalctangent",_("BLine Tangent") );
124 ADD_VALUENODE(ValueNode_BLineCalcVertex,"blinecalcvertex", _("BLine Vertex") );
125 ADD_VALUENODE(ValueNode_BLineRevTangent,"blinerevtangent", _("Reverse Tangent") );
126 ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent", _("Segment Tangent") );
127 ADD_VALUENODE(ValueNode_SegCalcVertex, "segcalcvertex", _("Segment Vertex") );
128 ADD_VALUENODE(ValueNode_Stripes, "stripes", _("Stripes") );
129 ADD_VALUENODE(ValueNode_Range, "range", _("Range") );
130 ADD_VALUENODE(ValueNode_Add, "add", _("Add") );
131 ADD_VALUENODE(ValueNode_Subtract, "subtract", _("Subtract") );
132 ADD_VALUENODE(ValueNode_TimedSwap, "timed_swap", _("Timed Swap") );
133 ADD_VALUENODE(ValueNode_TwoTone, "twotone", _("Two-Tone") );
134 ADD_VALUENODE(ValueNode_BLine, "bline", _("BLine") );
135 ADD_VALUENODE2(ValueNode_DynamicList, "dynamic_list", _("Dynamic List") );
136 ADD_VALUENODE(ValueNode_GradientRotate, "gradient_rotate", _("Gradient Rotate") );
137 ADD_VALUENODE(ValueNode_Sine, "sine", _("Sine") );
138 ADD_VALUENODE(ValueNode_Cos, "cos", _("Cos") );
139 ADD_VALUENODE(ValueNode_Atan2, "atan2", _("aTan2") );
140 ADD_VALUENODE(ValueNode_Exp, "exp", _("Exponential") );
141 ADD_VALUENODE(ValueNode_Switch, "switch", _("Switch") );
142 ADD_VALUENODE(ValueNode_TimeLoop, "timeloop", _("Time Loop") );
143 ADD_VALUENODE(ValueNode_Reciprocal, "reciprocal", _("Reciprocal") );
144 ADD_VALUENODE(ValueNode_Duplicate, "duplicate", _("Duplicate") );
145 ADD_VALUENODE(ValueNode_Integer, "fromint", _("From Integer") );
148 #undef ADD_VALUENODE2
154 ValueNode::subsys_stop()
157 /* if(global_value_node_map.size() || value_node_count)
160 synfig::error("%d ValueNodes haven't been destroyed yet!",value_node_count);
162 if(global_value_node_map.size()!=value_node_count)
163 synfig::error("value node count mismatch! map.size()!=value_node_count (%d!=%d)",global_value_node_map.size(),value_node_count);
165 GlobalValueNodeMap::iterator iter;
166 for(iter=global_value_node_map.begin();iter!=global_value_node_map.end();++iter)
168 if(!iter->second->is_exported())
169 synfig::info(_("%s: count:%d name:%s type:%s"),
170 iter->first.get_string().c_str(),
171 iter->second->count(),
172 iter->second->get_name().c_str(),
173 ValueBase::type_local_name(iter->second->get_type()).c_str()
176 synfig::info(_("%s: id:%s count:%d name:%s type:%s"),
177 iter->first.get_string().c_str(),
178 iter->second->get_id().c_str(),
179 iter->second->count(),
180 iter->second->get_name().c_str(),
181 ValueBase::type_local_name(iter->second->get_type()).c_str()
189 ValueNode::ValueNode(ValueBase::Type type):type(type)
194 LinkableValueNode::Book&
195 LinkableValueNode::book()
200 LinkableValueNode::Handle
201 LinkableValueNode::create(const String &name, const ValueBase& x)
203 if(!book().count(name))
206 if (!check_type(name, x.get_type()) &&
207 // the Duplicate ValueNode is an exception - we don't want the
208 // user creating it for themselves, so check_type() fails for
209 // it even when it is valid
210 !(name == "duplicate" && x.get_type() == ValueBase::TYPE_REAL))
212 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());
216 return book()[name].factory(x);
220 LinkableValueNode::check_type(const String &name, ValueBase::Type x)
222 if(!book().count(name) || !book()[name].check_type)
224 return book()[name].check_type(x);
228 LinkableValueNode::set_link(int i,ValueNode::Handle x)
230 ValueNode::Handle previous(get_link(i));
232 if(set_link_vfunc(i,x))
235 remove_child(previous.get());
238 if(!x->is_exported() && get_parent_canvas())
240 x->set_parent_canvas(get_parent_canvas());
248 ValueNode::LooseHandle
249 LinkableValueNode::get_link(int i)const
251 return get_link_vfunc(i);
255 LinkableValueNode::unlink_all()
257 for(int i=0;i<link_count();i++)
259 ValueNode::LooseHandle value_node(get_link(i));
261 value_node->parent_set.erase(this);
265 ValueNode::~ValueNode()
273 ValueNode::on_changed()
275 etl::loose_handle<Canvas> parent_canvas = get_parent_canvas();
277 do // signal to all the ancestor canvases
278 parent_canvas->signal_value_node_changed()(this);
279 while (parent_canvas = parent_canvas->parent());
280 else if(get_root_canvas())
281 get_root_canvas()->signal_value_node_changed()(this);
287 ValueNode::replace(etl::handle<ValueNode> x)
292 while(parent_set.size())
294 (*parent_set.begin())->add_child(x.get());
295 (*parent_set.begin())->remove_child(this);
296 //x->parent_set.insert(*parent_set.begin());
297 //parent_set.erase(parent_set.begin());
299 int r(RHandle(this).replace(x));
305 ValueNode::set_id(const String &x)
310 signal_id_changed_();
315 ValueNode::get_description(bool show_exported_name)const
317 String ret(_("ValueNode"));
319 if (dynamic_cast<const LinkableValueNode*>(this))
320 return (dynamic_cast<const LinkableValueNode*>(this))->get_description(-1, show_exported_name);
322 if (show_exported_name && !is_exported())
323 show_exported_name = false;
325 if (show_exported_name)
326 ret += strprintf(" (%s)", get_id().c_str());
331 ValueNodeList::ValueNodeList():
332 placeholder_count_(0)
337 ValueNodeList::count(const String &id)const
344 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
354 ValueNodeList::find(const String &id)
359 throw Exception::IDNotFound("Empty ID");
361 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
365 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
370 ValueNode::ConstHandle
371 ValueNodeList::find(const String &id)const
376 throw Exception::IDNotFound("Empty ID");
378 for(iter=begin();iter!=end() && id!=(*iter)->get_id();++iter)
382 throw Exception::IDNotFound("ValueNode in ValueNodeList: "+id);
388 ValueNodeList::surefind(const String &id)
391 throw Exception::IDNotFound("Empty ID");
393 ValueNode::Handle value_node;
399 catch(Exception::IDNotFound)
401 value_node=PlaceholderValueNode::create();
402 value_node->set_id(id);
403 push_back(value_node);
404 placeholder_count_++;
411 ValueNodeList::erase(ValueNode::Handle value_node)
417 for(iter=begin();iter!=end();++iter)
418 if(value_node.get()==iter->get())
420 std::list<ValueNode::RHandle>::erase(iter);
421 if(PlaceholderValueNode::Handle::cast_dynamic(value_node))
422 placeholder_count_--;
429 ValueNodeList::add(ValueNode::Handle value_node)
433 if(value_node->get_id().empty())
438 ValueNode::RHandle other_value_node=find(value_node->get_id());
439 if(PlaceholderValueNode::Handle::cast_dynamic(other_value_node))
441 other_value_node->replace(value_node);
442 placeholder_count_--;
448 catch(Exception::IDNotFound)
450 push_back(value_node);
458 ValueNodeList::audit()
462 for(next=begin(),iter=next++;iter!=end();iter=next++)
464 std::list<ValueNode::RHandle>::erase(iter);
469 PlaceholderValueNode::get_name()const
471 return "placeholder";
475 PlaceholderValueNode::get_local_name()const
477 return _("Placeholder");
481 PlaceholderValueNode::clone(const GUID& deriv_guid)const
483 ValueNode* ret(new PlaceholderValueNode());
484 ret->set_guid(get_guid()^deriv_guid);
488 PlaceholderValueNode::Handle
489 PlaceholderValueNode::create(ValueBase::Type type)
491 return new PlaceholderValueNode(type);
495 PlaceholderValueNode::operator()(Time /*t*/)const
501 PlaceholderValueNode::PlaceholderValueNode(ValueBase::Type type):
507 LinkableValueNode::clone(const GUID& deriv_guid)const
509 // printf("%s:%d clone()\n", __FILE__, __LINE__);
510 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)
512 printf("VALUENODE FOUND VALUENODE\n");
517 LinkableValueNode *ret=create_new();
518 ret->set_guid(get_guid()^deriv_guid);
520 for(i=0;i<link_count();i++)
522 ValueNode::Handle link=get_link_vfunc(i);
523 if(!link->is_exported())
525 ValueNode::LooseHandle value_node(find_value_node(link->get_guid()^deriv_guid));
527 value_node=link->clone(deriv_guid);
528 ret->set_link(i,value_node);
531 ret->set_link(i,link);
538 ValueNode::get_relative_id(etl::loose_handle<const Canvas> x)const
540 assert(is_exported());
543 if(x.get()==canvas_.get())
546 return canvas_->_get_relative_id(x)+':'+get_id();
550 ValueNode::set_parent_canvas(etl::loose_handle<Canvas> x)
552 canvas_=x; if(x) root_canvas_=x->get_root();
556 ValueNode::set_root_canvas(etl::loose_handle<Canvas> x)
558 root_canvas_=x->get_root();
561 void LinkableValueNode::get_times_vfunc(Node::time_set &set) const
563 ValueNode::LooseHandle h;
565 int size = link_count();
567 //just add it to the set...
568 for(int i=0; i < size; ++i)
574 const Node::time_set &tset = h->get_times();
575 set.insert(tset.begin(),tset.end());
581 LinkableValueNode::get_description(int index, bool show_exported_name)const
587 if (show_exported_name && is_exported())
588 description += strprintf(" (%s)", get_id().c_str());
592 description = String(":") + link_local_name(index);
594 if (show_exported_name)
596 ValueNode::LooseHandle link(get_link(index));
597 if (link->is_exported())
598 description += strprintf(" (%s)", link->get_id().c_str());
602 const synfig::Node* node = this;
603 LinkableValueNode::ConstHandle parent_linkable_vn = 0;
605 // walk up through the valuenodes trying to find the layer at the top
606 while (!node->parent_set.empty() && !dynamic_cast<const Layer*>(node))
608 LinkableValueNode::ConstHandle linkable_value_node(dynamic_cast<const LinkableValueNode*>(node));
609 if (linkable_value_node)
612 int cnt = linkable_value_node->link_count();
613 for (int i = 0; i < cnt; i++)
614 if (linkable_value_node->get_link(i) == parent_linkable_vn)
616 link = String(":") + linkable_value_node->link_local_name(i);
620 description = linkable_value_node->get_local_name() + link + (parent_linkable_vn?">":"") + description;
622 node = *node->parent_set.begin();
623 parent_linkable_vn = linkable_value_node;
626 Layer::ConstHandle parent_layer(dynamic_cast<const Layer*>(node));
630 const Layer::DynamicParamList &dynamic_param_list(parent_layer->dynamic_param_list());
631 // loop to find the parameter in the dynamic parameter list - this gives us its name
632 for (Layer::DynamicParamList::const_iterator iter = dynamic_param_list.begin(); iter != dynamic_param_list.end(); iter++)
633 if (iter->second == parent_linkable_vn)
634 param = String(":") + parent_layer->get_param_local_name(iter->first);
635 description = strprintf("(%s)%s>%s",
636 parent_layer->get_non_empty_description().c_str(),
638 description.c_str());