Fix bug 3060560: Link two tangents and one is animated, the animated is lost.
[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 **
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 "valuedesclink.h"
34
35 #include <synfigapp/canvasinterface.h>
36 #include <synfig/valuenode_const.h>
37
38 #include <synfigapp/general.h>
39
40 #endif
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45 using namespace synfigapp;
46 using namespace Action;
47
48 /* === M A C R O S ========================================================= */
49
50 ACTION_INIT(Action::ValueDescLink);
51 ACTION_SET_NAME(Action::ValueDescLink,"ValueDescLink");
52 ACTION_SET_LOCAL_NAME(Action::ValueDescLink,N_("Link"));
53 ACTION_SET_TASK(Action::ValueDescLink,"connect");
54 ACTION_SET_CATEGORY(Action::ValueDescLink,Action::CATEGORY_VALUEDESC);
55 ACTION_SET_PRIORITY(Action::ValueDescLink,0);
56 ACTION_SET_VERSION(Action::ValueDescLink,"0.0");
57 ACTION_SET_CVS_ID(Action::ValueDescLink,"$Id$");
58
59 /* === G L O B A L S ======================================================= */
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65 Action::ValueDescLink::ValueDescLink(): poison(false), status_level(0)
66 {
67 }
68
69 Action::ParamVocab
70 Action::ValueDescLink::get_param_vocab()
71 {
72         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
73
74         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
75                 .set_local_name(_("ValueDesc to link"))
76                 .set_requires_multiple()
77         );
78
79         return ret;
80 }
81
82 bool
83 Action::ValueDescLink::is_candidate(const ParamList &x)
84 {
85         return candidate_check(get_param_vocab(),x);
86 }
87
88 bool
89 Action::ValueDescLink::set_param(const synfig::String& name, const Action::Param &param)
90 {
91         if(name=="time" && param.get_type()==Param::TYPE_TIME)
92         {
93                 time=param.get_time();
94                 return true;
95         }
96
97         // don't bother looking for the best value to use if there's already been an error
98         if (poison==true) return false;
99
100         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
101         {
102                 ValueDesc value_desc(param.get_value_desc());
103
104                 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
105                 {
106                         if(link_value_node==value_desc.get_value_node())
107                                 return true;
108
109                         if(link_value_node && link_value_node->is_exported())
110                         {
111                                 poison=true;
112                                 status_message = (_("Cannot link two different exported values ('") +
113                                                                   value_desc.get_value_node()->get_id() + _("' and '") +
114                                                                   link_value_node->get_id()) + _("')");
115                                 return false;
116                         }
117
118                         link_value_node=value_desc.get_value_node();
119                         status_message = _("Used exported ValueNode ('") + link_value_node->get_id() + _("').");
120                 }
121                 else if(value_desc.is_value_node())
122                 {
123                         if(!link_value_node)
124                         {
125                                 status_level = 1;
126                                 status_message = _("Using the only available ValueNode.");
127                                 link_value_node=value_desc.get_value_node();
128                         }
129                         else if(link_value_node->is_exported())
130                         {
131                                 // we've already seen an exported value, so use that rather than the current value
132                         }
133                         // Use the one that is referenced more
134                         else if(link_value_node->rcount()!=value_desc.get_value_node()->rcount())
135                         {
136                                 if(link_value_node->rcount()<value_desc.get_value_node()->rcount())
137                                 {
138                                         status_level = 2;
139                                         status_message = _("Using the most referenced ValueNode.");
140                                         link_value_node=value_desc.get_value_node();
141                                 }
142                                 else if (status_level <= 2)
143                                 {
144                                         status_level = 2;
145                                         status_message = _("Using the most referenced ValueNode.");
146                                 }
147                         }
148                         // If the current link value node is a constant and
149                         // this one isn't, then give preference to the exotic
150                         else if(ValueNode_Const::Handle::cast_dynamic(link_value_node) && !ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
151                         {
152                                 status_level = 3;
153                                 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
154                                 link_value_node=value_desc.get_value_node();
155                         }
156                         else if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()) && !ValueNode_Const::Handle::cast_dynamic(link_value_node))
157                         {
158                                 if (status_level <= 3)
159                                 {
160                                         status_level = 3;
161                                         status_message = _("There's a tie for most referenced; using the animated ValueNode.");
162                                 }
163                         }
164                         // If both are animated, and this one has more waypoints, then use the one with more waypoints
165                         else if(ValueNode_Animated::Handle::cast_dynamic(link_value_node) &&
166                                         ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()) &&
167                                         ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() !=
168                                         ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
169                         {
170                                 if (ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() <
171                                         ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
172                                 {
173                                         status_level = 4;
174                                         status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
175                                         link_value_node=value_desc.get_value_node();
176                                 }
177                                 else if (status_level <= 4)
178                                 {
179                                         status_level = 4;
180                                         status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
181                                 }
182                         }
183                         // If both are Linkable Value Nodes and has waypoint in its children, use the one with more waypoints
184                         else if(LinkableValueNode::Handle::cast_dynamic(link_value_node) &&
185                                         LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node()) &&
186                                         LinkableValueNode::Handle::cast_dynamic(link_value_node)->get_times().size() !=
187                                         LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node())->get_times().size())
188                         {
189                                 if(LinkableValueNode::Handle::cast_dynamic(link_value_node)->get_times().size() <
190                                 LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node())->get_times().size())
191                                 {
192                                         status_level = 4;
193                                         status_message = _("There's a tie for most referenced, and both are linkable value node animated; using the one with the most waypoints.");
194                                         link_value_node=value_desc.get_value_node();
195                                 }
196                                 else if (status_level <= 4)
197                                 {
198                                         status_level = 4;
199                                         status_message = _("There's a tie for most referenced, and both are linkable value node animated; using the one with the most waypoints.");
200                                 }
201                         }
202                         // Use the one that was least recently changed
203                         else if(link_value_node->get_time_last_changed()!=value_desc.get_value_node()->get_time_last_changed())
204                         {
205                                 if(link_value_node->get_time_last_changed()>value_desc.get_value_node()->get_time_last_changed())
206                                 {
207                                         status_level = 5;
208                                         status_message = _("Everything is tied; using the least recently modified value.");
209                                         link_value_node=value_desc.get_value_node();
210                                 }
211                                 else if (status_level <= 5)
212                                 {
213                                         status_level = 5;
214                                         status_message = _("Everything is tied; using the least recently modified value.");
215                                 }
216                         }
217                         else
218                         {
219                                 status_level = 6;
220                                 status_message = _("Absolutely everything is tied.");
221                         }
222                 }
223
224                 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
225                 {
226                         // Everything must be of the same type
227                         poison=true;
228                         status_message = (strprintf(_("Cannot link two values of different types ('%s' and '%s')"),
229                                                                                 ValueBase::type_local_name(value_desc.get_value_type()).c_str(),
230                                                                                 ValueBase::type_local_name(value_desc_list.front().get_value_type()).c_str()));
231                         return false;
232                 }
233
234                 value_desc_list.push_back(value_desc);
235
236                 return true;
237         }
238
239         return Action::CanvasSpecific::set_param(name,param);
240 }
241
242 bool
243 Action::ValueDescLink::is_ready()const
244 {
245         if(poison)
246                 return true;
247         if(value_desc_list.size()<=1)
248                 return false;
249         return Action::CanvasSpecific::is_ready();
250 }
251
252 void
253 Action::ValueDescLink::prepare()
254 {
255         if(poison)
256                 throw Error(status_message.c_str());
257
258         if(value_desc_list.empty())
259                 throw Error(Error::TYPE_NOTREADY);
260
261         clear();
262
263         if(!link_value_node)
264         {
265                 status_message = _("No ValueNodes were available, so one was created.");
266                 ValueDesc& value_desc(value_desc_list.back());
267
268                 link_value_node=ValueNode_Const::create(value_desc.get_value(time));
269
270                 Action::Handle action(Action::create("ValueDescConnect"));
271
272                 action->set_param("canvas",get_canvas());
273                 action->set_param("canvas_interface",get_canvas_interface());
274                 action->set_param("src",link_value_node);
275                 action->set_param("dest",value_desc);
276
277                 assert(action->is_ready());
278                 if(!action->is_ready())
279                         throw Error(Error::TYPE_NOTREADY);
280
281                 add_action_front(action);
282         }
283
284         /*
285         if(!link_value_node->is_exported())
286         {
287                 Action::Handle action(Action::create("ValueNodeAdd"));
288
289                 action->set_param("canvas",get_canvas());
290                 action->set_param("canvas_interface",get_canvas_interface());
291                 action->set_param("new",link_value_node);
292                 action->set_param("name",strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
293
294                 assert(action->is_ready());
295                 if(!action->is_ready())
296                         throw Error(Error::TYPE_NOTREADY);
297
298                 add_action_front(action);
299         }
300         */
301
302         std::list<ValueDesc>::iterator iter;
303         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
304         {
305                 ValueDesc& value_desc(*iter);
306
307                 // only one of the selected items can be exported - that's the one we're linking to
308                 // don't link it to itself
309                 if (value_desc.is_exported())
310                         continue;
311
312                 Action::Handle action(Action::create("ValueDescConnect"));
313
314                 action->set_param("canvas",get_canvas());
315                 action->set_param("canvas_interface",get_canvas_interface());
316                 action->set_param("src",link_value_node);
317                 action->set_param("dest",value_desc);
318
319                 assert(action->is_ready());
320                 if(!action->is_ready())
321                         throw Error(Error::TYPE_NOTREADY);
322
323                 add_action_front(action);
324         }
325
326         synfig::info("http://synfig.org/Linking#Tier_%d : %s", status_level, status_message.c_str());
327 }