1 /* === S Y N F I G ========================================================= */
2 /*! \file valuedescsmartlink.cpp
3 ** \brief Template File
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 ======================================================= */
33 #include "valuedescsmartlink.h"
35 #include <synfigapp/canvasinterface.h>
36 #include <synfig/valuenode_const.h>
37 #include <synfig/valuenode_scale.h>
39 #include <synfigapp/general.h>
45 using namespace synfig;
46 using namespace synfigapp;
47 using namespace Action;
49 /* === M A C R O S ========================================================= */
51 ACTION_INIT(Action::ValueDescSmartLink);
52 ACTION_SET_NAME(Action::ValueDescSmartLink,"ValueDescSmartLink");
53 ACTION_SET_LOCAL_NAME(Action::ValueDescSmartLink,N_("Smart Link"));
54 ACTION_SET_TASK(Action::ValueDescSmartLink,"connect");
55 ACTION_SET_CATEGORY(Action::ValueDescSmartLink,Action::CATEGORY_VALUEDESC);
56 ACTION_SET_PRIORITY(Action::ValueDescSmartLink,0);
57 ACTION_SET_VERSION(Action::ValueDescSmartLink,"0.0");
58 ACTION_SET_CVS_ID(Action::ValueDescSmartLink,"$Id$");
60 /* === G L O B A L S ======================================================= */
62 /* === P R O C E D U R E S ================================================= */
64 /* === M E T H O D S ======================================================= */
66 Action::ValueDescSmartLink::ValueDescSmartLink():
67 poison(false), status_level(0), link_scalar(0.0)
72 Action::ValueDescSmartLink::get_param_vocab()
74 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
76 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
77 .set_local_name(_("ValueDesc to smart link"))
78 .set_requires_multiple()
85 Action::ValueDescSmartLink::is_candidate(const ParamList &x)
87 // If action parameters are not Value Desc
88 if(!candidate_check(get_param_vocab(),x))
91 Real current_scalar(1.0);
92 bool found_inverse(false);
94 ParamList::const_iterator iter;
95 //Search thru all the Param and pick up the value descriptions
96 for(iter=x.begin(); iter!=x.end(); iter++)
98 if(iter->first == "value_desc")
100 ValueDesc v_desc(iter->second.get_value_desc());
101 // if the value description parent is linkable value node, continue
102 if(!v_desc.parent_is_linkable_value_node())
104 // if the link describe to any tangent (index 4 or 5), continue
105 if(v_desc.get_index() != 4 && v_desc.get_index() != 5)
107 synfig::Real iter_scalar=v_desc.get_scalar();
108 // Let's compare the sign of scalar of the value node witht the current one
109 // and remember if a change of sign is seen.
110 if(iter_scalar*current_scalar < 0) // if they have different signs
113 current_scalar=iter_scalar;
117 // If we found two inverse tangents then continue
120 // We have reached two or more opposite tangents
125 Action::ValueDescSmartLink::set_param(const synfig::String& name, const Action::Param ¶m)
127 if(name=="time" && param.get_type()==Param::TYPE_TIME)
129 time=param.get_time();
133 // don't bother looking for the best value to use if there's already been an error
134 if (poison==true) return false;
136 if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
138 ValueDesc value_desc(param.get_value_desc());
140 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
142 if(link_value_node==value_desc.get_value_node())
145 if(link_value_node && link_value_node->is_exported())
148 status_message = (_("Cannot link two different exported values ('") +
149 value_desc.get_value_node()->get_id() + _("' and '") +
150 link_value_node->get_id()) + _("')");
154 link_value_node=value_desc.get_value_node();
155 link_scalar=value_desc.get_scalar();
156 status_message = _("Used exported ValueNode ('") + link_value_node->get_id() + _("').");
158 else if(value_desc.is_value_node())
163 status_message = _("Using the only available ValueNode.");
164 link_value_node=value_desc.get_value_node();
165 link_scalar=value_desc.get_scalar();
167 else if(link_value_node->is_exported())
169 // we've already seen an exported value, so use that rather than the current value
171 // Use the one that is referenced more
172 else if(link_value_node->rcount()!=value_desc.get_value_node()->rcount())
174 if(link_value_node->rcount()<value_desc.get_value_node()->rcount())
177 status_message = _("Using the most referenced ValueNode.");
178 link_value_node=value_desc.get_value_node();
179 link_scalar=value_desc.get_scalar();
181 else if (status_level <= 2)
184 status_message = _("Using the most referenced ValueNode.");
187 // If the current link value node is a constant and
188 // this one isn't, then give preference to the exotic
189 else if(ValueNode_Const::Handle::cast_dynamic(link_value_node) && !ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
192 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
193 link_value_node=value_desc.get_value_node();
194 link_scalar=value_desc.get_scalar();
196 else if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()) && !ValueNode_Const::Handle::cast_dynamic(link_value_node))
198 if (status_level <= 3)
201 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
204 // If both are animated, and this one has more waypoints, then use the one with more waypoints
205 else if(ValueNode_Animated::Handle::cast_dynamic(link_value_node) &&
206 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()) &&
207 ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() !=
208 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
210 if (ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() <
211 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
214 status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
215 link_value_node=value_desc.get_value_node();
216 link_scalar=value_desc.get_scalar();
218 else if (status_level <= 4)
221 status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
224 // If both are Linkable Value Nodes and has waypoint in its children, use the one with more waypoints
225 else if(LinkableValueNode::Handle::cast_dynamic(link_value_node) &&
226 LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node()) &&
227 LinkableValueNode::Handle::cast_dynamic(link_value_node)->get_times().size() !=
228 LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node())->get_times().size())
230 if(LinkableValueNode::Handle::cast_dynamic(link_value_node)->get_times().size() <
231 LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node())->get_times().size())
234 status_message = _("There's a tie for most referenced, and both are linkable value node animated; using the one with the most waypoints.");
235 link_value_node=value_desc.get_value_node();
236 link_scalar=value_desc.get_scalar();
238 else if (status_level <= 4)
241 status_message = _("There's a tie for most referenced, and both are linkable value node animated; using the one with the most waypoints.");
244 // Use the one that was least recently changed
245 else if(link_value_node->get_time_last_changed()!=value_desc.get_value_node()->get_time_last_changed())
247 if(link_value_node->get_time_last_changed()>value_desc.get_value_node()->get_time_last_changed())
250 status_message = _("Everything is tied; using the least recently modified value.");
251 link_value_node=value_desc.get_value_node();
252 link_scalar=value_desc.get_scalar();
254 else if (status_level <= 5)
257 status_message = _("Everything is tied; using the least recently modified value.");
263 status_message = _("Absolutely everything is tied.");
267 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
269 // Everything must be of the same type
271 status_message = (strprintf(_("Cannot link two values of different types ('%s' and '%s')"),
272 ValueBase::type_local_name(value_desc.get_value_type()).c_str(),
273 ValueBase::type_local_name(value_desc_list.front().get_value_type()).c_str()));
277 value_desc_list.push_back(value_desc);
282 return Action::CanvasSpecific::set_param(name,param);
286 Action::ValueDescSmartLink::is_ready()const
290 if(value_desc_list.size()<=1)
292 return Action::CanvasSpecific::is_ready();
296 Action::ValueDescSmartLink::prepare()
299 throw Error(status_message.c_str());
301 if(value_desc_list.empty())
302 throw Error(Error::TYPE_NOTREADY);
308 // we should have a value node selected because is_candidate()
309 // should have checked it before
310 throw Error(Error::TYPE_BUG);
313 // Check if the selected link value node is already a scale -1.0 Linkable Value Node
314 bool link_is_scaled(false);
315 if(synfig::ValueNode_Scale::Handle::cast_dynamic(link_value_node))
317 synfig::ValueNode_Const::Handle scale_vn(
318 synfig::ValueNode_Const::Handle::cast_dynamic(
319 synfig::ValueNode_Scale::Handle::cast_dynamic(link_value_node)->get_link(1)
323 if((*scale_vn)(synfig::Time(0))==synfig::ValueBase(Real(-1.0)))
327 //See what is the tangent selected to convert.
328 std::list<ValueDesc>::const_iterator vd_iter;
329 for(vd_iter=value_desc_list.begin(); vd_iter!=value_desc_list.end(); vd_iter++)
331 // Don't link the selected to itself
332 if(vd_iter->get_value_node() == link_value_node)
334 //Check if the current value node has opposite scalar than the link
335 // value node to convert to scale -1.0 before connect.
336 // Check also if the link value node is NOT also a scale -1
337 if( (vd_iter->get_scalar()*link_scalar<0) && (link_is_scaled==false) )
339 //Let's create a Scale Value Node
340 synfig::ValueNode::Handle scale_value_node=synfig::LinkableValueNode::create("scale",vd_iter->get_value(time));
341 if(!scale_value_node)
342 throw Error(Error::TYPE_BUG);
343 scale_value_node->set_parent_canvas(get_canvas());
344 //Let's connect the new Scale Value Node to the value node
345 Action::Handle action1(Action::create("ValueDescConnect"));
347 throw Error(Error::TYPE_CRITICAL);
348 action1->set_param("canvas",get_canvas());
349 action1->set_param("canvas_interface",get_canvas_interface());
350 action1->set_param("dest",*vd_iter);
351 action1->set_param("src",scale_value_node);
352 assert(action1->is_ready());
353 if(!action1->is_ready())
354 throw Error(Error::TYPE_NOTREADY);
355 add_action_front(action1);
357 //Let's Connect the link value node to the scale value node link subparam
358 Action::Handle action2(Action::create("ValueNodeLinkConnect"));
360 throw Error(Error::TYPE_CRITICAL);
362 action2->set_param("canvas",get_canvas());
363 action2->set_param("canvas_interface",get_canvas_interface());
364 action2->set_param("parent_value_node",scale_value_node);
365 action2->set_param("index",0);
366 action2->set_param("value_node",link_value_node);
367 assert(action2->is_ready());
368 if(!action2->is_ready())
369 throw Error(Error::TYPE_NOTREADY);
370 add_action_front(action2);
372 //Let's Set the scale to -1
373 Action::Handle action3(Action::create("ValueNodeConstSet"));
375 throw Error(Error::TYPE_CRITICAL);
377 action3->set_param("canvas",get_canvas());
378 action3->set_param("canvas_interface",get_canvas_interface());
379 action3->set_param("value_node",synfig::LinkableValueNode::Handle::cast_dynamic(scale_value_node)->get_link(1));
380 action3->set_param("new_value",synfig::ValueBase(Real(-1.0)));
381 assert(action3->is_ready());
382 if(!action3->is_ready())
383 throw Error(Error::TYPE_NOTREADY);
384 add_action_front(action3);
386 else if((vd_iter->get_scalar()*link_scalar<0) && (link_is_scaled==true) )
388 synfig::info("adding action4");
389 //Let's connect the link value node -> link to the value node
390 // There is not needed conversion to scale of the value node
391 // because the link value node is already a scale -1
392 Action::Handle action4(Action::create("ValueDescConnect"));
394 throw Error(Error::TYPE_CRITICAL);
395 action4->set_param("canvas",get_canvas());
396 action4->set_param("canvas_interface",get_canvas_interface());
397 action4->set_param("dest",*vd_iter);
398 action4->set_param("src",synfig::ValueNode_Scale::Handle::cast_dynamic(link_value_node)->get_link(0));
399 assert(action4->is_ready());
400 if(!action4->is_ready())
401 throw Error(Error::TYPE_NOTREADY);
402 add_action_front(action4);
406 //Let's connect the link value node to the value node
407 Action::Handle action(Action::create("ValueDescConnect"));
409 throw Error(Error::TYPE_CRITICAL);
410 action->set_param("canvas",get_canvas());
411 action->set_param("canvas_interface",get_canvas_interface());
412 action->set_param("dest",*vd_iter);
413 action->set_param("src",link_value_node);
414 assert(action->is_ready());
415 if(!action->is_ready())
416 throw Error(Error::TYPE_NOTREADY);
417 add_action_front(action);
420 synfig::info("http://synfig.org/Linking#Tier_%d : %s", status_level, status_message.c_str());