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(0);
320 if(value_desc_list.begin()->parent_is_linkable_value_node())
321 current_scalar=value_desc_list.begin()->get_scalar();
322 bool found_inverse(false);
323 // Check if we are dealing the case of linking differnt types of tangents
324 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
326 ValueDesc& v_desc(*iter);
327 // If parent is linkable value node
328 if(v_desc.parent_is_linkable_value_node())
330 // if the link describe to any tangent (index 4 or 5), continue
331 if(v_desc.get_index() == 4 || v_desc.get_index() == 5)
333 synfig::Real iter_scalar=v_desc.get_scalar();
334 // Let's compare the sign of scalar of the value node witht the current one
335 // and remember if a change of sign is seen.
336 if(iter_scalar*current_scalar < 0) // if they have different signs
339 current_scalar=iter_scalar;
342 else // link doesn't describe a tangent
348 else // parent is not a linkable value node
354 // found_inverse = true only if all they are tangents and some are inversed tangents
355 //Noe let's loop throug all the value desc
356 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
358 ValueDesc& value_desc(*iter);
360 // only one of the selected items can be exported - that's the one we're linking to
361 // don't link it to itself
362 if (value_desc.is_exported())
364 // Don't link the selected to itself (maybe it is redundant with the previous check)
365 if(value_desc.get_value_node() == link_value_node)
367 // found_inverse xor link_opposite
368 // If found_inverse and not link_opposite then scale by -1 first (smart link)
369 // If found inverse and link_opposite then do a direct link instead
370 // If not found_inverse and not link_opposite then do a direct link instead
371 // If not found_inverse and link_opposite then scale by -1 first (smart link)
372 if((found_inverse && !link_opposite) || (!found_inverse && link_opposite))
374 //Check if the current value node has opposite scalar than the link
375 // value node to convert to scale -1.0 before connect.
376 // Check also if the link value node is NOT also a scale -1
377 // And check also if we are linking opposite
378 if( (value_desc.get_scalar()*link_scalar<0 || link_opposite) && (link_is_scaled==false))
380 //Let's create a Scale Value Node
381 synfig::ValueNode::Handle scale_value_node=synfig::LinkableValueNode::create("scale",iter->get_value(time));
382 if(!scale_value_node)
383 throw Error(Error::TYPE_BUG);
384 scale_value_node->set_parent_canvas(get_canvas());
385 //Let's connect the new Scale Value Node to the value node
386 Action::Handle action1(Action::create("ValueDescConnect"));
388 throw Error(Error::TYPE_CRITICAL);
389 action1->set_param("canvas",get_canvas());
390 action1->set_param("canvas_interface",get_canvas_interface());
391 action1->set_param("dest",value_desc);
392 action1->set_param("src",scale_value_node);
393 assert(action1->is_ready());
394 if(!action1->is_ready())
395 throw Error(Error::TYPE_NOTREADY);
396 add_action_front(action1);
398 //Let's Connect the link value node to the scale value node link subparam
399 Action::Handle action2(Action::create("ValueNodeLinkConnect"));
401 throw Error(Error::TYPE_CRITICAL);
403 action2->set_param("canvas",get_canvas());
404 action2->set_param("canvas_interface",get_canvas_interface());
405 action2->set_param("parent_value_node",scale_value_node);
406 action2->set_param("index",0);
407 action2->set_param("value_node",link_value_node);
408 assert(action2->is_ready());
409 if(!action2->is_ready())
410 throw Error(Error::TYPE_NOTREADY);
411 add_action_front(action2);
413 //Let's Set the scale to -1
414 Action::Handle action3(Action::create("ValueNodeConstSet"));
416 throw Error(Error::TYPE_CRITICAL);
418 action3->set_param("canvas",get_canvas());
419 action3->set_param("canvas_interface",get_canvas_interface());
420 action3->set_param("value_node",synfig::LinkableValueNode::Handle::cast_dynamic(scale_value_node)->get_link(1));
421 action3->set_param("new_value",synfig::ValueBase(Real(-1.0)));
422 assert(action3->is_ready());
423 if(!action3->is_ready())
424 throw Error(Error::TYPE_NOTREADY);
425 add_action_front(action3);
427 else if((iter->get_scalar()*link_scalar<0 || link_opposite) && (link_is_scaled==true))
429 //Let's connect the link value node -> link to the value node
430 // There is not needed conversion to scale of the value node
431 // because the link value node is already a scale -1
432 Action::Handle action4(Action::create("ValueDescConnect"));
434 throw Error(Error::TYPE_CRITICAL);
435 action4->set_param("canvas",get_canvas());
436 action4->set_param("canvas_interface",get_canvas_interface());
437 action4->set_param("dest",value_desc);
438 action4->set_param("src",synfig::ValueNode_Scale::Handle::cast_dynamic(link_value_node)->get_link(0));
439 assert(action4->is_ready());
440 if(!action4->is_ready())
441 throw Error(Error::TYPE_NOTREADY);
442 add_action_front(action4);
446 //Let's connect the link value node to the value node
447 Action::Handle action(Action::create("ValueDescConnect"));
449 action->set_param("canvas",get_canvas());
450 action->set_param("canvas_interface",get_canvas_interface());
451 action->set_param("src",link_value_node);
452 action->set_param("dest",value_desc);
454 assert(action->is_ready());
455 if(!action->is_ready())
456 throw Error(Error::TYPE_NOTREADY);
458 add_action_front(action);
461 else // Not found inverse so do a regualr link
463 //Let's connect the link value node to the value node
464 Action::Handle action(Action::create("ValueDescConnect"));
466 action->set_param("canvas",get_canvas());
467 action->set_param("canvas_interface",get_canvas_interface());
468 action->set_param("src",link_value_node);
469 action->set_param("dest",value_desc);
471 assert(action->is_ready());
472 if(!action->is_ready())
473 throw Error(Error::TYPE_NOTREADY);
475 add_action_front(action);
479 synfig::info("http://synfig.org/Linking#Tier_%d : %s", status_level, status_message.c_str());
483 Action::ValueDescLinkOpposite::ValueDescLinkOpposite()
488 Action::ValueDescLinkOpposite::get_param_vocab()
490 return Action::ValueDescLink::get_param_vocab();
494 Action::ValueDescLinkOpposite::is_candidate(const ParamList &x)
496 // If action parameters are not Value Desc
497 if(!candidate_check(get_param_vocab(),x))
500 int total_tangents=0;
501 ParamList::const_iterator iter;
502 //Search thru all the Param and pick up the value descriptions
503 for(iter=x.begin(); iter!=x.end(); iter++)
505 if(iter->first == "value_desc")
507 ValueDesc v_desc(iter->second.get_value_desc());
508 // if the value description parent is linkable value node, continue
509 if(!v_desc.parent_is_linkable_value_node())
511 // if the link describe to any tangent (index 4 or 5), continue
512 if(v_desc.get_index() != 4 && v_desc.get_index() != 5)
517 // If we found two tangents then continue
518 if(total_tangents!=2)
520 // We have reached exactly two tangents
525 Action::ValueDescLinkOpposite::set_param(const synfig::String& name, const Action::Param ¶m)
527 return Action::ValueDescLink::set_param(name,param);
531 Action::ValueDescLinkOpposite::is_ready()const
533 return Action::ValueDescLink::is_ready();
537 Action::ValueDescLinkOpposite::prepare()
539 // prepare to do a opposite link and reuse the code from ValueDescLink
541 ValueDescLink::prepare();