Don't scale by -1.0 value nodes when the selected link value node is already a scaled...
[synfig.git] / synfig-studio / src / synfigapp / actions / valuedescsmartlink.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedescsmartlink.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "valuedescsmartlink.h"
34
35 #include <synfigapp/canvasinterface.h>
36 #include <synfig/valuenode_const.h>
37 #include <synfig/valuenode_scale.h>
38
39 #include <synfigapp/general.h>
40
41 #endif
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46 using namespace synfigapp;
47 using namespace Action;
48
49 /* === M A C R O S ========================================================= */
50
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$");
59
60 /* === G L O B A L S ======================================================= */
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 Action::ValueDescSmartLink::ValueDescSmartLink():
67 poison(false), status_level(0), link_scalar(0.0)
68 {
69 }
70
71 Action::ParamVocab
72 Action::ValueDescSmartLink::get_param_vocab()
73 {
74         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
75
76         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
77                 .set_local_name(_("ValueDesc to smart link"))
78                 .set_requires_multiple()
79         );
80
81         return ret;
82 }
83
84 bool
85 Action::ValueDescSmartLink::is_candidate(const ParamList &x)
86 {
87         // If action parameters are not Value Desc
88         if(!candidate_check(get_param_vocab(),x))
89                 return false;
90
91         Real current_scalar(1.0);
92         bool found_inverse(false);
93
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++)
97         {
98                 if(iter->first == "value_desc")
99                 {
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())
103                                 return false;
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)
106                                 return false;
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
111                         {
112                                 found_inverse=true;
113                                 current_scalar=iter_scalar;
114                         }
115                 }
116         }
117         // If we found two inverse tangents then continue
118         if(!found_inverse)
119                 return false;
120         // We have reached two or more opposite tangents
121         return true;
122 }
123
124 bool
125 Action::ValueDescSmartLink::set_param(const synfig::String& name, const Action::Param &param)
126 {
127         if(name=="time" && param.get_type()==Param::TYPE_TIME)
128         {
129                 time=param.get_time();
130                 return true;
131         }
132
133         // don't bother looking for the best value to use if there's already been an error
134         if (poison==true) return false;
135
136         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
137         {
138                 ValueDesc value_desc(param.get_value_desc());
139
140                 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
141                 {
142                         if(link_value_node==value_desc.get_value_node())
143                                 return true;
144
145                         if(link_value_node && link_value_node->is_exported())
146                         {
147                                 poison=true;
148                                 status_message = (_("Cannot link two different exported values ('") +
149                                                                   value_desc.get_value_node()->get_id() + _("' and '") +
150                                                                   link_value_node->get_id()) + _("')");
151                                 return false;
152                         }
153
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() + _("').");
157                 }
158                 else if(value_desc.is_value_node())
159                 {
160                         if(!link_value_node)
161                         {
162                                 status_level = 1;
163                                 status_message = _("Using the only available ValueNode.");
164                                 link_value_node=value_desc.get_value_node();
165                                 link_scalar=value_desc.get_scalar();
166                         }
167                         else if(link_value_node->is_exported())
168                         {
169                                 // we've already seen an exported value, so use that rather than the current value
170                         }
171                         // Use the one that is referenced more
172                         else if(link_value_node->rcount()!=value_desc.get_value_node()->rcount())
173                         {
174                                 if(link_value_node->rcount()<value_desc.get_value_node()->rcount())
175                                 {
176                                         status_level = 2;
177                                         status_message = _("Using the most referenced ValueNode.");
178                                         link_value_node=value_desc.get_value_node();
179                                         link_scalar=value_desc.get_scalar();
180                                 }
181                                 else if (status_level <= 2)
182                                 {
183                                         status_level = 2;
184                                         status_message = _("Using the most referenced ValueNode.");
185                                 }
186                         }
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()))
190                         {
191                                 status_level = 3;
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();
195                         }
196                         else if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()) && !ValueNode_Const::Handle::cast_dynamic(link_value_node))
197                         {
198                                 if (status_level <= 3)
199                                 {
200                                         status_level = 3;
201                                         status_message = _("There's a tie for most referenced; using the animated ValueNode.");
202                                 }
203                         }
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())
209                         {
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())
212                                 {
213                                         status_level = 4;
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();
217                                 }
218                                 else if (status_level <= 4)
219                                 {
220                                         status_level = 4;
221                                         status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
222                                 }
223                         }
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())
229                         {
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())
232                                 {
233                                         status_level = 4;
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();
237                                 }
238                                 else if (status_level <= 4)
239                                 {
240                                         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.");
242                                 }
243                         }
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())
246                         {
247                                 if(link_value_node->get_time_last_changed()>value_desc.get_value_node()->get_time_last_changed())
248                                 {
249                                         status_level = 5;
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();
253                                 }
254                                 else if (status_level <= 5)
255                                 {
256                                         status_level = 5;
257                                         status_message = _("Everything is tied; using the least recently modified value.");
258                                 }
259                         }
260                         else
261                         {
262                                 status_level = 6;
263                                 status_message = _("Absolutely everything is tied.");
264                         }
265                 }
266
267                 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
268                 {
269                         // Everything must be of the same type
270                         poison=true;
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()));
274                         return false;
275                 }
276
277                 value_desc_list.push_back(value_desc);
278
279                 return true;
280         }
281
282         return Action::CanvasSpecific::set_param(name,param);
283 }
284
285 bool
286 Action::ValueDescSmartLink::is_ready()const
287 {
288         if(poison)
289                 return true;
290         if(value_desc_list.size()<=1)
291                 return false;
292         return Action::CanvasSpecific::is_ready();
293 }
294
295 void
296 Action::ValueDescSmartLink::prepare()
297 {
298         if(poison)
299                 throw Error(status_message.c_str());
300
301         if(value_desc_list.empty())
302                 throw Error(Error::TYPE_NOTREADY);
303
304         clear();
305
306         if(!link_value_node)
307         {
308                 // we should have a value node selected because is_candidate()
309                 // should have checked it before
310                 throw Error(Error::TYPE_BUG);
311         }
312
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))
316                 {
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)
320                                                         )
321                                                 );
322                         if(scale_vn)
323                                 if((*scale_vn)(synfig::Time(0))==synfig::ValueBase(Real(-1.0)))
324                                         link_is_scaled=true;
325                 }
326
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++)
330         {
331                 // Don't link the selected to itself
332                 if(vd_iter->get_value_node() == link_value_node)
333                         continue;
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) )
338                 {
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"));
346                         if(!action1)
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);
356
357                         //Let's Connect the link value node to the scale value node link subparam
358                         Action::Handle action2(Action::create("ValueNodeLinkConnect"));
359                         if(!action2)
360                                 throw Error(Error::TYPE_CRITICAL);
361
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);
371
372                         //Let's Set the scale to -1
373                         Action::Handle action3(Action::create("ValueNodeConstSet"));
374                         if(!action3)
375                                 throw Error(Error::TYPE_CRITICAL);
376
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);
385                 }
386                 else if((vd_iter->get_scalar()*link_scalar<0) && (link_is_scaled==true) )
387                 {
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"));
393                         if(!action4)
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);
403                 }
404                 else
405                 {
406                         //Let's connect the link value node to the value node
407                         Action::Handle action(Action::create("ValueDescConnect"));
408                         if(!action)
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);
418                 }
419         }
420         synfig::info("http://synfig.org/Linking#Tier_%d : %s", status_level, status_message.c_str());
421 }