1 /* === S Y N F I G ========================================================= */
2 /*! \file valuedesclink.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 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 "valuedesclink.h"
35 #include <synfigapp/canvasinterface.h>
36 #include <synfig/valuenode_const.h>
42 using namespace synfig;
43 using namespace synfigapp;
44 using namespace Action;
46 /* === M A C R O S ========================================================= */
48 ACTION_INIT(Action::ValueDescLink);
49 ACTION_SET_NAME(Action::ValueDescLink,"value_desc_link");
50 ACTION_SET_LOCAL_NAME(Action::ValueDescLink,"Link");
51 ACTION_SET_TASK(Action::ValueDescLink,"connect");
52 ACTION_SET_CATEGORY(Action::ValueDescLink,Action::CATEGORY_VALUEDESC);
53 ACTION_SET_PRIORITY(Action::ValueDescLink,0);
54 ACTION_SET_VERSION(Action::ValueDescLink,"0.0");
55 ACTION_SET_CVS_ID(Action::ValueDescLink,"$Id$");
57 /* === G L O B A L S ======================================================= */
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
63 Action::ValueDescLink::ValueDescLink(): poison(false), status_level(0)
68 Action::ValueDescLink::get_param_vocab()
70 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
72 ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
73 .set_local_name(_("ValueDesc to link"))
74 .set_requires_multiple()
81 Action::ValueDescLink::is_candidate(const ParamList &x)
83 return candidate_check(get_param_vocab(),x);
87 Action::ValueDescLink::set_param(const synfig::String& name, const Action::Param ¶m)
89 if(name=="time" && param.get_type()==Param::TYPE_TIME)
91 time=param.get_time();
95 // don't bother looking for the best value to use if there's already been an error
96 if (poison==true) return false;
98 if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
100 ValueDesc value_desc(param.get_value_desc());
102 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
104 if(link_value_node==value_desc.get_value_node())
107 if(link_value_node && link_value_node->is_exported())
110 status_message = (_("Cannot link two different exported values ('") +
111 value_desc.get_value_node()->get_id() + _("' and '") +
112 link_value_node->get_id()) + _("')");
116 link_value_node=value_desc.get_value_node();
117 status_message = _("Used exported ValueNode ('") + link_value_node->get_id() + _("').");
119 else if(value_desc.is_value_node())
124 status_message = _("Using the only available ValueNode.");
125 link_value_node=value_desc.get_value_node();
127 else if(link_value_node->is_exported())
129 // we've already seen an exported value, so use that rather than the current value
131 // Use the one that is referenced more
132 else if(link_value_node->rcount()!=value_desc.get_value_node()->rcount())
134 if(link_value_node->rcount()<value_desc.get_value_node()->rcount())
137 status_message = _("Using the most referenced ValueNode.");
138 link_value_node=value_desc.get_value_node();
140 else if (status_level <= 2)
143 status_message = _("Using the most referenced ValueNode.");
146 // If the current link value node is a constant and
147 // this one isn't, then give preference to the exotic
148 else if(ValueNode_Const::Handle::cast_dynamic(link_value_node) && !ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
151 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
152 link_value_node=value_desc.get_value_node();
154 else if(ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()) && !ValueNode_Const::Handle::cast_dynamic(link_value_node))
156 if (status_level <= 3)
159 status_message = _("There's a tie for most referenced; using the animated ValueNode.");
162 // If both are animated, and this one has more waypoints, then use the one with more waypoints
163 else if(ValueNode_Animated::Handle::cast_dynamic(link_value_node) &&
164 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node()) &&
165 ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() !=
166 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
168 if (ValueNode_Animated::Handle::cast_dynamic(link_value_node)->waypoint_list().size() <
169 ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())->waypoint_list().size())
172 status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
173 link_value_node=value_desc.get_value_node();
175 else if (status_level <= 4)
178 status_message = _("There's a tie for most referenced, and both are animated; using the one with the most waypoints.");
181 // Use the one that was least recently changed
182 else if(link_value_node->get_time_last_changed()!=value_desc.get_value_node()->get_time_last_changed())
184 if(link_value_node->get_time_last_changed()>value_desc.get_value_node()->get_time_last_changed())
187 status_message = _("Everything is tied; using the least recently modified value.");
188 link_value_node=value_desc.get_value_node();
190 else if (status_level <= 5)
193 status_message = _("Everything is tied; using the least recently modified value.");
199 status_message = _("Absolutely everything is tied.");
203 if(value_desc_list.size() && value_desc.get_value_type()!=value_desc_list.front().get_value_type())
205 // Everything must be of the same type
207 status_message = (_("Cannot link two values of different types ('") +
208 value_desc.get_value().type_name() + _("' and '") +
209 value_desc_list.front().get_value().type_name() +_("')"));
213 value_desc_list.push_back(value_desc);
218 return Action::CanvasSpecific::set_param(name,param);
222 Action::ValueDescLink::is_ready()const
226 if(value_desc_list.size()<=1)
228 return Action::CanvasSpecific::is_ready();
232 Action::ValueDescLink::prepare()
235 throw Error(status_message.c_str());
237 if(value_desc_list.empty())
238 throw Error(Error::TYPE_NOTREADY);
244 status_message = _("No ValueNodes were available, so one was created.");
245 ValueDesc& value_desc(value_desc_list.front());
247 link_value_node=ValueNode_Const::create(value_desc.get_value(time));
249 Action::Handle action(Action::create("value_desc_connect"));
251 action->set_param("canvas",get_canvas());
252 action->set_param("canvas_interface",get_canvas_interface());
253 action->set_param("src",link_value_node);
254 action->set_param("dest",value_desc);
256 assert(action->is_ready());
257 if(!action->is_ready())
258 throw Error(Error::TYPE_NOTREADY);
260 add_action_front(action);
264 if(!link_value_node->is_exported())
266 Action::Handle action(Action::create("value_node_add"));
268 action->set_param("canvas",get_canvas());
269 action->set_param("canvas_interface",get_canvas_interface());
270 action->set_param("new",link_value_node);
271 action->set_param("name",strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
273 assert(action->is_ready());
274 if(!action->is_ready())
275 throw Error(Error::TYPE_NOTREADY);
277 add_action_front(action);
281 std::list<ValueDesc>::iterator iter;
282 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
284 ValueDesc& value_desc(*iter);
286 Action::Handle action(Action::create("value_desc_connect"));
288 action->set_param("canvas",get_canvas());
289 action->set_param("canvas_interface",get_canvas_interface());
290 action->set_param("src",link_value_node);
291 action->set_param("dest",value_desc);
293 assert(action->is_ready());
294 if(!action->is_ready())
295 throw Error(Error::TYPE_NOTREADY);
297 add_action_front(action);
300 synfig::info(status_message);