1 /* === S Y N F I G ========================================================= */
2 /*! \file valuedesclink.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
10 ** Copyright (c) 2010 Carlos López
12 ** This package is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU General Public License as
14 ** published by the Free Software Foundation; either version 2 of
15 ** the License, or (at your option) any later version.
17 ** This package is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
23 /* ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include "valuedesclink.h"
36 #include <synfigapp/canvasinterface.h>
37 #include <synfig/valuenode_const.h>
38 #include <synfig/valuenode_scale.h>
40 #include <synfigapp/general.h>
46 using namespace synfig;
47 using namespace synfigapp;
48 using namespace Action;
50 /* === M A C R O S ========================================================= */
52 ACTION_INIT(Action::ValueDescLink);
53 ACTION_SET_NAME(Action::ValueDescLink,"ValueDescLink");
54 ACTION_SET_LOCAL_NAME(Action::ValueDescLink,N_("Link"));
55 ACTION_SET_TASK(Action::ValueDescLink,"connect");
56 ACTION_SET_CATEGORY(Action::ValueDescLink,Action::CATEGORY_VALUEDESC);
57 ACTION_SET_PRIORITY(Action::ValueDescLink,0);
58 ACTION_SET_VERSION(Action::ValueDescLink,"0.0");
59 ACTION_SET_CVS_ID(Action::ValueDescLink,"$Id$");
61 ACTION_INIT(Action::ValueDescLinkOpposite);
62 ACTION_SET_NAME(Action::ValueDescLinkOpposite,"ValueDescLinkOpposite");
63 ACTION_SET_LOCAL_NAME(Action::ValueDescLinkOpposite,N_("Link Opposite"));
64 ACTION_SET_TASK(Action::ValueDescLinkOpposite,"connect");
65 ACTION_SET_CATEGORY(Action::ValueDescLinkOpposite,Action::CATEGORY_VALUEDESC);
66 ACTION_SET_PRIORITY(Action::ValueDescLinkOpposite,0);
67 ACTION_SET_VERSION(Action::ValueDescLinkOpposite,"0.0");
68 ACTION_SET_CVS_ID(Action::ValueDescLinkOpposite,"$Id$");
70 /* === G L O B A L S ======================================================= */
72 /* === P R O C E D U R E S ================================================= */
74 /* === M E T H O D S ======================================================= */
76 Action::ValueDescLink::ValueDescLink():
77 poison(false), status_level(0), link_scalar(0.0), link_opposite(false)
82 Action::ValueDescLink::get_param_vocab()
84 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
86 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
87 .set_local_name(_("ValueDesc to smart link"))
88 .set_requires_multiple()
95 Action::ValueDescLink::is_candidate(const ParamList &x)
97 return candidate_check(get_param_vocab(),x);
101 Action::ValueDescLink::set_param(const synfig::String& name, const Action::Param ¶m)
103 if(name=="time" && param.get_type()==Param::TYPE_TIME)
105 time=param.get_time();
109 // don't bother looking for the best value to use if there's already been an error
110 if (poison==true) return false;
112 if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
114 ValueDesc value_desc(param.get_value_desc());
116 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
118 if(link_value_node==value_desc.get_value_node())
121 if(link_value_node && link_value_node->is_exported())
124 status_message = (_("Cannot link two different exported values ('") +
125 value_desc.get_value_node()->get_id() + _("' and '") +
126 link_value_node->get_id()) + _("')");
130 link_value_node=value_desc.get_value_node();
131 link_scalar=value_desc.get_scalar();
132 status_message = _("Used exported ValueNode ('") + link_value_node->get_id() + _("').");
134 else if(value_desc.is_value_node())
139 status_message = _("Using the only available ValueNode.");
140 link_value_node=value_desc.get_value_node();
141 link_scalar=value_desc.get_scalar();
143 else if(link_value_node->is_exported())
145 // we've already seen an exported value, so use that rather than the current value
147 // Use the one that is referenced more
148 else if(link_value_node->rcount()!=value_desc.get_value_node()->rcount())
150 if(link_value_node->rcount()<value_desc.get_value_node()->rcount())
153 status_message = _("Using the most referenced ValueNode.");
154 link_value_node=value_desc.get_value_node();
155 link_scalar=value_desc.get_scalar();
157 else if (status_level <= 2)
160 status_message = _("Using the most referenced ValueNode.");
163 // If the current link value node is a constant and
164 // this one isn't, then give preference to the exotic
165 else if(ValueNode_Const::Handle::cast_dynamic(link_value_node) && !ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
168 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
169 link_value_node=value_desc.get_value_node();
170 link_scalar=value_desc.get_scalar();
172 else if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()) && !ValueNode_Const::Handle::cast_dynamic(link_value_node))
174 if (status_level <= 3)
177 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
180 // If both are animated, and this one has more waypoints, then use the one with more waypoints
181 else if(ValueNode_Animated::Handle::cast_dynamic(link_value_node) &&
182 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()) &&
183 ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() !=
184 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
186 if (ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() <
187 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
190 status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
191 link_value_node=value_desc.get_value_node();
192 link_scalar=value_desc.get_scalar();
194 else if (status_level <= 4)
197 status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
200 // If both are Linkable Value Nodes and has waypoint in its children, use the one with more waypoints
201 else if(LinkableValueNode::Handle::cast_dynamic(link_value_node) &&
202 LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node()) &&
203 LinkableValueNode::Handle::cast_dynamic(link_value_node)->get_times().size() !=
204 LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node())->get_times().size())
206 if(LinkableValueNode::Handle::cast_dynamic(link_value_node)->get_times().size() <
207 LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node())->get_times().size())
210 status_message = _("There's a tie for most referenced, and both are linkable value node animated; using the one with the most waypoints.");
211 link_value_node=value_desc.get_value_node();
212 link_scalar=value_desc.get_scalar();
214 else if (status_level <= 4)
217 status_message = _("There's a tie for most referenced, and both are linkable value node animated; using the one with the most waypoints.");
220 // Use the one that was least recently changed
221 else if(link_value_node->get_time_last_changed()!=value_desc.get_value_node()->get_time_last_changed())
223 if(link_value_node->get_time_last_changed()>value_desc.get_value_node()->get_time_last_changed())
226 status_message = _("Everything is tied; using the least recently modified value.");
227 link_value_node=value_desc.get_value_node();
228 link_scalar=value_desc.get_scalar();
230 else if (status_level <= 5)
233 status_message = _("Everything is tied; using the least recently modified value.");
239 status_message = _("Absolutely everything is tied.");
243 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
245 // Everything must be of the same type
247 status_message = (strprintf(_("Cannot link two values of different types ('%s' and '%s')"),
248 ValueBase::type_local_name(value_desc.get_value_type()).c_str(),
249 ValueBase::type_local_name(value_desc_list.front().get_value_type()).c_str()));
253 value_desc_list.push_back(value_desc);
258 return Action::CanvasSpecific::set_param(name,param);
262 Action::ValueDescLink::is_ready()const
266 if(value_desc_list.size()<=1)
268 return Action::CanvasSpecific::is_ready();
272 Action::ValueDescLink::prepare()
275 throw Error(status_message.c_str());
277 if(value_desc_list.empty())
278 throw Error(Error::TYPE_NOTREADY);
284 status_message = _("No ValueNodes were available, so one was created.");
285 ValueDesc& value_desc(value_desc_list.back());
287 link_value_node=ValueNode_Const::create(value_desc.get_value(time));
289 Action::Handle action(Action::create("ValueDescConnect"));
291 action->set_param("canvas",get_canvas());
292 action->set_param("canvas_interface",get_canvas_interface());
293 action->set_param("src",link_value_node);
294 action->set_param("dest",value_desc);
296 assert(action->is_ready());
297 if(!action->is_ready())
298 throw Error(Error::TYPE_NOTREADY);
300 add_action_front(action);
303 // Check if the selected link value node is already a scale -1.0 Linkable Value Node
304 bool link_is_scaled(false);
305 if(synfig::ValueNode_Scale::Handle::cast_dynamic(link_value_node))
307 synfig::ValueNode_Const::Handle scale_vn(
308 synfig::ValueNode_Const::Handle::cast_dynamic(
309 synfig::ValueNode_Scale::Handle::cast_dynamic(link_value_node)->get_link(1)
313 if((*scale_vn)(synfig::Time(0))==synfig::ValueBase(Real(-1.0)))
317 std::list<ValueDesc>::iterator iter;
318 // Gets the scalar value of the current value node
319 Real current_scalar(value_desc_list.begin()->get_scalar());
320 bool found_inverse(false);
321 // Check if we are dealing the case of linking differnt types of tangents
322 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
324 ValueDesc& v_desc(*iter);
325 // If parent is linkable value node
326 if(v_desc.parent_is_linkable_value_node())
328 // if the link describe to any tangent (index 4 or 5), continue
329 if(v_desc.get_index() == 4 || v_desc.get_index() == 5)
331 synfig::Real iter_scalar=v_desc.get_scalar();
332 // Let's compare the sign of scalar of the value node witht the current one
333 // and remember if a change of sign is seen.
334 if(iter_scalar*current_scalar < 0) // if they have different signs
337 current_scalar=iter_scalar;
340 else // link doesn't describe a tangent
346 else // parent is not a linkable value node
352 // found_inverse = true only if all they are tangents and some are inversed tangents
353 //Noe let's loop throug all the value desc
354 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
356 ValueDesc& value_desc(*iter);
358 // only one of the selected items can be exported - that's the one we're linking to
359 // don't link it to itself
360 if (value_desc.is_exported())
362 // Don't link the selected to itself (maybe it is redundant with the previous check)
363 if(value_desc.get_value_node() == link_value_node)
365 // found_inverse xor link_opposite
366 // If found_inverse and not link_opposite then scale by -1 first (smart link)
367 // If found inverse and link_opposite then do a direct link instead
368 // If not found_inverse and not link_opposite then do a direct link instead
369 // If not found_inverse and link_opposite then scale by -1 first (smart link)
370 if((found_inverse && !link_opposite) || (!found_inverse && link_opposite))
372 //Check if the current value node has opposite scalar than the link
373 // value node to convert to scale -1.0 before connect.
374 // Check also if the link value node is NOT also a scale -1
375 // And check also if we are linking opposite
376 if( (value_desc.get_scalar()*link_scalar<0 || link_opposite) && (link_is_scaled==false))
378 //Let's create a Scale Value Node
379 synfig::ValueNode::Handle scale_value_node=synfig::LinkableValueNode::create("scale",iter->get_value(time));
380 if(!scale_value_node)
381 throw Error(Error::TYPE_BUG);
382 scale_value_node->set_parent_canvas(get_canvas());
383 //Let's connect the new Scale Value Node to the value node
384 Action::Handle action1(Action::create("ValueDescConnect"));
386 throw Error(Error::TYPE_CRITICAL);
387 action1->set_param("canvas",get_canvas());
388 action1->set_param("canvas_interface",get_canvas_interface());
389 action1->set_param("dest",value_desc);
390 action1->set_param("src",scale_value_node);
391 assert(action1->is_ready());
392 if(!action1->is_ready())
393 throw Error(Error::TYPE_NOTREADY);
394 add_action_front(action1);
396 //Let's Connect the link value node to the scale value node link subparam
397 Action::Handle action2(Action::create("ValueNodeLinkConnect"));
399 throw Error(Error::TYPE_CRITICAL);
401 action2->set_param("canvas",get_canvas());
402 action2->set_param("canvas_interface",get_canvas_interface());
403 action2->set_param("parent_value_node",scale_value_node);
404 action2->set_param("index",0);
405 action2->set_param("value_node",link_value_node);
406 assert(action2->is_ready());
407 if(!action2->is_ready())
408 throw Error(Error::TYPE_NOTREADY);
409 add_action_front(action2);
411 //Let's Set the scale to -1
412 Action::Handle action3(Action::create("ValueNodeConstSet"));
414 throw Error(Error::TYPE_CRITICAL);
416 action3->set_param("canvas",get_canvas());
417 action3->set_param("canvas_interface",get_canvas_interface());
418 action3->set_param("value_node",synfig::LinkableValueNode::Handle::cast_dynamic(scale_value_node)->get_link(1));
419 action3->set_param("new_value",synfig::ValueBase(Real(-1.0)));
420 assert(action3->is_ready());
421 if(!action3->is_ready())
422 throw Error(Error::TYPE_NOTREADY);
423 add_action_front(action3);
425 else if((iter->get_scalar()*link_scalar<0 || link_opposite) && (link_is_scaled==true))
427 //Let's connect the link value node -> link to the value node
428 // There is not needed conversion to scale of the value node
429 // because the link value node is already a scale -1
430 Action::Handle action4(Action::create("ValueDescConnect"));
432 throw Error(Error::TYPE_CRITICAL);
433 action4->set_param("canvas",get_canvas());
434 action4->set_param("canvas_interface",get_canvas_interface());
435 action4->set_param("dest",value_desc);
436 action4->set_param("src",synfig::ValueNode_Scale::Handle::cast_dynamic(link_value_node)->get_link(0));
437 assert(action4->is_ready());
438 if(!action4->is_ready())
439 throw Error(Error::TYPE_NOTREADY);
440 add_action_front(action4);
444 //Let's connect the link value node to the value node
445 Action::Handle action(Action::create("ValueDescConnect"));
447 action->set_param("canvas",get_canvas());
448 action->set_param("canvas_interface",get_canvas_interface());
449 action->set_param("src",link_value_node);
450 action->set_param("dest",value_desc);
452 assert(action->is_ready());
453 if(!action->is_ready())
454 throw Error(Error::TYPE_NOTREADY);
456 add_action_front(action);
459 else // Not found inverse so do a regualr link
461 //Let's connect the link value node to the value node
462 Action::Handle action(Action::create("ValueDescConnect"));
464 action->set_param("canvas",get_canvas());
465 action->set_param("canvas_interface",get_canvas_interface());
466 action->set_param("src",link_value_node);
467 action->set_param("dest",value_desc);
469 assert(action->is_ready());
470 if(!action->is_ready())
471 throw Error(Error::TYPE_NOTREADY);
473 add_action_front(action);
477 synfig::info("http://synfig.org/Linking#Tier_%d : %s", status_level, status_message.c_str());
481 Action::ValueDescLinkOpposite::ValueDescLinkOpposite()
486 Action::ValueDescLinkOpposite::get_param_vocab()
488 return Action::ValueDescLink::get_param_vocab();
492 Action::ValueDescLinkOpposite::is_candidate(const ParamList &x)
494 // If action parameters are not Value Desc
495 if(!candidate_check(get_param_vocab(),x))
498 int total_tangents=0;
499 ParamList::const_iterator iter;
500 //Search thru all the Param and pick up the value descriptions
501 for(iter=x.begin(); iter!=x.end(); iter++)
503 if(iter->first == "value_desc")
505 ValueDesc v_desc(iter->second.get_value_desc());
506 // if the value description parent is linkable value node, continue
507 if(!v_desc.parent_is_linkable_value_node())
509 // if the link describe to any tangent (index 4 or 5), continue
510 if(v_desc.get_index() != 4 && v_desc.get_index() != 5)
515 // If we found two tangents then continue
516 if(total_tangents!=2)
518 // We have reached exactly two tangents
523 Action::ValueDescLinkOpposite::set_param(const synfig::String& name, const Action::Param ¶m)
525 return Action::ValueDescLink::set_param(name,param);
529 Action::ValueDescLinkOpposite::is_ready()const
531 return Action::ValueDescLink::is_ready();
535 Action::ValueDescLinkOpposite::prepare()
537 // prepare to do a opposite link and reuse the code from ValueDescLink
539 ValueDescLink::prepare();