1 /* === S Y N F I G ========================================================= */
2 /*! \file waypointsetsmart.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 "waypointsetsmart.h"
34 #include "valuenodelinkconnect.h"
35 #include "valuenodereplace.h"
37 #include "waypointset.h"
38 #include "waypointadd.h"
40 #include "valuedescconnect.h"
41 #include <synfigapp/canvasinterface.h>
42 #include <synfig/exception.h>
43 #include <synfigapp/main.h>
45 #include <synfigapp/general.h>
51 using namespace synfig;
52 using namespace synfigapp;
53 using namespace Action;
55 /* === M A C R O S ========================================================= */
57 ACTION_INIT(Action::WaypointSetSmart);
58 ACTION_SET_NAME(Action::WaypointSetSmart,"WaypointSetSmart");
59 ACTION_SET_LOCAL_NAME(Action::WaypointSetSmart,N_("Add Waypoint"));
60 ACTION_SET_TASK(Action::WaypointSetSmart,"set");
61 ACTION_SET_CATEGORY(Action::WaypointSetSmart,Action::CATEGORY_WAYPOINT|Action::CATEGORY_VALUEDESC|Action::CATEGORY_VALUENODE);
62 ACTION_SET_PRIORITY(Action::WaypointSetSmart,0);
63 ACTION_SET_VERSION(Action::WaypointSetSmart,"0.0");
64 ACTION_SET_CVS_ID(Action::WaypointSetSmart,"$Id$");
66 /* === G L O B A L S ======================================================= */
68 /* === P R O C E D U R E S ================================================= */
70 /* === M E T H O D S ======================================================= */
72 Action::WaypointSetSmart::WaypointSetSmart()
74 waypoint.set_time(Time::begin()-1);
80 Action::WaypointSetSmart::get_param_vocab()
82 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
84 ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
85 .set_local_name(_("Destination ValueNode (Animated)"))
88 ret.push_back(ParamDesc("waypoint",Param::TYPE_WAYPOINT)
89 .set_local_name(_("New Waypoint"))
90 .set_desc(_("Waypoint to be added"))
94 ret.push_back(ParamDesc("waypoint_model",Param::TYPE_WAYPOINTMODEL)
95 .set_local_name(_("Waypoint Model"))
99 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
100 .set_local_name(_("Time"))
101 .set_desc(_("Time where waypoint is to be added"))
109 Action::WaypointSetSmart::is_candidate(const ParamList &x)
111 return (candidate_check(get_param_vocab(),x) &&
112 // We need an animated valuenode.
113 ValueNode_Animated::Handle::cast_dynamic(x.find("value_node")->second.get_value_node()) &&
114 // We need either a waypoint or a time.
115 (x.count("waypoint") || x.count("time")));
119 Action::WaypointSetSmart::set_param(const synfig::String& name, const Action::Param ¶m)
121 if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
123 value_node=ValueNode_Animated::Handle::cast_dynamic(param.get_value_node());
127 return static_cast<bool>(value_node);
129 if(name=="waypoint" && param.get_type()==Param::TYPE_WAYPOINT && !time_set)
131 waypoint=param.get_waypoint();
136 if(name=="time" && param.get_type()==Param::TYPE_TIME && waypoint.get_time()==(Time::begin()-1))
138 waypoint.set_time(param.get_time());
147 if(name=="model" && param.get_type()==Param::TYPE_WAYPOINTMODEL)
152 waypoint.apply_model(param.get_waypoint_model());
157 return Action::CanvasSpecific::set_param(name,param);
161 Action::WaypointSetSmart::is_ready()const
164 synfig::error("Missing value_node");
166 if(waypoint.get_time()==(Time::begin()-1))
167 synfig::error("Missing waypoint");
169 if(!value_node || waypoint.get_time()==(Time::begin()-1))
171 return Action::CanvasSpecific::is_ready();
174 // This function is called if a time is specified, but not
175 // a waypoint. In this case, we need to calculate the value
178 Action::WaypointSetSmart::calc_waypoint()
180 Time time=waypoint.get_time();
183 // Trivial case, we are sitting on a waypoint
184 waypoint=*value_node->find(waypoint.get_time());
188 waypoint=value_node->new_waypoint_at_time(time);
189 waypoint.set_before(synfigapp::Main::get_interpolation());
190 waypoint.set_after(synfigapp::Main::get_interpolation());
193 Time time=waypoint.get_time();
194 ValueNode_Animated::WaypointList &waypoint_list(value_node->waypoint_list());
195 ValueNode_Animated::WaypointList::iterator iter;
197 if(waypoint_list.empty())
199 waypoint.set_value((*value_node)(time));
203 ValueNode_Animated::WaypointList::iterator closest=waypoint_list.begin();
205 for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
207 const Real dist(abs(iter->get_time()-time));
208 if(dist<abs(closest->get_time()-time))
211 if(!closest->is_static())
212 waypoint.set_value_node(closest->get_value_node());
214 waypoint.set_value((*value_node)(time));
219 Action::WaypointSetSmart::enclose_waypoint(const synfig::Waypoint& waypoint)
221 times.insert(waypoint.get_time());
224 times.insert(value_node->find(waypoint)->get_time());
225 // synfig::info(__FILE__":%d: value_node->find(waypoint)->get_time()=%s",__LINE__,value_node->find(waypoint)->get_time().get_string().c_str());
228 // First we need to add any waypoints necessary to
229 // maintain the integrity of the keyframes.
230 if(get_edit_mode()&MODE_ANIMATE_PAST) try
232 Time curr_time(waypoint.get_time());
234 //while(value_node->waypoint_list().front().get_time()<=curr_time)
236 // Try to find prev keyframe
237 Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(curr_time));
238 curr_time=keyframe.get_time();
240 // synfig::info(__FILE__":%d: prev_keyframe->time=%s",__LINE__,keyframe.get_time().get_string().c_str());
241 // synfig::info(__FILE__":%d: waypoint->time=%s",__LINE__,waypoint.get_time().get_string().c_str());
243 if(times.count(keyframe.get_time()))
247 if(waypoint.get_time().is_equal(keyframe.get_time()))
252 times.insert(keyframe.get_time());
255 value_node->find(keyframe.get_time());
256 // synfig::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
258 catch(synfig::Exception::NotFound)
260 Action::Handle action(WaypointAdd::create());
262 action->set_param("canvas",get_canvas());
263 action->set_param("canvas_interface",get_canvas_interface());
264 action->set_param("value_node",ValueNode::Handle(value_node));
266 if(!value_node->waypoint_list().empty())
268 action->set_param("time",keyframe.get_time());
272 synfig::Waypoint tmp;
274 tmp.set_value(waypoint.get_value());
275 tmp.set_time(keyframe.get_time());
276 action->set_param("waypoint",tmp);
279 assert(action->is_ready());
280 if(!action->is_ready())
281 throw Error(Error::TYPE_NOTREADY);
287 catch(Error x) { throw x; }
288 catch(synfig::Exception::NotFound) { }
292 if(get_edit_mode()&MODE_ANIMATE_FUTURE)try
294 Time curr_time(waypoint.get_time());
296 //while(value_node->waypoint_list().back().get_time()>=curr_time)
299 // Try to find next keyframe
300 //synfig::info("FUTURE waypoint.get_time()=%s",waypoint.get_time().get_string().c_str());
301 Keyframe keyframe(*get_canvas()->keyframe_list().find_next(curr_time));
302 //synfig::info("FUTURE keyframe.get_time()=%s",keyframe.get_time().get_string().c_str());
303 curr_time=keyframe.get_time();
305 if(times.count(keyframe.get_time())|| waypoint.get_time().is_equal(keyframe.get_time()))
308 times.insert(keyframe.get_time());
312 value_node->find(keyframe.get_time());
313 synfig::info(__FILE__":%d: time=%s",__LINE__,keyframe.get_time().get_string().c_str());
314 synfig::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
317 catch(synfig::Exception::NotFound)
319 Action::Handle action(WaypointAdd::create());
321 action->set_param("canvas",get_canvas());
322 action->set_param("canvas_interface",get_canvas_interface());
323 action->set_param("value_node",ValueNode::Handle(value_node));
325 if(!value_node->waypoint_list().empty())
327 action->set_param("time",keyframe.get_time());
331 synfig::Waypoint tmp;
333 tmp.set_value(waypoint.get_value());
334 tmp.set_time(keyframe.get_time());
335 action->set_param("waypoint",tmp);
338 assert(action->is_ready());
339 if(!action->is_ready())
340 throw Error(Error::TYPE_NOTREADY);
346 catch(Error x) { throw x; }
347 catch(synfig::Exception::NotFound) { }
353 Action::WaypointSetSmart::prepare()
358 // First we need to add any waypoints necessary to
359 // maintain the integrity of the keyframes.
360 enclose_waypoint(waypoint);
364 //synfig::info("WaypointSetSmart: Move/Update?");
365 // Let's try to replace the old waypoint, if it exists
366 WaypointList::iterator iter(value_node->find(waypoint));
368 if(iter == value_node->waypoint_list().end())
371 enclose_waypoint(*iter);
373 Action::Handle action(WaypointSet::create());
375 action->set_param("canvas",get_canvas());
376 action->set_param("canvas_interface",get_canvas_interface());
377 action->set_param("value_node",ValueNode::Handle(value_node));
378 action->set_param("waypoint",waypoint);
380 assert(action->is_ready());
381 if(!action->is_ready())
382 throw Error(Error::TYPE_NOTREADY);
388 catch(synfig::Exception::NotFound){ } catch(int){ }
392 //synfig::info("WaypointSetSmart: Replace?");
393 // Check to see if a waypoint exists at this point in time
394 WaypointList::iterator iter=value_node->find(waypoint.get_time());
396 waypoint.mimic(*iter);
398 enclose_waypoint(*iter);
400 Action::Handle action(WaypointSet::create());
402 action->set_param("canvas",get_canvas());
403 action->set_param("canvas_interface",get_canvas_interface());
404 action->set_param("value_node",ValueNode::Handle(value_node));
405 action->set_param("waypoint",waypoint);
407 assert(action->is_ready());
408 if(!action->is_ready())
409 throw Error(Error::TYPE_NOTREADY);
415 catch(synfig::Exception::NotFound){ } catch(int){ }
419 //synfig::info("WaypointSetSmart: Add?");
420 // At this point we know that the old waypoint doesn't exist,
421 // so we need to create it.
422 Action::Handle action(WaypointAdd::create());
424 action->set_param("canvas",get_canvas());
425 action->set_param("canvas_interface",get_canvas_interface());
426 action->set_param("value_node",ValueNode::Handle(value_node));
427 action->set_param("waypoint",waypoint);
429 assert(action->is_ready());
430 if(!action->is_ready())
431 throw Error(Error::TYPE_NOTREADY);
437 catch(synfig::Exception::NotFound){ } catch(int){ }
439 throw Error(_("Unable to determine how to proceed. This is a bug."));