Fix missing back slash to do not stop all the extra dist files list
[synfig.git] / synfig-studio / src / synfigapp / actions / valuedesclink.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedesclink.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 **      Copyright (c) 2010 Carlos López
11 **
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.
16 **
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.
21 **      \endlegal
22 */
23 /* ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "valuedesclink.h"
35
36 #include <synfigapp/canvasinterface.h>
37 #include <synfig/valuenode_const.h>
38 #include <synfig/valuenode_scale.h>
39
40 #include <synfigapp/general.h>
41
42 #endif
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace synfigapp;
48 using namespace Action;
49
50 /* === M A C R O S ========================================================= */
51
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$");
60
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$");
69
70 /* === G L O B A L S ======================================================= */
71
72 /* === P R O C E D U R E S ================================================= */
73
74 /* === M E T H O D S ======================================================= */
75
76 Action::ValueDescLink::ValueDescLink():
77 poison(false), status_level(0), link_scalar(0.0), link_opposite(false)
78 {
79 }
80
81 Action::ParamVocab
82 Action::ValueDescLink::get_param_vocab()
83 {
84         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
85
86         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
87                 .set_local_name(_("ValueDesc to smart link"))
88                 .set_requires_multiple()
89         );
90
91         return ret;
92 }
93
94 bool
95 Action::ValueDescLink::is_candidate(const ParamList &x)
96 {
97         return candidate_check(get_param_vocab(),x);
98 }
99
100 bool
101 Action::ValueDescLink::set_param(const synfig::String& name, const Action::Param &param)
102 {
103         if(name=="time" && param.get_type()==Param::TYPE_TIME)
104         {
105                 time=param.get_time();
106                 return true;
107         }
108
109         // don't bother looking for the best value to use if there's already been an error
110         if (poison==true) return false;
111
112         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
113         {
114                 ValueDesc value_desc(param.get_value_desc());
115
116                 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
117                 {
118                         if(link_value_node==value_desc.get_value_node())
119                                 return true;
120
121                         if(link_value_node && link_value_node->is_exported())
122                         {
123                                 poison=true;
124                                 status_message = (_("Cannot link two different exported values ('") +
125                                                                   value_desc.get_value_node()->get_id() + _("' and '") +
126                                                                   link_value_node->get_id()) + _("')");
127                                 return false;
128                         }
129
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() + _("').");
133                 }
134                 else if(value_desc.is_value_node())
135                 {
136                         if(!link_value_node)
137                         {
138                                 status_level = 1;
139                                 status_message = _("Using the only available ValueNode.");
140                                 link_value_node=value_desc.get_value_node();
141                                 link_scalar=value_desc.get_scalar();
142                         }
143                         else if(link_value_node->is_exported())
144                         {
145                                 // we've already seen an exported value, so use that rather than the current value
146                         }
147                         // Use the one that is referenced more
148                         else if(link_value_node->rcount()!=value_desc.get_value_node()->rcount())
149                         {
150                                 if(link_value_node->rcount()<value_desc.get_value_node()->rcount())
151                                 {
152                                         status_level = 2;
153                                         status_message = _("Using the most referenced ValueNode.");
154                                         link_value_node=value_desc.get_value_node();
155                                         link_scalar=value_desc.get_scalar();
156                                 }
157                                 else if (status_level <= 2)
158                                 {
159                                         status_level = 2;
160                                         status_message = _("Using the most referenced ValueNode.");
161                                 }
162                         }
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()))
166                         {
167                                 status_level = 3;
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();
171                         }
172                         else if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()) && !ValueNode_Const::Handle::cast_dynamic(link_value_node))
173                         {
174                                 if (status_level <= 3)
175                                 {
176                                         status_level = 3;
177                                         status_message = _("There's a tie for most referenced; using the animated ValueNode.");
178                                 }
179                         }
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())
185                         {
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())
188                                 {
189                                         status_level = 4;
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();
193                                 }
194                                 else if (status_level <= 4)
195                                 {
196                                         status_level = 4;
197                                         status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
198                                 }
199                         }
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())
205                         {
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())
208                                 {
209                                         status_level = 4;
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();
213                                 }
214                                 else if (status_level <= 4)
215                                 {
216                                         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.");
218                                 }
219                         }
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())
222                         {
223                                 if(link_value_node->get_time_last_changed()>value_desc.get_value_node()->get_time_last_changed())
224                                 {
225                                         status_level = 5;
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();
229                                 }
230                                 else if (status_level <= 5)
231                                 {
232                                         status_level = 5;
233                                         status_message = _("Everything is tied; using the least recently modified value.");
234                                 }
235                         }
236                         else
237                         {
238                                 status_level = 6;
239                                 status_message = _("Absolutely everything is tied.");
240                         }
241                 }
242
243                 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
244                 {
245                         // Everything must be of the same type
246                         poison=true;
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()));
250                         return false;
251                 }
252
253                 value_desc_list.push_back(value_desc);
254
255                 return true;
256         }
257
258         return Action::CanvasSpecific::set_param(name,param);
259 }
260
261 bool
262 Action::ValueDescLink::is_ready()const
263 {
264         if(poison)
265                 return true;
266         if(value_desc_list.size()<=1)
267                 return false;
268         return Action::CanvasSpecific::is_ready();
269 }
270
271 void
272 Action::ValueDescLink::prepare()
273 {
274         if(poison)
275                 throw Error(status_message.c_str());
276
277         if(value_desc_list.empty())
278                 throw Error(Error::TYPE_NOTREADY);
279
280         clear();
281
282         if(!link_value_node)
283         {
284                 status_message = _("No ValueNodes were available, so one was created.");
285                 ValueDesc& value_desc(value_desc_list.back());
286
287                 link_value_node=ValueNode_Const::create(value_desc.get_value(time));
288
289                 Action::Handle action(Action::create("ValueDescConnect"));
290
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);
295
296                 assert(action->is_ready());
297                 if(!action->is_ready())
298                         throw Error(Error::TYPE_NOTREADY);
299
300                 add_action_front(action);
301         }
302
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))
306                 {
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)
310                                                         )
311                                                 );
312                         if(scale_vn)
313                                 if((*scale_vn)(synfig::Time(0))==synfig::ValueBase(Real(-1.0)))
314                                         link_is_scaled=true;
315                 }
316
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)
325         {
326                 ValueDesc& v_desc(*iter);
327                 // If parent is linkable value node
328                 if(v_desc.parent_is_linkable_value_node())
329                 {
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)
332                         {
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
337                                 {
338                                         found_inverse=true;
339                                         current_scalar=iter_scalar;
340                                 }
341                         }
342                         else // link doesn't describe a tangent
343                         {
344                                 found_inverse=false;
345                                 break;
346                         }
347                 }
348                 else // parent is not a linkable value node
349                 {
350                         found_inverse=false;
351                         break;
352                 }
353         }
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)
357         {
358                 ValueDesc& value_desc(*iter);
359
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())
363                         continue;
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)
366                         continue;
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))
373                 {
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))
379                         {
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"));
387                                 if(!action1)
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);
397
398                                 //Let's Connect the link value node to the scale value node link subparam
399                                 Action::Handle action2(Action::create("ValueNodeLinkConnect"));
400                                 if(!action2)
401                                         throw Error(Error::TYPE_CRITICAL);
402
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);
412
413                                 //Let's Set the scale to -1
414                                 Action::Handle action3(Action::create("ValueNodeConstSet"));
415                                 if(!action3)
416                                         throw Error(Error::TYPE_CRITICAL);
417
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);
426                         }
427                         else if((iter->get_scalar()*link_scalar<0 || link_opposite) && (link_is_scaled==true))
428                         {
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"));
433                                 if(!action4)
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);
443                         }
444                         else
445                         {
446                                 //Let's connect the link value node to the value node
447                                 Action::Handle action(Action::create("ValueDescConnect"));
448
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);
453
454                                 assert(action->is_ready());
455                                 if(!action->is_ready())
456                                         throw Error(Error::TYPE_NOTREADY);
457
458                                 add_action_front(action);
459                         }
460                 } // found inverse
461                 else // Not found inverse so do a regualr link
462                 {
463                         //Let's connect the link value node to the value node
464                         Action::Handle action(Action::create("ValueDescConnect"));
465
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);
470
471                         assert(action->is_ready());
472                         if(!action->is_ready())
473                                 throw Error(Error::TYPE_NOTREADY);
474
475                         add_action_front(action);
476                 }
477         }
478
479         synfig::info("http://synfig.org/Linking#Tier_%d : %s", status_level, status_message.c_str());
480 }
481
482
483 Action::ValueDescLinkOpposite::ValueDescLinkOpposite()
484 {
485 }
486
487 Action::ParamVocab
488 Action::ValueDescLinkOpposite::get_param_vocab()
489 {
490         return Action::ValueDescLink::get_param_vocab();
491 }
492
493 bool
494 Action::ValueDescLinkOpposite::is_candidate(const ParamList &x)
495 {
496         // If action parameters are not Value Desc
497         if(!candidate_check(get_param_vocab(),x))
498                 return false;
499
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++)
504         {
505                 if(iter->first == "value_desc")
506                 {
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())
510                                 return false;
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)
513                                 return false;
514                         total_tangents++;
515                 }
516         }
517         // If we found two tangents then continue
518         if(total_tangents!=2)
519                 return false;
520         // We have reached exactly two tangents
521         return true;
522 }
523
524 bool
525 Action::ValueDescLinkOpposite::set_param(const synfig::String& name, const Action::Param &param)
526 {
527         return Action::ValueDescLink::set_param(name,param);
528 }
529
530 bool
531 Action::ValueDescLinkOpposite::is_ready()const
532 {
533         return Action::ValueDescLink::is_ready();
534 }
535
536 void
537 Action::ValueDescLinkOpposite::prepare()
538 {
539         // prepare to do a opposite link and reuse the code from ValueDescLink
540         link_opposite=true;
541         ValueDescLink::prepare();
542 }