84effe4fc9fc96ff144e81072827e35b659bb0b1
[synfig.git] /
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,"value_desc_link");
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                         // Use the one that was least recently changed
184                         else if(link_value_node->get_time_last_changed()!=value_desc.get_value_node()->get_time_last_changed())
185                         {
186                                 if(link_value_node->get_time_last_changed()>value_desc.get_value_node()->get_time_last_changed())
187                                 {
188                                         status_level = 5;
189                                         status_message = _("Everything is tied; using the least recently modified value.");
190                                         link_value_node=value_desc.get_value_node();
191                                 }
192                                 else if (status_level <= 5)
193                                 {
194                                         status_level = 5;
195                                         status_message = _("Everything is tied; using the least recently modified value.");
196                                 }
197                         }
198                         else
199                         {
200                                 status_level = 6;
201                                 status_message = _("Absolutely everything is tied.");
202                         }
203                 }
204
205                 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
206                 {
207                         // Everything must be of the same type
208                         poison=true;
209                         status_message = (strprintf(_("Cannot link two values of different types ('%s' and '%s')"),
210                                                                                 ValueBase::type_local_name(value_desc.get_value_type()).c_str(),
211                                                                                 ValueBase::type_local_name(value_desc_list.front().get_value_type()).c_str()));
212                         return false;
213                 }
214
215                 value_desc_list.push_back(value_desc);
216
217                 return true;
218         }
219
220         return Action::CanvasSpecific::set_param(name,param);
221 }
222
223 bool
224 Action::ValueDescLink::is_ready()const
225 {
226         if(poison)
227                 return true;
228         if(value_desc_list.size()<=1)
229                 return false;
230         return Action::CanvasSpecific::is_ready();
231 }
232
233 void
234 Action::ValueDescLink::prepare()
235 {
236         if(poison)
237                 throw Error(status_message.c_str());
238
239         if(value_desc_list.empty())
240                 throw Error(Error::TYPE_NOTREADY);
241
242         clear();
243
244         if(!link_value_node)
245         {
246                 status_message = _("No ValueNodes were available, so one was created.");
247                 ValueDesc& value_desc(value_desc_list.back());
248
249                 link_value_node=ValueNode_Const::create(value_desc.get_value(time));
250
251                 Action::Handle action(Action::create("value_desc_connect"));
252
253                 action->set_param("canvas",get_canvas());
254                 action->set_param("canvas_interface",get_canvas_interface());
255                 action->set_param("src",link_value_node);
256                 action->set_param("dest",value_desc);
257
258                 assert(action->is_ready());
259                 if(!action->is_ready())
260                         throw Error(Error::TYPE_NOTREADY);
261
262                 add_action_front(action);
263         }
264
265         /*
266         if(!link_value_node->is_exported())
267         {
268                 Action::Handle action(Action::create("value_node_add"));
269
270                 action->set_param("canvas",get_canvas());
271                 action->set_param("canvas_interface",get_canvas_interface());
272                 action->set_param("new",link_value_node);
273                 action->set_param("name",strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
274
275                 assert(action->is_ready());
276                 if(!action->is_ready())
277                         throw Error(Error::TYPE_NOTREADY);
278
279                 add_action_front(action);
280         }
281         */
282
283         std::list<ValueDesc>::iterator iter;
284         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
285         {
286                 ValueDesc& value_desc(*iter);
287
288                 // only one of the selected items can be exported - that's the one we're linking to
289                 // don't link it to itself
290                 if (value_desc.is_exported())
291                         continue;
292
293                 Action::Handle action(Action::create("value_desc_connect"));
294
295                 action->set_param("canvas",get_canvas());
296                 action->set_param("canvas_interface",get_canvas_interface());
297                 action->set_param("src",link_value_node);
298                 action->set_param("dest",value_desc);
299
300                 assert(action->is_ready());
301                 if(!action->is_ready())
302                         throw Error(Error::TYPE_NOTREADY);
303
304                 add_action_front(action);
305         }
306
307         synfig::info("http://synfig.org/Linking#Tier_%d : %s", status_level, status_message.c_str());
308 }